:root {
    --felt: #0f3d2e;
    --felt-2: #17533f;
    --bg: #f4efe4;
    --card: #fffdf8;
    --text: #1d1d1d;
    --muted: #5f5a52;
    --line: #e2d9c6;
    --gold: #f2c94c;
    --gold-bg: #fff4cc;
    --danger: #b3261e;
    --radius: 18px;
    --nav-h: 76px;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #101914;
        --card: #1b2620;
        --text: #f1ede4;
        --muted: #a9b3ad;
        --line: #2c3a33;
        --gold-bg: #3a3218;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 18px;
    line-height: 1.4;
    background: var(--bg);
    color: var(--text);
    padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
    -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- Header ---------- */

.top {
    background: var(--felt);
    color: #fff;
    padding: calc(env(safe-area-inset-top) + 14px) 16px 14px;
    text-align: center;
    position: sticky;
    top: 0;
    z-index: 10;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .2);
}

.top h1 {
    margin: 0;
    font-size: 22px;
    letter-spacing: .04em;
}

.sync-status {
    margin-top: 8px;
    font-size: 15px;
    background: rgba(255, 255, 255, .14);
    border-radius: 10px;
    padding: 6px 10px;
}

main {
    max-width: 560px;
    margin: 0 auto;
    padding: 16px;
}

.tab { display: none; }
.tab.active { display: block; }

/* ---------- Buttons ---------- */

.btn {
    min-height: 56px;
    padding: 12px 20px;
    border-radius: 14px;
    border: 2px solid transparent;
    font-weight: 700;
    font-size: 19px;
}

.btn:disabled { opacity: .4; cursor: default; }
.btn-primary { background: var(--felt); color: #fff; }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.wide { width: 100%; margin-top: 16px; }

.actions {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 12px;
    margin-top: 20px;
}

/* ---------- New game ---------- */

.date-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 10px 16px;
    font-weight: 700;
}

.date-row input {
    font: inherit;
    font-weight: 400;
    font-size: 19px;
    padding: 8px 10px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--bg);
    color: var(--text);
    min-height: 48px;
}

.hint {
    text-align: center;
    font-size: 20px;
    font-weight: 600;
    margin: 20px 0 14px;
    min-height: 2.8em;
}

.players {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
}

.player {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 18px 8px 16px;
    background: var(--card);
    border: 3px solid var(--line);
    border-radius: var(--radius);
    box-shadow: 0 2px 6px rgba(0, 0, 0, .08);
    transition: transform .1s, border-color .15s;
}

.player:active { transform: scale(.97); }

.player-img {
    width: 96px;
    height: 96px;
    border-radius: 50%;
    object-fit: cover;
}

.player-name { font-weight: 700; font-size: 19px; }

.player.ranked { border-color: var(--felt-2); }
.player.place-1 { border-color: var(--gold); background: var(--gold-bg); }
.player.ranked .player-img { box-shadow: 0 0 0 4px var(--felt-2); }
.player.place-1 .player-img { box-shadow: 0 0 0 4px var(--gold); }

.place-medal {
    position: absolute;
    top: 6px;
    left: 8px;
    font-size: 32px;
    line-height: 1;
}

.place-points {
    min-height: 1.3em;
    margin-top: -6px;
    font-size: 15px;
    font-weight: 700;
    color: var(--felt-2);
}

@media (prefers-color-scheme: dark) {
    .place-points { color: #7fd1a8; }
}

.avatar-letter {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #c9b88f;
    color: #fff;
    font-weight: 800;
    font-size: 1.4em;
}

/* ---------- Standings ---------- */

.chips {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 6px;
    margin-bottom: 12px;
}

.chip {
    flex: none;
    min-height: 44px;
    padding: 6px 18px;
    border-radius: 999px;
    border: 2px solid var(--line);
    background: var(--card);
    font-weight: 700;
}

.chip.active { background: var(--felt); border-color: var(--felt); color: #fff; }

.standings, .history {
    list-style: none;
    margin: 0;
    padding: 0;
}

.standing {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 12px 16px;
    margin-bottom: 10px;
}

.standing.leader { background: var(--gold-bg); border-color: var(--gold); }

.standing-rank {
    width: 40px;
    text-align: center;
    font-size: 28px;
    font-weight: 800;
    color: var(--muted);
}

.standing-img {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    object-fit: cover;
    flex: none;
}

.standing-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.standing-name { font-weight: 700; font-size: 20px; }
.standing-sub { color: var(--muted); font-size: 15px; }

.standing-points {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    line-height: 1;
}

.standing-points strong { font-size: 32px; }
.standing-points small { color: var(--muted); font-size: 14px; margin-top: 4px; }

.empty {
    text-align: center;
    color: var(--muted);
    padding: 24px 0;
}

/* ---------- History ---------- */

.history-date {
    font-weight: 700;
    color: var(--muted);
    margin: 18px 4px 8px;
    font-size: 16px;
}

.history-date:first-child { margin-top: 0; }

.game {
    position: relative;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 12px 56px 12px 12px;
    margin-bottom: 10px;
}

.game.pending { border-style: dashed; }

.game-order {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
}

.game-player {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    min-width: 0;
}

.game-medal { font-size: 20px; }

.game-img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    margin: 2px 0;
}

.game-name {
    font-size: 14px;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.game-pending {
    display: block;
    margin-top: 8px;
    font-size: 14px;
    color: var(--muted);
}

.game-delete {
    position: absolute;
    top: 50%;
    right: 6px;
    transform: translateY(-50%);
    width: 46px;
    height: 46px;
    border: 0;
    border-radius: 12px;
    background: transparent;
    font-size: 22px;
}

.game-delete:active { background: var(--line); }

/* ---------- Bottom navigation ---------- */

.bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    background: var(--card);
    border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
    z-index: 10;
}

.nav-btn {
    height: var(--nav-h);
    border: 0;
    background: transparent;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    font-size: 15px;
    font-weight: 600;
    color: var(--muted);
}

.nav-icon { font-size: 26px; line-height: 1; filter: grayscale(1); opacity: .7; }
.nav-btn.active { color: var(--text); }
.nav-btn.active .nav-icon { filter: none; opacity: 1; }

/* ---------- Overlays ---------- */

.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .5);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    z-index: 50;
}

.overlay[hidden], .install[hidden], [hidden] { display: none !important; }

.sheet {
    width: 100%;
    max-width: 420px;
    background: var(--card);
    border-radius: 22px;
    padding: 24px;
    text-align: center;
}

.sheet h2 { margin: 0 0 8px; }

.sheet input {
    width: 100%;
    font-size: 28px;
    text-align: center;
    letter-spacing: .3em;
    padding: 12px;
    border-radius: 14px;
    border: 2px solid var(--line);
    background: var(--bg);
    color: var(--text);
    margin-top: 8px;
}

.error { color: var(--danger); font-weight: 600; }

.install {
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 12px);
    background: var(--card);
    border: 2px solid var(--felt-2);
    border-radius: var(--radius);
    padding: 14px 16px;
    box-shadow: 0 6px 20px rgba(0, 0, 0, .2);
    z-index: 20;
    max-width: 540px;
    margin: 0 auto;
}

.install p { margin: 0 0 10px; }
.install-actions { display: flex; gap: 10px; justify-content: flex-end; }
.install .btn { min-height: 46px; font-size: 17px; }

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
    transform: translate(-50%, 20px);
    background: #222;
    color: #fff;
    padding: 14px 20px;
    border-radius: 14px;
    font-weight: 600;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s, transform .2s;
    z-index: 60;
    width: max-content;
    max-width: calc(100% - 32px);
    text-align: center;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 360px) {
    .player-img { width: 76px; height: 76px; }
    .place-medal { font-size: 26px; }
}
