:root {
    --bg-1: #0f1115;
    --bg-2: #161a22;
    --bg-3: #1f2532;
    --surface: #1a1f2b;
    --surface-2: #252b3a;
    --board: #2a2f3d;
    --cell-empty: rgba(255, 255, 255, 0.04);
    --text: #f1f3f8;
    --text-muted: #8a93a6;
    --accent: #ffd34e;
    --accent-soft: rgba(255, 211, 78, 0.18);
    --danger: #ff6b6b;
    --radius: 14px;
    --radius-sm: 10px;
    --shadow-1: 0 4px 16px rgba(0, 0, 0, 0.35);
    --shadow-2: 0 8px 28px rgba(0, 0, 0, 0.45);
    --gap: 8px;

    --side-arrow: 36px;
    --vert-arrow: 44px;
    /* Horizontal budget: viewport - (2*page-padding 14 + 2*side-arrow 36 + 2*gap 6) = vw - 112 */
    /* Vertical budget: viewport - (header + stats + 2*vert-arrow + coach + speed + controls + paddings) ≈ vh - 540 */
    --grid-size: min(calc(100vw - 112px), calc(100dvh - 540px), 320px);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg-1);
    color: var(--text);
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    overscroll-behavior: none;
    touch-action: manipulation;
}

body {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: max(env(safe-area-inset-top, 0px), 8px) 14px max(env(safe-area-inset-bottom, 0px), 14px);
    gap: 12px;
    background:
        radial-gradient(1200px 600px at 50% -10%, #20263a 0%, transparent 60%),
        radial-gradient(800px 500px at 100% 100%, #1a2030 0%, transparent 60%),
        var(--bg-1);
    background-attachment: fixed;
    /* Prevent text selection on tile drags / D-pad presses; coach text overrides this. */
    -webkit-user-select: none;
    user-select: none;
}

.coach,
#ai-suggestion,
.suggestion {
    -webkit-user-select: text;
    user-select: text;
}

/* ---------- Header ---------- */
.app-header {
    width: 100%;
    max-width: 460px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.brand {
    display: flex;
    align-items: baseline;
    gap: 6px;
}

.brand-mark {
    font-size: 22px;
    font-weight: 800;
    letter-spacing: -0.02em;
    background: linear-gradient(135deg, #ffd34e 0%, #ff8d5a 100%);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

.brand-tag {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.coach-level {
    appearance: none;
    -webkit-appearance: none;
    background: var(--surface);
    color: var(--text);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 999px;
    padding: 7px 30px 7px 14px;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.02em;
    box-shadow: var(--shadow-1);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 12 12'%3E%3Cpath fill='%238a93a6' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
}

/* ---------- Stats ---------- */
.stats {
    width: 100%;
    max-width: 460px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.stat {
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, 0.04);
    border-radius: var(--radius-sm);
    padding: 8px 10px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    box-shadow: var(--shadow-1);
}

.stat-label {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.stat-value {
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

/* ---------- Game area ---------- */
.play-area {
    width: 100%;
    display: flex;
    justify-content: center;
}

.dpad {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.dpad-mid {
    display: flex;
    align-items: center;
    gap: 6px;
}

.direction-arrow {
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 12px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.15s ease, background 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

.direction-up,
.direction-down {
    width: var(--grid-size);
    max-width: 320px;
    height: var(--vert-arrow);
}

.direction-left,
.direction-right {
    width: var(--side-arrow);
    height: var(--grid-size);
    max-height: 320px;
}

.arrow-icon {
    font-size: 22px;
    line-height: 1;
    font-weight: 600;
}

.direction-arrow:active {
    transform: scale(0.94);
    background: var(--surface-2);
}

.direction-arrow.active {
    background: linear-gradient(135deg, #ffd34e 0%, #ff8d5a 100%);
    color: #1a1208;
    box-shadow: 0 0 24px var(--accent-soft), 0 4px 14px rgba(255, 141, 90, 0.3);
    animation: pulse 1.4s ease-in-out infinite;
}

.direction-arrow.active .arrow-icon {
    color: #1a1208;
    font-weight: 800;
}

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 18px var(--accent-soft), 0 4px 14px rgba(255, 141, 90, 0.25); }
    50%      { box-shadow: 0 0 32px rgba(255, 211, 78, 0.45), 0 6px 22px rgba(255, 141, 90, 0.4); }
}

#grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 6px;
    background-color: var(--board);
    border-radius: 12px;
    padding: 6px;
    width: var(--grid-size);
    height: var(--grid-size);
    max-width: 320px;
    max-height: 320px;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.03), var(--shadow-2);
}

.cell {
    background-color: var(--cell-empty);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: clamp(16px, 5.4vw, 26px);
    color: #f9f6f2;
    transition: transform 0.12s ease, background-color 0.12s ease;
}

/* Newly-spawned tile: a quick scale-up plus an accent ring that lingers until
   the next move re-renders. The ring is what the player actually scans for. */
.cell.cell--new {
    animation: tile-spawn 0.24s ease-out forwards;
    z-index: 1;
}

@keyframes tile-spawn {
    0%   { transform: scale(0.0); box-shadow: inset 0 0 0 0 rgba(255, 211, 78, 0); }
    55%  { transform: scale(1.14); box-shadow: inset 0 0 0 3px rgba(255, 211, 78, 0.95); }
    100% { transform: scale(1);    box-shadow: inset 0 0 0 2px rgba(255, 211, 78, 0.7); }
}

@media (prefers-reduced-motion: reduce) {
    .cell.cell--new {
        animation: none;
        box-shadow: inset 0 0 0 2px rgba(255, 211, 78, 0.7);
    }
}

/* Tile palette — keep classic 2048 colors so they're recognizable */
.tile-2    { background-color: #eee4da; color: #776e65; }
.tile-4    { background-color: #ede0c8; color: #776e65; }
.tile-8    { background-color: #f2b179; color: #f9f6f2; }
.tile-16   { background-color: #f59563; color: #f9f6f2; }
.tile-32   { background-color: #f67c5f; color: #f9f6f2; }
.tile-64   { background-color: #f65e3b; color: #f9f6f2; }
.tile-128  { background-color: #edcf72; color: #f9f6f2; font-size: clamp(14px, 4.8vw, 22px); }
.tile-256  { background-color: #edcc61; color: #f9f6f2; font-size: clamp(14px, 4.8vw, 22px); }
.tile-512  { background-color: #edc850; color: #f9f6f2; font-size: clamp(14px, 4.8vw, 22px); }
.tile-1024 { background-color: #edc53f; color: #f9f6f2; font-size: clamp(12px, 4.0vw, 18px); }
.tile-2048 { background-color: #edc22e; color: #f9f6f2; font-size: clamp(12px, 4.0vw, 18px); box-shadow: 0 0 22px rgba(237, 194, 46, 0.5); }
.tile-4096 { background-color: #3c3a32; color: #f9f6f2; font-size: clamp(11px, 3.6vw, 17px); }
.tile-8192 { background-color: #3c3a32; color: #f9f6f2; font-size: clamp(11px, 3.6vw, 17px); }

/* ---------- Coach ---------- */
.coach {
    width: 100%;
    max-width: 460px;
    background: linear-gradient(180deg, var(--surface) 0%, var(--surface-2) 100%);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: var(--radius);
    padding: 12px 14px;
    box-shadow: var(--shadow-1);
}

.coach-label {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 4px;
}

#ai-suggestion {
    color: var(--text);
    font-size: 14px;
    line-height: 1.45;
    /* Fixed height so the buttons below stay anchored regardless of explanation length. */
    height: 116px;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    overflow-y: auto;
}

#move-suggestion {
    width: 100%;
    color: var(--text-muted);
    font-style: italic;
    font-size: 13px;
}

/* The TeachingAdvisor injects this structure */
.suggestion {
    width: 100%;
    color: var(--text);
    font-style: normal;
}

.suggestion .move-direction {
    display: inline-block;
    background: linear-gradient(135deg, #ffd34e 0%, #ff8d5a 100%);
    color: #1a1208;
    font-weight: 800;
    letter-spacing: 0.08em;
    font-size: 12px;
    padding: 4px 10px;
    border-radius: 999px;
    margin-bottom: 6px;
}

.suggestion .move-reasoning .principle {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 2px;
    line-height: 1.4;
}

.suggestion .move-reasoning .detail {
    font-size: 13px;
    color: var(--text-muted);
    line-height: 1.45;
}

.suggestion .stats {
    margin-top: 6px;
    font-size: 11px;
    color: var(--text-muted);
    font-family: 'JetBrains Mono', 'Courier New', monospace;
    opacity: 0.85;
}

/* ---------- Speed control ---------- */
.speed-control {
    width: 100%;
    max-width: 460px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 999px;
    padding: 4px;
    box-shadow: var(--shadow-1);
}

.speed-option {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.04em;
    padding: 8px 6px;
    border-radius: 999px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    transition: background 0.18s ease, color 0.18s ease;
}

.speed-option.is-active {
    background: var(--surface-2);
    color: var(--text);
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.04);
}

/* ---------- Controls ---------- */
.controls {
    width: 100%;
    max-width: 460px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.button {
    border: none;
    border-radius: var(--radius-sm);
    padding: 13px 14px;
    font-size: 14px;
    font-weight: 700;
    letter-spacing: 0.02em;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    transition: transform 0.1s ease, box-shadow 0.2s ease, background 0.2s ease;
    min-height: 48px;
}

.button:active { transform: translateY(1px); }

.button--primary {
    background: linear-gradient(135deg, #ffd34e 0%, #ff8d5a 100%);
    color: #1a1208;
    box-shadow: 0 6px 18px rgba(255, 141, 90, 0.3);
}

.button--primary.is-running {
    background: linear-gradient(135deg, #ff6b6b 0%, #d23a3a 100%);
    color: #fff;
    box-shadow: 0 6px 18px rgba(210, 58, 58, 0.35);
}

.button--ghost {
    background: var(--surface);
    color: var(--text);
    border: 1px solid rgba(255, 255, 255, 0.06);
    box-shadow: var(--shadow-1);
}

/* ---------- Responsive tweaks ---------- */
@media (min-height: 820px) and (min-width: 420px) {
    :root { --grid-size: min(calc(100vw - 112px), 360px); }
}

@media (max-width: 360px) {
    :root {
        --side-arrow: 32px;
        --vert-arrow: 38px;
        --grid-size: min(calc(100vw - 100px), calc(100dvh - 460px), 280px);
    }
    .stat-value { font-size: 16px; }
    .button { font-size: 13px; padding: 12px 10px; }
}

/* When the user prefers reduced motion, drop the pulse */
@media (prefers-reduced-motion: reduce) {
    .direction-arrow.active { animation: none; }
    .cell, .button, .direction-arrow { transition: none; }
}
