/* CSS Variables */
:root {
    --bg-primary: #fafaf7;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f5f5f2;
    --text-primary: #1a1a1a;
    --text-secondary: #5a5a5a;
    --text-muted: #8a8a8a;
    --accent: #e85d5d;
    --accent-hover: #ff6b6b;
    --accent-glow: rgba(232, 93, 93, 0.2);
    --border-color: rgba(0, 0, 0, 0.08);
    --guide-line: rgba(0, 0, 0, 0.06);
    --serif-font: 'Crimson Pro', Georgia, serif;
    --sans-font: 'Outfit', -apple-system, BlinkMacSystemFont, sans-serif;

    /* Aliases used by newer modules (library, import modal, top bar, etc.) */
    --bg: var(--bg-primary);
    --bg-elev: var(--bg-secondary);
    --text: var(--text-primary);
    --text-dim: var(--text-secondary);
    --border: var(--border-color);
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

/* Dark mode overrides */
:root[data-theme="dark"] {
    --bg-primary: #14141a;
    --bg-secondary: #1f1f27;
    --bg-tertiary: #2a2a32;
    --text-primary: #ececec;
    --text-secondary: #9a9aa3;
    --text-muted: #6a6a73;
    --accent: #ff7070;
    --accent-hover: #ff8585;
    --accent-glow: rgba(255, 112, 112, 0.3);
    --border-color: rgba(255, 255, 255, 0.08);
    --guide-line: rgba(255, 255, 255, 0.06);
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}

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

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--sans-font);
    background-color: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
}

/* App Container — flex row: sidebar + reader */
.app-container {
    height: 100vh;
    width: 100vw;
    display: flex;
}

/* ====================  Sidebar (ChatGPT-style)  ==================== */

.sidebar {
    width: 260px;
    min-width: 240px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: margin-left .25s ease;
}

.app-container.sidebar-collapsed .sidebar {
    margin-left: -260px;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border-color);
}
.sidebar-header .logo {
    font-family: var(--serif-font);
    font-size: 1.5rem;
    font-weight: 400;
    letter-spacing: -0.02em;
    margin: 0;
}
.sidebar-header .logo .accent { color: var(--accent); }

.sidebar-collapse, .sidebar-expand {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    width: 26px; height: 26px;
    border-radius: 6px; cursor: pointer; padding: 0;
    display: inline-flex; align-items: center; justify-content: center;
    transition: color .15s, border-color .15s;
}
.sidebar-collapse:hover, .sidebar-expand:hover { color: var(--text-primary); border-color: var(--text-secondary); }

.sidebar-expand {
    position: fixed; top: 14px; left: 14px; z-index: 50;
    background: var(--bg-secondary);
}
.sidebar-expand[hidden] { display: none; }

.sidebar-actions {
    display: flex; flex-direction: column; gap: 6px;
    padding: 12px 12px 8px;
}
.sidebar-btn {
    display: flex; align-items: center; gap: 8px;
    background: transparent; color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px; padding: 8px 12px;
    font-family: inherit; font-size: 13px; cursor: pointer;
    text-align: left;
    transition: background .15s, border-color .15s, color .15s;
}
.sidebar-btn:hover { background: var(--bg-tertiary); border-color: var(--text-secondary); }
.sidebar-btn.primary {
    background: var(--accent); color: white; border-color: transparent;
}
.sidebar-btn.primary:hover { filter: brightness(1.06); background: var(--accent); }

/* Scrollable middle area of the sidebar (holds both sections) */
.sidebar-scroll {
    flex: 1; overflow-y: auto;
    padding: 4px 0 8px;
}

/* Collapsible sections (Library / Pasted texts) */
.sidebar-section { padding: 4px 0; }
.sidebar-section-header {
    display: flex; align-items: center; gap: 6px;
    padding: 6px 16px;
    font-size: 11px; font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase; letter-spacing: 0.06em;
    cursor: pointer; user-select: none;
    list-style: none;
}
.sidebar-section-header::-webkit-details-marker { display: none; }
.sidebar-section .caret {
    transition: transform .15s;
    color: var(--text-muted);
}
.sidebar-section:not([open]) .caret { transform: rotate(-90deg); }

.sidebar-library, .sidebar-sessions {
    padding: 0 8px 8px;
}
.sidebar-library-empty {
    padding: 12px 16px;
    color: var(--text-secondary); font-size: 12px;
}
.sidebar-library-empty p { margin: 2px 0; }
.sidebar-library-empty .muted { color: var(--text-muted); font-size: 11px; }

/* Library items (with cover thumbnail) */
.lib-item {
    display: flex; align-items: center; gap: 10px;
    padding: 6px 28px 6px 10px; border-radius: 8px;
    cursor: pointer; position: relative;
    transition: background .12s;
    margin-bottom: 2px;
}
.lib-item:hover { background: var(--bg-tertiary); }
.lib-item.active { background: var(--bg-tertiary); }
.lib-item .lib-cover {
    width: 28px; height: 40px;
    border-radius: 3px; flex-shrink: 0;
    object-fit: cover;
    background: var(--bg-primary);
    box-shadow: var(--shadow);
}
.lib-item .lib-meta { flex: 1; min-width: 0; }
.lib-item .lib-title {
    font-size: 13px; color: var(--text-primary);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.lib-item .lib-sub {
    font-size: 11px; color: var(--text-muted);
    margin-top: 1px;
}

/* Top-right delete control shared by library items + session items */
.lib-delete, .session-delete {
    position: absolute; top: 4px; right: 4px;
    background: transparent; border: none; color: var(--text-muted);
    cursor: pointer; padding: 2px;
    width: 18px; height: 18px;
    border-radius: 50%;
    opacity: 0;
    transition: opacity .12s, color .12s, background .12s;
    font-size: 10px; line-height: 1;
    display: inline-flex; align-items: center; justify-content: center;
}
.lib-item:hover .lib-delete,
.session-item:hover .session-delete { opacity: 1; }
.lib-delete:hover, .session-delete:hover {
    color: white; background: var(--accent);
}

/* Paste-session items (no cover; chat-bubble icon + title) */
.session-item {
    display: flex; align-items: center; gap: 8px;
    padding: 6px 28px 6px 10px;
    border-radius: 8px;
    cursor: pointer; position: relative;
    transition: background .12s;
    margin-bottom: 2px;
}
.session-item:hover { background: var(--bg-tertiary); }
.session-item.active { background: var(--bg-tertiary); }
.session-icon { color: var(--text-muted); display: flex; flex-shrink: 0; }
.session-meta { flex: 1; min-width: 0; }
.session-title {
    font-size: 13px; color: var(--text-primary);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.sidebar-footer {
    border-top: 1px solid var(--border-color);
    padding: 12px;
}

/* Settings Row inside sidebar-footer */
.settings-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.setting-group {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}

.setting-group label {
    font-size: 0.65rem;
    font-weight: 500;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.setting-group select {
    font-family: var(--sans-font);
    font-size: 0.8rem;
    font-weight: 500;
    padding: 0.35rem 0.5rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-primary);
    cursor: pointer;
    transition: border-color 0.2s ease;
}

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

/* Paste-mode textarea inside main reader area */
.paste-input {
    padding: 16px 16px 0;
    flex-shrink: 0;
}
#text-input {
    width: 100%;
    height: 120px;
    font-family: var(--sans-font);
    font-size: 0.95rem;
    line-height: 1.6;
    padding: 0.8rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    resize: vertical;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
#text-input::placeholder { color: var(--text-muted); }
#text-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

/* Main reader area */
.reader-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    position: relative;
    min-width: 0;  /* allow shrinking below content width */
}

/* ====================  Mode visibility  ==================== */
/* Paste mode: textarea visible, doc UI hidden */
.app-container.mode-paste .reader-topbar,
.app-container.mode-paste .faithful-container { display: none !important; }
/* Doc mode: textarea hidden, RSVP hidden until fasty starts */
.app-container.mode-doc .paste-input { display: none; }
.app-container.mode-doc:not(.view-faithful) .rsvp-container { display: flex; }

/* Theme toggle button (inline inside the sidebar footer) */
.theme-toggle {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--accent);
    width: 28px;
    height: 28px;
    padding: 0;
    border-radius: 6px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    opacity: 0.85;
    transition: opacity 0.15s, transform 0.15s, border-color 0.15s;
    flex-shrink: 0;
}

.theme-toggle .theme-icon { display: flex; }

.theme-toggle:hover {
    opacity: 1;
    transform: scale(1.08);
}

/* RSVP Container */
.rsvp-container {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Fixed horizontal lines spanning the screen */
.guide-line-horizontal {
    position: absolute;
    left: 0;
    right: 0;
    height: 1px;
    background: var(--guide-line);
}

.guide-line-horizontal.top {
    top: 25%;
}

.guide-line-horizontal.bottom {
    bottom: 25%;
}

/* Fixed vertical line in center */
.guide-line-vertical {
    position: absolute;
    left: 50%;
    width: 1px;
    background: var(--guide-line);
    transform: translateX(-50%);
}

.guide-line-vertical.top {
    top: 25%;
    height: calc(25% - 70px);
}

.guide-line-vertical.bottom {
    bottom: 25%;
    height: calc(25% - 70px);
}

/* Word display */
.word-display {
    font-family: var(--serif-font);
    font-size: clamp(4rem, 10vw, 7rem);
    font-weight: 400;
    letter-spacing: 0.02em;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: baseline;
    white-space: nowrap;
    opacity: 0;
    transition: opacity 0.15s ease;
}

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

.word-display .word-before,
.word-display .word-after {
    color: var(--text-primary);
}

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

/* Navigation arrows */
.nav-arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: 50%;
    color: var(--text-muted);
    cursor: pointer;
    opacity: 0;
    transition: all 0.2s ease;
    z-index: 10;
}

.nav-arrow:hover {
    background: var(--bg-tertiary);
    border-color: var(--text-muted);
    color: var(--text-primary);
}

.nav-prev {
    left: 2rem;
}

.nav-next {
    right: 2rem;
}

/* Show arrows when reading has started */
.app-container.reading .nav-arrow,
.app-container.paused .nav-arrow {
    opacity: 1;
}

/* Status message - positioned below the RSVP area */
.status-message {
    text-align: center;
    padding: 1rem;
    opacity: 1;
    transition: opacity 0.15s ease;
}

.status-message.hidden {
    opacity: 0;
    pointer-events: none;
}

.status-text {
    font-size: 1rem;
    color: var(--text-muted);
}

.status-text.paragraph-break {
    color: var(--text-secondary);
}

.status-text kbd {
    display: inline-block;
    padding: 0.2em 0.5em;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-family: inherit;
    font-size: 0.9em;
}

/* Reader Footer */
.reader-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border-color);
    background: var(--bg-secondary);
}

.progress-container {
    height: 3px;
    background: var(--bg-tertiary);
    border-radius: 2px;
    overflow: hidden;
    margin-bottom: 0.75rem;
}

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

.reader-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.reader-info .hint {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.reader-info kbd {
    display: inline-block;
    padding: 0.15em 0.4em;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 3px;
    font-family: inherit;
    font-size: 0.9em;
}

/* State: Reading active */
.app-container.reading .status-message {
    opacity: 0;
    pointer-events: none;
}

.app-container.reading .word-display {
    opacity: 1;
}

/* State: Paused */
.app-container.paused .status-message {
    opacity: 1;
}

.app-container.paused .word-display {
    opacity: 0.3;
}

/* Scrollbar styling */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Responsive - stack on mobile */
@media (max-width: 768px) {
    .main-view {
        flex-direction: column;
    }
    
    .input-panel {
        width: 100%;
        min-width: 100%;
        height: 40vh;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }
    
    .reader-panel {
        height: 60vh;
    }
    
    .word-display {
        font-size: clamp(2.5rem, 12vw, 4rem);
    }
    
    .guide-line-vertical.top {
        height: calc(25% - 40px);
    }
    
    .guide-line-vertical.bottom {
        height: calc(25% - 40px);
    }
}

/* Very small screens */
@media (max-width: 480px) {
    .input-panel {
        padding: 1rem;
    }

    .logo {
        font-size: 1.5rem;
    }

    .header {
        padding-bottom: 0.75rem;
    }

    .reader-info .hint {
        display: none;
    }
}

/* Toast notifications */
.toast-stack {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 1000;
  pointer-events: none;
}
.toast {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  max-width: 420px;
  pointer-events: auto;
  animation: toast-in .2s ease-out;
}
.toast.toast-error { border-color: #e8765c; color: #e8765c; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Reader top bar */
/* Compact top bar — in-flow but slim (~32px). */
.reader-topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--text-dim);
  padding: 6px 56px 6px 16px; /* right padding leaves room for the theme toggle */
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 32px;
}
.reader-topbar[hidden] { display: none; }
.reader-topbar .doc-title {
  color: var(--text); font-weight: 500;
  max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.reader-topbar .muted { color: var(--text-dim); }
.topbar-exit {
  background: transparent; border: 1px solid var(--border); color: var(--text-dim);
  width: 22px; height: 22px; border-radius: 50%; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 11px; line-height: 1; padding: 0;
  transition: color .15s, border-color .15s;
}
.topbar-exit:hover { color: var(--text); border-color: var(--text-dim); }

/* Back-to-page button (visible only during fasty selection mode) */
.topbar-back {
  margin-left: auto; /* push toward the right, before the exit ✕ */
  display: inline-flex; align-items: center; gap: 5px;
  background: transparent; border: 1px solid var(--border); color: var(--text);
  padding: 3px 9px; border-radius: 999px; font-family: inherit;
  font-size: 12px; cursor: pointer; line-height: 1;
  transition: color .15s, border-color .15s, background .15s;
}
.topbar-back[hidden] { display: none; }
.topbar-back:hover { border-color: var(--accent); color: var(--accent); }
.topbar-back svg { display: inline-block; }

/* Header buttons */
.header { display: flex; align-items: center; justify-content: space-between; }
.header-actions { display: flex; gap: 8px; }
.btn-ghost, .btn-primary {
  border-radius: 6px; padding: 6px 12px; font-size: 14px; cursor: pointer;
  font-family: inherit;
}
.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { border-color: var(--text-dim); }
.btn-primary { background: var(--accent); color: white; border: none; }
.btn-primary:hover { filter: brightness(1.05); }

/* Modal styles */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 500;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--bg-elev); color: var(--text);
  border-radius: 12px; padding: 28px; min-width: 420px; max-width: 560px;
  box-shadow: var(--shadow); position: relative;
}
.modal h2 { margin: 0 0 16px; font-size: 18px; }
.modal-close {
  position: absolute; top: 12px; right: 12px;
  background: transparent; border: none; color: var(--text-dim);
  font-size: 16px; cursor: pointer;
}
.drop-zone {
  border: 2px dashed var(--border); border-radius: 10px;
  padding: 32px; text-align: center; transition: border-color .15s, background .15s;
}
.drop-zone.dragover { border-color: var(--accent); background: rgba(200,85,61,0.05); }
.drop-zone p { margin: 4px 0; }
.drop-zone .muted { color: var(--text-dim); font-size: 13px; }
.url-row { display: flex; gap: 8px; margin-top: 16px; }
.url-row input { flex: 1; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text); font-family: inherit; }
.import-progress { margin-top: 16px; }
.progress-bar-shell { height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; margin-top: 6px; }
.progress-bar-fill { height: 100%; background: var(--accent); width: 0; transition: width .2s; }

/* Library overlay */
.library-overlay {
  position: fixed; inset: 0;
  background: var(--bg);
  z-index: 600;
  display: flex; flex-direction: column;
  overflow: auto;
}
.library-overlay[hidden] { display: none; }
.library-topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 24px; border-bottom: 1px solid var(--border);
  position: relative;
}
.library-topbar h2 { margin: 0; font-size: 18px; }
.library-topbar .modal-close { position: static; }
.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 24px; padding: 24px;
}
.library-card {
  position: relative; cursor: pointer;
  display: flex; flex-direction: column; gap: 8px;
}
.library-card .cover {
  aspect-ratio: 2 / 3;
  background: var(--bg-elev);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: var(--shadow);
  position: relative;
  transition: transform .15s, box-shadow .15s;
}
.library-card:hover .cover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(0,0,0,0.12); }
.library-card .cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.library-card .cover .progress {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 3px; background: rgba(0,0,0,0.2);
}
.library-card .cover .progress > div {
  height: 100%; background: var(--accent);
}
.library-card .title {
  font-size: 13px; color: var(--text);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.library-card .delete {
  position: absolute; top: 4px; right: 4px;
  background: rgba(0,0,0,0.5); color: white; border: none;
  width: 24px; height: 24px; border-radius: 50%; cursor: pointer;
  opacity: 0; transition: opacity .15s; font-size: 12px;
}
.library-card:hover .delete { opacity: 1; }
.library-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; color: var(--text-dim);
}

.app-container.view-faithful .rsvp-container,
.app-container.view-faithful .status-message {
  display: none;
}
.app-container.view-faithful .faithful-container { display: block; }
.faithful-container { flex: 1; overflow: auto; padding: 24px; min-height: 0; }
.faithful-container[hidden] { display: none; }

/* Faithful Text View */
.ft-page {
  font-family: 'Crimson Pro', Georgia, serif;
  font-size: 19px; line-height: 1.7; color: var(--text-primary);
  max-width: 640px; margin: 0 auto;
  white-space: pre-wrap;
}
.ft-nav { display: flex; align-items: center; gap: 16px; justify-content: center; margin: 24px 0; }
.ft-pageinfo { color: var(--text-secondary); font-size: 13px; }

/* Faithful PDF View */
.fp-pages { display: flex; flex-direction: column; gap: 16px; align-items: center; }
.fp-page { background: white; box-shadow: 0 2px 8px rgba(0,0,0,0.1); border-radius: 4px; overflow: hidden; position: relative; }
:root[data-theme="dark"] .fp-page { background: #2a2a32; }

/* PDF text layer (invisible but selectable, overlaid on the canvas) */
.fp-textlayer {
  position: absolute; inset: 0;
  overflow: hidden; opacity: 0.25; line-height: 1.0;
  pointer-events: auto;
}
.fp-textlayer span, .fp-textlayer br {
  color: transparent; position: absolute; white-space: pre;
  cursor: text; transform-origin: 0 0;
}
.fp-textlayer ::selection { background: rgba(232, 93, 93, 0.35); color: transparent; }

/* Selection-read button (floating) */
.selection-read-btn {
  position: fixed; z-index: 1100;
  background: var(--accent); color: white; border: none;
  padding: 6px 12px; border-radius: 6px;
  font-family: inherit; font-size: 13px; font-weight: 500;
  cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.18);
  display: inline-flex; gap: 4px; align-items: center;
}
.selection-read-btn[hidden] { display: none; }
.selection-read-btn:hover { filter: brightness(1.06); }

/* Click-to-read affordance on faithful text pages and PDF pages */
.ft-page { cursor: pointer; }
.ft-page:hover { background: rgba(0,0,0,0.02); }
:root[data-theme="dark"] .ft-page:hover { background: rgba(255,255,255,0.02); }
.fp-page { cursor: pointer; }
.fp-page:hover { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ====================  Auth UI  ==================== */

/* Account chip in sidebar footer */
.account-chip {
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border-color);
}
.account-chip .sidebar-btn { width: 100%; justify-content: center; }
.account-empty {
  display: block; padding: 6px 8px;
  font-size: 11px; color: var(--text-muted); text-align: center;
}
.account-empty a { color: var(--accent); text-decoration: none; }
.account-empty a:hover { text-decoration: underline; }
.account-info {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px;
}
.account-email {
  flex: 1; min-width: 0;
  font-size: 12px; color: var(--text-secondary);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.account-signout {
  background: transparent; border: 1px solid var(--border-color);
  color: var(--text-secondary); cursor: pointer;
  width: 26px; height: 26px; border-radius: 6px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: color .15s, border-color .15s;
  padding: 0;
}
.account-signout:hover { color: var(--accent); border-color: var(--accent); }

/* Auth modal */
.auth-modal { min-width: 360px; max-width: 420px; }
.auth-modal h2 { margin: 0 0 6px; }
.auth-sub { margin: 0 0 16px; font-size: 13px; }
.auth-form { display: flex; flex-direction: column; gap: 10px; }
.auth-field { display: flex; flex-direction: column; gap: 4px; }
.auth-field > span {
  font-size: 11px; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.05em; font-weight: 500;
}
.auth-field input {
  padding: 8px 10px; border: 1px solid var(--border-color);
  border-radius: 6px; background: var(--bg);
  color: var(--text); font-family: inherit; font-size: 14px;
}
.auth-field input:focus { outline: none; border-color: var(--accent); }
.auth-error {
  background: rgba(232, 93, 93, 0.08); color: #e8765c;
  border: 1px solid rgba(232, 93, 93, 0.25);
  border-radius: 6px; padding: 8px 10px; font-size: 12px;
}
.auth-error[hidden] { display: none; }
.auth-submit { width: 100%; padding: 10px; font-size: 14px; }
.auth-submit:disabled { opacity: 0.6; cursor: wait; }
.auth-divider {
  display: flex; align-items: center; gap: 10px;
  margin: 16px 0; color: var(--text-muted); font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.05em;
}
.auth-divider::before, .auth-divider::after {
  content: ''; flex: 1; height: 1px; background: var(--border-color);
}
.auth-google { width: 100%; padding: 10px; font-size: 14px; }
.auth-switch {
  text-align: center; margin: 14px 0 0;
  font-size: 12px; color: var(--text-muted);
}
.auth-switch a { color: var(--accent); text-decoration: none; }
.auth-switch a:hover { text-decoration: underline; }

/* Mandatory sign-in gate — when no user is authenticated, the app body is
   blurred and inert behind the auth modal. */
body.auth-required .app-container > * {
  filter: blur(8px) brightness(0.6);
  pointer-events: none;
  user-select: none;
}
body.auth-required .modal-backdrop {
  filter: none;
  pointer-events: auto;
}

/* Onboarding modal — first-time-sign-in name + country + opt-in. */
.onboarding-modal { min-width: 420px; max-width: 480px; }
.onboarding-form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.onboarding-field { display: flex; flex-direction: column; gap: 4px; }
.onboarding-field > span {
  font-size: 11px; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.05em; font-weight: 500;
}
.onboarding-field input, .onboarding-field select {
  padding: 8px 10px; border: 1px solid var(--border-color); border-radius: 6px;
  background: var(--bg); color: var(--text); font-family: inherit; font-size: 14px;
}
.onboarding-check {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--text-secondary);
}
.onboarding-buttons {
  display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px;
}

/* Sidebar section cap badge — "X / cap" next to Library / Pasted texts headers. */
.section-cap-badge {
  margin-left: auto;
  font-size: 10px; font-weight: 500;
  padding: 2px 6px; border-radius: 999px;
  background: var(--bg-tertiary); color: var(--text-muted);
}
.section-cap-badge:empty { display: none; }
.section-cap-badge.at-cap { background: var(--accent); color: white; }

/* Leaderboard overlay — full-page table on top of the app. */
.leaderboard-overlay {
  position: fixed; inset: 0; background: var(--bg);
  z-index: 650; display: flex; flex-direction: column; overflow: auto;
}
.leaderboard-overlay[hidden] { display: none; }
.leaderboard-topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 16px 24px; border-bottom: 1px solid var(--border-color);
}
.leaderboard-topbar h2 { margin: 0; font-size: 18px; }
.lb-tabs {
  display: inline-flex; gap: 4px;
  border: 1px solid var(--border-color); border-radius: 8px; overflow: hidden;
}
.lb-tab {
  background: transparent; color: var(--text-secondary);
  padding: 4px 12px; border: none; cursor: pointer; font-size: 13px;
  font-family: inherit;
}
.lb-tab.active { background: var(--accent); color: white; }
.leaderboard-topbar .modal-close {
  margin-left: auto; position: static;
}
.leaderboard-body { padding: 24px; max-width: 720px; margin: 0 auto; width: 100%; box-sizing: border-box; }
.lb-row {
  display: grid; grid-template-columns: 40px 1fr 56px auto auto;
  gap: 16px; align-items: center;
  padding: 10px 12px; border-radius: 8px;
  border-bottom: 1px solid var(--border-color);
}
.lb-row.self { background: var(--bg-tertiary); }
.lb-row.lb-header {
  font-size: 10px; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.06em;
  border-bottom: 1px solid var(--border-color); border-radius: 0;
  padding: 8px 12px;
}
.lb-row.lb-header .lb-streak,
.lb-row.lb-header .lb-wpm,
.lb-row.lb-header .lb-meta { font-size: 10px; font-weight: 500; color: var(--text-muted); }
.lb-rank { font-weight: 600; color: var(--text-muted); }
.lb-name { font-weight: 500; }
.lb-name .country { color: var(--text-muted); font-size: 12px; margin-left: 6px; }
.lb-streak { font-size: 13px; font-weight: 600; color: #ff8a3d; text-align: center; }
.lb-streak .streak-unit { font-size: 10px; color: var(--text-muted); margin-left: 1px; font-weight: 500; }
.lb-wpm { font-size: 18px; font-weight: 600; color: var(--accent); }
.lb-wpm .wpm-unit { font-size: 10px; color: var(--text-muted); font-weight: 500; }
.lb-meta { font-size: 11px; color: var(--text-muted); text-align: right; }
.lb-empty { text-align: center; color: var(--text-muted); padding: 40px 0; }

/* Upgrade-to-Pro CTA in the sidebar footer + modal. */
.upgrade-cta {
  width: 100%;
  background: linear-gradient(135deg, var(--accent), #ff8585);
  color: white; border: none; padding: 10px;
  border-radius: 8px; cursor: pointer;
  font-family: inherit; font-size: 13px; font-weight: 500;
  margin-bottom: 10px;
  transition: filter .15s;
}
.upgrade-cta:hover { filter: brightness(1.06); }

.upgrade-modal { min-width: 420px; max-width: 480px; }
.upgrade-features {
  list-style: none; padding: 0; margin: 16px 0;
  display: flex; flex-direction: column; gap: 6px;
  font-size: 13px;
}
.upgrade-soon { font-size: 13px; color: var(--text-secondary); margin-top: 16px; }
.upgrade-form { display: flex; gap: 8px; margin-top: 8px; }
.upgrade-form input {
  flex: 1; padding: 8px 10px;
  border: 1px solid var(--border-color); border-radius: 6px;
  background: var(--bg); color: var(--text);
  font-family: inherit;
}
.upgrade-thanks { color: var(--accent); margin-top: 8px; font-size: 13px; }
.upgrade-buy { width: 100%; padding: 12px; font-size: 14px; margin-top: 8px; }
.upgrade-waiting { margin-top: 12px; }
.upgrade-waiting p { margin: 0 0 8px; font-size: 13px; }
.upgrade-waiting .btn-ghost { width: 100%; }
.upgrade-success {
  color: var(--accent); margin-top: 12px;
  font-size: 14px; text-align: center;
  padding: 12px; border: 1px solid var(--accent); border-radius: 8px;
}

