:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --ink: #1b2230;
  --muted: #6b7280;
  --line: #e4e7ec;
  --brand: #2563eb;
  --brand-ink: #1e40af;
  --good: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --cite: #fde68a;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  font-size: 14px;
  line-height: 1.55;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 38px; height: 38px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--brand), #4f46e5);
  color: #fff; font-weight: 700; letter-spacing: .5px;
  font-size: 13px;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text small { color: var(--muted); font-size: 12px; }

.case-picker { display: flex; align-items: center; gap: 8px; }
.case-picker label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
#case-select {
  appearance: none;
  min-width: 260px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  font-size: 14px; color: var(--ink);
  cursor: pointer;
}
#case-select:focus { outline: 2px solid var(--brand); border-color: var(--brand); }

.ghost-btn, .primary-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  padding: 7px 12px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.ghost-btn:hover { background: #f1f3f7; }
.primary-btn { background: var(--brand); color: #fff; border-color: var(--brand); font-weight: 600; }
.primary-btn:hover { background: var(--brand-ink); }
.primary-btn:disabled { opacity: .6; cursor: progress; }

/* ---------- Layout ---------- */
.layout { max-width: 1280px; margin: 0 auto; padding: 24px; }
.empty { text-align: center; color: var(--muted); padding: 80px 0; font-size: 15px; }

.banner {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  margin-bottom: 20px;
}
.banner h1 { margin: 0 0 4px; font-size: 20px; }
.banner-meta { display: flex; flex-wrap: wrap; gap: 10px 22px; margin-top: 12px; }
.meta-item { display: flex; flex-direction: column; }
.meta-item .k { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.meta-item .v { font-weight: 600; }
.encounters { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 16px; }
.enc-card {
  flex: 1 1 240px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  background: #fbfcfe;
}
.enc-card h3 { margin: 0 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--brand-ink); }
.enc-card .row { display: flex; justify-content: space-between; gap: 12px; font-size: 13px; padding: 2px 0; }
.enc-card .row span:first-child { color: var(--muted); }

.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: #eef2ff;
  color: var(--brand-ink);
}
.badge.good { background: #e7f6ec; color: var(--good); }
.badge.warn { background: #fdf2e3; color: var(--warn); }
.badge.bad { background: #fdeaea; color: var(--bad); }

/* ---------- Content (stacked top-to-bottom) ---------- */
.content { display: flex; flex-direction: column; gap: 20px; }

/* Index | Readmission side-by-side */
.summary-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }
@media (max-width: 900px) { .summary-pair { grid-template-columns: 1fr; } }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
.panel-head h2 { margin: 0; font-size: 15px; }
.head-actions { display: flex; align-items: center; gap: 10px; }

/* ---------- Summaries ---------- */
.summary-body { padding: 18px; }
.summary-text { white-space: pre-wrap; }
.summary-text p { margin: 0 0 12px; }
.summary-text p:last-child { margin-bottom: 0; }

/* ---------- Collapsible citation lists ---------- */
.cite-block {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.cite-summary {
  display: flex; align-items: center; gap: 8px;
  padding: 14px 18px;
  font-size: 15px; font-weight: 600;
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.cite-summary::-webkit-details-marker { display: none; }
.cite-summary::before {
  content: "▸";
  color: var(--muted);
  font-size: 12px;
  transition: transform .15s;
}
.cite-block[open] .cite-summary::before { transform: rotate(90deg); }
.cite-summary:hover { background: #f8f9fb; }
.cite-count {
  margin-left: auto;
  display: inline-grid; place-items: center;
  min-width: 22px; height: 22px; padding: 0 7px;
  background: #eef2ff; color: var(--brand-ink);
  border-radius: 999px; font-size: 12px; font-weight: 700;
}
.cite-block-body { padding: 4px 18px 14px; border-top: 1px solid var(--line); }

.cite-item { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid #f0f2f5; }
.cite-item:last-child { border-bottom: none; }
.cite-num {
  flex: 0 0 auto;
  width: 22px; height: 22px;
  display: grid; place-items: center;
  background: #eef2ff; color: var(--brand-ink);
  border-radius: 6px; font-size: 12px; font-weight: 700;
}
.cite-meta { min-width: 0; }
.cite-quote { font-style: italic; }
.cite-src { font-size: 12px; color: var(--muted); margin-top: 2px; word-break: break-word; }
.cite-src code { background: #f1f3f7; padding: 1px 5px; border-radius: 4px; font-size: 11px; }
.cite-unlocated { color: var(--warn); font-size: 11px; }

/* ---------- Evaluation ---------- */
.eval-body { padding: 16px 18px; }
.score-cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
@media (max-width: 900px) { .score-cards { grid-template-columns: 1fr 1fr; } }
.score-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  background: #fbfcfe;
}
.score-card .label { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.score-card .value { font-size: 26px; font-weight: 700; line-height: 1.1; margin-top: 4px; }
.score-card .value small { font-size: 14px; font-weight: 600; color: var(--muted); }
.score-card.span2 { grid-column: 1 / -1; }
.value.good { color: var(--good); }
.value.warn { color: var(--warn); }
.value.bad { color: var(--bad); }

.meter { height: 8px; border-radius: 999px; background: #eceff3; margin-top: 8px; overflow: hidden; }
.meter > span { display: block; height: 100%; border-radius: 999px; }
.meter > span.good { background: var(--good); }
.meter > span.warn { background: var(--warn); }
.meter > span.bad { background: var(--bad); }

/* Secondary stat row */
.stat-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 4px; }
.stat-chip { border: 1px solid var(--line); border-radius: 9px; padding: 8px 11px; background: #fff; }
.stat-chip .label { font-size: 10.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.stat-chip .value { font-size: 15px; font-weight: 700; margin-top: 2px; }

.eval-section { margin-top: 18px; }
.eval-section h3 { font-size: 13px; margin: 0 0 8px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.eval-list { margin: 6px 0 0; padding-left: 18px; }
.eval-list li { margin: 5px 0; }
.eval-list.bad li::marker { color: var(--bad); }
.eval-list.warn li::marker { color: var(--warn); }

/* Inline status tags / scope pills */
.tag { display: inline-block; padding: 0 7px; border-radius: 999px; font-size: 10.5px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .03em; background: #eef2ff; color: var(--brand-ink); margin-right: 6px; }
.tag.good { background: #e7f6ec; color: var(--good); }
.tag.warn { background: #fdf2e3; color: var(--warn); }
.tag.bad { background: #fdeaea; color: var(--bad); }
.scope-pill { display: inline-block; padding: 0 7px; border-radius: 6px; font-size: 11px; font-weight: 600;
  background: #eef0f4; color: var(--muted); margin-right: 6px; text-transform: capitalize; }

/* Duplicate / contradiction blocks */
.dup { border: 1px solid var(--line); border-left: 4px solid var(--warn); border-radius: 8px;
  padding: 8px 11px; margin-bottom: 8px; background: #fbfcfe; }
.dup-head { font-size: 12px; margin-bottom: 4px; }
.dup-line { font-size: 12.5px; }
.dup-line.muted { color: var(--muted); }

/* Readability table */
.read-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.read-table th, .read-table td { text-align: left; padding: 5px 8px; border-bottom: 1px solid var(--line); }
.read-table th { color: var(--muted); font-weight: 600; text-transform: uppercase; font-size: 10.5px; letter-spacing: .03em; }
.read-table td:first-child { text-transform: capitalize; }

.issue {
  border: 1px solid var(--line);
  border-left-width: 4px;
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 10px;
  background: #fbfcfe;
}
.issue.high { border-left-color: var(--bad); }
.issue.medium { border-left-color: var(--warn); }
.issue.low { border-left-color: var(--muted); }
.issue .issue-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 4px; }
.issue .cat { font-weight: 600; }
.issue .excerpt { font-style: italic; background: #fff7e6; padding: 4px 8px; border-radius: 6px; margin: 6px 0; }
.issue .suggestion { font-size: 12.5px; color: var(--muted); }
.issue .suggestion b { color: var(--ink); }

.computed-at { font-size: 11px; color: var(--muted); margin-top: 14px; text-align: right; }

.spinner {
  display: inline-block; width: 16px; height: 16px;
  border: 2px solid #cbd5e1; border-top-color: var(--brand);
  border-radius: 50%; animation: spin .7s linear infinite; vertical-align: -3px; margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Root cause + justifications ---------- */
.rootcause-panel .rc-body { padding: 18px; }
.rootcause-panel .rc-desc { margin: 0 0 14px; font-size: 13px; }
.rc-controls {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding-bottom: 12px; border-bottom: 1px solid var(--line); margin-bottom: 14px;
}
.rc-controls label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
#rootcause-select {
  flex: 1; min-width: 240px; max-width: 460px;
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--ink); font-size: 14px;
}
.just-listhead { display: flex; align-items: baseline; gap: 10px; margin-bottom: 10px; }
.just-listhead h3 { margin: 0; font-size: 15px; }
.just-count { font-size: 12px; color: var(--muted); }

.justifications { display: flex; flex-direction: column; gap: 10px; }
.just-row {
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px;
  background: #fcfcfd;
}
.just-head { display: flex; align-items: center; gap: 6px; margin-bottom: 6px; }
.badge.strength { text-transform: none; }
.just-main { display: flex; align-items: flex-start; gap: 16px; margin-bottom: 8px; }
.just-text { font-size: 14px; line-height: 1.5; flex: 1; min-width: 0; }
.just-decide { display: flex; gap: 16px; flex-shrink: 0; }
.decide-opt { font-size: 13px; color: var(--ink); cursor: pointer; display: inline-flex; align-items: center; gap: 4px; }
.just-source { margin-top: 4px; }
.just-source > summary { cursor: pointer; font-size: 12px; color: var(--brand); font-weight: 600; }
.just-cites { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.just-cite { border-left: 3px solid var(--line); padding: 4px 0 4px 10px; }
.just-cite.record { border-left-color: var(--brand); }
.just-cite.guideline { border-left-color: var(--good); }
.src-tag {
  display: inline-block; padding: 0 7px; border-radius: 999px; font-size: 10.5px;
  font-weight: 700; margin-right: 6px; text-transform: uppercase; letter-spacing: .03em;
}
.src-tag.record { background: #e8eefc; color: var(--brand-ink); }
.src-tag.guideline { background: #e7f6ec; color: var(--good); }

/* ---------- Generate Letter button row ---------- */
.letter-actions {
  display: flex; align-items: center; gap: 12px;
  margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line);
}
#generate-letter-btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Letter view (overlay) ---------- */
.letter-view {
  position: fixed; inset: 0; z-index: 60;
  background: #eef1f5; display: flex; flex-direction: column;
}
.letter-topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px; background: var(--surface);
  border-bottom: 1px solid var(--line); box-shadow: var(--shadow);
}
.letter-title { display: flex; flex-direction: column; line-height: 1.2; }
.letter-title small { color: var(--muted); font-size: 12px; }
.letter-tools { display: flex; align-items: center; gap: 10px; margin-left: auto; flex-wrap: wrap; }
.letter-hint { font-size: 12px; }

.letter-stage {
  flex: 1; display: grid; grid-template-columns: 1fr 320px;
  gap: 20px; padding: 24px; overflow: auto; align-items: start;
}
@media (max-width: 1000px) { .letter-stage { grid-template-columns: 1fr; } }

.letter-paper-wrap { display: flex; flex-direction: column; align-items: center; }
.letter-paper {
  background: #fff; width: 100%; max-width: 820px;
  border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 4px 24px rgba(16,24,40,.10);
  padding: 56px 64px; color: #1b2230;
  font-family: "Times New Roman", Georgia, serif; font-size: 15px; line-height: 1.6;
}
.letter-paper .letter-loading {
  display: flex; align-items: center; justify-content: center;
  min-height: 320px; color: var(--muted);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif; font-size: 15px;
}
.letter-paper .letter-loading.bad { color: #b42318; }
.letter-paper .l-line { white-space: pre-wrap; min-height: 1.2em; }
.letter-paper .l-para { margin: 0 0 14px; text-align: justify; }
.letter-paper .l-para.l-center { text-align: center; }
/* Temporary handwritten-signature placeholder (no signature image yet) */
.letter-paper .l-signature {
  font-family: "Snell Roundhand", "Apple Chancery", "Brush Script MT", "Segoe Script", cursive;
  font-size: 26px; line-height: 1.25; margin: 2px 0 2px;
}
.letter-paper .l-points { margin: 0 0 14px; padding-left: 22px; }
.letter-paper .l-points li { margin: 0 0 10px; }
.letter-paper .l-crit { margin: 0 0 14px; padding-left: 22px; }
.letter-paper .l-gl { color: var(--muted); font-style: italic; }

/* Letterhead masthead (dynamic facility wordmark + website) */
.letter-paper .letter-masthead {
  text-align: center;
  padding-bottom: 12px;
  margin-bottom: 6px;
  border-bottom: 2px solid #1b2230;
}
.letter-paper .letter-masthead .lm-name { font-size: 21px; font-weight: 700; letter-spacing: .01em; }
.letter-paper .letter-masthead .lm-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* Right-aligned return address block */
.letter-paper .letter-return { text-align: right; margin: 10px 0 18px; }
.letter-paper .letter-return .lr-line { min-height: 1.2em; }

/* Two-stay identifier row: HAR left, DOS pushed to the right margin */
.letter-paper .two-stay-row { display: flex; justify-content: space-between; gap: 16px; }
.letter-paper .two-stay-row .ts-dos { white-space: nowrap; }

.ai-cited {
  text-decoration: underline; text-decoration-color: #93b4f5;
  text-decoration-thickness: 2px; text-underline-offset: 2px;
  cursor: help;
}
.ai-cited:hover, .ai-cited:focus-visible { background: var(--cite); outline: none; }

/* Inline [n] source markers */
.cite-mark {
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 10px; font-weight: 700; line-height: 1;
  color: var(--brand); vertical-align: super;
  margin-left: 1px; cursor: help; white-space: nowrap;
}
.cite-mark:hover, .cite-mark:focus-visible { color: var(--brand-ink); text-decoration: underline; outline: none; }

/* Custom citation popover */
.cite-pop {
  position: fixed; z-index: 80; max-width: 360px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px;
  box-shadow: 0 8px 28px rgba(16,24,40,.16);
  font-family: ui-sans-serif, system-ui, sans-serif; font-size: 12.5px; line-height: 1.45;
  pointer-events: none;
}
.cite-pop.hidden { display: none; }
.cite-pop .cite-item + .cite-item { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 10px; }
.cite-pop .cite-head { display: flex; align-items: center; gap: 6px; margin-bottom: 3px; }
.cite-pop .cite-n {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 16px; height: 16px; padding: 0 4px; border-radius: 999px;
  background: #eef2f8; color: var(--muted); font-size: 10.5px; font-weight: 700;
}
.cite-pop .cite-where { font-weight: 600; }
.cite-pop .cite-q { margin-top: 4px; color: var(--muted); font-style: italic; }

.cite-kind {
  display: inline-block; padding: 0 7px; border-radius: 999px;
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
}
.cite-kind.record { background: #e8eefc; color: var(--brand-ink); }
.cite-kind.guideline { background: #e7f6ec; color: var(--good); }

/* Sources sidebar list */
.side-sources { margin: 0; padding-left: 20px; }
.side-sources li.muted { list-style: none; margin-left: -20px; font-size: 12px; }
.side-source { margin: 0 0 10px; font-size: 12.5px; }
.side-source .cite-kind { margin-right: 6px; }
.side-source-where { font-weight: 600; }
.side-source-q { margin-top: 3px; color: var(--muted); font-style: italic; }

.letter-editor {
  width: 100%; max-width: 820px; min-height: 70vh;
  border: 1px solid var(--line); border-radius: 8px; padding: 24px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; line-height: 1.55;
}
.letter-edit-bar { display: flex; gap: 10px; margin-top: 12px; }

.letter-side { display: flex; flex-direction: column; gap: 16px; position: sticky; top: 0; }
.side-card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px;
}
.side-card h3 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--brand-ink); }
.side-just { margin: 0; padding-left: 20px; }
.side-just li { margin: 0 0 8px; font-size: 13px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1b2230; color: #fff; padding: 10px 18px; border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.2); z-index: 50; font-size: 13px;
}
.toast.bad { background: var(--bad); }
