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

:root {
    --bg: #f8fafc;
    --card: #fff;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
    --radius: 16px;
    --radius-sm: 12px;
    --text: #1e293b;
    --text-sub: #64748b;
    --border: #e2e8f0;
    --accent-pi: #4f46e5;
    --accent-e: #059669;
    --accent-sqrt2: #e11d48;
    --accent-phi: #b8860b;
    --correct: #16a34a;
    --wrong: #dc2626;
    --mono: 'SF Mono', 'Menlo', 'Consolas', monospace;
    --max-w: 480px;
    --accent: var(--accent-pi);
}

html { -webkit-text-size-adjust: 100%; }

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

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; border: none; background: none; }
input, select { font-family: inherit; }

/* ===== App Shell ===== */
.app {
    max-width: var(--max-w);
    margin: 0 auto;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    width: 100%;
    position: relative;
}

.app[data-const="pi"]    { --accent: var(--accent-pi); }
.app[data-const="e"]     { --accent: var(--accent-e); }
.app[data-const="sqrt2"] { --accent: var(--accent-sqrt2); }
.app[data-const="phi"]   { --accent: var(--accent-phi); }

/* ===== Header ===== */
.header {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    background: var(--card);
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow);
    min-height: 56px;
}

.header-left, .header-right {
    width: 40px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
}

.header-right { justify-content: flex-end; }

.header-center {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
}

.back-btn {
    color: var(--accent);
    padding: 4px;
    display: flex;
    align-items: center;
    font-size: 1.1rem;
}

.icon-btn {
    color: var(--text-sub);
    padding: 4px;
    display: flex;
    align-items: center;
    font-size: 1.2rem;
}

.icon-btn:hover { color: var(--text); }

/* ===== Constant Toggles ===== */
.const-toggles {
    display: flex;
    gap: 3px;
    background: var(--bg);
    padding: 3px;
    border-radius: 10px;
}

.const-btn {
    padding: 6px 16px;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-sub);
    transition: all 0.2s;
}

.const-btn:hover { color: var(--text); }

.const-btn.active {
    background: var(--accent);
    color: white;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.view-title {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text);
}

/* ===== Views ===== */
.view {
    display: none;
    flex-direction: column;
    flex: 1;
}

.view.active {
    display: flex;
    animation: viewIn 0.2s ease;
}

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

/* ===== HOME VIEW ===== */
.home-hero {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 24px 20px 8px;
    text-align: center;
}

.penguin {
    margin-bottom: 4px;
}

.penguin svg {
    width: 160px;
    height: auto;
    color: var(--accent);
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-6px); }
}

.home-title {
    font-size: 2.8rem;
    font-weight: 800;
    color: var(--accent);
    line-height: 1;
    margin-bottom: 2px;
}

.home-greeting {
    font-size: 0.88rem;
    color: var(--text-sub);
    margin-bottom: 2px;
}

.home-greeting .nickname {
    color: var(--accent);
    font-weight: 600;
}

.footnote-star {
    color: var(--accent);
    font-size: 0.7em;
    margin-left: 1px;
}

.home-user-desc, .setting-user-desc {
    font-size: 0.75rem;
    color: var(--text-sub);
    min-height: 1.1em;
}

.home-user-desc {
    margin-bottom: 12px;
}

/* Flowing digits */
.digits-flow {
    width: 100%;
    overflow: hidden;
    -webkit-mask-image: linear-gradient(90deg, transparent, black 10%, black 90%, transparent);
    mask-image: linear-gradient(90deg, transparent, black 10%, black 90%, transparent);
    padding: 4px 0 12px;
}

.digits-flow-track {
    font-family: var(--mono);
    font-size: 0.88rem;
    color: var(--accent);
    white-space: nowrap;
    display: inline-block;
    animation: flow 30s linear infinite;
    opacity: 0.45;
    letter-spacing: 1.5px;
}

@keyframes flow {
    from { transform: translateX(0); }
    to { transform: translateX(-50%); }
}

/* Action buttons */
.home-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    padding: 0 20px;
    width: 100%;
}

.action-btn {
    padding: 28px 16px;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 700;
    transition: all 0.15s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    box-shadow: var(--shadow);
}

.action-btn .action-sub {
    font-size: 0.72rem;
    font-weight: 500;
    opacity: 0.65;
    letter-spacing: 0.5px;
}

.action-btn.primary {
    background: var(--accent);
    color: white;
}

.action-btn.primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.action-btn.primary:active { transform: translateY(0); }

.action-btn.outline {
    background: var(--card);
    color: var(--accent);
    border: 2px solid var(--accent);
}

.action-btn.outline:hover {
    background: var(--accent);
    color: white;
}

.home-records-link {
    text-align: center;
    padding: 16px 20px 0;
}

.home-records-link button {
    font-size: 0.85rem;
    color: var(--text-sub);
    font-weight: 500;
}

.home-records-link button:hover { color: var(--accent); }

/* Footer */
.footer {
    padding: 20px;
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-sub);
    margin-top: auto;
}

.footer a:hover { color: var(--text); }
.footer .dot { margin: 0 6px; }

/* ===== SUB-VIEWS (memorize / search / records) ===== */
#memorizeView, #searchView, #recordsView {
    padding: 20px;
}

/* ===== Progress ===== */
.progress-section { margin-bottom: 16px; }

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

.progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 3px;
    width: 0%;
    transition: width 0.3s ease;
}

.stats {
    display: flex;
    justify-content: space-between;
    font-size: 0.82rem;
    color: var(--text-sub);
}

/* ===== Digits Display ===== */
.digits-display {
    font-family: var(--mono);
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--accent);
    word-break: break-all;
    line-height: 1.5;
    padding: 16px;
    background: var(--card);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    text-align: center;
    margin-bottom: 12px;
    min-height: 56px;
    max-height: 120px;
    overflow-y: auto;
}

.question-text {
    text-align: center;
    font-size: 0.9rem;
    color: var(--text-sub);
    margin-bottom: 16px;
}

/* ===== Choices Grid ===== */
.choices-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 12px;
}

.choice {
    padding: 14px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--card);
    font-family: var(--mono);
    font-size: 1.1rem;
    font-weight: 600;
    text-align: center;
    cursor: pointer;
    transition: all 0.15s;
    color: var(--text);
}

.choice:hover {
    border-color: var(--accent);
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.choice.correct { background: var(--correct); color: #fff; border-color: var(--correct); }
.choice.wrong { background: var(--wrong); color: #fff; border-color: var(--wrong); }
.choice.disabled { pointer-events: none; opacity: 0.7; }

/* ===== Keypad Grid ===== */
.keypad-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    max-width: 280px;
    margin: 0 auto 12px;
}

.keypad-key {
    padding: 16px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--card);
    font-size: 1.2rem;
    font-weight: 600;
    text-align: center;
    cursor: pointer;
    transition: all 0.12s;
    color: var(--text);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

.keypad-key:hover { border-color: var(--accent); background: #f1f5f9; }
.keypad-key:active { background: var(--border); transform: scale(0.97); }
.keypad-key.delete { background: #fef2f2; border-color: #fecaca; color: var(--wrong); }
.keypad-key.delete:hover { background: #fee2e2; }

/* ===== Result Message ===== */
.result-msg {
    text-align: center;
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-weight: 600;
    display: none;
    margin-top: 8px;
}

.result-msg.show { display: block; }
.result-msg.correct { background: #f0fdf4; color: var(--correct); }
.result-msg.wrong { background: #fef2f2; color: var(--wrong); }
.result-msg img { display: block; max-width: 180px; margin: 0 auto 8px; }

/* ===== Game Over Panel ===== */
.gameover-panel {
    display: none;
    margin-top: 20px;
    padding: 20px;
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.gameover-panel.show { display: block; }

.gameover-score img { display: block; max-width: 120px; margin: 0 auto 8px; }

.gameover-score {
    text-align: center;
    font-size: 0.95rem;
    color: var(--text-sub);
    margin-bottom: 16px;
    line-height: 1.6;
}

.gameover-score strong {
    color: var(--accent);
    font-size: 1.3rem;
}

.gameover-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 16px;
}

.gameover-actions .btn-outline {
    background: var(--card);
    color: var(--accent);
    border: 1.5px solid var(--accent);
}

.gameover-actions .btn-outline:hover {
    background: var(--accent);
    color: white;
}

.gameover-save {
    display: flex;
    gap: 8px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}

.gameover-save input {
    flex: 1;
    padding: 8px 12px;
    border: 1.5px solid var(--border);
    border-radius: 10px;
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.2s;
}

.gameover-save input:focus { border-color: var(--accent); }

/* ===== Records List ===== */
.records-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

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

.record-item-info { flex: 1; min-width: 0; }
.record-item-name { font-weight: 600; font-size: 0.9rem; }
.record-item-details { font-size: 0.8rem; color: var(--text-sub); margin-top: 2px; }

.record-item .btn-delete {
    padding: 6px 10px;
    border-radius: 8px;
    background: #fef2f2;
    color: var(--wrong);
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    flex-shrink: 0;
    margin-left: 8px;
}

.record-item .btn-delete:hover { background: #fee2e2; }

.empty-msg {
    text-align: center;
    color: var(--text-sub);
    font-size: 0.9rem;
    padding: 40px 0;
}

/* ===== Search ===== */
.search-desc {
    font-size: 0.88rem;
    color: var(--text-sub);
    line-height: 1.6;
    margin-bottom: 16px;
    text-align: center;
}

.search-desc img {
    display: block;
    max-width: 120px;
    margin: 0 auto 8px;
}

.search-desc span {
    color: var(--accent);
    font-weight: 700;
}

.search-input-group {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

.search-input {
    flex: 1;
    padding: 12px 14px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    font-family: var(--mono);
    text-align: center;
    outline: none;
    transition: border-color 0.2s;
}

.search-input:focus { border-color: var(--accent); }

.search-result-card {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
}

.search-result-card img { display: block; max-width: 120px; margin: 0 auto 8px; }
.search-result-card h4 { font-size: 0.95rem; color: var(--accent); margin-bottom: 8px; }
.search-result-card p { font-size: 0.9rem; color: var(--text-sub); line-height: 1.6; margin-bottom: 4px; }
.search-result-card strong { color: var(--text); }

.search-context {
    margin-top: 12px;
    padding: 12px;
    background: var(--bg);
    border-radius: 10px;
    font-family: var(--mono);
    font-size: 0.82rem;
    line-height: 1.8;
    text-align: center;
    word-break: break-all;
    letter-spacing: 0.5px;
}

.search-context .ctx-dim { color: var(--text-sub); }
.search-context .ctx-main { color: var(--text); font-weight: 600; }
.search-context mark {
    background: #fef08a;
    padding: 1px 3px;
    border-radius: 3px;
    font-weight: 700;
    color: var(--text);
}

/* ===== Modal ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 500;
    display: none;
    align-items: flex-end;
    justify-content: center;
}

.modal-overlay.show { display: flex; }

.modal {
    background: var(--card);
    border-radius: 20px 20px 0 0;
    width: 100%;
    max-width: var(--max-w);
    padding: 24px 20px 32px;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.12);
    animation: slideUp 0.25s ease;
}

@keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

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

.modal-header h3 { font-size: 1.1rem; font-weight: 700; }

.modal-close {
    font-size: 1.5rem;
    color: var(--text-sub);
    padding: 4px;
    line-height: 1;
}

.setting-group { margin-bottom: 16px; }

.setting-label {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-sub);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 6px;
}

.setting-value {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    background: var(--bg);
    border-radius: 10px;
    font-size: 0.95rem;
}

.setting-value span { flex: 1; }
.setting-value button { color: var(--accent); font-size: 0.85rem; font-weight: 600; padding: 4px 8px; }

.setting-user-desc {
    padding: 0 12px 4px;
}

.setting-name-edit {
    display: none;
    gap: 6px;
    margin-top: 8px;
}

.setting-name-edit.show { display: flex; }

.setting-name-edit input {
    flex: 1;
    padding: 8px 10px;
    border: 1.5px solid var(--border);
    border-radius: 8px;
    font-size: 0.9rem;
    outline: none;
}

.setting-name-edit input:focus { border-color: var(--accent); }

.setting-select {
    width: 100%;
    padding: 10px 12px;
    border: 1.5px solid var(--border);
    border-radius: 10px;
    font-size: 0.9rem;
    background: var(--card);
    color: var(--text);
    outline: none;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M3 5l3 3 3-3' stroke='%2364748b' fill='none' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 32px;
}

.setting-select:focus { border-color: var(--accent); }

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-weight: 600;
    border: none;
    transition: all 0.15s;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { opacity: 0.9; }
.btn-sm { padding: 8px 14px; font-size: 0.85rem; }
.btn-block { width: 100%; }

/* ===== Debug ===== */
.debug-answer {
    text-align: center;
    font-family: var(--mono);
    font-size: 0.8rem;
    color: var(--bg);
    background: var(--bg);
    border-radius: 6px;
    padding: 4px 8px;
    margin-bottom: 8px;
    cursor: pointer;
    user-select: none;
    transition: color 0.15s;
}

.debug-answer:hover {
    color: var(--text-sub);
}

/* ===== Utilities ===== */
.hidden { display: none !important; }

/* ===== Dark Mode ===== */
:root[data-theme="dark"] {
    --bg: #0f172a;
    --card: #1e293b;
    --shadow: 0 1px 3px rgba(0,0,0,0.4);
    --text: #e2e8f0;
    --text-sub: #94a3b8;
    --border: #334155;
    --accent-pi: #818cf8;
    --accent-e: #34d399;
    --accent-sqrt2: #fb7185;
    --accent-phi: #fbbf24;
    --correct: #22c55e;
    --wrong: #ef4444;
}

[data-theme="dark"] .keypad-key:hover { background: #334155; }
[data-theme="dark"] .keypad-key.delete { background: #3b1113; border-color: #7f1d1d; }
[data-theme="dark"] .keypad-key.delete:hover { background: #501418; }
[data-theme="dark"] .result-msg.correct { background: #052e16; }
[data-theme="dark"] .result-msg.wrong { background: #350a0a; }
[data-theme="dark"] .record-item .btn-delete { background: #3b1113; }
[data-theme="dark"] .record-item .btn-delete:hover { background: #501418; }
[data-theme="dark"] .search-context mark { background: #854d0e; color: #fef9c3; }
[data-theme="dark"] .modal-overlay { background: rgba(0,0,0,0.6); }

/* ===== Responsive ===== */
@media (min-width: 480px) {
    body {
        display: flex;
        justify-content: center;
    }
    .app {
        border-left: 1px solid var(--border);
        border-right: 1px solid var(--border);
    }
}
