:root {
  color-scheme: dark;
  --bg: #0f100e;
  --surface: #151613;
  --surface-2: #1c1e1a;
  --hover: #23261f;
  --field: #0b0c0a;
  --rule: #2a2d27;
  --rule-strong: #3b3f36;
  --text: #e8e6df;
  --muted: #9a9889;
  --faint: #66665a;
  --accent: #c5ef4b;
  --accent-hover: #d7f77e;
  --accent-soft: rgba(197, 239, 75, 0.1);
  --on-accent: #141a05;
  --err: #ff7a57;
  --overlay: rgba(0, 0, 0, 0.55);
  --xp: #6fc3c9;
  --flag: #e39a6b;

  --sans: "Chivo", system-ui, sans-serif;
  --mono: "Chivo Mono", ui-monospace, "Cascadia Code", monospace;
  --radius: 3px;
  --sidebar-w: 240px;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #f3f0e8;
    --surface: #fbfaf6;
    --surface-2: #ece8dd;
    --hover: #e6e1d4;
    --field: #fffefb;
    --rule: #dcd6c8;
    --rule-strong: #c2bba9;
    --text: #1b1c17;
    --muted: #5f5d52;
    --faint: #8e8b7d;
    --accent: #3f7d12;
    --accent-hover: #316509;
    --accent-soft: rgba(63, 125, 18, 0.09);
    --on-accent: #fbfaf6;
    --err: #c2410c;
    --overlay: rgba(27, 28, 23, 0.3);
    --xp: #1f7a82;
    --flag: #b4532a;
  }
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f3f0e8;
  --surface: #fbfaf6;
  --surface-2: #ece8dd;
  --hover: #e6e1d4;
  --field: #fffefb;
  --rule: #dcd6c8;
  --rule-strong: #c2bba9;
  --text: #1b1c17;
  --muted: #5f5d52;
  --faint: #8e8b7d;
  --accent: #3f7d12;
  --accent-hover: #316509;
  --accent-soft: rgba(63, 125, 18, 0.09);
  --on-accent: #fbfaf6;
  --err: #c2410c;
  --overlay: rgba(27, 28, 23, 0.3);
  --xp: #1f7a82;
  --flag: #b4532a;
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }
code { font-family: var(--mono); font-size: 0.88em; color: var(--text); }
:focus-visible { outline: 1px solid var(--accent); outline-offset: 2px; }
.mono { font-family: var(--mono); }

/* shell */

.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: 100vh;
}
.main { padding: 36px 40px 64px; }
.content { max-width: 1120px; }
.topbar, .backdrop { display: none; }

/* sidebar */

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 22px 0 16px;
  background: var(--bg);
  border-right: 1px solid var(--rule);
  overflow-y: auto;
}

.brand {
  padding: 0 20px 18px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.brand:hover { text-decoration: none; }
.brand .slash { color: var(--accent); }

.nav-section { display: grid; margin-top: 18px; }
.nav-label { padding: 0 20px 6px; font-family: var(--mono); font-size: 11px; color: var(--faint); }
.nav-link {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr) auto;
  align-items: center;
  padding: 5px 20px 5px 18px;
  border-left: 2px solid transparent;
  color: var(--muted);
}
.nav-link:hover { color: var(--text); background: var(--hover); text-decoration: none; }
.nav-link[aria-current="page"] { color: var(--text); border-left-color: var(--accent); background: var(--accent-soft); }
.nav-link.soon { color: var(--faint); cursor: default; }
.nav-link.soon:hover { background: none; }
.nav-tag { font-family: var(--mono); font-size: 11px; color: var(--faint); }
.nav-link[aria-current="page"] .nav-tag { color: var(--accent); }
.nav-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-meta { font-family: var(--mono); font-size: 10px; color: var(--faint); }

.sidebar-foot {
  margin-top: auto;
  padding: 14px 20px 0;
  border-top: 1px solid var(--rule);
  display: grid;
  gap: 12px;
}
.status-line { display: flex; align-items: center; gap: 8px; font-family: var(--mono); font-size: 11px; color: var(--muted); }
.dot { width: 7px; height: 7px; background: var(--faint); flex: none; }
.dot.on { background: var(--accent); }

.sidebar-foot button { justify-content: center; }
.sync-error { font-size: 11px; line-height: 1.4; color: var(--err); }

.theme-toggle { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--rule); border-radius: var(--radius); }
.theme-toggle button {
  padding: 3px 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--faint);
  font-family: var(--mono);
  font-size: 11px;
  justify-content: center;
}
.theme-toggle button + button { border-left: 1px solid var(--rule); }
.theme-toggle button:hover { background: var(--hover); color: var(--text); }
.theme-toggle button[aria-pressed="true"] { color: var(--accent); background: var(--accent-soft); }

/* page */

.page-head { margin-bottom: 28px; padding-bottom: 18px; border-bottom: 1px solid var(--rule); }
h1 { font-size: 26px; font-weight: 700; letter-spacing: -0.025em; line-height: 1.2; }
.page-head p { margin-top: 6px; color: var(--muted); max-width: 60ch; }
h2 { font-size: 15px; font-weight: 700; margin-bottom: 14px; }
.section-title {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 36px 0 14px;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
}
.section-title::after { content: ""; flex: 1; height: 1px; background: var(--rule); }
.muted { color: var(--muted); font-size: 13px; }

.card { padding: 22px 24px; background: var(--surface); border: 1px solid var(--rule); border-radius: var(--radius); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.actions { margin-top: 14px; }
.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 22px 0 14px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--faint);
}
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--rule); }

/* controls */

button, .button, input[type="file"]::file-selector-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
button:hover, .button:hover, input[type="file"]::file-selector-button:hover { border-color: var(--muted); background: var(--hover); text-decoration: none; }
button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 700; }
button.primary:hover, .button.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
button.ghost { border-color: transparent; color: var(--muted); }
button.ghost:hover { color: var(--text); border-color: transparent; }
button:disabled, button:disabled:hover { opacity: 0.45; cursor: not-allowed; }
input[type="file"] { font-size: 12px; color: var(--muted); font-family: var(--mono); }
input[type="file"]::file-selector-button { margin-right: 10px; font-family: var(--sans); }

input[type="text"], textarea {
  width: 100%;
  padding: 8px 10px;
  background: var(--field);
  color: var(--text);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  font: inherit;
  font-size: 13px;
}
input[type="text"]:focus, textarea:focus { outline: none; border-color: var(--accent); }
textarea { min-height: 110px; margin: 10px 0 12px; font-family: var(--mono); font-size: 12px; resize: vertical; }
.grow { flex: 1; min-width: 12rem; width: auto; }

.alert { margin-top: 16px; padding: 2px 0 2px 12px; border-left: 2px solid; font-size: 13px; }
.alert.ok { border-color: var(--accent); color: var(--text); }
.alert.err { border-color: var(--err); color: var(--err); }

.tag {
  display: inline-block;
  padding: 0 5px;
  border: 1px solid var(--rule-strong);
  border-radius: 2px;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 10px;
  line-height: 16px;
  white-space: nowrap;
}
.tag.live { border-color: var(--accent); color: var(--accent); }

/* sign-in */

.login-panel {
  display: grid;
  gap: 10px;
  margin-top: 16px;
  padding: 14px 16px;
  background: var(--surface-2);
  border-left: 2px solid var(--accent);
}
.login-panel > button { justify-self: start; }
.device-code {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 10px;
  border: 1px solid var(--rule-strong);
  background: var(--field);
  color: var(--accent);
  font-family: var(--mono);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.14em;
  user-select: all;
}

/* hairline grids: tiles share 1px rules instead of floating as cards */

.tool-grid, .stats { display: grid; border-top: 1px solid var(--rule); border-left: 1px solid var(--rule); }
.tool-card, .stat { border-right: 1px solid var(--rule); border-bottom: 1px solid var(--rule); }
.tool-grid { grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
.stats { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }

.tool-card { display: grid; align-content: start; gap: 6px; padding: 16px 18px 18px; background: var(--surface); color: var(--text); }
a.tool-card:hover { background: var(--hover); text-decoration: none; }
a.tool-card:hover .tool-title { color: var(--accent); }
.tool-card.planned { color: var(--faint); }
.tool-card.planned .tool-desc { color: var(--faint); }
.tool-head { display: flex; justify-content: space-between; align-items: baseline; font-family: var(--mono); font-size: 11px; color: var(--faint); }
.tool-title { margin-top: 6px; font-size: 15px; font-weight: 700; letter-spacing: -0.01em; }
.tool-desc { font-size: 13px; color: var(--muted); }

.stat { padding: 14px 16px 16px; background: var(--surface); }
.stat-label { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.stat-value { margin-top: 6px; font-family: var(--mono); font-size: 26px; font-weight: 500; letter-spacing: -0.03em; line-height: 1; }
.stat-sub { margin-top: 8px; font-family: var(--mono); font-size: 11px; color: var(--faint); }
.delta { color: var(--muted); }
.delta.up { color: var(--accent); }
.delta.down { color: var(--err); }

/* tables */

.table-wrap { overflow: auto; max-height: 72vh; border: 1px solid var(--rule); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; }
th, td { padding: 6px 12px; border-bottom: 1px solid var(--rule); white-space: nowrap; text-align: left; background: var(--surface); }
th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  color: var(--muted);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 500;
}
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--hover); }
td.num, th.num { text-align: right; }
td.num { font-family: var(--mono); font-size: 12px; }
td.zero { color: var(--faint); }
.sticky-col { position: sticky; left: 0; z-index: 1; border-right: 1px solid var(--rule); }
th.sticky-col { z-index: 2; }

.empty { padding: 36px 24px; }
.empty p { color: var(--muted); margin-bottom: 14px; }

/* narrow screens: sidebar becomes a drawer */

@media (max-width: 820px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .main { padding: 22px 16px 40px; }

  .topbar {
    display: flex;
    align-items: center;
    gap: 4px;
    position: sticky;
    top: 0;
    z-index: 20;
    padding: 8px 10px;
    background: var(--bg);
    border-bottom: 1px solid var(--rule);
  }
  .topbar .brand { padding: 0; }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 40;
    width: min(280px, 85vw);
    transform: translateX(-100%);
    transition: transform 0.18s ease;
  }
  .nav-open .sidebar { transform: none; }
  .nav-open .backdrop { display: block; position: fixed; inset: 0; z-index: 30; background: var(--overlay); }
}

/* cog board */

.priorities .muted { margin-top: 4px; }
.sliders { display: grid; gap: 10px; margin-top: 16px; max-width: 520px; }
.slider { display: grid; grid-template-columns: 110px minmax(0, 1fr) 24px; align-items: center; gap: 14px; }
.slider.off { opacity: 0.45; }
.slider-label { font-size: 13px; }
.slider-value { font-family: var(--mono); font-size: 13px; text-align: right; color: var(--accent); }
input[type="range"] { width: 100%; accent-color: var(--accent); cursor: pointer; }
input[type="range"]:disabled { cursor: not-allowed; }

.progress { width: 160px; height: 6px; background: var(--surface-2); border: 1px solid var(--rule); }
.progress-fill { height: 100%; background: var(--accent); }
.result-note { margin-top: 12px; }
.board-toggle { margin-bottom: 12px; gap: 0; }
.board-toggle button { border-radius: 0; font-family: var(--mono); font-size: 12px; }
.board-toggle button:first-child { border-radius: var(--radius) 0 0 var(--radius); }
.board-toggle button + button { margin-left: -1px; border-radius: 0 var(--radius) var(--radius) 0; }
.board-toggle button.active { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); position: relative; }

.board-layout { display: grid; gap: 20px; align-items: start; }
.board-layout.with-moves { grid-template-columns: minmax(0, 1fr) 280px; }

.cog-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  border-top: 1px solid var(--rule);
  border-left: 1px solid var(--rule);
  max-width: 820px;
}
.cell {
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  min-width: 0;
  border-right: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  background: var(--surface);
  font-family: var(--mono);
  line-height: 1.1;
  overflow: hidden;
}
.cell-sprite { width: 74%; image-rendering: pixelated; pointer-events: none; }
.cell-name { font-size: 11px; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 92%; }
.cell-boost {
  position: absolute;
  right: 2px;
  bottom: 1px;
  padding: 0 2px;
  font-size: 11px;
  font-weight: 600;
  background: color-mix(in srgb, var(--surface) 75%, transparent);
}
.cell.build .cell-boost { color: var(--accent); }
.cell.xp .cell-boost { color: var(--xp); }
.cell.flag .cell-boost { color: var(--flag); }
.cell.player { background: var(--accent-soft); }
.cell.locked { background: repeating-linear-gradient(135deg, var(--bg) 0 4px, var(--surface-2) 4px 5px); }
.cell.moved { box-shadow: inset 0 0 0 1px var(--accent); }
.cell.done { outline: 1px dashed var(--faint); outline-offset: -4px; }
.cell.from { box-shadow: inset 0 0 0 3px var(--flag); background: color-mix(in srgb, var(--flag) 18%, var(--surface)); }
.cell.to { box-shadow: inset 0 0 0 3px var(--accent); background: color-mix(in srgb, var(--accent) 18%, var(--surface)); }
/* stepping: everything but the move itself fades back */
.cog-grid.focus .cell:not(.from):not(.to) > * { opacity: 0.3; }

.legend { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 16px; margin-top: 12px; font-size: 12px; }
.key { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 10px; height: 10px; }
.swatch.build { background: var(--accent); }
.swatch.xp { background: var(--xp); }
.swatch.flag { background: var(--flag); }
.swatch.moved { box-shadow: inset 0 0 0 1px var(--accent); }
.swatch.done { outline: 1px dashed var(--muted); outline-offset: -1px; }
.swatch.from { box-shadow: inset 0 0 0 2px var(--flag); }
.swatch.to { box-shadow: inset 0 0 0 2px var(--accent); }

.step-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: var(--surface-2);
  border-left: 2px solid var(--accent);
}
.step-bar button { padding: 2px 10px; font-family: var(--mono); }
.step-count { font-size: 12px; color: var(--muted); min-width: 44px; text-align: center; }
.step-text { font-size: 13px; }
.mark-from { color: var(--flag); }
.mark-to { color: var(--accent); }

.moves { position: sticky; top: 16px; border: 1px solid var(--rule); background: var(--surface); }
.moves-head { padding: 8px 12px; font-size: 11px; color: var(--muted); border-bottom: 1px solid var(--rule); background: var(--surface-2); }
.move-list { margin: 0; padding: 0; list-style: none; max-height: 560px; overflow-y: auto; }
.move {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr);
  grid-template-rows: auto auto;
  column-gap: 8px;
  width: 100%;
  padding: 6px 12px;
  border: 0;
  border-bottom: 1px solid var(--rule);
  border-radius: 0;
  text-align: left;
}
.move:hover { background: var(--hover); border-color: var(--rule); }
.move.active { background: var(--accent-soft); box-shadow: inset 2px 0 0 var(--accent); }
.move-n { grid-row: span 2; font-size: 11px; color: var(--faint); padding-top: 1px; }
.move-cog { font-size: 13px; }
.move-path { font-size: 11px; color: var(--muted); }

/* jelly operator */

.jelly-grid { max-width: 700px; }
.jelly-grid .cell { background: var(--surface-2); }
.jelly-grid .cell.open, .jelly-grid .cell.taken { background: var(--surface); }
.jelly-grid .cell.obstruction { background: color-mix(in srgb, var(--err) 14%, var(--surface)); border-color: transparent; }
.jelly-grid .cell.planned { background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.jelly-grid .cell.organelle::after, .jelly-grid .cell.infected::before {
  content: "";
  position: absolute;
  top: 1px;
  width: 3px;
  height: 3px;
  z-index: 2;
}
.jelly-grid .cell.organelle::after { left: 1px; background: var(--accent); }
.jelly-grid .cell.infected::before { right: 1px; background: var(--flag); }
.jelly-unit { width: 100%; height: 100%; z-index: 1; image-rendering: pixelated; pointer-events: none; }
.swatch.open { background: var(--surface); box-shadow: inset 0 0 0 1px var(--rule-strong); }
.swatch.locked { background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--rule); }
.swatch.planned { background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.swatch.organelle { background: var(--accent); }
.swatch.infected { background: var(--flag); }

.jelly-target { display: flex; align-items: center; gap: 12px; margin-top: 16px; }
.jelly-target select {
  padding: 6px 10px;
  max-width: 360px;
  color: var(--text);
  background: var(--field);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  font-family: var(--mono);
  font-size: 12px;
}
.jelly-inputs { margin-top: 14px; font-family: var(--mono); font-size: 11px; color: var(--faint); }
.jelly-notes { margin: 14px 0 0; padding-left: 18px; font-size: 13px; }
.jelly-notes li { margin-top: 4px; }
.cell-sprite.inline { width: 18px; height: 18px; margin-right: 8px; vertical-align: -4px; }

@media (max-width: 1100px) {
  .board-layout.with-moves { grid-template-columns: minmax(0, 1fr); }
  .moves { position: static; }
  .move-list { max-height: 320px; }
}

@media (max-width: 820px) {
  .jelly-grid .cell.organelle::after, .jelly-grid .cell.infected::before { width: 2px; height: 2px; }
  .cell-name { font-size: 8px; }
  .cell-boost { font-size: 8px; right: 0; bottom: 0; padding: 0 1px; }
  .slider { grid-template-columns: 84px minmax(0, 1fr) 20px; gap: 10px; }
}