* { box-sizing: border-box; }
:root { --sidebar-width: 260px; }
body { font-family: Segoe UI, Roboto, Arial, sans-serif; margin: 0; color: #222; background: #f6f7f9; }
.topbar { position: sticky; top: 0; display: flex; align-items: center; justify-content: space-between; padding: 0 16px 0 0; height: 72px; background: #111827; color: white; z-index: 10; width: 100%; max-width: 100vw; }
.topbar .brand { font-weight: 600; display: flex; align-items: center; gap: 10px; height: 100%; }
.topbar .brand .brand-logo { height: 100%; width: auto; object-fit: contain; border-radius: 0; background: #fff; display: block; }
.topbar .brand span {
	color: #00e5ff; /* electric cyan */
	font-weight: 600; /* thinner than before */
	font-size: 28px; /* bigger */
	letter-spacing: 0.2px;
	/* Subtle reduced glow */
	text-shadow:
		0 0 2px #00e5ff,
		0 0 6px #00b7ff;
}
.topbar .actions button { margin-left: 8px; }
.icon-btn { background:#111827; color:#00e5ff; border:1px solid #00e5ff55; padding:8px; border-radius:8px; cursor:pointer; display:inline-flex; align-items:center; justify-content:center; box-shadow:0 0 6px rgba(0,229,255,0.4); }
.icon-btn:hover { background:#0b1321; color:#4ff4ff; border-color:#00e5ff; }
.icon-logout { display:block; }
/* Autosave label in top bar */
.topbar .actions label.autosave { display: inline-flex; align-items: center; gap: 6px; margin-left: 8px; color: #fff; font-size: 13px; }
/* Make main full-width and flush-left so sidebar can start at the very left */
main { max-width: none; margin: 18px 0 64px; padding: 0 16px 0 0; }
.status-row { max-width: none; margin: 10px 0 0; padding: 0 16px 0 0; }
.panel { background: white; border: 1px solid #e5e7eb; border-radius: 8px; padding: 16px; margin-bottom: 18px; box-shadow: 0 1px 2px rgba(0,0,0,0.04); }
.panel h2 { margin-top: 0; }
.grid { display: grid; gap: 12px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 800px){ .grid.two, .grid.three { grid-template-columns: 1fr; } }
label { display: block; font-size: 13px; color: #374151; margin: 6px 0; }
input[type=text], input[type=email], input[type=number], input[type=date], select, textarea { width: 100%; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 6px; background: #fff; }
textarea { resize: vertical; }
.hint { color: #6b7280; font-size: 12px; margin: 6px 0; }
.help { background: #f9fafb; border: 1px dashed #d1d5db; padding: 8px 10px; border-radius: 6px; margin: 8px 0; }
.add-row { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
button { background: #e5e7eb; border: none; border-radius: 6px; padding: 8px 12px; cursor: pointer; }
button.primary { background: #2563eb; color: white; }
button.danger { background: #ef4444; color: white; }
.block { border: 1px solid #e5e7eb; border-radius: 8px; padding: 12px; margin: 10px 0; background: #fafafa; position: relative; }
.block .block-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; padding-left: 26px; position: relative; min-height: 28px; }
.block .block-title { font-weight: 600; }
.block .block-head .block-title { position: absolute; top: 0; left: 50%; transform: translateX(-50%); width: calc(100% - 52px); text-align: center; pointer-events: none; }
.block .drag-handle { cursor: grab; user-select: none; font-size: 18px; line-height: 18px; color: #6b7280; position: absolute; top: 2px; left: 6px; }
.block.dragging { opacity: 0.6; }
.block .remove { background: transparent; color: #ef4444; font-size: 18px; line-height: 18px; padding: 0 8px; position: absolute; top: 2px; right: 6px; z-index: 1; }
.block .duplicate { font-size: 12px; padding: 6px 8px; background: #e5e7eb; color: #111827; }
/* Bottom-right grouped actions for each block */
.block .block-actions { position: absolute; right: 8px; bottom: 8px; display: inline-flex; gap: 6px; }
.block .block-actions .duplicate,
.block .block-actions .add-below { position: static; font-size: 12px; padding: 6px 8px; background: #e5e7eb; color: #111827; }

/* Ensure multiline equation content doesn't overlap bottom-right action buttons */
.block[data-type="multiline_equation"] { padding-bottom: 64px; }
/* When using unified equation block in multi mode, reserve bottom space too */
.block[data-type="equation"].mode-multi { padding-bottom: 64px; }
.block[data-type="table"] { padding-bottom: 64px; }

/* Align hint with bottom action buttons in multi-line equation blocks */
.block[data-type="multiline_equation"] .hint,
.block[data-type="equation"].mode-multi .hint {
	position: absolute;
	left: 12px;
	bottom: 10px;
	margin: 0;
}

/* Grey selection in equation inputs to indicate applied ranges (e.g., for \text{}) */
.block[data-type="equation"] .expr::selection,
.block[data-type="multiline_equation"] .lhs::selection,
.block[data-type="multiline_equation"] .rhs::selection {
	background: #e5e7eb;
	color: inherit;
}

/* Rich text editor toolbar */
.block .block-toolbar { display: flex; justify-content: center; align-items: center; gap: 6px; padding: 4px 0 8px; border-bottom: 1px solid #eee; margin-bottom: 8px; }
.block .block-toolbar button { background: #f3f4f6; padding: 4px 8px; border-radius: 4px; font-size: 12px; }
.block .block-toolbar button.toggle-btn.active { background: #dbeafe; border: 1px solid #93c5fd; color: #1e40af; }
.block .block-toolbar .sep { width: 1px; height: 18px; background: #e5e7eb; margin: 0 4px; }
/* Hide the toolbar specifically for citation blocks */
.block[data-type="citation"] .block-toolbar { display: none !important; }
.rte { min-height: 96px; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 6px; background: #fff; resize: vertical; overflow: auto; white-space: pre-wrap; word-break: break-word; }
.rte:focus { outline: 2px solid #93c5fd; }
.rte[contenteditable="true"]:empty:before { content: attr(data-placeholder); color: #9ca3af; }
.color-popover { background:#fff; border:1px solid #ddd; border-radius:8px; box-shadow:0 4px 16px rgba(0,0,0,0.15); padding:8px; min-width: 180px; }
.color-popover .row { display:flex; align-items:center; justify-content:space-between; gap:8px; margin:6px 0; }
.color-popover .row.rgb label { display:flex; align-items:center; gap:6px; font-size:12px; color:#374151; }
.color-popover .row.rgb input[type=number] { width:68px; padding:6px 8px; border:1px solid #d1d5db; border-radius:6px; }
.color-popover .actions { justify-content: flex-end; }
.color-popover button.primary { background:#2563eb; color:#fff; }
.panel.center { text-align: center; }
.result { margin-top: 10px; font-size: 14px; }
.switch-row { display: flex; align-items: center; gap: 10px; }
/* Compact controls inside layout rows so they fit on a single line */
.switch-row input[type=text],
.switch-row input[type=email],
.switch-row input[type=file],
.switch-row input[type=color],
.switch-row select {
	width: auto;
}
/* Let long text fields grow but cap their width so rows stay compact */
#wm_text { flex: 1 1 160px; max-width: 240px; }
#header_right_fields input[type=text] { flex: 1 1 200px; max-width: 280px; }
#footer_left_fields input[type=text],
#footer_right_fields input[type=text] { flex: 1 1 160px; max-width: 220px; }
#header_logo_fields { gap: 8px; }

/* Larger color swatches for header/footer options */
#header_right_color,
#footer_left_color,
#footer_right_color {
	width: 48px;
	height: 28px;
	padding: 0;
	border: 1px solid #cbd5e1;
	border-radius: 4px;
}
/* Highlight new page blocks clearly */
.block[data-type="new_page"] { border-color: #2563eb; background: #eef2ff; }
.block[data-type="new_page"] .block-title { color: #1e40af; }
.newpage-banner { border: 2px dashed #1e40af; background: #e0e7ff; color: #1e3a8a; padding: 10px 12px; border-radius: 6px; text-align: center; font-weight: 700; }
/* Small pill/chip for inline labels */
.chip { display: inline-block; background: #1d4ed8; color: #fff; font-weight: 600; border-radius: 999px; padding: 2px 8px; font-size: 12px; margin-left: 8px; vertical-align: middle; }

/* Reports gallery */
.gallery-wrapper { border: 2px solid #e5e7eb; border-radius: 12px; }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; padding: 8px; }
.tile { background: #fff; border: 1px solid #e5e7eb; border-radius: 12px; min-height: 160px; cursor: pointer; display: flex; align-items: center; justify-content: center; padding: 12px; transition: box-shadow .15s ease, transform .05s ease; }
.tile:hover { box-shadow: 0 6px 18px rgba(0,0,0,0.08); transform: translateY(-1px); }
.tile .tile-inner { width: 100%; text-align: center; }
.tile .tile-name { font-weight: 600; overflow: hidden; max-width: 100%; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-clamp: 2; }
.tile.new { background: #f9fafb; border-style: dashed; flex-direction: column; }
.tile .tile-plus { font-size: 48px; line-height: 1; color: #1f2937; }
.tile .tile-label { font-weight: 700; color: #374151; margin-bottom: 6px; }
/* Reports header within panel */
.gallery-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.gallery-actions button { margin-left: 8px; }

/* Layout with sidebar */
.layout { display: grid; grid-template-columns: var(--sidebar-width) 1fr; gap: 16px; align-items: stretch; transition: grid-template-columns 0.15s ease; }
.layout.collapsed { grid-template-columns: 24px 1fr; }
.sidebar { background: white; border: 1px solid #e5e7eb; border-radius: 8px; box-shadow: 0 1px 2px rgba(0,0,0,0.04); overflow: hidden; position: relative; z-index: 2; }
.sidebar .side-toggle { position: absolute; top: 8px; right: 8px; left: auto; font-weight: 700; cursor: pointer; opacity: 0.9; z-index: 3; padding: 2px 6px; border-radius: 6px; }
.sidebar .side-inner { height: 100%; display: flex; flex-direction: column; }
.sidebar .side-header { padding: 12px 12px 6px; font-weight: 600; border-bottom: 1px solid #e5e7eb; }
.sidebar .side-header { position: relative; padding-right: 36px; }
.sidebar .side-item { padding: 8px 12px; cursor: pointer; border-bottom: 1px solid #f3f4f6; display: flex; align-items: center; gap: 8px; }
.sidebar .side-item.main { background: #f9fafb; font-weight: 600; }
.sidebar .side-item.selected { background: #eef2ff; }
.sidebar .side-list { flex: 1; overflow-y: auto; overflow-x: hidden; }
.sidebar .side-actions { padding: 12px; display: grid; gap: 8px; border-top: 1px solid #e5e7eb; }
.sidebar .side-actions button { width: 100%; }
.sidebar .indent-1 { padding-left: 24px; }
.sidebar .indent-2 { padding-left: 40px; }
.sidebar .indent-3 { padding-left: 56px; }
.sidebar .indent-4 { padding-left: 72px; }
.sidebar .indent-5 { padding-left: 88px; }
.sidebar .badge { background:#e5e7eb; border-radius: 999px; font-size: 11px; padding: 2px 6px; }
.sidebar .level-btn { margin-left: auto; background: #eef2ff; color: #1e40af; font-weight: 800; padding: 2px 6px; border-radius: 4px; }
.sidebar .level-btn + .level-btn { margin-left: 4px; }
.sidebar .side-resizer { position: absolute; top: 0; right: 0; width: 6px; height: 100%; cursor: col-resize; background: linear-gradient(to right, rgba(0,0,0,0), rgba(0,0,0,0.06)); z-index: 2; }
.sidebar .side-resizer:hover { background: linear-gradient(to right, rgba(0,0,0,0), rgba(0,0,0,0.12)); }
/* Kebab and context menu */
.sidebar .kebab { margin-left: 4px; background: transparent; color: #6b7280; font-weight: 700; padding: 2px 6px; border-radius: 6px; }
.sidebar .kebab:hover { background: #f3f4f6; }
.context-menu { position: fixed; z-index: 1000; background: #fff; border: 1px solid #e5e7eb; border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,0.12); min-width: 180px; overflow: hidden; }
.context-menu .item { padding: 8px 12px; cursor: pointer; }
.context-menu .item:hover { background: #f3f4f6; }
.context-menu .item.danger { color: #b91c1c; }

.sidebar.collapsed { border-color: transparent; width: 24px; min-width: 24px; }
.sidebar.collapsed .side-inner { display: none; }
.sidebar.collapsed .side-resizer { display: none; }
.sidebar.collapsed .side-toggle { right: 2px; left: auto; opacity: 1; background: #111827; color: #fff; padding: 4px 6px; border-radius: 4px; }

@media (max-width: 1000px){
	.layout { grid-template-columns: var(--sidebar-width) 1fr; }
	.layout.collapsed { grid-template-columns: 24px 1fr; }
	.sidebar { position: relative; height: auto; }
}

/* Mobile-first responsive adjustments */
@media (max-width: 768px){
	:root { --sidebar-width: 240px; }
	.topbar { height: 56px; padding: 0 8px; }
	/* Prevent any horizontal overflow and ensure full-width banner */
	html, body { max-width: 100vw; overflow-x: hidden; }
	.topbar .brand { gap: 6px; }
	.topbar .brand .brand-logo { height: 56px; }
	.topbar .brand span { font-size: 20px; text-shadow: 0 0 1px #00e5ff, 0 0 3px #00b7ff; }
	/* Keep actions aligned to the right without horizontal scrolling */
	.topbar .actions { display: flex; flex: 1 1 auto; min-width: 0; flex-wrap: nowrap; gap: 6px; justify-content: flex-end; overflow: visible; }
	.topbar .actions::-webkit-scrollbar { display: none; }
	.topbar .actions button { font-size: 12px; padding: 6px 8px; margin-left: 0; white-space: nowrap; }
	/* On phones, show only kebab + logout + autosave inline */
	#submitBtn, #downloadBtn, #exportBtn, #manageBtn, #topToggleSidebar, #saveBtn { display: none; }
	#moreBtn { display: inline-flex; align-items: center; justify-content: center; padding: 6px 8px; border-radius: 8px; border: 1px solid #00e5ff55; background: #111827; color: #00e5ff; box-shadow: 0 0 6px rgba(0,229,255,0.4); }
	#moreBtn:hover { background:#0b1321; color:#4ff4ff; border-color:#00e5ff; }
	/* Ensure logout form doesn't push layout */
	.topbar .actions form { display: inline; margin: 0; }
	main { margin: 12px 0 48px; padding-right: 8px; }
	.status-row { padding-right: 8px; }
	.layout { grid-template-columns: 1fr; gap: 10px; }
	/* Off-canvas sidebar overlay on mobile */
	.sidebar { position: fixed; top: 56px; left: 0; bottom: 0; width: min(80vw, 320px); max-width: 90vw; height: calc(100vh - 56px); transform: translateX(-100%); transition: transform .2s ease; border-radius: 0 8px 8px 0; z-index: 50; }
	.sidebar .side-resizer { display: none; }
	.sidebar.collapsed { transform: translateX(-100%); }
	.sidebar:not(.collapsed) { transform: translateX(0); box-shadow: 4px 0 24px rgba(0,0,0,0.2); }
	/* Ensure content takes full width under topbar */
	.layout.collapsed { grid-template-columns: 1fr; }
	/* Block toolbar wraps nicely */
	.block .block-toolbar { flex-wrap: wrap; gap: 4px; }
	/* Prevent overlap: place block action buttons below content */
	.block .block-actions { position: static; margin-top: 8px; }
	.block { padding-bottom: 12px; }
	/* Ensure hints and inputs don't sit under floating elements */
	.block[data-type="multiline_equation"],
	.block[data-type="equation"].mode-multi,
	.block[data-type="table"] { padding-bottom: 12px; }
	.grid.two, .grid.three { grid-template-columns: 1fr; }
	/* Inputs inside switch rows wrap */
	.switch-row { flex-wrap: wrap; }
}

/* Kebab dropdown menu for mobile */
.topbar .more-menu { position: absolute; right: 8px; top: 56px; background: #ffffff; color:#111; border: 1px solid #e5e7eb; border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,0.12); min-width: 180px; z-index: 100; display: none; max-width: calc(100vw - 16px); max-height: calc(100vh - 64px); overflow-y: auto; }
.topbar .more-menu.open { display: block; }
.topbar .more-menu .item { padding: 10px 12px; cursor: pointer; border-bottom: 1px solid #f3f4f6; }
.topbar .more-menu .item:last-child { border-bottom: none; }
.topbar .more-menu .item:hover { background: #f9fafb; }

/* Hide kebab by default (desktop) */
#moreBtn { display: none; }

/* Re-enable kebab on phones (ensure overrides) */
@media (max-width: 768px){
	#moreBtn { display: inline-flex !important; }
}

/* Modal */
.modal { position: fixed; inset: 0; z-index: 50; }
.modal .modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.35); }
.modal .modal-dialog { position: relative; max-width: 420px; margin: 10vh auto 0; background: #fff; border-radius: 10px; border: 1px solid #e5e7eb; overflow: hidden; box-shadow: 0 10px 30px rgba(0,0,0,0.12); }
.modal .modal-header { font-weight: 700; padding: 12px 16px; border-bottom: 1px solid #e5e7eb; }
.modal .modal-body { padding: 12px 16px; display: grid; gap: 10px; }
.modal .modal-actions { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid #e5e7eb; }

/* Save toast */
.toast { position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); background: rgba(17,24,39,0.95); color: #fff; padding: 10px 14px; border-radius: 8px; z-index: 60; font-weight: 600; box-shadow: 0 10px 30px rgba(0,0,0,0.2); opacity: 0; pointer-events: none; transition: opacity .2s ease; }
.toast.show { opacity: 1; }

/* Error highlight for problematic blocks */
.error-block { border: 2px solid #dc2626 !important; background: #fef2f2; border-radius: 8px; }
.block-error-hint { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; padding: 6px 8px; border-radius: 6px; margin: 8px 0; font-size: 13px; }

/* Table grid (graphical editor) */
.table-grid { border: 1px dashed #d1d5db; border-radius: 6px; padding: 8px; min-height: 120px; position: relative; background: #fafafa; }
.table-grid .table-placeholder { color: #6b7280; font-size: 12px; }
.table-grid .grid { display: grid; gap: 0; }
.tbl-cell { border: 1px solid #d1d5db; background: #fff; min-height: 28px; position: relative; }
.tbl-cell .rte-cell { min-height: 24px; padding: 6px 8px; border: none; outline: none; }
.tbl-cell.sel { outline: 2px solid #2563eb; outline-offset: -2px; }
.tbl-cell.hidden { display: none; }
.tbl-cell.header { background: #e5e7eb !important; }
.tbl-cell.header .rte-cell { background: transparent; }
/* Selection mode UX for tables */
.block[data-type="table"].tbl-select-mode .table-grid .tbl-cell { cursor: pointer; }
.block[data-type="table"].tbl-select-mode .table-grid .rte-cell { pointer-events: none; }
/* Table add handles */
.table-grid .tbl-handle { position: absolute; background: #2563eb; color: #fff; border-radius: 4px; padding: 2px 6px; font-size: 12px; cursor: pointer; opacity: 0.9; display: none; box-shadow: 0 2px 6px rgba(0,0,0,0.15); }
.table-grid:hover .tbl-handle { display: block; }
.table-grid .tbl-handle.right { top: 50%; right: 6px; transform: translateY(-50%); }
.table-grid .tbl-handle.bottom { left: 50%; bottom: 6px; transform: translateX(-50%); }
/* In-between insert handles */
.table-grid .tbl-ins-handle { position: absolute; z-index: 2; background: #2563eb; color: #fff; border: none; border-radius: 999px; font-size: 12px; line-height: 1; padding: 4px 6px; cursor: pointer; box-shadow: 0 2px 8px rgba(0,0,0,0.2); opacity: 0.0; transition: opacity .12s ease, transform .12s ease; transform: translate(-50%, -50%); }
.table-grid:hover .tbl-ins-handle { opacity: 0.95; }
.table-grid .tbl-ins-handle.vertical::after { content: "＋"; }
.table-grid .tbl-ins-handle.horizontal::after { content: "＋"; }
.table-grid .tbl-ins-handle.vertical { width: 22px; height: 22px; top: 0; }
.table-grid .tbl-ins-handle.horizontal { width: 22px; height: 22px; left: 0; }

/* Toolbar active state and Mono button styling */
.block-toolbar button.active {
	background: #dbeafe;
	color: #1e40af;
	border-color: #60a5fa;
}

/* Inline cross-reference token */
.xref {
	display:inline-block;
	padding:2px 6px;
	margin:0 2px;
	border:1px solid #c7d2fe;
	background:#eef2ff;
	color:#1e3a8a;
	border-radius:6px;
	font-size:0.9em;
	line-height:1.2;
}
.xref[contenteditable="false"] { cursor:default; }

/* Inline citation token — styled the same as xref */
.citation {
	display:inline-block;
	padding:2px 6px;
	margin:0 2px;
	border:1px solid #c7d2fe;
	background:#eef2ff;
	color:#1e3a8a;
	border-radius:6px;
	font-size:0.9em;
	line-height:1.2;
}
.citation[contenteditable="false"] { cursor:default; }

/* Floating xref chooser */
.xref-chooser { position:absolute; z-index:1000; background:#fff; border:1px solid #e5e7eb; border-radius:8px; box-shadow:0 8px 20px rgba(0,0,0,0.12); padding:8px; min-width:260px; max-width:360px; }
.xref-chooser .types { display:flex; gap:6px; margin-bottom:6px; }
.xref-chooser .types button { padding:6px 10px; border:1px solid #ddd; border-radius:6px; background:#fff; cursor:pointer; }
.xref-chooser .types button.active { background:#eef2ff; border-color:#c7d2fe; }
.xref-chooser .list { max-height:220px; overflow:auto; border:1px solid #eee; border-radius:6px; }
.xref-chooser .list .item { padding:6px 8px; cursor:pointer; border-bottom:1px solid #f3f4f6; }
.xref-chooser .list .item:hover { background:#f9fafb; }
.xref-chooser .search { width:100%; padding:6px 8px; border:1px solid #ddd; border-radius:6px; margin:6px 0; }
