/* CSS Variables for Theming */
:root {
    /* Light theme (default) */
    --bg-primary: #fafaf8;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f5f5f3;
    --text-primary: #1a1a1a;
    --text-secondary: #666666;
    --text-muted: #999999;
    --accent: #8b4513;
    --accent-hover: #a0522d;
    --border: #e0ddd5;
    --shadow: rgba(0, 0, 0, 0.08);
    --reader-bg: #fffffe;
    --word-color: #1a1a1a;
    --focal-guide: #8b4513;
    --progress-bg: #e8e4dc;
    --progress-fill: #8b4513;
}

[data-theme="sepia"] {
    --bg-primary: #f4ecd8;
    --bg-secondary: #faf6eb;
    --bg-tertiary: #ebe3d0;
    --text-primary: #3d3325;
    --text-secondary: #5c5041;
    --text-muted: #8a7f6d;
    --accent: #7c5e3c;
    --accent-hover: #8b6b45;
    --border: #d4c9b5;
    --shadow: rgba(60, 50, 30, 0.1);
    --reader-bg: #fdfaf3;
    --word-color: #2d2518;
    --focal-guide: #7c5e3c;
    --progress-bg: #e0d6c3;
    --progress-fill: #7c5e3c;
}

[data-theme="dark"] {
    --bg-primary: #1a1a1a;
    --bg-secondary: #252525;
    --bg-tertiary: #2d2d2d;
    --text-primary: #e8e6e3;
    --text-secondary: #a8a5a0;
    --text-muted: #706d68;
    --accent: #c9a86c;
    --accent-hover: #d4b87a;
    --border: #3d3d3d;
    --shadow: rgba(0, 0, 0, 0.3);
    --reader-bg: #1f1f1f;
    --word-color: #f0ede8;
    --focal-guide: #c9a86c;
    --progress-bg: #333333;
    --progress-fill: #c9a86c;
}

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

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    font-family: 'Crimson Pro', Georgia, 'Times New Roman', serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* App Container */
.app-container {
    max-width: 900px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Header */
.header {
    text-align: center;
    margin-bottom: 2.5rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border);
}

.logo {
    font-family: 'Playfair Display', Georgia, serif;
    font-size: 2.5rem;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: -0.02em;
    margin-bottom: 0.5rem;
}

.tagline {
    font-size: 1.125rem;
    color: var(--text-secondary);
    font-style: italic;
    font-weight: 300;
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

/* Reader Display */
.reader-display {
    background: var(--reader-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 3rem 2rem 2rem;
    box-shadow: 0 4px 20px var(--shadow);
    text-align: center;
}

.word-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 140px;
    position: relative;
}

.focal-guide {
    width: 8px;
    height: 8px;
    background: var(--focal-guide);
    opacity: 0.6;
    transform: rotate(45deg);
    transition: opacity 0.3s ease;
}

.focal-guide.hidden {
    opacity: 0;
}

.focal-guide-top {
    margin-bottom: 1rem;
}

.focal-guide-bottom {
    margin-top: 1rem;
}

.word-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 80px;
    width: 100%;
    position: relative;
    overflow: hidden;
}

.word-display {
    position: absolute;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    width: 100%;
    max-width: 100%;
    font-family: 'Crimson Pro', Georgia, serif;
    font-size: clamp(32px, 8vw, 48px);
    font-weight: 500;
    color: var(--word-color);
    letter-spacing: 0.02em;
    user-select: none;
    white-space: nowrap;
    /* GPU-accelerated transitions for smooth crossfade */
    opacity: 0;
    transition: opacity var(--transition-speed, 0.05s) ease-out;
    will-change: opacity;
}

.word-display-active {
    opacity: 1;
}

/* High-speed mode: disable transitions at very high WPM */
.word-display.no-transition {
    transition: none;
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    .word-display {
        transition: none;
    }
}

/* Three-part word structure for ORP centering */
.word-prefix {
    text-align: right;
    overflow: hidden;
    text-overflow: clip;
    color: var(--word-color);
    justify-self: end;
}

.word-orp {
    color: var(--accent);
    font-weight: 600;
    position: relative;
    justify-self: center;
    /* Visual marker for exact center */
}

.word-suffix {
    text-align: left;
    overflow: hidden;
    text-overflow: clip;
    color: var(--word-color);
    justify-self: start;
}

/* Progress Bar */
.progress-container {
    width: 100%;
    height: 4px;
    background: var(--progress-bg);
    border-radius: 2px;
    margin-top: 2rem;
    overflow: hidden;
}

.progress-bar {
    height: 100%;
    width: 0%;
    background: var(--progress-fill);
    border-radius: 2px;
    transition: width 0.1s ease;
}

.stats {
    margin-top: 1rem;
    font-size: 0.875rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.actual-wpm {
    margin-left: 1rem;
    padding: 0.2rem 0.5rem;
    background: var(--bg-tertiary);
    border-radius: 4px;
    font-size: 0.75rem;
}

.actual-wpm:empty {
    display: none;
}

/* Controls */
.controls {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    padding: 1.5rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 12px;
}

.control-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
}

.primary-controls {
    gap: 0.75rem;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    cursor: pointer;
    transition: all 0.2s ease;
    font-family: inherit;
}

.btn-control {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
}

.btn-control:hover {
    background: var(--border);
    color: var(--text-primary);
}

.btn-control svg {
    width: 20px;
    height: 20px;
}

.btn-play {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    box-shadow: 0 4px 12px rgba(139, 69, 19, 0.3);
}

.btn-play:hover {
    background: var(--accent-hover);
    transform: scale(1.05);
}

.btn-play svg {
    width: 28px;
    height: 28px;
}

.btn-play .icon-play {
    margin-left: 3px;
}

.btn-primary {
    padding: 0.875rem 2rem;
    font-size: 1rem;
    font-weight: 500;
    background: var(--accent);
    color: white;
    border-radius: 8px;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

/* Speed Control */
.speed-control {
    flex-direction: column;
    gap: 1rem;
}

.speed-label {
    font-size: 1.125rem;
    color: var(--text-secondary);
}

.speed-value {
    font-weight: 600;
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
}

.wpm-slider {
    width: 100%;
    max-width: 400px;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--progress-bg);
    border-radius: 3px;
    outline: none;
}

.wpm-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    transition: transform 0.2s ease;
}

.wpm-slider::-webkit-slider-thumb:hover {
    transform: scale(1.15);
}

.wpm-slider::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border: none;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.speed-presets {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    justify-content: center;
}

.preset-btn {
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    font-family: inherit;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.preset-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.preset-btn.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

/* Input Section */
.input-section {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
}

.input-tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
}

.tab-btn {
    flex: 1;
    padding: 1rem;
    font-family: inherit;
    font-size: 1rem;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: none;
    cursor: pointer;
    transition: all 0.2s ease;
}

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

.tab-btn.active {
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-weight: 500;
}

.tab-content {
    padding: 1.5rem;
}

.tab-content.hidden {
    display: none;
}

.text-input {
    width: 100%;
    padding: 1rem;
    font-family: 'Crimson Pro', Georgia, serif;
    font-size: 1rem;
    line-height: 1.7;
    color: var(--text-primary);
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: 8px;
    resize: vertical;
    margin-bottom: 1rem;
    transition: border-color 0.2s ease;
}

.text-input:focus {
    outline: none;
    border-color: var(--accent);
}

.text-input::placeholder {
    color: var(--text-muted);
    font-style: italic;
}

/* URL Input */
.url-input-wrapper {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.url-input {
    flex: 1;
    padding: 0.875rem 1rem;
    font-family: inherit;
    font-size: 1rem;
    color: var(--text-primary);
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: 8px;
    transition: border-color 0.2s ease;
}

.url-input:focus {
    outline: none;
    border-color: var(--accent);
}

.url-input::placeholder {
    color: var(--text-muted);
}

.url-hint {
    font-size: 0.8125rem;
    color: var(--text-muted);
    font-style: italic;
    margin-bottom: 1rem;
}

.btn-primary .btn-loading {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.btn-primary .btn-loading.hidden,
.btn-primary.loading .btn-text {
    display: none;
}

.btn-primary.loading .btn-loading {
    display: inline-flex;
}

.spinner {
    width: 16px;
    height: 16px;
    animation: spin 1s linear infinite;
}

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

/* Bookmarklet Section */
.bookmarklet-section {
    text-align: center;
    padding-bottom: 1.25rem;
}

.section-label {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.5rem;
}

.bookmarklet-desc {
    font-size: 0.8125rem;
    color: var(--text-secondary);
    margin-bottom: 1rem;
    line-height: 1.5;
}

.bookmarklet-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
}

.bookmarklet-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.25rem;
    font-family: inherit;
    font-size: 0.9375rem;
    font-weight: 500;
    color: white;
    background: linear-gradient(135deg, var(--accent) 0%, var(--accent-hover) 100%);
    border-radius: 8px;
    text-decoration: none;
    cursor: grab;
    box-shadow: 0 2px 8px rgba(139, 69, 19, 0.25);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.bookmarklet-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(139, 69, 19, 0.35);
}

.bookmarklet-btn:active {
    cursor: grabbing;
}

.bookmarklet-btn svg {
    width: 18px;
    height: 18px;
}

.bookmarklet-hint {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-style: italic;
    animation: bounce 2s ease-in-out infinite;
}

@keyframes bounce {
    0%, 100% { transform: translateX(0); }
    50% { transform: translateX(5px); }
}

.section-divider {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin: 1.25rem 0;
    color: var(--text-muted);
    font-size: 0.8125rem;
}

.section-divider::before,
.section-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}

.url-input-section {
    padding-top: 0.25rem;
}

/* Article Info */
.article-info {
    padding: 1rem;
    background: var(--bg-tertiary);
    border-radius: 8px;
    margin-top: 1rem;
}

.article-info.hidden {
    display: none;
}

.article-title {
    font-family: 'Playfair Display', Georgia, serif;
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.5rem;
    line-height: 1.4;
}

.article-meta {
    font-size: 0.8125rem;
    color: var(--text-muted);
    display: flex;
    gap: 1rem;
}

/* Upload Area */
.upload-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 2rem;
    border: 2px dashed var(--border);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.upload-area:hover,
.upload-area.dragover {
    border-color: var(--accent);
    background: var(--bg-tertiary);
}

.upload-icon {
    margin-bottom: 1rem;
    color: var(--text-muted);
}

.upload-icon svg {
    width: 48px;
    height: 48px;
}

.upload-text {
    font-size: 1.125rem;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
}

.upload-subtext {
    font-size: 0.875rem;
    color: var(--text-muted);
    margin-bottom: 1rem;
}

.upload-formats {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-style: italic;
}

.file-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem;
    background: var(--bg-tertiary);
    border-radius: 8px;
    margin-top: 1rem;
}

.file-info.hidden {
    display: none;
}

.file-name {
    font-weight: 500;
    color: var(--text-primary);
}

.btn-clear {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    background: var(--border);
    color: var(--text-secondary);
    border: none;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.2s ease;
}

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

/* Settings Section */
.settings-section {
    margin-top: auto;
}

.settings-panel {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
}

.settings-toggle {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem 1.5rem;
    font-family: inherit;
    font-size: 1rem;
    color: var(--text-secondary);
    cursor: pointer;
    list-style: none;
    transition: color 0.2s ease;
}

.settings-toggle::-webkit-details-marker {
    display: none;
}

.settings-toggle svg {
    width: 20px;
    height: 20px;
}

.settings-toggle:hover {
    color: var(--text-primary);
}

.settings-content {
    padding: 1rem 1.5rem 1.5rem;
    border-top: 1px solid var(--border);
    display: grid;
    gap: 1.25rem;
}

.setting-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.setting-group label {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.setting-select {
    padding: 0.625rem 1rem;
    font-family: inherit;
    font-size: 0.9375rem;
    color: var(--text-primary);
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    transition: border-color 0.2s ease;
}

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

.setting-slider {
    width: 100%;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--progress-bg);
    border-radius: 2px;
    outline: none;
}

.setting-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

.setting-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border: none;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--accent);
    cursor: pointer;
}

/* Footer */
.footer {
    margin-top: 2rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border);
    text-align: center;
}

.footer p {
    font-size: 0.875rem;
    color: var(--text-muted);
}

.footer a {
    color: var(--accent);
    text-decoration: none;
    transition: opacity 0.2s ease;
}

.footer a:hover {
    opacity: 0.8;
}

/* Responsive Design */
@media (max-width: 640px) {
    .app-container {
        padding: 1.5rem 1rem;
    }

    .logo {
        font-size: 2rem;
    }

    .tagline {
        font-size: 1rem;
    }

    .reader-display {
        padding: 2rem 1.5rem 1.5rem;
    }

    .word-display {
        font-size: clamp(28px, 7vw, 36px);
    }

    .btn-play {
        width: 56px;
        height: 56px;
    }

    .btn-control {
        width: 42px;
        height: 42px;
    }

    .speed-presets {
        gap: 0.375rem;
    }

    .preset-btn {
        padding: 0.375rem 0.75rem;
        font-size: 0.8125rem;
    }

    .tab-btn {
        padding: 0.75rem;
        font-size: 0.9375rem;
    }

    .bookmarklet-wrapper {
        flex-direction: column;
        gap: 0.5rem;
    }

    .bookmarklet-hint {
        animation: none;
    }

    .url-input-wrapper {
        flex-direction: column;
    }

    .url-input-wrapper .btn-primary {
        width: 100%;
    }
}

/* Extra small screens */
@media (max-width: 380px) {
    .app-container {
        padding: 1rem 0.75rem;
    }

    .logo {
        font-size: 1.75rem;
    }

    .reader-display {
        padding: 1.5rem 1rem 1rem;
    }

    .word-display {
        font-size: clamp(24px, 6vw, 32px);
    }

    .word-container {
        min-height: 100px;
    }

    .focal-guide {
        width: 6px;
        height: 6px;
    }

    .btn-play {
        width: 50px;
        height: 50px;
    }

    .btn-control {
        width: 38px;
        height: 38px;
    }

    .controls {
        padding: 1rem;
        gap: 1rem;
    }
}

/* Landscape orientation on mobile */
@media (max-height: 500px) and (orientation: landscape) {
    .app-container {
        padding: 1rem;
    }

    .header {
        margin-bottom: 1rem;
        padding-bottom: 0.75rem;
    }

    .logo {
        font-size: 1.5rem;
        margin-bottom: 0.25rem;
    }

    .tagline {
        font-size: 0.875rem;
    }

    .main-content {
        gap: 1rem;
    }

    .reader-display {
        padding: 1rem;
    }

    .word-container {
        min-height: 80px;
    }

    .word-wrapper {
        min-height: 60px;
    }

    .word-display {
        font-size: clamp(24px, 5vh, 36px);
    }

    .focal-guide-top {
        margin-bottom: 0.5rem;
    }

    .focal-guide-bottom {
        margin-top: 0.5rem;
    }

    .progress-container {
        margin-top: 1rem;
    }

    .stats {
        margin-top: 0.5rem;
    }

    .controls {
        padding: 0.75rem;
        gap: 0.75rem;
    }

    .btn-play {
        width: 48px;
        height: 48px;
    }

    .btn-control {
        width: 36px;
        height: 36px;
    }

    .input-section,
    .settings-section,
    .footer {
        display: none;
    }
}

/* Tablet landscape */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
    .word-display {
        font-size: clamp(36px, 5vw, 48px);
    }
}

/* Animation for word changes */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.word-display.animate {
    animation: fadeIn 0.1s ease-out;
}

/* Keyboard hints */
.keyboard-hint {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    font-family: monospace;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-muted);
}
