/* ─── Tokens ──────────────────────────────────────────────────────────────── */
:root {
    --bg:       #fafaf8;
    --surface:  #ffffff;
    --surface2: #f3f2ee;
    --border:   #e4e2db;
    --text:     #1a1a18;
    --muted:    #6f6d68;
    --accent:   #2563eb;
    --accent2:  #1d4ed8;
    --success:  #16a34a;
    --warn:     #ea580c;
    --danger:   #dc2626;
    --mono:     'DM Mono', monospace;
    --sans:     'DM Sans', system-ui, sans-serif;
}

/* ─── Reset ───────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: var(--sans);
    background: var(--bg);
    color: var(--text);
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
}

/* ─── Header ──────────────────────────────────────────────────────────────── */
.site-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}

@media (max-width: 600px) {
    .site-header {
        padding: 10px 14px;
    }
}

.logo {
    font-family: var(--mono);
    font-size: 18px;
    font-weight: 500;
    color: var(--accent);
    letter-spacing: -0.5px;
}
.logo span { color: var(--text); }

.header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-date {
    font-size: 13px;
    color: var(--muted);
    font-family: var(--mono);
}

.streak-pill {
    display: flex;
    align-items: center;
    gap: 5px;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 4px 10px;
    font-size: 13px;
    font-weight: 700;
    color: var(--warn);
}

/* ─── Layout ──────────────────────────────────────────────────────────────── */
.app-container {
    flex: 1;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 16px;
    overflow-y: auto;
}

/* On mobile, squeeze side padding so board fills the screen width */
@media (max-width: 600px) {
    .app-container {
        padding: 8px 6px;
    }
}

.view-panel {
    width: 100%;
    max-width: 520px;
    padding: 8px 0 24px;
}

.hidden { display: none !important; }

/* ─── Selection screen ────────────────────────────────────────────────────── */
.welcome-hero {
    text-align: center;
    padding: 28px 0 32px;
}
.welcome-hero h2 {
    font-size: 30px;
    font-weight: 800;
    margin-bottom: 10px;
    line-height: 1.15;
}
.welcome-hero p {
    color: var(--muted);
    font-size: 15px;
    line-height: 1.6;
    max-width: 340px;
    margin: 0 auto;
}

.stats-row {
    display: flex;
    gap: 10px;
    margin-bottom: 24px;
}
.stat-card {
    flex: 1;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px 12px;
    text-align: center;
}
.stat-val {
    font-size: 26px;
    font-weight: 800;
    font-family: var(--mono);
    color: var(--accent);
    line-height: 1;
    margin-bottom: 4px;
}
.stat-label {
    font-size: 11px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.8px;
    font-weight: 700;
}

.difficulty-card-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.menu-card-btn {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 18px 20px;
    text-align: left;
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s, transform 0.1s;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: var(--text);
    width: 100%;
    font-family: var(--sans);
}
.menu-card-btn:hover  { border-color: var(--accent); background: var(--surface2); }
.menu-card-btn:active { transform: scale(0.98); }
.menu-card-btn:disabled { opacity: 0.4; cursor: default; }

.menu-card-left { display: flex; flex-direction: column; gap: 3px; }
.card-title     { font-size: 17px; font-weight: 700; }
.card-desc      { font-size: 13px; color: var(--muted); }

.card-badge {
    font-size: 11px;
    font-weight: 700;
    padding: 3px 8px;
    border-radius: 20px;
    letter-spacing: 0.5px;
}
.badge-easy   { background: rgba(22,163,74,0.12); color: var(--success); }
.badge-medium { background: rgba(37,99,235,0.12); color: var(--accent); }
.badge-hard   { background: rgba(220,38,38,0.12); color: var(--danger); }

.card-solved { border-color: var(--success) !important; }
.card-solved .card-title::after { content: ' ✓'; color: var(--success); font-size: 14px; }

/* ─── Game screen ─────────────────────────────────────────────────────────── */
.game-controls-bar {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    margin-bottom: 16px;
}
.game-controls-bar > #back-to-menu-btn { justify-self: start; }
.game-controls-bar > .game-meta { justify-self: center; }

@media (max-width: 600px) {
    .game-controls-bar {
        margin-bottom: 8px;
    }
}

.secondary-btn {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 7px;
    padding: 7px 13px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    color: var(--muted);
    font-family: var(--sans);
    transition: color 0.15s, border-color 0.15s;
}
.secondary-btn:hover { color: var(--text); border-color: var(--muted); }

.game-meta {
    display: flex;
    align-items: center;
    gap: 10px;
}
.diff-label {
    font-family: var(--mono);
    font-size: 13px;
    font-weight: 500;
    color: var(--accent);
}
.timer-display {
    font-family: var(--mono);
    font-size: 13px;
    color: var(--muted);
    min-width: 38px;
    text-align: right;
}

.pause-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    line-height: 1;
}

.dashboard-badge-row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 14px;
    justify-content: center;
}
.target-badge {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 14px;
    font-weight: 700;
    font-size: 15px;
    font-family: var(--mono);
    color: var(--text);
    transition: background 0.3s, color 0.3s, border-color 0.3s, transform 0.3s;
}

@media (max-width: 600px) {
    .dashboard-badge-row {
        gap: 5px;
        margin-bottom: 8px;
    }
    .target-badge {
        padding: 4px 10px;
        font-size: 13px;
    }
}
.target-badge.found {
    background: var(--success);
    color: #fff;
    border-color: var(--success);
    transform: scale(1.05);
}

.equation-container {
    text-align: center;
    margin-bottom: 14px;
    min-height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
}

@media (max-width: 600px) {
    .equation-container {
        margin-bottom: 8px;
        min-height: 24px;
    }
    .equation-text {
        font-size: 14px;
    }
}
.equation-text {
    font-family: var(--mono);
    font-size: 17px;
    font-weight: 500;
    letter-spacing: 1px;
    color: var(--text);
    transition: color 0.2s;
}
.equation-text.wrong   { color: var(--danger); }
.equation-text.correct { color: var(--success); }

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20%      { transform: translateX(-6px); }
    40%      { transform: translateX(6px); }
    60%      { transform: translateX(-4px); }
    80%      { transform: translateX(4px); }
}
.shake { animation: shake 0.35s ease; }

/* ─── Board ───────────────────────────────────────────────────────────────── */
.relative-board-box {
    position: relative;
    width: 100%;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: var(--board-padding, 12px);
    touch-action: none;
}

@media (max-width: 600px) {
    .relative-board-box {
        --board-padding: 6px;
        border-radius: 8px;
    }
    #grid-board {
        --board-gap: 4px;
    }
}

#grid-board {
    display: grid;
    gap: var(--board-gap, 7px);
    width: 100%;
}

.tile {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--tile-font-size, 18px);
    font-weight: 700;
    font-family: var(--mono);
    user-select: none;
    cursor: pointer;
    color: var(--text);
    transition: background 0.1s, border-color 0.1s, transform 0.1s;
}
.tile.selected {
    background: rgba(37,99,235,0.15);
    border-color: var(--accent);
    color: var(--accent);
    transform: scale(1.06);
    z-index: 2;
}
.tile.solved {
    background: var(--surface2);
    color: var(--muted);
    border-color: var(--border);
    cursor: default;
}

/* ─── Pause overlay ───────────────────────────────────────────────────────── */
.pause-overlay {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 18px;
    background: rgba(250, 250, 248, 0.1);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    border-radius: inherit;
    /* Block all pointer/touch interaction with the board underneath */
    touch-action: none;
}
.pause-overlay.hidden { display: none !important; }

.pause-overlay-icon {
    font-size: 36px;
    color: var(--muted);
}

.pause-resume-btn.pause-resume-btn {
    width: 50%;
    padding: 10px 22px;
    font-size: 14px;
}

/* ─── SVG overlay ─────────────────────────────────────────────────────────── */
#line-overlay {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    pointer-events: none;
    z-index: 10;
    border-radius: 12px;
    overflow: hidden;
}
.solved-line {
    fill: none;
    stroke: var(--accent);
    stroke-width: 14;
    stroke-linecap: round;
    stroke-linejoin: round;
    opacity: 0.25;
}
.active-line {
    fill: none;
    stroke: var(--accent);
    stroke-width: 7;
    stroke-linecap: round;
    stroke-linejoin: round;
    opacity: 0.9;
}

/* ─── Win screen ──────────────────────────────────────────────────────────── */
.win-hero {
    text-align: center;
    padding: 32px 0 24px;
}
.win-trophy {
    font-size: 60px;
    display: block;
    margin-bottom: 16px;
    animation: trophyPop 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275) both;
}
@keyframes trophyPop {
    0%   { transform: scale(0.2) rotate(-15deg); opacity: 0; }
    100% { transform: scale(1)   rotate(0deg);   opacity: 1; }
}
.win-hero h2 { font-size: 28px; font-weight: 800; margin-bottom: 8px; }
.win-hero p  { color: var(--muted); font-size: 15px; }

.win-time-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 18px;
    text-align: center;
    margin-bottom: 16px;
}
.win-time-val   { font-family: var(--mono); font-size: 36px; font-weight: 700; color: var(--accent); }
.win-time-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.8px; font-weight: 700; margin-top: 4px; }
.win-time-note  { font-size: 13px; color: var(--success); margin-top: 6px; font-weight: 600; }

.win-btn-group { display: flex; flex-direction: column; gap: 10px; margin-top: 20px; }

.primary-btn {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 10px;
    padding: 14px 20px;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    font-family: var(--sans);
    width: 100%;
    transition: background 0.15s, transform 0.1s;
}
.primary-btn:hover  { background: var(--accent2); }
.primary-btn:active { transform: scale(0.98); }

.outline-btn {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 13px 20px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    font-family: var(--sans);
    width: 100%;
    transition: border-color 0.15s, background 0.15s;
}
.outline-btn:hover { border-color: var(--muted); background: var(--surface2); }

/* ─── Toast ───────────────────────────────────────────────────────────────── */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(80px);
    background: var(--success);
    color: #fff;
    font-weight: 700;
    font-size: 14px;
    padding: 10px 20px;
    border-radius: 30px;
    transition: transform 0.3s ease;
    z-index: 1000;
    pointer-events: none;
    white-space: nowrap;
}
.toast.show { transform: translateX(-50%) translateY(0); }

.help-btn {
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 50%;
    width: 28px;
    height: 28px;
    font-size: 14px;
    font-weight: 700;
    color: var(--muted);
    cursor: pointer;
    font-family: var(--sans);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.15s, border-color 0.15s;
}
.help-btn:hover { color: var(--text); border-color: var(--muted); }

/* ─── How to play overlay ─────────────────────────────────────────────────── */
.htp-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.htp-backdrop.hidden { display: none !important; }

.htp-modal {
    background: var(--surface);
    border-radius: 16px;
    padding: 28px 24px 24px;
    max-width: 360px;
    width: 100%;
    box-shadow: 0 8px 32px rgba(0,0,0,0.18);
}
.htp-title {
    font-size: 22px;
    font-weight: 800;
    margin-bottom: 20px;
    text-align: center;
}
.htp-steps {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin-bottom: 24px;
}
.htp-steps li {
    display: flex;
    gap: 12px;
    align-items: flex-start;
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
}
.htp-icon {
    font-size: 20px;
    flex-shrink: 0;
    margin-top: 1px;
}

/* Tile tap-active state (finger down but not yet extending) */
.tile.tap-active {
    background: var(--accent);
    border-color: var(--accent2);
    color: #fff;
    box-shadow: 0 0 0 3px rgba(37,99,235,0.3);
}

/* ─── Footer ──────────────────────────────────────────────────────────────── */
.site-footer {
    border-top: 1px solid var(--border);
    padding: 12px 20px;
    text-align: center;
    font-size: 12px;
    color: var(--muted);
    background: var(--surface);
    flex-shrink: 0;
}

@media (max-width: 600px) {
    .site-footer { display: none; }
}

/* ─── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 380px) {
    .tile          { font-size: 15px; }
    .welcome-hero h2 { font-size: 24px; }
}

@media (prefers-reduced-motion: reduce) {
    .win-trophy, .tile, .target-badge { animation: none !important; transition: none !important; }
    .shake { animation: none !important; }
}
