:root {
  --blue: #00003c;
  --orange: #cc6600;
  --bg: #f4f5f8;
  --card: #fff;
  --line: #e3e6ec;
  --text: #1a1d29;
  --muted: #6b7280;
  --green: #138a52;
  --red: #c0392b;
  --amber: #b8860b;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 1px 3px rgba(16,24,40,.06);
  --shadow-md: 0 6px 18px rgba(16,24,40,.10);
}
* { box-sizing: border-box; }
body {
  margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text); line-height: 1.55;
  -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;
}
::selection { background: rgba(204,102,0,.18); }
a { color: var(--blue); }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: linear-gradient(180deg, #0a0a52 0%, var(--blue) 100%); color: #fff; padding: 13px 28px;
  box-shadow: 0 2px 10px rgba(0,0,0,.14); position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 16px; }
.brand strong { font-weight: 700; }
.logo-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--orange); display: inline-block; }
.nav { display: flex; align-items: center; gap: 18px; font-size: 14px; }
.nav a { color: #cdd2e0; text-decoration: none; }
.nav a:hover { color: #fff; }
.nav .who { color: #fff; font-weight: 600; }
.btn-link { color: #fff !important; }
.container { max-width: none; margin: 28px 0; padding: 0 32px; }
h1 { font-size: 24px; margin: 0 0 16px; color: var(--blue); }
h2 { font-size: 19px; margin: 0 0 6px; color: var(--blue); }
h3 { font-size: 16px; margin: 0 0 12px; color: var(--blue); }
h4 { font-size: 15px; margin: 0; }
.card {
  background: var(--card); border: 1px solid #e9ecf2; border-radius: var(--radius);
  padding: 22px; margin-bottom: 20px; box-shadow: var(--shadow);
}
.card.highlight { border-left: 4px solid var(--orange); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.cycle-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
label { display: block; margin-bottom: 14px; font-size: 14px; font-weight: 600; }
input, select, textarea {
  width: 100%; padding: 9px 11px; margin-top: 5px; border: 1px solid #d6dae3;
  border-radius: 8px; font-size: 14px; font-weight: 400; font-family: inherit; background: #fff;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(0,0,60,.08);
}
textarea { resize: vertical; line-height: 1.55; }
.check { display: flex; align-items: center; gap: 8px; }
.check input { width: auto; margin: 0; }
.btn {
  display: inline-block; background: var(--orange); color: #fff; border: none; cursor: pointer;
  padding: 10px 18px; border-radius: 8px; font-size: 14px; font-weight: 600; text-decoration: none;
  box-shadow: 0 1px 2px rgba(0,0,0,.10); transition: filter .15s, box-shadow .15s, transform .04s;
}
.btn:hover { filter: brightness(1.06); box-shadow: var(--shadow-md); }
.btn:active { transform: translateY(1px); }
.btn.ghost { background: #fff; color: var(--blue); border: 1px solid #d6dae3; box-shadow: none; }
.btn.ghost:hover { background: #f3f4f8; box-shadow: none; }
.btn.warn-btn { background: var(--red); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row form { margin: 0; }
.link-btn { background: none; border: none; color: var(--blue); cursor: pointer; font-size: 13px; padding: 2px 4px; }
.link-btn.danger { color: var(--red); }
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 18px; }
.grid > div { align-self: end; margin-bottom: 14px; }
.edit-row { border-top: 1px solid var(--line); padding-top: 14px; grid-template-columns: repeat(5, 1fr); }
.pill { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill.green { background: #e3f5ec; color: var(--green); }
.pill.red { background: #fdecea; color: var(--red); }
.pill.grey { background: #eef0f4; color: var(--muted); }
.pill.amber { background: #fbf3df; color: var(--amber); }
.pill.blue { background: #e6e9f7; color: var(--blue); }
.alert { padding: 11px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 14px; }
.alert.ok { background: #e3f5ec; color: var(--green); }
.alert.err { background: #fdecea; color: var(--red); }
.alert.warn { background: #fbf3df; color: var(--amber); }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); }
.table th { color: var(--muted); font-weight: 600; font-size: 13px; }
.submission { border-top: 1px solid var(--line); padding-top: 14px; margin-top: 14px; }
.submission:first-of-type { border-top: none; }
/* Section header with live status summary */
.section-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.section-title { margin: 0 0 3px; font-size: 18px; }
.section-sub { margin: 0; color: var(--muted); font-size: 13px; max-width: 560px; }
.section-summary { display: flex; gap: 8px; flex-wrap: wrap; }
.sum-badge { display: inline-flex; align-items: center; gap: 7px; background: #fff; border: 1px solid #e9ecf2; border-radius: 999px; padding: 6px 13px; font-size: 12.5px; color: var(--muted); box-shadow: var(--shadow); }
.sum-badge strong { color: var(--text); font-weight: 700; }
.sum-badge::before { content: ""; width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.sum-badge.green::before { background: var(--green); }
.sum-badge.amber::before { background: var(--amber); }
.sum-badge.grey::before { background: #c3c7d0; }

/* Segmented-control style department tabs */
.tabs { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 22px; padding: 5px; background: #eaedf3; border-radius: 12px; }
.tab {
  background: transparent; border: none; cursor: pointer; border-radius: 9px;
  padding: 9px 15px; font-size: 13px; font-weight: 600; color: var(--muted);
  display: inline-flex; align-items: center; gap: 8px;
  transition: background .15s, color .15s, box-shadow .15s;
}
.tab:hover { color: var(--blue); background: rgba(255,255,255,.55); }
.tab.tab-active { background: #fff; color: var(--blue); box-shadow: var(--shadow); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }
.dot.green { background: var(--green); }
.dot.amber { background: var(--amber); }
.dot.grey { background: #c3c7d0; }
.legend { margin-left: 8px; white-space: nowrap; }
.legend .dot { margin: 0 2px 0 8px; }
.ai-panel { margin-top: 10px; padding: 12px 14px; background: #f7f8fb; border: 1px solid var(--line); border-radius: 8px; }
.ai-presets { gap: 8px; margin-bottom: 8px; }
.chip { background: #fff; border: 1px solid var(--line); border-radius: 999px; padding: 5px 12px; font-size: 12px; font-weight: 600; color: var(--blue); cursor: pointer; }
.chip:hover { background: #eef0f7; }
.chip.selected { background: var(--blue); color: #fff; border-color: var(--blue); }
.chip.selected::before { content: "✓ "; }
.ai-instruction { width: 100%; resize: vertical; font-family: inherit; }
.ai-actions { margin-top: 8px; gap: 10px; }
.file-btn { display: inline-flex; align-items: center; gap: 6px; background: #fff; border: 1px solid var(--line); border-radius: 7px; padding: 8px 12px; font-size: 13px; font-weight: 600; color: var(--blue); cursor: pointer; margin: 0; }
.file-btn:hover { background: #eef0f7; }
.ai-file-name { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-file-clear { color: var(--red); text-decoration: none; margin-left: 4px; font-weight: 700; }
.ai-result { margin-top: 12px; }
.ai-result h5 { margin: 0 0 6px; font-size: 14px; color: var(--blue); }
.ai-status { margin-top: 8px; }
.err-text { color: var(--red); }
.view-toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.seg { background: #fff; border: none; padding: 5px 12px; font-size: 12px; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg + .seg { border-left: 1px solid var(--line); }
.seg-active { background: var(--blue); color: #fff; }
.ai-diff {
  border: 1px solid var(--line); border-radius: 7px; padding: 12px 14px; background: #fff;
  white-space: pre-wrap; word-wrap: break-word; font-size: 13px; line-height: 1.6;
  max-height: 420px; overflow: auto;
}
.ai-diff .d-ins { background: #d8f5e3; color: #0a6b3a; text-decoration: none; }
.ai-diff .d-del { background: #fde0dd; color: #b3261e; text-decoration: line-through; }
.diff-fallback { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.diff-fallback pre { white-space: pre-wrap; background: #f7f8fa; padding: 8px; border-radius: 6px; font-size: 12px; }
.latex-block { margin-top: 12px; }
.latex-editor { margin-top: 8px; }
.latex-area {
  width: 100%; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px; line-height: 1.5; tab-size: 2; background: #1a1d29; color: #e6e6e6;
  border-color: #2a2d3a;
}
.pdf-box { margin-top: 10px; border: 1px solid #e9ecf2; border-radius: 10px; overflow: hidden; box-shadow: var(--shadow); }
.pdf-bar { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; background: #f3f4f8; border-bottom: 1px solid #e9ecf2; }
.pdf-title { font-size: 13px; font-weight: 600; color: var(--blue); }
.pdf-close { background: #fff; border: 1px solid #d6dae3; border-radius: 6px; padding: 4px 11px; font-size: 12px; font-weight: 600; color: var(--muted); cursor: pointer; }
.pdf-close:hover { background: #fdecea; color: var(--red); border-color: #f0c4be; }
.pdf-status { padding: 8px 12px; }
.pdf-status:empty { display: none; }
.pdf-frame { width: 100%; height: 560px; border: none; background: #fff; display: block; }
.past-item { border: 1px solid var(--line); border-radius: 8px; margin-bottom: 8px; background: #fff; }
.past-item summary { display: flex; align-items: center; gap: 12px; padding: 11px 14px; cursor: pointer; list-style: none; font-size: 14px; }
.past-item summary::-webkit-details-marker { display: none; }
.past-item summary::before { content: "▸"; color: var(--muted); transition: transform .15s; }
.past-item[open] summary::before { transform: rotate(90deg); }
.past-item[open] summary { border-bottom: 1px solid var(--line); }
.past-cycle { font-weight: 600; color: var(--blue); }
.past-date { margin-left: auto; }
.past-body { padding: 14px; }
.note-box { background: #fff7ed; border: 1px solid #f6d8b0; border-left: 4px solid var(--orange); border-radius: 8px; padding: 12px 14px; margin: 4px 0 14px; }
.note-title { font-size: 13px; font-weight: 700; color: var(--orange); margin-bottom: 6px; }
.note-body { font-size: 14px; color: var(--text); white-space: pre-wrap; }
.tab-note { margin-left: 2px; font-size: 12px; }
.attach-block { margin-top: 12px; }
.attach-grid { display: flex; flex-wrap: wrap; gap: 12px; margin: 8px 0; }
.attach { width: 150px; border: 1px solid var(--line); border-radius: 8px; padding: 6px; background: #fff; }
.attach img { width: 100%; height: 96px; object-fit: cover; border-radius: 5px; display: block; }
.attach-file { display: block; height: 96px; line-height: 96px; text-align: center; background: #f3f4f8; border-radius: 5px; font-size: 13px; overflow: hidden; }
.attach-tag { margin-top: 5px; font-size: 11px; color: var(--muted); }
.attach-tag code { background: #eef0f4; padding: 1px 5px; border-radius: 4px; user-select: all; font-size: 11px; }
.formatted .img-ph, .img-ph { background: #eef0f7; border: 1px dashed #b9c0d6; border-radius: 6px; padding: 8px 10px; color: var(--blue); font-size: 13px; text-align: center; }
.attach-meta { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin-top: 5px; }
.attach-meta span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-upload { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 8px; }
.attach-upload input[type=text] { flex: 1; min-width: 160px; }
.attach-upload input[type=file] { font-size: 13px; }
.help { margin: 10px 0; font-size: 13px; color: var(--muted); }
.help code { background: #eef0f4; padding: 1px 5px; border-radius: 4px; }
.editor-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
.editor-grid textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.preview-pane { position: sticky; top: 16px; }
.preview-pane h3 { margin-bottom: 8px; }
.formatted {
  border: 1px solid var(--line); border-radius: 8px; padding: 16px 18px; background: #fbfbfd;
  min-height: 280px; max-height: 520px; overflow: auto;
}
.formatted h4 { color: var(--blue); font-size: 15px; margin: 14px 0 6px; font-weight: 700; }
.formatted h4:first-child { margin-top: 0; }
.formatted ul { margin: 4px 0 10px; padding-left: 20px; }
.formatted li { margin: 3px 0; font-size: 14px; }
.formatted p { margin: 6px 0; font-size: 14px; }
.formatted strong { color: #111; }
.formatted .md-table { border-collapse: collapse; width: 100%; margin: 10px 0; font-size: 13px; }
.formatted .md-table th { background: var(--blue); color: #fff; text-align: left; padding: 6px 9px; border: 1px solid var(--blue); }
.formatted .md-table td { padding: 6px 9px; border: 1px solid var(--line); vertical-align: top; }
@media (max-width: 760px) { .editor-grid { grid-template-columns: 1fr; } .preview-pane { position: static; } }
.preview { margin-top: 16px; }
.preview pre, pre.readonly { background: #1a1d29; color: #e6e6e6; padding: 14px; border-radius: 8px; overflow: auto; font-size: 13px; white-space: pre-wrap; }
pre.readonly { background: #f7f8fa; color: var(--text); border: 1px solid var(--line); }
.hidden { display: none; }
.auth-card {
  max-width: 380px; margin: 60px auto; background: #fff; border: 1px solid var(--line);
  border-radius: 12px; padding: 32px;
}
.auth-card h1 { margin-bottom: 4px; }
.auth-card.wide { max-width: 460px; }
.qr-wrap { text-align: center; margin: 18px 0; }
.qr-wrap img { border: 1px solid var(--line); border-radius: 8px; padding: 8px; background: #fff; }
pre.key { font-size: 15px; letter-spacing: 1px; text-align: center; user-select: all; }
.foot { text-align: center; color: var(--muted); font-size: 12px; padding: 24px; }
@media (max-width: 640px) {
  .grid, .edit-row { grid-template-columns: 1fr; }
  .nav { gap: 12px; font-size: 13px; flex-wrap: wrap; }
}

/* ---- Section builder (head report editor) -------------------------------- */
.btn.sm { padding: 6px 11px; font-size: 13px; }
.tabs { display: flex; gap: 4px; margin-bottom: 12px; border-bottom: 1px solid var(--line); }
.tab {
  background: none; border: none; border-bottom: 2px solid transparent; cursor: pointer;
  padding: 8px 14px; font-size: 14px; color: var(--muted); margin-bottom: -1px;
}
.tab:hover { color: var(--blue); }
.tab.active { color: var(--blue); font-weight: 600; border-bottom-color: var(--orange); }

.builder-toolbar { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.blocks { display: flex; flex-direction: column; gap: 6px; min-height: 40px; }

.block {
  display: flex; align-items: flex-start; gap: 6px; background: #fff; border: 1px solid var(--line);
  border-left: 3px solid #c9cede; border-radius: 8px; padding: 6px 8px;
}
.block.dragging { opacity: .45; }
.block.over { border-color: var(--orange); box-shadow: 0 0 0 2px rgba(204,102,0,.15); }
.block-section { border-left-color: var(--blue); }
.block-subsection { border-left-color: #3a4a8c; }
.block-subsubsection { border-left-color: #6b78ae; }
.block-paragraph { border-left-color: #9aa2b8; }
.block-bullet { border-left-color: var(--orange); }
.block.indent-1 { margin-left: 26px; }
.block.indent-2 { margin-left: 52px; }
.block.indent-3 { margin-left: 78px; }

.grip { cursor: grab; color: #aab; user-select: none; padding: 4px 2px; font-size: 15px; line-height: 1.6; }
.block-type {
  flex: 0 0 auto; width: auto; font-size: 12px; padding: 5px 6px; margin: 0; border-radius: 6px;
  color: var(--blue); background: #f3f4f8;
}
.block-text {
  flex: 1 1 auto; font-size: 14px; padding: 6px 8px; margin: 0; border-radius: 6px; resize: none;
  overflow: hidden; min-height: 34px; font-family: inherit;
}
.block-btn {
  flex: 0 0 auto; background: #f3f4f8; border: 1px solid #dfe3ec; color: var(--blue); cursor: pointer;
  border-radius: 6px; width: 30px; height: 32px; font-size: 15px; line-height: 1;
}
.block-btn:hover { background: #e9ecf4; }
.block-btn.del { color: var(--red); }
.block-btn.nest { font-size: 14px; }

.preview-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.pdf-frame { width: 100%; height: 620px; border: 1px solid var(--line); border-radius: 8px; background: #f7f8fa; margin-top: 6px; }
#pdfWrap { margin-top: 16px; }

/* Live preview styled to resemble the compiled report */
.report-like { border: 1px solid var(--line); border-radius: 8px; padding: 16px 18px; background: #fff; }
.formatted h5, .report-like h5 { color: var(--blue); font-size: 14px; font-weight: 700; margin: 12px 0 5px; }
.formatted h6, .report-like h6 { color: var(--blue); font-size: 13px; font-weight: 700; margin: 10px 0 4px; }
.report-like .rh-sub { padding-left: 10px; }
.report-like .rh-subsub { padding-left: 20px; font-style: normal; }
.formatted ul ul { margin: 2px 0 2px; }

/* Insert palette (drag-to-insert toolbar above the textarea) */
.palette { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 8px; }
.palette-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-right: 2px; }
.chip {
  background: #f3f4f8; border: 1px solid #d6dae3; color: var(--blue); cursor: grab;
  border-radius: 999px; padding: 5px 12px; font-size: 13px; font-weight: 600; user-select: none;
}
.chip:hover { background: #e9ecf4; box-shadow: var(--shadow); }
.chip:active { cursor: grabbing; transform: translateY(1px); }
.chip-table { background: var(--blue); color: #fff; border-color: var(--blue); cursor: pointer; }
.chip-table:hover { filter: brightness(1.1); background: var(--blue); }

/* Modal (table designer) */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(16,24,40,.45); display: flex; align-items: center;
  justify-content: center; z-index: 50; padding: 20px;
}
.modal-backdrop[hidden] { display: none; } /* keep the hidden attribute authoritative */
.modal { background: #fff; border-radius: 14px; box-shadow: var(--shadow-md); width: 100%; max-width: 520px; overflow: hidden; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; }
.modal-body { padding: 18px 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--line); background: #fafbfd; }
.modal-fields { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-end; margin-bottom: 14px; }
.modal-fields label { font-size: 13px; color: var(--text); }
.modal-fields input[type=number] { width: 80px; margin-top: 4px; }
.check { display: inline-flex; align-items: center; gap: 7px; }
.check input { width: auto; margin: 0; }
.swatch-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px var(--line); cursor: pointer; padding: 0; }
.swatch.active { box-shadow: 0 0 0 2px var(--orange); }
.swatch-custom { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; border: 1px dashed #c3c9d6; border-radius: 8px; cursor: pointer; font-size: 14px; }
.swatch-custom input[type=color] { width: 0; height: 0; opacity: 0; position: absolute; }
.width-row { margin-bottom: 14px; }
.width-inputs { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.width-inputs input[type=number] { width: 56px; }
.tbl-preview { border: 1px solid var(--line); border-radius: 8px; padding: 12px; background: #fff; overflow-x: auto; }
.tbl-preview .md-table { margin: 0; }
.md-table td, .md-table th { overflow-wrap: anywhere; }

/* Admin section editor (one editor, Section|LaTeX toggle) */
.sec-editor { margin-bottom: 8px; }
.sec-editor .edit-mode { align-items: center; gap: 10px; margin-bottom: 8px; }
.sec-editor textarea { width: 100%; }
.sec-editor .sec-latex { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.sec-restore-form { margin: 0; display: inline-flex; margin-left: auto; }
