:root {
  --bg: #f2ede3;
  --ink: #2b2925;
  --muted: #7d766b;
  --paper: rgba(251, 248, 242, 0.88);
  --paper-solid: #fbf8f2;
  --line: rgba(43, 41, 37, 0.14);
  --accent: #b9573b;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Songti SC", "Noto Serif CJK SC", "Source Han Serif SC", serif;
  --sans: system-ui, -apple-system, "Segoe UI", "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --panel-w: 300px;
  --gap: 16px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--ink); font-family: var(--sans); -webkit-font-smoothing: antialiased; }
button, input { font: inherit; color: inherit; }

#stage { position: fixed; inset: 0; }
#stage canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
#stage canvas:active { cursor: grabbing; }

/* labels that follow riders */
#labels { position: fixed; inset: 0; pointer-events: none; overflow: hidden; }
.lbl { position: absolute; left: 0; top: 0; white-space: nowrap; font-size: 11px; line-height: 1; padding: 3px 7px 4px; border-radius: 999px; background: var(--paper); border: 1px solid var(--line); box-shadow: 0 1px 3px rgba(0,0,0,.06); }
.rider-lbl { border-left: 3px solid var(--tc, #888); }
.rider-lbl.dim { opacity: .5; }
.comet-lbl { font-weight: 600; background: var(--ink); color: var(--paper-solid); border-color: transparent; box-shadow: inset 3px 0 0 var(--tc, #d2453a); }
.spark-lbl { font-family: var(--serif); font-style: italic; color: #8a6514; background: rgba(255, 246, 214, .9); }

/* overlay root */
#ui { position: fixed; inset: 0; pointer-events: none; }
#ui > * { pointer-events: auto; }
.hidden { display: none !important; }
body.photo #ui, body.photo #labels { display: none; }
#photoExit { position: fixed; top: var(--gap); right: var(--gap); z-index: 10; font-size: 18px; width: 36px; height: 36px; padding: 0; }

/* title */
#title { position: absolute; top: 20px; left: 24px; max-width: 340px; pointer-events: none; }
.kicker, .rc-kicker, .pc-kicker { font-size: 10.5px; letter-spacing: .16em; text-transform: uppercase; color: var(--muted); }
#title h1 { font-family: var(--serif); font-weight: 500; font-size: 44px; line-height: 1; margin: 6px 0 8px; letter-spacing: -.01em; }
#title p { margin: 0; font-size: 13px; line-height: 1.45; color: #5b564e; max-width: 300px; }

/* scorecard */
#score { position: absolute; top: var(--gap); right: var(--gap); width: var(--panel-w); background: var(--paper); border: 1px solid var(--line); border-radius: 14px; padding: 10px 14px 9px; backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); }
.sc-row { display: flex; align-items: center; gap: 8px; padding: 2px 0; }
.sw { width: 14px; height: 14px; border-radius: 4px; background: linear-gradient(135deg, var(--a) 0 55%, var(--b) 55% 100%); box-shadow: inset 0 0 0 1px rgba(0,0,0,.12); flex: none; }
.sc-name { font-family: var(--serif); font-size: 16px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sc-pts { font-family: var(--serif); font-size: 24px; font-weight: 500; font-variant-numeric: tabular-nums; min-width: 2ch; text-align: right; }
.tw { font-size: 12px; color: var(--accent); font-weight: 700; }
.sc-meta { font-size: 11.5px; color: var(--muted); border-top: 1px solid var(--line); margin-top: 6px; padding-top: 5px; font-variant-numeric: tabular-nums; }
.sc-meta + .sc-meta { margin-top: 0; border-top: 0; padding-top: 2px; }
.sc-meta.live { color: #9a6c0c; font-weight: 600; }
.sc-meta.tail { color: var(--accent); }
.chip { display: inline-block; margin: 1px 3px 1px 0; padding: 1px 6px; border-radius: 999px; border: 1px solid var(--line); border-left: 3px solid var(--tc); color: var(--ink); }
.sc-warn { margin-top: 6px; font-size: 11px; color: #a3401f; }

#badge { position: absolute; top: var(--gap); left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--paper-solid); border-radius: 999px; padding: 6px 8px 6px 14px; font-size: 12px; display: flex; gap: 8px; align-items: center; }
#badge .pill { background: transparent; color: inherit; border-color: rgba(255,255,255,.3); }

/* cards */
.card { position: absolute; background: var(--paper); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px; backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); box-shadow: 0 6px 24px rgba(40, 30, 20, .08); }
.card h3 { font-family: var(--serif); font-weight: 500; font-size: 20px; margin: 4px 0 8px; line-height: 1.2; padding-right: 20px; }
.card .x { position: absolute; top: 6px; right: 8px; border: 0; background: none; font-size: 20px; line-height: 1; cursor: pointer; color: var(--muted); padding: 4px; }
#rulecard { left: 24px; top: 168px; width: 330px; }
.rc-body { display: flex; gap: 12px; align-items: flex-start; }
.rc-sig { flex: none; width: 54px; color: var(--ink); }
.rc-sig svg { width: 54px; height: 63px; }
.rc-what { margin: 0 0 6px; font-size: 13px; line-height: 1.45; }
.rc-rule { margin: 0; font-family: var(--serif); font-size: 14px; line-height: 1.45; border-left: 2px solid var(--accent); padding-left: 9px; }
#summary { left: 24px; top: 168px; width: 330px; }
#pcard { right: calc(var(--panel-w) + var(--gap) * 2); top: var(--gap); width: 250px; border-top: 4px solid var(--tc, #888); }
body.panel-closed #pcard { right: var(--gap); top: 200px; }
#pcard .num { color: var(--muted); font-size: 15px; }
.pc-state { font-size: 12px; color: var(--accent); margin: -4px 0 8px; }
.pc-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px 10px; margin: 0 0 10px; }
.pc-stats dt { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.pc-stats dd { margin: 0; font-family: var(--serif); font-size: 18px; }
.pc-btns { display: flex; flex-wrap: wrap; gap: 6px; }

/* ticker */
#ticker { position: absolute; left: 24px; bottom: 70px; width: 400px; max-width: calc(100vw - 48px); pointer-events: none; }
.tk { font-size: 12.5px; line-height: 1.45; padding: 3px 0; border-top: 1px solid var(--line); color: #4c4841; opacity: .62; }
.tk:last-child { opacity: 1; color: var(--ink); }
.tk:nth-last-child(2) { opacity: .85; }
.tk-t { font-size: 10.5px; color: var(--muted); font-variant-numeric: tabular-nums; margin-right: 4px; }

/* bottom bar */
#bar { position: absolute; left: var(--gap); right: var(--gap); bottom: 12px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; justify-content: center; pointer-events: none; }
#bar > * { pointer-events: auto; }
.sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; pointer-events: none !important; }

.pill { border: 1px solid var(--line); background: var(--paper); border-radius: 999px; padding: 5px 11px; font-size: 12px; line-height: 1.3; cursor: pointer; white-space: nowrap; transition: background .15s, color .15s; backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }
.pill:hover { background: var(--paper-solid); border-color: rgba(43,41,37,.3); }
.pill.on { background: var(--ink); color: var(--paper-solid); border-color: var(--ink); }
.pill.icon { min-width: 30px; text-align: center; padding: 5px 8px; }
.pill:focus-visible, .switch:focus-visible, .tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.seg { display: inline-flex; flex-wrap: wrap; gap: 4px; }

.switch { display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: var(--paper); border-radius: 999px; padding: 4px 11px 4px 4px; font-size: 12px; cursor: pointer; }
.switch .knob { width: 28px; height: 16px; border-radius: 999px; background: #d9d2c4; position: relative; transition: background .15s; flex: none; }
.switch .knob::after { content: ""; position: absolute; top: 2px; left: 2px; width: 12px; height: 12px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.2); transition: left .15s; }
.switch.on .knob { background: var(--accent); }
.switch.on .knob::after { left: 14px; }
.switch .val { color: var(--muted); font-size: 11px; }
#tabBody .switch { width: 100%; margin: 3px 0; border-color: transparent; background: transparent; padding-left: 0; }
#tabBody .switch .sw-l { flex: 1; text-align: left; }

/* popovers */
.pop { position: absolute; right: var(--gap); bottom: 56px; background: var(--paper-solid); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; box-shadow: 0 8px 28px rgba(40,30,20,.12); max-width: calc(100vw - 32px); }
#help { width: 330px; }
#help h4 { margin: 0 0 8px; font-family: var(--serif); font-weight: 500; font-size: 16px; }
#help dl { margin: 0; display: grid; gap: 4px; }
#help dl > div { display: flex; gap: 10px; font-size: 12px; align-items: baseline; }
#help dt { width: 64px; flex: none; }
#help dd { margin: 0; }
kbd { font-family: var(--sans); font-size: 11px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 1px 5px; background: #fff; }
#langMenu { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 10px; width: 240px; }
#langMenu a { color: var(--ink); text-decoration: none; font-size: 13px; padding: 4px 6px; border-radius: 6px; }
#langMenu a:hover { background: rgba(0,0,0,.05); }
#langMenu a.on { font-weight: 600; box-shadow: inset 2px 0 0 var(--accent); }

/* side panel */
#panel { position: absolute; right: var(--gap); top: 214px; bottom: 60px; width: var(--panel-w); background: var(--paper); border: 1px solid var(--line); border-radius: 14px; display: flex; flex-direction: column; backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); overflow: hidden; }
#panelToggle { position: absolute; top: 8px; right: 8px; z-index: 2; font-size: 11px; padding: 3px 9px; }
#panel.closed { top: auto; bottom: 60px; height: auto; width: auto; background: transparent; border: 0; backdrop-filter: none; -webkit-backdrop-filter: none; overflow: visible; }
#panel.closed #tabs, #panel.closed #tabBody { display: none; }
#panel.closed #panelToggle { position: static; font-size: 12px; padding: 5px 12px; }
#tabs { display: flex; flex-wrap: wrap; gap: 2px 10px; padding: 12px 14px 8px; padding-right: 96px; border-bottom: 1px solid var(--line); }
.tab { border: 0; background: none; padding: 3px 0; font-size: 12px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; }
.tab.on { color: var(--ink); border-bottom-color: var(--accent); }
#tabBody { overflow-y: auto; padding: 8px 14px 16px; flex: 1; overscroll-behavior: contain; }
#tabBody h4 { font-family: var(--serif); font-weight: 500; font-size: 15px; margin: 14px 0 6px; padding-top: 10px; border-top: 1px solid var(--line); }
#tabBody h4:first-child { border-top: 0; padding-top: 0; }
.note { font-size: 12px; line-height: 1.45; color: var(--muted); margin: 6px 0 10px; }
.basic { font-family: var(--serif); font-size: 13.5px; line-height: 1.45; margin: 0 0 8px; }
.warn { font-size: 12px; color: #a3401f; margin: 6px 0; }
.muted { color: var(--muted); }
.btns { display: flex; flex-wrap: wrap; gap: 5px; margin: 4px 0 6px; }
.row { margin: 8px 0; }
.row-h { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 4px; }
.lbl-t { font-size: 12px; }
.val { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; }
.row.inline { display: flex; align-items: center; gap: 8px; }
.row.inline .lbl-t { flex: 1; }
input[type="range"] { width: 100%; accent-color: var(--accent); margin: 0; }
input[type="text"], input[type="number"] { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; background: #fff; font-size: 13px; }
input[type="number"] { width: 56px; text-align: center; }
input[type="color"] { width: 34px; height: 24px; border: 1px solid var(--line); border-radius: 6px; padding: 0 2px; background: #fff; cursor: pointer; }
.score-set { display: flex; align-items: center; gap: 6px; font-size: 12px; }
.roster { display: flex; flex-direction: column; gap: 4px; align-items: stretch; }
.roster .pill { text-align: left; }
.roster .num { color: var(--muted); }
.pill.sit { font-size: 11.5px; }

.tbl { width: 100%; border-collapse: collapse; font-size: 12px; margin: 4px 0 8px; }
.tbl th, .tbl td { padding: 4px 4px; border-bottom: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; }
.tbl th:first-child { text-align: left; font-weight: 400; color: var(--muted); }
.tbl tr:first-child th { font-family: var(--serif); font-weight: 500; color: var(--ink); font-size: 13px; }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { font-size: 12px; line-height: 1.4; padding: 5px 0 5px 8px; border-left: 3px solid var(--tc, var(--line)); margin-bottom: 3px; }
.timeline li.can { cursor: pointer; }
.timeline li.can:hover { background: rgba(0,0,0,.035); }
.timeline .play { color: var(--accent); font-size: 11px; white-space: nowrap; }
.ev-goal, .ev-spark { font-weight: 500; }

/* narrow screens: keep the middle of the scene clear */
@media (max-width: 760px) {
  :root { --panel-w: min(300px, calc(100vw - 32px)); --gap: 10px; }
  #title { top: 12px; left: 14px; }
  #title h1 { font-size: 30px; }
  #title p { display: none; }
  #score { width: 190px; padding: 8px 10px; }
  .sc-name { font-size: 13px; }
  .sc-pts { font-size: 19px; }
  #panel { top: auto; height: 52vh; bottom: 96px; }
  #ticker { left: 14px; bottom: 100px; width: 70vw; }
  .tk { font-size: 11.5px; }
  .tk:nth-child(-n+2) { display: none; }
  #rulecard, #summary { left: 10px; top: auto; bottom: 96px; width: calc(100vw - 20px); }
  #pcard, body.panel-closed #pcard { right: 10px; top: 150px; width: 230px; }
  #bar { bottom: 8px; gap: 4px; }
  .pill { padding: 5px 9px; }
  .sep { display: none; }
}
@media (max-height: 560px) {
  #title p { display: none; }
  #panel { top: 170px; }
}
