/* ── Reset & Base ───────────────────────────────────────────────── */

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

:root {
    --bg:         #f8fafc;
    --surface:    #ffffff;
    --border:     #e2e8f0;
    --text:       #0f172a;
    --text-dim:   #64748b;
    --accent:     #6366f1;
    --accent-h:   #4f46e5;
    --accent-soft:#eef2ff;
    --word-color: #0f172a;
    --focus-color:#6366f1;
    --danger:     #ef4444;
    --success:    #22c55e;
    --radius:     12px;
    --shadow:     0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
    --font:       -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, sans-serif;
}

[data-theme="dark"] {
    --bg:         #0f172a;
    --surface:    #1e293b;
    --border:     #334155;
    --text:       #f1f5f9;
    --text-dim:   #94a3b8;
    --accent-soft:#1e1b4b;
    --word-color: #f1f5f9;
    --focus-color:#818cf8;
    --shadow:     0 1px 3px rgba(0,0,0,.3);
}

html {
    font-family: var(--font);
    font-size: 16px;
    color: var(--text);
    background: var(--bg);
    height: 100%;
    -webkit-text-size-adjust: 100%;
}

body {
    min-height: 100%;
    display: flex;
    flex-direction: column;
    transition: background .2s, color .2s;
}

/* ── Utility ────────────────────────────────────────────────────── */

.hidden { display: none !important; }

/* ── Header ─────────────────────────────────────────────────────── */

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    position: sticky;
    top: 0;
    z-index: 10;
}

.app-title {
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: -.02em;
}

.app-title span { color: var(--accent); }

.icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--border); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; fill: currentColor; }

/* ── Input Screen ───────────────────────────────────────────────── */

.input-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    max-width: 540px;
    width: 100%;
    margin: 0 auto;
    padding: 24px 20px 40px;
    gap: 20px;
}

.hero {
    text-align: center;
    padding: 16px 0 4px;
}

.hero-icon {
    font-size: 3rem;
    margin-bottom: 8px;
}

.hero h1 {
    font-size: 1.75rem;
    font-weight: 800;
    letter-spacing: -.03em;
}

.hero p {
    color: var(--text-dim);
    font-size: .95rem;
    margin-top: 6px;
    line-height: 1.5;
}

/* Sections */

.section-label {
    font-size: .85rem;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: .04em;
    margin-bottom: 6px;
}

textarea {
    width: 100%;
    min-height: 140px;
    padding: 12px;
    font-family: var(--font);
    font-size: .95rem;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    resize: vertical;
    outline: none;
    transition: border-color .15s;
}
textarea:focus { border-color: var(--accent); }
textarea::placeholder { color: var(--text-dim); opacity: .6; }

.est-time {
    font-size: .8rem;
    color: var(--text-dim);
    margin-top: 6px;
    min-height: 1.2em;
    font-variant-numeric: tabular-nums;
}

.url-row {
    display: flex;
    gap: 8px;
}

.url-row input {
    flex: 1;
    padding: 10px 12px;
    font-size: .95rem;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    outline: none;
    transition: border-color .15s;
}
.url-row input:focus { border-color: var(--accent); }
.url-row input::placeholder { color: var(--text-dim); opacity: .6; }

/* Buttons */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 20px;
    font-size: .95rem;
    font-weight: 600;
    border-radius: var(--radius);
    border: none;
    cursor: pointer;
    transition: background .15s, transform .1s, box-shadow .15s;
    user-select: none;
}
.btn:active { transform: scale(.97); }

.btn-primary {
    background: var(--accent);
    color: #fff;
    box-shadow: var(--shadow);
}
.btn-primary:hover { background: var(--accent-h); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }

.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--border); }

.btn-full { width: 100%; }

.btn-row {
    display: flex;
    gap: 8px;
}
.btn-row .btn { flex: 1; }

.btn svg { width: 18px; height: 18px; fill: currentColor; }

/* Speed slider */

.speed-control {
    display: flex;
    align-items: center;
    gap: 12px;
}

.speed-control label {
    font-size: .85rem;
    font-weight: 600;
    color: var(--text-dim);
    white-space: nowrap;
}

.speed-control .wpm-value {
    font-size: .85rem;
    font-weight: 700;
    color: var(--accent);
    min-width: 68px;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    flex: 1;
    height: 6px;
    background: var(--border);
    border-radius: 3px;
    outline: none;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: 2px solid var(--surface);
    box-shadow: var(--shadow);
}
input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: 2px solid var(--surface);
    box-shadow: var(--shadow);
}

/* ── Reading Screen ─────────────────────────────────────────────── */

.read-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    max-width: 640px;
    width: 100%;
    margin: 0 auto;
    padding: 16px 20px 24px;
}

/* Progress */

.progress-area {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.progress-track {
    height: 5px;
    background: var(--border);
    border-radius: 3px;
    overflow: hidden;
}

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

.progress-info {
    font-size: .8rem;
    color: var(--text-dim);
    text-align: center;
    font-variant-numeric: tabular-nums;
}

/* Seek slider */

.seek-area {
    padding: 4px 0 0;
}

.seek-area input[type="range"] {
    width: 100%;
    height: 4px;
}

/* Word display */

.word-area {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    padding: 24px 16px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.word {
    font-size: clamp(2.4rem, 10vw, 4.5rem);
    font-weight: 800;
    color: var(--word-color);
    text-align: center;
    line-height: 1.1;
    letter-spacing: -.02em;
    word-break: break-word;
    max-width: 100%;
    outline: none;
    user-select: none;
}

.word .focus-letter {
    color: var(--focus-color);
}

.word.finished {
    color: var(--text-dim);
    font-size: 1.5rem;
    font-weight: 600;
}

.word.finished .focus-letter {
    color: var(--text-dim);
}

/* Controls */

.controls {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.playback-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 20px;
}

.ctrl-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    color: var(--text);
    cursor: pointer;
    border-radius: 50%;
    transition: background .15s, opacity .15s, transform .1s;
}
.ctrl-btn:active { transform: scale(.9); }
.ctrl-btn:disabled { opacity: .25; cursor: not-allowed; }
.ctrl-btn svg { fill: currentColor; }

.ctrl-btn.small { width: 48px; height: 48px; }
.ctrl-btn.small svg { width: 22px; height: 22px; }
.ctrl-btn.small:hover:not(:disabled) { background: var(--border); }

.ctrl-btn.play-btn {
    width: 72px;
    height: 72px;
    background: var(--accent);
    color: #fff;
    box-shadow: 0 4px 12px rgba(99,102,241,.35);
}
.ctrl-btn.play-btn svg { width: 30px; height: 30px; fill: #fff; }
.ctrl-btn.play-btn:hover { background: var(--accent-h); }

.action-row {
    display: flex;
    gap: 8px;
}
.action-row .btn { flex: 1; font-size: .85rem; padding: 10px 12px; }

.btn-danger {
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
}
.btn-danger:hover { background: var(--danger); color: #fff; }

.btn-outline {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
}
.btn-outline:hover { background: var(--border); }

.shortcuts-hint {
    text-align: center;
    font-size: .75rem;
    color: var(--text-dim);
    opacity: .6;
    line-height: 1.6;
}

/* ── Done Screen ────────────────────────────────────────────────── */

.done-screen {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px 20px;
}

.done-card {
    text-align: center;
    max-width: 400px;
    width: 100%;
}

.done-icon {
    font-size: 4rem;
    margin-bottom: 12px;
}

.done-card h2 {
    font-size: 1.5rem;
    font-weight: 800;
    margin-bottom: 24px;
}

.stats-grid {
    display: flex;
    gap: 12px;
    margin-bottom: 32px;
}

.stat {
    flex: 1;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 8px;
}

.stat-value {
    font-size: 1.4rem;
    font-weight: 800;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
}

.stat-label {
    font-size: .75rem;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: .04em;
    margin-top: 4px;
}

.done-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* ── Install Hint ───────────────────────────────────────────────── */

.install-hint {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    width: 100%;
    padding: 10px;
    background: transparent;
    border: none;
    color: var(--text-dim);
    font-size: .85rem;
    font-weight: 500;
    cursor: pointer;
    border-radius: var(--radius);
    transition: background .15s, color .15s;
}
.install-hint:hover { background: var(--accent-soft); color: var(--accent); }
.install-hint svg { opacity: .7; }
.install-hint:hover svg { opacity: 1; }

/* ── Install Modal ──────────────────────────────────────────────── */

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 150;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,.45);
    backdrop-filter: blur(4px);
    padding: 20px;
}

.modal-card {
    background: var(--surface);
    border-radius: 20px;
    padding: 28px 24px 24px;
    max-width: 380px;
    width: 100%;
    box-shadow: 0 16px 48px rgba(0,0,0,.25);
}

.modal-card h3 {
    font-size: 1.2rem;
    font-weight: 800;
    text-align: center;
    margin-bottom: 20px;
}

.install-steps {
    list-style: none;
    counter-reset: step;
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin-bottom: 24px;
    padding: 0;
}

.install-steps li {
    counter-increment: step;
    display: flex;
    align-items: flex-start;
    gap: 12px;
    font-size: .93rem;
    line-height: 1.5;
    color: var(--text);
}

.install-steps li::before {
    content: counter(step);
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent);
    color: #fff;
    font-size: .8rem;
    font-weight: 700;
    border-radius: 50%;
}

.install-steps .step-icon { display: none; }

.share-icon-inline {
    display: inline-flex;
    vertical-align: middle;
    color: var(--accent);
    margin: 0 2px;
}

/* ── Drop Zone ──────────────────────────────────────────────────── */

.drop-zone {
    position: fixed;
    inset: 0;
    z-index: 90;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(99,102,241,.15);
    backdrop-filter: blur(4px);
}

.drop-zone-inner {
    background: var(--surface);
    border: 3px dashed var(--accent);
    border-radius: 24px;
    padding: 48px 64px;
    text-align: center;
    color: var(--accent);
}

.drop-zone-inner svg { margin-bottom: 12px; }

.drop-zone-inner p {
    font-size: 1.1rem;
    font-weight: 700;
}

/* ── Loading overlay ────────────────────────────────────────────── */

.loading-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,.4);
    backdrop-filter: blur(4px);
}

.loading-card {
    background: var(--surface);
    padding: 32px 40px;
    border-radius: 16px;
    text-align: center;
    box-shadow: 0 8px 32px rgba(0,0,0,.2);
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin .7s linear infinite;
    margin: 0 auto 12px;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-card p {
    font-size: .95rem;
    font-weight: 600;
    color: var(--text);
}

/* ── Toast ──────────────────────────────────────────────────────── */

.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--text);
    color: var(--bg);
    padding: 12px 24px;
    border-radius: 12px;
    font-size: .9rem;
    font-weight: 500;
    box-shadow: 0 8px 24px rgba(0,0,0,.25);
    opacity: 0;
    transition: transform .3s ease, opacity .3s ease;
    z-index: 200;
    max-width: calc(100% - 40px);
    text-align: center;
    pointer-events: none;
}

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

/* ── Responsive ─────────────────────────────────────────────────── */

@media (max-width: 480px) {
    .hero h1 { font-size: 1.4rem; }
    textarea { min-height: 100px; }
    .shortcuts-hint { display: none; }
    .stats-grid { gap: 6px; }
    .stat { padding: 12px 6px; }
    .stat-value { font-size: 1.1rem; }
}

/* ── Accessibility ──────────────────────────────────────────────── */

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ── Safe area (notch devices) ──────────────────────────────────── */

@supports (padding-top: env(safe-area-inset-top)) {
    .app-header { padding-top: max(12px, env(safe-area-inset-top)); }
    .input-screen, .read-screen, .done-screen { padding-bottom: max(24px, env(safe-area-inset-bottom)); }
}
