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

:root {
    --bg: linear-gradient(180deg, #dbeafe 0%, #fef3c7 100%);
    --surface: #ffffff;
    --ink: #1e293b;
    --ink-soft: #475569;
    --ink-faint: #94a3b8;
    --line: #e2e8f0;
    --blue: #2563eb;
    --blue-dark: #1d4ed8;
    --blue-soft: #dbeafe;
    --yellow: #facc15;
    --yellow-warm: #f59e0b;
    --yellow-soft: #fef3c7;
    --good: #10b981;
    --good-soft: #d1fae5;
    --bad: #ef4444;
    --bad-soft: #fee2e2;
    --shadow: 0 8px 24px rgba(30, 64, 175, 0.10);
    --shadow-pop: 0 14px 40px rgba(30, 64, 175, 0.18);
    --radius: 20px;
    --radius-sm: 14px;
}

html, body {
    height: 100%;
    background: var(--bg);
    background-attachment: fixed;
    color: var(--ink);
    font-family: 'Nunito', 'Avenir Next', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    font-size: 16px;
    line-height: 1.4;
    -webkit-font-smoothing: antialiased;
    touch-action: manipulation;
}

body {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    background: var(--blue);
    color: #fff;
    box-shadow: 0 2px 10px rgba(30, 64, 175, 0.20);
}

.brand {
    font-weight: 900;
    font-size: 1.05rem;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: #fff;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 4px 6px;
    border-radius: 8px;
    transition: background 0.15s ease;
}

.brand:hover { background: rgba(255, 255, 255, 0.12); }
.brand-name  { display: inline-flex; align-items: center; gap: 8px; }
.brand-name::before { content: "🧮"; font-size: 1.15rem; }

.lang-toggle {
    display: inline-flex;
    background: rgba(255, 255, 255, 0.18);
    border-radius: 999px;
    padding: 3px;
}

.lang-btn {
    background: transparent;
    border: 0;
    padding: 6px 14px;
    font-weight: 800;
    font-size: 0.82rem;
    color: rgba(255, 255, 255, 0.78);
    cursor: pointer;
    min-height: 32px;
    border-radius: 999px;
    transition: background 0.15s ease, color 0.15s ease;
}

.lang-btn.is-active {
    background: #fff;
    color: var(--blue);
}

.screen {
    display: none;
    flex: 1;
    padding: 24px 18px 48px;
    overflow-y: auto;
}

body.screen-setup      .screen-setup-view      { display: flex; flex-direction: column; align-items: center; }
body.screen-game       .screen-game-view       { display: flex; flex-direction: column; align-items: stretch; }
body.screen-scoreboard .screen-scoreboard-view { display: flex; flex-direction: column; align-items: center; }

.card {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 24px 22px;
    width: 100%;
    max-width: 480px;
}

/* ---------- Setup screen ---------- */

.setup-card { display: flex; flex-direction: column; gap: 18px; }

.quick-mode {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 22px 20px;
    background: linear-gradient(135deg, var(--yellow) 0%, var(--yellow-warm) 100%);
    color: #422006;
    border: 0;
    border-radius: var(--radius);
    cursor: pointer;
    min-height: 84px;
    box-shadow: var(--shadow-pop);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
    position: relative;
    overflow: hidden;
}

.quick-mode::after {
    content: "";
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at 80% 30%, rgba(255, 255, 255, 0.5), transparent 60%);
    pointer-events: none;
}

.quick-mode:hover { transform: translateY(-2px) scale(1.01); box-shadow: 0 18px 44px rgba(245, 158, 11, 0.35); }
.quick-mode:active { transform: translateY(0) scale(0.99); }

.quick-label {
    font-weight: 900;
    font-size: 1.6rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    position: relative;
}

.quick-label::before {
    content: "▶";
    font-size: 1.1rem;
}

.quick-sub {
    font-size: 0.9rem;
    font-weight: 700;
    opacity: 0.85;
    position: relative;
}

.customize-toggle {
    background: transparent;
    border: 0;
    color: var(--ink-soft);
    font-size: 0.95rem;
    font-weight: 800;
    font-family: inherit;
    padding: 10px 12px;
    border-radius: 12px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    align-self: center;
    transition: background 0.15s ease, color 0.15s ease;
}

.customize-toggle:hover { background: var(--blue-soft); color: var(--blue-dark); }

.customize-toggle .chevron {
    transition: transform 0.2s ease;
    font-size: 0.9rem;
}

.customize-toggle[aria-expanded="true"] .chevron { transform: rotate(180deg); }

.customize-panel {
    display: flex;
    flex-direction: column;
    gap: 18px;
    margin-top: 2px;
    animation: panelOpen 0.3s ease-out;
}

.customize-panel[hidden] { display: none; }

@keyframes panelOpen {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.opt-group { display: flex; flex-direction: column; gap: 8px; }

.opt-group > label {
    font-size: 0.95rem;
    font-weight: 800;
    color: var(--ink);
    text-align: center;
}

.opt-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
}

.opt-btn {
    flex: 1 1 0;
    min-height: 56px;
    min-width: 56px;
    padding: 10px 12px;
    background: var(--surface);
    border: 2px solid var(--line);
    border-radius: var(--radius-sm);
    color: var(--ink);
    font-size: 1rem;
    font-weight: 800;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    transition: transform 0.1s ease, border-color 0.15s ease, background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
    line-height: 1;
}

.opt-btn:hover { border-color: var(--blue); transform: translateY(-1px); }
.opt-btn:active { transform: translateY(0); }

.opt-btn.is-active {
    background: var(--blue);
    border-color: var(--blue);
    color: #fff;
    box-shadow: 0 6px 16px rgba(37, 99, 235, 0.30);
}

/* Operation row: large symbols */
.opt-row.op-row .opt-btn {
    flex-direction: column;
    min-height: 72px;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-weight: 800;
    gap: 2px;
}

.op-sym {
    font-size: 1.6rem;
    line-height: 1;
    font-weight: 900;
}

/* Difficulty row: star pills */
.opt-row.diff-row .opt-btn {
    flex-direction: column;
    gap: 2px;
    min-height: 68px;
    font-size: 0.85rem;
    font-weight: 800;
}

.diff-stars {
    font-size: 1.1rem;
    letter-spacing: 0.08em;
    color: var(--yellow);
}

.opt-btn.is-active .diff-stars { color: #fde68a; }

.start-btn {
    margin-top: 6px;
    min-height: 60px;
    padding: 14px 18px;
    background: linear-gradient(135deg, var(--blue) 0%, var(--blue-dark) 100%);
    color: #fff;
    border: 0;
    border-radius: var(--radius);
    font-size: 1.15rem;
    font-weight: 900;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    cursor: pointer;
    box-shadow: 0 10px 24px rgba(37, 99, 235, 0.30);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.start-btn:hover, .start-btn:focus { transform: translateY(-2px); box-shadow: 0 14px 30px rgba(37, 99, 235, 0.40); }
.start-btn:active { transform: translateY(0); }

/* ---------- Game screen ---------- */

.game-top {
    display: flex;
    align-items: center;
    gap: 14px;
    width: 100%;
    max-width: 560px;
    margin: 0 auto 14px;
}

.quit-btn {
    background: var(--surface);
    border: 0;
    border-radius: 999px;
    width: 44px;
    height: 44px;
    font-size: 1.2rem;
    cursor: pointer;
    color: var(--ink-soft);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow);
    flex-shrink: 0;
}

.progress-bar {
    flex: 1;
    height: 14px;
    background: rgba(255, 255, 255, 0.65);
    border-radius: 999px;
    overflow: hidden;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.06);
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--yellow) 0%, var(--yellow-warm) 100%);
    border-radius: 999px;
    transition: width 0.35s ease;
    width: 0%;
}

.streak {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-weight: 900;
    font-size: 1rem;
    color: var(--ink-soft);
    background: var(--surface);
    border-radius: 999px;
    padding: 6px 12px;
    min-height: 36px;
    box-shadow: var(--shadow);
    flex-shrink: 0;
}

.streak.is-hot { color: var(--yellow-warm); animation: streakPulse 0.5s ease; }

@keyframes streakPulse {
    0%   { transform: scale(1); }
    50%  { transform: scale(1.18); }
    100% { transform: scale(1); }
}

.problem-wrap {
    flex: 1;
    width: 100%;
    max-width: 560px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 36vh;
    position: relative;
}

.problem {
    font-family: 'Nunito', 'Avenir Next', -apple-system, system-ui, sans-serif;
    font-size: clamp(2.8rem, 11vw, 4.6rem);
    font-weight: 900;
    letter-spacing: -0.01em;
    text-align: center;
    color: var(--blue-dark);
    background: var(--surface);
    border-radius: 28px;
    padding: 28px 32px;
    box-shadow: var(--shadow-pop);
    animation: problemPop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.problem .eq { color: var(--ink-faint); margin: 0 0.15em; }
.problem .qmark { color: var(--yellow-warm); }

@keyframes problemPop {
    from { transform: scale(0.85); opacity: 0; }
    to   { transform: scale(1);    opacity: 1; }
}

.answer-form {
    width: 100%;
    max-width: 560px;
    margin: 0 auto;
    display: flex;
    gap: 10px;
}

#answerInput {
    flex: 1;
    min-height: 64px;
    padding: 10px 18px;
    font-size: clamp(1.7rem, 6vw, 2.4rem);
    font-weight: 900;
    text-align: center;
    border: 3px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--ink);
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
    font-family: inherit;
}

#answerInput:focus { border-color: var(--blue); box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.15); }

.submit-btn {
    min-height: 64px;
    padding: 0 26px;
    background: linear-gradient(135deg, var(--blue) 0%, var(--blue-dark) 100%);
    color: #fff;
    border: 0;
    border-radius: var(--radius);
    font-weight: 900;
    font-size: 1.05rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    cursor: pointer;
    box-shadow: 0 8px 20px rgba(37, 99, 235, 0.30);
    transition: transform 0.12s ease, box-shadow 0.15s ease;
    font-family: inherit;
}

.submit-btn:hover, .submit-btn:focus { transform: translateY(-1px); box-shadow: 0 12px 26px rgba(37, 99, 235, 0.40); }
.submit-btn:disabled { opacity: 0.4; cursor: default; transform: none; box-shadow: none; }

/* Result overlay */
.result-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 100;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
}

.result-overlay.is-visible { display: flex; }

.result-card {
    background: var(--surface);
    border-radius: 26px;
    padding: 30px 28px;
    text-align: center;
    min-width: 280px;
    max-width: 92vw;
    box-shadow: 0 30px 70px rgba(0, 0, 0, 0.32);
    animation: pop 0.22s cubic-bezier(0.34, 1.56, 0.64, 1);
    position: relative;
}

.result-overlay.is-incorrect .result-card { animation: shake 0.36s ease-out; }

@keyframes pop {
    from { transform: scale(0.7); opacity: 0; }
    to   { transform: scale(1);   opacity: 1; }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20%      { transform: translateX(-10px); }
    40%      { transform: translateX(10px); }
    60%      { transform: translateX(-6px); }
    80%      { transform: translateX(6px); }
}

.result-icon {
    font-size: 4rem;
    line-height: 1;
    margin-bottom: 10px;
    display: inline-block;
}

.result-overlay.is-correct   .result-card { border: 3px solid var(--good); }
.result-overlay.is-correct   .result-icon { color: var(--good); animation: iconBurst 0.5s ease; }
.result-overlay.is-incorrect .result-card { border: 3px solid var(--bad); }
.result-overlay.is-incorrect .result-icon { color: var(--bad); }

@keyframes iconBurst {
    0%   { transform: scale(0.4) rotate(-10deg); }
    60%  { transform: scale(1.25) rotate(8deg); }
    100% { transform: scale(1) rotate(0); }
}

.result-text {
    font-size: 1.05rem;
    font-weight: 800;
    color: var(--ink);
    margin-bottom: 6px;
}

.result-text .answer-pill {
    display: inline-block;
    background: var(--blue-soft);
    color: var(--blue-dark);
    padding: 3px 10px;
    border-radius: 999px;
    font-weight: 900;
    margin-left: 4px;
}

.result-time {
    font-size: 1.5rem;
    font-weight: 900;
    color: var(--ink);
    margin-bottom: 14px;
}

.result-time::before {
    content: "⏱ ";
    font-size: 1.1rem;
    margin-right: 2px;
}

.result-tap {
    font-size: 0.78rem;
    color: var(--ink-faint);
    text-transform: uppercase;
    letter-spacing: 0.12em;
    font-weight: 800;
}

/* Confetti burst */
.confetti {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    pointer-events: none;
    opacity: 0;
}

.result-overlay.is-correct .confetti {
    animation: confettiBurst 0.9s ease-out forwards;
}

.confetti:nth-child(1)  { background: var(--yellow);     --dx:  -60px; --dy:  -80px; }
.confetti:nth-child(2)  { background: var(--blue);       --dx:   70px; --dy:  -70px; }
.confetti:nth-child(3)  { background: var(--good);       --dx:   90px; --dy:   30px; }
.confetti:nth-child(4)  { background: var(--yellow-warm);--dx:  -90px; --dy:   20px; }
.confetti:nth-child(5)  { background: var(--bad);        --dx:    0px; --dy: -100px; }
.confetti:nth-child(6)  { background: var(--blue-dark);  --dx:  -40px; --dy:   80px; }
.confetti:nth-child(7)  { background: var(--yellow);     --dx:   60px; --dy:   80px; }
.confetti:nth-child(8)  { background: var(--good);       --dx: -100px; --dy:  -20px; }

@keyframes confettiBurst {
    0%   { opacity: 1; transform: translate(-50%, -50%) scale(0.5); }
    60%  { opacity: 1; }
    100% { opacity: 0; transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(1.2); }
}

/* ---------- Scoreboard screen ---------- */

.scoreboard-card { padding-top: 28px; }

.score-stars {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin: 0 auto 14px;
    font-size: 2.4rem;
    line-height: 1;
}

.score-stars .star {
    color: var(--line);
    transition: color 0.25s ease, transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
    display: inline-block;
}

.score-stars .star.lit {
    color: var(--yellow);
    transform: scale(1.15) rotate(-6deg);
    filter: drop-shadow(0 4px 8px rgba(245, 158, 11, 0.35));
}

.score-stars .star.lit:nth-child(2) { transform: scale(1.2) rotate(0deg); }
.score-stars .star.lit:nth-child(3) { transform: scale(1.15) rotate(6deg); }

.scoreboard-card h1 {
    font-size: 1.7rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    text-align: center;
    margin-bottom: 6px;
    color: var(--blue-dark);
    font-weight: 900;
}

.score-blurb {
    text-align: center;
    color: var(--ink-soft);
    font-weight: 700;
    margin-bottom: 18px;
    font-size: 0.95rem;
}

.stats { display: flex; flex-direction: column; gap: 0; margin-bottom: 18px; }

.stat-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 11px 4px;
    border-bottom: 2px solid var(--line);
    font-size: 1rem;
}

.stat-row:last-child { border-bottom: 0; }

.stat-row > span:first-child {
    color: var(--ink-soft);
    font-size: 0.85rem;
    font-weight: 800;
}

.stat-row > span:last-child {
    font-weight: 900;
    font-size: 1.1rem;
    color: var(--blue-dark);
}

.per-problem {
    margin-bottom: 18px;
    border-top: 2px solid var(--line);
    padding-top: 14px;
}

.per-problem summary {
    cursor: pointer;
    font-weight: 800;
    color: var(--blue);
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    padding: 4px 0;
    list-style: none;
}

.per-problem summary::-webkit-details-marker { display: none; }
.per-problem summary::before {
    content: "▸ ";
    display: inline-block;
    transition: transform 0.15s ease;
}
.per-problem[open] summary::before { content: "▾ "; }

#problemList {
    list-style: none;
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#problemList li {
    display: grid;
    grid-template-columns: 1fr auto auto;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 0.95rem;
    border: 2px solid transparent;
}

#problemList li.is-correct   { background: var(--good-soft); border-color: rgba(16, 185, 129, 0.30); }
#problemList li.is-incorrect { background: var(--bad-soft);  border-color: rgba(239, 68, 68, 0.30); }

#problemList .pl-problem { font-weight: 800; font-family: 'Nunito', ui-monospace, SFMono-Regular, monospace; color: var(--ink); }
#problemList .pl-time    { color: var(--ink-soft); font-variant-numeric: tabular-nums; font-weight: 700; }
#problemList .pl-mark    { font-weight: 900; font-size: 1.1rem; }
#problemList .is-correct   .pl-mark { color: var(--good); }
#problemList .is-incorrect .pl-mark { color: var(--bad); }

#problemList .pl-given {
    display: block;
    font-size: 0.78rem;
    color: var(--ink-soft);
    margin-top: 2px;
    font-weight: 700;
}

.end-buttons {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.primary-btn, .secondary-btn {
    flex: 1 1 140px;
    min-height: 56px;
    border-radius: var(--radius);
    border: 0;
    font-size: 1.05rem;
    font-weight: 900;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.12s ease, box-shadow 0.15s ease;
}

.primary-btn   { background: linear-gradient(135deg, var(--blue) 0%, var(--blue-dark) 100%); color: #fff; box-shadow: 0 8px 20px rgba(37, 99, 235, 0.30); }
.primary-btn:hover, .primary-btn:focus { transform: translateY(-1px); box-shadow: 0 12px 26px rgba(37, 99, 235, 0.40); }
.secondary-btn { background: var(--surface); color: var(--ink); border: 2px solid var(--line); }
.secondary-btn:hover, .secondary-btn:focus { border-color: var(--blue); }

/* ---------- Responsive ---------- */

@media (max-width: 480px) {
    .opt-btn { font-size: 0.9rem; min-width: 52px; padding: 8px 8px; }
    .quick-label { font-size: 1.35rem; }
    .answer-form { flex-direction: column; }
    .submit-btn { width: 100%; }
    .problem { padding: 22px 18px; border-radius: 22px; }
    .score-stars { font-size: 2rem; }
}
