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

:root {
    /* Light mode (default) */
    --bg-primary: #f5f5f5;
    --bg-secondary: #ffffff;
    --bg-tertiary: #e8e8e8;
    --text-primary: #1a1a1a;
    --text-secondary: #666666;
    --text-muted: #999999;
    --accent: #2d7d46;
    --accent-light: #e8f5e9;
    --operator-bg: #e0e0e0;
    --operator-border: #bdbdbd;
    --operator-filled: #d68c1a;
    --operator-filled-border: #c67d15;
    --correct: #2d7d46;
    --incorrect: #c62828;
    --border: #ddd;
    --shadow: rgba(0, 0, 0, 0.1);
    --modal-bg: rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-primary: #1a1a2e;
        --bg-secondary: #2d2d44;
        --bg-tertiary: #3d3d5c;
        --text-primary: #eeeeee;
        --text-secondary: #aaaaaa;
        --text-muted: #666666;
        --accent: #7ee787;
        --accent-light: #23363a;
        --operator-bg: #3d3d5c;
        --operator-border: #4d4d6c;
        --operator-filled: #f0c674;
        --operator-filled-border: #f0c674;
        --correct: #238636;
        --incorrect: #f85149;
        --border: #444;
        --shadow: rgba(0, 0, 0, 0.3);
        --modal-bg: rgba(0, 0, 0, 0.7);
    }
}

html {
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    min-height: 100dvh;
    line-height: 1.5;
}

.container {
    max-width: 500px;
    margin: 0 auto;
    padding: 20px 16px;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

/* Header */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border);
}

h1 {
    font-size: 1.8rem;
    font-weight: 700;
    letter-spacing: -0.5px;
}

h1 .dot {
    color: var(--accent);
}

.header-buttons {
    display: flex;
    gap: 8px;
}

.icon-btn {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.1s;
}

.icon-btn:hover {
    background: var(--bg-tertiary);
}

.icon-btn:active {
    transform: scale(0.95);
}

/* Main */
main {
    flex: 1;
    display: flex;
    flex-direction: column;
}

/* Difficulty Tabs */
.difficulty-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 24px;
}

.tab {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.tab:hover {
    background: var(--bg-tertiary);
}

.tab.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

@media (prefers-color-scheme: dark) {
    .tab.active {
        color: #1a1a2e;
    }
}

.tab.completed {
    position: relative;
}

.tab.completed::after {
    content: '✓';
    position: absolute;
    top: 4px;
    right: 6px;
    font-size: 0.7rem;
    color: var(--correct);
}

.tab.active.completed::after {
    color: #fff;
}

@media (prefers-color-scheme: dark) {
    .tab.active.completed::after {
        color: #1a1a2e;
    }
}

/* Puzzle Area */
.puzzle-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 24px 0;
}

.target {
    font-size: 1.1rem;
    color: var(--text-secondary);
    margin-bottom: 24px;
}

.target-number {
    color: var(--accent);
    font-weight: 700;
    font-size: 1.5rem;
}

.equation-container {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: center;
    margin-bottom: 16px;
}

.number {
    width: 50px;
    height: 60px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.6rem;
    font-weight: 700;
    flex-shrink: 0;
}

.operator {
    width: 46px;
    height: 46px;
    background: var(--operator-bg);
    border: 2px solid transparent;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    cursor: pointer;
    transition: all 0.15s;
    user-select: none;
    flex-shrink: 0;
    color: var(--text-muted);
}

.operator:hover {
    background: var(--bg-tertiary);
    transform: scale(1.05);
}

.operator:active {
    transform: scale(0.95);
}

.operator.empty {
    font-size: 1.6rem;
}

.operator.filled {
    color: var(--operator-filled);
    border-color: var(--operator-filled-border);
    font-weight: 600;
}

.operator.locked {
    cursor: default;
    opacity: 0.7;
}

.operator.locked:hover {
    transform: none;
}

.equals {
    font-size: 1.6rem;
    margin: 0 8px;
    color: var(--text-muted);
    flex-shrink: 0;
}

.result {
    min-width: 64px;
    height: 60px;
    padding: 0 14px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: 700;
    transition: all 0.3s;
    flex-shrink: 0;
}

.result.correct {
    background: var(--correct);
    border-color: var(--correct);
    color: #fff;
}

.result.incorrect {
    color: var(--incorrect);
}

.order-note {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 20px;
}

.timer {
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

/* Completion Area */
.completion-area {
    display: none;
    flex-direction: column;
    align-items: center;
    padding: 20px 0;
    animation: fadeIn 0.3s ease-out;
}

.completion-area.visible {
    display: flex;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.completion-message {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--correct);
    margin-bottom: 8px;
}

.completion-icon {
    width: 32px;
    height: 32px;
    background: var(--correct);
    color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
}

.completion-time {
    font-size: 1.1rem;
    color: var(--text-secondary);
    margin-bottom: 20px;
}

.share-btn {
    padding: 14px 32px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

@media (prefers-color-scheme: dark) {
    .share-btn {
        color: #1a1a2e;
    }
}

.share-btn:hover {
    filter: brightness(1.1);
}

.share-btn:active {
    transform: scale(0.98);
}

/* Modals */
.modal {
    position: fixed;
    inset: 0;
    background: var(--modal-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s;
    z-index: 100;
}

.modal.visible {
    opacity: 1;
    visibility: visible;
}

.modal-content {
    background: var(--bg-secondary);
    border-radius: 16px;
    padding: 24px;
    max-width: 360px;
    width: 100%;
    position: relative;
    transform: scale(0.95);
    transition: transform 0.2s;
}

.modal.visible .modal-content {
    transform: scale(1);
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
}

.modal-close:hover {
    background: var(--bg-tertiary);
}

.modal h2 {
    font-size: 1.3rem;
    margin-bottom: 20px;
    padding-right: 30px;
}

.modal h3 {
    font-size: 1rem;
    margin: 20px 0 12px;
    color: var(--text-secondary);
}

/* Help Content */
.help-content p {
    margin-bottom: 16px;
    color: var(--text-secondary);
}

.help-example {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 16px;
    background: var(--bg-tertiary);
    border-radius: 10px;
    margin-bottom: 16px;
}

.ex-num {
    width: 36px;
    height: 42px;
    background: var(--bg-secondary);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 1.2rem;
}

.ex-op {
    width: 32px;
    height: 32px;
    background: var(--operator-bg);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    color: var(--operator-filled);
    font-weight: 600;
}

.ex-eq {
    color: var(--text-muted);
    font-size: 1.2rem;
    margin: 0 4px;
}

.help-note {
    font-size: 0.9rem;
    padding: 12px;
    background: var(--accent-light);
    border-radius: 8px;
    color: var(--text-primary);
}

/* Stats */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    margin-bottom: 20px;
}

.stat-item {
    text-align: center;
}

.stat-value {
    font-size: 1.8rem;
    font-weight: 700;
}

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

.best-times {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.best-time-row {
    display: flex;
    justify-content: space-between;
    padding: 10px 14px;
    background: var(--bg-tertiary);
    border-radius: 8px;
}

.best-label {
    font-weight: 500;
}

.best-value {
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

/* Toast */
.toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--text-primary);
    color: var(--bg-primary);
    padding: 12px 24px;
    border-radius: 8px;
    font-weight: 500;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s;
    z-index: 200;
}

.toast.visible {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(0);
}

/* Responsive */
@media (max-width: 380px) {
    .number {
        width: 42px;
        height: 52px;
        font-size: 1.4rem;
    }
    
    .operator {
        width: 40px;
        height: 40px;
        font-size: 1.3rem;
    }
    
    .result {
        min-width: 54px;
        height: 52px;
        font-size: 1.3rem;
    }
    
    .equals {
        margin: 0 4px;
    }
    
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 340px) {
    .number {
        width: 36px;
        height: 46px;
        font-size: 1.2rem;
    }
    
    .operator {
        width: 34px;
        height: 34px;
        font-size: 1.1rem;
    }
    
    .result {
        min-width: 48px;
        height: 46px;
        font-size: 1.1rem;
    }
    
    .equation-container {
        gap: 4px;
    }
}
