/* ===== CSS Reset & Variables ===== */
:root {
    --primary: #5B8DEF;
    --primary-light: #7BA4F7;
    --primary-dark: #4A7AE0;
    --success: #34C759;
    --success-light: #5DD879;
    --error: #FF3B30;
    --error-light: #FF6961;
    --warning: #FF9500;
    --warning-light: #FFB340;
    --background: #F5F7FA;
    --surface: #FFFFFF;
    --surface-light: #FAFBFC;
    --text: #1A1A2E;
    --text-secondary: #6B7280;
    --text-light: #9CA3AF;
    --border: #E5E7EB;

    --font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --radius-sm: 10px;
    --radius-md: 14px;
    --radius-lg: 20px;
    --radius-xl: 28px;
    --radius-full: 50px;

    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
    --shadow-xl: 0 12px 40px rgba(0, 0, 0, 0.15);
}

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

body {
    font-family: var(--font-family);
    background: var(--background);
    color: var(--text);
    min-height: 100vh;
    min-height: 100dvh;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* ===== App Container ===== */
.app-container {
    max-width: 500px;
    margin: 0 auto;
    min-height: 100vh;
    min-height: 100dvh;
    position: relative;
}

.page {
    display: none;
    padding: 16px;
    min-height: 100vh;
    min-height: 100dvh;
    animation: fadeIn 0.3s ease;
}

.page.active {
    display: flex;
    flex-direction: column;
}

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

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ===== Config Page ===== */
.config-card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 24px 20px;
    box-shadow: var(--shadow-md);
}

.app-title {
    text-align: center;
    font-size: 24px;
    font-weight: 800;
    margin-bottom: 20px;
    color: var(--primary);
}

.config-section {
    margin-bottom: 16px;
}

.config-section.half {
    margin-bottom: 0;
}

.config-row {
    display: flex;
    gap: 16px;
    margin-bottom: 16px;
}

.config-row .config-section.half {
    flex: 1;
}

.section-title {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.section-title .hint {
    font-size: 10px;
    font-weight: 400;
    color: var(--text-secondary);
    text-transform: none;
    opacity: 0.8;
}

.hidden {
    display: none !important;
}

/* Mode Selection */
.mode-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
}

.mode-option {
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius-md);
    padding: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    position: relative;
}

.mode-option input[type="radio"] {
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
}

.mode-option:hover {
    border-color: var(--primary);
}

.mode-option.active {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    border-color: var(--primary);
    box-shadow: var(--shadow-glow);
}

.mode-icon {
    font-size: 24px;
}

.mode-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
}

/* Operation Checkboxes (Multi-select) */
.operation-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
}

.operation-grid.compact {
    grid-template-columns: repeat(4, 1fr);
}

.operation-checkbox {
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    position: relative;
}

.operation-checkbox input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
}

.operation-checkbox:hover {
    border-color: var(--primary);
}

.operation-checkbox.active {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    border-color: var(--primary);
    box-shadow: var(--shadow-glow);
}

.op-symbol {
    font-size: 20px;
    font-weight: 700;
    color: var(--text);
}

.op-name {
    font-size: 11px;
    color: var(--text-secondary);
}

.operation-checkbox.active .op-name {
    color: var(--text);
}

/* Time Options */
.time-options {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}

.time-btn {
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 8px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
    cursor: pointer;
    transition: all 0.2s ease;
}

.time-btn:hover {
    border-color: var(--primary);
}

.time-btn.active {
    background: var(--primary);
    border-color: var(--primary);
}

/* Custom Input Fields */
.custom-input-group {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius-md);
    padding: 12px 16px;
    transition: border-color 0.2s ease;
}

.custom-input-group:focus-within {
    border-color: var(--primary);
}

.custom-input {
    flex: 1;
    background: transparent;
    border: none;
    font-size: 24px;
    font-weight: 700;
    color: var(--text);
    text-align: center;
    outline: none;
    width: 100%;
    min-width: 0;
    -moz-appearance: textfield;
}

.custom-input::-webkit-outer-spin-button,
.custom-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.input-suffix {
    font-size: 16px;
    font-weight: 500;
    color: var(--text-secondary);
    white-space: nowrap;
}

/* Start Button */
.start-btn {
    width: 100%;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    border: none;
    border-radius: var(--radius-lg);
    padding: 18px;
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.3s ease;
    box-shadow: var(--shadow-md), var(--shadow-glow);
}

.start-btn:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg), 0 0 30px rgba(99, 102, 241, 0.5);
}

.start-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.start-btn .arrow {
    font-size: 20px;
    transition: transform 0.3s ease;
}

.start-btn:hover:not(:disabled) .arrow {
    transform: translateX(4px);
}

/* ===== Quiz Page ===== */
.quiz-container {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

.quiz-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.timer {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 18px;
    font-weight: 700;
    color: var(--primary);
    background: var(--surface);
    padding: 8px 14px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
}

.timer-icon {
    font-size: 18px;
}

.progress-info {
    font-size: 16px;
    font-weight: 700;
    color: var(--text);
    background: var(--surface);
    padding: 8px 14px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
}

.progress-bar {
    height: 6px;
    background: var(--border);
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 12px;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary), var(--success));
    border-radius: 3px;
    transition: width 0.3s ease;
    width: 0%;
}

.question-area {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px 0;
}

.question-card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 20px 32px;
    box-shadow: var(--shadow-md);
    font-size: 40px;
    font-weight: 800;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    color: var(--text);
}

.question-card #operator {
    color: var(--primary);
}

.equals {
    color: var(--text-light);
}

/* Inline Answer Display */
.answer-value {
    min-width: 60px;
    text-align: center;
    color: var(--primary);
    background: var(--surface-light);
    border-radius: var(--radius-sm);
    padding: 6px 16px;
    border: 2px dashed var(--border);
    transition: all 0.2s ease;
}

.answer-value.has-input {
    color: var(--success);
    border-color: var(--success);
    border-style: solid;
    background: #F0FFF4;
}

/* Numpad */
.numpad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    padding: 16px;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
}

.numpad-btn {
    background: var(--surface-light);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 16px;
    font-size: 24px;
    font-weight: 700;
    color: var(--text);
    cursor: pointer;
    transition: all 0.15s ease;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.numpad-btn:hover {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
}

.numpad-btn:active {
    transform: scale(0.96);
}

.numpad-btn.clear {
    background: #FFF5F5;
    border-color: #FED7D7;
    color: var(--error);
}

.numpad-btn.clear:hover {
    background: var(--error);
    border-color: var(--error);
    color: white;
}

.numpad-btn.backspace {
    background: #FFFBEB;
    border-color: #FDE68A;
    color: var(--warning);
}

.numpad-btn.backspace:hover {
    background: var(--warning);
    border-color: var(--warning);
    color: white;
}

.numpad-btn.confirm {
    background: var(--success);
    border-color: var(--success);
    color: white;
    font-size: 20px;
}

.numpad-btn.confirm:hover {
    background: var(--success-light);
    border-color: var(--success-light);
}

.numpad-btn.full-width {
    grid-column: span 3;
    padding: 14px;
}

/* ===== Result Page ===== */
.result-container {
    flex: 1;
    overflow-y: auto;
}

.result-header {
    text-align: center;
    margin-bottom: 20px;
}

.result-header h2 {
    font-size: 22px;
    font-weight: 800;
    margin-bottom: 8px;
    color: var(--primary);
}

.result-emoji {
    font-size: 48px;
    animation: bounce 1s ease infinite;
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-10px);
    }
}

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

.stat-card {
    background: var(--surface);
    border-radius: var(--radius-md);
    padding: 14px 10px;
    text-align: center;
    box-shadow: var(--shadow-sm);
}

.stat-value {
    font-size: 28px;
    font-weight: 800;
    margin-bottom: 4px;
}

.stat-card.accuracy .stat-value {
    color: var(--success);
}

.stat-card.time .stat-value {
    color: var(--warning);
}

.stat-card.avg-time .stat-value {
    color: var(--primary-light);
}

.stat-card.correct-wrong .correct {
    color: var(--success);
}

.stat-card.correct-wrong .wrong {
    color: var(--error);
}

.stat-label {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 500;
}

/* Analysis Section */
.analysis-section {
    margin-bottom: 24px;
}

.analysis-table-wrapper {
    overflow-x: auto;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
}

.analysis-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

.analysis-table th,
.analysis-table td {
    padding: 12px 8px;
    text-align: center;
    border-bottom: 1px solid var(--border);
}

.analysis-table th {
    background: var(--surface);
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.analysis-table tbody tr {
    background: var(--surface);
    transition: background 0.2s ease;
}

.analysis-table tbody tr:hover {
    background: var(--surface-light);
}

.analysis-table tbody tr:last-child td {
    border-bottom: none;
}

.analysis-table .result-correct {
    color: var(--success);
}

.analysis-table .result-wrong {
    color: var(--error);
}

.analysis-table .user-wrong {
    color: var(--error);
    text-decoration: line-through;
}

.decisiveness-fast {
    color: var(--success);
}

.decisiveness-normal {
    color: var(--warning);
}

.decisiveness-slow {
    color: var(--error);
}

/* Restart Button */
.restart-btn {
    width: 100%;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    border: none;
    border-radius: var(--radius-lg);
    padding: 18px;
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.3s ease;
    box-shadow: var(--shadow-md), var(--shadow-glow);
}

.restart-btn:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg), 0 0 30px rgba(99, 102, 241, 0.5);
}

.restart-btn .arrow {
    font-size: 20px;
    transition: transform 0.3s ease;
}

.restart-btn:hover .arrow {
    transform: rotate(360deg);
}

/* ===== Responsive - Mobile ===== */
@media (max-width: 400px) {
    .question-card {
        font-size: 32px;
        padding: 32px 24px;
    }

    .numpad-btn {
        padding: 14px;
        font-size: 20px;
    }

    .stats-grid {
        grid-template-columns: 1fr 1fr;
    }

    .stat-value {
        font-size: 24px;
    }
}

/* ===== Responsive - Tablet ===== */
@media (min-width: 600px) {
    .app-container {
        max-width: 700px;
        padding: 20px;
    }

    .page {
        padding: 32px;
    }

    .config-card {
        padding: 48px 40px;
    }

    .app-title {
        font-size: 36px;
    }

    .app-title .emoji {
        font-size: 64px;
    }

    .operation-grid {
        grid-template-columns: repeat(4, 1fr);
    }

    .operation-checkbox {
        padding: 20px;
    }

    .op-symbol {
        font-size: 32px;
    }

    .op-name {
        font-size: 15px;
    }

    .custom-input-group {
        max-width: 300px;
        margin: 0 auto;
        padding: 16px 24px;
    }

    .custom-input {
        font-size: 32px;
    }

    .input-suffix {
        font-size: 20px;
    }

    .start-btn {
        max-width: 400px;
        margin: 0 auto;
        padding: 18px;
        font-size: 20px;
    }

    /* Quiz page tablet */
    .quiz-header {
        margin-bottom: 12px;
    }

    .timer {
        font-size: 20px;
    }

    .progress-info {
        font-size: 18px;
    }

    .progress-bar {
        height: 6px;
        margin-bottom: 12px;
    }

    .question-card {
        font-size: 36px;
        padding: 20px 24px;
        gap: 14px;
    }

    .answer-value {
        min-width: 60px;
        padding: 6px 14px;
        font-size: 36px;
    }

    .numpad {
        max-width: 360px;
        margin: 0 auto;
        gap: 8px;
        padding: 12px;
    }

    .numpad-btn {
        padding: 14px;
        font-size: 24px;
    }

    .numpad-btn.full-width {
        padding: 12px;
    }

    /* Result page tablet */
    .result-header h2 {
        font-size: 36px;
    }

    .result-emoji {
        font-size: 80px;
    }

    .stats-grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 16px;
    }

    .stat-card {
        padding: 24px 20px;
    }

    .stat-value {
        font-size: 32px;
    }

    .stat-label {
        font-size: 14px;
    }

    .analysis-table {
        font-size: 16px;
    }

    .analysis-table th,
    .analysis-table td {
        padding: 16px 12px;
    }

    .restart-btn {
        max-width: 400px;
        margin: 0 auto;
        padding: 24px;
        font-size: 22px;
    }
}

/* ===== Responsive - Large Tablet / Small Desktop ===== */
@media (min-width: 900px) {
    .app-container {
        max-width: 900px;
    }

    .config-card {
        padding: 40px 48px;
    }

    .app-title {
        font-size: 32px;
    }

    .question-card {
        font-size: 48px;
        padding: 28px 36px;
    }

    .numpad {
        max-width: 400px;
    }

    .numpad-btn {
        padding: 18px;
        font-size: 28px;
    }
}

/* ===== History Button ===== */
.history-btn {
    width: 100%;
    padding: 12px;
    margin-top: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
}

.history-btn:hover {
    background: var(--surface-light);
    color: var(--primary);
    border-color: var(--primary);
}

/* ===== History Page ===== */
.history-container,
.wrong-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.history-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
}

.history-header h2 {
    font-size: 20px;
    font-weight: 700;
    color: var(--text);
    flex: 1;
}

.back-btn {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px 12px;
    font-size: 14px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
}

.back-btn:hover {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

/* Stats Overview */
.stats-overview {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
}

.stat-item {
    background: var(--surface);
    border-radius: var(--radius-md);
    padding: 16px 12px;
    text-align: center;
    box-shadow: var(--shadow-sm);
}

.stat-number {
    display: block;
    font-size: 24px;
    font-weight: 800;
    color: var(--primary);
    margin-bottom: 4px;
}

.stat-label {
    font-size: 12px;
    color: var(--text-secondary);
}

/* Trend Section */
.trend-section {
    background: var(--surface);
    border-radius: var(--radius-md);
    padding: 16px;
    box-shadow: var(--shadow-sm);
}

.trend-section h3 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 12px;
}

.trend-chart {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    height: 100px;
    gap: 8px;
}

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

.trend-bar-fill {
    width: 100%;
    background: linear-gradient(180deg, var(--primary) 0%, var(--primary-light) 100%);
    border-radius: 4px 4px 0 0;
    min-height: 4px;
    transition: height 0.3s ease;
}

.trend-bar-label {
    font-size: 10px;
    color: var(--text-secondary);
}

.trend-bar-value {
    font-size: 10px;
    font-weight: 600;
    color: var(--primary);
}

/* History List */
.history-list-section {
    background: var(--surface);
    border-radius: var(--radius-md);
    padding: 16px;
    box-shadow: var(--shadow-sm);
    flex: 1;
    overflow-y: auto;
}

.history-list-section h3 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 12px;
}

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

.history-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    background: var(--surface-light);
    border-radius: var(--radius-sm);
}

.history-item-left {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.history-item-date {
    font-size: 12px;
    color: var(--text-secondary);
}

.history-item-mode {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
}

.history-item-right {
    text-align: right;
}

.history-item-accuracy {
    font-size: 18px;
    font-weight: 700;
    color: var(--success);
}

.history-item-count {
    font-size: 12px;
    color: var(--text-secondary);
}

.empty-hint {
    text-align: center;
    color: var(--text-secondary);
    font-size: 14px;
    padding: 20px;
}

/* Wrong Section */
.wrong-section {
    margin-top: auto;
}

.wrong-btn {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
    cursor: pointer;
    transition: all 0.2s ease;
}

.wrong-btn:hover {
    background: #FFF5F5;
    border-color: var(--error);
    color: var(--error);
}

.wrong-count {
    background: var(--error);
    color: white;
    font-size: 12px;
    padding: 2px 8px;
    border-radius: 10px;
}

/* Wrong List */
.wrong-list {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow-y: auto;
}

.wrong-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px;
    background: var(--surface);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
}

.wrong-item-question {
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
}

.wrong-item-info {
    text-align: right;
}

.wrong-item-count {
    font-size: 14px;
    color: var(--error);
    font-weight: 600;
}

.wrong-item-date {
    font-size: 11px;
    color: var(--text-secondary);
}

.wrong-answer {
    color: var(--error);
    text-decoration: line-through;
    font-weight: 600;
}

.correct-answer {
    color: var(--success);
    font-weight: 600;
    margin-left: 8px;
}