:root {
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, system-ui, "Helvetica Neue", sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --ok: #3FB950; --stale: #E3A008; --down: #F85149; --shade: rgba(0, 0, 0, .6);
  color-scheme: dark;
}
[data-theme="ink"] {
  --bg: #0B0F14; --text: #ECE6D8; --muted: #98A3AF; --line: #222C38; --chip: #151C25; --raise: #111720;
  --pos: #FFC85A; --neg: #93A6FF; --spot: #FFFFFF; --spot-t: #0B0F14; --flip: #C9CFD6; --king: #FFFFFF; --king-t: #0B0F14;
}
[data-theme="paper"] {
  --bg: #F5F1E8; --text: #1D1B18; --muted: #615A50; --line: #DCD3C4; --chip: #ECE6DA; --raise: #FBF8F2;
  --pos: #0A5A53; --neg: #A8441A; --spot: #1D1B18; --spot-t: #F5F1E8; --flip: #615A50; --king: #1D1B18; --king-t: #F5F1E8;
  --ok: #1F8A3E; --stale: #B7791F; --down: #C53030; --shade: rgba(70, 50, 20, .22);
  color-scheme: light;
}
[data-theme="spectral"] {
  --bg: #09090D; --text: #EEEEF2; --muted: #9C9CAB; --line: #22222C; --chip: #15151C; --raise: #101016;
  --pos: #E8FF4F; --neg: #A47BFF; --spot: #5CE1E6; --spot-t: #09090D; --flip: #B4B4C2; --king: #FFFFFF; --king-t: #09090D;
}

[data-layout="full"] {
  --pad: 28px; --sw: 92px; --cw: 92px; --cmax: 240px; --aw: 184px; --rh: 28px; --hh: 44px;
  --fs: 12px; --ks: 12.5px; --tagfs: 8.5px; --pillfs: 10px; --pillh: 15px;
}
[data-layout="phone"] {
  --pad: 12px; --sw: 60px; --cw: 54px; --cmax: 200px; --aw: 76px; --rh: 24px; --hh: 36px;
  --fs: 10.5px; --ks: 11px; --tagfs: 7.5px; --pillfs: 8.5px; --pillh: 11px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text); font: 14px/1.3 var(--sans);
  -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: 100%; overflow: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.cap { font-size: 10.5px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
[hidden] { display: none !important; }

.app {
  display: flex; flex-direction: column; height: 100vh; height: 100dvh;
  padding: env(safe-area-inset-top) calc(var(--pad) + env(safe-area-inset-right)) 0 calc(var(--pad) + env(safe-area-inset-left));
  max-width: 1480px; margin: 0 auto;
}

/* top bar */
.bar { display: flex; align-items: center; gap: 10px; min-height: 52px; padding-top: 4px; }
.word { font: italic 400 24px/1 var(--serif); letter-spacing: -.01em; padding-right: 4px; }
.title { font-size: 14px; font-weight: 600; color: var(--muted); margin-right: auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status {
  display: inline-flex; align-items: center; gap: 7px; height: 44px; padding: 0 8px; border-radius: 8px;
  font-size: 12px; color: var(--muted); white-space: nowrap;
}
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.status[data-s="ok"] .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); animation: pulse 2.4s ease-in-out infinite; }
.status[data-s="stale"] .dot { background: var(--stale); }
.status[data-s="down"] .dot { background: var(--down); }
.status[data-s="stale"] .sw { color: var(--stale); }
.status[data-s="stale"] #updated { color: var(--stale); }
.status[data-s="down"] .sw { color: var(--down); }
.status .sw { font-weight: 600; font-size: 11px; letter-spacing: .06em; text-transform: uppercase; }
@keyframes pulse { 50% { box-shadow: 0 0 0 5px color-mix(in srgb, var(--ok) 0%, transparent); } }
.icon { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 10px; color: var(--text); flex: none; }
.icon:active, .status:active { background: var(--chip); }

/* segmented controls */
.seg { display: inline-flex; padding: 3px; gap: 2px; background: var(--chip); border-radius: 10px; flex: none; }
.seg button {
  min-height: 38px; padding: 0 12px; border-radius: 8px; font-size: 13px; font-weight: 600; color: var(--muted);
  white-space: nowrap; transition: background-color .15s, color .15s;
}
.seg button[aria-pressed="true"] { background: var(--bg); color: var(--text); box-shadow: 0 1px 0 var(--line), 0 0 0 1px var(--line); }
.seg button.missing { opacity: .45; }
.seg.grow { flex: 1 1 auto; }
.seg.grow button { flex: 1 1 0; }

/* stats */
.stats {
  display: grid; grid-template-columns: repeat(6, minmax(0, auto)); justify-content: space-between; gap: 12px 24px;
  padding: 14px 0; margin-top: 4px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
}
.stat { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.stat .v { font-size: 17px; font-weight: 600; white-space: nowrap; }
.stat.spot .v { font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
.stat .sub { font-size: 11.5px; font-weight: 500; color: var(--muted); margin-left: 8px; }

.controls { display: flex; align-items: center; gap: 10px; padding: 12px 0 8px; }
.controls .seg.grow { flex: 0 1 460px; }
.pillbtn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 44px; padding: 0 14px;
  border-radius: 10px; border: 1px solid var(--line); font-size: 13px; font-weight: 600; flex: none;
}
.pillbtn:active { background: var(--chip); }

.banner {
  margin: 2px 0 6px; padding: 8px 12px; border-radius: 8px; font-size: 12.5px;
  background: color-mix(in srgb, var(--stale) 14%, var(--bg)); color: var(--text); border: 1px solid color-mix(in srgb, var(--stale) 40%, transparent);
}
.banner[data-s="down"] { background: color-mix(in srgb, var(--down) 14%, var(--bg)); border-color: color-mix(in srgb, var(--down) 40%, transparent); }

/* grid */
.wrap { position: relative; flex: 1 1 auto; min-height: 0; display: flex; }
.scroller {
  flex: 1; min-width: 0; overflow: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  outline: none; scrollbar-width: thin; scrollbar-color: var(--line) transparent;
}
.scroller.blank { visibility: hidden; }
.sheet {
  position: relative; display: grid;
  grid-template-columns: var(--sw) repeat(var(--n), minmax(var(--cw), 1fr)) var(--aw);
  grid-template-rows: var(--hh); grid-auto-rows: var(--rh);
  min-width: calc(var(--sw) + var(--n) * var(--cw) + var(--aw));
  max-width: calc(var(--sw) + var(--n) * var(--cmax) + var(--aw));
  font-family: var(--mono); font-variant-numeric: tabular-nums;
}
.h, .k, .c, .a { border: 1px solid var(--bg); background-clip: padding-box; }

.h {
  position: sticky; top: 0; z-index: 5; background: var(--bg); background-clip: border-box;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end; gap: 2px; padding-bottom: 6px;
  box-shadow: 0 1px 0 var(--line); font-family: var(--sans);
}
.h .d { font-size: 12.5px; font-weight: 600; color: var(--text); white-space: nowrap; }
.h .t { font-family: var(--mono); font-size: 10px; color: var(--muted); white-space: nowrap; }
.h.corner { left: 0; z-index: 7; align-items: flex-end; padding-right: 10px; font-size: 10px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.h.all { right: 0; z-index: 7; align-items: flex-start; padding-left: 10px; }

.k {
  position: sticky; left: 0; z-index: 3; background: var(--bg); background-clip: border-box;
  display: flex; align-items: center; justify-content: flex-end; gap: 5px; padding-right: 10px;
  font-size: var(--ks); color: var(--muted); cursor: pointer;
}
.k.mark { color: var(--text); font-weight: 700; }
.k[data-tag]:not([data-tag=""])::before {
  content: attr(data-tag); font-size: var(--tagfs); font-weight: 700; letter-spacing: .04em; line-height: 1;
  padding: 2px 3px; border-radius: 2px; color: var(--muted); border: 1px solid var(--line);
}
.k[data-tag="CW"]::before { color: var(--pos); border-color: color-mix(in srgb, var(--pos) 45%, transparent); }
.k[data-tag="PW"]::before { color: var(--neg); border-color: color-mix(in srgb, var(--neg) 45%, transparent); }

.c {
  position: relative; display: flex; align-items: center; justify-content: center; border-radius: 3px;
  font-size: var(--fs); font-weight: 500; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; cursor: pointer;
}
.c.nil { cursor: default; }
.c.emax::after {
  content: ""; position: absolute; top: 0; right: 0; border-style: solid; border-width: 0 6px 6px 0;
  border-color: transparent currentColor transparent transparent; opacity: .7;
}
.c.king { overflow: visible; z-index: 2; font-weight: 700; outline: 2px solid var(--king); outline-offset: -1px; }
.c.king::before {
  content: "KING"; position: absolute; top: 50%; left: 4px; transform: translateY(-50%); padding: 2px 3px; border-radius: 2px;
  background: var(--king); color: var(--king-t); font-size: 7.5px; font-weight: 800; letter-spacing: .08em; line-height: 1.1;
}

.a {
  position: sticky; right: 0; z-index: 3; display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: 8px;
  padding: 0 8px; background: var(--chip); border-radius: 3px; cursor: pointer;
}
.a .n { font-size: var(--fs); font-weight: 600; text-align: right; min-width: 5.2ch; }
.a.pos .n { color: var(--pos); }
.a.neg .n { color: var(--neg); }
.a .b { height: 9px; display: flex; }
.a .b i { display: block; height: 100%; border-radius: 1px; background: currentColor; opacity: .75; }
.a.pos .b { color: var(--pos); }
.a.neg .b { color: var(--neg); }

.sl .k { box-shadow: 8px 0 8px -8px var(--shade); }
.sl .h.corner { box-shadow: 0 1px 0 var(--line), 8px 0 8px -8px var(--shade); }
.sr .a { box-shadow: -8px 0 8px -8px var(--shade); }
.sr .h.all { box-shadow: 0 1px 0 var(--line), -8px 0 8px -8px var(--shade); }
.line { position: absolute; left: 0; right: 0; height: 0; z-index: 4; pointer-events: none; }
.line::before { content: ""; position: absolute; left: 0; right: 0; top: -1px; height: 2px; background: var(--spot); }
.line.flip::before { top: 0; height: 0; background: none; border-top: 1px dashed var(--flip); }
.line .tag { position: sticky; left: 0; width: var(--sw); height: 0; display: flex; align-items: center; padding-left: 2px; }
.pill {
  flex: none; height: var(--pillh); line-height: var(--pillh); padding: 0 5px; border-radius: 3px;
  font-size: var(--pillfs); font-weight: 800; white-space: nowrap;
}
.spot .pill { background: var(--spot); color: var(--spot-t); }
.flip .pill { background: var(--bg); color: var(--text); border: 1px dashed var(--flip); font-weight: 600; line-height: calc(var(--pillh) - 2px); }
.flip.quiet .pill { display: none; }
[data-layout="phone"] .pill .fv { display: none; }

.empty { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; color: var(--muted); font-size: 14px; padding: 24px; }

/* legend */
.legend {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 10px 0 calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); font-size: 12px; color: var(--muted);
}
.legend .ends { color: var(--text); font-size: 12px; }
.ramp { display: inline-flex; gap: 1px; }
.ramp i { width: 16px; height: 10px; }
.ramp i:first-child { border-radius: 2px 0 0 2px; }
.ramp i:last-child { border-radius: 0 2px 2px 0; }
.lt { margin-left: 8px; }

/* detail card */
.detail {
  position: fixed; z-index: 20; right: calc(16px + env(safe-area-inset-right)); bottom: calc(56px + env(safe-area-inset-bottom));
  width: min(320px, calc(100vw - 32px)); padding: 12px 14px 12px; border-radius: 14px;
  background: var(--raise); border: 1px solid var(--line); box-shadow: 0 12px 40px rgba(0, 0, 0, .35);
}
.dh { display: flex; align-items: baseline; gap: 10px; margin-bottom: 8px; }
.dk { font-size: 18px; font-weight: 700; }
.dx { font-size: 12px; color: var(--muted); flex: 1; }
.dclose { width: 44px; height: 36px; margin: -8px -10px -8px 0; font-size: 22px; color: var(--muted); align-self: center; }
.detail dl { display: grid; grid-template-columns: auto 1fr; gap: 5px 16px; margin: 0; font-size: 13px; }
.detail dt { color: var(--muted); }
.detail dd { margin: 0; text-align: right; }
.detail dd.pos { color: var(--pos); }
.detail dd.neg { color: var(--neg); }

/* settings */
dialog {
  width: min(420px, calc(100vw - 24px)); max-height: calc(100dvh - 40px); padding: 0; border-radius: 16px;
  background: var(--raise); color: var(--text); border: 1px solid var(--line); box-shadow: 0 20px 60px rgba(0, 0, 0, .45);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog form { display: flex; flex-direction: column; gap: 10px; padding: 14px 16px calc(18px + env(safe-area-inset-bottom)); }
dialog .seg { display: flex; }
dialog .seg button { flex: 1; min-height: 40px; }
dialog label.cap, dialog .cap { margin-top: 6px; }
.sh { display: flex; align-items: center; justify-content: space-between; }
.sh .cap { margin: 0; }
.done { height: 44px; padding: 0 6px; font-weight: 600; color: var(--text); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-top: 8px; }
.rt { font-weight: 600; font-size: 14px; }
.hint { margin: 2px 0 0; font-size: 12px; line-height: 1.45; color: var(--muted); }
.switch { flex: none; width: 52px; height: 44px; position: relative; }
.switch::before { content: ""; position: absolute; left: 0; right: 0; top: 7px; height: 30px; border-radius: 15px; background: var(--chip); border: 1px solid var(--line); transition: background-color .15s; }
.switch i { position: absolute; left: 3px; top: 10px; width: 24px; height: 24px; border-radius: 50%; background: var(--muted); transition: transform .15s, background-color .15s; }
.switch[aria-checked="true"]::before { background: color-mix(in srgb, var(--pos) 35%, var(--chip)); }
.switch[aria-checked="true"] i { transform: translateX(22px); background: var(--text); }

/* sign-in gate (cloud) */
.gate {
  position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; overflow-y: auto; background: var(--bg);
  padding: calc(24px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) calc(24px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
}
.gate[hidden] { display: none; }
.gate-in { display: flex; flex-direction: column; align-items: center; gap: 10px; width: min(400px, 100%); text-align: center; }
.gate .gate-in .word { font-size: 32px; margin: 0; padding: 0; }
.gate-in .hint { margin-bottom: 8px; }
#signin { width: 100%; display: flex; justify-content: center; }
#acct .pillbtn { height: 40px; }

/* phone layout */
[data-layout="phone"] .title, [data-layout="phone"] .wsub, [data-layout="phone"] .long,
[data-layout="phone"] .h .dow, [data-layout="phone"] .status .sw { display: none; }
[data-layout="phone"] .status[data-s="stale"] .sw, [data-layout="phone"] .status[data-s="down"] .sw { display: inline; }
[data-layout="phone"] .bar { min-height: 48px; gap: 6px; padding-top: 2px; }
[data-layout="phone"] .word { font-size: 21px; margin-right: auto; }
[data-layout="phone"] .seg button { min-height: 36px; padding: 0 11px; }
[data-layout="phone"] .bar .seg button { min-height: 32px; }
[data-layout="phone"] .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0; padding: 0; margin-top: 2px; }
[data-layout="phone"] .stat { padding: 6px 0 6px 10px; border-left: 1px solid var(--line); gap: 1px; }
[data-layout="phone"] .stat:nth-child(3n + 1) { border-left: 0; padding-left: 0; }
[data-layout="phone"] .stat:nth-child(n + 4) { border-top: 1px solid var(--line); }
[data-layout="phone"] .stat .cap { font-size: 9.5px; }
[data-layout="phone"] .stat .v { font-size: 15px; }
[data-layout="phone"] .stat.spot .v { font-size: 15px; font-weight: 700; }
[data-layout="phone"] .stat .sub { font-size: 10px; margin-left: 5px; }
[data-layout="phone"] .controls { padding: 8px 0 6px; gap: 8px; }
[data-layout="phone"] .controls .seg.grow { flex: 1 1 auto; }
[data-layout="phone"] .pillbtn { height: 42px; padding: 0 12px; }
[data-layout="phone"] .h { padding-bottom: 4px; }
[data-layout="phone"] .h .d { font-size: 11.5px; }
[data-layout="phone"] .h .t { font-size: 9px; }
[data-layout="phone"] .h.corner { font-size: 8.5px; padding-right: 7px; }
[data-layout="phone"] .k { padding-right: 7px; gap: 3px; }
[data-layout="phone"] .h.all { padding-left: 7px; }
[data-layout="phone"] .a { padding: 0 5px; gap: 5px; }
[data-layout="phone"] .a .n { min-width: 4.6ch; }
[data-layout="phone"] .a .b { height: 7px; }
[data-layout="phone"] .c.emax::after { border-width: 0 5px 5px 0; }
[data-layout="phone"] .c.king::before { content: "K"; top: 2px; left: 2px; transform: none; padding: 1px 2px; font-size: 6.5px; }
[data-layout="phone"] .legend { padding-top: 7px; font-size: 10.5px; gap: 6px; }
[data-layout="phone"] .ramp i { width: 11px; height: 8px; }
[data-layout="phone"] .lt { margin-left: 2px; }
[data-layout="phone"] .detail { left: calc(12px + env(safe-area-inset-left)); right: calc(12px + env(safe-area-inset-right)); width: auto; bottom: calc(44px + env(safe-area-inset-bottom)); }

/* phone landscape: levels and view controls share one row */
@media (orientation: landscape) and (max-height: 520px) {
  [data-layout="phone"] .app { display: grid; grid-template-columns: minmax(0, 1fr) auto; grid-template-rows: auto auto auto minmax(0, 1fr); column-gap: 14px; }
  [data-layout="phone"] .bar, [data-layout="phone"] .banner, [data-layout="phone"] .wrap { grid-column: 1 / -1; }
  [data-layout="phone"] .bar { min-height: 44px; }
  [data-layout="phone"] .stats { grid-template-columns: repeat(6, minmax(0, auto)); justify-content: space-between; border-bottom: 0; margin: 0; }
  [data-layout="phone"] .stat:nth-child(n) { border: 0; padding: 6px 0; }
  [data-layout="phone"] .controls { border-top: 1px solid var(--line); padding: 4px 0; }
  [data-layout="phone"] .controls .seg button { min-height: 34px; padding: 0 10px; }
  [data-layout="phone"] .pillbtn { height: 42px; }
  [data-layout="phone"] .banner { display: none !important; }
  [data-layout="phone"] .status .sw { display: inline; }
  [data-layout="phone"] .wrap { border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
  [data-layout="phone"] .legend { display: none; }
}

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
