/* ---------- Theme tokens ---------- */
:root {
    /* Dark (default) */
    --bg: #0f1220;
    --panel: #171a2b;
    --grid: #3a3f5c;
    --dot: #e7e9f7;
    --guide: #8f96c9;
    --text: #e9ecff;
    --heading: #cfd4ff;

    --p1: #22A46B;
    /* Player 1 green */
    --p2: #60A5FA;
    /* Player 2 cyan */

    --border: #2a2f49;
    /* shared borders */
    --muted: #aab0de;
    /* secondary text */
    --chipBg: #0c1130;
    /* pills, turn badge */

    --btnBg: #0f1430;
    --btnBg2: #151a3c;
    /* hover */
    --ring: #b8c0ff;
    /* focus ring */
    --shadow: 0 12px 40px rgba(0, 0, 0, .35);

    color-scheme: dark;

    --tb-gap: 10px;
    --tb-pad-x: 12px;
    --tb-pad-y: 10px;
    --tb-radius: 12px;
    --tb-blur: 12px;
    --safe-top: env(safe-area-inset-top, 0px);

    --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans",
        Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji",
        "Noto Color Emoji", sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
        "Liberation Mono", "Courier New", monospace;
    --turn-color: var(--p1);
}

html {
    font-family: var(--font-sans);
    font-size: 16px;
}

*,
*::before,
*::after {
    font: inherit;
}

/* includes weight/size */
input,
button,
select,
textarea {
    font: inherit;
}

/* override UA defaults */
code,
pre,
kbd,
samp {
    font-family: var(--font-mono);
}

/* Light overrides */
:root[data-theme="light"] {
    --bg: #f6f7ff;
    --panel: #ffffff;
    --grid: #cbd3f5;
    --dot: #1f2341;
    --guide: #9aa4d6;
    --text: #11152a;
    --heading: #1d2343;

    --p1: #198754;
    --p2: #2563EB;

    --border: #d9defb;
    --muted: #5d6798;
    --chipBg: #eef1ff;

    --btnBg: #eef1ff;
    --btnBg2: #e6ebff;
    --ring: #6b7cff;
    --shadow: 0 10px 30px rgba(20, 24, 60, .12);

    color-scheme: light;
}

/* Respect OS if no saved theme */
@media (prefers-color-scheme: light) {
    :root:not([data-theme]) {
        color-scheme: light;
    }
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme]) {
        color-scheme: dark;
    }
}

/* ---------- Base ---------- */
html,
body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Arial, "Noto Sans", sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* smooth theme swap */
    transition: background-color .18s ease, color .18s ease;
}

.wrap {
    max-width: 1200px;
    margin: 0 auto;
    padding: 12px;
    height: 100%;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* --- Topbar base (desktop-first grid) --- */
.topbar {
    position: sticky;
    top: 0;
    /* stick to top */
    z-index: 50;

    display: grid;
    grid-template-columns: 1fr auto auto;
    /* title · action(s) */
    align-items: center;
    gap: var(--tb-gap);

    padding: calc(var(--tb-pad-y) + var(--safe-top)) var(--tb-pad-x) var(--tb-pad-y);
    background: color-mix(in hsl, var(--panel) 85%, transparent);
    backdrop-filter: saturate(150%) blur(var(--tb-blur));
    -webkit-backdrop-filter: saturate(150%) blur(var(--tb-blur));

    border: 1px solid var(--border);
    border-radius: 0 0 var(--tb-radius) var(--tb-radius);
    /* looks like a floating header */
    box-shadow: 0 6px 20px rgb(0 0 0 / 0.06);
}

/* Optional clusters if you have them */
.topbar .title {
    font-weight: 700;
    letter-spacing: .2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
    /* allow truncation */
}

.topbar .meta {
    display: flex;
    gap: 8px;
    align-items: center;
    min-width: 0;
    overflow-x: auto;
    /* long room codes/status can scroll on tiny phones */
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.topbar .meta::-webkit-scrollbar {
    display: none;
}

.topbar .actions {
    display: flex;
    gap: 8px;
    align-items: center;
    justify-self: end;
}

.navLast {
    display: flex;
    justify-content: flex-start;
    gap: 8px;
}

/* overlay is z-index: 20 */

h1 {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 800;
    color: var(--heading);
}

.card h3 {
    margin: 0;
    font-size: .95rem;
    color: var(--heading);
}

.score {
    display: flex;
    gap: 14px;
    align-items: center;
    font-weight: 700;
}

.turn {
    font-weight: 800;
    padding: 2px 10px;
    border-radius: 999px;
    background: var(--chipBg);
    border: 1px solid var(--border);
}

.p1 {
    color: var(--p1);
}

.p2 {
    color: var(--p2);
}

.online-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.75em;
    padding: .25rem .5rem;
    margin-left: .5rem;
    border-radius: 999px;
    font-size: .8125rem;
    line-height: 1;
    background: var(--accent, #4f46e5);
    color: #fff;
}

#game {
    align-self: center;
}

/* ---------- Buttons / inputs ---------- */
button,
.iconbtn,
.bigbtn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    text-decoration: none;
    padding: 6px 12px;
    font-size: 14px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--btnBg);
    color: var(--text);
    cursor: pointer;
    transition: background-color .12s ease, border-color .12s ease, color .12s ease, transform .02s ease;
}

button:hover,
.iconbtn:hover,
.bigbtn:hover {
    background: var(--btnBg2);
}

button:active,
.iconbtn:active,
.bigbtn:active {
    transform: translateY(1px);
}

button:focus-visible,
.iconbtn:focus-visible,
.bigbtn:focus-visible,
input:focus-visible {
    outline: 2px solid var(--ring);
    outline-offset: 2px;
}

input,
select {
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--btnBg);
    color: var(--text);
}

/* ---------- Layout ---------- */
.hidden {
    display: none !important;
}

.main {
    display: flex;
    gap: 12px;
    align-items: stretch;
}

.board {
    background: var(--panel);
    border-radius: 12px;
    padding: 12px;
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 10px;
    flex: 1 1 auto;
    min-width: 0;
}

.board-wrap {
    border-radius: 12px;
    transition: box-shadow .2s ease, outline-color .2s ease;
}

.board-wrap.is-your-turn {
    /* subtle colored outline + inner glow */
    box-shadow:
        0 0 0 2px var(--turn-color),
        0 0 18px 2px color-mix(in srgb, var(--turn-color) 30%, transparent) inset;
}

/* Roll button pulse when user must roll */
.pulse {
    animation: roll-pulse 1.2s ease-out infinite;
}

@keyframes roll-pulse {
    0% {
        box-shadow: 0 0 0 0 var(--turn-color);
        transform: translateZ(0);
    }

    70% {
        box-shadow: 0 0 0 12px color-mix(in srgb, var(--turn-color) 0%, transparent);
    }

    100% {
        box-shadow: 0 0 0 0 transparent;
    }
}

/* optional: small bump on the "your turn" label */
.turn-badge.bump {
    animation: bump .35s ease;
}

@keyframes bump {
    0% {
        transform: scale(1)
    }

    60% {
        transform: scale(1.06)
    }

    100% {
        transform: scale(1)
    }
}

.controls {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.nums {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    width: 100%;
    justify-content: center;
    padding: 4px;
}

.nums button {
    min-width: 40px;
    font-weight: 800;
}

.nums button.sel {
    outline: 2px solid var(--ring);
    outline-offset: 1px;
}

.status {
    font-size: 17px;
    color: var(--muted);
}

.moves-left {
    font-weight: 800;
    padding: 0px 8px;
    color: var(--turn-color);
}

canvas {
    width: 100%;
    height: auto;
    display: block;
    border-radius: 8px;
    background: transparent;
}

.sidebar {
    width: 320px;
    flex: 0 0 320px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.card {
    background: var(--panel);
    border-radius: 12px;
    padding: 12px;
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 10px;
    box-shadow: var(--shadow);
}

.leaderboard .table {
    width: 100%;
    border-collapse: collapse;
    font-size: .9rem;
}

.leaderboard .table th,
.leaderboard .table td {
    padding: 6px 8px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.leaderboard .table tbody tr:nth-child(odd) {
    background: color-mix(in oklab, var(--panel) 92%, var(--text) 8%);
}

:root[data-theme="light"] .leaderboard .table tbody tr:nth-child(odd) {
    background: color-mix(in oklab, var(--panel) 96%, var(--text) 4%);
}

.leaderboard .totals {
    margin-top: 8px;
    border-top: 1px solid var(--border);
    padding-top: 8px;
    display: grid;
    grid-template-columns: 1fr auto;
    row-gap: 6px;
}

.leaderboard .badge {
    font-weight: 700;
}

.leaderboard .badge.p1 {
    color: var(--p1);
}

.leaderboard .badge.p2 {
    color: var(--p2);
}

.local-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 10px;
    margin-bottom: 8px;
}

.local-stats .stat {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 8px 10px;
}

.local-stats .label {
    font-size: 12px;
    opacity: .7;
}

.local-stats .value {
    font-size: 20px;
    font-weight: 600;
}

.die {
    font-size: 56px;
    line-height: 1;
    text-align: center;
    user-select: none;
}

#rollBtn {
    width: 100%;
}

#diceCard {
    text-align: center;
    align-items: center;
}

.hint {
    font-size: .85rem;
    color: var(--muted);
}

/* ---------- Modals ---------- */
.overlay {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 20;
    background: rgba(0, 0, 0, .55);
}

:root[data-theme="light"] .overlay {
    background: rgba(0, 0, 0, .35);
}

.modal {
    width: min(560px, 92vw);
    background: var(--panel);
    color: var(--text);
    border-radius: 14px;
    padding: 18px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.modal h2 {
    margin: 0;
    font-size: 1.05rem;
    color: var(--heading);
}

.modal label {
    font-size: .9rem;
    color: var(--muted);
}

/* ---------- Landing (index) ---------- */
.landing {
    max-width: 900px;
    margin: 24px auto;
    display: grid;
    gap: 16px;
}

.cta {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
    justify-content: center;
}

.cta a {
    text-decoration: none;
}

/* Leaderboard card on landing */
.lb {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px;
    box-shadow: var(--shadow);
}

.lb h2 {
    margin: 0 0 8px;
    font-size: 1rem;
    color: var(--heading);
}

.lb table {
    width: 100%;
    border-collapse: collapse;
}

.lb th,
.lb td {
    padding: 6px 8px;
    border-bottom: 1px solid var(--border);
    text-align: left;
}

.lb tbody tr:nth-child(odd) {
    background: color-mix(in oklab, var(--panel) 96%, var(--text) 4%);
}

.note {
    color: var(--muted);
    font-size: .9rem;
}

/* ---------- Online lobby ---------- */
.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px;
    box-shadow: var(--shadow);
}

.row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: center;
    margin: 10px 0;
}

/* Lobby inputs */
.wrap input,
.wrap select {
    padding: 2px 12px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--btnBg);
    color: var(--text);
}

.wrap input::placeholder {
    color: color-mix(in oklab, var(--text) 45%, var(--bg) 55%);
    opacity: .9;
}

/* Back link styled like a button */
a.linkbtn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--btnBg);
    color: inherit;
    text-decoration: none;
    transition: background-color .12s ease, transform .02s ease;
}

a.linkbtn:hover {
    background: var(--btnBg2);
}

a.linkbtn:active {
    transform: translateY(1px);
}

/* ---------- Utilities ---------- */
@media (max-width: 980px) {
    .main {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        flex: 0 0 auto;
    }
}

/* Optional: nicer scrollbars (Chromium/Edge/WebKit) */
*::-webkit-scrollbar {
    height: 12px;
    width: 12px;
}

*::-webkit-scrollbar-track {
    background: transparent;
}

*::-webkit-scrollbar-thumb {
    background: color-mix(in oklab, var(--text) 20%, var(--bg) 80%);
    border-radius: 12px;
    border: 3px solid transparent;
    background-clip: content-box;
}

:root[data-theme="light"] *::-webkit-scrollbar-thumb {
    background: color-mix(in oklab, var(--text) 25%, var(--bg) 75%);
}

/* --- Mobile layout: wrap to two rows, keep actions pinned --- */
@media (max-width: 860px) {
    .topbar {
        position: relative;
        gap: 8px;
        display: flex;
        align-items: flex-start;
        flex-direction: column;

        .score {
            font-size: 14px;
        }

        .navLast {
            display: flex;
            justify-content: flex-start;
            gap: 8px;
        }
    }
}

/* --- Very small phones: compress spacing, allow icon-only buttons --- */
@media (max-width: 420px) {
    .wrap {
        gap: 6px;
    }

    .main {
        gap: 6px;
    }

    .topbar {
        position: relative;
        gap: 8px;
        display: flex;
        align-items: flex-start;
        flex-direction: column;

        .score {
            font-size: 14px;
        }

        .navLast {
            display: flex;
            justify-content: flex-start;
            gap: 8px;
        }
    }

    .board {
        gap: 0;
    }
}

/* --- Reduce Motion polish --- */
@media (prefers-reduced-motion: reduce) {

    .topbar,
    .bigbtn {
        transition: none;
    }
}


.board.is-your-turn {
    /* or .board-wrap if that’s your wrapper */
    box-shadow:
        0 0 0 2px var(--turn-color),
        0 0 10px 2px color-mix(in srgb, var(--turn-color) 30%, transparent) inset;
}

.pulse {
    animation: roll-pulse 1.2s ease-out infinite;
}

@keyframes roll-pulse {
    0% {
        box-shadow: 0 0 0 0 var(--turn-color)
    }

    70% {
        box-shadow: 0 0 0 12px color-mix(in srgb, var(--turn-color) 0%, transparent)
    }

    100% {
        box-shadow: 0 0 0 0 transparent
    }
}

.turn-badge.bump {
    animation: bump .35s ease;
}

@keyframes bump {
    0% {
        transform: scale(1)
    }

    60% {
        transform: scale(1.06)
    }

    100% {
        transform: scale(1)
    }
}

/* if your badge text is currently forced to white, let it inherit */
.turn-badge.use-player-color {
    color: var(--turn-color) !important;
}