:root {
  --bg: #0f1720;
  --surface: #182330;
  --surface2: #213041;
  --line: #2e4054;
  --text: #e8eef5;
  --muted: #8ea2b8;
  --home: #3b9cff;
  --away: #ff8a3d;
  --ok: #2fbf71;
  --ng: #e5484d;
  --accent: #f5c542;
  --floor: #c98d55;
  --floor-paint: #b5733f;
  --court-line: #fffaf0;
  --radius: 10px;
  --tap: 48px;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  font-size: 15px;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
[hidden] { display: none !important; }

/* 上部 */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; letter-spacing: .04em; white-space: nowrap; }
.scoreboard { flex: 1; display: flex; justify-content: center; align-items: baseline; gap: 10px; min-width: 0; }
.scoreboard .team { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 30vw; font-size: 14px; }
.scoreboard .team.home { color: var(--home); }
.scoreboard .team.away { color: var(--away); }
.scoreboard .score { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; white-space: nowrap; }
.sync-badge {
  border: 1px solid var(--line); background: var(--surface2); border-radius: 999px;
  padding: 4px 10px; font-size: 12px; white-space: nowrap;
}
.sync-badge.is-ok { border-color: var(--ok); color: var(--ok); }
.sync-badge.is-wait { border-color: var(--accent); color: var(--accent); }
.sync-badge.is-demo { color: var(--muted); }

/* タブ */
.tabs { display: flex; background: var(--surface); border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs button {
  flex: 1; min-width: 64px; min-height: 44px; border: 0; background: none; color: var(--muted);
  border-bottom: 3px solid transparent; white-space: nowrap;
}
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 700; }
.lock { font-size: 11px; margin-left: 2px; }

main { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; }
.view { display: none; padding: 12px 16px 32px; }
.view.active { display: block; }
#view-record { padding: 8px; }

h2 { font-size: 16px; margin: 20px 0 10px; }
h2:first-child { margin-top: 4px; }
h2 small { color: var(--muted); font-weight: 400; margin-left: 6px; }
.muted, .note { color: var(--muted); font-size: 13px; }

/* 記録の画面配置: スマホは縦、タブレット(横長)は左右 */
.record-layout { display: grid; gap: 10px; grid-template-columns: 1fr; }
@media (min-width: 820px) and (min-aspect-ratio: 1/1) {
  .record-layout { grid-template-columns: minmax(0, 1.25fr) minmax(320px, 1fr); align-items: start; }
  .court-wrap { position: sticky; top: 0; }
}
.court-wrap { display: flex; flex-direction: column; gap: 6px; }
.court {
  /* 縦横比を保ったまま画面に収める。スマホは画面の半分弱にして、下の選手・ボタンがスクロールなしで見えるように */
  display: block; aspect-ratio: 150 / 140; height: auto; margin: 0 auto; border-radius: 8px; touch-action: none;
  width: min(100%, calc(44vh * 150 / 140)); width: min(100%, calc(44dvh * 150 / 140));
  user-select: none; -webkit-user-select: none;
}
@media (min-width: 820px) and (min-aspect-ratio: 1/1) {
  .court { width: min(100%, calc((100vh - 190px) * 150 / 140)); width: min(100%, calc((100dvh - 190px) * 150 / 140)); }
}
.court-floor { fill: var(--floor); }
.court-paint { fill: var(--floor-paint); }
.court-line { fill: none; stroke: var(--court-line); stroke-width: .5; }
.court-line .dash { stroke-dasharray: 1.5 1.5; }
.court-board { stroke: #fff; stroke-width: .9; }
.court-rim { fill: none; stroke: #ff5a1f; stroke-width: .6; }
.mark.made { stroke: #fff; stroke-width: .5; }
.mark.made.home { fill: var(--home); }
.mark.made.away { fill: var(--away); }
.mark.miss { fill: none; stroke-width: 1; stroke-linecap: round; }
.mark.miss.home { stroke: #0d4f99; }
.mark.miss.away { stroke: #9a3c00; }
.pending { fill: none; stroke: var(--accent); stroke-width: 1.2; animation: pulse 1s infinite; }
@keyframes pulse { 50% { stroke-width: 2.2; } }
.hint { margin: 0; text-align: center; color: var(--muted); font-size: 12px; }

.quarter-bar { display: flex; gap: 4px; overflow-x: auto; }
.quarter-bar button {
  flex: 1; min-width: 44px; min-height: 36px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); font-size: 13px;
}
.quarter-bar button.active { background: var(--accent); color: #1a1a1a; border-color: var(--accent); font-weight: 700; }

.panel { display: flex; flex-direction: column; gap: 10px; }
.team-toggle, .value-toggle, .seg { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.seg { grid-template-columns: repeat(3, 1fr); }
.team-toggle button, .value-toggle button, .seg button {
  min-height: var(--tap); border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); font-weight: 700;
}
.team-toggle button.active[data-team="home"] { background: var(--home); border-color: var(--home); color: #fff; }
.team-toggle button.active[data-team="away"] { background: var(--away); border-color: var(--away); color: #1a1a1a; }
.value-toggle button.active, .seg button.active { background: var(--accent); border-color: var(--accent); color: #1a1a1a; }

.players { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 6px; }
.player {
  min-height: 58px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface);
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 4px; line-height: 1.15;
}
.player .no { font-size: 20px; font-weight: 800; }
.player .name { font-size: 11px; color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player .sub { font-size: 10px; color: var(--accent); min-height: 11px; }
.player.active { border-color: var(--accent); box-shadow: inset 0 0 0 2px var(--accent); background: var(--surface2); }
.player.fouled-out { opacity: .5; }
.players.compact .player { min-height: 50px; }
@media (max-width: 819px) {
  .players { grid-template-columns: repeat(auto-fill, minmax(60px, 1fr)); }
  .player { min-height: 50px; }
  .player .no { font-size: 18px; }
  .actions button { min-height: 44px; }
}

.actions { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.actions button {
  min-height: var(--tap); border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface2); font-weight: 700; font-size: 14px;
}
.actions button:active, .player:active, .big:active { transform: scale(.97); }
.ok { background: var(--ok) !important; border-color: var(--ok) !important; color: #fff; }
.ng { background: var(--ng) !important; border-color: var(--ng) !important; color: #fff; }

.log-head { max-width: 100%; display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 13px; margin-top: 4px; }
.log { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.log li {
  display: grid; grid-template-columns: auto auto 1fr auto; gap: 8px; align-items: center;
  background: var(--surface); border-radius: 8px; padding: 6px 8px; border-left: 4px solid var(--line); font-size: 13px;
}
.log li.home { border-left-color: var(--home); }
.log li.away { border-left-color: var(--away); }
.log li.empty, .game-list li.empty, .report-list li.empty { display: block; color: var(--muted); border-left: 0; background: none; }
.log .q { color: var(--muted); font-size: 11px; }
.log .t { max-width: 6em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 11px; }
.log small { color: var(--muted); }

button.small { min-height: 34px; padding: 4px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface2); font-size: 13px; }
button.link { background: none; border: 0; color: var(--muted); text-decoration: underline; min-height: 40px; }

/* シュートの結果のシート */
.sheet { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: flex; align-items: flex-end; justify-content: center; z-index: 20; }
.sheet-body {
  width: 100%; max-width: 560px; background: var(--surface); border-radius: 16px 16px 0 0; padding: 14px 16px calc(14px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 10px; max-height: 80vh; overflow: auto; animation: up .15s ease-out;
}
@media (min-width: 820px) { .sheet { align-items: center; } .sheet-body { border-radius: 16px; } }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.sheet-title { font-weight: 700; text-align: center; }
.result-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.big { min-height: 64px; border-radius: 12px; border: 0; font-size: 20px; font-weight: 800; }

/* フォーム */
.form { display: flex; flex-direction: column; gap: 12px; max-width: 640px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
input, textarea {
  font: inherit; font-size: 16px; color: var(--text); background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 12px; width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; }
button.primary, .form button[type="submit"], .button-row button {
  min-height: var(--tap); border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface2); font-weight: 700; padding: 0 16px;
}
button.primary, .form button.primary { background: var(--accent); border-color: var(--accent); color: #1a1a1a; }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; }
details summary { color: var(--muted); font-size: 13px; min-height: 32px; cursor: pointer; }
details label { margin-top: 8px; }

.no-game { text-align: center; padding: 40px 16px; color: var(--muted); }

.game-list, .report-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-width: 640px; }
.game-list li {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px;
}
.game-list li.active { border-color: var(--accent); }
.pill { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--surface2); border: 1px solid var(--line); white-space: nowrap; }
.game-list .pill { border-color: var(--accent); color: var(--accent); }

.report-list li { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.report-head { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-bottom: 6px; }
.report-text { white-space: pre-wrap; word-break: break-word; }
.reply { margin-top: 8px; padding: 8px 10px; background: var(--surface2); border-radius: 8px; border-left: 3px solid var(--ok); font-size: 14px; }
.kind-bug { color: var(--ng); border-color: var(--ng); }
.kind-request { color: var(--home); border-color: var(--home); }
.status-in_progress { color: var(--accent); border-color: var(--accent); }
.status-done { color: var(--ok); border-color: var(--ok); }
.status-unsent { color: var(--muted); }

.license-box { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; max-width: 640px; }

.locked-card { max-width: 560px; margin: 16px auto; background: var(--surface); border: 1px dashed var(--line); border-radius: 14px; padding: 16px 20px; }
.locked-card ul { padding-left: 20px; line-height: 1.8; }
.locked-card .lock-state { color: var(--accent); }
.locked-card.unlocked { border-style: solid; border-color: var(--ok); }
.locked-card.unlocked .lock-state { color: var(--ok); }

.toast {
  /* 上に出す(下だと記録ボタンに重なって押せなくなる) */
  position: fixed; left: 50%; top: calc(56px + env(safe-area-inset-top)); transform: translate(-50%, -10px);
  background: #000c; color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 30; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--ng); }

#view-report .log-head { max-width: 640px; }
