:root {
    --bg: #ffffff;
    --text: #141414;
    --muted: #767676;
    --surface: #f4f4f2;
    --surface-2: #ebebe8;
    --border: #e2e2de;
    --accent: #111111;
    --on-accent: #ffffff;
    --good: #1a7f37;
    --miss: #cf222e;
    --learning: #bf8700;
    --shadow: 0 1px 2px rgba(0,0,0,0.06), 0 4px 16px rgba(0,0,0,0.06);
    --radius: 16px;
}

[data-theme="dark"] {
    --bg: #111111;
    --text: #ececec;
    --muted: #9a9a9a;
    --surface: #1c1c1c;
    --surface-2: #262626;
    --border: #2e2e2e;
    --accent: #f2f2f2;
    --on-accent: #111111;
    --good: #4ac26b;
    --miss: #ff6b6b;
    --learning: #e3b341;
    --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 4px 16px rgba(0,0,0,0.3);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    margin: 0;
    line-height: 1.5;
    transition: background 0.3s, color 0.3s;
}

.app { max-width: 560px; margin: 0 auto; padding: 12px 16px 40px; padding-top: max(12px, env(safe-area-inset-top)); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
h2 { font-size: 1.35rem; margin: 0 0 8px; letter-spacing: -0.01em; }
h3.section-title { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 28px 0 10px; }

/* --- Top bar --- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 48px; margin-bottom: 16px; }
.topbar h2 { margin: 0; font-size: 1.1rem; flex: 1; text-align: center; }
.topbar-actions { display: flex; gap: 6px; }
.wordmark { font-size: 1.3rem; font-weight: 800; letter-spacing: -0.02em; margin: 0; }
.icon-btn {
    width: 44px; height: 44px; border-radius: 50%;
    border: 1px solid var(--border); background: var(--surface); color: var(--text);
    font-size: 1.1rem; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.topbar-spacer { width: 44px; flex-shrink: 0; }

/* --- Buttons --- */
.btn {
    font: inherit; font-size: 1rem; font-weight: 600; cursor: pointer; text-decoration: none;
    border-radius: 999px; padding: 12px 20px; border: 1px solid transparent;
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    transition: transform 0.1s, opacity 0.2s;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.big { width: 100%; min-height: 56px; font-size: 1.05rem; margin-top: 12px; }
.btn.huge { width: 100%; min-height: 72px; font-size: 1.2rem; }
.btn.good { background: var(--good); color: #fff; }
.btn.miss { background: var(--miss); color: #fff; }
.btn.listening { background: var(--miss); color: #fff; animation: pulse 1.4s ease-in-out infinite; }
.btn.danger { background: none; color: var(--miss); border-color: var(--border); }
@keyframes pulse { 50% { opacity: 0.75; } }
.link { background: none; border: none; color: inherit; text-decoration: underline; font: inherit; cursor: pointer; padding: 0; }
.row-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 24px; }
.row-actions .btn { flex: 1 1 auto; }

/* --- Forms --- */
label { display: block; font-weight: 600; font-size: 0.9rem; margin: 16px 0 6px; }
input[type=text], input[type=password], textarea, select {
    width: 100%; padding: 14px; font: inherit; font-size: 1rem;
    background: var(--bg); color: var(--text);
    border: 1px solid var(--border); border-radius: 12px;
}
textarea { resize: vertical; }
.visually-hidden { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.file-drop {
    display: flex; align-items: center; justify-content: center; text-align: center;
    min-height: 88px; padding: 16px; margin: 0; font-weight: 500;
    border: 2px dashed var(--border); border-radius: var(--radius); background: var(--surface); cursor: pointer;
}
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 0; border-bottom: 1px solid var(--border); }
.switch-row label { margin: 0; font-weight: 500; }
.switch-row small { display: block; color: var(--muted); font-weight: 400; }
.switch { position: relative; width: 52px; height: 32px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--surface-2); border-radius: 999px; transition: 0.2s; cursor: pointer; }
.switch span::before { content: ""; position: absolute; width: 26px; height: 26px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: 0.2s; box-shadow: 0 1px 3px rgba(0,0,0,0.3); }
.switch input:checked + span { background: var(--good); }
.switch input:checked + span::before { transform: translateX(20px); }
.status { color: var(--muted); font-size: 0.9rem; margin-top: 12px; min-height: 1.3em; }
.status.error { color: var(--miss); }

/* --- Cards and lists --- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 12px; }
.show-card {
    display: block; width: 100%; text-align: left; font: inherit; color: inherit; cursor: pointer;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 18px; margin-bottom: 12px;
}
.show-card strong { display: block; font-size: 1.1rem; margin-bottom: 2px; }
.show-card .muted { font-size: 0.9rem; }
.option-card {
    display: flex; gap: 16px; align-items: center; width: 100%; text-align: left; font: inherit; color: inherit; cursor: pointer;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 12px;
}
.option-card strong { display: block; font-size: 1.05rem; }
.option-card small { display: block; color: var(--muted); font-size: 0.9rem; margin-top: 2px; }
.option-emoji { font-size: 1.8rem; }
.role-badge {
    display: inline-block; background: var(--accent); color: var(--on-accent);
    padding: 4px 12px; border-radius: 999px; font-size: 0.75rem; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase;
}
.show-title { font-size: 1.7rem; margin: 0 0 8px; letter-spacing: -0.02em; }

/* --- Progress --- */
.bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--good); border-radius: 999px; transition: width 0.4s; }
.bar.big { height: 12px; }
.progress-hero { margin: 20px 0 8px; }
.progress-hero .bar { margin-bottom: 8px; }
.progress-text { font-size: 0.95rem; color: var(--muted); }
.progress-text strong { color: var(--text); }
.scene-row {
    display: flex; align-items: center; gap: 12px; width: 100%; font: inherit; color: inherit; text-align: left; cursor: pointer;
    background: none; border: none; border-bottom: 1px solid var(--border); padding: 14px 2px;
}
.scene-row .scene-name { flex: 1; min-width: 0; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scene-row .bar { width: 70px; flex-shrink: 0; }
.scene-row .scene-count { font-size: 0.85rem; color: var(--muted); width: 52px; text-align: right; flex-shrink: 0; }
.scene-row .done { color: var(--good); }
.streak { display: inline-flex; gap: 6px; align-items: center; font-weight: 600; font-size: 0.9rem; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 6px 14px; margin-bottom: 16px; }

/* --- Empty state and banners --- */
.empty { text-align: center; padding: 32px 8px 8px; }
.empty-emoji { font-size: 3rem; margin-bottom: 8px; }
.empty p { margin: 8px 0; }
.banner { display: flex; gap: 12px; align-items: flex-start; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px; font-size: 0.92rem; }
.banner .banner-body { flex: 1; }
.banner .banner-close { background: none; border: none; color: var(--muted); font-size: 1.2rem; cursor: pointer; padding: 0 4px; }
.share-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-top: 12px; }
.share-box code { display: block; word-break: break-all; font-size: 0.85rem; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px; margin: 8px 0; }

/* --- Character picker --- */
.pick-list { display: flex; flex-direction: column; gap: 8px; margin: 16px 0; }
.pick-item {
    display: flex; align-items: center; gap: 14px; margin: 0; font-weight: 500; cursor: pointer;
    background: var(--surface); border: 2px solid var(--border); border-radius: 14px; padding: 14px 16px;
}
.pick-item input { width: 22px; height: 22px; accent-color: var(--good); flex-shrink: 0; }
.pick-item:has(input:checked) { border-color: var(--good); }
.pick-item .pick-name { flex: 1; }
.pick-item .pick-count { color: var(--muted); font-size: 0.85rem; font-weight: 400; }

/* --- Practice --- */
.practice-top .practice-progress { flex: 1; display: flex; align-items: center; gap: 10px; }
.practice-top .practice-progress .bar { flex: 1; }
.practice-top .practice-progress span { font-size: 0.85rem; color: var(--muted); min-width: 44px; text-align: right; }
.scene-label { text-align: center; font-size: 0.8rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-bottom: 10px; min-height: 1.2em; }
.practice-card {
    background: var(--surface); border: 1px solid var(--border); border-radius: 22px; box-shadow: var(--shadow);
    padding: 24px 22px; min-height: 280px; display: flex; flex-direction: column; justify-content: center; text-align: center;
}
.context { font-size: 0.9rem; color: var(--muted); margin-bottom: 18px; text-align: left; }
.context p { margin: 0 0 6px; }
.speaker { font-size: 0.72rem; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
.context .speaker { display: inline; margin-right: 6px; }
.cue-text { font-size: 1.35rem; font-weight: 500; line-height: 1.45; }
.line-text { font-size: 1.3rem; font-weight: 600; line-height: 1.45; }
.hint-letters { margin-top: 16px; font-size: 1.2rem; letter-spacing: 0.08em; color: var(--muted); }
.heard { font-size: 0.9rem; color: var(--muted); font-style: italic; margin-top: 12px; min-height: 1.3em; }
.reveal { margin-top: 20px; padding-top: 20px; border-top: 1px solid var(--border); }
.check-result { font-weight: 700; margin-top: 12px; font-size: 1.05rem; }
.check-result.good { color: var(--good); }
.check-result.miss { color: var(--miss); }
.word-missed { color: var(--miss); text-decoration: underline wavy; text-underline-offset: 5px; }
.primary-area { margin-top: 16px; }
.grade-row { display: flex; gap: 10px; }
.grade-row .btn { flex: 1; min-height: 72px; font-size: 1.1rem; }
.fix-grade { text-align: center; margin-top: 10px; font-size: 0.9rem; color: var(--muted); min-height: 1.4em; }
.tool-row { display: flex; justify-content: space-around; margin-top: 18px; }
.tool {
    background: none; border: none; color: var(--text); cursor: pointer; font-size: 1.35rem;
    display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 10px; border-radius: 12px; min-width: 56px;
}
.tool span { font-size: 0.72rem; color: var(--muted); }
.tool:disabled { opacity: 0.35; }
.summary { text-align: center; }
.summary .big-number { font-size: 3rem; font-weight: 800; margin: 8px 0 0; }

/* --- Edit script --- */
.line-item { display: flex; align-items: center; border-bottom: 1px solid var(--border); padding: 12px 0; gap: 10px; }
.line-item.scene-break { background: var(--surface-2); border-radius: 10px; margin: 16px 0 4px; padding: 8px 10px; border: none; }
.line-item.dragging { opacity: 0.5; }
.line-item.editing { display: block; padding: 12px; background: var(--surface); border-radius: 12px; }
.handle { cursor: grab; color: var(--muted); font-size: 1.2rem; padding: 4px 6px; touch-action: none; user-select: none; -webkit-user-select: none; }
.line-content { flex-grow: 1; min-width: 0; font-size: 0.9rem; cursor: pointer; }
.line-content > div { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.line-content .cue-preview { color: var(--muted); }
.line-content.break-text { font-weight: 700; text-transform: uppercase; font-size: 0.8rem; }
.delete-line { background: none; border: none; color: var(--miss); font-size: 1.3rem; cursor: pointer; padding: 0 6px; }
.edit-actions { display: flex; gap: 10px; margin-top: 12px; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; background: var(--surface-2); border: 1px solid var(--border); }
.status-dot.learning { background: var(--learning); border-color: var(--learning); }
.status-dot.known { background: var(--good); border-color: var(--good); }
.inline-field { display: flex; gap: 10px; }
.inline-field input { flex: 1; }

/* --- Sheets --- */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 50; }
.sheet {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 60; margin: 0 auto; max-width: 560px;
    background: var(--bg); border-radius: 22px 22px 0 0; padding: 12px 20px 24px; padding-bottom: max(24px, env(safe-area-inset-bottom));
    max-height: 88vh; overflow-y: auto; box-shadow: 0 -8px 30px rgba(0,0,0,0.2);
}
.sheet-handle { width: 40px; height: 5px; border-radius: 999px; background: var(--border); margin: 0 auto 14px; }
.sheet h2 { text-align: center; }
.choice-list { display: flex; flex-direction: column; gap: 8px; }
.choice {
    display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; font: inherit; color: inherit; text-align: left; cursor: pointer;
    background: var(--surface); border: 2px solid var(--border); border-radius: 14px; padding: 14px 16px;
}
.choice.selected { border-color: var(--accent); }
.choice small { color: var(--muted); }
.segmented { display: flex; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 4px; gap: 4px; }
.segmented button { flex: 1; font: inherit; font-weight: 600; font-size: 0.92rem; border: none; background: none; color: var(--text); padding: 10px 6px; border-radius: 999px; cursor: pointer; }
.segmented button.selected { background: var(--accent); color: var(--on-accent); }
.how-hint { font-size: 0.85rem; color: var(--muted); text-align: center; margin: 8px 0 0; min-height: 1.3em; }

/* --- Toast and confetti --- */
.toast {
    position: fixed; left: 50%; top: max(16px, env(safe-area-inset-top)); transform: translateX(-50%); z-index: 80;
    background: var(--accent); color: var(--on-accent); padding: 12px 20px; border-radius: 999px; font-weight: 600;
    box-shadow: var(--shadow); max-width: calc(100% - 32px); text-align: center;
}
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 90; width: 100%; height: 100%; }
