/* See DESIGN.md: neutral graphite, piece colours are the only saturated
   colours, hairlines instead of cards, no gradients or shadows. */
:root {
  color-scheme: dark;
  --bg: #101113; --panel: #16171a; --field: #0b0c0e;
  --line: #26282d; --line-strong: #383b42;
  --text: #e8e8ea; --muted: #8d9099;
  --bad: #e5484d; --warn: #d9a441; --ok: #4fbf7a;
  --radius: 6px;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

button, select, input[type=number], .file {
  font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--line-strong); border-radius: var(--radius);
  padding: 5px 10px; line-height: 20px;
}
button, select, .file { cursor: pointer; }
button:hover, select:hover, .file:hover { border-color: #50545c; background: #1c1d21; }
button:disabled, select:disabled { opacity: .45; cursor: default; }
button:focus-visible, select:focus-visible, input:focus-visible, .file:focus-within { outline: 2px solid var(--text); outline-offset: 1px; }
.primary { background: var(--text); color: var(--bg); border-color: var(--text); }
.primary:hover { background: #fff; }
.file input { display: none; }
input[type=number] { width: 64px; padding: 3px 6px; font-variant-numeric: tabular-nums; }

/* toolbar */
.bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 8px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.bar label { color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
#who { max-width: 170px; }
#match { max-width: 250px; }
#round { max-width: 170px; }
#player { max-width: 130px; }
.spacer { margin-left: auto; }
.app { font-weight: 650; margin-right: 8px; }
.score { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.score b { color: var(--text); font-weight: 650; }

/* stage: fields + timeline on the left, side column on the right */
.stage { display: grid; grid-template-columns: minmax(0, 1fr) 320px; grid-template-areas: "fields side" "track side"; max-width: 1760px; margin: 0 auto; }
.fields { grid-area: fields; display: grid; grid-template-columns: 1fr 1fr; gap: 24px; padding: 16px 16px 8px; }
.track { grid-area: track; padding: 0 16px 16px; }
.side { grid-area: side; border-left: 1px solid var(--line); min-height: calc(100vh - 49px); }

/* fields */
.field { position: relative; min-width: 0; }
/* the name row sits in the spawn rows, beside the spawning piece */
.field-head { position: absolute; top: 0; left: 0; right: 0; z-index: 1; display: flex; gap: 8px; pointer-events: none; }
.pname { font-weight: 650; max-width: 30%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pending { margin-left: auto; color: var(--bad); font-variant-numeric: tabular-nums; }
.field.off-script .pname::after { content: " · 自由模式"; color: var(--warn); font-weight: 500; }
.playfield { --cell: 18px; --board-w: calc((100vh - 200px) * 400 / 920); display: grid; grid-template-columns: 70px minmax(0, max(220px, var(--board-w))) 70px; justify-content: center; gap: 10px; align-items: start; }
.playfield > .side-box { margin-top: var(--buffer, 0px); }
canvas { display: block; width: 100%; height: auto; }
.side-box { display: flex; flex-direction: column; align-items: center; padding-top: 2px; min-width: 0; }
.box-label { font-size: 11px; font-weight: 650; letter-spacing: .08em; color: var(--muted); margin-bottom: 10px; }
.mini-holder { min-height: calc(var(--cell) * 2); display: flex; align-items: center; justify-content: center; }
.mini-holder.used { opacity: .3; }
.mini { --mc: var(--cell); display: grid; grid-template-columns: repeat(var(--w, 4), var(--mc)); grid-auto-rows: var(--mc); gap: 1px; }
.mini-cell.filled { background: var(--piece); border-radius: 2px; }
.next-list { display: flex; flex-direction: column; align-items: center; gap: calc(var(--cell) * .8); }
.next-list .mini:not(:first-child) { --mc: calc(var(--cell) * .78); }

/* mode switch, key display, incoming garbage, watch feed */
.seg { display: inline-flex; }
.seg button { border-radius: 0; }
.seg button:first-child { border-radius: var(--radius) 0 0 var(--radius); }
.seg button:last-child { border-radius: 0 var(--radius) var(--radius) 0; border-left: 0; }
.seg button[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }
body.watching .practice-only, body:not(.watching) .watch-only { display: none !important; }
.keys { display: flex; justify-content: center; gap: 4px; margin-top: 8px; min-height: 22px; }
.keys span { min-width: 26px; padding: 1px 5px; text-align: center; font-size: 12px; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; font-variant-numeric: tabular-nums; }
.keys span.on { color: var(--bg); background: var(--text); border-color: var(--text); }
.feed { list-style: none; margin: 0; padding: 0; }
.feed li { padding: 3px 0; border-bottom: 1px solid var(--line); display: flex; gap: 8px; }
.feed li:last-child { border-bottom: 0; }
.feed .who { color: var(--muted); min-width: 72px; max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.feed .atk { font-weight: 600; }
.feed .when { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }

/* account: sign in, favourites, saved moments */
#account .user { display: inline-flex; align-items: center; gap: 6px; }
#account .user img { width: 20px; height: 20px; border-radius: 50%; }
.icon { padding: 5px 8px; }
#fav[aria-pressed="true"] { color: var(--warn); border-color: #5c4a22; }
dialog.small { width: min(calc(100% - 24px), 420px); }
.providers { display: grid; gap: 8px; margin: 12px 0 4px; }
.provider { display: block; text-align: center; padding: 9px 12px; border: 1px solid var(--line-strong); border-radius: var(--radius); color: var(--text); text-decoration: none; background: var(--bg); }
.provider:hover { border-color: #50545c; background: #1c1d21; }
.provider[hidden] { display: none; }
#moment-note { width: 100%; font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line-strong); border-radius: var(--radius); padding: 8px 10px; margin: 8px 0 4px; }
dialog h3 { font-size: 12px; font-weight: 600; color: var(--muted); margin: 14px 0 6px; }
.saved { list-style: none; margin: 0; padding: 0; }
.saved li { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.saved li:last-child { border-bottom: 0; }
.saved .note { color: var(--muted); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.saved button:not(.link) { padding: 2px 8px; }
.link { background: none; border: 0; padding: 0; color: var(--text); text-align: left; text-decoration: underline; text-decoration-color: var(--line-strong); cursor: pointer; }
.link:hover { background: none; text-decoration-color: var(--text); }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: var(--radius); font-weight: 600; z-index: 10; }

/* stats page */
dialog.wide { width: min(calc(100% - 24px), 1400px); max-height: 90vh; }
.stats-scroll { overflow: auto; max-height: 60vh; border: 1px solid var(--line); }
.wide-table { min-width: 1100px; }
.wide-table th, .wide-table td { padding: 4px 8px; white-space: nowrap; }
.wide-table thead th { position: sticky; top: 0; background: var(--panel); }
.wide-table td:first-child { text-align: left; }
.sort { background: none; border: 0; padding: 0; font-weight: 600; color: var(--text); cursor: pointer; }
.sort:hover { background: none; text-decoration: underline; }
label.inline { display: inline-flex; gap: 6px; align-items: center; }

/* timeline */
.track-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 8px; }
.track-bar button { padding: 3px 9px; }
.stepinput { color: var(--muted); font-variant-numeric: tabular-nums; margin: 0 4px; }
.clock { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
.branch { display: inline-flex; align-items: center; gap: 8px; color: var(--warn); margin-left: 6px; }
.branch button { border-color: #5c4a22; color: var(--warn); padding: 2px 8px; }
#timeline { width: 100%; height: 64px; cursor: pointer; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }

/* side column */
.side section { padding: 14px 16px; border-bottom: 1px solid var(--line); }
.side h2 { font-size: 12px; font-weight: 600; color: var(--muted); margin: 0 0 6px; }
.side h2:not(:first-child) { margin-top: 14px; }
.now { font-size: 16px; font-weight: 650; font-variant-numeric: tabular-nums; }
#status { margin: 8px 0 0; }
.move { line-height: 1.7; }
.move .t { display: inline-block; margin-right: 10px; white-space: nowrap; }
.move .atk { color: var(--text); font-weight: 600; }
.move .in { color: var(--bad); }
.stats { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.stats th, .stats td { padding: 3px 0; text-align: right; border-bottom: 1px solid var(--line); }
.stats th:first-child, .stats td:first-child { text-align: left; color: var(--muted); font-weight: 400; }
.stats thead th { font-weight: 600; max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stats tr:last-child td { border-bottom: 0; }
.key { display: flex; flex-wrap: wrap; gap: 6px 14px; color: var(--muted); font-size: 12px; }
.key i { display: inline-block; width: 11px; height: 11px; margin-right: 6px; vertical-align: -1px; }
.k-target { border: 2px solid #f2f2f2; }
.k-ghost { border: 1px dashed #8d9099; }
.k-miss { border: 2px dashed var(--bad); }

/* settings */
dialog { width: min(calc(100% - 24px), 680px); max-height: min(88vh, 720px); overflow: auto; background: var(--panel); border: 1px solid var(--line-strong); border-radius: 8px; color: var(--text); padding: 20px; }
dialog::backdrop { background: #000a; }
.dialog-header, .dialog-footer { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.dialog-header h2 { font-size: 18px; margin: 0 0 14px; }
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 28px; }
.settings-grid h3 { font-size: 12px; font-weight: 600; color: var(--muted); margin: 0 0 8px; }
.bind-row { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--line); min-height: 38px; gap: 12px; }
.bind-row button { min-width: 76px; padding: 2px 8px; }
.bind-row button.recording { border-color: var(--warn); color: var(--warn); }
.setting-message { color: var(--muted); font-size: 12px; min-height: 34px; }
.slider { display: block; margin-bottom: 16px; }
.slider span { display: flex; justify-content: space-between; margin-bottom: 4px; }
.slider output { color: var(--muted); font-variant-numeric: tabular-nums; }
.slider input { width: 100%; accent-color: var(--text); }
.toggle { display: flex; gap: 10px; align-items: flex-start; margin: 4px 0 12px; cursor: pointer; }
.toggle input { margin-top: 3px; accent-color: var(--text); }
.toggle small { display: block; color: var(--muted); font-size: 12px; }
.dialog-footer { border-top: 1px solid var(--line); margin-top: 14px; padding-top: 14px; }

/* narrower screens: side column moves under the timeline */
@media (max-width: 1100px) {
  .stage { grid-template-columns: 1fr; grid-template-areas: "fields" "track" "side"; }
  .side { border-left: 0; border-top: 1px solid var(--line); min-height: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
}
@media (max-width: 700px) {
  .bar { padding: 8px 10px; }
  .score { margin-left: 0; width: 100%; }
  .fields { gap: 10px; padding: 10px; }
  .track { padding: 0 10px 12px; }
  .field-head { position: static; margin-bottom: 6px; }
  .pname { max-width: none; }
  .playfield { --cell: 7px; grid-template-columns: 1fr 1fr; grid-template-areas: "hold next" "board board"; gap: 6px; }
  .playfield > .side-box:first-child { grid-area: hold; }
  .playfield > canvas { grid-area: board; }
  .playfield > .side-box:last-child { grid-area: next; }
  .playfield > .side-box { margin-top: 0; flex-direction: row; justify-content: center; gap: 6px; }
  .box-label { font-size: 9px; margin: 0; }
  .next-list { flex-direction: row; gap: 4px; }
  .next-list .mini:nth-child(n+4) { display: none; }
  .mini-holder { min-height: 0; }
  .settings-grid { grid-template-columns: 1fr; gap: 16px; }
}
