:root {
    --bg-top: #1A1530;
    --bg-bottom: #0C0A1A;
    --surface: #211B3A;
    --surface-2: #2E2750;
    --primary: #6C5CE7;
    --accent: #00D2A8;
    --text: #FFFFFF;
    --text-dim: #B8B2D6;
    --text-muted: #7E7AA0;
    --gold: #FFD166;
    --radius: 16px;
    --gap: 10px;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
    height: 100%;
    background: linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
    overscroll-behavior: none;
}

body {
    display: flex;
    justify-content: center;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

#app {
    width: 100%;
    max-width: 520px;
    min-height: 100%;
    position: relative;
    padding: 14px 14px 8px;
}

/* ---------- View switching ---------- */
.view { display: none; flex-direction: column; min-height: calc(100vh - 22px); }
.view.is-active { display: flex; animation: fade .25s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* ---------- Buttons (shared) ---------- */
.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--accent));
    border: 0; color: #fff; font-size: 17px; font-weight: 800;
    padding: 15px; border-radius: 14px; cursor: pointer; width: 100%;
}
.btn-primary:active { transform: scale(.98); }
.btn-secondary {
    background: var(--surface-2);
    border: 1px solid rgba(255, 255, 255, .12);
    color: var(--text); font-size: 14px; font-weight: 700;
    padding: 12px 16px; border-radius: 12px; cursor: pointer; white-space: nowrap;
}
.btn-secondary.wide { width: 100%; text-align: center; }
.btn-secondary:active { transform: scale(.98); }
.icon-btn {
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, .08);
    color: var(--text);
    width: 42px; height: 42px; border-radius: 12px;
    font-size: 20px; line-height: 1; cursor: pointer;
}
.icon-btn:active { transform: scale(.95); }
.icon-btn-spacer { width: 42px; height: 42px; }

/* ---------- Segmented control ---------- */
.segmented {
    display: flex;
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, .06);
    border-radius: 12px; padding: 4px; gap: 2px;
}
.seg {
    flex: 1; border: 0; background: transparent; color: var(--text-dim);
    font-size: 14px; font-weight: 700; padding: 10px 12px; border-radius: 9px; cursor: pointer;
}
.seg.is-active { background: linear-gradient(135deg, var(--primary), var(--accent)); color: #fff; }

/* ---------- MENU ---------- */
.menu-top { display: flex; justify-content: flex-end; min-height: 42px; }
.hero { text-align: center; margin: auto 0; padding: 24px 0; }
.logo {
    width: 92px; height: 92px; margin: 0 auto 18px;
    display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
}
.logo-tile { border-radius: 12px; background: var(--surface-2); }
.logo-tile:nth-child(1), .logo-tile:nth-child(4) {
    background: linear-gradient(135deg, var(--primary), var(--accent));
    box-shadow: 0 0 18px rgba(108, 92, 231, .5);
}
.hero-title { font-size: 40px; font-weight: 900; letter-spacing: .5px; }
.hero-sub { margin-top: 6px; color: var(--text-dim); font-size: 15px; }

.menu-panel {
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, .06);
    border-radius: 20px; padding: 18px; display: flex; flex-direction: column; gap: 12px;
}
.panel-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .6px; }

/* ---------- Top bar (game / records) ---------- */
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 14px;
}
.topbar-title { font-size: 18px; font-weight: 800; }

/* ---------- Stats ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--gap); margin-bottom: 12px; }
.stat {
    background: var(--surface); border: 1px solid rgba(255, 255, 255, .06);
    border-radius: var(--radius); padding: 10px 8px; text-align: center;
}
.stat-label { display: block; font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .6px; }
.stat-value { display: block; margin-top: 4px; font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; margin-bottom: 14px; }
.toolbar.center { justify-content: center; }
.toolbar .btn-secondary { margin-left: auto; }

/* ---------- Board ---------- */
.board { flex: 1; display: grid; gap: var(--gap); align-content: start; }
.board[data-cols="4"] { grid-template-columns: repeat(4, 1fr); }

.card { position: relative; aspect-ratio: 1 / 1; perspective: 800px; cursor: pointer; }
.card-inner {
    position: absolute; inset: 0; transform-style: preserve-3d;
    transition: transform .35s cubic-bezier(.2, .8, .2, 1); border-radius: 14px;
}
.card.is-flipped .card-inner, .card.is-matched .card-inner { transform: rotateY(180deg); }
.card-face {
    position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
    border-radius: 14px; backface-visibility: hidden; -webkit-backface-visibility: hidden;
    font-size: clamp(26px, 9vw, 46px); user-select: none;
}
.card-front {
    background: linear-gradient(135deg, #322a5c, #241f40);
    border: 1px solid rgba(255, 255, 255, .08); color: rgba(255, 255, 255, .25);
}
.card-back {
    background: linear-gradient(135deg, var(--surface), #1b1733);
    border: 1px solid rgba(0, 210, 168, .25); transform: rotateY(180deg);
}
.card.is-matched .card-back { border-color: var(--accent); box-shadow: 0 0 16px rgba(0, 210, 168, .35); }
.card.is-matched { animation: pop .35s ease; }
@keyframes pop { 0% { transform: scale(1); } 50% { transform: scale(1.06); } 100% { transform: scale(1); } }

/* ---------- Records ---------- */
.records { display: flex; flex-direction: column; gap: var(--gap); flex: 1; }
.record-row {
    background: var(--surface); border: 1px solid rgba(255, 255, 255, .06);
    border-radius: var(--radius); padding: 16px;
    display: flex; align-items: center; justify-content: space-between;
}
.record-name { font-size: 16px; font-weight: 800; }
.record-vals { display: flex; gap: 22px; text-align: right; }
.record-vals div { display: flex; flex-direction: column; }
.record-vals b { font-size: 18px; font-weight: 800; color: var(--gold); font-variant-numeric: tabular-nums; }
.record-vals small { color: var(--text-muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }
.record-empty b { color: var(--text-muted); }

/* ---------- Footer ---------- */
.footer { text-align: center; padding: 12px 0 4px; margin-top: auto; }
.version { font-size: 11px; color: var(--text-muted); }

/* ---------- Win overlay ---------- */
.overlay {
    position: fixed; inset: 0; background: rgba(12, 10, 26, .82);
    backdrop-filter: blur(6px); display: flex; align-items: center; justify-content: center;
    padding: 24px; z-index: 50;
}
.overlay[hidden] { display: none; }
.overlay-card {
    width: 100%; max-width: 340px;
    background: linear-gradient(180deg, var(--surface), #1a1630);
    border: 1px solid rgba(255, 255, 255, .1);
    border-radius: 22px; padding: 28px 22px; text-align: center; animation: rise .3s ease;
}
@keyframes rise { from { transform: translateY(16px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.overlay-emoji { font-size: 56px; }
.overlay-title { margin-top: 8px; font-size: 26px; font-weight: 800; }
.record-badge {
    display: inline-block; margin-top: 8px; padding: 5px 12px; border-radius: 999px;
    background: rgba(255, 209, 102, .15); color: var(--gold); font-weight: 800; font-size: 13px;
}
.record-badge[hidden] { display: none; }
.overlay-stats { display: flex; justify-content: center; gap: 32px; margin: 20px 0 22px; }
.overlay-stats div { display: flex; flex-direction: column; }
.overlay-stats span { font-size: 26px; font-weight: 800; }
.overlay-stats small { color: var(--text-muted); font-size: 12px; }
.overlay-card .btn-secondary { margin-top: 10px; }
