:root {
  /* Layout: fixed app bar + sheet navigator rail + scrolling workbook canvas that stays "paper" in every theme */
  --bg: #f4f2f1;
  --panel: #ffffff;
  --ink: #221c1b;
  --muted: #6d6362;
  --line: #e2dcda;
  --accent: #a3171c;
  --accent-ink: #ffffff;
  --accent-soft: #f6e3e2;
  --ok: #2f7a4b;
  --warn: #b26a00;
  --paper: #ffffff;
  --paper-ink: #000000;
  --input: #fff9c4;
  --display: "Public Sans", "Segoe UI", system-ui, sans-serif;
  --body: "Public Sans", "Segoe UI", system-ui, sans-serif;
  --grid: Arial, "Liberation Sans", Helvetica, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171313; --panel: #211b1b; --ink: #f1eceb; --muted: #a99d9b; --line: #3a3130;
    --accent: #e0575a; --accent-ink: #1a0d0d; --accent-soft: #3a2222; --ok: #6cc28d; --warn: #e2a74c;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #171313; --panel: #211b1b; --ink: #f1eceb; --muted: #a99d9b; --line: #3a3130;
  --accent: #e0575a; --accent-ink: #1a0d0d; --accent-soft: #3a2222; --ok: #6cc28d; --warn: #e2a74c;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font-family: var(--body); font-size: 14px; display: grid; grid-template-rows: auto 1fr auto; grid-template-columns: 264px 1fr; grid-template-areas: "top top" "nav main" "nav status"; overflow: hidden; }
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* top bar */
#top { grid-area: top; display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); min-width: 0; }
#brand { display: flex; align-items: baseline; gap: 8px; white-space: nowrap; }
#brand b { font-family: var(--display); font-weight: 800; font-size: 19px; letter-spacing: -0.02em; }
#brand b span { color: var(--accent); }
#brand small { color: var(--muted); font-size: 12px; }
#run { flex: 1; min-width: 0; display: flex; flex-direction: column; padding-left: 12px; border-left: 1px solid var(--line); }
#run .lbl { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
#run .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chg { font-size: 12px; color: var(--muted); white-space: nowrap; }
#chg.on { color: var(--warn); }
.tb { display: flex; gap: 6px; flex-wrap: nowrap; }
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 6px 11px; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn[hidden] { display: none; }
#menuBtn { display: none; }

/* nav */
#nav { grid-area: nav; background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; padding: 10px 8px 24px; }
#nav h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 14px 8px 6px; font-weight: 600; }
#nav a { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; color: var(--ink); text-decoration: none; font-size: 13.5px; line-height: 1.25; }
#nav a:hover { background: var(--bg); }
#nav a.cur { background: var(--accent-soft); color: var(--ink); font-weight: 600; box-shadow: inset 3px 0 0 var(--accent); }
#nav a .num { font-variant-numeric: tabular-nums; color: var(--muted); width: 18px; text-align: right; flex: none; font-size: 12px; }
#nav a .t { flex: 1; min-width: 0; }
#nav a .pill { font-size: 10.5px; padding: 1px 6px; border-radius: 99px; background: #fff3a3; color: #5a4b00; flex: none; }
#nav a .pill.c { background: var(--warn); color: #fff; }
#nav details summary { cursor: pointer; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 14px 8px 6px; font-weight: 600; list-style: none; }
#nav details summary::before { content: "▸ "; }
#nav details[open] summary::before { content: "▾ "; }
#nav .hint { font-size: 12px; color: var(--muted); margin: 0 8px 6px; }

/* main */
#main { grid-area: main; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#sheetbar { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--bg); flex-wrap: wrap; }
#sheetbar h1 { font-family: var(--display); font-size: 18px; margin: 0; font-weight: 700; text-wrap: balance; }
#sheetbar .meta { color: var(--muted); font-size: 12.5px; }
#sheetbar .sp { flex: 1; }
#canvas { flex: 1; overflow: auto; background: var(--bg); padding: 16px; position: relative; }
#paper { background: var(--paper); color: var(--paper-ink); display: inline-block; position: relative; box-shadow: 0 1px 2px rgba(0,0,0,.08), 0 0 0 1px rgba(0,0,0,.06); border-radius: 4px; padding: 6px; min-width: 100%; color-scheme: light; }
#status { grid-area: status; display: flex; gap: 16px; align-items: center; padding: 5px 16px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); background: var(--panel); font-variant-numeric: tabular-nums; }
#status .k { font-weight: 600; color: var(--ink); font-family: ui-monospace, monospace; }
#status .legend { margin-left: auto; display: flex; gap: 12px; align-items: center; }
#status .sw { display: inline-block; width: 12px; height: 12px; border: 1px solid #c9b800; background: #ffff00; vertical-align: -2px; margin-right: 4px; }
#status .sw.ch { background: #ffff00; box-shadow: inset 3px 0 0 #1f6feb; }

/* workbook grid */
table.g { border-collapse: collapse; table-layout: fixed; font-family: var(--grid); font-size: 13.3px; color: #000; }
table.g td { padding: 0 3px; white-space: nowrap; overflow: visible; vertical-align: bottom; line-height: 1.2; position: relative; }
table.g.grid td { border: 1px solid #e6e6e6; }
table.g td.n { text-align: right; }
table.g td.b { text-align: center; }
table.g td.e { text-align: center; color: #b00020; }
table.g td.clip { overflow: hidden; text-overflow: clip; }
table.g td.lnk { cursor: pointer; color: #0b57d0; text-decoration: underline; }
table.g td.note::after { content: ""; position: absolute; top: 0; right: 0; border-style: solid; border-width: 0 7px 7px 0; border-color: transparent #d00 transparent transparent; }
table.g td.ed { padding: 0; }
table.g td.ed.chg { box-shadow: inset 3px 0 0 #1f6feb; }
table.g td.ed input, table.g td.ed select { width: 100%; height: 100%; min-height: 18px; border: 0; background: transparent; padding: 0 3px; font: inherit; color: inherit; text-align: inherit; outline: none; border-radius: 0; }
table.g td.ed select { padding: 0 1px; cursor: pointer; }
table.g td.ed input:focus, table.g td.ed select:focus { background: #fff; box-shadow: inset 0 0 0 2px #1f6feb; }
table.g td.ed input:hover { box-shadow: inset 0 0 0 1px #8a7a00; }
.ov { position: absolute; }
.ov.btnx { display: flex; align-items: center; justify-content: center; text-align: center; font: 600 12px/1.15 var(--body); border: 1px solid #8f8f8f; border-radius: 5px; background: linear-gradient(#fbfbfb, #e9e9e9); color: #111; padding: 2px 4px; box-shadow: 0 1px 1px rgba(0,0,0,.12); }
.ov.btnx:hover { border-color: var(--accent); color: var(--accent); }
.ov.shape { display: flex; align-items: center; justify-content: center; text-align: center; font: 11px/1.2 var(--grid); color: #000; pointer-events: none; }
.ov.chart { background: #fff; border: 1px solid #d0d0d0; }
.ov.chart svg { display: block; width: 100%; height: 100%; font-family: var(--grid); }

/* tooltip + dialogs */
#tip { position: fixed; z-index: 50; max-width: 340px; background: #fffbe6; color: #222; border: 1px solid #d8c76c; border-radius: 6px; padding: 8px 10px; font-size: 12.5px; line-height: 1.4; box-shadow: 0 4px 16px rgba(0,0,0,.18); white-space: pre-wrap; pointer-events: none; display: none; }
.pop { position: absolute; z-index: 40; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 10px 30px rgba(0,0,0,.18); padding: 6px; min-width: 250px; }
.pop h4 { margin: 8px 10px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); }
.pop button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 7px 10px; border-radius: 5px; }
.pop button:hover { background: var(--bg); }
.pop button small { display: block; color: var(--muted); font-size: 12px; }
dialog { border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--ink); padding: 0; width: min(520px, calc(100% - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(20,10,10,.35); }
dialog header { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; }
dialog header h2 { margin: 0; font-size: 16px; font-family: var(--display); }
dialog .body { padding: 14px 18px; display: grid; gap: 12px; }
dialog .row { display: flex; gap: 8px; }
dialog .row input { flex: 1; padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
dialog .list { display: grid; gap: 6px; max-height: 320px; overflow: auto; }
dialog .item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; }
dialog .item .t { flex: 1; min-width: 0; }
dialog .item .t b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
dialog .item .t small { color: var(--muted); }
dialog .note { font-size: 12.5px; color: var(--muted); margin: 0; }
#toast { position: fixed; left: 50%; bottom: 44px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 9px 14px; border-radius: 8px; font-size: 13px; display: none; gap: 12px; align-items: center; z-index: 60; box-shadow: 0 8px 24px rgba(0,0,0,.25); }
#toast button { background: none; border: 0; color: inherit; font-weight: 700; text-decoration: underline; padding: 0; }

@media (max-width: 860px) {
  body { grid-template-columns: 1fr; grid-template-areas: "top" "main" "status"; }
  #nav { position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: 280px; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 30px rgba(0,0,0,.3); }
  body.navopen #nav { transform: none; }
  #menuBtn { display: inline-flex; }
  #brand small, #run .lbl, #chg, .tb .wide { display: none; }
  #top { flex-wrap: wrap; gap: 8px; }
  #status .legend { display: none; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
@media print {
  body { display: block; overflow: visible; height: auto; }
  #top, #nav, #status, #sheetbar .tb, #toast { display: none !important; }
  #canvas { overflow: visible; padding: 0; }
  #paper { box-shadow: none; }
}

/* server calculation status */
#paper .loading { padding: 32px; font: 600 15px var(--body); color: var(--muted); }
#calcTime[data-state="busy"] { color: var(--muted); }
#calcTime[data-state="error"] { color: var(--accent); font-weight: 600; }
#retryBtn { padding: 1px 8px; font-size: 12px; }

/* account + saved runs */
#top form.acct { display: inline-flex; align-items: center; gap: 8px; margin: 0; }
#top .who { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 12px; }
dialog .item.cur { border-color: var(--accent); background: var(--accent-soft); }
@media (max-width: 1100px) { #top .who { display: none; } }
