:root {
  --bg: #0e0f0d;
  --panel: #1a1c19;
  --panel-2: #252823;
  --line: #30332e;
  --text: #ecebe6;
  --muted: #8f8e85;
  --accent: #66c191;
  --idle-bg: #20221f;
  --idle-ink: #7d7c74;
  --active-bg: #f1eee4;
  --active-ink: #10110f;
  --low-bg: #f2b44c;
  --low-ink: #261800;
  --flag-bg: #d9443b;
  --flag-ink: #ffffff;
  --win-bg: #2f6b4f;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.4 var(--font);
  touch-action: manipulation;
  overscroll-behavior: none;
}
body.on-board {
  position: fixed; inset: 0; overflow: hidden;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- Setup ---------- */
.setup {
  max-width: 560px; margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 28px);
}
.setup-top { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; margin-bottom: 6px; }
.setup-top h1 { margin: 0; font-size: 1.25rem; }
.back { color: var(--accent); text-decoration: none; font-weight: 600; padding: 8px 0; }
.setup-note { color: var(--muted); font-size: 0.9rem; margin: 6px 0 16px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.field small { font-weight: 400; }
.field input {
  width: 100%; min-height: 48px; padding: 10px 12px; font: inherit; font-size: 16px; color: var(--text);
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
}
.field input:focus { outline: 2px solid var(--accent); border-color: transparent; }

.preset-group h2 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 20px 0 8px; }
.preset-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.preset {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 72px; padding: 10px 6px; border-radius: 14px;
  background: var(--panel); border: 1px solid var(--line);
}
.preset b { font-size: 1.4rem; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.preset small { font-size: 0.72rem; color: var(--muted); }
.preset:active { background: var(--panel-2); transform: scale(0.98); }
.preset.is-last { border-color: var(--accent); }

.custom { margin-top: 22px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; }
.custom summary { padding: 16px; font-weight: 600; cursor: pointer; }
.custom-form { display: flex; flex-direction: column; gap: 14px; padding: 0 16px 16px; }
.custom-form .field input { background: var(--bg); }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { display: flex; align-items: center; gap: 10px; font-size: 0.95rem; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }
.start {
  min-height: 52px; border: 0; border-radius: 12px; background: var(--accent); color: #0c1a12;
  font-weight: 700; font-size: 1.05rem;
}
.opts { border: 0; margin: 20px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 10px 20px; }

/* ---------- Board ---------- */
.board {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  gap: 6px; background: var(--bg);
}
.side {
  position: relative; flex: 1 1 0; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  margin: 0 6px; padding: 0; border: 0; border-radius: 22px;
  background: var(--idle-bg); color: var(--idle-ink);
  touch-action: none;
  transition: background-color 0.12s, color 0.12s;
}
.side:first-child { margin-top: 6px; transform: rotate(180deg); }
.side:last-of-type { margin-bottom: 6px; }
.side.is-active { background: var(--active-bg); color: var(--active-ink); }
.side.is-active.is-low { background: var(--low-bg); color: var(--low-ink); }
.side.is-active.is-paused { background: var(--panel-2); color: var(--text); }
.side.is-flagged { background: var(--flag-bg); color: var(--flag-ink); }
.side.is-winner { background: var(--win-bg); color: #fff; }

.face {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center;
  width: 100%; height: 100%; pointer-events: none;
}
.time {
  font-size: min(27vw, 17vh); font-weight: 700; line-height: 1;
  font-variant-numeric: tabular-nums; letter-spacing: -0.03em;
}
.tag {
  position: absolute; top: 14px; left: 18px;
  font-size: 0.9rem; font-weight: 700; opacity: 0.75;
}
.moves {
  position: absolute; top: 14px; right: 18px;
  font-size: 0.9rem; font-weight: 600; opacity: 0.65; font-variant-numeric: tabular-nums;
}
.hint {
  position: absolute; bottom: 16px; left: 16px; right: 16px; text-align: center;
  font-size: 0.95rem; font-weight: 600; opacity: 0.85;
}

.bar {
  flex: none; height: 52px; margin: 0 6px;
  display: grid; grid-template-columns: 64px 1fr 64px; align-items: center;
}
.bar-btn {
  width: 52px; height: 52px; justify-self: center;
  display: grid; place-items: center; border: 0; border-radius: 50%;
  background: var(--panel-2); color: var(--text);
}
.bar-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.bar-label {
  text-align: center; color: var(--muted); font-size: 0.85rem; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.i-pause { display: none; }
body.is-running .i-pause { display: block; }
body.is-running .i-menu { display: none; }
.i-mute { display: none; }
body.is-muted .i-mute { display: block; }
body.is-muted .i-wave { display: none; }

/* Landscape: players sit at the short ends, so each half faces outwards. */
@media (orientation: landscape) {
  .board { flex-direction: row; }
  .side { margin: 6px 0; }
  .side:first-child { margin-left: 6px; transform: none; }
  .side:last-of-type { margin-right: 6px; transform: rotate(180deg); }
  .face { writing-mode: vertical-rl; }
  .time { font-size: min(24vh, 13vw); }
  .bar { width: 52px; height: auto; margin: 6px 0; grid-template-columns: none; grid-template-rows: 64px 1fr 64px; }
  .bar-label { writing-mode: vertical-rl; align-self: center; justify-self: center; max-height: 100%; }
  .tag { top: 18px; left: auto; right: 14px; }
  .moves { top: auto; bottom: 18px; right: 14px; }
  .hint { left: 16px; right: auto; top: 16px; bottom: 16px; }
}

/* ---------- Menu ---------- */
.menu {
  position: fixed; inset: 0; z-index: 10;
  display: grid; place-items: center; padding: 20px;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.menu-card {
  width: min(380px, 100%); max-height: 100%; overflow: auto;
  display: flex; flex-direction: column; gap: 10px;
  padding: 22px; border-radius: 22px; background: var(--panel); border: 1px solid var(--line);
}
.menu-card h2 { margin: 0; font-size: 1.4rem; text-align: center; }
.menu-sub { margin: 0 0 6px; text-align: center; color: var(--muted); font-size: 0.92rem; }
.m-btn {
  min-height: 52px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--panel-2); font-weight: 650; font-size: 1rem;
}
.m-btn.primary { background: var(--accent); color: #0c1a12; border-color: transparent; }
.m-btn.armed { background: var(--flag-bg); color: #fff; border-color: transparent; }
.m-btn.danger { background: transparent; color: #f07a70; border-color: rgba(240, 122, 112, 0.35); }
.m-btn.danger.armed { background: var(--flag-bg); color: #fff; }

@media (prefers-reduced-motion: reduce) {
  .side { transition: none; }
}
