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

    :root {
      --accent:      #7c6dff;
      --bg:          #111118;
      --surface:     #1c1c28;
      --text:        #f2f2f2;
      --muted:       rgba(242,242,242,0.4);
      --border:      rgba(124,109,255,0.4);
      --reader-font: 'OpenDyslexic', Arial, sans-serif;
      --size-scale:  1;
    }

    html, body {
      height: 100%;
      font-family: var(--reader-font);
      background: var(--bg);
      color: var(--text);
      overflow: hidden;
    }

    /* ── Screens ─────────────────────────────── */
    .screen { display: none; height: 100%; flex-direction: column; }
    .screen.active { display: flex; }

    /* ── Upload screen ───────────────────────── */
    #upload-screen {
      align-items: center;
      justify-content: center;
      gap: 24px;
      padding: 28px 24px;
      overflow-y: auto;
    }

    .logo { text-align: center; }
    .logo .icon img { width: 160px; height: auto; margin-bottom: 4px; }
    .logo h1   { font-size: 2.2rem; color: var(--accent); margin-top: 8px; letter-spacing: -0.01em; }
    .logo .tagline { color: var(--muted); margin-top: 5px; font-size: 0.9rem; }

    .how-it-works {
      max-width: 460px;
      width: 100%;
      background: var(--surface);
      border-radius: 18px;
      padding: 20px 22px;
      display: flex;
      flex-direction: column;
      gap: 14px;
    }
    .how-it-works h2 {
      font-size: 0.75rem;
      text-transform: uppercase;
      letter-spacing: 0.1em;
      color: var(--muted);
      margin-bottom: 2px;
    }
    .how-row {
      display: flex;
      align-items: flex-start;
      gap: 14px;
    }
    .how-row .icon {
      font-size: 1.4rem;
      flex-shrink: 0;
      margin-top: 1px;
    }
    .how-row .text strong {
      display: block;
      font-size: 0.95rem;
      margin-bottom: 2px;
    }
    .how-row .text span {
      font-size: 0.82rem;
      color: var(--muted);
      line-height: 1.45;
    }

    #drop-zone {
      width: 100%; max-width: 460px;
      border: 2px dashed var(--border);
      border-radius: 22px;
      padding: 52px 32px;
      text-align: center;
      cursor: pointer;
      transition: background 0.2s, border-color 0.2s;
    }
    #drop-zone:hover, #drop-zone.over {
      background: rgba(124,109,255,0.09);
      border-color: var(--accent);
    }
    #drop-zone .dz-icon { font-size: 2.8rem; }
    #drop-zone p        { font-size: 1.1rem; margin-top: 14px; }
    #drop-zone small    { color: var(--muted); font-size: 0.82rem; display: block; margin-top: 8px; }

    /* ── Reading screen ──────────────────────── */
    #progress-bar  { height: 3px; background: var(--surface); flex-shrink: 0; }
    #progress-fill { height: 100%; background: var(--accent); width: 0; transition: width 0.15s; }

    #topbar {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 8px 14px;
      background: var(--surface);
      flex-shrink: 0;
    }
    #book-title {
      flex: 1;
      font-size: 0.82rem;
      color: var(--muted);
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    /* ── Word stage ──────────────────────────── */
    #word-stage {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 28px 20px;
      -webkit-tap-highlight-color: transparent;
      user-select: none;
      overflow: hidden;
      position: relative;
    }

    /* Tap-zone hints (show on first visit) */
    .tap-hint {
      position: absolute;
      top: 0; bottom: 0;
      display: flex;
      align-items: center;
      padding: 0 14px;
      font-size: 1.6rem;
      opacity: 0;
      pointer-events: none;
      transition: opacity 0.3s;
    }
    .tap-hint.left  { left: 0; }
    .tap-hint.right { right: 0; }
    #word-stage:active .tap-hint { opacity: 0.18; }

    #word-display {
      font-family: var(--reader-font);
      font-size: calc(clamp(3rem, 14vw, 8rem) * var(--size-scale));
      font-weight: 700;
      text-align: center;
      letter-spacing: 0.03em;
      line-height: 1.2;
      word-break: break-word;
      max-width: 92vw;
    }
    /* Scale font down when showing more words */
    #word-display[data-words="2"] { font-size: calc(clamp(2.2rem,  9vw, 5.5rem) * var(--size-scale)); }
    #word-display[data-words="3"] { font-size: calc(clamp(1.8rem,  7vw, 4.2rem) * var(--size-scale)); }
    #word-display[data-words="4"] { font-size: calc(clamp(1.5rem,5.5vw, 3.5rem) * var(--size-scale)); }
    #word-display[data-words="5"] { font-size: calc(clamp(1.2rem,4.5vw, 2.8rem) * var(--size-scale)); }
    /* Headings shown in full — smaller, coloured */
    #word-display.heading {
      font-size: clamp(1.3rem, 5vw, 2.8rem);
      color: var(--accent);
      max-width: 88vw;
      line-height: 1.35;
    }

    #word-display strong.bionic {
      font-weight: 900;
    }
    
    #word-display span.vis-syl {
      color: var(--muted);
    }

    /* ── Loading spinner ─────────────────────── */
    #loader {
      display: none;
      flex-direction: column;
      align-items: center;
      gap: 18px;
    }
    #loader.show { display: flex; }
    .spinner {
      width: 52px; height: 52px;
      border: 4px solid rgba(124,109,255,0.2);
      border-top-color: var(--accent);
      border-radius: 50%;
      animation: spin 0.75s linear infinite;
    }
    .spinner-label {
      font-size: 1rem;
      color: var(--muted);
      letter-spacing: 0.04em;
    }
    @keyframes spin { to { transform: rotate(360deg); } }

    #hint {
      text-align: center;
      color: var(--muted);
      font-size: 0.72rem;
      padding: 4px 10px;
      flex-shrink: 0;
    }

    /* ── Page nav ────────────────────────────── */
    #page-nav {
      display: flex;
      gap: 5px;
      padding: 10px 12px;
      background: var(--surface);
      overflow-x: auto;
      flex-shrink: 0;
      scrollbar-width: thin;
      scrollbar-color: var(--accent) transparent;
      -webkit-overflow-scrolling: touch;
    }
    #page-nav::-webkit-scrollbar       { height: 3px; }
    #page-nav::-webkit-scrollbar-thumb { background: var(--accent); border-radius: 2px; }
    #page-nav::-webkit-scrollbar-track { background: transparent; }

    .pg-btn {
      min-width: 38px; height: 38px;
      border: 1px solid var(--border);
      border-radius: 8px;
      background: transparent;
      color: var(--text);
      cursor: pointer;
      font-size: 0.82rem;
      flex-shrink: 0;
      padding: 0 8px;
      transition: background 0.15s;
    }
    .pg-btn:hover:not(.dot)  { background: rgba(124,109,255,0.22); }
    .pg-btn.current { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 700; }
    .pg-btn.dot     { border-color: transparent; color: var(--muted); cursor: default; }

    /* ── Overlay / dialog ────────────────────── */
    #overlay {
      display: none;
      position: fixed; inset: 0;
      background: rgba(0,0,0,0.78);
      align-items: center;
      justify-content: center;
      z-index: 500;
      padding: 20px;
    }
    #overlay.open { display: flex; }

    .dlg {
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: 22px;
      padding: 34px 28px;
      text-align: center;
      max-width: 380px;
      width: 100%;
    }
    .dlg h2 { font-size: 1.4rem; margin-bottom: 10px; }
    .dlg p  { color: var(--muted); margin-bottom: 24px; line-height: 1.5; }

    /* ── Buttons ─────────────────────────────── */
    .btn {
      display: inline-flex; align-items: center; gap: 6px;
      padding: 10px 20px;
      border-radius: 10px; border: none;
      cursor: pointer;
      font-size: 0.9rem; font-weight: 600;
      margin: 4px;
      transition: opacity 0.15s;
    }
    .btn:hover    { opacity: 0.85; }
    .btn-primary  { background: var(--accent); color: #fff; }
    .btn-ghost    { background: rgba(255,255,255,0.1); color: var(--text); }
    .btn-sm       { padding: 6px 13px; font-size: 0.8rem; }

    /* ── Settings drawer ─────────────────────── */
    #settings-drawer {
      position: fixed; inset: 0;
      z-index: 600;
      display: none;
    }
    #settings-drawer.open { display: block; }

    #settings-backdrop {
      position: absolute; inset: 0;
      background: rgba(0,0,0,0.55);
    }
    #settings-panel {
      position: absolute; bottom: 0; left: 0; right: 0;
      background: var(--surface);
      border-radius: 22px 22px 0 0;
      padding: 20px 22px 32px;
      max-height: 80vh;
      overflow-y: auto;
      transform: translateY(100%);
      transition: transform 0.3s cubic-bezier(.32,.72,0,1);
    }
    #settings-drawer.open #settings-panel { transform: translateY(0); }

    .s-handle {
      width: 40px; height: 4px;
      background: rgba(255,255,255,0.2);
      border-radius: 2px;
      margin: 0 auto 18px;
    }
    .s-title { font-size: 1.1rem; font-weight: 700; margin-bottom: 18px; }
    .s-section { margin-bottom: 20px; }
    .s-label {
      font-size: 0.75rem;
      text-transform: uppercase;
      letter-spacing: 0.08em;
      color: var(--muted);
      margin-bottom: 10px;
    }

    /* Font buttons */
    .font-options {
      display: flex; flex-wrap: wrap; gap: 8px;
    }
    .font-btn {
      padding: 8px 14px;
      border-radius: 10px;
      border: 1px solid var(--border);
      background: transparent;
      color: var(--text);
      cursor: pointer;
      font-size: 0.9rem;
      transition: background 0.15s, border-color 0.15s;
    }
    .font-btn.active {
      background: var(--accent);
      border-color: var(--accent);
      color: #fff;
    }

    /* Theme swatches */
    .theme-options {
      display: flex; flex-wrap: wrap; gap: 10px;
    }
    .theme-swatch {
      width: 44px; height: 44px;
      border-radius: 50%;
      border: 2px solid transparent;
      cursor: pointer;
      transition: transform 0.15s, border-color 0.15s;
      position: relative;
    }
    .theme-swatch.active {
      border-color: var(--accent);
      transform: scale(1.15);
    }
    .theme-swatch span {
      position: absolute; inset: 0;
      border-radius: 50%;
      display: flex; align-items: center; justify-content: center;
      font-size: 0.6rem;
      font-weight: 700;
      pointer-events: none;
    }

    /* ── Library overlay ─────────────────────── */
    #lib-overlay {
      position: fixed; inset: 0;
      background: rgba(0,0,0,0.6);
      display: flex; align-items: flex-end;
      z-index: 300;
      opacity: 0; pointer-events: none;
      transition: opacity 0.25s;
    }
    #lib-overlay.open { opacity: 1; pointer-events: all; }

    #lib-panel {
      background: var(--bg);
      width: 100%; max-height: 92vh;
      border-radius: 22px 22px 0 0;
      display: flex; flex-direction: column;
      transform: translateY(100%);
      transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
      overflow: hidden;
    }
    #lib-overlay.open #lib-panel { transform: translateY(0); }

    /* Header — never scrolls */
    .lib-header {
      display: flex; align-items: center; gap: 12px;
      padding: 18px 18px 12px;
      flex-shrink: 0;
    }
    .lib-header-text        { flex: 1; }
    .lib-header-text h2     { font-size: 1.15rem; margin: 0; }
    .lib-header-text small  { color: var(--muted); font-size: 0.72rem; display: block; margin-top: 1px; }
    .lib-close {
      background: var(--surface); border: none; color: var(--text);
      cursor: pointer; font-size: 1rem;
      width: 34px; height: 34px; border-radius: 50%;
      display: flex; align-items: center; justify-content: center;
      flex-shrink: 0;
    }

    /* Search row — pinned, never scrolls away */
    .lib-search-row {
      display: flex; gap: 8px;
      padding: 0 16px 14px;
      flex-shrink: 0;
    }
    #lib-input {
      flex: 1; padding: 12px 16px;
      border-radius: 14px;
      border: 1.5px solid var(--border);
      background: var(--surface);
      color: var(--text);
      font-family: var(--reader-font);
      font-size: 1rem;
      outline: none;
      -webkit-appearance: none;
      min-width: 0;
    }
    #lib-input:focus  { border-color: var(--accent); }
    #lib-input::placeholder { color: var(--muted); }
    #lib-go {
      padding: 12px 20px; border-radius: 14px; border: none;
      background: var(--accent); color: #fff;
      cursor: pointer; font-family: var(--reader-font);
      font-weight: 700; font-size: 1rem; flex-shrink: 0;
    }

    /* Scrollable content */
    #lib-scroll {
      flex: 1; overflow-y: auto; overflow-x: hidden;
      padding: 4px 16px 20px;
      -webkit-overflow-scrolling: touch;
    }
    .lib-section-label {
      font-size: 0.68rem; text-transform: uppercase;
      letter-spacing: 0.1em; color: var(--muted);
      margin: 8px 0 12px;
    }

    /* Popular books — 2-column grid */
    .books-grid {
      display: grid; grid-template-columns: repeat(2,1fr);
      gap: 16px; padding-bottom: 8px;
    }
    .book-grid-card { display: flex; flex-direction: column; gap: 7px; cursor: pointer; }
    .book-grid-cover {
      width: 100%; aspect-ratio: 2/3; border-radius: 12px;
      background: var(--surface); overflow: hidden;
      display: flex; align-items: center; justify-content: center;
      font-size: 2.8rem; position: relative;
      box-shadow: 0 4px 14px rgba(0,0,0,0.2);
    }
    .book-grid-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
    .book-grid-badge {
      position: absolute; bottom: 7px; right: 7px;
      background: var(--accent); color: #fff;
      font-size: 0.58rem; font-weight: 800; letter-spacing: 0.05em;
      padding: 2px 8px; border-radius: 20px;
    }
    .book-grid-title {
      font-size: 0.86rem; font-weight: 700; line-height: 1.25;
      overflow: hidden; display: -webkit-box;
      -webkit-line-clamp: 2; -webkit-box-orient: vertical;
    }
    .book-grid-author { font-size: 0.7rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

    /* Search result list */
    .book-card { display: flex; gap: 13px; padding: 13px 0; border-bottom: 1px solid var(--border); }
    .book-cover {
      width: 60px; height: 84px; border-radius: 9px;
      background: var(--surface); flex-shrink: 0;
      display: flex; align-items: center; justify-content: center;
      font-size: 1.9rem; overflow: hidden;
      box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    }
    .book-cover img  { width: 100%; height: 100%; object-fit: cover; }
    .book-info       { flex: 1; min-width: 0; }
    .book-title      { font-size: 0.92rem; font-weight: 700; margin-bottom: 3px;
      overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
    .book-author     { font-size: 0.75rem; color: var(--muted); margin-bottom: 9px; }
    .book-actions    { display: flex; gap: 6px; flex-wrap: wrap; }

    /* Buttons shared */
    .bk-btn {
      font-size: 0.73rem; font-family: var(--reader-font);
      padding: 6px 14px; border-radius: 9px; border: none;
      cursor: pointer; font-weight: 700; line-height: 1.2;
      transition: opacity 0.15s; text-decoration: none; display: inline-block;
    }
    .bk-btn:active  { opacity: 0.7; }
    .bk-btn.primary { background: var(--accent); color: #fff; }
    .bk-btn.ghost   { background: var(--surface); color: var(--text); border: 1px solid var(--border); }

    /* Pagination (rendered inline inside #lib-scroll) */
    .lib-pg-row {
      display: flex; justify-content: center; align-items: center; gap: 14px;
      padding: 16px 0 4px;
    }
    .lib-pg-btn {
      padding: 8px 20px; border-radius: 10px; border: 1px solid var(--border);
      background: transparent; color: var(--text);
      cursor: pointer; font-family: var(--reader-font); font-size: 0.85rem;
    }
    .lib-pg-btn:disabled { opacity: 0.3; cursor: default; }
    .lib-pg-lbl { color: var(--muted); font-size: 0.78rem; white-space: nowrap; }

    /* Misc */
    .lib-status { text-align: center; color: var(--muted); padding: 36px 0; font-size: 0.9rem; line-height: 1.6; }
    .lib-notice {
      background: var(--surface); border: 1px solid var(--accent);
      border-radius: 10px; padding: 11px 14px;
      font-size: 0.82rem; color: var(--text); margin-bottom: 12px; line-height: 1.5;
    }

    /* ── Library button on upload screen ──────── */
    #lib-open-btn {
      max-width: 460px; width: 100%; padding: 16px;
      border-radius: 18px; border: 1px solid var(--border);
      background: var(--surface); color: var(--text);
      cursor: pointer; font-family: var(--reader-font);
      font-size: 1rem; font-weight: 700;
      display: flex; align-items: center; justify-content: center; gap: 10px;
      transition: background 0.2s;
    }
    #lib-open-btn:hover { background: var(--bg); border-color: var(--accent); }

    /* Words-per-view buttons */
    .wpv-options { display: flex; gap: 8px; }
    .wpv-btn {
      flex: 1;
      padding: 10px 4px;
      border-radius: 10px;
      border: 1px solid var(--border);
      background: transparent;
      color: var(--text);
      cursor: pointer;
      font-size: 0.88rem;
      font-weight: 600;
      transition: background 0.15s;
    }
    .wpv-btn.active {
      background: var(--accent);
      border-color: var(--accent);
      color: #fff;
    }

    /* Size slider */
    #size-slider {
      width: 100%;
      accent-color: var(--accent);
      margin-top: 4px;
    }
    .size-labels {
      display: flex;
      justify-content: space-between;
      font-size: 0.75rem;
      color: var(--muted);
      margin-top: 4px;
    }
