:root {
  --bg: #ffffff;
  --panel: #f6f6f5;
  --line: #e2e1de;
  --text: #1c1c1b;
  --muted: #6c6b67;
  --accent: #f26b1d;
  --accent-soft: #fde9dc;
  --good: #2f7d4f;
  --warn: #a8660a;
  --bad: #b3361f;
  --chip: #efeeec;
  --shadow: 0 8px 30px rgba(0,0,0,.12);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141413; --panel: #1d1d1b; --line: #33322f; --text: #ecebe8; --muted: #9c9a94;
    --accent: #ff7a2e; --accent-soft: #3a2416; --good: #6cc391; --warn: #e3a64b; --bad: #ef7c66;
    --chip: #2a2927; --shadow: 0 8px 30px rgba(0,0,0,.5);
  }
}
:root[data-theme="dark"] {
  --bg: #141413; --panel: #1d1d1b; --line: #33322f; --text: #ecebe8; --muted: #9c9a94;
  --accent: #ff7a2e; --accent-soft: #3a2416; --good: #6cc391; --warn: #e3a64b; --bad: #ef7c66;
  --chip: #2a2927; --shadow: 0 8px 30px rgba(0,0,0,.5);
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, sans-serif;
}
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 24px 0 8px; }
h3 { font-size: 14px; margin: 16px 0 6px; }
a { color: var(--accent); }
button, select, input { font: inherit; color: inherit; }
input, select {
  background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 4px 8px;
}
button { background: none; border: 0; cursor: pointer; padding: 0; }
.btn {
  border: 1px solid var(--line); border-radius: 6px; padding: 5px 12px; background: var(--bg);
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.top {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 24px;
  padding: 10px 20px; background: var(--bg); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.brand { font-weight: 650; }
.tag { font-weight: 500; font-size: 11px; color: var(--accent); margin-left: 6px; text-transform: uppercase; letter-spacing: .04em; }
#tabs { display: flex; gap: 4px; }
#tabs button { padding: 6px 12px; border-radius: 6px; color: var(--muted); }
#tabs button.on { color: var(--text); background: var(--panel); font-weight: 600; }
.count { color: var(--accent); font-weight: 600; }

.tab { display: none; }
.tab.on { display: flex; min-height: calc(100vh - var(--top, 110px)); }
.side {
  width: 260px; flex: none; border-right: 1px solid var(--line); padding: 12px; overflow: auto;
  max-height: calc(100vh - var(--top, 110px)); position: sticky; top: var(--top, 52px);
}
.side input { width: 100%; margin-bottom: 10px; }
.side .group { margin: 12px 0 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.side .item { display: block; width: 100%; text-align: left; padding: 5px 8px; border-radius: 6px; }
.side .item:hover { background: var(--panel); }
.side .item.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.main { flex: 1; padding: 20px 28px 60px; min-width: 0; max-width: 1200px; }
.main.wide { max-width: none; }

.kv { display: flex; gap: 6px 18px; flex-wrap: wrap; color: var(--muted); font-size: 12px; margin-bottom: 8px; }
.details { display: flex; gap: 10px 18px; flex-wrap: wrap; padding: 12px; background: var(--panel); border-radius: 8px; }
.details label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.details select { min-width: 160px; color: var(--text); }
.details input[type=number] { width: 160px; color: var(--text); }
.details select, .details input[type=number] { height: 28px; box-sizing: border-box; }
.details select.set { border-color: var(--accent); }

.finance { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; margin: 14px 0; }
.finance label { display: inline-flex; align-items: center; gap: 8px; }
.finance label.range .num { min-width: 4.8em; font-variant-numeric: tabular-nums; line-height: 20px; }

/* Budget sliders: drawn here, not by the browser, so the part left of the
   handle is filled from the first paint (app.js sets --fill). */
.finance input[type=range] {
  -webkit-appearance: none; appearance: none; display: block; width: 200px; height: 20px; margin: 0; padding: 0;
  border: 0; border-radius: 0; background: transparent; cursor: pointer;
  --range-on: var(--accent); --range-off: var(--line);
}
.finance input[type=range]::-webkit-slider-runnable-track {
  height: 6px; border-radius: 3px;
  background: linear-gradient(to right, var(--range-on) var(--fill, 0%), var(--range-off) var(--fill, 0%));
}
.finance input[type=range]::-moz-range-track { height: 6px; border-radius: 3px; background: var(--range-off); }
.finance input[type=range]::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--range-on); }
.finance input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 18px; height: 18px; margin-top: -6px; border-radius: 50%;
  background: #fff; border: 1px solid #C4C4CC; box-shadow: 0 1px 3px rgba(0,0,0,.22);
}
.finance input[type=range]::-moz-range-thumb {
  width: 18px; height: 18px; border-radius: 50%; box-sizing: border-box;
  background: #fff; border: 1px solid #C4C4CC; box-shadow: 0 1px 3px rgba(0,0,0,.22);
}
.finance input[type=range]:focus { outline: none; }
.finance input[type=range]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 3px var(--accent-soft), 0 1px 3px rgba(0,0,0,.22); }

table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tr.opt { cursor: pointer; }
tr.opt:hover td { background: var(--panel); }
tr.opt.open td { background: var(--panel); }
tr.expand td { background: var(--panel); padding: 4px 18px 18px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

.badge { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 10px; background: var(--chip); color: var(--muted); white-space: nowrap; }
.badge.fit { background: var(--accent-soft); color: var(--accent); }
.badge.good { color: var(--good); }
.badge.warn { color: var(--warn); }
.badge.bad { color: var(--bad); }

.stat { font-variant-numeric: tabular-nums; }
.stat .adj { color: var(--accent); font-weight: 600; }
.stat .gen { color: var(--muted); text-decoration: line-through; margin-right: 4px; }
.src { display: inline-block; margin: 2px 4px 0 0; font-size: 11px; padding: 1px 7px; border-radius: 10px; background: var(--chip); color: var(--text); }
.src:hover { background: var(--accent-soft); color: var(--accent); }
.src.review { outline: 1px dashed var(--warn); }
.src.rejected { text-decoration: line-through; opacity: .6; }
.src.unverified::after { content: " ?"; color: var(--bad); }

.outcome { padding: 8px 0; border-bottom: 1px dashed var(--line); }
.outcome:last-child { border-bottom: 0; }
.outcome .head { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.outcome .label { font-weight: 550; }
.outcome .note { color: var(--muted); font-size: 12px; margin-top: 2px; }
.leads { font-size: 12px; color: var(--muted); }
.leads button { color: var(--accent); }

.tree { margin-top: 10px; font-size: 13px; }
.tree ul { list-style: none; margin: 0; padding-left: 18px; border-left: 1px solid var(--line); }
.tree > ul { padding-left: 0; border-left: 0; }
.tree li { margin: 4px 0; }
.tree .node { display: inline-flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.tree .p { color: var(--accent); font-weight: 600; font-variant-numeric: tabular-nums; }

.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(520px, 100vw); background: var(--bg);
  box-shadow: var(--shadow); border-left: 1px solid var(--line); padding: 20px 22px; overflow: auto; z-index: 20;
}
.drawer .close { float: right; color: var(--muted); }
.drawer h2 { margin-top: 4px; }
.drawer .cite { font-size: 13px; }
.drawer .row { margin: 8px 0; }
.drawer textarea { width: 100%; min-height: 70px; border: 1px solid var(--line); border-radius: 6px; padding: 6px; background: var(--bg); color: var(--text); font: inherit; }
.choices { display: flex; gap: 6px; margin: 8px 0; }
.choices .btn.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }

.plan-row td { vertical-align: middle; }
.offered { display: flex; flex-wrap: wrap; gap: 4px 10px; font-size: 12px; }
.offered label { display: inline-flex; gap: 3px; align-items: center; }
.totals { display: flex; gap: 28px; flex-wrap: wrap; margin: 16px 0; padding: 14px; background: var(--panel); border-radius: 8px; }
.totals div b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.import textarea { width: 100%; min-height: 90px; font: 12px ui-monospace, Menlo, monospace; border: 1px solid var(--line); border-radius: 6px; padding: 8px; background: var(--bg); color: var(--text); }

.empty { padding: 40px; color: var(--muted); }
.foot { padding: 10px 20px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; }

@media (max-width: 760px) {
  .tab.on { flex-direction: column; }
  .side { width: auto; max-height: 40vh; position: static; border-right: 0; border-bottom: 1px solid var(--line); }
  .main { padding: 16px; }
  table { display: block; overflow-x: auto; }
}

/* Segmented control */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.seg button { padding: 5px 12px; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* Node map */
.maptools { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 4px 0 8px; }
.mapwrap {
  overflow: auto; border: 1px solid var(--line); border-radius: 14px; background: #FFFFFF;
  height: min(72vh, 820px); cursor: grab;
}
.mapwrap.grab { cursor: grabbing; user-select: none; }
#map { display: block; margin: 0 auto; }
/* What moves when a branch opens or closes is drawn on this layer, over
   the map, so the map itself (every glass card's lens) is not redrawn on
   each frame (app.js animateMap). */
.mapwrap { position: relative; }
.map-overlay { position: absolute; pointer-events: none; overflow: visible; }
/* Each on its own layer: the camera pan scrolls a finished picture of the
   map, and redrawing the overlay never touches it. */
#map, .map-overlay { will-change: transform; }

/* The map is always a white sheet with a grey dot grid, in either theme. */
:is(#map, .map-overlay) .dot { fill: #BDBDC6; }
:is(#map, .map-overlay) .edge { fill: none; stroke: #A1A1AA; stroke-width: 1.6; stroke-linecap: round; }
:is(#map, .map-overlay) .arrow path, :is(#map, .map-overlay) .sprout-tip { fill: #A1A1AA; }

/* Glass cards (nodeSVG): a soft shadow, the magnified and bent dots, a
   faint white tint, a sheen across the top, a bright rim. The inner ring
   carries the card's kind: survives, complication, fails, often added. */
:is(#map, .map-overlay) .node { cursor: pointer; }
:is(#map, .map-overlay) .node .shade { fill: rgba(24, 24, 40, .13); filter: url(#glass-shadow); transform: translateY(7px); }
:is(#map, .map-overlay) .node .base { fill: #FFFFFF; }
:is(#map, .map-overlay) .node .tint { fill: rgba(255, 255, 255, .08); }
:is(#map, .map-overlay) .node .sheen { pointer-events: none; }
:is(#map, .map-overlay) .sheen-top { stop-color: #FFFFFF; stop-opacity: .5; }
:is(#map, .map-overlay) .sheen-mid { stop-color: #FFFFFF; stop-opacity: .1; }
:is(#map, .map-overlay) .sheen-end { stop-color: #FFFFFF; stop-opacity: .02; }
:is(#map, .map-overlay) .rim-hi { stop-color: #FFFFFF; stop-opacity: 1; }
:is(#map, .map-overlay) .rim-mid { stop-color: #B9B9C3; stop-opacity: .7; }
:is(#map, .map-overlay) .rim-lo { stop-color: #8E8E99; stop-opacity: .75; }
:is(#map, .map-overlay) .node .rim { stroke-width: 1.3; }
:is(#map, .map-overlay) .node .kind { stroke: rgba(255, 255, 255, .75); stroke-width: 1.2; }
:is(#map, .map-overlay) .node:hover .shade { fill: rgba(24, 24, 40, .2); }
/* Selected: the pane lifts off the sheet and its shadow deepens, instead of
   an outline. */
:is(#map, .map-overlay) .node .pane, :is(#map, .map-overlay) .node .shade { transition: transform .18s ease, fill .18s ease; }
:is(#map, .map-overlay) .node.sel .pane { transform: translateY(-5px); }
:is(#map, .map-overlay) .node.sel .shade { fill: rgba(24, 24, 40, .32); transform: translateY(14px) scale(1.03); transform-box: fill-box; transform-origin: center; }

:is(#map, .map-overlay) .node.condition .tint { fill: rgba(255, 255, 255, .45); }
:is(#map, .map-overlay) .node.survive .kind, :is(#map, .map-overlay) .node.resume .kind, :is(#map, .map-overlay) .node.outcome.good .kind { stroke: rgba(4, 120, 87, .6); stroke-width: 1.6; }
:is(#map, .map-overlay) .node.fail .kind, :is(#map, .map-overlay) .node.mode .kind, :is(#map, .map-overlay) .node.outcome.bad .kind { stroke: rgba(179, 38, 30, .55); stroke-width: 1.6; }
:is(#map, .map-overlay) .node.comp .kind, :is(#map, .map-overlay) .node.mode.comp .kind, :is(#map, .map-overlay) .node.outcome.comp .kind, :is(#map, .map-overlay) .node.outcome.warn .kind { stroke: rgba(26, 18, 6, .7); stroke-width: 1; }
:is(#map, .map-overlay) .node.mode .kind, :is(#map, .map-overlay) .node.resume .kind { stroke-dasharray: 4 3; }
:is(#map, .map-overlay) .node.addon .kind { stroke: rgba(26, 18, 6, .7); stroke-width: 1; stroke-dasharray: 6 3; }
:is(#map, .map-overlay) .node.add .tint { fill: rgba(255, 255, 255, .2); }
:is(#map, .map-overlay) .node.add .rim { stroke: #8E8E99; stroke-dasharray: 6 4; }
:is(#map, .map-overlay) .node.add .kind { stroke: none; }

:is(#map, .map-overlay) text { fill: #1A1206; font-size: 12.5px; }
/* A soft white halo keeps the words clear of the dots seen through the glass. */
:is(#map, .map-overlay) .node text { paint-order: stroke; stroke: rgba(255, 255, 255, .82); stroke-width: 3.2px; stroke-linejoin: round; }
:is(#map, .map-overlay) text.l1 { font-weight: 600; }
:is(#map, .map-overlay) text.l2 { fill: #5C5142; font-size: 11.5px; font-variant-numeric: tabular-nums; }
:is(#map, .map-overlay) text.toggle { fill: #1A1206; font-weight: 700; font-size: 15px; }

.legend { margin: 8px 0 0; display: flex; gap: 6px 10px; align-items: center; flex-wrap: wrap; }
.legend .key { display: inline-block; width: 12px; height: 12px; border-radius: 3px; border: 2px solid var(--line); }
.legend .key.fit { background: var(--accent-soft); border-color: var(--accent); }
.legend .key.good { border-color: var(--good); }
.legend .key.bad { border-color: var(--bad); }
.legend .key.warn { border-color: var(--warn); }
.legend .key.comp { border-color: var(--warn); }
.legend .key.add { border-color: var(--accent); border-style: dashed; }
#map-detail { margin-top: 12px; }
details.more { margin: 12px 0; }
/* "More detail" reads as a dropdown: a bordered box with a chevron that
   turns over when open. */
details.more summary {
  list-style: none; cursor: pointer; display: inline-flex; align-items: center; gap: 10px;
  padding: 7px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
}
details.more summary::-webkit-details-marker { display: none; }
details.more summary:hover { border-color: var(--muted); }
details.more summary .chev {
  width: 7px; height: 7px; margin: 0 2px 0 4px; border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor;
  transform: translateY(-2px) rotate(45deg); transition: transform .15s ease;
}
details.more[open] summary .chev { transform: translateY(2px) rotate(-135deg); }
details.more .details { margin-top: 6px; }

/* Patient background */
.patient-bar { flex-basis: 100%; }
.pb-toggle { display: flex; gap: 12px; align-items: baseline; width: 100%; text-align: left; padding: 6px 10px; border-radius: 8px; background: var(--panel); flex-wrap: wrap; }
.pb-toggle .pb-edit { margin-left: auto; color: var(--accent); font-weight: 600; }
.pb-panel { margin-top: 8px; align-items: flex-end; }
.details select.set { border-color: var(--accent); }
.uses { margin: 6px 0 10px; color: var(--muted); }
.uses .unset { color: var(--warn); }
.linkish { color: var(--accent); text-decoration: underline; margin-left: 6px; }
.gate { border: 1px solid var(--accent); background: var(--accent-soft); border-radius: 10px; padding: 14px 16px; margin: 14px 0; }
.gate h3 { margin-top: 0; }
.gate .details { background: var(--bg); margin: 10px 0; }

/* The start-of-visit notice (showNotice in app.js). */
.notice-back {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  padding: 16px; background: rgba(24, 24, 30, .28); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
}
.notice {
  width: min(460px, 100%); background: var(--bg); color: var(--text); border: 1px solid var(--line);
  border-radius: 14px; padding: 22px 24px; box-shadow: var(--shadow);
}
.notice h2 { margin: 0 0 10px; font-size: 18px; }
.notice p { margin: 0 0 10px; line-height: 1.5; }
.notice .btn { display: block; margin: 6px auto 0; padding: 8px 18px; }

/* Settings (top right), Customize pricing and Export (office.js). */
.settings-btn { margin-left: auto; }
.mapframe { position: relative; }
.map-export {
  position: absolute; right: 18px; bottom: 18px; z-index: 2; padding: 7px 16px; font-weight: 600;
  background: var(--bg); box-shadow: 0 2px 10px rgba(0, 0, 0, .14);
}
.office-dlg { width: min(560px, 100%); max-height: calc(100vh - 32px); overflow: auto; }
.office-dlg.office-fees { width: min(780px, 100%); }
.office-dlg.office-export { width: min(1200px, 100%); }
.office-dlg .btn { display: inline-block; margin: 0; padding: 5px 12px; }
.office-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.office-head h2 { margin: 0; }
.office-head > span { display: inline-flex; gap: 8px; }
.office-dlg h3 { font-size: 13px; margin: 18px 0 6px; }
.office-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.office-row label { display: inline-flex; gap: 6px; align-items: center; }
.office-dlg textarea { width: 100%; box-sizing: border-box; font: 12px ui-monospace, Menlo, Consolas, monospace; }
.fee-scroll { overflow: auto; max-height: 58vh; border: 1px solid var(--line); border-radius: 8px; }
.fee-table th { position: sticky; top: 0; z-index: 1; background: var(--bg); }
.fee-table td, .fee-table th { padding: 6px 10px; }
.fee-in { white-space: nowrap; }
.fee-in input { width: 92px; text-align: right; margin-left: 2px; }
.exp-grid { display: grid; grid-template-columns: 230px minmax(0, 1fr); gap: 20px; align-items: start; }
.exp-side h3:first-child { margin-top: 4px; }
.exp-check { display: flex; gap: 6px; align-items: center; margin-top: 12px; }
.exp-main { min-width: 0; }
.exp-view { margin-bottom: 10px; }
/* The preview is the page: drawn at Letter size and scaled down to fit. */
.exp-preview { display: flex; justify-content: center; }
.exp-page { overflow: hidden; background: #fff; border: 1px solid var(--line); box-shadow: 0 2px 14px rgba(0, 0, 0, .12); }
.exp-paper { box-sizing: border-box; background: #fff; transform-origin: 0 0; }
.exp-paper.picking [data-pick] { cursor: pointer; }
.exp-paper.picking [data-pick]:hover rect { stroke-width: 2.6; }
.exp-paper.picking [data-pick].ghost:hover rect { stroke: #71717A; }
@media (max-width: 820px) { .exp-grid { grid-template-columns: 1fr; } }

/* The export sheet, in the preview and on paper. Ink only where it says
   something: white cards, thin lines, no fills unless Color is picked. */
.sheet {
  display: flex; flex-direction: column; height: 100%; box-sizing: border-box;
  color: #111; background: #fff; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.sheet-head h1 { font-size: 18px; margin: 0 0 2px; }
.sheet-head p { margin: 0; font-size: 12px; }
.sheet-head .sheet-meta { color: #444; margin-top: 3px; }
/* The map is as wide as the page and shrinks to fit what height is left;
   the legend follows straight under it. */
.sheet-map { display: block; flex: 0 1 auto; min-height: 0; width: 100%; height: auto; margin: 12px 0 10px; }
.sheet-legend { display: flex; flex-wrap: wrap; gap: 4px 18px; font-size: 11px; color: #222; }
.sheet-legend span { display: inline-flex; align-items: center; gap: 6px; }
.sheet-foot { font-size: 10.5px; color: #444; margin: 8px 0 0; }
#print-sheet { display: none; }
@media print {
  body.print-export > :not(#print-sheet) { display: none !important; }
  body.print-export { background: #fff; }
  body.print-export #print-sheet { display: block; }
  /* Letter, less the 10 mm margins set by @page. */
  body.print-landscape #print-sheet .sheet { height: calc(8.5in - 20mm - 2px); }
  body.print-portrait #print-sheet .sheet { height: calc(11in - 20mm - 2px); }
  .sheet { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
