/* ---------- design tokens ---------- */
:root {
    --bg: #f4f5f8;
    --surface: #ffffff;
    --surface-muted: #eceef3;
    --text: #14161c;
    --text-muted: #6b7280;
    --border: #e1e4ea;
    --primary: #3563e9;
    --primary-active: #2749b8;
    --danger: #e0483e;
    --success: #1f9d55;
    --radius: 12px;
    --radius-sm: 8px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --bar-height: 4rem;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #12141c;
        --surface: #1a1d27;
        --surface-muted: #232631;
        --text: #eef0f4;
        --text-muted: #9aa1b1;
        --border: #2c303c;
        --primary: #5b82f5;
        --primary-active: #7c9bff;
    }
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
}

body {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, p { margin: 0 0 .5rem; }
h1 { font-size: clamp(1.15rem, 4vw, 1.4rem); }
h2 { font-size: clamp(1rem, 3.5vw, 1.15rem); color: var(--text-muted); font-weight: 600; }

a { color: var(--primary); }

/* ---------- header ---------- */
header {
    position: sticky;
    top: 0;
    z-index: 50;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: calc(.6rem + var(--safe-top)) .9rem .5rem;
}

.header-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: .75rem;
}

.header-titles {
    flex: 1;
    min-width: 0;
}

.exit-btn {
    flex: 0 0 auto;
    min-height: 34px;
    padding: .35rem .7rem;
    background: var(--surface-muted);
    color: var(--text);
    font-size: .8rem;
    font-weight: 600;
    white-space: nowrap;
}

.exit-btn--danger {
    background: var(--danger);
    color: #fff;
}

.action-log {
    margin-top: .35rem;
}

.action-log summary {
    list-style: none;
    display: inline-flex;
    align-items: center;
    gap: .3rem;
    font-size: .8rem;
    color: var(--text-muted);
    padding: .25rem .6rem;
    background: var(--surface-muted);
    border-radius: 999px;
    cursor: pointer;
    min-height: 30px;
}
.action-log summary::-webkit-details-marker { display: none; }
.action-log summary::before { content: "\1F4DC"; font-size: .8rem; }
.action-log[open] summary::before { content: "\25BC"; font-size: .6rem; }

#action-logs {
    list-style: none;
    margin: .5rem 0 0;
    padding: .4rem .6rem;
    max-height: 30vh;
    overflow-y: auto;
    background: var(--surface-muted);
    border-radius: var(--radius-sm);
    font-size: .82rem;
    color: var(--text-muted);
}
#action-logs li {
    padding: .25rem 0;
    border-bottom: 1px solid var(--border);
}
#action-logs li:last-child { border-bottom: none; }

.error {
    color: var(--danger);
    background: color-mix(in srgb, var(--danger) 12%, transparent);
    border-radius: var(--radius-sm);
    padding: .5rem .7rem;
    margin-top: .5rem;
    font-size: .9rem;
}

/* ---------- layout ---------- */
main {
    flex: 1;
    padding: .75rem 0 1.5rem;
}

main:has(.play-container) {
    padding-bottom: calc(var(--bar-height) + var(--safe-bottom) + 1rem);
}

footer {
    text-align: center;
    color: var(--text-muted);
    font-size: .75rem;
    padding: .75rem 1rem calc(.75rem + var(--safe-bottom));
}

.container {
    max-width: 480px;
    margin: 0 auto;
    padding: 0 1rem;
}

.section-heading {
    margin: .25rem .9rem .4rem;
}

/* ---------- responsive card grid ---------- */
.card-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
    gap: .6rem;
    padding: .25rem .9rem 1rem;
    margin: 0;
}

.card-container {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 4px;
    border: 2px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
    transition: border-color .15s ease, background-color .15s ease, transform .1s ease;
}

.card-container:active { transform: scale(.96); }

.card-container.selected {
    border-color: var(--primary);
    background: color-mix(in srgb, var(--primary) 12%, transparent);
}

.card-container.selected::after {
    content: "\2713";
    position: absolute;
    top: -6px;
    right: -6px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    font-size: .75rem;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 1px 3px rgba(0,0,0,.3);
}

.card-image {
    width: 100%;
    height: auto;
    border-radius: var(--radius-sm);
    aspect-ratio: 63 / 88;
    box-shadow: 0 1px 3px rgba(0,0,0,.18);
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* ---------- card zoom overlay ---------- */
.card-zoom-overlay {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem 1rem;
    background: rgba(0, 0, 0, .82);
}

.card-zoom-overlay[hidden] {
    display: none;
}

.card-zoom-overlay img {
    max-width: 100%;
    max-height: 100%;
    border-radius: var(--radius);
    box-shadow: 0 8px 30px rgba(0, 0, 0, .5);
}

/* ---------- buttons ---------- */
.select-button, button, input[type="submit"] {
    -webkit-appearance: none;
    appearance: none;
    min-height: 44px;
    padding: .55rem 1rem;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
}

.select-button:active, button:active, input[type="submit"]:active {
    background: var(--primary-active);
    transform: scale(.98);
}

/* ---------- bottom action bar (card / player selection) ---------- */
.play-container {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .75rem;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: .6rem .9rem calc(.6rem + var(--safe-bottom));
    box-shadow: 0 -2px 10px rgba(0,0,0,.08);
}

.play-container h2 {
    margin: 0;
    font-size: .9rem;
    color: var(--text-muted);
}

.play-container #selected_count { color: var(--text); font-weight: 700; }

/* ---------- players list (select_players) ---------- */
.players-list {
    list-style: none;
    margin: 0;
    padding: 0 .9rem;
    display: flex;
    flex-direction: column;
    gap: .5rem;
}

.player-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: .75rem .9rem;
    min-height: 44px;
    cursor: pointer;
}

.player-row.selected {
    border-color: var(--primary);
    background: color-mix(in srgb, var(--primary) 10%, var(--surface));
}

.player-check {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 2px solid var(--border);
    flex: 0 0 auto;
}

.player-row.selected .player-check {
    background: var(--primary);
    border-color: var(--primary);
    position: relative;
}
.player-row.selected .player-check::after {
    content: "\2713";
    position: absolute;
    inset: 0;
    color: #fff;
    font-size: .8rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ---------- lobby ---------- */
.lobby ul {
    list-style: none;
    margin: 0 0 1rem;
    padding: 0 .9rem;
    display: flex;
    flex-direction: column;
    gap: .5rem;
}
.lobby li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: .6rem .8rem;
}
.lobby li button {
    min-height: 32px;
    padding: .25rem .6rem;
    background: var(--danger);
    font-weight: 700;
}
.player-count-number {
    font-weight: 800;
    font-size: 1.15rem;
    color: var(--text-muted);
    transition: color .2s ease;
}

.player-count-number.ready {
    color: var(--success);
}

.mode-container {
    padding: 0 .9rem 1rem;
}

.setting-row {
    margin-bottom: .75rem;
}

.setting-row:last-child {
    margin-bottom: 0;
}

.setting-label {
    display: block;
    font-size: .8rem;
    color: var(--text-muted);
    margin-bottom: .3rem;
}

.mode-toggle {
    display: flex;
    gap: .5rem;
}

.mode-option {
    flex: 1;
    background: var(--surface-muted);
    color: var(--text);
}

.mode-option.active {
    background: var(--primary);
    color: #fff;
}

.mode-option:disabled {
    cursor: default;
}

/* ---------- home / forms ---------- */
form {
    display: flex;
    flex-direction: column;
    gap: .5rem;
    padding-bottom: 1rem;
}
form label {
    font-size: .85rem;
    color: var(--text-muted);
    margin-top: .5rem;
}
form input, form select {
    min-height: 44px;
    padding: .5rem .7rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    background: var(--surface);
    color: var(--text);
}

.public-games-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: .5rem;
}

.public-game-button {
    width: 100%;
    text-align: left;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    font-weight: 500;
}

.public-game-button:active {
    background: var(--surface-muted);
}

/* ---------- finished page ---------- */
.finished-page {
    padding-top: .5rem;
}

.result-banner {
    text-align: center;
    padding: 2rem 1rem 1.75rem;
    border-radius: var(--radius);
    margin-bottom: 1.25rem;
}

.result-banner--win {
    background: linear-gradient(135deg, var(--primary), var(--primary-active));
    color: #fff;
}

.result-banner--lose {
    background: color-mix(in srgb, var(--danger) 16%, var(--surface));
    color: var(--text);
}

.result-emoji {
    font-size: 2.75rem;
    line-height: 1;
    margin-bottom: .5rem;
}

.result-title {
    margin: 0;
    font-size: 1.3rem;
    font-weight: 700;
    color: inherit;
}

.section-title {
    padding: 0 .1rem;
}

.leaderboard {
    list-style: none;
    margin: 0 0 1.5rem;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: .5rem;
}

.leaderboard li {
    display: flex;
    align-items: center;
    gap: .75rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: .65rem .9rem;
}

.leaderboard li.winner {
    border-color: var(--primary);
    background: color-mix(in srgb, var(--primary) 8%, var(--surface));
}

.leaderboard .rank {
    font-size: 1.1rem;
    font-weight: 700;
    min-width: 1.6rem;
    text-align: center;
    color: var(--text-muted);
}

.leaderboard-name {
    flex: 1;
    font-weight: 600;
}

.leaderboard-score {
    font-weight: 700;
    color: var(--text-muted);
}

.finished-actions {
    display: flex;
    flex-direction: column;
    gap: .6rem;
}

.btn {
    display: block;
    text-align: center;
    text-decoration: none;
    margin-bottom: .6rem;
    min-height: 44px;
    line-height: 44px;
    border-radius: var(--radius-sm);
    background: var(--primary);
    color: #fff;
    font-weight: 600;
}

.finished-actions .btn { margin-bottom: 0; }

.btn-secondary {
    background: var(--surface-muted);
    color: var(--text);
}

/* ---------- confirm modal ---------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: rgba(0, 0, 0, .5);
}

.modal-overlay[hidden] {
    display: none;
}

.modal-box {
    width: 100%;
    max-width: 360px;
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.25rem;
    box-shadow: 0 8px 30px rgba(0, 0, 0, .3);
}

.modal-box p {
    margin: 0 0 1.1rem;
    color: var(--text);
    font-size: .95rem;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: .6rem;
}

.modal-actions button {
    min-height: 40px;
    padding: .5rem 1rem;
}

/* ---------- backend chip ---------- */
/* Sits in the footer: outside every region live.js swaps, and independent of
   the current game, so a view change never disturbs a search in progress. */
.backend-chip {
    display: flex;
    flex-wrap: wrap;
    gap: .5rem;
    align-items: center;
    justify-content: center;
    margin-top: .5rem;
}

.backend-chip__label {
    color: var(--text-muted);
}

.backend-chip__status {
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.backend-chip__button {
    font: inherit;
    font-size: .75rem;
    padding: .25rem .7rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
}

.backend-chip__button:hover {
    border-color: var(--primary);
    color: var(--primary);
}

/* ---------- added for the P2P app ---------- */
.hint {
    color: var(--text-muted);
    font-size: .85rem;
}

.lobby .offline {
    color: var(--text-muted);
    font-size: .8rem;
}

#btn-install {
    width: 100%;
    margin-top: .5rem;
}

/* Pairing two devices by QR (crates/app/src/pairing.rs). Above the confirm modal:
   it is opened from screens that may have one up. */
.pair-overlay {
    position: fixed;
    inset: 0;
    z-index: 120;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: calc(1rem + var(--safe-top)) 1rem calc(1rem + var(--safe-bottom));
    background: rgba(0, 0, 0, .6);
    overflow-y: auto;
}

.pair-overlay[hidden] {
    display: none;
}

.pair-box {
    width: 100%;
    max-width: 420px;
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.1rem;
    box-shadow: 0 8px 30px rgba(0, 0, 0, .3);
}

.pair-box h2 {
    color: var(--text);
}

/* A QR has to be read off a phone screen by another phone's camera: black on white
   whatever the theme, with its quiet zone, as large as the box allows. */
.qr {
    background: #fff;
    border-radius: var(--radius-sm);
    padding: .5rem;
    margin: .5rem 0;
}

.qr svg {
    display: block;
    width: 100%;
    height: auto;
}

#pair-video {
    width: 100%;
    border-radius: var(--radius-sm);
    background: #000;
    margin: .5rem 0;
}

.pair-warn {
    color: var(--danger);
    background: color-mix(in srgb, var(--danger) 12%, transparent);
    border-radius: var(--radius-sm);
    padding: .5rem .7rem;
    font-size: .85rem;
}

.pair-actions {
    display: flex;
    gap: .6rem;
    justify-content: flex-end;
    margin: .5rem 0;
}

.pair-actions button {
    min-height: 40px;
    padding: .5rem 1rem;
}

.pair-box details {
    margin-top: .6rem;
    font-size: .85rem;
}

.pair-box summary {
    color: var(--text-muted);
    cursor: pointer;
}

.pair-box textarea {
    width: 100%;
    font: .8rem/1.3 ui-monospace, Menlo, Consolas, monospace;
    background: var(--surface-muted);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: .4rem;
    margin: .3rem 0;
}

.pair-box label {
    display: block;
    color: var(--text-muted);
    margin-top: .4rem;
}

.status {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: .2rem .8rem;
    margin: .4rem 0 0;
}

.status dt {
    color: var(--text-muted);
}

.status dd {
    margin: 0;
    word-break: break-all;
}

.status dd[data-state="ok"] { color: var(--success); }
.status dd[data-state="wait"] { color: #c98a14; }
.status dd[data-state="bad"] { color: var(--danger); }

/* `.btn` centres one line by line-height; this label wraps to two. */
#btn-install {
    line-height: 1.3;
    padding: .6rem 1rem;
}

.lobby .add-player {
    display: block;
    width: calc(100% - 1.8rem);
    margin: .2rem .9rem .8rem;
}

/* The header's buttons stack, so a second one costs the title no width. */
.header-actions {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: .35rem;
}

/* `hidden` must win over any class that sets `display` — `.btn` sets `block`,
   which once left the install button showing where it could do nothing. */
[hidden] {
    display: none !important;
}
