:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --text: #17201b;
  --muted: #66716b;
  --line: #dde2de;
  --accent: #0f7a4a;
  --accent-text: #ffffff;
  --accent-soft: #e0f1e8;
  --danger: #b3261e;
  --radius: 14px;
  --p0: #0072b2; --p1: #d55e00; --p2: #00875f; --p3: #b5478a; --p4: #e69f00; --p5: #6a3d9a;
  --pf0: #fff; --pf1: #fff; --pf2: #fff; --pf3: #fff; --pf4: #1a1300; --pf5: #fff;
  --tab-h: 56px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111513; --surface: #1b211e; --text: #eef2ef; --muted: #98a39d; --line: #2c3530;
    --accent: #3ccf8e; --accent-text: #06130c; --accent-soft: #1d3a2d; --danger: #f2877f;
    color-scheme: dark;
    --p0: #56b4e9; --p1: #ff8a3d; --p2: #2fd6a0; --p3: #f08bc4; --p4: #f2c14e; --p5: #b48cf0;
    --pf0: #06121b; --pf1: #1d0d00; --pf2: #00180f; --pf3: #240716; --pf4: #1a1300; --pf5: #12081f;
  }
}
:root[data-theme="dark"] {
  --bg: #111513; --surface: #1b211e; --text: #eef2ef; --muted: #98a39d; --line: #2c3530;
  --accent: #3ccf8e; --accent-text: #06130c; --accent-soft: #1d3a2d; --danger: #f2877f;
  color-scheme: dark;
  --p0: #56b4e9; --p1: #ff8a3d; --p2: #2fd6a0; --p3: #f08bc4; --p4: #f2c14e; --p5: #b48cf0;
  --pf0: #06121b; --pf1: #1d0d00; --pf2: #00180f; --pf3: #240716; --pf4: #1a1300; --pf5: #12081f;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  touch-action: manipulation;
}
#app { max-width: 560px; margin: 0 auto; min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; }
h1 { font-size: 1.4rem; margin: .4rem 0 .6rem; }
h2 { font-size: 1.05rem; margin: 1.4rem 0 .5rem; }
.muted { color: var(--muted); font-size: .9rem; }
.small { font-size: .8rem; text-align: center; margin-top: 1.5rem; }

/* header + tabs */
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 6px) 16px 6px;
}
.brand { font-weight: 700; letter-spacing: .02em; }
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; justify-content: center; background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs a {
  flex: 1; max-width: 112px; min-width: 0; padding: 0 2px; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; height: var(--tab-h); display: grid; place-items: center;
  color: var(--muted); text-decoration: none; font-size: .72rem; font-weight: 600;
}
.tabs a.on { color: var(--accent); box-shadow: inset 0 3px 0 var(--accent); }
.view { flex: 1; padding: 4px 16px calc(var(--tab-h) + env(safe-area-inset-bottom) + 24px); }

/* buttons */
button, select, input { font: inherit; color: inherit; }
button {
  min-height: 48px; border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius);
  padding: 0 16px; cursor: pointer; font-weight: 600;
}
button:disabled { opacity: .35; cursor: default; }
button:active:not(:disabled) { transform: scale(.97); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); width: 100%; margin-top: 12px; }
button.ghost { background: transparent; }
button.icon { min-width: 44px; padding: 0; background: transparent; border-color: transparent; }
button.danger { color: var(--danger); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.actions { display: flex; gap: 10px; margin-top: 14px; }
.actions > * { flex: 1; }
.actions.col { flex-direction: column; }

/* play screen */
.status { padding: 4px 0 8px; }
.hint { color: var(--muted); font-size: .8rem; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 8px; }
.chip { min-height: 44px; display: flex; gap: 8px; align-items: center; white-space: nowrap; font-weight: 500; }
.chip.shown { border-color: var(--pc); border-width: 2px; }
.chip.playing { background: var(--pc); color: var(--pf); border-color: var(--pc); }
.chip.playing .dot { background: var(--pf); color: var(--pc); }
.total { text-align: center; padding: 4px 0 10px; }
.big { font-size: 4rem; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.turn { margin-top: 14px; font-weight: 700; text-align: center; }
.turn.seat { background: var(--pc); color: var(--pf); border-radius: var(--radius); padding: 12px; font-size: 1.25rem; }
.dot { display: inline-grid; place-items: center; width: 1.5em; height: 1.5em; border-radius: 50%; background: var(--pc); color: var(--pf); font-size: .75rem; font-weight: 800; flex: none; }
.turn .dot { background: var(--pf); color: var(--pc); margin-right: 8px; }

.keypad { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin-top: 8px; }
.key { min-height: 56px; font-size: 1.3rem; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1; }
.key small, .plus small { font-size: .7rem; font-weight: 500; opacity: .8; }
.key.wide { grid-column: 1 / -1; }
.key.accent, .plus { background: var(--pc, var(--accent)); color: var(--pf, var(--accent-text)); border-color: var(--pc, var(--accent)); }
.counter { display: grid; gap: 8px; margin-top: 8px; }
.plus { min-height: 120px; font-size: 2rem; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.counter .row { display: grid; grid-template-columns: 1fr 2fr; gap: 8px; }

/* scorecard (bowling sheet) */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; margin: 8px 0; }
.card.seat { border-left: 6px solid var(--pc); }
.card.current { border-color: var(--pc, var(--accent)); box-shadow: 0 0 0 2px var(--pc, var(--accent)); }
.card.seat header strong { display: flex; align-items: center; gap: 8px; }
.card header { display: flex; justify-content: space-between; margin-bottom: 8px; }
.card-total { font-weight: 700; font-variant-numeric: tabular-nums; }
.frames { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; }
.frame { border: 1px solid var(--line); border-radius: 8px; overflow: hidden; text-align: center; }
.frame.active { border-color: var(--pc, var(--accent)); box-shadow: 0 0 0 2px var(--pc, var(--accent)); }
.fnum { font-size: .65rem; color: var(--muted); background: var(--bg); }
.boxes { display: flex; justify-content: flex-end; border-bottom: 1px solid var(--line); }
.box { flex: 1; min-width: 0; height: 22px; line-height: 22px; font-size: .85rem; font-weight: 700; border-left: 1px solid var(--line); }
.box:first-child { border-left: 0; }
.box.x, .box.sp { color: var(--accent); }
.cum { height: 26px; line-height: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.cum.pending { color: var(--muted); }
@media (min-width: 520px) {
  .frames { grid-template-columns: repeat(10, 1fr); }
  .frame.tenth { grid-column: span 1; }
}

/* setup, summary, settings */
.player-row { display: flex; gap: 8px; margin-bottom: 8px; }
input.name, select {
  flex: 1; min-height: 48px; padding: 0 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface);
}
input.name { font-size: 16px; } /* >=16px prevents iOS zoom on focus */
.results { margin: 8px 0; }
.result { display: flex; align-items: center; gap: 10px; padding: 12px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 8px; font-size: 1.1rem; }
.result span:first-child { flex: 1; }
.result b { font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.result.winner { border-color: var(--accent); background: var(--accent-soft); }
.result.seat { border-left: 6px solid var(--pc); }
.badge { font-size: .7rem; font-weight: 700; background: var(--accent); color: var(--accent-text); border-radius: 999px; padding: 2px 8px; }
.setting { display: flex; flex-direction: column; gap: 8px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.seg { display: flex; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.seg button { flex: 1; border: 0; border-radius: 0; background: transparent; }
.seg button.on { background: var(--accent); color: var(--accent-text); }

/* stats + history */
.controls { display: flex; gap: 8px; margin-bottom: 12px; }
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 6px; text-align: center; }
.tile-v { font-size: 1.4rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.tile-l { font-size: .75rem; font-weight: 600; }
.tile-s { font-size: .65rem; color: var(--muted); }
.chart { width: 100%; height: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.chart-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }
.chart-dot { fill: var(--accent); }
.chart-mean { stroke: var(--muted); stroke-dasharray: 4 4; }
.chart-label { fill: var(--muted); font-size: 10px; }
.history { list-style: none; margin: 0; padding: 0; }
.history li { display: flex; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 8px; }
.hist-main { flex: 1; display: flex; flex-direction: column; padding: 10px 14px; color: inherit; text-decoration: none; }
.hist-scores { display: flex; flex-wrap: wrap; gap: 4px 14px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 12px); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-weight: 700;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 10;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } button:active { transform: none !important; } }

/* Play screen: fit one phone screen (portrait) with no scroll; the keypad absorbs the leftover height. */
html:has(main.play), body:has(main.play) { overflow: hidden; overscroll-behavior: none; }
#app:has(main.play) { height: 100vh; height: 100dvh; min-height: 0; }
.play {
  flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 8px; overflow: hidden;
  padding: 0 14px calc(var(--tab-h) + env(safe-area-inset-bottom) + 8px);
}
.play .head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.play .status { padding: 0; flex: 1; min-width: 0; line-height: 1.3; }
.play .hint { font-size: .72rem; line-height: 1.25; }
.play .muted { font-size: .8rem; }
.play .total { padding: 0; text-align: right; flex: none; }
.play .big { font-size: 3rem; }
.play .chips { padding: 0; gap: 6px; flex: none; }
.play .chip { min-height: 36px; padding: 0 10px; gap: 6px; font-size: .9rem; }
.play .card { margin: 0; padding: 8px; flex: none; }
.play .card header { margin-bottom: 4px; font-size: .9rem; }
.play .frames { grid-template-columns: repeat(9, 1fr) 1.55fr; gap: 3px; }
.play .frame { border-radius: 6px; }
.play .box { height: 20px; line-height: 20px; font-size: .78rem; }
.play .cum { height: 24px; line-height: 24px; font-size: .85rem; }
.play .turn { margin: 0; flex: none; font-size: 1rem; }
.play .turn.seat { padding: 8px; font-size: 1.1rem; }
.play .keypad, .play .counter { flex: 1; min-height: 0; margin: 0; gap: 6px; }
.play .keypad { grid-auto-rows: minmax(40px, 1fr); }
.play .key { min-height: 0; }
.play .counter { grid-template-rows: 1fr auto; }
.play .counter .row { grid-auto-rows: minmax(56px, 1fr); }
.play .plus { min-height: 0; }
.play .actions { margin: 0; flex: none; }
.play .actions button { min-height: 44px; }
/* short screens (small phones, Safari with toolbars): trim a bit more */
@media (max-height: 700px) {
  .play { gap: 6px; }
  .play .big { font-size: 2.5rem; }
  .play .hint { display: none; }
  .play .turn:not(.seat) { display: none; }
}

.end-choice { margin-top: 14px; }
.end-choice p { text-align: center; }

.top-actions { display: flex; align-items: center; gap: 4px; }
button.lang { min-height: 36px; padding: 0 10px; border-color: var(--line); font-size: .8rem; }
.rules h2 { margin-top: 1.2rem; }
.rules ul { padding-left: 1.2rem; margin: .4rem 0; }
.rules li { margin: .35rem 0; }
.rules p { margin: .4rem 0; }
