/* ╔═══════════════════════════════════════════════╗
   ║  RSVP Reader — stylesheet v3                  ║
   ╚═══════════════════════════════════════════════╝ */

:root {
  --bg:          #0d0d0d;
  --text:        #e8e8e8;
  --orp:         #e74c3c;
  --ctx:         #555555;
  --accent:      #3498db;
  --surface:     #1a1a1a;
  --surface2:    #252525;
  --surface3:    #2e2e2e;
  --border:      #333333;
  --danger:      #c0392b;

  --font-word:   42px;
  --font-ctx:    14px;
  --font-family: 'Courier New', monospace;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-family);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ── Screens ─────────────────────────────────────── */
.screen        { display: none; height: 100dvh; }
.screen.active { display: flex; flex-direction: column; }
.hidden        { display: none !important; }

/* ════════════════════════════════════════
   AUTH SCREEN
════════════════════════════════════════ */
#auth-screen {
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  padding: 2rem 1rem 3rem;
}

.auth-hero {
  width: 100%; max-width: 360px;
  display: flex; flex-direction: column;
  align-items: center; gap: 1.1rem;
}

.auth-hero .logo { font-size: 3rem; }
.auth-hero h1    { font-size: 1.8rem; font-weight: 700; letter-spacing: -.03em; }
.auth-hero .subtitle { color: var(--ctx); font-size: .9rem; }

/* Tabs */
.auth-tabs {
  display: flex; width: 100%;
  background: var(--surface2);
  border-radius: 10px; padding: 3px; gap: 3px;
}
.auth-tab {
  flex: 1; background: none; border: none;
  color: var(--ctx); font-size: .9rem; font-weight: 600;
  padding: .55rem; border-radius: 8px; cursor: pointer;
  transition: background .15s, color .15s;
}
.auth-tab.active { background: var(--surface3); color: var(--text); }

/* Forms */
.auth-form {
  display: none; flex-direction: column;
  gap: .75rem; width: 100%;
}
.auth-form.active { display: flex; }
.auth-form label {
  display: flex; flex-direction: column; gap: .3rem;
  font-size: .85rem; color: var(--ctx);
}
.auth-form input[type="text"],
.auth-form input[type="password"] {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 10px; color: var(--text);
  font-family: inherit; font-size: .95rem;
  padding: .65rem .85rem; width: 100%;
  transition: border-color .15s;
}
.auth-form input:focus { outline: none; border-color: var(--accent); }

.checkbox-label {
  flex-direction: row !important; align-items: center !important;
  gap: .5rem !important; cursor: pointer;
}
.checkbox-label input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; }
.checkbox-label span { color: var(--text) !important; font-size: .86rem !important; }

/* ════════════════════════════════════════
   UPLOAD / HOME SCREEN
════════════════════════════════════════ */
#upload-screen {
  align-items: center;
  justify-content: flex-start;
  overflow-y: auto;
  padding: 1.4rem 1rem 4rem;
}
.upload-hero {
  width: 100%; max-width: 480px;
  display: flex; flex-direction: column;
  align-items: center; gap: 1rem;
}
.home-header {
  width: 100%; display: flex;
  align-items: center; justify-content: space-between;
}
.logo-row      { display: flex; align-items: center; gap: .5rem; }
.logo-sm       { font-size: 1.9rem; line-height: 1; }
h1             { font-size: 1.7rem; font-weight: 700; letter-spacing: -.03em; }
.home-header-right { display: flex; align-items: center; gap: .3rem; }
.home-username {
  font-size: .8rem; color: var(--ctx); padding: .2rem .5rem;
  background: var(--surface2); border-radius: 6px; max-width: 100px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* Drop zone */
.drop-zone {
  display: block; width: 100%;
  border: 2px dashed var(--border); border-radius: 16px;
  padding: 1.6rem 1rem; cursor: pointer;
  transition: border-color .2s, background .2s; text-align: center;
}
.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--accent); background: rgba(52,152,219,.06);
}
.drop-zone input[type="file"] { display: none; }
.drop-inner { display: flex; flex-direction: column; gap: .4rem; align-items: center; }
.drop-icon  { font-size: 2rem; }
.drop-label { font-size: .98rem; font-weight: 600; color: var(--text); }
.drop-hint  { font-size: .76rem; color: var(--ctx); }

/* Paste */
.paste-area { width: 100%; display: flex; flex-direction: column; gap: .6rem; }
.paste-area textarea {
  width: 100%; background: var(--surface2);
  border: 1px solid var(--border); border-radius: 10px;
  color: var(--text); font-family: inherit;
  font-size: .88rem; padding: .65rem; resize: vertical;
}

/* Recent */
.recent-section { width: 100%; }
.recent-section h3 {
  color: var(--ctx); font-size: .73rem;
  text-transform: uppercase; letter-spacing: .08em; margin-bottom: .45rem;
}
#recent-list { list-style: none; display: flex; flex-direction: column; gap: .3rem; }
#recent-list li {
  background: var(--surface2); border-radius: 10px;
  padding: .55rem .85rem;
  display: flex; align-items: center; gap: .6rem;
  cursor: pointer; transition: background .15s;
}
#recent-list li:hover { background: var(--surface3); }
.recent-cover {
  width: 32px; height: 42px; flex-shrink: 0;
  background: var(--surface3); border-radius: 4px;
  display: flex; align-items: center; justify-content: center; font-size: 1rem;
}
.recent-info { flex: 1; overflow: hidden; min-width: 0; }
.recent-name {
  font-size: .84rem; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block;
}
.recent-meta { font-size: .7rem; color: var(--ctx); display: flex; gap: .45rem; margin-top: .12rem; }
.recent-pct-bar {
  height: 3px; background: var(--border); border-radius: 2px; margin-top: .25rem; overflow: hidden;
}
.recent-pct-fill { height: 100%; background: var(--accent); border-radius: 2px; }

/* ── Shared buttons ───────────────────────────── */
.btn-primary, .btn-secondary, .btn-danger {
  border: none; border-radius: 12px;
  padding: .7rem 1.3rem;
  font-size: .9rem; font-weight: 600; cursor: pointer; width: 100%;
  transition: opacity .15s, transform .1s;
}
.btn-primary   { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-danger    { background: var(--danger); color: #fff; }
.btn-primary:active, .btn-secondary:active, .btn-danger:active { transform: scale(.97); }

.spinner {
  width: 30px; height: 30px;
  border: 3px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-msg {
  color: #e74c3c; font-size: .83rem;
  background: rgba(231,76,60,.1);
  border-radius: 8px; padding: .5rem .85rem;
  width: 100%; text-align: center;
}

/* ════════════════════════════════════════
   READER SCREEN
════════════════════════════════════════ */
#reader-screen { justify-content: space-between; overflow: hidden; }

.reader-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .4rem .65rem;
  background: var(--surface); border-bottom: 1px solid var(--border);
  min-height: 44px; flex-shrink: 0;
}
.filename-display {
  font-size: .76rem; color: var(--ctx);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  max-width: 38vw; flex: 1; margin: 0 .4rem;
}
.header-right { display: flex; gap: .2rem; align-items: center; }
.lang-badge {
  font-size: .66rem; padding: .17rem .42rem;
  background: var(--surface2); border-radius: 6px;
  color: var(--ctx); text-transform: uppercase;
}
.icon-btn {
  background: none; border: none; color: var(--text);
  font-size: 1.1rem; cursor: pointer;
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 8px; transition: background .15s;
}
.icon-btn:hover { background: var(--surface2); }

/* Context zones */
.context-zone {
  flex: 1;
  display: flex; align-items: center; justify-content: center;
  padding: .35rem 1.1rem;
  overflow: hidden; text-align: center; min-height: 0;
}
.context-zone span {
  font-size: var(--font-ctx); color: var(--ctx);
  font-family: var(--font-family); line-height: 1.6;
}
.ctx-edge { color: var(--text); opacity: .55; }

/* ── Redicle ─────────────────────────────────────
   Key fix: word-before and word-after use flex:1
   so the ORP char is ALWAYS at the exact horizontal
   centre of the redicle, no matter what's in the word.
──────────────────────────────────────────────────── */
.redicle-wrap {
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  padding: .15rem 0;
}
.redicle {
  position: relative;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px;
  width: min(92vw, 580px);
  flex-shrink: 0;
  /* Let flexbox do the vertical centering instead of padding */
  display: flex;
  align-items: center;
  justify-content: center;
  /* Enough room for the word + footer without the word shifting */
  padding: 0 0 1.6rem 0;
  min-height: calc(var(--font-word) * 2.6);
}
.guide-lines {
  position: absolute; inset: 0; border-radius: 14px;
  overflow: hidden; pointer-events: none;
  z-index: 0;   /* explicitly below the word */
}
.guide { position: absolute; left: 0; right: 0; height: 1px; background: var(--border); }
.guide-top { top: 33%; }
.guide-bot { bottom: 33%; }

/* Top + bottom ORP notches centred on the ORP character */
.redicle::before, .redicle::after {
  content: ''; position: absolute;
  left: 50%; transform: translateX(-50%);
  width: 2px; background: var(--orp); border-radius: 2px;
  z-index: 0;   /* notches also below the word */
}
.redicle::before { top: -1px;    height: 8px; border-radius: 0 0 2px 2px; }
.redicle::after  { bottom: -1px; height: 8px; border-radius: 2px 2px 0 0; }

.redicle-inner {
  display: flex;
  align-items: center;
  font-family: var(--font-family);
  font-size: var(--font-word);
  line-height: 1;
  white-space: nowrap;
  width: calc(100% - 2rem);  /* side breathing room */
  overflow: hidden;
  height: calc(var(--font-word) * 1.45);
  flex-shrink: 0;
  /* Sit above guide lines, notches, and footer */
  position: relative;
  z-index: 2;
  transform: translateY(25%);
}

/*
 * word-before: flex:1, right-aligned → grows leftward from ORP
 * word-orp:    flex:0 fixed width (1 char) → pinned at dead centre
 * word-after:  flex:1, left-aligned  → grows rightward from ORP
 *
 * This guarantees ORP is always at 50 % of the redicle width.
 * Overflow is hidden so text never bleeds outside its half.
 */
.word-before {
  flex: 1;
  text-align: right;
  color: var(--text);
  overflow: hidden;
  /* Extra safety: clip direction is left (text is right-aligned) */
  direction: ltr;
}
.word-orp {
  flex: 0 0 auto;
  /* Minimum 1ch; for proportional fonts, we set explicit width in JS */
  min-width: .6em;
  text-align: center;
  color: var(--orp);
  font-weight: 700;
}
.word-after {
  flex: 1;
  text-align: left;
  color: var(--text);
  overflow: hidden;
}

.redicle-footer {
  /* Pull out of normal flow so it cannot push the word upward */
  position: absolute;
  bottom: .4rem;
  left: 1rem;
  right: 1rem;
  z-index: 1;   /* above guides, below word */
  display: flex; justify-content: space-between;
  font-size: .64rem; color: var(--ctx);
}

/* ── Controls ────────────────────────────────────── */
.controls {
  background: var(--surface); border-top: 1px solid var(--border);
  padding: .5rem .65rem calc(.5rem + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: .4rem;
  flex-shrink: 0;
}
.controls-row { display: flex; align-items: center; justify-content: center; gap: .4rem; }
.ctrl-btn {
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); border-radius: 10px;
  font-size: .86rem; font-weight: 600; cursor: pointer;
  min-width: 48px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, transform .1s;
}
.ctrl-btn:active  { transform: scale(.93); }
.ctrl-btn.small   { min-width: 36px; font-size: 1rem; }
.ctrl-play {
  background: var(--accent); border-color: var(--accent);
  font-size: 1.15rem; min-width: 58px; height: 48px;
}
.ctrl-play.playing { background: #e67e22; border-color: #e67e22; }
.wpm-wrap { display: flex; align-items: center; gap: .4rem; flex: 1; }
.wpm-wrap input[type="range"] { flex: 1; accent-color: var(--accent); }
#wpm-label { font-size: .76rem; color: var(--text); white-space: nowrap; min-width: 4rem; text-align: right; }
.progress-wrap { display: flex; flex-direction: column; gap: .15rem; }
.progress-wrap input[type="range"] { width: 100%; accent-color: var(--accent); }
.progress-labels { display: flex; justify-content: space-between; font-size: .68rem; color: var(--ctx); }

/* ════════════════════════════════════════
   SHARED SIDE PANEL
════════════════════════════════════════ */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.65);
  z-index: 100; backdrop-filter: blur(2px);
}
.side-panel {
  position: fixed; top: 0; right: 0; bottom: 0;
  width: min(100vw, 400px);
  background: var(--surface); border-left: 1px solid var(--border);
  z-index: 101;
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform .28s cubic-bezier(.4,0,.2,1);
  overflow: hidden;
}
.side-panel.open  { transform: translateX(0); }
.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .9rem 1rem .7rem;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.panel-header h2 { font-size: 1rem; }
.panel-body { overflow-y: auto; flex: 1; padding: .85rem; display: flex; flex-direction: column; gap: 1rem; }

/* Library */
.library-empty {
  display: flex; align-items: center; justify-content: center;
  flex: 1; text-align: center; color: var(--ctx); font-size: .88rem; line-height: 1.7;
}
.library-list { list-style: none; display: flex; flex-direction: column; gap: .45rem; }
.lib-item {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 12px; padding: .7rem .8rem;
  display: flex; align-items: center; gap: .65rem;
  transition: background .15s;
}
.lib-item:hover { background: var(--surface3); }
.lib-cover {
  width: 36px; height: 48px; flex-shrink: 0;
  background: var(--surface3); border-radius: 5px;
  display: flex; align-items: center; justify-content: center; font-size: 1.25rem;
}
.lib-info { flex: 1; overflow: hidden; min-width: 0; }
.lib-title {
  font-size: .86rem; font-weight: 600; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: block; cursor: pointer;
}
.lib-title:hover { color: var(--accent); }
.lib-meta { font-size: .7rem; color: var(--ctx); display: flex; gap: .45rem; margin-top: .12rem; flex-wrap: wrap; }
.lib-bar  { height: 3px; background: var(--border); border-radius: 2px; margin-top: .3rem; overflow: hidden; }
.lib-fill { height: 100%; background: var(--accent); border-radius: 2px; }
.lib-actions { display: flex; gap: .28rem; flex-shrink: 0; }
.lib-btn {
  background: none; border: 1px solid var(--border);
  color: var(--ctx); border-radius: 7px;
  font-size: .72rem; padding: .22rem .45rem; cursor: pointer;
  transition: border-color .15s, color .15s;
}
.lib-btn:hover     { border-color: var(--accent); color: var(--accent); }
.lib-btn.del:hover { border-color: var(--danger);  color: var(--danger); }

/* Settings */
.setting-group { display: flex; flex-direction: column; gap: .55rem; }
.setting-group h3 { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: var(--ctx); }
.setting-group label {
  display: flex; align-items: center; justify-content: space-between;
  gap: .4rem; font-size: .84rem; color: var(--text);
}
.setting-group label input[type="color"]  { width: 36px; height: 28px; border: none; background: none; cursor: pointer; border-radius: 5px; }
.setting-group label input[type="range"]  { flex: 1; accent-color: var(--accent); }
.setting-group label select {
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: .26rem .4rem; font-size: .8rem;
}
.lang-wpm-list { display: flex; flex-direction: column; gap: .32rem; max-height: 160px; overflow-y: auto; }
.lang-wpm-row  { display: flex; align-items: center; gap: .4rem; font-size: .8rem; }
.lang-wpm-row .lang-code { width: 2.7rem; font-weight: 700; color: var(--accent); flex-shrink: 0; }
.lang-wpm-row input[type="number"] {
  width: 4.5rem; background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); border-radius: 6px; padding: .2rem .35rem; font-size: .8rem;
}
.settings-footer { padding: .6rem 0 0; flex-shrink: 0; }

/* ════════════════════════════════════════
   HELP OVERLAY
════════════════════════════════════════ */
.help-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,.75);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.help-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; width: 100%; max-width: 420px;
  display: flex; flex-direction: column; overflow: hidden;
  max-height: 90dvh;
}
.help-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .9rem 1rem .7rem;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.help-header h2 { font-size: 1rem; }
.help-body { overflow-y: auto; padding: .85rem; }
.help-table { width: 100%; border-collapse: collapse; font-size: .82rem; }
.help-table th {
  text-align: left; color: var(--ctx);
  font-size: .68rem; text-transform: uppercase; letter-spacing: .07em;
  padding: 0 .5rem .5rem 0; border-bottom: 1px solid var(--border);
}
.help-table td {
  padding: .42rem .5rem .42rem 0;
  border-bottom: 1px solid rgba(255,255,255,.04);
  color: var(--text); vertical-align: middle;
}
.help-table td:nth-child(2),
.help-table td:nth-child(3) { color: var(--ctx); font-size: .8rem; }

/* Desktop */
@media (min-width: 640px) {
  :root { --font-word: 52px; --font-ctx: 16px; }
  .redicle { width: min(88vw, 660px); }
}
