:root {
    --bg-primary: #12141c;
    --bg-secondary: #1b1f2b;
    --bg-card: #252b3a;
    --text-primary: #eef1f7;
    --text-secondary: #9aa3b8;
    --accent-primary: #7c9cff;
    --accent-hover: #96b0ff;
    --accent-success: #5ed3a0;
    --accent-warn: #f2b263;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
    --radius: 12px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100dvh;
    user-select: none;
    -webkit-user-select: none;
    overflow: hidden;
}

#app {
    width: 100vw;
    height: 100dvh;
    position: relative;
}

/* Screens */
.screen {
    position: absolute;
    inset: 0;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.screen.active {
    display: flex;
}

/* Main Menu */
.menu-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    padding: 24px;
}

.logo {
    text-align: center;
}

.logo-tiles {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-bottom: 18px;
}

.logo-tiles span {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 18px;
    color: #1b1f2b;
    background: linear-gradient(160deg, #f5f7fb, #dbe1ee);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
}

.logo-tiles .logo-blank {
    background: transparent;
    box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.12);
}

.logo h1 {
    font-size: 44px;
    letter-spacing: 2px;
    margin-bottom: 6px;
}

.tagline {
    color: var(--text-secondary);
    font-size: 15px;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 260px;
}

/* Buttons */
.btn {
    border: none;
    border-radius: var(--radius);
    padding: 14px 20px;
    font-size: 16px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: transform 0.08s, background 0.15s, opacity 0.15s;
    color: var(--text-primary);
    background: var(--bg-card);
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: var(--accent-primary);
    color: #12141c;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: var(--bg-card);
}

.btn-secondary:hover {
    background: #2f3648;
}

.btn-tertiary {
    background: transparent;
    color: var(--text-secondary);
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.btn-tertiary:hover {
    color: var(--text-primary);
}

.btn-large {
    padding: 18px 20px;
    font-size: 18px;
}

.btn-sub {
    display: block;
    margin-top: 3px;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.3px;
    opacity: 0.7;
}

[hidden] {
    display: none !important;
}

.btn-control {
    background: var(--bg-card);
    padding: 12px 22px;
    font-size: 15px;
}

.btn-control:hover {
    background: #2f3648;
}

.btn-back {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 16px;
    font-family: inherit;
    cursor: pointer;
    padding: 8px;
}

.btn-back:hover {
    color: var(--text-primary);
}

/* In the flex-column screens the back button would otherwise stretch full
   width and centre its label. The game header is a flex row, so leave it. */
.info-container .btn-back {
    align-self: flex-start;
}

/* Setup panel (Custom screen) */
.setup {
    display: flex;
    flex-direction: column;
    gap: 14px;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    padding: 16px;
    width: 100%;
}

.setup-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.setup-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-secondary);
}

.size-picker {
    display: flex;
    align-items: center;
    gap: 8px;
}

.size-times {
    color: var(--text-secondary);
    font-size: 13px;
}

.stepper {
    display: flex;
    align-items: center;
    background: var(--bg-card);
    border-radius: 8px;
    overflow: hidden;
}

.stepper button {
    border: none;
    background: none;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 16px;
    font-weight: 700;
    width: 30px;
    height: 32px;
    cursor: pointer;
    transition: background 0.12s, opacity 0.12s;
}

.stepper button:hover:not(:disabled) {
    background: #333c50;
}

.stepper button:disabled {
    opacity: 0.28;
    cursor: default;
}

.stepper-value {
    min-width: 22px;
    text-align: center;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.segmented {
    display: flex;
    background: var(--bg-card);
    border-radius: 8px;
    padding: 3px;
    gap: 3px;
}

.seg {
    border: none;
    background: none;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 12px;
    font-weight: 600;
    padding: 7px 10px;
    border-radius: 6px;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.12s, color 0.12s;
}

.seg:hover {
    color: var(--text-primary);
}

.seg.active {
    background: var(--accent-primary);
    color: #12141c;
}

.custom-records {
    font-size: 12px;
    color: var(--text-secondary);
    text-align: center;
    padding-top: 2px;
}

/* Stats bar */
.stats-bar {
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    padding: 16px 8px;
    width: 300px;
}

.stats-row {
    display: flex;
    gap: 8px;
}

.stats-caption {
    text-align: center;
    font-size: 11px;
    color: var(--text-secondary);
    letter-spacing: 0.4px;
}

.stat-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.stat-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-secondary);
}

.stat-value {
    font-size: 20px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

/* How to Play */
.info-container {
    width: min(92vw, 460px);
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.info-container h2 {
    font-size: 28px;
}

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

.rule-item {
    display: flex;
    gap: 14px;
    align-items: flex-start;
    background: var(--bg-secondary);
    padding: 16px;
    border-radius: var(--radius);
}

.rule-num {
    flex: 0 0 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--accent-primary);
    color: #12141c;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
}

.rule-item p {
    color: var(--text-secondary);
    line-height: 1.5;
    font-size: 15px;
}

.rule-item strong {
    color: var(--text-primary);
}

/* Game screen */
#game-screen {
    justify-content: flex-start;
    padding: 20px 16px;
    gap: 20px;
}

.game-header {
    width: min(92vw, 400px);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.mode-badge {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-secondary);
    background: var(--bg-card);
    border-radius: 999px;
    padding: 5px 12px;
    white-space: nowrap;
}

.mode-badge.challenge,
.mode-badge.random {
    color: var(--accent-warn);
    box-shadow: inset 0 0 0 1px rgba(242, 178, 99, 0.35);
}

.hud {
    display: flex;
    gap: 8px;
}

.hud-item {
    background: var(--bg-secondary);
    border-radius: 10px;
    padding: 8px 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 78px;
}

.hud-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-secondary);
}

.hud-value {
    font-size: 18px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.board-container {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
}

.board {
    /* Overridden inline per mode; 4x4 is the Classic default. */
    --rows: 4;
    --cols: 4;

    /* Fit the width, but never let a wide board grow taller than the viewport. */
    --board-w: min(88vw, 400px, calc(56dvh * var(--cols) / var(--rows)));
    /* Subtract the border, scale by aspect, add it back, so cells stay square. */
    --cell: calc((var(--board-w) - 8px) / var(--cols));

    position: relative;
    width: var(--board-w);
    height: calc(var(--cell) * var(--rows) + 8px);
    background: var(--bg-secondary);
    border-radius: 16px;
    /* A transparent border (not padding) insets the tiles' containing block, so
       each tile is exactly 1/cols of it and translate(100%) is one cell. */
    border: 4px solid transparent;
    background-clip: border-box;
    box-shadow: var(--shadow);
    transition: box-shadow 0.4s;
}

.board.solved {
    box-shadow: 0 0 0 2px var(--accent-success), 0 6px 30px rgba(94, 211, 160, 0.25);
}

/* Tiles are absolutely positioned and translated into place, so a
   position change animates the slide for free. */
.tile {
    position: absolute;
    top: 0;
    left: 0;
    width: calc(100% / var(--cols));
    height: calc(100% / var(--rows));
    padding: 4px;
    border: none;
    background: none;
    font-family: inherit;
    cursor: default;
    transition: transform 130ms cubic-bezier(0.2, 0.8, 0.3, 1);
}

.tile-face {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    border-radius: 10px;
    background: linear-gradient(160deg, #f5f7fb, #dbe1ee);
    color: #1b1f2b;
    font-size: calc(var(--cell) * 0.42);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.35);
    transition: background 0.15s, transform 0.08s, box-shadow 0.4s;
}

.tile.movable {
    cursor: pointer;
}

.tile.movable .tile-face {
    background: linear-gradient(160deg, #ffffff, #e6ebf6);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.35), inset 0 0 0 2px var(--accent-primary);
}

.tile.movable:hover .tile-face {
    background: linear-gradient(160deg, #ffffff, #eef2fa);
}

.tile.movable:active .tile-face {
    transform: scale(0.95);
}

.board.solved .tile-face {
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.35), inset 0 0 0 2px var(--accent-success);
}

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

.tile.nudge .tile-face {
    animation: nudge 160ms ease-in-out;
}

.game-controls {
    display: flex;
    gap: 10px;
}

.verdict {
    width: min(92vw, 400px);
    text-align: center;
    font-size: 15px;
    line-height: 1.45;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.verdict.solvable strong {
    color: var(--accent-success);
}

.verdict.impossible strong {
    color: var(--accent-warn);
}

.verdict-math {
    font-size: 13px;
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

/* Modal */
.modal {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.modal.active {
    display: flex;
}

.modal-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
}

.modal-content {
    position: relative;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    padding: 32px 28px;
    width: min(88vw, 340px);
    text-align: center;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.modal-content h2 {
    font-size: 28px;
}

.win-record {
    color: var(--accent-success);
    font-size: 14px;
    font-weight: 600;
    display: none;
}

.win-record.visible {
    display: block;
}

.win-stats {
    display: flex;
    gap: 8px;
}

.win-stat {
    flex: 1;
    background: var(--bg-card);
    border-radius: 10px;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

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

.modal-buttons .btn {
    flex: 1;
}
