:root {
    color-scheme: light;
    --bg: #F6F8FC;
    --surface: #FFFFFF;
    --surface-strong: #EAF1FF;
    --ink: #172033;
    --muted: #667085;
    --primary: #2563EB;
    --primary-strong: #1749B8;
    --accent: #F59E0B;
    --line: #CAD5E8;
    --correct: #15803D;
    --wrong: #B42318;
    --warning: #B45309;
    --note: #FFF7D6;
    --result: #E6F7EE;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html,
body {
    margin: 0;
    min-height: 100%;
    background: var(--bg);
    color: var(--ink);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body {
    min-height: 100vh;
    padding: 12px;
    touch-action: manipulation;
}

button {
    min-height: 52px;
    border: 0;
    border-radius: 8px;
    font: inherit;
    font-weight: 800;
    cursor: pointer;
}

button:focus-visible,
canvas:focus-visible {
    outline: 4px solid rgba(37, 99, 235, 0.35);
    outline-offset: 3px;
}

.app-header,
.app-shell,
.task-panel,
.action-bar,
.feedback-panel {
    width: min(100%, 880px);
    margin-inline: auto;
}

.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 10px;
}

.eyebrow,
.panel-label {
    margin: 0 0 4px;
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 900;
    letter-spacing: 0;
    text-transform: uppercase;
}

h1 {
    margin: 0;
    font-size: clamp(1.8rem, 4vw, 2.8rem);
    line-height: 1;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

.mode-switch,
.level-switch,
.task-actions,
.action-bar {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.mode-button,
.level-button,
.tool-button {
    padding: 0 14px;
    background: var(--surface);
    color: var(--ink);
    border: 2px solid var(--line);
}

.level-button {
    width: 48px;
    padding: 0;
}

.header-controls .mode-switch,
.header-controls .level-switch {
    flex-wrap: nowrap;
    gap: 6px;
}

.mode-button.is-active,
.level-button.is-active {
    background: var(--primary);
    border-color: var(--primary);
    color: #FFFFFF;
}

.primary-button {
    min-width: 160px;
    padding: 0 22px;
    background: var(--primary);
    color: #FFFFFF;
}

.primary-button:active,
.mode-button.is-active:active {
    background: var(--primary-strong);
}

.tool-button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.app-shell {
    display: grid;
    gap: 10px;
}

.task-panel,
.feedback-panel {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    background: var(--surface);
    border: 2px solid var(--line);
    border-radius: 8px;
}

.task-line {
    min-height: 36px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    font-size: clamp(1.4rem, 3vw, 2.1rem);
    font-weight: 900;
    line-height: 1.1;
}

.workspace {
    overflow-x: auto;
    padding: 2px 0;
}

.arithmetic-grid {
    width: max-content;
    max-width: 100%;
    margin-inline: auto;
    display: grid;
    grid-template-columns: 52px repeat(var(--grid-cols), 80px);
    gap: 6px;
}

.grid-separator {
    grid-column: 1 / -1;
    height: 8px;
    border-top: 6px double var(--ink);
    align-self: center;
    margin: 1px 0 -1px;
}

.column-label,
.row-label {
    display: grid;
    place-items: center;
    min-height: 30px;
    color: var(--muted);
    font-weight: 900;
    font-size: 0.8rem;
}

.row-label {
    min-height: 80px;
    color: var(--ink);
}

.row-label.addend {
    color: var(--primary);
}

.row-label.note {
    background: var(--note);
    border-radius: 8px;
}

.row-label.result {
    background: var(--result);
    border-radius: 8px;
}

.cell {
    position: relative;
    min-height: 80px;
    aspect-ratio: 1;
    border: 0;
    border-radius: 8px;
    background: var(--surface);
    box-shadow: inset 0 0 0 2px var(--line);
    overflow: hidden;
}

.cell.note {
    background: var(--note);
}

.cell.result {
    background: var(--result);
    box-shadow: inset 0 0 0 2px #8AD6A8;
}

.cell.is-active {
    box-shadow: inset 0 0 0 2px var(--primary), 0 0 0 4px rgba(37, 99, 235, 0.16);
}

.cell.correct {
    background: #ECFDF3;
    box-shadow: inset 0 0 0 2px var(--correct);
}

.cell.wrong {
    background: #FEF3F2;
    box-shadow: inset 0 0 0 2px var(--wrong);
}

.cell.warning {
    background: #FFF7ED;
    box-shadow: inset 0 0 0 2px var(--warning);
}

.cell.missing {
    background: #FFFBEB;
    box-shadow: inset 0 0 0 2px var(--accent);
}

.cell canvas {
    display: block;
    width: 100%;
    height: 100%;
    touch-action: none;
}

.cell-marker {
    position: absolute;
    right: 5px;
    top: 3px;
    min-width: 24px;
    min-height: 24px;
    display: grid;
    place-items: center;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.88);
    color: var(--ink);
    font-size: 0.9rem;
    font-weight: 900;
    pointer-events: none;
}

.action-bar {
    justify-content: center;
    padding-block: 0;
}

.feedback-panel {
    align-items: start;
}

.feedback-summary {
    margin: 0;
    font-size: 1.35rem;
    font-weight: 900;
}

.recognized-matrix {
    display: grid;
    gap: 4px;
}

.matrix-row {
    display: grid;
    grid-template-columns: 54px repeat(4, 34px);
    gap: 4px;
    align-items: center;
}

.matrix-label {
    color: var(--muted);
    font-size: 0.8rem;
    font-weight: 900;
}

.matrix-cell {
    min-height: 32px;
    display: grid;
    place-items: center;
    border-radius: 6px;
    background: var(--surface-strong);
    font-weight: 900;
}

.matrix-cell.warning {
    background: #FFF1C2;
    color: var(--warning);
}

.matrix-cell.empty {
    color: #98A2B3;
}

.confetti-layer {
    position: fixed;
    inset: 0;
    z-index: 1000;
    overflow: hidden;
    pointer-events: none;
}

.confetti-flake {
    position: absolute;
    left: var(--start-x);
    top: var(--start-y);
    width: var(--flake-width);
    height: var(--flake-height);
    border-radius: 2px;
    background: var(--flake-color);
    box-shadow: 0 0 10px rgba(245, 158, 11, 0.85);
    opacity: 0;
    transform: translate(-50%, -50%) rotate(var(--rotate-start));
    animation: golden-confetti var(--duration) cubic-bezier(0.12, 0.75, 0.18, 1) var(--delay) forwards;
}

@keyframes golden-confetti {
    0% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(0.75) rotate(var(--rotate-start));
    }

    42% {
        opacity: 1;
        transform: translate(calc(-50% + var(--burst-x)), calc(-50% + var(--burst-y))) scale(1) rotate(calc(var(--rotate-start) + 420deg));
    }

    100% {
        opacity: 0;
        transform: translate(calc(-50% + var(--fall-x)), calc(-50% + var(--fall-y))) scale(0.9) rotate(calc(var(--rotate-start) + 980deg));
    }
}

@media (max-width: 680px) {
    body {
        padding: 10px;
    }

    .app-header,
    .task-panel,
    .feedback-panel {
        align-items: stretch;
        flex-direction: column;
    }

    .header-controls {
        display: grid;
        justify-items: stretch;
    }

    .mode-switch,
    .level-switch,
    .task-actions,
    .action-bar {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
    }

    .mode-switch,
    .task-actions {
        grid-template-columns: 1fr 1fr;
    }

    .level-button {
        width: auto;
    }

    .primary-button {
        grid-column: 1 / -1;
        width: 100%;
    }

    .arithmetic-grid {
        grid-template-columns: 52px repeat(var(--grid-cols), 80px);
        gap: 5px;
    }

    .row-label,
    .cell {
        min-height: 80px;
    }
}
