/* === Reset & Base === */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-card: #1f2937;
    --bg-input: #374151;
    --text-primary: #e2e8f0;
    --text-secondary: #94a3b8;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --danger: #ef4444;
    --success: #22c55e;
    --grid-bg: #0f172a;
    --cell-inactive: rgba(100, 116, 139, 0.2);
    --border: rgba(148, 163, 184, 0.2);
    --overlay-bg: rgba(0, 0, 0, 0.6);
    --toggle-bg: #4b5563;
    --toggle-active: #3b82f6;
}

/* Light theme */
[data-theme="light"] {
    --bg-primary: #f8fafc;
    --bg-secondary: #e2e8f0;
    --bg-card: #ffffff;
    --bg-input: #e5e7eb;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --grid-bg: #cbd5e1;
    --cell-inactive: rgba(100, 116, 139, 0.15);
    --border: rgba(100, 116, 139, 0.3);
    --overlay-bg: rgba(255, 255, 255, 0.8);
    --toggle-bg: #d1d5db;
}

@media (prefers-color-scheme: light) {
    :root:not([data-theme="dark"]) {
        --bg-primary: #f8fafc;
        --bg-secondary: #e2e8f0;
        --bg-card: #ffffff;
        --bg-input: #e5e7eb;
        --text-primary: #1e293b;
        --text-secondary: #64748b;
        --grid-bg: #cbd5e1;
        --cell-inactive: rgba(100, 116, 139, 0.15);
        --border: rgba(100, 116, 139, 0.3);
        --overlay-bg: rgba(255, 255, 255, 0.8);
        --toggle-bg: #d1d5db;
    }
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

/* === Screens === */
.screen {
    display: none;
    position: absolute;
    inset: 0;
    flex-direction: column;
}
.screen.active {
    display: flex;
}

.screen-content {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 600px;
    margin: 0 auto;
    width: 100%;
    padding: 12px 16px;
    overflow: hidden;
}

.screen-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding-bottom: 12px;
    flex-shrink: 0;
}

.screen-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
}

/* === Buttons === */
.btn {
    border: none;
    border-radius: 10px;
    padding: 12px 24px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.15s, transform 0.1s;
    touch-action: manipulation;
}
.btn:active {
    transform: scale(0.97);
}

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

.btn-secondary {
    background: var(--bg-card);
    color: var(--text-primary);
    border: 1px solid var(--border);
}
.btn-secondary:hover {
    background: var(--bg-input);
}

.btn-danger {
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
}
.btn-danger:hover {
    background: var(--danger);
    color: #fff;
}

.btn-large {
    font-size: 1.2rem;
    padding: 14px 36px;
}

.btn-icon {
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 1.4rem;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    line-height: 1;
    touch-action: manipulation;
}
.btn-icon:hover {
    background: var(--bg-card);
}

.btn-stepper {
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text-primary);
    width: 36px;
    height: 36px;
    border-radius: 8px;
    font-size: 1.2rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
}
.btn-stepper:hover {
    background: var(--bg-input);
}

/* === HOME === */
.home-content {
    justify-content: center;
    align-items: center;
    gap: 20px;
}

.app-title {
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.mode-badge {
    background: var(--accent);
    color: #fff;
    padding: 6px 18px;
    border-radius: 20px;
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: 0.05em;
}

.home-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
    max-width: 260px;
    margin-top: 12px;
}

/* === GAME === */
.game-content {
    position: relative;
}

.game-top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    min-height: 44px;
}

.game-nback-area {
    display: flex;
    align-items: center;
    gap: 8px;
}

.nback-label {
    font-size: 1.1rem;
    font-weight: 600;
}

.stepper {
    display: flex;
    gap: 4px;
}

.game-area {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 0;
}

.btn-column {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 24px;
    width: 68px;
    flex-shrink: 0;
}

.grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 4px;
    aspect-ratio: 1;
    max-width: min(80vw, 400px);
    max-height: min(80vh, 400px);
    width: 100%;
    height: auto;
    background: var(--grid-bg);
    border-radius: 8px;
    padding: 4px;
}

.cell {
    background-color: var(--cell-inactive);
    border-radius: 4px;
    opacity: 0.2;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.cell-shape {
    width: 60%;
    height: 60%;
    object-fit: contain;
    position: absolute;
}

/* Attribute buttons */
.attr-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    cursor: pointer;
    transition: opacity 0.15s;
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
}

.attr-icon {
    width: 52px;
    height: 52px;
    object-fit: contain;
    pointer-events: none;
}

.attr-feedback {
    font-size: 0.75rem;
    font-weight: 700;
    height: 1em;
    line-height: 1;
}
.feedback-ok { color: var(--success); }
.feedback-ko { color: var(--danger); }

/* Game bottom bar */
.game-bottom-bar {
    display: flex;
    justify-content: flex-end;
    padding-top: 8px;
    flex-shrink: 0;
}

/* Score overlay */
.score-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--overlay-bg);
    z-index: 10;
}

.score-card {
    background: var(--bg-card);
    border-radius: 16px;
    padding: 28px 36px;
    text-align: center;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.score-main {
    font-size: 1.6rem;
    font-weight: 700;
    margin-bottom: 12px;
}

.score-detail {
    font-size: 1rem;
    color: var(--text-secondary);
    padding: 4px 0;
}

/* === SETTINGS === */
.settings-content {
    overflow: hidden;
}

.settings-scroll {
    flex: 1;
    overflow-y: auto;
    padding-bottom: 24px;
    -webkit-overflow-scrolling: touch;
}

.settings-section {
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 16px;
    margin-bottom: 12px;
}

.settings-section legend {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0 4px;
}

.toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 0;
    cursor: pointer;
}

.toggle-row span {
    font-size: 0.95rem;
}

/* Toggle switch */
.toggle {
    -webkit-appearance: none;
    appearance: none;
    width: 44px;
    height: 24px;
    background: var(--toggle-bg);
    border-radius: 12px;
    position: relative;
    cursor: pointer;
    transition: background 0.2s;
    flex-shrink: 0;
}
.toggle::after {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 20px;
    height: 20px;
    background: #fff;
    border-radius: 50%;
    transition: transform 0.2s;
}
.toggle:checked {
    background: var(--toggle-active);
}
.toggle:checked::after {
    transform: translateX(20px);
}
.toggle:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Stepper row */
.stepper-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 0;
}

/* Slider row */
.slider-row {
    padding: 8px 0;
}
.slider-row span {
    display: block;
    font-size: 0.95rem;
    margin-bottom: 6px;
}

input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 6px;
    background: var(--bg-input);
    border-radius: 3px;
    outline: none;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    background: var(--accent);
    border: none;
    border-radius: 50%;
    cursor: pointer;
}

/* Segmented control */
.segmented {
    display: flex;
    gap: 0;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--border);
}

.seg-option {
    flex: 1;
    text-align: center;
    cursor: pointer;
}

.seg-option input {
    display: none;
}

.seg-option span {
    display: block;
    padding: 8px 4px;
    font-size: 0.85rem;
    font-weight: 500;
    transition: background 0.15s, color 0.15s;
    color: var(--text-secondary);
}

.seg-option input:checked + span {
    background: var(--accent);
    color: #fff;
}

.seg-option:not(:last-child) span {
    border-right: 1px solid var(--border);
}

/* Select */
.select-input {
    width: 100%;
    padding: 8px 12px;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.95rem;
    cursor: pointer;
}

/* Keyboard hint bar (game screen) */
.keyboard-hint {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    margin-right: auto;
    font-size: 0.8rem;
    color: var(--text-secondary);
}
.keyboard-hint kbd {
    display: inline-block;
    padding: 2px 8px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.8rem;
    font-family: inherit;
    font-weight: 600;
    color: var(--text-primary);
}
.keyboard-hint .hint-sep {
    margin: 0 2px;
    opacity: 0.5;
}

/* Keyboard shortcut row */
.shortcut-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 0;
}
.shortcut-row span:first-child {
    font-size: 0.95rem;
}
.key-capture-btn {
    min-width: 60px;
    padding: 8px 16px;
    background: var(--bg-input);
    border: 2px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}
.key-capture-btn:hover {
    border-color: var(--accent);
}
.key-capture-btn.listening {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
    animation: pulse-key 1.5s infinite;
}
@keyframes pulse-key {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}
.settings-hint {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-top: 8px;
    line-height: 1.4;
}

/* === STATS === */
.stats-scroll {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 24px;
}

.empty-message {
    text-align: center;
    color: var(--text-secondary);
    padding: 40px 0;
    font-size: 1rem;
}

.stats-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.stat-row {
    background: var(--bg-card);
    border-radius: 10px;
    padding: 12px 16px;
}

.stat-main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.stat-mode {
    font-weight: 700;
    font-size: 1rem;
    color: var(--accent);
}

.stat-score {
    font-weight: 700;
    font-size: 1.2rem;
}

.stat-details {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: var(--text-secondary);
}

/* === HELP === */
.help-scroll {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 24px;
    line-height: 1.6;
}

.help-scroll h2 {
    font-size: 1.3rem;
    margin-bottom: 12px;
}

.help-scroll h3 {
    font-size: 1.05rem;
    margin-top: 20px;
    margin-bottom: 6px;
    color: var(--accent);
}

.help-scroll p {
    color: var(--text-secondary);
    margin-bottom: 12px;
    font-size: 0.95rem;
}

/* === Responsive === */
@media (min-width: 768px) {
    .grid {
        max-width: 400px;
        max-height: 400px;
    }
    .attr-icon {
        width: 60px;
        height: 60px;
    }
}

@media (max-width: 380px) {
    .attr-icon {
        width: 40px;
        height: 40px;
    }
    .btn-column {
        width: 50px;
    }
    .grid {
        max-width: calc(100vw - 140px);
    }
}
