/* ---------- theme ---------- */
/* An inline script in index.html sets data-theme on <html> before first paint:
   the player's saved choice, or the system setting. */
:root {
    color-scheme: light;
    --bg: #eef2f6;
    --surface: #ffffff;
    --surface-2: #f3f5f8;
    --border: #e5e7eb;
    --border-strong: #9ca3af;
    --text: #111827;
    --text-2: #4b5563;
    --muted: #6b7280;
    --faint: #9ca3af;
    --topbar: #0b1d33;
    --accent: #198754;
    --accent-hover: #157347;
    --accent-text: #146c43;
    --accent-bg: #e8f5ee;
    --danger-text: #b42318;
    --danger: #dc3545;
    --danger-border: #f1aeb5;
    --warn-text: #b7791f;
    --note-bg: #fff7e0;
    --note-text: #7a5a00;
    --btn-outline-bg: #ffffff;
    --btn-outline-text: #374151;
    --overlay: rgba(255, 255, 255, 0.96);
    --overlay-text: #0b1d33;
    --stage-shadow: 0 10px 30px rgba(11, 29, 51, 0.35);
    --dialog-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
    --backdrop: rgba(11, 29, 51, 0.6);
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #0a1220;
    --surface: #131d2e;
    --surface-2: #1b283d;
    --border: #26354d;
    --border-strong: #4a5d7a;
    --text: #e6edf5;
    --text-2: #b6c2d1;
    --muted: #8a9bb0;
    --faint: #66778d;
    --topbar: #060d18;
    --accent: #1f9d63;
    --accent-hover: #24b371;
    --accent-text: #5dd39e;
    --accent-bg: #123a2a;
    --danger-text: #ff8a80;
    --danger: #ff6b6b;
    --danger-border: #6b2a30;
    --warn-text: #e9b45a;
    --note-bg: #33290d;
    --note-text: #f2cf6b;
    --btn-outline-bg: transparent;
    --btn-outline-text: #d6dee8;
    --overlay: rgba(19, 29, 46, 0.96);
    --overlay-text: #ffffff;
    --stage-shadow: 0 10px 30px rgba(0, 0, 0, 0.55);
    --dialog-shadow: 0 20px 60px rgba(0, 0, 0, 0.7);
    --backdrop: rgba(0, 0, 0, 0.65);
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
    margin: 0;
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 15px;
    line-height: 1.45;
    color: var(--text);
    background: var(--bg);
    -webkit-tap-highlight-color: transparent;
}
h2 { margin-top: 0; }

/* ---------- top bar ---------- */
.gg-topbar {
    flex: none;
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 8px 16px;
    padding-top: max(8px, env(safe-area-inset-top));
    background: var(--topbar);
    color: #fff;
}
.gg-brand { font-weight: 800; font-size: 1.1rem; white-space: nowrap; }
.gg-holenav { margin-left: auto; display: flex; align-items: center; }
.gg-nav-btn {
    width: 30px; height: 34px;
    display: grid; place-items: center;
    border-radius: 8px;
    color: #fff;
    font-size: 1.6rem; line-height: 1;
    text-decoration: none;
}
.gg-nav-btn[href]:hover { background: rgba(255, 255, 255, 0.12); }
.gg-nav-btn:not([href]) { opacity: 0.25; cursor: default; }
.gg-hole-btn {
    padding: 4px 8px;
    border: 0; border-radius: 8px;
    background: transparent;
    color: #fff;
    font: inherit;
    white-space: nowrap;
    cursor: pointer;
}
.gg-hole-btn:hover { background: rgba(255, 255, 255, 0.12); }
.gg-score { display: flex; gap: 12px; }
.gg-score > span { display: flex; flex-direction: column; align-items: center; line-height: 1.05; }
.gg-score small { font-size: 0.62rem; text-transform: uppercase; letter-spacing: 0.06em; opacity: 0.65; }
.gg-score strong { font-size: 1.15rem; }
.gg-topbar-btns { display: flex; gap: 8px; }
.gg-icon-btn {
    width: 34px; height: 34px;
    display: grid; place-items: center;
    padding: 0;
    border-radius: 50%;
    border: 1.5px solid rgba(255, 255, 255, 0.5);
    background: transparent;
    color: #fff;
    font: inherit; font-weight: 700;
    cursor: pointer;
}
.gg-icon-btn:hover { background: rgba(255, 255, 255, 0.12); }
.gg-icon-btn svg { width: 18px; height: 18px; }

/* ---------- buttons ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 0.45rem 0.9rem;
    font: inherit;
    font-weight: 600;
    border: 1px solid transparent;
    border-radius: 10px;
    background: transparent;
    color: inherit;
    cursor: pointer;
    touch-action: manipulation;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.btn:disabled { opacity: 0.5; cursor: default; }
.btn:focus-visible, .gg-piece:focus-visible, .gg-float-btn:focus-visible, .gg-icon-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 0.1rem var(--surface), 0 0 0 0.25rem #258cfb;
}
.btn-lg { font-size: 1.08rem; }
.btn-success { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-success:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-warning { background: #ffc107; border-color: #ffc107; color: #111827; }
.btn-warning:hover:not(:disabled) { background: #ffca2c; }
.btn-outline-secondary { border-color: var(--border-strong); color: var(--btn-outline-text); background: var(--btn-outline-bg); }
.btn-outline-secondary:hover:not(:disabled) { background: #6b7280; border-color: #6b7280; color: #fff; }
.grow { flex-grow: 1; }

/* ---------- layout ---------- */
.gg-main {
    flex: 1;
    min-height: 0;
    display: flex;
    justify-content: center;
    gap: 24px;
    padding: 16px;
}

.gg-stage {
    position: relative;
    width: min(440px, 100%);
    height: 100%;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: var(--stage-shadow);
    background: #0b1d33;
}
#gg-canvas {
    display: block;
    width: 100%;
    height: 100%;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.gg-toast {
    position: absolute;
    top: 22%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 10px 20px;
    border-radius: 999px;
    background: var(--overlay);
    color: var(--overlay-text);
    font-weight: 800;
    font-size: 1.35rem;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3);
    white-space: nowrap;
    pointer-events: none;
    animation: gg-pop 0.35s cubic-bezier(.2, 1.6, .4, 1);
    transition: opacity 0.4s;
}
.gg-toast-out { opacity: 0; }
@keyframes gg-pop { from { transform: translate(-50%, -50%) scale(0.6); opacity: 0; } }

/* floating controls for the selected piece */
.gg-float {
    position: absolute;
    left: 50%;
    bottom: 14px;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 6px 6px 14px;
    border-radius: 999px;
    background: var(--overlay);
    color: var(--overlay-text);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
    white-space: nowrap;
}
#gg-selected-name { font-weight: 700; margin-right: 4px; font-variant-numeric: tabular-nums; }
.gg-float-btn {
    width: 44px; height: 44px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 1.25rem;
    line-height: 1;
    cursor: pointer;
    touch-action: manipulation;
}
.gg-float-btn:disabled { opacity: 0.35; }
.gg-float-btn.danger { color: var(--danger); border-color: var(--danger-border); }

/* ---------- side panel ---------- */
.gg-panel {
    width: 320px;
    flex: none;
    overflow-y: auto;
    overflow-x: hidden;
}

.gg-h2 { font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-bottom: 8px; }

/* two columns on desktop: every piece type is available, so keep the tray compact */
.gg-tray { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 7px 7px 0 0; }   /* room for the count badges */
.gg-piece {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    min-width: 0;
    position: relative;
    padding: 4px 8px 4px 4px;
    border: 2px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    font: inherit;
    color: inherit;
    text-align: left;
    cursor: pointer;
    touch-action: manipulation;
    transition: border-color 0.15s, transform 0.1s;
}
.gg-piece:hover:not(:disabled) { border-color: var(--border-strong); }
.gg-piece:active:not(:disabled) { transform: scale(0.97); }
.gg-piece:disabled { opacity: 0.4; cursor: default; }
.gg-piece-icon {
    flex: none;
    width: 46px; height: 34px;
    display: grid; place-items: center;
    border-radius: 8px;
    background: #0b1d33;
}
.gg-piece-icon svg { width: 38px; height: 24px; }
.gg-piece-text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; font-size: 0.9rem; }
.gg-piece-text strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gg-piece-text small { display: none; }   /* shown as the button's tooltip instead */
.gg-piece-count {
    position: absolute;
    top: -7px; right: -7px;
    min-width: 22px; height: 22px;
    padding: 0 4px;
    display: grid; place-items: center;
    border-radius: 11px;
    border: 2px solid var(--bg);
    background: var(--accent-bg);
    color: var(--accent-text);
    font-weight: 800;
    font-size: 0.78rem;
}
.gg-piece:disabled .gg-piece-count { background: var(--surface-2); color: var(--muted); }
.gg-piece-count.unlimited { font-size: 1.05rem; line-height: 1; }

.gg-hint { margin: 10px 0; color: var(--muted); font-size: 0.88rem; }
.gg-actions { display: flex; gap: 8px; }
.gg-message { margin-top: 8px; min-height: 1.4em; font-weight: 600; color: var(--danger-text); font-size: 0.92rem; }

.gg-result { text-align: center; }
.gg-result-name { font-size: 2.1rem; font-weight: 800; color: var(--accent-text); }
.gg-result-detail { color: var(--text-2); }
.gg-result-log { font-size: 1.3rem; letter-spacing: 0.1em; margin: 6px 0 14px; }
.gg-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 16px 0 10px; }
.gg-stats > div { display: flex; flex-direction: column; }
.gg-stats strong { font-size: 1.35rem; }
.gg-stats span { font-size: 0.72rem; color: var(--muted); }
.gg-next { color: var(--muted); font-variant-numeric: tabular-nums; }
.gg-result-first { margin: -6px 0 12px; font-weight: 600; color: var(--text-2); }
.gg-today-link { display: inline-block; margin-top: 4px; font-weight: 700; color: var(--accent-text); }

.gg-note {
    margin-bottom: 10px;
    padding: 8px 10px;
    border-radius: 10px;
    background: var(--note-bg);
    color: var(--note-text);
    font-size: 0.85rem;
    font-weight: 600;
}

/* ---------- archive ---------- */
.gg-archive-list {
    list-style: none;
    margin: 10px 0 14px;
    padding: 0;
    max-height: min(420px, 60dvh);
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: 12px;
}
.gg-archive-list li + li { border-top: 1px solid var(--border); }
.gg-archive-list a {
    display: grid;
    grid-template-columns: auto 1fr auto auto;
    gap: 10px;
    align-items: center;
    padding: 10px 12px;
    color: inherit;
    text-decoration: none;
}
.gg-archive-list a:hover { background: var(--surface-2); }
.gg-archive-list a[aria-current] { background: var(--accent-bg); }
.gg-archive-list span { color: var(--muted); font-size: 0.88rem; }
.gg-archive-list em { font-style: normal; font-size: 0.85rem; color: var(--faint); white-space: nowrap; }
.gg-archive-list em.done { color: var(--accent-text); font-weight: 700; }
.gg-archive-list em.started { color: var(--warn-text); font-weight: 600; }
.gg-star { font-style: normal; font-size: 1.05rem; line-height: 1; color: var(--border); }
.gg-star.got { color: #f4b400; text-shadow: 0 0 6px rgba(255, 209, 102, 0.6); }
.gg-archive-stars { font-weight: 700; color: #c98a00; }
:root[data-theme="dark"] .gg-archive-stars { color: #ffd166; }

.gg-result-star { margin: -4px 0 12px; font-size: 0.92rem; color: var(--muted); }
.gg-result-star.got { color: #c98a00; font-weight: 700; }
:root[data-theme="dark"] .gg-result-star.got { color: #ffd166; }

/* ---------- ball collection ---------- */
/* top-bar button showing the current ball; opens the collection */
.gg-ball-btn { position: relative; flex: none; }
.gg-ball-btn canvas { pointer-events: none; }
.gg-ball-btn.has-new::after {                 /* "new ball unlocked" dot */
    content: '';
    position: absolute; top: -3px; right: -3px;
    width: 11px; height: 11px;
    border-radius: 50%;
    background: #ffd166;
    border: 2px solid var(--topbar);
}

.gg-balls-stars { color: #c98a00; white-space: nowrap; }
:root[data-theme="dark"] .gg-balls-stars { color: #ffd166; }
.gg-balls-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
    gap: 8px;
    margin: 12px 0 16px;
    max-height: min(440px, 58dvh);
    overflow-y: auto;
    padding: 2px;
}
.gg-ball-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 8px 4px 6px;
    border: 2px solid var(--border);
    border-radius: 12px;
    background: var(--surface-2);
    color: inherit;
    font: inherit;
    cursor: pointer;
    touch-action: manipulation;
}
.gg-ball-item canvas { margin-bottom: 2px; }
.gg-ball-item strong { font-size: 0.82rem; line-height: 1.2; text-align: center; }
.gg-ball-item small { font-size: 0.72rem; color: var(--muted); }
.gg-ball-item:hover:not(:disabled) { border-color: var(--border-strong); }
.gg-ball-item.selected { border-color: var(--accent); background: var(--accent-bg); }
.gg-ball-item.selected small { color: var(--accent-text); font-weight: 700; }
.gg-ball-item.locked { cursor: default; }
.gg-ball-item.locked canvas { filter: grayscale(1) brightness(0.55); opacity: 0.6; }
.gg-ball-item.locked strong { color: var(--muted); }
.gg-ball-item.locked small { color: #c98a00; font-weight: 700; }
:root[data-theme="dark"] .gg-ball-item.locked small { color: #ffd166; }

/* collection tabs */
.gg-tabs {
    display: flex;
    gap: 4px;
    padding: 4px;
    margin-bottom: 10px;
    border-radius: 12px;
    background: var(--surface-2);
}
.gg-tab {
    position: relative;
    flex: 1;
    min-height: 38px;
    border: 0;
    border-radius: 9px;
    background: transparent;
    color: var(--muted);
    font: inherit;
    font-weight: 700;
    cursor: pointer;
}
.gg-tab[aria-selected="true"] { background: var(--accent); color: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15); }
.gg-tab:focus-visible { outline: none; box-shadow: 0 0 0 0.1rem var(--surface), 0 0 0 0.25rem #258cfb; }
.gg-tab.has-new::after {
    content: '';
    position: absolute; top: 7px; right: 10px;
    width: 8px; height: 8px;
    border-radius: 50%;
    background: #ffd166;
}

/* ---------- achievements ---------- */
.gg-ach-count { color: #c98a00; }
:root[data-theme="dark"] .gg-ach-count { color: #ffd166; }
.gg-ach-list {
    list-style: none;
    margin: 8px 0 16px;
    padding: 0;
    max-height: min(420px, 54dvh);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.gg-ach {
    display: grid;
    grid-template-columns: 40px 1fr auto;
    gap: 10px;
    align-items: center;
    padding: 8px 10px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--surface-2);
}
.gg-ach-icon {
    width: 40px; height: 40px;
    display: grid; place-items: center;
    border-radius: 50%;
    background: var(--surface);
    font-size: 1.3rem;
    filter: grayscale(1);
    opacity: 0.45;
}
.gg-ach.got .gg-ach-icon { filter: none; opacity: 1; background: #fff3cd; }
:root[data-theme="dark"] .gg-ach.got .gg-ach-icon { background: #3a2f10; }
.gg-ach-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.gg-ach-text strong { font-size: 0.92rem; }
.gg-ach-text small { font-size: 0.78rem; color: var(--muted); }
.gg-ach:not(.got) .gg-ach-text strong { color: var(--text-2); }
.gg-ach-bar { display: block; height: 5px; margin: 5px 0 2px; border-radius: 3px; background: var(--border); overflow: hidden; }
.gg-ach-bar i { display: block; height: 100%; background: #f4b400; border-radius: 3px; }
.gg-ach em { font-style: normal; font-size: 0.75rem; font-weight: 700; color: var(--accent-text); white-space: nowrap; }
.gg-ach em.locked { font-size: 0.9rem; opacity: 0.5; }

/* ---------- replay viewer ---------- */
.gg-watch { text-align: center; }
.gg-watch-kicker {
    display: inline-block;
    margin-bottom: 6px;
    padding: 3px 10px;
    border-radius: 999px;
    background: var(--accent-bg);
    color: var(--accent-text);
    font-size: 0.78rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}
.gg-watch-title { font-size: 1.6rem; font-weight: 800; line-height: 1.2; }
.gg-watch-detail { color: var(--text-2); margin: 4px 0 14px; }
a.btn { text-decoration: none; }

/* ---------- settings ---------- */
.gg-settings { margin: 4px 0 16px; padding-top: 12px; border-top: 1px solid var(--border); }
.gg-settings h3 { margin: 0 0 8px; font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.gg-switch { display: inline-flex; align-items: center; gap: 10px; margin-right: 22px; font-weight: 600; cursor: pointer; min-height: 36px; }
.gg-switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.gg-switch-ui {
    position: relative;
    width: 42px; height: 24px;
    border-radius: 12px;
    background: var(--border-strong);
    transition: background 0.15s;
}
.gg-switch-ui::after {
    content: '';
    position: absolute; top: 3px; left: 3px;
    width: 18px; height: 18px;
    border-radius: 50%;
    background: #fff;
    transition: transform 0.15s;
}
.gg-switch input:checked + .gg-switch-ui { background: var(--accent); }
.gg-switch input:checked + .gg-switch-ui::after { transform: translateX(18px); }
.gg-switch input:focus-visible + .gg-switch-ui { box-shadow: 0 0 0 0.1rem var(--surface), 0 0 0 0.25rem #258cfb; }
.gg-switch input:disabled + .gg-switch-ui { opacity: 0.4; }
.gg-switch:has(input:disabled) { cursor: default; color: var(--muted); }

/* ---------- dialogs ---------- */
.gg-help {
    width: min(440px, calc(100vw - 32px));
    border: none;
    border-radius: 16px;
    padding: 22px 22px 18px;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--dialog-shadow);
}
.gg-help::backdrop { background: var(--backdrop); }
.gg-help h2 { font-size: 1.4rem; margin-bottom: 8px; }
.gg-help ul { padding-left: 1.1rem; margin: 10px 0 16px; }
.gg-help li { margin-bottom: 8px; }
.gg-help .btn { width: 100%; }

/* ---------- phones ---------- */
@media (max-width: 760px) {
    .gg-topbar { gap: 10px; padding-left: 12px; padding-right: 12px; }
    .gg-brand { font-size: 1rem; }
    .gg-main { flex-direction: column; padding: 0; gap: 0; }
    .gg-stage { width: 100%; flex: 1; min-height: 0; height: auto; border-radius: 0; box-shadow: none; }
    .gg-panel {
        width: 100%;
        max-height: 46dvh;
        padding: 10px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom));
        background: var(--surface);
        border-top: 1px solid var(--border);
    }
    .gg-h2 { display: none; }
    .gg-tray {
        display: flex;
        flex-direction: row;
        overflow-x: auto;
        gap: 8px;
        padding: 8px 24px 2px 2px;   /* room for the count badges */
        scrollbar-width: none;
        /* fade at the right edge hints that the tray scrolls */
        -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
        mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
    }
    .gg-tray::-webkit-scrollbar { display: none; }
    .gg-piece {
        flex: none;
        width: auto;
        position: relative;
        flex-direction: column;
        gap: 2px;
        padding: 6px 8px 4px;
    }
    .gg-piece-text small { display: none; }
    .gg-piece-text { font-size: 0.78rem; align-items: center; }
    .gg-piece-count { position: absolute; top: -6px; right: -6px; min-width: 22px; height: 22px; font-size: 0.78rem; border: 2px solid var(--surface); }
    .gg-hint { margin: 6px 0 8px; font-size: 0.8rem; }
    .gg-message { min-height: 0; margin-top: 6px; }
    .gg-message:empty { display: none; }
    .gg-result-name { font-size: 1.6rem; }
    .gg-stats { margin: 10px 0 6px; }
}

@media (max-width: 430px) {
    .gg-topbar { gap: 6px; }
    .gg-brand { display: none; }
    .gg-holenav { margin-left: -6px; }
    .gg-score { margin-left: auto; gap: 8px; }
    .gg-nav-btn { width: 26px; }
    .gg-topbar-btns { gap: 6px; }
    .gg-icon-btn { width: 32px; height: 32px; }
    .gg-result .btn { padding-left: 0.6rem; padding-right: 0.6rem; }
}

@media (max-width: 370px) {
    .gg-hole-word { display: none; }
    .gg-nav-btn { width: 22px; }
}
