/* ============ Family Olympics ============ */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f4f2fb;
  --card: #ffffff;
  --card2: #f7f5ff;
  --ink: #221a44;
  --ink-soft: #6b6392;
  --line: #e5e0f5;
  --brand: #5b3df5;
  --brand2: #8b5cf6;
  --gold: #f5b301;
  --silver: #9aa5b1;
  --bronze: #c97a3d;
  --good: #14a05a;
  --bad: #e0475b;
  --header-grad: linear-gradient(135deg, #2a1a6e 0%, #5b3df5 60%, #8b5cf6 100%);
  --shadow: 0 4px 18px rgba(43, 26, 110, 0.10);
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14102a;
    --card: #1e1839;
    --card2: #262047;
    --ink: #efeaff;
    --ink-soft: #a79fd0;
    --line: #322a5c;
    --brand: #8b74ff;
    --brand2: #a78bfa;
    --header-grad: linear-gradient(135deg, #1a1145 0%, #3a2a8e 60%, #5b3df5 100%);
    --shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
  }
}

html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100dvh;
  padding-bottom: calc(74px + env(safe-area-inset-bottom));
}

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; -webkit-tap-highlight-color: transparent; }
input { font: inherit; }

/* ---------- header ---------- */
#topbar {
  background: var(--header-grad);
  color: #fff;
  padding: calc(14px + env(safe-area-inset-top)) 18px 14px;
  border-radius: 0 0 22px 22px;
  box-shadow: var(--shadow);
  position: sticky; top: 0; z-index: 20;
}
#appTitle { font-size: 21px; font-weight: 800; letter-spacing: 0.2px; cursor: pointer; }
#headerSub { font-size: 12.5px; opacity: 0.85; margin-top: 2px; }

/* ---------- layout ---------- */
#main { max-width: 640px; margin: 0 auto; padding: 14px 14px 20px; }
.view { display: none; }
.view.active { display: block; animation: pop-in 0.18s ease-out; }
@keyframes pop-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

h2.section { font-size: 15px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.8px; color: var(--ink-soft); margin: 18px 2px 8px; }

/* ---------- tab bar ---------- */
#tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  box-shadow: 0 -4px 18px rgba(0,0,0,0.08);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 9px 0 8px; font-size: 11px; font-weight: 700; color: var(--ink-soft);
}
.tab-ico { font-size: 22px; line-height: 1; }
.tab.active { color: var(--brand); }
.tab.active .tab-ico { transform: scale(1.15); }

/* ---------- chips & filters ---------- */
.chip-row { display: flex; gap: 7px; overflow-x: auto; padding: 4px 2px 8px; scrollbar-width: none; }
.chip-row::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; padding: 8px 13px; border-radius: 999px;
  background: var(--card); border: 1.5px solid var(--line);
  font-size: 13.5px; font-weight: 700; color: var(--ink-soft); white-space: nowrap;
}
.chip.on { background: var(--brand); border-color: var(--brand); color: #fff; }

.searchbar {
  width: 100%; padding: 12px 15px; border-radius: 14px;
  border: 1.5px solid var(--line); background: var(--card); color: var(--ink);
  font-size: 15px; outline: none; margin-bottom: 8px;
}
.searchbar:focus { border-color: var(--brand); }
.searchbar::placeholder { color: var(--ink-soft); }

.result-count { font-size: 12.5px; color: var(--ink-soft); margin: 2px 4px 8px; }

/* ---------- game cards ---------- */
.game-list { display: flex; flex-direction: column; gap: 9px; }
.game-card {
  display: flex; align-items: stretch; text-align: left; width: 100%; overflow: hidden;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.game-card.played { border-color: rgba(20,160,90,0.45); }
.game-open { display: flex; align-items: center; gap: 12px; min-width: 0; flex: 1; padding: 12px 8px 12px 14px; text-align: left; }
.game-open:active { transform: scale(0.985); }
.game-emoji { font-size: 30px; flex: 0 0 auto; }
.game-info { flex: 1; min-width: 0; }
.game-name { font-size: 15.5px; font-weight: 800; }
.game-meta { display: block; font-size: 12px; color: var(--ink-soft); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.game-badges { display: flex; flex-wrap: wrap; gap: 4px; }
.badge {
  display: inline-block; font-size: 10.5px; font-weight: 800; padding: 2px 8px;
  border-radius: 999px; background: rgba(20,160,90,0.13); color: var(--good); margin-top: 4px;
}
.badge.gear { background: rgba(91,61,245,0.10); color: var(--brand); }
.badge.played-badge { background: rgba(20,160,90,0.16); color: var(--good); }
.favorite-btn {
  width: 46px; flex: 0 0 46px; display: grid; place-items: center;
  border-left: 1px solid var(--line); color: var(--ink-soft); font-size: 25px;
}
.favorite-btn.on { color: var(--gold); }
.favorite-btn:active { background: var(--card2); }

/* ---------- random / pick tab ---------- */
.bigbtn {
  width: 100%; padding: 18px; border-radius: 18px; font-size: 19px; font-weight: 900;
  background: var(--header-grad); color: #fff; box-shadow: var(--shadow);
  letter-spacing: 0.3px;
}
.bigbtn:active { transform: scale(0.98); }
.bigbtn.alt { background: linear-gradient(135deg, #b8860b, #f5b301); color: #2a1a00; }
.bigbtn.small { font-size: 15px; padding: 13px; }
.bigbtn:disabled { opacity: 0.45; cursor: not-allowed; box-shadow: none; }

.pick-card {
  background: var(--card); border: 1px solid var(--line); border-radius: 20px;
  box-shadow: var(--shadow); padding: 20px 18px; margin-top: 14px; text-align: center;
}
.pick-card .game-emoji { font-size: 52px; }
.pick-card h3 { font-size: 22px; font-weight: 900; margin: 6px 0 2px; }
.pick-card .cat { color: var(--ink-soft); font-weight: 700; font-size: 13px; }
.tiebreak-tag {
  display: inline-block; background: var(--gold); color: #3a2a00; font-weight: 900;
  font-size: 12px; padding: 3px 12px; border-radius: 999px; margin-bottom: 8px; letter-spacing: 1px;
}

/* ---------- event programs ---------- */
.program-empty { margin-top: 8px; padding: 18px; }
.program-empty .big { font-size: 28px; }
.program-head { display: flex; justify-content: space-between; align-items: center; margin: 10px 3px 7px; color: var(--ink-soft); font-size: 12.5px; font-weight: 800; }
.text-action { color: var(--brand); font-weight: 800; padding: 4px 6px; }
.program-list { display: flex; flex-direction: column; gap: 6px; }
.program-game {
  display: grid; grid-template-columns: 28px 30px minmax(0, 1fr) auto; align-items: center; gap: 8px;
  width: 100%; min-height: 48px; padding: 8px 12px; text-align: left;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
}
.program-game.done { border-color: rgba(20,160,90,0.45); background: rgba(20,160,90,0.06); }
.program-number { width: 26px; height: 26px; display: grid; place-items: center; border-radius: 50%; background: var(--card2); color: var(--ink-soft); font-size: 12px; font-weight: 900; }
.program-game.done .program-number { background: var(--good); color: #fff; }
.program-emoji { font-size: 22px; }
.program-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; font-weight: 800; }
.program-time { color: var(--ink-soft); font-size: 11px; font-weight: 700; white-space: nowrap; }

/* ---------- detail modal ---------- */
#modalRoot[hidden] { display: none; }
#modalRoot {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(15, 10, 40, 0.55);
  display: flex; align-items: flex-end; justify-content: center;
  backdrop-filter: blur(2px);
}
.sheet {
  background: var(--bg); width: 100%; max-width: 640px;
  max-height: 88dvh; overflow-y: auto;
  border-radius: 22px 22px 0 0; padding: 10px 18px calc(24px + env(safe-area-inset-bottom));
  animation: sheet-up 0.22s ease-out;
}
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0.4; } to { transform: none; opacity: 1; } }
.sheet-grab { width: 44px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 12px; }
.sheet-head { display: flex; align-items: flex-start; gap: 12px; }
.sheet-head .game-emoji { font-size: 44px; }
.sheet-head h3 { font-size: 22px; font-weight: 900; line-height: 1.15; }
.sheet-head .cat { color: var(--ink-soft); font-weight: 700; font-size: 13px; margin-top: 2px; }
.sheet-close {
  margin-left: auto; flex: 0 0 auto; width: 34px; height: 34px; border-radius: 50%;
  background: var(--card); border: 1px solid var(--line); font-size: 15px; font-weight: 800; color: var(--ink-soft);
}
.tag-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0 4px; }
.tag { font-size: 12px; font-weight: 700; background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px; color: var(--ink-soft); }

.info-block { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 13px 15px; margin-top: 10px; }
.info-block h4 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--ink-soft); margin-bottom: 5px; }
.info-block p { font-size: 15px; line-height: 1.45; }
.info-block ul { padding-left: 20px; font-size: 15px; line-height: 1.5; }
.info-block.swap { background: var(--card2); border-style: dashed; }

/* ---------- timer inside game rules ---------- */
.game-timer { border-color: rgba(20,160,90,0.4); background: rgba(20,160,90,0.07); }
.game-timer-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.game-timer-head h4 { margin: 0; }
.game-timer-head span { color: var(--ink-soft); font-size: 11.5px; font-weight: 700; }
.sheet-timer-display {
  min-height: 58px; display: flex; align-items: baseline; justify-content: center; margin: 8px 0 4px;
  color: var(--ink); font-size: 44px; font-weight: 900; font-variant-numeric: tabular-nums; line-height: 1;
}
.sheet-timer-display.warn { color: var(--bad); }
.sheet-timer-display .tenths { margin-left: 2px; color: var(--ink-soft); font-size: 0.45em; }
.sheet-timer-presets { display: flex; justify-content: center; gap: 6px; flex-wrap: wrap; margin: 6px 0 10px; }
.sheet-timer-presets .preset { padding: 7px 12px; font-size: 12px; }
.sheet-timer-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sheet-timer-actions button {
  min-height: 44px; border: 1.5px solid var(--line); border-radius: 8px;
  background: var(--card); color: var(--ink-soft); font-weight: 900;
}
.sheet-timer-actions .go { border-color: var(--good); background: var(--good); color: #fff; }
.sheet-timer-actions .stop { border-color: var(--bad); background: var(--bad); color: #fff; }
.sheet-timer-status { min-height: 16px; margin-top: 6px; color: var(--ink-soft); font-size: 11.5px; font-weight: 800; text-align: center; }

/* ---------- medals / recording ---------- */
.medal-section { margin-top: 14px; }
.medal-row { margin-top: 10px; }
.medal-row h4 { font-size: 14px; font-weight: 800; margin-bottom: 6px; }
.player-chips { display: flex; flex-wrap: wrap; gap: 7px; }
.pchip {
  padding: 8px 13px; border-radius: 999px; font-size: 14px; font-weight: 700;
  background: var(--card); border: 1.5px solid var(--line);
}
.pchip.on-gold { background: var(--gold); border-color: var(--gold); color: #3a2a00; }
.pchip.on-silver { background: var(--silver); border-color: var(--silver); color: #1d232b; }
.pchip.on-bronze { background: var(--bronze); border-color: var(--bronze); color: #fff; }
.quick-add { display: flex; gap: 7px; margin-top: 10px; }
.quick-add input { flex: 1; padding: 9px 13px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--card); color: var(--ink); outline: none; }
.quick-add button { padding: 9px 14px; border-radius: 12px; background: var(--card); border: 1.5px solid var(--line); font-weight: 800; color: var(--brand); }
.save-row { display: flex; gap: 9px; margin-top: 16px; }
.save-row .bigbtn { flex: 1; }
.hint { font-size: 12.5px; color: var(--ink-soft); margin-top: 8px; text-align: center; }

/* ---------- podium ---------- */
.stand-list { display: flex; flex-direction: column; gap: 8px; }
.stand-row {
  display: flex; align-items: center; gap: 11px; width: 100%; text-align: left;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow);
}
.stand-row.first { border-color: var(--gold); border-width: 2px; }
.stand-rank { font-size: 20px; width: 30px; text-align: center; flex: 0 0 auto; font-weight: 900; color: var(--ink-soft); }
.stand-ava { font-size: 26px; flex: 0 0 auto; }
.stand-name { font-weight: 800; font-size: 16px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.stand-medals { font-size: 13px; font-weight: 700; color: var(--ink-soft); white-space: nowrap; }
.stand-pts { font-size: 15px; font-weight: 900; color: var(--brand); margin-left: 8px; white-space: nowrap; }

.empty-box {
  background: var(--card); border: 1.5px dashed var(--line); border-radius: var(--radius);
  padding: 26px 18px; text-align: center; color: var(--ink-soft); font-size: 15px; line-height: 1.5;
}
.empty-box .big { font-size: 38px; display: block; margin-bottom: 6px; }

.hist-item {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border: 1px solid var(--line); border-radius: 13px;
  padding: 10px 13px; margin-bottom: 7px;
}
.hist-body { flex: 1; min-width: 0; }
.hist-game { font-weight: 800; font-size: 14px; }
.hist-medals { font-size: 12.5px; color: var(--ink-soft); margin-top: 1px; }
.hist-actions { display: flex; flex: 0 0 auto; gap: 2px; }
.hist-edit, .hist-del { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 50%; font-weight: 800; font-size: 15px; }
.hist-edit { color: var(--brand); }
.hist-del { color: var(--bad); }
.hist-edit:active, .hist-del:active { background: var(--card2); }

.row-2 { display: flex; gap: 9px; margin-top: 10px; }
.row-2 > * { flex: 1; }
.ghostbtn {
  padding: 12px; border-radius: 14px; font-weight: 800; font-size: 14px;
  background: var(--card); border: 1.5px solid var(--line); color: var(--ink-soft);
}
.ghostbtn.danger { color: var(--bad); }

/* ---------- closing ceremony ---------- */
.ceremony-launch { margin-top: 12px; background: var(--gold); color: #2a1a00; }
.ceremony-sheet { position: relative; overflow-x: hidden; background: var(--bg); }
.ceremony-close { position: absolute; top: 14px; right: 16px; }
.ceremony-head { padding: 8px 38px 0; text-align: center; }
.ceremony-kicker { color: var(--brand); font-size: 12px; font-weight: 900; letter-spacing: 0.8px; text-transform: uppercase; }
.ceremony-head h2 { margin-top: 4px; font-size: 25px; line-height: 1.15; overflow-wrap: anywhere; }
.ceremony-head p { margin-top: 5px; color: var(--ink-soft); font-size: 12.5px; font-weight: 700; }
.ceremony-podium {
  min-height: 310px; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr));
  align-items: end; gap: 7px; margin: 18px auto 12px; padding: 0 2px; border-bottom: 5px solid var(--line);
}
.ceremony-slot { min-width: 0; display: flex; flex-direction: column; align-items: stretch; justify-content: flex-end; }
.ceremony-person {
  min-height: 116px; display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  gap: 3px; padding: 0 2px 8px; text-align: center;
}
.ceremony-avatar { font-size: 46px; line-height: 1; }
.ceremony-person strong { width: 100%; font-size: 13px; line-height: 1.15; overflow-wrap: anywhere; }
.ceremony-person small { color: var(--ink-soft); font-size: 10px; font-weight: 800; line-height: 1.25; }
.ceremony-slot.empty .ceremony-person { opacity: 0.42; }
.ceremony-block {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 72px; border: 1px solid rgba(0,0,0,0.12); border-bottom: 0; border-radius: 8px 8px 0 0;
  color: #211800; background: var(--bronze);
}
.place-1 .ceremony-block { min-height: 132px; background: var(--gold); }
.place-2 .ceremony-block { min-height: 102px; background: var(--silver); color: #17202b; }
.place-3 .ceremony-block { min-height: 78px; color: #fff; }
.ceremony-block > span { font-size: 22px; }
.ceremony-block > strong { font-size: 31px; line-height: 1; }
.ceremony-block > small { min-height: 15px; margin-top: 3px; font-size: 11px; font-weight: 900; }
.ceremony-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.ceremony-actions .ghostbtn { border-radius: 8px; }
.ceremony-finish { margin-top: 9px; }

/* ---------- archives & lifetime ---------- */
.readonly-standings .stand-row { cursor: default; }
.archive-list { display: flex; flex-direction: column; gap: 8px; }
.archive-item { background: var(--card); border: 1px solid var(--line); border-radius: 13px; overflow: hidden; }
.archive-item summary { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 13px 14px; cursor: pointer; list-style: none; }
.archive-item summary::-webkit-details-marker { display: none; }
.archive-title { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.archive-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.archive-title small, .archive-champ { color: var(--ink-soft); font-size: 11.5px; font-weight: 700; }
.archive-champ { flex: 0 0 auto; text-align: right; }
.archive-detail { padding: 0 10px 10px; border-top: 1px solid var(--line); }
.archive-detail .stand-list { margin-top: 10px; }
.archive-detail .stand-row { padding: 9px 10px; box-shadow: none; }
.archive-detail .stand-ava { font-size: 21px; }
.archive-detail .stand-name { font-size: 14px; }
.archive-events { margin-top: 10px; border-top: 1px solid var(--line); }
.archive-event { display: grid; grid-template-columns: 26px minmax(0, 1fr); gap: 8px; padding: 9px 4px; border-bottom: 1px solid var(--line); font-size: 13px; }
.archive-event > span:last-child { display: flex; flex-direction: column; min-width: 0; }
.archive-event small { color: var(--ink-soft); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }

/* ---------- timer ---------- */
.timer-wrap { text-align: center; padding-top: 8px; }
.mode-toggle { display: inline-flex; background: var(--card); border: 1.5px solid var(--line); border-radius: 999px; padding: 4px; gap: 4px; margin-bottom: 12px; }
.mode-toggle button { padding: 8px 18px; border-radius: 999px; font-weight: 800; font-size: 14px; color: var(--ink-soft); }
.mode-toggle button.on { background: var(--brand); color: #fff; }
#timerDisplay {
  font-size: clamp(64px, 22vw, 110px); font-weight: 900; font-variant-numeric: tabular-nums;
  letter-spacing: -2px; line-height: 1.05; margin: 12px 0 4px;
}
#timerDisplay.warn { color: var(--bad); }
#timerDisplay .tenths { font-size: 0.45em; color: var(--ink-soft); }
.preset-row { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; margin: 8px 0 16px; }
.preset { padding: 9px 16px; border-radius: 999px; background: var(--card); border: 1.5px solid var(--line); font-weight: 800; font-size: 14px; }
.preset.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.timer-btns { display: flex; justify-content: center; gap: 12px; }
.timer-btns button {
  width: 92px; height: 92px; border-radius: 50%; font-size: 16px; font-weight: 900;
  border: 2px solid var(--line); background: var(--card); box-shadow: var(--shadow);
}
.timer-btns .go { background: var(--good); border-color: var(--good); color: #fff; }
.timer-btns .stop { background: var(--bad); border-color: var(--bad); color: #fff; }
.flash { animation: flashbg 0.4s ease-in-out 3; }
@keyframes flashbg { 50% { background: var(--bad); } }

/* ---------- confetti ---------- */
#confettiRoot { position: fixed; inset: 0; pointer-events: none; z-index: 90; overflow: hidden; }
.confetti { position: absolute; top: -40px; font-size: 24px; animation: fall linear forwards; }
@keyframes fall {
  to { transform: translateY(110vh) rotate(540deg); opacity: 0.7; }
}

@media (prefers-reduced-motion: reduce) {
  .view.active, .sheet, .confetti, .flash { animation: none; }
}

@media (min-width: 640px) {
  #topbar { border-radius: 0; }
  .game-list { display: grid; grid-template-columns: 1fr 1fr; }
}
