:root {
    --pink:   #FF6B9D;
    --green:  #6BCB77;
    --blue:   #4D96FF;
    --orange: #FF9A3C;
}

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

body {
    font-family: 'Nunito', 'Comic Sans MS', 'Chalkboard SE', 'Marker Felt', cursive;
    background: linear-gradient(135deg, #FFE5F0 0%, #FFF3B0 100%);
    min-height: 100vh;
    padding: 12px;
}

/* ── Caramelo candy pieces ──────────────────────────────────────── */

.cp-pink   { background: #FF6EB4; }
.cp-blue   { background: #5BB8F5; }
.cp-mint   { background: #2FD9A8; }
.cp-orange { background: #FFB347; }
.cp-purple { background: #9B6DFF; }

/* Inline piece — header counter, correct-mark */
.caramelo-piece {
    display: inline-block;
    width: 10px; height: 14px;
    border-radius: 50% 50% 45% 45% / 55% 55% 45% 45%;
    vertical-align: middle;
    position: relative;
    flex-shrink: 0;
}
.caramelo-piece::after {
    content: '';
    position: absolute;
    top: 2px; left: 2px;
    width: 3px; height: 3px;
    background: rgba(255,255,255,.3);
    border-radius: 50%;
}

/* ── Sound toggle ───────────────────────────────────────────────── */

.sound-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.2em;
    padding: 4px 6px;
    opacity: .6;
    transition: opacity .15s, transform .1s;
    flex-shrink: 0;
    line-height: 1;
}
.sound-btn:hover { opacity: 1; transform: scale(1.15); }

/* ── Home page ─────────────────────────────────────────────────── */

.home-page {
    max-width: 600px;
    margin: 0 auto;
    text-align: center;
    padding: 12px 10px;
}

/* ── Home top bar ────────────────────────────────────────────────── */

.home-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: rgba(255,255,255,.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border-radius: 16px;
    padding: 8px 12px 8px 10px;
    margin-bottom: 18px;
    box-shadow: 0 2px 10px rgba(0,0,0,.07);
    gap: 8px;
}

.topbar-player { flex-shrink: 0; min-width: 0; }

.topbar-profile-link {
    display: flex;
    align-items: center;
    gap: 7px;
    text-decoration: none;
    color: #444;
    font-weight: bold;
    font-size: .9em;
    min-width: 0;
}
.topbar-profile-link:hover { opacity: .85; }

.topbar-name {
    color: var(--pink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 110px;
}

.topbar-balance {
    color: #888;
    font-size: .85em;
    white-space: nowrap;
    flex-shrink: 0;
}

.topbar-signin-link {
    display: block;
    color: var(--blue);
    text-decoration: none;
    font-size: .88em;
    opacity: .8;
    white-space: nowrap;
}
.topbar-signin-link:hover { opacity: 1; }

.home-topbar .lang-picker {
    margin-bottom: 0;
    flex-shrink: 0;
}

/* ── ── */

.home-title {
    font-size: clamp(2em, 8vw, 3.2em);
    color: var(--pink);
    text-shadow: 3px 3px 0 rgba(0,0,0,.1);
    margin-bottom: 14px;
}

.home-subtitle {
    font-size: .95em;
    color: #666;
    margin-bottom: 8px;
}

.lang-picker {
    display: flex;
    gap: 6px;
    justify-content: center;
    margin-bottom: 20px;
}

.lang-btn {
    background: white;
    border: 2px solid transparent;
    border-radius: 10px;
    font-size: 1.2em;
    cursor: pointer;
    padding: 3px 6px;
    transition: border-color .15s, transform .1s;
    font-family: inherit;
    line-height: 1;
}
.lang-btn.active { border-color: var(--pink); }
.lang-btn:hover  { transform: scale(1.1); }

.cat-filter {
    display: flex;
    gap: 8px;
    justify-content: center;
    flex-wrap: wrap;
    margin-bottom: 14px;
}

.cat-btn {
    padding: 7px 18px;
    border: 2px solid #e8e8e8;
    border-radius: 20px;
    background: white;
    font-family: inherit;
    font-size: .88em;
    font-weight: bold;
    color: #999;
    cursor: pointer;
    transition: border-color .15s, color .15s, background .15s;
}
.cat-btn.active {
    border-color: var(--pink);
    color: var(--pink);
    background: #FFF0F6;
}
.cat-btn:hover:not(.active) { border-color: #ccc; color: #666; }

.games-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    max-width: 440px;
    margin: 0 auto;
}

.game-card {
    display: block;
    text-decoration: none;
    background: white;
    border-radius: 16px;
    padding: 16px 12px 14px;
    box-shadow: 0 4px 14px rgba(0,0,0,.09);
    border: 2px solid transparent;
    transition: transform .15s, box-shadow .15s, border-color .15s;
}
.game-card:hover, .game-card:active {
    transform: translateY(-3px) scale(1.02);
    box-shadow: 0 8px 22px rgba(0,0,0,.13);
    border-color: var(--pink);
}

.game-card-icon {
    font-size: 2.4em;
    line-height: 1;
    margin-bottom: 6px;
    color: #333;
}

.game-card-name {
    font-size: 1.1em;
    font-weight: bold;
    color: #333;
    margin-bottom: 3px;
}

.game-card-desc {
    font-size: .8em;
    color: #999;
    margin-bottom: 6px;
}

.game-card-best {
    font-size: .8em;
    color: var(--blue);
    font-weight: bold;
    min-height: 1.2em;
}

/* ── Teacher CTA ────────────────────────────────────────────────── */

.teacher-cta {
    margin-top: 28px;
    text-align: center;
}

.teacher-cta-btn {
    display: block;
    width: fit-content;
    max-width: calc(100% - 24px);
    margin: 0 auto;
    padding: 14px 32px;
    background: var(--pink);
    color: #fff;
    border-radius: 32px;
    font-size: 1.2em;
    font-weight: bold;
    text-decoration: none;
    transition: transform .1s, opacity .1s;
}

.teacher-cta-btn:hover, .teacher-cta-btn:active {
    transform: scale(1.04);
    opacity: .9;
}

.teacher-cta-label {
    margin-top: 8px;
    font-size: .8em;
    color: #aaa;
}

/* ── Game screens ───────────────────────────────────────────────── */

.screen { display: none; max-width: 760px; margin: 0 auto; }
.screen.active { display: block; }

#start-screen, #win-screen, #lose-screen {
    text-align: center;
    padding: 16px 10px;
}

/* Back link + sound button row on game start screens */
.start-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 14px;
}

.home-link {
    display: inline-block;
    color: var(--blue);
    text-decoration: none;
    font-size: .95em;
    opacity: .75;
}
.home-link:hover { opacity: 1; text-decoration: underline; }
.start-header .home-link { margin-bottom: 0; }

.start-title {
    font-size: clamp(1.6em, 7vw, 3em);
    color: var(--pink);
    text-shadow: 3px 3px 0 rgba(0,0,0,.1);
    margin-bottom: 12px;
}

.rules-box {
    background: white;
    border-radius: 16px;
    padding: 14px 20px;
    margin: 12px auto;
    max-width: 400px;
    box-shadow: 0 4px 15px rgba(0,0,0,.1);
    line-height: 2.1;
    font-size: 1em;
    color: #444;
}

.play-btn {
    display: inline-block;
    background: linear-gradient(135deg, var(--pink), var(--orange));
    color: white;
    border: none;
    padding: 16px 48px;
    font-size: 1.4em;
    font-family: inherit;
    border-radius: 50px;
    cursor: pointer;
    box-shadow: 0 6px 20px rgba(255,107,157,.4);
    transition: transform .1s, box-shadow .1s;
    margin: 18px 8px;    /* overridden to 0 inside .mode-btns */
}

.mode-btns .play-btn { margin: 0; }
.play-btn:hover, .play-btn:active {
    transform: scale(1.05);
    box-shadow: 0 8px 25px rgba(255,107,157,.5);
}

.play-btn--secondary {
    background: white;
    color: var(--blue);
    border: 3px solid var(--blue);
    box-shadow: 0 6px 20px rgba(77,150,255,.25);
}
.play-btn--secondary:hover, .play-btn--secondary:active {
    box-shadow: 0 8px 25px rgba(77,150,255,.35);
}
.play-btn--selected {
    outline: 4px solid var(--blue);
    outline-offset: 3px;
    transform: scale(1.05);
    box-shadow: 0 8px 25px rgba(77,150,255,.45);
}

.mode-btns {
    display: flex;
    gap: 14px;
    justify-content: center;
    flex-wrap: wrap;
    margin: 18px 8px;
}

.scores-section { margin-top: 16px; }
.scores-section h3 { color: var(--blue); font-size: 1.1em; margin-bottom: 8px; }

/* Keep scores table within the viewport — table scrolls internally, page does not */
.scores-section > div {
    max-height: 32vh;
    overflow-y: auto;
    scrollbar-width: none;
}
.scores-section > div::-webkit-scrollbar { display: none; }

.scores-table {
    width: 100%;
    max-width: 420px;
    margin: 0 auto;
    border-collapse: collapse;
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,.08);
    font-size: .88em;
}
.scores-table th {
    background: var(--blue);
    color: white;
    padding: 8px 10px;
    position: sticky;
    top: 0;
    z-index: 2;
}
.scores-table thead tr th:first-child { border-radius: 12px 0 0 0; }
.scores-table thead tr th:last-child  { border-radius: 0 12px 0 0; }
.scores-table td { padding: 7px 10px; border-bottom: 1px solid #f0f0f0; text-align: center; color: #555; }
.scores-table tr:last-child td { border-bottom: none; }

/* ── Game header ────────────────────────────────────────────────── */

/* Push grid below the fixed header */
#game-screen { padding-top: 72px; }

#game-header {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    padding: 8px 14px;
    background: white;
    border-radius: 16px;
    box-shadow: 0 4px 12px rgba(0,0,0,.08);
    margin-bottom: 10px;
    gap: 8px;
    /* Fixed to viewport — body scrolls underneath */
    position: fixed;
    top: 12px;           /* matches body padding */
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 24px);
    max-width: 736px;    /* 760px screen max minus 2×12px padding */
    z-index: 100;
}

/* ── Live top-3 bar (session mode) ─────────────────────────────── */

#live-top3 {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    padding-top: 6px;
    border-top: 1px solid #f0f0f0;
    font-size: .8em;
    color: #555;
}

.top3-entry { white-space: nowrap; }
.top3-me    { font-weight: bold; color: var(--pink); }
.top3-placeholder { color: #aaa; font-style: italic; }

/* Lead-change toast (session mode) */
.lead-toast {
    position: fixed;
    top: 90px;
    left: 50%;
    transform: translateX(-50%);
    background: linear-gradient(135deg, #FFE5F0, #FFF3B0);
    border: 2px solid var(--orange);
    border-radius: 24px;
    padding: 10px 24px;
    font-size: 1.1em;
    font-weight: bold;
    color: #333;
    z-index: 200;
    animation: lead-toast-anim 3s ease-out forwards;
    white-space: nowrap;
    box-shadow: 0 4px 16px rgba(0,0,0,.15);
    pointer-events: none;
}

@keyframes lead-toast-anim {
    0%   { opacity: 0; transform: translateX(-50%) translateY(-8px); }
    10%  { opacity: 1; transform: translateX(-50%) translateY(0); }
    75%  { opacity: 1; }
    100% { opacity: 0; }
}

.home-link-small {
    color: var(--blue);
    text-decoration: none;
    font-size: .85em;
    opacity: .65;
    white-space: nowrap;
    flex-shrink: 0;
}
.home-link-small:hover { opacity: 1; }

.timer-wrap {
    display: flex;
    align-items: baseline;
    gap: 5px;
    white-space: nowrap;
}

#timer {
    font-size: clamp(1.2em, 5vw, 1.8em);
    font-weight: bold;
    color: #333;
    transition: color .3s;
    white-space: nowrap;
}
#timer.warning { color: var(--orange); }
#timer.danger  { color: #e63946; animation: pulse 1s infinite; }

.timer-bonus {
    font-size: .75em;
    font-weight: bold;
    color: var(--green);
    opacity: 0;
}
.timer-bonus.playing    { animation: bonus-pop 1.4s ease-out forwards; }
.timer-bonus.penalizing { animation: bonus-pop 1.0s ease-out forwards; color: #e63946; }

@keyframes bonus-pop {
    0%   { opacity: 0; transform: translateY(4px); }
    15%  { opacity: 1; transform: translateY(-5px); }
    70%  { opacity: 1; transform: translateY(-5px); }
    100% { opacity: 0; transform: translateY(-14px); }
}

#caramelo-count {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: clamp(.85em, 3vw, 1.1em);
    color: #555;
    white-space: nowrap;
    flex-shrink: 0;
}

/* ── Grid ───────────────────────────────────────────────────────── */

#grid { display: grid; gap: 5px; }

.cell {
    background: white;
    border: 2px solid #e0e0e0;
    border-radius: 10px;
    padding: 5px 3px;
    text-align: center;
    transition: background .25s, border-color .25s;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    animation: cell-pop-in .22s cubic-bezier(.34,1.56,.64,1) both;
}
.cell.active-cell { border-color: var(--blue); box-shadow: 0 0 0 2px rgba(77,150,255,.3); }
.cell.correct     { background: #C8F7C5; border-color: #4CAF50; }
.cell.wrong       { background: #FFB3B3; border-color: #e63946; animation: shake .4s; }
.cell.just-correct { animation: pop .3s; }

.problem-text {
    font-size: clamp(.7em, 2.5vw, .9em);
    font-weight: bold;
    color: #444;
    white-space: nowrap;
}

.cell-input {
    width: 100%;
    max-width: 46px;
    font-size: clamp(.85em, 3.2vw, 1.1em);
    font-family: inherit;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 6px;
    padding: 3px 2px;
    background: #fafafa;
    outline: none;
    -moz-appearance: textfield;
    appearance: textfield;
}
.cell-input::-webkit-outer-spin-button,
.cell-input::-webkit-inner-spin-button { -webkit-appearance: none; }
.cell-input:focus { border-color: var(--blue); background: white; }
.cell.correct .cell-input { display: none; }

.correct-mark {
    display: none;
    justify-content: center;
    align-items: center;
    gap: 5px;
    min-height: 18px;
}
.cell.correct .correct-mark { display: flex; }
.cell.wrong   .correct-mark { display: flex; }
.correct-mark .caramelo-piece { width: 14px; height: 18px; }
.correct-mark .caramelo-piece::after { width: 4px; height: 4px; }

.answer-value {
    font-size: .8em;
    font-weight: bold;
    color: #2e7d32;
}

.correct-answer {
    font-size: .72em;
    font-weight: bold;
    color: #1e7e34;
    background: rgba(107,203,119,.25);
    border-radius: 4px;
    padding: 1px 5px;
    white-space: nowrap;
}

/* ── Difficulty slider ──────────────────────────────────────────── */

.difficulty-slider-wrap {
    display: flex;
    align-items: center;
    gap: 14px;
    justify-content: center;
    margin: 16px 8px 4px;
}

.diff-emoji { font-size: 2em; line-height: 1; user-select: none; }

.difficulty-slider {
    -webkit-appearance: none;
    appearance: none;
    width: 130px;
    height: 8px;
    border-radius: 4px;
    background: linear-gradient(to right, var(--green) 0%, var(--orange) 50%, #e63946 100%);
    outline: none;
    cursor: pointer;
}

.difficulty-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: white;
    border: 3px solid var(--pink);
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,.2);
    transition: transform .12s;
}
.difficulty-slider::-webkit-slider-thumb:hover { transform: scale(1.2); }

.difficulty-slider::-moz-range-thumb {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: white;
    border: 3px solid var(--pink);
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,.2);
}

/* ── Practice-mode end banner (shown on start screen after time runs out) ── */

.practice-result {
    background: linear-gradient(135deg, rgba(255,107,157,.1), rgba(255,154,60,.1));
    border: 2px solid var(--pink);
    border-radius: 14px;
    padding: 10px 18px;
    margin: 0 auto 12px;
    max-width: 400px;
    font-size: 1.05em;
    font-weight: bold;
    color: #444;
}

/* ── Test-mode submit ───────────────────────────────────────────── */

.submit-area {
    text-align: center;
    padding: 14px 0 4px;
}

/* ── Result screens ─────────────────────────────────────────────── */

.result-emoji { font-size: 5em; display: block; margin: 8px 0; }
.result-title { font-size: clamp(2em, 8vw, 3em); margin: 8px 0; }
.result-title.win  { color: var(--green); }
.result-title.lose { color: var(--orange); }
.result-message { font-size: 1.1em; color: #555; margin: 10px 0 18px; line-height: 1.7; }

/* ── Animations ─────────────────────────────────────────────────── */

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50%       { transform: scale(1.12); }
}

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

@keyframes pop {
    0%, 100% { transform: scale(1); }
    50%       { transform: scale(1.12); }
}

@keyframes exclamation-pop {
    0%   { opacity: 0; transform: translate(-50%, -50%) scale(.6); }
    25%  { opacity: 1; transform: translate(-50%, -50%) scale(1.08); }
    45%  { transform: translate(-50%, -50%) scale(1); }
    72%  { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(-50%, -72%) scale(.85); }
}

.row-exclamation {
    position: fixed;
    top: 42%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: clamp(1.8em, 7vw, 2.8em);
    font-weight: bold;
    color: var(--pink);
    text-shadow: 2px 2px 0 rgba(0,0,0,.12);
    pointer-events: none;
    z-index: 200;
    white-space: nowrap;
    animation: exclamation-pop 1.6s ease-out forwards;
}

@keyframes capybara-enter {
    0%   { transform: translateX(110vw) rotate(-10deg); }
    70%  { transform: translateX(-15px) rotate(5deg); }
    100% { transform: translateX(0) rotate(0deg); }
}

@keyframes candy-fall {
    to { transform: translateY(110vh) rotate(720deg); }
}

.capybara-anim {
    display: inline-block;
    font-size: 5em;
    animation: capybara-enter .9s cubic-bezier(.22,.61,.36,1) both;
}

.candy-rain {
    position: fixed;
    inset: 0;
    pointer-events: none;
    overflow: hidden;
    z-index: 999;
}

/* Falling candy pieces in win animation */
.candy-fall {
    position: absolute;
    top: -24px;
    width: 14px;
    height: 18px;
    border-radius: 50% 50% 45% 45% / 55% 55% 45% 45%;
    animation: candy-fall linear forwards;
}
.candy-fall::after {
    content: '';
    position: absolute;
    top: 3px; left: 3px;
    width: 4px; height: 4px;
    background: rgba(255,255,255,.3);
    border-radius: 50%;
}

/* ── Page-clear explosion (Beat the Clock) ──────────────────────── */

@keyframes cell-pop-in {
    from { transform: scale(0.55); opacity: 0; }
    to   { transform: scale(1);    opacity: 1; }
}

@keyframes cell-burst {
    0%   { transform: scale(1);    opacity: 1; }
    35%  { transform: scale(1.22); opacity: 1; }
    100% { transform: scale(0.05); opacity: 0; }
}

.cell.exploding {
    animation: cell-burst .4s ease-in forwards;
    pointer-events: none;
}

/* ── Compact cells when mobile keyboard is visible ──────────────── */
/* The keyboard shrinks the viewport to ~350-450 px on most phones. */

@media (max-height: 520px) {
    /* Compact game screen when mobile keyboard is visible */
    #game-header { padding: 5px 10px; margin-bottom: 6px; border-radius: 10px; }
    .cell        { padding: 2px 3px; gap: 1px; border-radius: 7px; }
    .problem-text { font-size: .65em; }
    .cell-input  { font-size: .8em; padding: 2px 1px; max-width: 38px; border-radius: 4px; }
    .correct-mark .caramelo-piece { width: 10px; height: 13px; }
    .submit-area { padding: 6px 0 2px; }
    .submit-area .play-btn { padding: 10px 30px; font-size: 1.1em; }

    /* 3-column grid on short viewports (landscape / small phones) */
    .home-page      { padding: 6px 10px; }
    .home-topbar    { padding: 5px 8px; margin-bottom: 10px; border-radius: 12px; }
    .home-title     { font-size: clamp(1.1em, 5vw, 1.8em); margin-bottom: 6px; }
    .home-subtitle  { font-size: .85em; margin-bottom: 4px; }
    .lang-btn       { font-size: 1em; padding: 2px 5px; }
    .cat-btn       { padding: 5px 13px; font-size: .78em; }
    .games-grid    { grid-template-columns: repeat(3, 1fr); gap: 6px; }
    .game-card     { padding: 8px 6px 7px; border-radius: 11px; }
    .game-card-icon { font-size: 1.7em; margin-bottom: 3px; }
    .game-card-name { font-size: .88em; margin-bottom: 1px; }
    .game-card-desc { font-size: .66em; margin-bottom: 3px; }
    .game-card-best { font-size: .66em; }
    .teacher-cta    { margin-top: 10px; }
    .teacher-cta-btn { padding: 9px 24px; font-size: 1.05em; }
    .teacher-cta-label { font-size: .72em; margin-top: 4px; }
}
