:root {
  --bg: #f4f1ea;
  --fg: #1d1d1b;
  --muted: #6b6a64;
  --card: #ffffff;
  --border: #e2ddd2;
  --accent: #2f6b3a;
  --accent-fg: #ffffff;
  --warn-bg: #fff4e0;
  --warn-border: #f0c77a;
  --sq-light: #eeeed2;
  --sq-dark: #769656;
  --sq-hl: rgba(255, 221, 0, 0.45);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161815;
    --fg: #ecebe6;
    --muted: #9a9a92;
    --card: #20231f;
    --border: #33372f;
    --accent: #4f9a5c;
    --warn-bg: #3a2e17;
    --warn-border: #7a5d24;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 520px;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
}

.screen { display: none; flex-direction: column; gap: 14px; }
.screen.active { display: flex; }

#screen-start { min-height: 85vh; justify-content: center; }
#screen-loading, #screen-error { min-height: 70vh; justify-content: center; align-items: center; text-align: center; }

.hero { text-align: center; margin-bottom: 18px; }
.logo { font-size: 64px; line-height: 1; }
h1 { margin: 8px 0 6px; font-size: 28px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 48px; padding: 12px 18px;
  border-radius: 12px; border: 1px solid var(--border);
  background: var(--card); color: var(--fg);
  font: inherit; font-weight: 600; cursor: pointer; user-select: none;
}
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.big { min-height: 60px; font-size: 18px; }
.btn:disabled { opacity: .5; }

.photo-preview { max-width: 100%; max-height: 38vh; border-radius: 10px; }

.spinner {
  width: 40px; height: 40px; border-radius: 50%;
  border: 4px solid var(--border); border-top-color: var(--accent);
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.turn-line { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; }
.dot { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--fg); }
.dot.white { background: #fff; }
.dot.black { background: #111; }

#board-wrap { width: 100%; aspect-ratio: 1; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 10px rgba(0,0,0,.15); }
#board { width: 100%; height: 100%; display: block; }
.sq-light { fill: var(--sq-light); }
.sq-dark { fill: var(--sq-dark); }
.sq-hl { fill: var(--sq-hl); }
.coord { font-size: 17px; font-weight: 700; fill: rgba(0,0,0,.45); }
.piece {
  font-size: 82px; text-anchor: middle; dominant-baseline: central;
  font-family: "DejaVu Sans", "Segoe UI Symbol", "Noto Sans Symbols 2", "Apple Symbols", sans-serif;
}
.pw { fill: #fff; stroke: #1a1a1a; stroke-width: 3px; paint-order: stroke; }
.pb { fill: #1a1a1a; stroke: #fff; stroke-width: 1px; paint-order: stroke; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.card.warn { background: var(--warn-bg); border-color: var(--warn-border); }
.card-title { font-weight: 700; margin-bottom: 8px; }

.segmented { display: flex; background: var(--bg); border-radius: 10px; padding: 3px; gap: 3px; margin-bottom: 6px; }
.segmented button {
  flex: 1; min-height: 40px; border: 0; border-radius: 8px;
  background: transparent; color: var(--fg); font: inherit; font-weight: 600; cursor: pointer;
}
.segmented button.on { background: var(--accent); color: var(--accent-fg); }

.moves { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.moves li {
  display: grid; grid-template-columns: 14px auto 1fr; column-gap: 10px; align-items: center;
  background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px;
}
.moves li:first-child { border-width: 2px; border-color: #16a34a; }
.swatch { width: 14px; height: 14px; border-radius: 4px; }
.san { font-weight: 800; font-size: 20px; }
.eval { justify-self: end; font-weight: 700; font-variant-numeric: tabular-nums; }
.moves .line { grid-column: 2 / 4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.actions.sticky {
  position: sticky; bottom: 0; z-index: 5;
  margin: 0 -16px; padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: var(--bg); border-top: 1px solid var(--border);
}
.photo-details summary { cursor: pointer; color: var(--muted); }
.photo-details img { margin-top: 8px; }

.camera {
  position: fixed; inset: 0; z-index: 20; background: #000;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  padding: 16px;
}
.camera[hidden] { display: none; }
.camera video { max-width: 100%; max-height: calc(100vh - 120px); border-radius: 10px; background: #111; }
.camera-error { color: #fff; margin: 0; }
.camera-error:empty { display: none; }
.camera-bar { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; width: min(480px, 100%); }
