:root {
    --game-primary: #c90c0f;
    --game-secondary: #f4c430;
    --sq-ink: #14121f;
    --sq-paper: #fffdf7;
    --sq-gold: #f4c430;
    --sq-red: #c90c0f;
    --sq-green: #15803d;
}

body.sq-page {
    margin: 0;
    min-height: 100vh;
    box-sizing: border-box;
    font-family: "Rockwell", "Rockwell Nova", "Roboto Slab", Georgia, serif;
    color: #f3efe2;
    overflow-x: hidden;
    background:
        linear-gradient(rgba(255, 255, 255, 0.035) 1px, transparent 1px) 0 0 / 28px 28px,
        linear-gradient(90deg, rgba(255, 255, 255, 0.035) 1px, transparent 1px) 0 0 / 28px 28px,
        radial-gradient(circle at 12% 0%, rgba(244, 196, 48, 0.2), transparent 38%),
        radial-gradient(circle at 92% 12%, rgba(201, 12, 15, 0.24), transparent 42%),
        linear-gradient(180deg, #102a43 0%, #1b1f4b 52%, #0d1026 100%);
    background-attachment: fixed;
}

body.sq-page.game-menu-active {
    overflow-y: auto;
}

body.sq-page.game-menu-active #game-header {
    display: none !important;
}

.sq-page .game-screen {
    display: none;
}

.sq-page .game-screen.active {
    display: block;
}

body.sq-page.with-game-header .sq-shell {
    padding-top: 1.1rem;
}

.sq-shell {
    --sq-cell: min(76px, calc((100dvh - 330px) / (var(--sq-rows, 5) + 1)));
    max-width: min(1040px, 96vw);
    margin: 0 auto;
    padding: 1rem 0.75rem 2rem;
    box-sizing: border-box;
}

/* ------------------------------------------------------------------ HUD */

.sq-hud {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    gap: 0.5rem;
    max-width: 720px;
    margin: 0 auto 0.6rem;
    padding: 0.45rem 0.6rem;
    background: linear-gradient(180deg, #24305e, #141834);
    border: 3px solid #0b0d1c;
    border-radius: 12px;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12), 0 10px 24px rgba(0, 0, 0, 0.35);
}

.sq-lcd {
    min-width: 4.4rem;
    padding: 0.15rem 0.45rem;
    background: #06121a;
    color: #f5c518;
    font-family: "Courier New", ui-monospace, monospace;
    font-size: 1.1rem;
    font-weight: 700;
    text-align: center;
    border-radius: 6px;
    box-shadow: inset 0 0 0 2px #7f1d1d, inset 0 8px 16px rgba(0, 0, 0, 0.45);
}

.sq-lcd span {
    display: block;
    font-size: 0.6rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: #f4c430;
}

.sq-chip {
    padding: 0.4rem 0.75rem;
    border-radius: 999px;
    background: rgba(244, 196, 48, 0.16);
    border: 1px solid rgba(244, 196, 48, 0.4);
    color: #fde68a;
    font-weight: 800;
    white-space: nowrap;
}

/* -------------------------------------------------------------- toolbar */

.sq-toolbar {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.45rem;
    margin: 0 auto 0.5rem;
    max-width: 720px;
}

.sq-tool-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.45rem 0.85rem;
    min-height: 42px;
    border-radius: 999px;
    border: 1px solid rgba(244, 196, 48, 0.35);
    background: rgba(8, 14, 32, 0.8) !important;
    color: #f3efe2 !important;
    font-family: inherit;
    font-weight: 700;
    font-size: 0.95rem;
    cursor: pointer;
}

.sq-tool-btn:hover,
.sq-tool-btn:focus-visible {
    border-color: #f5c518;
    background: rgba(8, 14, 32, 0.95) !important;
    color: #f5c518 !important;
}

.sq-tool-btn.is-on {
    background: #f4c430 !important;
    border-color: #f4c430;
    color: #1a1305 !important;
}

.sq-tool-btn:disabled {
    opacity: 0.45;
    cursor: default;
}

/* --------------------------------------------------------------- status */

.sq-status {
    text-align: center;
    min-height: 1.4rem;
    max-width: 46rem;
    margin: 0 auto 0.6rem;
    padding: 0 0.5rem;
    color: #fde68a;
    font-weight: 700;
    line-height: 1.35;
}

.sq-status.is-good {
    color: #bbf7d0;
}

.sq-status.is-bad {
    color: #fecaca;
}

.sq-status.is-hint {
    color: #fff3c4;
    background: rgba(244, 196, 48, 0.12);
    border: 1px solid rgba(244, 196, 48, 0.4);
    border-radius: 10px;
    padding: 0.4rem 0.7rem;
}

/* --------------------------------------------------------------- layout */

.sq-layout {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
}

.sq-board-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.6rem;
    max-width: 100%;
}

.sq-side {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    width: min(560px, 100%);
}

/* ---------------------------------------------------------------- board */

.sq-board {
    --cell: max(38px, min(var(--sq-cell), calc((100vw - 34px) / (var(--sq-n) + 1.15))));
    display: grid;
    grid-template-columns: repeat(var(--sq-n), var(--cell)) calc(var(--cell) * 1.05);
    grid-auto-rows: var(--cell);
    gap: 4px;
    padding: 8px;
    background: linear-gradient(180deg, #fffdf7, #f3ead6);
    border: 3px solid var(--sq-ink);
    border-radius: 16px;
    box-shadow: 0 16px 34px rgba(0, 0, 0, 0.4), inset 0 1px 0 #fff;
    box-sizing: border-box;
}

.sq-cell {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    padding: 0;
    border: 2px solid #d9cfb8;
    border-radius: 10px;
    background: #fff !important;
    color: var(--sq-ink) !important;
    cursor: pointer;
    transition: box-shadow 0.15s ease, transform 0.15s ease;
}

.sq-cell:hover {
    transform: translateY(-1px);
    border-color: #b9a47a;
}

.sq-cell.is-selected {
    border-color: var(--sq-red);
    box-shadow: 0 0 0 2px rgba(201, 12, 15, 0.35);
}

.sq-cell.is-hint {
    background: #fff7d6 !important;
    border-color: #e0a800;
}

.sq-total {
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    background: var(--sq-ink);
    color: #f5c518;
    font-family: "Roboto Slab", "Rockwell", Georgia, serif;
    font-weight: 800;
    font-size: calc(var(--cell) * 0.4);
    box-shadow: inset 0 0 0 2px rgba(244, 196, 48, 0.25);
    transition: background 0.2s ease, color 0.2s ease;
}

.sq-total.is-hidden-total {
    background: repeating-linear-gradient(45deg, #2a2440, #2a2440 6px, #1d1830 6px, #1d1830 12px);
    color: #c4b5fd;
}

.sq-total.is-ok {
    background: var(--sq-green);
    color: #fff;
}

.sq-total.is-bad {
    background: var(--sq-red);
    color: #fff;
    animation: sq-shake 0.35s ease;
}

.sq-total.is-hint {
    background: #f4c430;
    color: #1a1305;
    box-shadow: 0 0 0 3px rgba(244, 196, 48, 0.45);
}

.sq-corner {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #b9a47a;
    font-size: calc(var(--cell) * 0.32);
}

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

/* -------------------------------------------------------------- symbols */

.sq-sym {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    pointer-events: none;
}

.sq-cell .sq-sym {
    width: 76%;
    height: 76%;
}

.sq-sym svg,
.sq-sym img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.sq-sym-fa {
    font-size: calc(var(--cell, 48px) * 0.56);
    filter: drop-shadow(0 1px 0 rgba(0, 0, 0, 0.12));
}

.sq-sym-fa i,
.sq-sym-fa svg {
    width: auto;
    height: 1em;
}

/* -------------------------------------------------------------- answers */

.sq-answers {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.45rem;
}

.sq-answers.sq-k4 {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}

.sq-answer {
    --cell: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.3rem;
    min-height: 52px;
    padding: 0.3rem 0.4rem;
    border-radius: 12px;
    border: 2px solid #d9cfb8;
    background: var(--sq-paper) !important;
    color: var(--sq-ink) !important;
    font-family: inherit;
    cursor: pointer;
    box-shadow: 0 6px 14px rgba(0, 0, 0, 0.25);
}

.sq-answer:hover {
    border-color: var(--sq-accent, var(--sq-red));
    background: #fff !important;
    color: var(--sq-ink) !important;
}

.sq-answer .sq-answer-sym {
    width: 34px;
    height: 34px;
    flex: 0 0 auto;
}

.sq-answer .sq-sym-fa {
    font-size: 26px;
}

.sq-eq {
    font-weight: 800;
    color: #6b6250;
}

.sq-value {
    min-width: 2.3ch;
    padding: 0.15rem 0.35rem;
    border-radius: 8px;
    background: #fff;
    border: 2px solid #cbbf9f;
    font-family: "Roboto Slab", "Rockwell", Georgia, serif;
    font-size: 1.3rem;
    font-weight: 800;
    text-align: center;
    color: var(--sq-ink);
}

.sq-value.is-empty {
    color: #b3a789;
}

.sq-answer.is-selected {
    border-color: var(--sq-red);
    box-shadow: 0 0 0 3px rgba(201, 12, 15, 0.35), 0 6px 14px rgba(0, 0, 0, 0.25);
}

.sq-answer.is-selected .sq-value {
    border-color: var(--sq-red);
    background: #fff5f5;
}

.sq-answer.is-given .sq-value {
    background: #fff3c4;
    border-color: #e0a800;
}

.sq-answer.is-solved {
    border-color: var(--sq-green);
}

.sq-answer.is-solved .sq-value {
    background: #dcfce7;
    border-color: var(--sq-green);
    color: #14532d;
}

/* --------------------------------------------------------------- keypad */

.sq-keypad {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 0.4rem;
}

.sq-key {
    min-height: 46px;
    border-radius: 10px;
    border: 2px solid #0b0d1c;
    background: linear-gradient(180deg, #fffdf7, #ece2c8) !important;
    color: var(--sq-ink) !important;
    font-family: "Roboto Slab", "Rockwell", Georgia, serif;
    font-size: 1.3rem;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 4px 0 #0b0d1c;
    touch-action: manipulation;
}

.sq-key:hover {
    background: #fff !important;
    color: var(--sq-red) !important;
}

.sq-key:active {
    transform: translateY(3px);
    box-shadow: 0 1px 0 #0b0d1c;
}

.sq-key-back {
    background: linear-gradient(180deg, #e8e2d4, #cfc5ad) !important;
    color: var(--sq-ink) !important;
}

.sq-key-check {
    background: var(--sq-red) !important;
    color: #fff !important;
    font-size: 1.05rem;
    letter-spacing: 0.02em;
}

.sq-key-check:hover {
    background: #a50a0c !important;
    color: #fff !important;
}

.sq-key-check:disabled {
    background: #7a5a5a !important;
    color: #fff !important;
    cursor: default;
}

/* ------------------------------------------------------------ equations */

.sq-equations {
    width: 100%;
    max-width: 560px;
    background: rgba(255, 253, 247, 0.96);
    color: var(--sq-ink);
    border: 3px solid var(--sq-ink);
    border-radius: 14px;
    padding: 0.5rem 0.75rem;
    box-sizing: border-box;
}

.sq-equations[hidden] {
    display: none;
}

.sq-eq-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    gap: 0.25rem 0.9rem;
}

.sq-eq-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.2rem;
    font-weight: 700;
    padding: 0.15rem 0.3rem;
    border-radius: 8px;
}

.sq-eq-row.is-ok {
    background: #dcfce7;
}

.sq-eq-row.is-bad {
    background: #fee2e2;
}

.sq-eq-label {
    min-width: 2.2em;
    font-size: 0.75rem;
    color: #7c2d12;
    letter-spacing: 0.06em;
}

.sq-term {
    --cell: 34px;
    display: inline-flex;
    align-items: center;
    gap: 0.1rem;
}

.sq-eq-sym {
    width: 22px;
    height: 22px;
}

.sq-term .sq-sym-fa {
    font-size: 18px;
}

.sq-coef {
    font-weight: 800;
}

.sq-plus {
    color: #6b6250;
    padding: 0 0.1rem;
}

.sq-eq-values {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem 0.9rem;
    margin: 0.6rem 0 0;
    font-size: 1.1rem;
}

.sq-result-solution {
    margin: 1rem 0;
    padding: 0.6rem 0.75rem;
    border-radius: 12px;
    background: #f8fafc;
    max-height: 32vh;
    overflow-y: auto;
}

/* --------------------------------------------------------------- modals */

.sq-settings-label {
    margin: 0 0 0.45rem;
    color: #1a120c;
    font-weight: 600;
}

.sq-settings-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
    margin-bottom: 0.85rem;
}

#settingsModal .sq-settings-note {
    margin: 0.75rem 0 0;
    color: #7f1d1d;
    font-size: 0.9rem;
}

.difficulty-pill {
    padding: 0.45rem 0.85rem;
    min-height: 44px;
    border-radius: 999px;
    border: 2px solid #c90c0f;
    background: #fff !important;
    color: #c90c0f !important;
    font-weight: 700;
    cursor: pointer;
}

.difficulty-pill.is-selected,
.difficulty-pill:hover {
    background: #c90c0f !important;
    color: #fff !important;
}

.modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.72);
    z-index: 12000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: #fff;
    color: #1a1a1a;
    border-radius: 1rem;
    max-width: 640px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-large {
    max-width: 760px;
}

.modal-header,
.modal-footer {
    padding: 1.1rem 1.4rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.modal-header {
    border-bottom: 2px solid #eee;
}

.modal-header h2 {
    margin: 0;
    color: #c90c0f;
    font-size: 1.35rem;
}

.modal-close {
    background: #fff !important;
    border: 2px solid #c90c0f;
    color: #c90c0f !important;
    border-radius: 8px;
    cursor: pointer;
    min-width: 40px;
    min-height: 40px;
    padding: 0 0.5rem;
    font-size: 1.5rem;
    line-height: 1;
}

.modal-close:hover {
    background: #c90c0f !important;
    color: #fff !important;
}

.modal-body {
    padding: 1.2rem 1.4rem;
}

.modal-footer {
    border-top: 2px solid #eee;
    justify-content: flex-end;
    flex-wrap: wrap;
}

.help-section {
    margin-bottom: 1.1rem;
}

.help-section h3 {
    color: #c90c0f;
    margin: 0 0 0.4rem;
}

.help-section ul {
    margin: 0.3rem 0 0;
    padding-left: 1.2rem;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.stat-card {
    background: #f8fafc;
    border-radius: 12px;
    padding: 0.85rem;
    text-align: center;
}

.stat-value {
    font-size: 1.6rem;
    color: #c90c0f;
    font-weight: 700;
}

.stat-label {
    color: #64748b;
    font-size: 0.85rem;
}

.difficulty-stats {
    display: grid;
    gap: 0.45rem;
}

.difficulty-stats div {
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.45rem 0.55rem;
    background: #f8fafc;
    border-radius: 8px;
}

.result-message {
    font-size: 1.05rem;
    margin: 0 0 1rem;
}

.win-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.6rem;
    text-align: center;
}

.win-stat strong {
    display: block;
    font-size: 1.35rem;
    color: #c90c0f;
}

.win-stat span {
    color: #64748b;
    font-size: 0.82rem;
}

.sq-page .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.65rem 1.1rem;
    border-radius: 0.55rem;
    border: 2px solid transparent;
    font-weight: 700;
    cursor: pointer;
}

.sq-page .btn-primary {
    background: #c90c0f !important;
    color: #fff !important;
    border-color: #c90c0f;
}

.sq-page .btn-primary:hover {
    background: #a50a0c !important;
    color: #fff !important;
}

.sq-page .btn-secondary {
    background: #fff !important;
    color: #c90c0f !important;
    border-color: #c90c0f;
}

.sq-page .btn-secondary:hover {
    background: #c90c0f !important;
    color: #fff !important;
}

/* ------------------------------------------------------------ responsive */

@media (min-width: 900px) {
    .sq-shell {
        --sq-cell: min(84px, calc((100dvh - 250px) / (var(--sq-rows, 5) + 1)));
    }

    .sq-layout {
        flex-direction: row;
        align-items: flex-start;
        justify-content: center;
        gap: 1.5rem;
    }

    .sq-side {
        width: 330px;
        position: sticky;
        top: 6rem;
    }

    .sq-answers,
    .sq-answers.sq-k4 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .sq-keypad {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .sq-key {
        min-height: 54px;
    }

    .sq-key-check {
        font-size: 1.15rem;
    }
}

@media (max-width: 640px) {
    body.sq-page.with-game-header .sq-shell {
        padding-top: 0.5rem;
        padding-left: 0.4rem;
        padding-right: 0.4rem;
        padding-bottom: 0.75rem;
    }

    .sq-shell {
        --sq-cell: calc((100dvh - 540px) / (var(--sq-rows, 5) + 1));
    }

    .sq-board-wrap {
        display: contents;
    }

    .sq-board {
        order: 1;
    }

    .sq-side {
        order: 2;
    }

    .sq-equations {
        order: 3;
    }

    .sq-hud {
        gap: 0.35rem;
        padding: 0.3rem 0.4rem;
        margin-bottom: 0.4rem;
    }

    .sq-lcd {
        min-width: 0;
        flex: 1 1 0;
        font-size: 0.95rem;
        padding: 0.1rem 0.3rem;
    }

    .sq-lcd span {
        font-size: 0.52rem;
    }

    .sq-chip {
        padding: 0.3rem 0.55rem;
        font-size: 0.85rem;
    }

    .sq-toolbar {
        gap: 0.3rem;
        margin-bottom: 0.35rem;
    }

    .sq-tool-btn {
        min-height: 38px;
        padding: 0.3rem 0.65rem;
        font-size: 0.88rem;
    }

    .sq-status {
        font-size: 0.84rem;
        line-height: 1.25;
        margin-bottom: 0.4rem;
        min-height: 1.2rem;
    }

    .sq-status.is-hint {
        padding: 0.3rem 0.5rem;
    }

    .sq-layout,
    .sq-side {
        gap: 0.45rem;
    }

    .sq-board {
        gap: 3px;
        padding: 6px;
        border-radius: 14px;
    }

    .sq-cell,
    .sq-total {
        border-radius: 8px;
    }

    .sq-answer {
        min-height: 46px;
        padding: 0.2rem 0.3rem;
        gap: 0.2rem;
    }

    .sq-answer .sq-answer-sym {
        width: 28px;
        height: 28px;
    }

    .sq-answer .sq-sym-fa {
        font-size: 22px;
    }

    .sq-value {
        font-size: 1.1rem;
    }

    .sq-key {
        min-height: 42px;
        font-size: 1.15rem;
    }

    .sq-key-check {
        font-size: 0.95rem;
    }
}

@media (max-width: 380px) {
    .sq-tool-btn span {
        display: none;
    }
}
