:root {
    color-scheme: light;
    --bg: #f6f2ea;
    --surface: #fffdf8;
    --surface-soft: #faf6ef;
    --surface-strong: #f0e8db;
    --text: #182230;
    --muted: #667085;
    --line: #ded5c7;
    --line-strong: #cbbdad;
    --primary: #286f72;
    --primary-dark: #1f595c;
    --primary-soft: #e5f2f0;
    --accent: #b66c24;
    --accent-soft: #f7eadc;
    --success: #18794e;
    --success-soft: #e7f5ed;
    --danger: #b54135;
    --danger-soft: #fae7e3;
    --warning: #8a5b00;
    --warning-soft: #fff1ce;
    --shadow: 0 10px 28px rgba(31, 41, 55, 0.09);
    --shadow-soft: 0 2px 10px rgba(31, 41, 55, 0.06);
    --radius-sm: 10px;
    --radius: 16px;
    --radius-lg: 22px;
    --focus: 0 0 0 4px rgba(40, 111, 114, 0.18);
}

[data-theme="dark"] {
    color-scheme: dark;
    --bg: #101820;
    --surface: #1b2630;
    --surface-soft: #222f3a;
    --surface-strong: #2d3b47;
    --text: #eef4f7;
    --muted: #aab7c2;
    --line: #3b4854;
    --line-strong: #4f5e6b;
    --primary: #78c9c4;
    --primary-dark: #a5dfdb;
    --primary-soft: rgba(120, 201, 196, 0.13);
    --accent: #e4a15c;
    --accent-soft: rgba(228, 161, 92, 0.14);
    --success: #7bd99d;
    --success-soft: rgba(123, 217, 157, 0.13);
    --danger: #ff9c8e;
    --danger-soft: rgba(255, 156, 142, 0.13);
    --warning: #f2c66d;
    --warning-soft: rgba(242, 198, 109, 0.15);
    --shadow: 0 12px 34px rgba(0, 0, 0, 0.26);
    --shadow-soft: 0 2px 12px rgba(0, 0, 0, 0.18);
    --focus: 0 0 0 4px rgba(120, 201, 196, 0.2);
}

* {
    box-sizing: border-box;
}

html {
    min-height: 100%;
    scroll-behavior: smooth;
}

body {
    min-height: 100vh;
    margin: 0;
    font-family: Inter, "Segoe UI", Roboto, Arial, sans-serif;
    color: var(--text);
    background: var(--bg);
}

button,
select,
input {
    font: inherit;
}

button,
select {
    -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
select:focus-visible,
input:focus-visible,
summary:focus-visible {
    outline: none;
    box-shadow: var(--focus);
}

#celebration-canvas {
    position: fixed;
    inset: 0;
    z-index: 30;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.app-shell {
    width: min(1120px, calc(100% - 28px));
    margin: 0 auto;
    padding: 18px 0 28px;
}

.topbar,
.session-panel,
.task-header,
.puzzle-card,
.actions-bar,
.result-summary,
.solution-panel,
.teacher-note {
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow-soft);
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 14px 16px;
    margin-bottom: 14px;
}

.title-block {
    min-width: 0;
}

.title-block h1 {
    margin: 0;
    font-size: clamp(1.4rem, 3vw, 1.95rem);
    line-height: 1.08;
    letter-spacing: -0.03em;
}

.title-block p {
    margin: 5px 0 0;
    color: var(--muted);
    font-size: 0.95rem;
    line-height: 1.4;
}

.back-link {
    display: inline-block;
    margin-bottom: 8px;
    color: var(--primary);
    text-decoration: none;
    font-size: 0.85rem;
    font-weight: 800;
    transition: color 0.15s ease;
}

.back-link:hover {
    color: var(--primary-dark);
}

.top-actions {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: flex-end;
}

.toolbar-button {
    min-height: 40px;
    padding: 0 14px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface-soft);
    color: var(--text);
    font-weight: 800;
    cursor: pointer;
    transition: transform 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.toolbar-button:hover {
    transform: translateY(-1px);
    border-color: var(--primary);
}

.toolbar-button-secondary {
    background: var(--surface);
}

.main-layout {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.session-panel {
    display: grid;
    grid-template-columns: minmax(0, 1.15fr) minmax(260px, 0.85fr);
    gap: 14px;
    align-items: stretch;
    padding: 14px;
}

.settings-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: end;
}

.settings-row label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 140px;
    color: var(--muted);
    font-size: 0.8rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.select {
    min-height: 44px;
    padding: 0 38px 0 12px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--surface-soft);
    color: var(--text);
    cursor: pointer;
}

.button {
    min-height: 44px;
    padding: 0 16px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-weight: 800;
    color: var(--text);
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}

.button:hover:not(:disabled) {
    transform: translateY(-1px);
}

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

.button-primary {
    background: var(--primary);
    color: #fff;
    box-shadow: 0 8px 18px rgba(40, 111, 114, 0.25);
}

.button-primary:hover:not(:disabled) {
    background: var(--primary-dark);
}

.button-secondary {
    background: var(--surface-soft);
    border-color: var(--line);
}

.button-ghost {
    background: var(--surface);
    border-color: var(--line);
}

.progress-area {
    padding: 12px 14px;
    border-radius: var(--radius);
    background: var(--surface-soft);
    border: 1px solid var(--line);
}

.progress-line,
.mini-stats {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    align-items: center;
}

.progress-line {
    font-weight: 800;
    color: var(--primary-dark);
}

.progress-track {
    width: 100%;
    height: 12px;
    margin: 12px 0 10px;
    border-radius: 999px;
    background: var(--surface-strong);
    overflow: hidden;
    border: 1px solid var(--line);
}

.progress-fill {
    width: 0;
    height: 100%;
    background: linear-gradient(90deg, var(--primary), color-mix(in srgb, var(--primary) 68%, white));
    transition: width 0.2s ease;
}

.mini-stats {
    color: var(--muted);
    font-weight: 700;
}

.task-header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: end;
    padding: 12px 16px;
}

.eyebrow {
    margin: 0 0 4px;
    color: var(--primary);
    font-size: 0.78rem;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: 0.12em;
}

.task-header h2 {
    margin: 0;
    font-size: 1.42rem;
    line-height: 1.1;
}

.task-header p:not(.eyebrow) {
    margin: 0;
    color: var(--muted);
    font-size: 0.95rem;
    text-align: right;
}

.puzzles-container {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}

.puzzle-card {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px;
    animation: cardIn 0.28s ease both;
}

.puzzle-card[data-status="correct"] {
    border-color: rgba(24, 121, 78, 0.45);
}

.puzzle-card[data-status="wrong"] {
    border-color: rgba(181, 65, 53, 0.45);
}

.puzzle-header {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 12px;
    align-items: start;
}

.puzzle-number {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border-radius: 12px;
    background: var(--primary);
    color: #fff;
    font-weight: 900;
    line-height: 1;
}

.puzzle-title {
    margin: 0;
    font-size: 1.08rem;
    line-height: 1.25;
    letter-spacing: -0.015em;
}

.puzzle-desc {
    margin: 4px 0 0;
    color: var(--muted);
    font-size: 0.92rem;
    line-height: 1.42;
}

.skill-pill {
    align-self: start;
    max-width: 130px;
    padding: 6px 10px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--primary-soft);
    color: var(--primary-dark);
    font-size: 0.78rem;
    font-weight: 900;
    text-align: center;
    white-space: nowrap;
}

.puzzle-body {
    display: grid;
    place-items: center;
    min-height: 170px;
    padding: 10px;
    border-radius: var(--radius);
    background: var(--surface-soft);
}

.game-input {
    width: 100%;
    height: 100%;
    min-width: 0;
    border: 2px solid var(--line-strong);
    border-radius: inherit;
    background: var(--surface);
    color: var(--primary-dark);
    text-align: center;
    font-size: clamp(1.1rem, 3vw, 1.35rem);
    font-weight: 900;
    outline: none;
    transition: border-color 0.16s ease, background 0.16s ease, transform 0.16s ease;
}

.game-input:focus {
    border-color: var(--primary);
    transform: scale(1.02);
}

.game-input.correct,
.choice-answer.correct {
    border-color: var(--success);
    background: var(--success-soft);
    color: var(--success);
}

.game-input.wrong,
.choice-answer.wrong {
    border-color: var(--danger);
    background: var(--danger-soft);
    color: var(--danger);
    animation: shake 0.36s ease;
}

.hint-box {
    display: none;
    padding: 10px 12px;
    border: 1px solid rgba(138, 91, 0, 0.24);
    border-radius: var(--radius-sm);
    background: var(--warning-soft);
    color: var(--warning);
    font-size: 0.92rem;
    line-height: 1.45;
}

.hint-box:not(:empty) {
    display: block;
}

.puzzle-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-top: auto;
}

.puzzle-footer .button {
    min-height: 38px;
    padding: 0 12px;
    font-size: 0.9rem;
}

.inline-feedback {
    min-height: 1.25rem;
    color: var(--muted);
    font-size: 0.9rem;
    font-weight: 800;
    text-align: right;
}

.inline-feedback.good {
    color: var(--success);
}

.inline-feedback.bad {
    color: var(--danger);
}

.number-block,
.symbol-token,
.choice-answer,
.choice-button,
.machine-box,
.operation-box {
    display: grid;
    place-items: center;
    min-width: 58px;
    min-height: 58px;
    border: 2px solid var(--line);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-size: 1.22rem;
    font-weight: 900;
    box-shadow: 0 1px 0 rgba(31, 41, 55, 0.06);
}

.input-holder {
    padding: 0;
    border-style: dashed;
    border-color: var(--primary);
    overflow: hidden;
}

.input-holder .game-input {
    border: 0;
}

.sequence {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 8px;
}

.seq-node {
    display: grid;
    place-items: center;
    width: 58px;
    height: 58px;
    border: 2px solid rgba(40, 111, 114, 0.24);
    border-radius: 50%;
    background: var(--primary-soft);
    color: var(--primary-dark);
    font-size: 1.12rem;
    font-weight: 900;
}

.seq-node.input-holder {
    padding: 0;
    border-style: dashed;
    border-color: var(--primary);
    background: var(--surface);
    overflow: hidden;
}

.seq-node.input-holder .game-input {
    border-radius: 50%;
}

.seq-arrow {
    color: var(--muted);
    font-weight: 900;
}

.pyramid {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    width: min(100%, 300px);
    margin-inline: auto;
}

.pyramid-row {
    display: flex;
    justify-content: center;
    gap: 10px;
    width: 100%;
}

.pyramid .number-block,
.pyramid .input-holder {
    width: 74px;
    height: 74px;
    min-width: 74px;
    min-height: 74px;
    flex: 0 0 74px;
}

.symbol-board,
.equation-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: min(100%, 420px);
}

.equation-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 10px;
    align-items: center;
    padding: 8px;
    border-radius: var(--radius-sm);
    background: var(--surface);
    border: 1px solid var(--line);
}

.equation-row.target-row {
    border-color: rgba(40, 111, 114, 0.38);
    background: var(--primary-soft);
}

.equation-expression {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.equation-row .input-holder {
    width: 70px;
    height: 50px;
}

.symbol-token {
    min-width: 38px;
    min-height: 38px;
    border-radius: 50%;
    font-size: 1.1rem;
    background: var(--accent-soft);
    border-color: rgba(182, 108, 36, 0.28);
    color: var(--accent);
}

.magic-square {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    width: min(100%, 260px);
    aspect-ratio: 1;
    padding: 8px;
    border: 2px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-strong);
}

.magic-cell {
    display: grid;
    place-items: center;
    border: 2px solid var(--line);
    border-radius: var(--radius-sm);
    background: var(--surface);
    font-size: clamp(1.12rem, 4vw, 1.55rem);
    font-weight: 900;
}

.magic-cell.input-holder {
    padding: 0;
    border-color: var(--primary);
    border-style: dashed;
}

.balance-board {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    width: min(100%, 460px);
}

.balance-equation {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 8px;
    width: 100%;
}

.choice-answer {
    border-style: dashed;
    border-color: var(--primary);
    color: var(--muted);
    min-width: 64px;
}

.choice-answer.filled {
    border-style: solid;
    background: var(--primary-soft);
    color: var(--primary-dark);
}

.choice-list {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 8px;
}

.choice-button {
    min-width: 58px;
    min-height: 44px;
    border-radius: 999px;
    background: var(--surface);
    cursor: pointer;
    color: var(--accent);
    border-color: rgba(182, 108, 36, 0.3);
}

.choice-button:hover,
.choice-button.selected {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.choice-button.used {
    opacity: 0.42;
}

.machine {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

.operation-box {
    min-width: 86px;
    background: var(--accent-soft);
    color: var(--accent);
    border-color: rgba(182, 108, 36, 0.28);
}

.machine .input-holder {
    width: 76px;
    height: 58px;
}

.actions-bar {
    position: sticky;
    bottom: 10px;
    z-index: 10;
    display: flex;
    gap: 10px;
    align-items: center;
    padding: 10px;
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    box-shadow: var(--shadow);
    backdrop-filter: blur(10px);
}

.actions-bar .button {
    flex: 1;
}

.result-summary {
    padding: 14px 16px;
    font-weight: 800;
    line-height: 1.5;
}

.result-summary.success {
    background: var(--success-soft);
    border-color: rgba(24, 121, 78, 0.3);
    color: var(--success);
}

.result-summary.warning {
    background: var(--warning-soft);
    border-color: rgba(138, 91, 0, 0.28);
    color: var(--warning);
}

.result-summary.error {
    background: var(--danger-soft);
    border-color: rgba(181, 65, 53, 0.26);
    color: var(--danger);
}

.solution-panel {
    padding: 16px;
}

.solution-panel h2 {
    margin: 0 0 12px;
    font-size: 1.2rem;
}

.solution-list {
    margin: 0;
    padding-left: 20px;
    display: grid;
    gap: 10px;
    line-height: 1.55;
}

.teacher-note {
    overflow: hidden;
}

.teacher-note summary {
    padding: 14px 16px;
    font-weight: 800;
    cursor: pointer;
}

.teacher-note summary::marker,
.teacher-note summary::-webkit-details-marker {
    color: var(--primary);
}

.teacher-note > div {
    padding: 0 16px 16px;
    color: var(--muted);
    line-height: 1.6;
}

.teacher-note p {
    margin: 0;
}

.teacher-note p + p {
    margin-top: 8px;
}

.noscript-message {
    width: min(1120px, calc(100% - 28px));
    margin: 16px auto;
    padding: 14px 16px;
    border: 1px solid rgba(181, 65, 53, 0.26);
    border-radius: var(--radius);
    background: var(--danger-soft);
    color: var(--danger);
    font-weight: 800;
}

.app-footer {
    margin-top: 24px;
    padding: 20px 0;
    text-align: center;
    border-top: 1px solid var(--line);
    color: var(--muted);
    font-size: 0.9rem;
}

.app-footer a {
    color: var(--primary);
    text-decoration: none;
    font-weight: 700;
}

.app-footer a:hover {
    text-decoration: underline;
}

[hidden] {
    display: none !important;
}

@keyframes cardIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes shake {
    0%, 100% {
        transform: translateX(0);
    }
    25% {
        transform: translateX(-5px);
    }
    50% {
        transform: translateX(5px);
    }
    75% {
        transform: translateX(-3px);
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        scroll-behavior: auto !important;
        transition-duration: 0.01ms !important;
    }
}

@media (max-width: 940px) {
    .session-panel {
        grid-template-columns: 1fr;
    }

    .puzzles-container {
        grid-template-columns: 1fr;
    }

    .task-header {
        align-items: start;
        flex-direction: column;
        gap: 8px;
    }

    .task-header p:not(.eyebrow) {
        text-align: left;
    }
}

@media (max-width: 720px) {
    .topbar {
        flex-direction: column;
        align-items: stretch;
    }

    .top-actions {
        justify-content: flex-start;
    }

    .toolbar-button {
        min-width: 120px;
    }
}

@media (max-width: 640px) {
    .app-shell {
        width: calc(100% - 18px);
        padding-top: 10px;
    }

    .topbar,
    .session-panel,
    .task-header,
    .puzzle-card,
    .actions-bar,
    .result-summary,
    .solution-panel,
    .teacher-note {
        border-radius: 16px;
    }

    .topbar {
        padding: 12px;
        gap: 10px;
    }

    .settings-row label {
        min-width: min(100%, 160px);
        flex: 1 1 140px;
    }

    .mini-stats,
    .progress-line {
        font-size: 0.95rem;
    }

    .puzzle-header {
        grid-template-columns: auto minmax(0, 1fr);
    }

    .skill-pill {
        grid-column: 2;
        justify-self: start;
    }

    .pyramid .number-block,
    .pyramid .input-holder {
        width: 64px;
        height: 64px;
        min-width: 64px;
        min-height: 64px;
        flex-basis: 64px;
    }

    .actions-bar {
        flex-direction: column;
    }

    .actions-bar .button {
        width: 100%;
    }
}

@media (max-width: 420px) {
    .puzzle-footer {
        flex-direction: column;
        align-items: stretch;
    }

    .inline-feedback {
        text-align: left;
    }

    .top-actions {
        width: 100%;
    }

    .toolbar-button {
        flex: 1;
        justify-content: center;
    }
}
