:root {
    --bg-a: #e9edf1;
    --bg-b: #cad7e2;
    --ink: #1f2937;
    --card: rgba(255, 255, 255, 0.82);
    --line: #9fb2c2;
    --accent: #2f7df6;
    --accent-2: #1ec8ab;
    --glow: rgba(47, 125, 246, 0.24);
    --font-main: "Nunito", sans-serif;
    --font-title: "Fredoka", sans-serif;
    --success: #2fbf71;
    --danger: #d55656;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-height: 100%;
    overflow: hidden;
    font-family: var(--font-main);
    color: var(--ink);
    background: radial-gradient(circle at 20% 20%, rgba(255, 255, 255, 0.7), transparent 40%),
        linear-gradient(140deg, var(--bg-a), var(--bg-b));
}

#app {
    position: relative;
    width: 100vw;
    height: 100vh;
}

#attract-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0.25;
    filter: saturate(1.1) blur(0.4px);
}

.screen {
    position: absolute;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
}

.screen.active {
    display: flex;
}

#menu-screen {
    animation: reveal 0.7s ease;
    padding: 20px;
}

.menu-glass {
    width: min(580px, 100%);
    padding: 28px;
    border-radius: 24px;
    background: var(--card);
    border: 1px solid rgba(255, 255, 255, 0.7);
    box-shadow: 0 24px 48px rgba(25, 44, 71, 0.18);
    backdrop-filter: blur(14px);
}

.title {
    margin: 0;
    font-family: var(--font-title);
    font-size: clamp(2rem, 6vw, 3.1rem);
    letter-spacing: 0.04em;
}

.title-logo {
    margin-bottom: 8px;
    font-size: clamp(1.5rem, 4vw, 2.1rem);
    letter-spacing: 0.22em;
    text-align: center;
    filter: drop-shadow(0 10px 14px rgba(18, 35, 53, 0.16));
}

.subtitle {
    margin: 10px 0 18px;
    opacity: 0.8;
}

.menu-stack {
    display: none;
    gap: 10px;
}

.menu-stack.active {
    display: grid;
}

#main-menu {
    grid-template-columns: 1fr 1fr;
}

#main-menu .btn.primary {
    grid-column: 1 / -1;
}

.menu-label {
    margin: 6px 0 2px;
    font-weight: 700;
}

.mode-select,
.difficulty-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.btn {
    border: 0;
    border-radius: 14px;
    padding: 12px 14px;
    font-size: 1rem;
    font-family: var(--font-main);
    font-weight: 800;
    color: var(--ink);
    background: rgba(255, 255, 255, 0.86);
    cursor: pointer;
    box-shadow: 0 8px 20px rgba(18, 35, 53, 0.12);
    transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.btn:hover,
.btn:focus-visible {
    transform: translateY(-1px);
    box-shadow: 0 12px 24px rgba(18, 35, 53, 0.16);
}

.btn.primary {
    color: #ffffff;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
}

.btn.ghost {
    background: rgba(255, 255, 255, 0.6);
}

.btn.small {
    font-size: 0.9rem;
    padding: 9px 10px;
}

.btn.active {
    outline: 2px solid var(--accent);
    background: var(--glow);
}

#game-screen {
    padding: 10px 8px 16px;
}

.play-layout {
    width: min(96vw, calc((100svh - 116px) / 2 + 108px), 500px);
    display: grid;
    gap: 8px;
    justify-items: center;
}

.hud {
    width: 100%;
    display: grid;
    grid-template-columns: auto repeat(3, minmax(0, 1fr));
    gap: 8px;
}

.hud.thin .hud-item,
.hud.thin .btn {
    min-height: 42px;
}

.hud-item {
    background: var(--card);
    border: 1px solid rgba(255, 255, 255, 0.8);
    border-radius: 12px;
    padding: 5px 9px;
    display: grid;
    box-shadow: 0 8px 16px rgba(18, 35, 53, 0.1);
}

.hud-item span {
    font-size: 0.72rem;
    opacity: 0.8;
}

.hud-item strong {
    font-size: 1rem;
}

.play-stage {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 92px;
    align-items: start;
    gap: 8px;
}

.board-card {
    background: var(--card);
    border-radius: 18px;
    padding: 12px;
    border: 1px solid rgba(255, 255, 255, 0.8);
    box-shadow: 0 12px 22px rgba(18, 35, 53, 0.13);
    animation: reveal 0.45s ease;
}

.board-main {
    padding: 8px;
}

.board-card canvas {
    display: block;
    border-radius: 12px;
    border: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.5);
    touch-action: none;
}

.board-main canvas {
    width: 100%;
    height: auto;
    max-height: calc(100svh - 148px);
    aspect-ratio: 1 / 2;
}

.hidden {
    display: none;
}

.side-rail {
    display: grid;
    gap: 8px;
    width: 92px;
}

.rail-card {
    display: grid;
    gap: 6px;
    justify-items: center;
    text-align: center;
    padding: 10px 8px;
    border-radius: 16px;
    background: var(--card);
    border: 1px solid rgba(255, 255, 255, 0.8);
    box-shadow: 0 10px 18px rgba(18, 35, 53, 0.11);
}

.rail-card span {
    font-size: 0.72rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.rail-card strong {
    font-size: 1.3rem;
}

.rail-card canvas {
    width: 100%;
    height: auto;
    border-radius: 10px;
}

.rail-card.compact {
    min-height: 76px;
    align-content: center;
}

.mobile-help {
    width: 100%;
    text-align: center;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 12px;
    font-size: 0.82rem;
    padding: 2px 8px;
}

.overlay {
    position: absolute;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(15, 23, 42, 0.4);
    z-index: 10;
}

.overlay.active {
    display: flex;
}

.modal {
    width: min(420px, calc(100% - 24px));
    border-radius: 20px;
    background: var(--card);
    border: 1px solid rgba(255, 255, 255, 0.8);
    box-shadow: 0 20px 40px rgba(18, 35, 53, 0.24);
    padding: 20px;
    display: grid;
    gap: 10px;
}

.modal h2 {
    margin: 0;
    font-family: var(--font-title);
}

.setting-block {
    display: grid;
    gap: 8px;
}

.setting-label {
    font-weight: 800;
}

.theme-picker {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
}

.theme-tile {
    position: relative;
    display: grid;
    place-items: center;
    min-height: 76px;
    padding: 8px;
    border-radius: 18px;
    background: linear-gradient(135deg, rgba(255,255,255,0.86), rgba(255,255,255,0.58));
    border: 2px solid transparent;
    cursor: pointer;
    box-shadow: 0 10px 20px rgba(18, 35, 53, 0.11);
    transition: transform 0.22s ease, border-color 0.22s ease, box-shadow 0.22s ease;
}

.theme-tile input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.theme-tile span {
    font-weight: 800;
}

.theme-tile[data-theme="plain"] {
    background: linear-gradient(135deg, #edf5ff, #d4e6ff);
}

.theme-tile[data-theme="joy"] {
    background: linear-gradient(135deg, #fff8d5, #ffd9b7);
}

.theme-tile[data-theme="girly"] {
    background: linear-gradient(135deg, #ffe3ef, #ffd8fb);
}

.theme-tile.active {
    transform: translateY(-1px);
    border-color: transparent;
    background:
        linear-gradient(var(--card), var(--card)) padding-box,
        linear-gradient(135deg, #ffe98d, #ffb348, #fff4bc) border-box;
    box-shadow: 0 14px 24px rgba(209, 158, 45, 0.24);
}

.switch-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    font-weight: 800;
}

.switch {
    position: relative;
    display: inline-flex;
    width: 60px;
    height: 32px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.switch-slider {
    position: absolute;
    inset: 0;
    border-radius: 999px;
    background: linear-gradient(135deg, var(--danger), #f08d8d);
    box-shadow: inset 0 2px 6px rgba(0,0,0,0.16);
    transition: background 0.25s ease;
}

.switch-slider::before {
    content: "";
    position: absolute;
    top: 4px;
    left: 4px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #ffffff;
    box-shadow: 0 4px 10px rgba(0,0,0,0.18);
    transition: transform 0.25s ease;
}

.switch input:checked + .switch-slider {
    background: linear-gradient(135deg, #44d26e, var(--success));
}

.switch input:checked + .switch-slider::before {
    transform: translateX(28px);
}

.scores-list {
    display: grid;
    gap: 8px;
    max-height: 320px;
    overflow: auto;
}

.score-row,
.gameover-stats {
    display: grid;
    gap: 8px;
}

.score-row {
    grid-template-columns: 30px 1fr auto auto;
    align-items: center;
    padding: 10px 12px;
    border-radius: 14px;
    background: rgba(255,255,255,0.72);
}

.gameover-stats {
    grid-template-columns: repeat(3, 1fr);
}

.gameover-stats div {
    display: grid;
    gap: 4px;
    text-align: center;
    padding: 10px;
    border-radius: 14px;
    background: rgba(255,255,255,0.7);
}

.gameover-stats span {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

label {
    display: grid;
    gap: 6px;
    font-weight: 700;
}

select {
    border-radius: 10px;
    border: 1px solid var(--line);
    padding: 8px;
    font-family: inherit;
}

#toast {
    position: absolute;
    left: 50%;
    bottom: 14px;
    transform: translateX(-50%) translateY(30px);
    background: rgba(15, 23, 42, 0.86);
    color: #ffffff;
    padding: 8px 14px;
    border-radius: 999px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
}

#toast.active {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

#app[data-theme="joy"] {
    --bg-a: #fff6cb;
    --bg-b: #ffcfad;
    --ink: #372414;
    --card: rgba(255, 252, 240, 0.84);
    --line: #ffc07a;
    --accent: #ff7f3e;
    --accent-2: #ffcf33;
    --glow: rgba(255, 127, 62, 0.28);
    --font-main: "Baloo 2", sans-serif;
    --font-title: "Baloo 2", sans-serif;
}

#app[data-theme="girly"] {
    --bg-a: #ffdce9;
    --bg-b: #ffd2f6;
    --ink: #442134;
    --card: rgba(255, 245, 251, 0.86);
    --line: #f8a8c8;
    --accent: #ef5da8;
    --accent-2: #f9a03f;
    --glow: rgba(239, 93, 168, 0.24);
    --font-main: "Fredoka", sans-serif;
    --font-title: "Fredoka", sans-serif;
}

@keyframes reveal {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@media (max-width: 860px) {
    .mode-select,
    .difficulty-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 560px) {
    .menu-glass {
        padding: 20px;
    }

    #main-menu {
        grid-template-columns: 1fr;
    }

    .btn {
        padding: 11px;
    }

    .play-layout {
        width: min(96vw, calc((100svh - 112px) / 2 + 104px));
    }

    .hud {
        grid-template-columns: auto repeat(2, minmax(0, 1fr));
    }

    .hud-item:last-of-type {
        display: none;
    }

    .theme-picker,
    .gameover-stats {
        grid-template-columns: 1fr;
    }
}