:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1c2733;
  --muted: #5b6b7c;
  --line: #dde4ec;
  --accent: #0e6ba8;
  --accent-dark: #0a5486;
  --good: #1a7f4f;
  --warn: #b07a1a;
  --bad: #b03030;
  --chip: #eef3f8;
  font-size: 15px;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  display: flex; flex-direction: column; min-height: 100vh;
}

/* ---- top bar ---- */
#topbar {
  display: flex; align-items: center; gap: 18px;
  padding: 10px 20px;
  background: #12354e; color: #fff;
}
.brand-title { font-size: 1.15rem; font-weight: 700; letter-spacing: .02em; }
.brand-sub { font-size: .72rem; opacity: .75; }
.topbar-study { flex: 1; font-size: .9rem; opacity: .9; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; gap: 8px; }

/* ---- tabs ---- */
#tabs {
  display: flex; gap: 2px; padding: 0 20px;
  background: #12354e;
}
#tabs[hidden] { display: none; }

#question-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 20px; background: #eaf2f9; border-bottom: 1px solid #cbdeee;
}
#question-bar[hidden] { display: none; }
#question-bar .qb-label { font-size: .8rem; font-weight: 600; color: #12354e; white-space: nowrap; }
#question-bar select { max-width: 900px; font-size: .85rem; }
.tab {
  border: none; background: transparent; color: #b9cbd9;
  padding: 9px 14px; cursor: pointer; font-size: .85rem;
  border-radius: 8px 8px 0 0;
}
.tab:hover { color: #fff; }
.tab.active { background: var(--bg); color: var(--ink); font-weight: 600; }

/* ---- layout ---- */
#content { flex: 1; padding: 18px 20px 40px; max-width: 1500px; width: 100%; margin: 0 auto; }
.panel { display: none; }
.panel.active { display: block; }
h2 { margin: 4px 0 6px; font-size: 1.25rem; }
h3 { margin: 0 0 10px; font-size: .98rem; }
.help { color: var(--muted); font-size: .85rem; margin: 4px 0 14px; max-width: 950px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 14px 16px; margin-bottom: 14px;
}
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: start; }
.split { display: flex; gap: 14px; align-items: flex-start; }
.split .grow { flex: 1; min-width: 0; }
#thematic-reader { width: 320px; flex-shrink: 0; }
.row { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.row input[type=text] { flex: 1; }

/* ---- controls ---- */
label { display: block; font-size: .82rem; color: var(--muted); margin-bottom: 12px; }
input[type=text], input[type=email], input[type=tel], input[type=search], input[type=password],
input[type=date], textarea, select {
  width: 100%; padding: 7px 9px; margin-top: 3px;
  border: 1px solid var(--line); border-radius: 7px;
  font: inherit; color: var(--ink); background: #fff;
}
.row input[type=email], .row input[type=tel], .row input[type=search], .row input[type=password] { flex: 1; }
textarea { resize: vertical; }
.btn {
  border: 1px solid var(--accent); background: var(--accent); color: #fff;
  padding: 7px 13px; border-radius: 7px; cursor: pointer; font-size: .84rem;
  white-space: nowrap;
}
.btn:hover { background: var(--accent-dark); }
.btn.ghost { background: transparent; color: inherit; border-color: currentColor; opacity: .85; }
.btn.ghost:hover { opacity: 1; }
#topbar .btn.ghost { color: #fff; border-color: #ffffff66; }
.btn.primary { background: #2e9e6b; border-color: #2e9e6b; font-weight: 600; }
.btn.primary:hover { background: #24875a; }
.btn.tiny { padding: 2px 8px; font-size: .75rem; }
.btn.danger { background: transparent; border-color: var(--bad); color: var(--bad); }
.btn:disabled, .btn:disabled:hover { opacity: .45; cursor: not-allowed; background: var(--accent); }
.btn.ghost:disabled, .btn.danger:disabled { background: transparent; }
.pill {
  display: inline-block; background: var(--chip); border-radius: 20px;
  font-size: .72rem; padding: 2px 9px; color: var(--muted); font-weight: 500;
}

/* ---- tables ---- */
.scroll-x { overflow-x: auto; }
.tbl { border-collapse: collapse; width: 100%; font-size: .83rem; }
.tbl th, .tbl td { border: 1px solid var(--line); padding: 5px 8px; text-align: left; vertical-align: middle; }
.tbl th { background: #eef3f8; font-weight: 600; position: sticky; top: 0; }
.tbl td.c, .tbl th.c { text-align: center; }
.tbl td.num { text-align: right; font-variant-numeric: tabular-nums; }
.tbl tbody tr:nth-child(even) { background: #fafbfd; }
.matrix td.cell {
  text-align: center; cursor: pointer; min-width: 34px; padding: 2px;
  user-select: none;
}
.matrix td.cell input {
  width: 40px; border: none; background: transparent; text-align: center; font: inherit; padding: 4px 0;
}
.matrix td.cell input:focus { outline: 2px solid var(--accent); border-radius: 4px; }
.matrix td.cell.nonzero { background: #e4f0e9; font-weight: 600; }
.matrix td.stmt { min-width: 260px; max-width: 380px; }
.matrix tfoot td, .tbl .totalrow td { background: #eef3f8; font-weight: 600; }
.grade { display: inline-block; min-width: 20px; text-align: center; border-radius: 5px; padding: 1px 7px; font-weight: 600; font-size: .78rem; }
.g1 { background: #d2ecdd; color: #14532d; }
.g2 { background: #e2f0d5; color: #365314; }
.g3 { background: #fdf0d2; color: #713f12; }
.g4 { background: #fde0d2; color: #7c2d12; }
.g5 { background: #fbd5d5; color: #7f1d1d; }
.action-utilise { color: var(--good); font-weight: 600; }
.action-investigate { color: var(--warn); font-weight: 600; }
.action-monitor { color: var(--bad); font-weight: 600; }

/* report matrix */
.report th, .report td { text-align: center; }
.report td.desc { text-align: left; font-size: .78rem; color: var(--muted); }
.report .sect { background: #12354e; color: #fff; font-weight: 700; }

/* ---- misc ---- */
.rq-banner {
  background: #eaf2f9; border: 1px solid #cbdeee; border-left: 4px solid var(--accent);
  padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; font-size: .9rem;
}
.resp-card { margin-bottom: 10px; }
.resp-card textarea { width: 100%; }
.resp-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 4px; }
.resp-head .who { font-weight: 600; font-size: .88rem; }
.resp-head .meta { font-size: .74rem; color: var(--muted); }
.reader-text {
  white-space: pre-wrap; font-size: .85rem; line-height: 1.5;
  background: #fbfcfe; border: 1px solid var(--line); border-radius: 8px;
  padding: 10px; margin-top: 8px; max-height: 260px; overflow-y: auto;
}

/* thematic coding list */
#coding-list { max-height: 340px; overflow-y: auto; }
.code-item {
  display: flex; align-items: center; gap: 8px;
  border: 1px solid var(--line); border-radius: 8px;
  padding: 5px 8px; margin-bottom: 5px; cursor: pointer;
  font-size: .8rem; user-select: none; background: #fff;
}
.code-item:hover { border-color: var(--accent); }
.code-item.on { background: #e4f0e9; border-color: #9fd0b3; }
.code-check {
  width: 18px; height: 18px; flex-shrink: 0; border-radius: 5px;
  border: 1px solid var(--line); background: #fff;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: .7rem; font-weight: 700; color: #14532d;
}
.code-item.on .code-check { background: #1a7f4f; border-color: #1a7f4f; color: #fff; }
.code-stmt { flex: 1; line-height: 1.3; }
.code-ctrls { display: inline-flex; align-items: center; gap: 4px; flex-shrink: 0; }
.code-count { min-width: 18px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.code-ctrls .btn { padding: 0 7px; line-height: 1.4; }
.verdict { margin-top: 10px; font-size: .9rem; font-weight: 600; }
.verdict.ok { color: var(--good); }
.verdict.no { color: var(--bad); }
.legend { font-size: .78rem; color: var(--muted); line-height: 1.7; }
.legend b { color: var(--ink); }

#statusbar {
  display: flex; justify-content: space-between;
  padding: 6px 20px; font-size: .75rem; color: var(--muted);
  border-top: 1px solid var(--line); background: #fff;
}
ol#final-items li, ol#excluded-items li { font-size: .85rem; margin-bottom: 4px; }

@media (max-width: 1000px) {
  .grid-2 { grid-template-columns: 1fr; }
  .split { flex-direction: column; }
  #thematic-reader { width: 100%; }
}

/* print survey */
@media print {
  #topbar, #tabs, #statusbar, .panel { display: none !important; }
  #print-area { display: block !important; }
}
#print-area { display: none; padding: 30px; font-size: 12pt; }
#print-area table { border-collapse: collapse; width: 100%; margin-top: 14px; }
#print-area th, #print-area td { border: 1px solid #444; padding: 6px 8px; font-size: 10pt; }

/* AI suggestion panel */
#ai-suggest-panel {
  border: 1px solid #cbdeee; background: #f3f8fc; border-radius: 10px;
  padding: 12px 14px; margin-bottom: 12px;
}
#ai-suggest-panel[hidden] { display: none; }
#ai-suggest-panel h4 { margin: 0 0 8px; font-size: .92rem; }
.ai-sugg {
  display: flex; align-items: flex-start; gap: 10px;
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; margin-bottom: 6px;
}
.ai-sugg.rejected { opacity: .45; }
.ai-sugg.rejected input { text-decoration: line-through; }
.ai-sugg .grow { flex: 1; min-width: 0; }
.ai-sugg input.ai-stmt { width: 100%; font-weight: 600; margin-bottom: 4px; }
.ai-sugg input.ai-domain { max-width: 180px; font-size: .8rem; }
.ai-sugg .ai-meta { font-size: .76rem; color: var(--muted); margin-top: 3px; }
.ai-sugg .ai-actions { display: flex; gap: 4px; flex-shrink: 0; align-items: center; }

/* progress timeline */
.tl-tiles { display: flex; gap: 12px; flex-wrap: wrap; }
.tl-tile {
  flex: 1; min-width: 110px; text-align: center;
  background: #f3f8fc; border: 1px solid #cbdeee; border-radius: 10px; padding: 12px 8px;
}
.tl-tile .n { font-size: 1.6rem; font-weight: 700; color: #12354e; }
.tl-tile .l { font-size: .74rem; color: var(--muted); }
.tl-legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: .78rem; margin-bottom: 8px; }
.tl-legend .swatch {
  display: inline-block; width: 10px; height: 10px; border-radius: 50%;
  margin-right: 5px; vertical-align: baseline;
}
.tl-axis {
  display: flex; justify-content: space-between; font-size: .72rem; color: var(--muted);
  margin: 0 0 6px 130px;
}
.tl-row { display: flex; align-items: center; gap: 10px; margin-bottom: 7px; }
.tl-name { width: 120px; font-size: .8rem; font-weight: 600; flex-shrink: 0; text-align: right; }
.tl-track {
  position: relative; flex: 1; height: 24px;
  background: #f0f4f8; border-radius: 6px; overflow: visible;
}
.tl-bar {
  position: absolute; top: 50%; height: 8px; transform: translateY(-50%);
  border-radius: 4px; opacity: .3;
}
.tl-dot {
  position: absolute; top: 50%; width: 13px; height: 13px; border-radius: 50%;
  transform: translate(-50%, -50%); border: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,.2); cursor: default;
}

/* CR2 readiness checklist */
input[type=number] {
  padding: 7px 9px; margin-top: 3px; border: 1px solid var(--line);
  border-radius: 7px; font: inherit; color: var(--ink); background: #fff;
}
.ready-check { font-size: .82rem; font-weight: 600; margin: 7px 0; line-height: 1.35; }
.ready-check.ok { color: var(--good); }
.ready-check.no { color: var(--bad); }
.ready-check .rc-detail { font-weight: 400; color: var(--muted); font-size: .78rem; }

/* quantitative questions: the analysis tabs show the numeric overlay only */
.panel.quant-mode > :not(.quant-overlay) { display: none !important; }

/* ---- UX layer: sticky nav, toasts, dialogs, checklist, tiles, editors ---- */
#tabs { position: sticky; top: 0; z-index: 30; overflow-x: auto; scrollbar-width: none; white-space: nowrap; }
#tabs::-webkit-scrollbar { display: none; }
.tab { flex-shrink: 0; }
.topbar-phase { font-size: .72rem; background: #ffffff22; border-radius: 20px; padding: 2px 9px; margin-left: 8px; }

#toast {
  position: fixed; right: 20px; bottom: 44px; z-index: 60;
  background: #12354e; color: #fff; padding: 10px 16px; border-radius: 10px;
  font-size: .88rem; box-shadow: 0 6px 20px rgba(0,0,0,.25);
  opacity: 0; transform: translateY(8px); transition: opacity .25s, transform .25s; pointer-events: none;
}
#toast.show { opacity: 1; transform: none; }
#toast.bad { background: var(--bad); }

dialog#modal {
  border: none; border-radius: 12px; padding: 0; max-width: 520px; width: calc(100% - 32px);
  box-shadow: 0 20px 60px rgba(0,0,0,.3); color: var(--ink);
}
dialog#modal::backdrop { background: rgba(18,53,78,.45); }
.modal-head { padding: 14px 18px 8px; font-weight: 700; font-size: 1rem; }
.modal-body { padding: 0 18px 8px; font-size: .88rem; color: var(--muted); white-space: pre-wrap; }
.modal-body label { color: var(--ink); font-weight: 600; font-size: .84rem; margin-bottom: 10px; }
.modal-body .mhelp { color: var(--muted); font-weight: 400; font-size: .78rem; display: block; margin-top: 2px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 18px 16px; }
.modal-err { color: var(--bad); font-size: .82rem; padding: 0 18px; min-height: 1.2em; }

.checklist { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 8px; }
.check-item {
  display: flex; align-items: center; gap: 9px; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: #fbfcfe; font-size: .82rem; cursor: pointer;
}
.check-item:hover { border-color: var(--accent); }
.check-item .ck { width: 20px; height: 20px; border-radius: 50%; flex-shrink: 0; display: inline-flex; align-items: center; justify-content: center; font-size: .7rem; font-weight: 700; background: #fdf0d2; color: #713f12; }
.check-item.ok .ck { background: #d2ecdd; color: #14532d; }
.check-item .ct { flex: 1; }
.check-item .cs { color: var(--muted); font-size: .76rem; }

.tiles { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.tile { flex: 1; min-width: 120px; background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.tile .n { font-size: 1.5rem; font-weight: 700; color: #12354e; line-height: 1.1; }
.tile .l { font-size: .74rem; color: var(--muted); }
.tile .sub { font-size: .72rem; color: var(--good); font-weight: 600; }

.tbl tr.row-hidden { display: none; }

/* demographics field editor */
.fld-row { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 6px; background: #fff; font-size: .84rem; }
.fld-row .fld-n { width: 24px; color: var(--muted); font-weight: 600; }
.fld-row .fld-lab { flex: 1; min-width: 0; }
.fld-row .fld-lab small { display: block; color: var(--muted); font-size: .74rem; }
.fld-row .fld-type { font-size: .72rem; background: var(--chip); border-radius: 20px; padding: 2px 8px; color: var(--muted); white-space: nowrap; }
.fld-row .fld-acts { display: flex; gap: 4px; }
.fld-editor { border: 1px solid #cbdeee; background: #f3f8fc; border-radius: 10px; padding: 12px 14px; margin: 8px 0 12px; }
.fld-editor .grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.fld-editor label { margin-bottom: 8px; }
.fld-editor .chk { display: flex; align-items: center; gap: 6px; font-size: .82rem; color: var(--ink); }
.fld-editor .chk input { width: auto; margin: 0; }
.fld-preview { background: #fff; border: 1px dashed var(--line); border-radius: 8px; padding: 10px 12px; margin-top: 8px; }
.fld-preview .pv-label { font-weight: 600; font-size: .86rem; }
.fld-preview .pv-help { color: var(--muted); font-size: .78rem; }

/* message template editor */
.tpl { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; background: #fff; }
.tpl-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.tpl-head .tpl-name { flex: 1; font-weight: 600; font-size: .88rem; }
.tpl-head input.tpl-label { flex: 1; font-weight: 600; }
.tpl textarea { font-size: .84rem; min-height: 64px; }
.tpl .tpl-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tpl .tpl-cols label { margin-bottom: 4px; }
.ph-chips { display: flex; flex-wrap: wrap; gap: 5px; margin: 4px 0 10px; }
.ph-chip { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .74rem; background: var(--chip); border-radius: 6px; padding: 2px 7px; cursor: pointer; border: 1px solid transparent; }
.ph-chip:hover { border-color: var(--accent); }
.tpl.custom .tpl-name::after { content: " · custom"; color: var(--muted); font-weight: 400; font-size: .76rem; }

/* Languages & translations (Setup) */
.tr-lang { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin: 8px 0; background: #fff; }
.tr-lang-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 6px; font-size: .88rem; }
.tr-lang-head .help { margin: 0; }
.tr-warn { font-size: .8rem; color: #713f12; background: #fdf0d2; border-radius: 6px; padding: 6px 9px; margin-top: 6px; }
.tr-ed { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-top: 10px; background: #fbfcfe; }
.tr-ed-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 8px; }
.tr-ed-head h4 { margin: 0; flex: 1; font-size: .95rem; }
.tr-ed h5 { margin: 14px 0 6px; font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.tr-unit { background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; margin-bottom: 6px; }
.tr-unit.st-missing, .tr-unit.st-stale, .tr-unit.has-issue { border-left: 3px solid #d97706; }
.tr-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: .78rem; color: var(--muted); margin-bottom: 4px; }
.tr-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tr-src { white-space: pre-wrap; font-size: .84rem; background: #f6f8fb; border-radius: 6px; padding: 6px 8px; max-height: 260px; overflow: auto; }
.tr-unit textarea { font-size: .84rem; margin: 0; }
.tr-issue { font-size: .78rem; color: var(--bad); margin-top: 4px; }

@media (max-width: 800px) {
  .fld-editor .grid-3, .tpl .tpl-cols, .tr-cols { grid-template-columns: 1fr; }
  #content { padding: 12px 12px 40px; }
  #topbar { flex-wrap: wrap; gap: 8px; }
}
