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

:root {
    --bg: #f7f4e3;
    --card: #fdfbee;
    --shadow: 3px 3px 0 #c4b49a;
    --radius: 4px;
    --radius-sm: 3px;
    --text: #2a2010;
    --text-sub: #7a6a4a;
    --border: #b0a07a;
    --line-color: #e0d5be;
    --accent-pi: #2e5490;
    --accent-e: #1f6b42;
    --accent-sqrt2: #8c2f2f;
    --accent-phi: #7a5e10;
    --correct: #1f6b42;
    --wrong: #8c2f2f;
    --mono: 'EB Garamond', Georgia, serif;
    --max-w: 480px;
    --accent: var(--accent-pi);
}

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

body {
    font-family: 'EB Garamond', Georgia, serif;
    background-color: var(--bg);
    background-image: repeating-linear-gradient(
        to bottom,
        transparent 0px,
        transparent 27px,
        var(--line-color) 28px
    );
    background-attachment: fixed;
    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;
    border-bottom: 2px solid var(--border);
    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.2rem;
    font-weight: 700;
}

.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: 2px;
    background: var(--bg);
    padding: 3px;
    border-radius: 5px;
    border: 2px solid var(--border);
}

.const-btn {
    padding: 5px 14px;
    border-radius: 3px;
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-sub);
    transition: all 0.15s;
}

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

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

.view-title {
    font-size: 1.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);
    filter: url(#pencil-sketch);
}

.home-title {
    font-size: 3rem;
    font-weight: 700;
    color: var(--accent);
    line-height: 1;
    margin-bottom: 20px;
}

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

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

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

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

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

/* Background digits */
#homeView {
    position: relative;
}

.digits-flow {
    position: absolute;
    inset: 0;
    overflow: hidden;
    pointer-events: none;
}

.digits-flow-track {
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--accent);
    line-height: 1.85;
    letter-spacing: 0.06em;
    word-break: break-all;
    white-space: normal;
    opacity: 0.18;
    display: block;
    padding: 10px 14px;
}


/* 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: 1.1rem;
    font-weight: 700;
    transition: all 0.1s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    border: 2px solid transparent;
}

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

.action-btn.primary:hover  { transform: translate(1px,1px); box-shadow: 2px 2px 0 #c4b49a; }
.action-btn.primary:active { transform: translate(3px,3px); box-shadow: none; }

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

.action-btn.outline:hover  { transform: translate(1px,1px); box-shadow: 2px 2px 0 #c4b49a; }
.action-btn.outline:active { transform: translate(3px,3px); box-shadow: none; }

.action-btn-full {
    grid-column: span 2;
    padding: 20px 16px;
}

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

.footer a:hover { color: var(--text); }
.footer .dot { margin: 0 6px; }
.footer span:last-child { white-space: nowrap; }

/* ===== SUB-VIEWS ===== */
#memorizeView, #searchView, #recordsView {
    padding: 20px;
}

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

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

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

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

/* ===== Digits Display ===== */
.digits-display {
    font-family: var(--mono);
    font-size: 1.3rem;
    font-weight: 600;
    color: var(--accent);
    word-break: break-all;
    line-height: 1.6;
    padding: 16px;
    background: var(--card);
    border: 2px solid var(--border);
    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: 1rem;
    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: 2px 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.1s;
    color: var(--text);
    box-shadow: var(--shadow);
}

.choice:hover  { border-color: var(--accent); transform: translate(1px,1px); box-shadow: 2px 2px 0 #c4b49a; }
.choice:active { transform: translate(3px,3px); box-shadow: none; }
.choice.correct { background: var(--correct); color: #fff; border-color: var(--correct); box-shadow: none; }
.choice.wrong   { background: var(--wrong);   color: #fff; border-color: var(--wrong);   box-shadow: none; }
.choice.disabled { pointer-events: none; opacity: 0.8; }

/* ===== 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: 2px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--card);
    font-family: var(--mono);
    font-size: 1.3rem;
    font-weight: 700;
    text-align: center;
    cursor: pointer;
    transition: all 0.1s;
    color: var(--text);
    box-shadow: var(--shadow);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

.keypad-key:hover  { border-color: var(--accent); transform: translate(1px,1px); box-shadow: 2px 2px 0 #c4b49a; }
.keypad-key:active { transform: translate(3px,3px); box-shadow: none; }
.keypad-key.delete { background: #fff0ee; border-color: #d4a0a0; color: var(--wrong); box-shadow: 3px 3px 0 #c4a0a0; }
.keypad-key.delete:hover { background: #ffe4e0; transform: translate(1px,1px); box-shadow: 2px 2px 0 #c4a0a0; }

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

.result-msg.show { display: block; }
.result-msg.correct { background: #eaf7ee; color: var(--correct); border-color: var(--correct); }
.result-msg.wrong   { background: #fdf0ee; color: var(--wrong);   border-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: 2px solid var(--border);
    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: 1rem;
    color: var(--text-sub);
    margin-bottom: 16px;
    line-height: 1.6;
}

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

.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: 2px solid var(--accent);
    box-shadow: var(--shadow);
}

.gameover-actions .btn-outline:hover  { transform: translate(1px,1px); box-shadow: 2px 2px 0 #c4b49a; }
.gameover-actions .btn-outline:active { transform: translate(3px,3px); box-shadow: none; }

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

.gameover-save input {
    flex: 1;
    padding: 8px 12px;
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    outline: none;
    background: var(--bg);
    color: var(--text);
    transition: border-color 0.2s;
}

.gameover-save input:focus { border-color: var(--accent); }
.gameover-save .btn { white-space: nowrap; min-width: 82px; }

/* ===== Records Tabs ===== */
.records-tabs {
    display: flex;
    gap: 2px;
    margin-bottom: 16px;
    background: var(--bg);
    padding: 4px;
    border-radius: 5px;
    border: 2px solid var(--border);
}

.tab-btn {
    flex: 1;
    padding: 7px 4px;
    border-radius: 3px;
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-sub);
    transition: all 0.15s;
    text-align: center;
}

.tab-btn.active {
    background: var(--accent);
    color: white;
    box-shadow: 2px 2px 0 rgba(0,0,0,0.2);
}

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

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

.record-item-rank {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-sub);
    min-width: 28px;
    text-align: center;
    flex-shrink: 0;
}

.record-item-me { border-left: 4px solid var(--accent); }

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

.record-item .btn-delete {
    padding: 5px 10px;
    border-radius: var(--radius-sm);
    border: 2px solid #d4a0a0;
    background: #fff0ee;
    color: var(--wrong);
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
    flex-shrink: 0;
    margin-left: 8px;
}

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

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

/* ===== Search ===== */
.search-desc {
    font-size: 1rem;
    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: 2px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1.1rem;
    font-family: var(--mono);
    text-align: center;
    outline: none;
    background: var(--card);
    color: var(--text);
    transition: border-color 0.2s;
    box-shadow: var(--shadow);
}

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

.search-result-card {
    background: var(--card);
    border: 2px solid var(--border);
    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: 1.1rem; color: var(--accent); margin-bottom: 8px; font-weight: 700; }
.search-result-card p { font-size: 0.95rem; 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: 2px solid var(--border);
    border-radius: var(--radius-sm);
    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: 700; }
.search-context mark {
    background: #f5e060;
    padding: 1px 3px;
    border-radius: 2px;
    font-weight: 700;
    color: var(--text);
}

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

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

.modal {
    background: var(--card);
    border-top: 2px solid var(--border);
    border-left: 2px solid var(--border);
    border-right: 2px solid var(--border);
    border-radius: 8px 8px 0 0;
    width: 100%;
    max-width: var(--max-w);
    padding: 24px 20px 32px;
    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;
    padding-bottom: 12px;
    border-bottom: 2px solid var(--border);
}

.modal-header h3 { font-size: 1.3rem; 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.85rem;
    font-weight: 700;
    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: 2px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
}

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

.setting-user-desc { padding: 0 12px 4px; font-size: 0.85rem; }

.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: 2px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    outline: none;
    background: var(--bg);
    color: var(--text);
}

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

.setting-select {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    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='%237a6a4a' 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;
    box-shadow: var(--shadow);
}

.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: 1rem;
    font-weight: 700;
    border: 2px solid transparent;
    transition: all 0.1s;
    font-family: inherit;
}

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

.btn-primary:hover  { transform: translate(1px,1px); box-shadow: 2px 2px 0 #c4b49a; }
.btn-primary:active { transform: translate(3px,3px); box-shadow: none; }
.btn-sm { padding: 7px 14px; font-size: 0.9rem; }
.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: 3px;
    padding: 4px 8px;
    margin-bottom: 8px;
    cursor: pointer;
    user-select: none;
    transition: color 0.15s;
    border: 1px dashed transparent;
}

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

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

/* ===== Dark Mode ===== */
:root[data-theme="dark"] {
    --bg: #1a3020;
    --card: #152818;
    --shadow: 3px 3px 0 #0c1a10;
    --text: #f0ede0;
    --text-sub: #90b090;
    --border: #2e5c2e;
    --line-color: #1f3a26;
    --accent-pi: #7a9fd4;
    --accent-e: #5abf8a;
    --accent-sqrt2: #d47a7a;
    --accent-phi: #c8a040;
    --correct: #5abf8a;
    --wrong: #d47a7a;
}

[data-theme="dark"] .keypad-key:hover { background: #1f3a26; }
[data-theme="dark"] .keypad-key.delete { background: #3a2020; border-color: #6a3030; box-shadow: 3px 3px 0 #261414; }
[data-theme="dark"] .keypad-key.delete:hover { background: #4a2828; }
[data-theme="dark"] .result-msg.correct { background: #0e2418; border-color: var(--correct); }
[data-theme="dark"] .result-msg.wrong   { background: #3a1a1a; border-color: var(--wrong); }
[data-theme="dark"] .record-item .btn-delete { background: #3a2020; border-color: #6a3030; }
[data-theme="dark"] .record-item .btn-delete:hover { background: #4a2828; }
[data-theme="dark"] .search-context mark { background: #5a4c00; color: #ffe87a; }
[data-theme="dark"] .modal-overlay { background: rgba(0,0,0,0.7); }
[data-theme="dark"] .gameover-save input { background: var(--bg); }
[data-theme="dark"] .setting-select { background: var(--card); }

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