/* --- Design System --- */
@import url('https://fonts.googleapis.com/css2?family=Geist+Mono:wght@400;500;600&family=Geist:wght@300;400;500;600&display=swap');

:root {
    --bg:        #0a0a0b;
    --bg-1:      #111113;
    --bg-2:      #18181b;
    --bg-3:      #242428;
    --border:    rgba(255,255,255,0.08);
    --border-md: rgba(255,255,255,0.13);
    --border-hi: rgba(255,255,255,0.22);
    --ink:       #f4f4f5;
    --ink-2:     #a1a1aa;
    --ink-3:     #71717a;
    --brand:     #6366f1;
    --brand-dim: rgba(99,102,241,0.15);
    --brand-hi:  #818cf8;
    --accent:    #22d3ee;
    --accent-dim:rgba(34,211,238,0.12);
    --success:   #4ade80;
    --warn:      #fb923c;
    --cell-empty: #141416;
    --cell-filled:#e4e4e7;
    --piece-filled:#6366f1;
    --preview:   rgba(99,102,241,0.28);
    --r:         8px;
    --r-lg:      12px;
    --mono: 'Geist Mono', 'Cascadia Code', Consolas, monospace;
    --sans: 'Geist', 'Segoe UI', system-ui, sans-serif;
}

[data-theme='light'] {
    --bg:        #fafafa;
    --bg-1:      #ffffff;
    --bg-2:      #f4f4f5;
    --bg-3:      #e4e4e7;
    --border:    rgba(0,0,0,0.08);
    --border-md: rgba(0,0,0,0.13);
    --border-hi: rgba(0,0,0,0.22);
    --ink:       #09090b;
    --ink-2:     #52525b;
    --ink-3:     #a1a1aa;
    --brand:     #4f46e5;
    --brand-dim: rgba(79,70,229,0.10);
    --brand-hi:  #6366f1;
    --accent:    #0891b2;
    --accent-dim:rgba(8,145,178,0.10);
    --cell-empty: #f4f4f5;
    --cell-filled:#18181b;
    --piece-filled:#4f46e5;
    --preview:   rgba(79,70,229,0.22);
}

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

html { min-height: 100%; background: var(--bg); }

body {
    min-height: 100vh;
    padding: 24px 20px max(48px, calc(48px + env(safe-area-inset-bottom)));
    display: flex;
    flex-direction: column;
    align-items: center;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--sans);
    font-size: 14px;
    line-height: 1.6;
    overflow-x: hidden;
    /* subtle grid texture */
    background-image:
        linear-gradient(var(--border) 1px, transparent 1px),
        linear-gradient(90deg, var(--border) 1px, transparent 1px);
    background-size: 32px 32px;
}

.container {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 1020px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* --- Header --- */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 20px 0 18px;
    border-bottom: 1px solid var(--border-md);
}

.eyebrow {
    font-family: var(--mono);
    font-size: 10px;
    font-weight: 500;
    letter-spacing: 0.12em;
    color: var(--brand-hi);
    text-transform: uppercase;
    margin-bottom: 6px;
}

h1 {
    font-family: var(--sans);
    font-size: clamp(1.6rem, 5vw, 2.8rem);
    font-weight: 600;
    letter-spacing: -0.03em;
    color: var(--ink);
    line-height: 1;
}

/* --- Panels --- */
.panel-heading {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 14px;
}

.panel-index {
    font-family: var(--mono);
    font-size: 10px;
    font-weight: 600;
    color: var(--ink-3);
    border: 1px solid var(--border-md);
    border-radius: 4px;
    padding: 2px 6px;
    background: var(--bg-3);
}

.instruction, .section-title {
    font-size: 13px;
    font-weight: 500;
    color: var(--ink);
}

.instruction-small {
    font-size: 12px;
    color: var(--ink-3);
    margin-bottom: 14px;
    line-height: 1.5;
}

/* --- Buttons --- */
button {
    min-height: 36px;
    padding: 0 14px;
    border: 1px solid var(--border-md);
    border-radius: var(--r);
    background: var(--bg-2);
    color: var(--ink-2);
    font: 500 13px/1 var(--sans);
    cursor: pointer;
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: background 0.12s, border-color 0.12s, color 0.12s;
    outline: none;
}

.icon {
    width: 1.05em;
    height: 1.05em;
    flex: 0 0 auto;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

button:hover:not(:disabled) {
    background: var(--bg-3);
    border-color: var(--border-hi);
    color: var(--ink);
}

button:active:not(:disabled) {
    background: var(--bg-3);
}

button:focus-visible,
.cell:focus-visible,
.piece-input-cell:focus-visible {
    outline: 2px solid var(--brand);
    outline-offset: 2px;
}

.btn-primary {
    background: var(--brand);
    border-color: var(--brand);
    color: #fff;
    font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
    background: var(--brand-hi);
    border-color: var(--brand-hi);
    color: #fff;
}

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

#theme-toggle {
    font-size: 12px;
    padding: 0 12px;
    background: var(--bg-2);
}

button:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

/* --- Layout --- */
#input-area-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-items: start;
    gap: 16px;
}

#board-container,
#piece-input-area {
    border: 1px solid var(--border-md);
    border-radius: var(--r-lg);
    background: var(--bg-1);
    padding: 16px;
    width: 100%;
    max-width: 432px;
    margin: 0 auto;
}

/* --- Board --- */
#board {
    display: grid;
    width: 100%;
    aspect-ratio: 1;
    border: 1px solid var(--border-hi);
    border-radius: var(--r);
    background: var(--border-md);
    gap: 1.5px;
    overflow: hidden;
}

.cell {
    position: relative;
    background: var(--cell-empty);
    cursor: pointer;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    min-width: 0;
    min-height: 0;
    transition: background 0.08s;
}

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

.cell.filled::after {
    content: "";
    position: absolute;
    inset: 15%;
    border-radius: 2px;
    background: rgba(255,255,255,0.06);
}

.cell.preview {
    background: var(--preview) !important;
    outline: 1px solid var(--brand);
    outline-offset: -1px;
    z-index: 1;
}

/* --- Piece Input --- */
.pieces-list {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
    gap: 12px;
}

.piece-input-grid {
    display: grid;
    width: 100%;
    max-width: 160px;
    aspect-ratio: 1;
    justify-self: center;
    border: 1px solid var(--border-md);
    border-radius: var(--r);
    background: var(--border);
    gap: 1.5px;
    overflow: hidden;
    transition: border-color 0.12s;
}

.piece-input-grid.active-preview {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px var(--brand-dim);
}

.piece-input-cell {
    position: relative;
    background: var(--cell-empty);
    cursor: pointer;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    min-width: 0;
    min-height: 0;
    transition: background 0.08s;
}

.piece-input-cell.filled { background: var(--piece-filled); }

.piece-input-cell.filled::after {
    content: "";
    position: absolute;
    inset: 15%;
    border-radius: 2px;
    background: rgba(255,255,255,0.14);
}

/* --- Playback --- */
.playback-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-bottom: 12px;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--r);
    background: var(--bg-2);
}

.playback-controls.hidden { display: none; }

#step-indicator {
    min-width: 100px;
    font-family: var(--mono);
    font-size: 12px;
    font-weight: 500;
    color: var(--ink-2);
    text-align: center;
}

.btn-control {
    min-height: 30px;
    padding: 0 10px;
    font-size: 12px;
    color: var(--brand-hi);
    border-color: var(--border);
}

.btn-control:hover:not(:disabled) {
    background: var(--brand-dim);
    border-color: var(--brand);
    color: var(--brand-hi);
}

/* --- Action Buttons --- */
.action-buttons {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
    width: 100%;
    max-width: 432px;
    margin: 0 auto;
}

#solve-button {
    min-height: 42px;
    font-size: 14px;
    letter-spacing: -0.01em;
}

/* --- Output --- */
#output-log {
    width: 100%;
    max-width: 1020px;
    min-height: 100px;
    max-height: 280px;
    padding: 14px 16px;
    overflow-y: auto;
    border: 1px solid var(--border-md);
    border-left: 3px solid var(--brand);
    border-radius: var(--r-lg);
    background: var(--bg-1);
    color: var(--ink-2);
    font-family: var(--mono);
    font-size: 12px;
    line-height: 1.7;
    white-space: pre-wrap;
}

#output-log::-webkit-scrollbar { width: 4px; }
#output-log::-webkit-scrollbar-track { background: transparent; }
#output-log::-webkit-scrollbar-thumb { background: var(--bg-3); border-radius: 4px; }

/* --- Trace Panel --- */
.trace-panel {
    width: 100%;
    max-width: 1020px;
    border: 1px solid var(--border-md);
    border-radius: var(--r-lg);
    background: linear-gradient(180deg, var(--bg-1), var(--bg-2));
    padding: 16px;
}

.trace-panel.hidden { display: none; }

.trace-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 14px;
}

.trace-title {
    font-size: 18px;
    font-weight: 600;
    letter-spacing: -0.02em;
}

.trace-summary,
.trace-event-label,
#trace-step-indicator {
    font-family: var(--mono);
    font-size: 12px;
    color: var(--ink-2);
}

.trace-layout {
    display: grid;
    grid-template-columns: minmax(220px, 300px) minmax(0, 1fr);
    gap: 16px;
    align-items: start;
}

.trace-board-wrap {
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--r);
    background: var(--bg-1);
}

.trace-board {
    display: grid;
    width: 100%;
    max-width: 280px;
    aspect-ratio: 1;
    border: 1px solid var(--border-hi);
    border-radius: var(--r);
    background: var(--border-md);
    gap: 1.5px;
    overflow: hidden;
}

.trace-cell {
    background: var(--cell-empty);
    min-width: 0;
    min-height: 0;
}

.trace-cell.filled { background: var(--cell-filled); }
.trace-cell.accent { background: var(--brand-hi); }

.trace-sidebar {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.trace-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--r);
    background: var(--bg-1);
}

.trace-log {
    min-height: 160px;
    max-height: 260px;
    padding: 12px 14px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: var(--r);
    background: rgba(0, 0, 0, 0.15);
    color: var(--ink-2);
    font-family: var(--mono);
    font-size: 12px;
    line-height: 1.7;
    white-space: pre-wrap;
}

/* --- Footer --- */
footer {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 1020px;
    margin: 20px auto 0;
    padding: 16px 0 max(10px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    color: var(--ink-3);
    font-family: var(--mono);
    font-size: 11px;
    text-align: center;
}

/* --- Responsive --- */
@media (min-width: 800px) {
    body { padding: 28px 28px 56px; }

    .container { gap: 24px; }

    #input-area-grid {
        grid-template-columns: minmax(320px, 432px) minmax(320px, 432px);
        justify-content: center;
        gap: 20px;
    }

    .action-buttons {
        grid-template-columns: 1.4fr 1fr 1fr;
        max-width: 720px;
    }

    .trace-layout {
        grid-template-columns: minmax(240px, 300px) minmax(0, 1fr);
    }
}

@media (max-width: 767px) {
    body {
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
    }

    #board-container,
    #piece-input-area,
    .action-buttons {
        max-width: min(432px, 96vw);
    }

    .pieces-list {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
    }

    .piece-input-grid { max-width: 144px; }
    .playback-controls { flex-wrap: wrap; }
    .trace-layout { grid-template-columns: 1fr; }
    .trace-board { max-width: min(280px, 100%); margin: 0 auto; }
}

@media (max-width: 380px) {
    h1 { font-size: 1.5rem; }
    #board-container, #piece-input-area { padding: 12px; }
    .piece-input-grid { max-width: 128px; }
}
