/* ============================================================
   Color Match — RGB color matching game
   Responsive · light/dark · touch-friendly · no-crop layout
   ============================================================ */

:root {
    color-scheme: light dark;
    --bg: #f4f6fb;
    --bg-glow-1: rgba(99, 102, 241, .10);
    --bg-glow-2: rgba(236, 72, 153, .07);
    --card: #ffffff;
    --ink: #1c2333;
    --muted: #6b7280;
    --line: #e5e8f0;
    --chip: #eef0f7;
    --chip-hover: #e2e6f0;
    --pat: rgba(28, 35, 51, .07);
    --accent: #6366f1;
    --accent-2: #8b5cf6;
    --bad: #ef4444;
    --cover-a: #e9ecf5;
    --cover-b: #f7f8fc;
    --cover-ink: #b9c0d4;
    --streak-bg: #fff4e5;
    --streak-border: #ffd9a8;
    --streak-ink: #b45309;
    --topbar-bg: rgba(255, 255, 255, .85);
    --radius: 20px;
    --shadow: 0 10px 30px rgba(28, 35, 51, .08);
    --header-h: 60px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #11131c;
        --bg-glow-1: rgba(99, 102, 241, .16);
        --bg-glow-2: rgba(236, 72, 153, .10);
        --card: #1a1e2c;
        --ink: #e8eaf4;
        --muted: #9aa3bc;
        --line: #2a3046;
        --chip: #242a3d;
        --chip-hover: #2c334a;
        --pat: rgba(232, 234, 244, .06);
        --cover-a: #232941;
        --cover-b: #1c2233;
        --cover-ink: #4a5474;
        --streak-bg: #2b2113;
        --streak-border: #4a3a1c;
        --streak-ink: #fbbf24;
        --topbar-bg: rgba(17, 19, 28, .82);
        --shadow: 0 10px 30px rgba(0, 0, 0, .35);
    }
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html {
    height: 100%;
    background:
        radial-gradient(1100px 520px at 12% -10%, var(--bg-glow-1), transparent 60%),
        radial-gradient(900px 520px at 108% 12%, var(--bg-glow-2), transparent 55%),
        var(--bg);
    background-attachment: fixed;
}

body {
    font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
    background: transparent;
    color: var(--ink);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
}

/* ---------- background patterns (tile over the base gradient) ---------- */
body.bg-polka {
    background-image: radial-gradient(var(--pat) 2px, transparent 2px);
    background-size: 22px 22px;
}
body.bg-graph {
    background-image:
        linear-gradient(var(--pat) 1px, transparent 1px),
        linear-gradient(90deg, var(--pat) 1px, transparent 1px);
    background-size: 24px 24px;
}
body.bg-stripes {
    background-image: repeating-linear-gradient(45deg,
        var(--pat) 0 12px, transparent 12px 26px);
}
body.bg-lines {
    background-image: repeating-linear-gradient(0deg,
        var(--pat) 0 1px, transparent 1px 26px);
}

.hidden { display: none !important; }

button {
    font-family: inherit;
    touch-action: manipulation;
    color: inherit;
}

:where(button, input, a, [tabindex]):focus-visible {
    outline: 3px solid rgba(99, 102, 241, .55);
    outline-offset: 2px;
}

/* ============================================================
   Top bar (sticky)
   ============================================================ */
.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: var(--header-h);
    padding: 8px max(18px, env(safe-area-inset-right)) 8px max(18px, env(safe-area-inset-left));
    background: var(--topbar-bg);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--line);
    user-select: none;
    -webkit-user-select: none;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.brand-mark {
    display: grid;
    grid-template-columns: repeat(2, 11px);
    gap: 3px;
    flex: none;
}
.brand-mark i { width: 11px; height: 11px; border-radius: 4px; display: block; }

.brand-name {
    font-weight: 800;
    font-size: 1.1rem;
    letter-spacing: .2px;
    white-space: nowrap;
}

.topbar-right { display: flex; align-items: center; gap: 8px; flex: none; }

.streak-pill {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--streak-bg);
    border: 1px solid var(--streak-border);
    color: var(--streak-ink);
    font-weight: 800;
    padding: 6px 12px;
    border-radius: 999px;
    font-size: .9rem;
    white-space: nowrap;
}

.icon-btn {
    width: 40px;
    height: 40px;
    flex: none;
    border-radius: 12px;
    border: 1px solid var(--line);
    background: var(--card);
    cursor: pointer;
    display: grid;
    place-items: center;
    transition: background .15s, transform .1s;
}
.icon-btn:hover { background: var(--chip); }
.icon-btn:active { transform: scale(.94); }

.ic {
    width: 19px; height: 19px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* ============================================================
   Mode tabs (sticky under the header)
   ============================================================ */
.modes {
    position: sticky;
    top: calc(var(--header-h) + 8px);
    z-index: 15;
    display: flex;
    gap: 4px;
    margin: 18px auto 0;
    padding: 5px;
    background: var(--chip);
    border: 1px solid var(--line);
    border-radius: 14px;
    width: fit-content;
    max-width: calc(100% - 20px);
    overflow-x: auto;
    scrollbar-width: none;
    box-shadow: 0 4px 14px rgba(28, 35, 51, .07);
    user-select: none;
    -webkit-user-select: none;
}
.modes::-webkit-scrollbar { display: none; }

.mode-tab {
    border: 0;
    background: transparent;
    padding: 9px 17px;
    border-radius: 10px;
    font-weight: 700;
    font-size: .92rem;
    color: var(--muted);
    cursor: pointer;
    white-space: nowrap;
    flex: none;
    transition: background .15s, color .15s;
}
.mode-tab:hover { color: var(--ink); }
.mode-tab.active {
    background: var(--card);
    color: var(--ink);
    box-shadow: 0 2px 8px rgba(28, 35, 51, .12);
}

/* ============================================================
   Game area
   ============================================================ */
.game-wrap {
    flex: 1;
    width: min(600px, 100% - 24px);
    margin: 0 auto;
    padding: 20px 0 10px;
    text-align: center;
}

.mode-heading {
    font-size: clamp(1.25rem, 4.5vw, 1.55rem);
    font-weight: 800;
    margin-bottom: 6px;
    overflow-wrap: break-word;
}
.mode-sub {
    color: var(--muted);
    font-size: .95rem;
    margin-bottom: 18px;
    overflow-wrap: break-word;
}

/* ---------- attack hud ---------- */
.attack-hud {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-bottom: 14px;
}
.hud-stat {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 8px 12px;
    flex: 1;
    max-width: 130px;
    min-width: 0;
}
.hud-stat small {
    display: block;
    text-transform: uppercase;
    letter-spacing: .08em;
    font-size: .68rem;
    color: var(--muted);
    font-weight: 700;
}
.hud-stat b { font-size: 1.25rem; }
.hud-stat b.low { color: var(--bad); }

/* ---------- card ---------- */
.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: clamp(16px, 4vw, 26px);
}

/* ---------- swatches ---------- */
.swatches, .result-compare {
    display: flex;
    justify-content: center;
    gap: clamp(12px, 4vw, 24px);
    margin-bottom: 20px;
}

.swatch-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.swatch {
    width: clamp(104px, 37vw, 150px);
    max-width: 100%;
    aspect-ratio: 1 / 1;
    border-radius: 18px;
    border: 1px solid var(--line);
    background: rgb(128, 128, 128);
    position: relative;
    overflow: hidden;
    transition: background .12s;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .06);
    user-select: none;
    -webkit-user-select: none;
}
.swatch-sm { width: clamp(88px, 28vw, 120px); }

.swatch-label {
    font-size: .82rem;
    font-weight: 700;
    color: var(--muted);
}
.swatch-label code {
    font-family: Consolas, Menlo, monospace;
    background: var(--chip);
    padding: 1px 6px;
    border-radius: 6px;
    color: var(--ink);
}

.swatch-cover {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    background: var(--cover-a);
    background-image: repeating-conic-gradient(var(--cover-a) 0% 25%, var(--cover-b) 0% 50%);
    background-size: 24px 24px;
}
.swatch-cover span {
    font-size: 3rem;
    font-weight: 800;
    color: var(--cover-ink);
}

.memorize-count {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    font-size: 3.4rem;
    font-weight: 800;
    color: #fff;
    text-shadow: 0 2px 12px rgba(0, 0, 0, .45);
    background: rgba(0, 0, 0, .12);
}

.memorize-bar {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 7px;
    width: 100%;
    background: rgba(255, 255, 255, .9);
    box-shadow: 0 0 6px rgba(0, 0, 0, .25);
    transform: scaleX(0);
    transform-origin: left center;
    z-index: 1;
}
@keyframes membar {
    from { transform: scaleX(1); }
    to   { transform: scaleX(0); }
}

.swatch-values {
    position: absolute;
    left: 0; right: 0; bottom: 0;
    padding: 5px 0;
    font-family: Consolas, Menlo, monospace;
    font-size: .78rem;
    font-weight: 700;
    color: #fff;
    background: rgba(0, 0, 0, .45);
    z-index: 2;
}

/* ---------- sliders ---------- */
.controls { margin: 0 auto 18px; max-width: 420px; }

.s-row {
    display: grid;
    grid-template-columns: 26px 1fr 52px;
    align-items: center;
    gap: 12px;
    margin-bottom: 14px;
}

.s-label { font-weight: 800; font-size: 1.05rem; text-align: center; }
.s-label-sm { font-size: .95rem; color: var(--muted); }

.s-val {
    font-family: Consolas, Menlo, monospace;
    font-weight: 700;
    font-size: .9rem;
    background: var(--chip);
    border-radius: 8px;
    padding: 4px 0;
    text-align: center;
}

input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    min-width: 0;
    height: 12px;
    border-radius: 999px;
    background: linear-gradient(to right, #000, #fff);
    outline-offset: 4px;
    border: 1px solid rgba(28, 35, 51, .12);
    cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #fff;
    border: 2px solid rgba(28, 35, 51, .25);
    box-shadow: 0 2px 6px rgba(28, 35, 51, .25);
    cursor: grab;
}
input[type="range"]::-moz-range-thumb {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #fff;
    border: 2px solid rgba(28, 35, 51, .25);
    box-shadow: 0 2px 6px rgba(28, 35, 51, .25);
    cursor: grab;
}
input[type="range"]::-moz-range-track { background: transparent; border: none; }
input[type="range"]:disabled { opacity: .45; cursor: not-allowed; }

/* bigger touch targets on touch devices */
@media (pointer: coarse) {
    input[type="range"] { height: 20px; }
    input[type="range"]::-webkit-slider-thumb { width: 30px; height: 30px; }
    input[type="range"]::-moz-range-thumb { width: 28px; height: 28px; }
    .s-row { margin-bottom: 16px; }
    .icon-btn { width: 44px; height: 44px; }
    .modal-x { width: 44px; height: 44px; }
}

/* ---------- number inputs (Impossible) ---------- */
.num-controls {
    display: flex;
    justify-content: center;
    gap: clamp(8px, 3vw, 16px);
}
.n-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    min-width: 0;
}
.n-box label { font-weight: 800; }
.n-box input {
    width: 100%;
    max-width: 92px;
    min-width: 64px;
    padding: 12px 6px;
    text-align: center;
    font-family: Consolas, Menlo, monospace;
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--ink);
    background: var(--card);
    border: 1.5px solid var(--line);
    border-radius: 12px;
    outline: none;
}
.n-box input:focus { border-color: var(--accent); }
.n-box input:focus-visible {
    outline: 3px solid rgba(99, 102, 241, .55);
    outline-offset: 2px;
}
.n-box input:disabled { opacity: .45; background: var(--chip); }

/* ---------- buttons ---------- */
.actions {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
}

.btn {
    border: 0;
    border-radius: 13px;
    padding: 13px 24px;
    min-height: 48px;
    font-size: 1rem;
    font-weight: 800;
    cursor: pointer;
    transition: transform .1s, box-shadow .15s, background .15s;
    user-select: none;
    -webkit-user-select: none;
}
.btn:active { transform: scale(.96); }

.btn-primary {
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    color: #fff;
    box-shadow: 0 6px 18px rgba(99, 102, 241, .35);
}
.btn-primary:hover { box-shadow: 0 8px 22px rgba(99, 102, 241, .45); }

.btn-ghost {
    background: transparent;
    border: 1.5px solid var(--line);
    color: var(--ink);
}
.btn-ghost:hover { background: var(--chip); }

.guess-timer {
    margin-top: 14px;
    font-family: Consolas, Menlo, monospace;
    font-weight: 700;
    color: var(--muted);
}

/* ---------- desktop split layout: swatches left, controls right ---------- */
@media (min-width: 1024px) {
    .game-wrap { width: min(880px, 100% - 32px); }

    #play-view {
        display: flex;
        align-items: center;
        gap: 30px;
    }
    #play-view .swatches {
        flex: none;
        flex-direction: column;
        gap: 16px;
        margin-bottom: 0;
    }
    #play-view .swatch { width: 175px; }
    #play-view .panel-right {
        flex: 1;
        min-width: 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 10px;
    }
    #play-view .controls { margin: 0; max-width: none; }
    #play-view .guess-timer { margin-top: 4px; }
}

/* ============================================================
   Result view
   ============================================================ */
#result-view {
    animation: rise .28s cubic-bezier(.2, .9, .3, 1.2);
}
@keyframes rise {
    from { opacity: 0; transform: translateY(14px) scale(.97); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

.result-acc {
    font-size: clamp(2.6rem, 9vw, 3.6rem);
    font-weight: 900;
    line-height: 1.1;
    color: var(--accent);
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.result-msg { font-size: 1.15rem; font-weight: 700; margin: 6px 0 4px; }

.result-extra { color: var(--muted); font-size: .92rem; min-height: 1.3em; }

.next-daily {
    margin: 16px auto 0;
    width: fit-content;
    max-width: 100%;
    background: var(--chip);
    border-radius: 12px;
    padding: 8px 18px;
    font-size: .92rem;
    color: var(--muted);
}
.next-daily b { font-family: Consolas, Menlo, monospace; color: var(--ink); }

.modal-btns {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
    margin-top: 20px;
}

/* ============================================================
   Overlays / modals
   ============================================================ */
.overlay {
    position: fixed;
    inset: 0;
    z-index: 50;
    background: rgba(10, 13, 22, .5);
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
    display: grid;
    place-items: center;
    padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
             max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.modal {
    position: relative;
    width: min(480px, 100%);
    max-height: 84vh;
    max-height: 84dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: 0 24px 60px rgba(0, 0, 0, .3);
    padding: 28px 24px 24px;
    text-align: left;
}

.modal-x {
    position: absolute;
    top: 14px;
    right: 14px;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    border: 0;
    background: var(--chip);
    cursor: pointer;
    display: grid;
    place-items: center;
}
.modal-x:hover { background: var(--chip-hover); }

.modal-title { font-size: 1.3rem; font-weight: 800; margin-bottom: 14px; padding-right: 40px; }

.help-list p { margin-bottom: 12px; font-size: .93rem; line-height: 1.55; }
.help-note {
    background: var(--chip);
    border-radius: 10px;
    padding: 10px 12px;
    color: var(--muted);
}

.settings-h {
    font-size: .8rem;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--muted);
    margin: 18px 0 8px;
}

.bg-grid { display: flex; gap: 8px; flex-wrap: wrap; }
.bg-opt {
    border: 1.5px solid var(--line);
    background: var(--card);
    border-radius: 10px;
    padding: 9px 14px;
    font-weight: 700;
    font-size: .85rem;
    cursor: pointer;
}
.bg-opt:hover { background: var(--chip); }
.bg-opt.active {
    border-color: var(--accent);
    color: var(--accent);
    background: rgba(99, 102, 241, .12);
}

.toggle-row {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    font-size: .9rem;
    line-height: 1.45;
    cursor: pointer;
}
.toggle-row input {
    margin-top: 3px;
    width: 18px;
    height: 18px;
    flex: none;
    accent-color: var(--accent);
}

/* ============================================================
   Guide
   ============================================================ */
.guide {
    margin-top: 30px;
    text-align: left;
}

.guide-title {
    text-align: center;
    font-size: 1.2rem;
    font-weight: 800;
    margin-bottom: 14px;
}

.guide-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 12px;
}
@media (min-width: 1024px) {
    /* always a symmetric 2x2 on desktop */
    .guide-grid { grid-template-columns: repeat(2, 1fr); }
}

.guide-tile {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 14px 16px;
    box-shadow: 0 4px 14px rgba(28, 35, 51, .05);
}
.guide-tile h3 {
    font-size: .95rem;
    font-weight: 800;
    margin-bottom: 6px;
}
.guide-tile p {
    font-size: .85rem;
    line-height: 1.55;
    color: var(--muted);
}
.guide-tile b { color: var(--ink); }

.guide-note {
    margin-top: 12px;
    background: var(--chip);
    border-radius: 14px;
    padding: 12px 16px;
    font-size: .85rem;
    line-height: 1.55;
    color: var(--muted);
}
.guide-note b { color: var(--ink); }

/* ============================================================
   Ads
   ============================================================ */
/* Slots are invisible until an ad actually renders (.ad-pending removed
   by the loader), so the game never shows empty ad boxes. */
.ad-slot {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
}
.ad-slot.ad-pending { display: none; }

.ad-frame {
    display: flex;
    align-items: center;
    justify-content: center;
    max-width: 100%;
    overflow: hidden;
    border-radius: 10px;
}
.ad-frame iframe { display: block; max-width: 100%; border: 0; }

/* below-game placements: banner then box, centered, after the play area */
.ad-banner-bottom { margin: 20px auto 4px; max-width: 100%; }
.ad-box { margin: 14px auto 6px; max-width: 100%; }

/* fixed side rails — gutters clear the 728px banner even mid-scroll */
.ad-rail {
    display: none;
    position: fixed;
    top: 130px;
    z-index: 10;
}
@media (min-width: 1320px) {
    .ad-rail-right:not(.ad-pending) { display: flex; left: calc(50% + 448px); }
}
@media (min-width: 1520px) {
    .ad-rail-left:not(.ad-pending) { display: flex; left: calc(50% - 748px); }
}

/* ============================================================
   Footer / toast
   ============================================================ */
.footer {
    text-align: center;
    padding: 8px max(16px, env(safe-area-inset-right))
             calc(12px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    color: var(--muted);
    font-size: .85rem;
}
.footer a {
    color: var(--accent);
    font-weight: 700;
    text-decoration: none;
    margin-left: 8px;
    white-space: nowrap;
}
.footer a:hover { text-decoration: underline; }

.toast {
    position: fixed;
    bottom: calc(24px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    max-width: calc(100vw - 32px);
    background: var(--ink);
    color: var(--card);
    font-weight: 700;
    font-size: .92rem;
    padding: 11px 22px;
    border-radius: 999px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, .35);
    z-index: 100;
    animation: toast-pop .18s ease-out;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
@keyframes toast-pop {
    from { opacity: 0; transform: translateX(-50%) translateY(8px); }
    to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ============================================================
   Small-screen tuning
   ============================================================ */
@media (max-width: 400px) {
    .mode-tab { padding: 8px 11px; font-size: .84rem; }
    .btn { padding: 12px 18px; font-size: .95rem; }
    .streak-pill { padding: 5px 10px; font-size: .85rem; }
    .brand-name { display: none; }
    .hud-stat b { font-size: 1.05rem; }
    .swatch-cover span { font-size: 2.4rem; }
    .memorize-count { font-size: 2.8rem; }
}

/* ============================================================
   Reduced motion
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
    }
}

/* The topbar and mode tabs are both sticky (~114px combined). Without scroll
   padding, anchor jumps and keyboard scrolling park headings UNDER them, so the
   first line of a guide card reads as cut off. */
html { scroll-padding-top: 124px; }
.guide-tile h3, .guide-title, .about h2, .about h3 { scroll-margin-top: 124px; }
