/*
 * Tyler Homework - SnapScan-inspired Design System v2
 * Native-quality mobile-first UI
 * No CDN. No emojis. Offline-first.
 */

/* ─────────────────────────────────────────────
   DESIGN TOKENS — SnapScan teal palette
   ───────────────────────────────────────────── */
:root {
  /* Force light mode — prevent Chrome/Android auto-dark-mode from washing out colours in PWA standalone */
  color-scheme: light;
  --c-primary:     #0ABFA3;
  --c-primary-dk:  #089B83;
  --c-primary-xdk: #067060;
  --c-primary-lt:  rgba(10, 191, 163, 0.13);
  --c-primary-xl:  rgba(10, 191, 163, 0.06);

  --c-accent:      #FF6B35;
  --c-accent-lt:   rgba(255, 107, 53, 0.10);

  --c-green:       #22C55E;
  --c-green-lt:    #DCFCE7;
  --c-amber:       #F59E0B;
  --c-amber-lt:    #FEF3CD;
  --c-red:         #EF4444;
  --c-red-lt:      #FEE2E2;
  --c-blue:        #3B82F6;
  --c-blue-lt:     rgba(59, 130, 246, 0.10);
  --c-indigo:      #6366F1;
  --c-indigo-lt:   rgba(99, 102, 241, 0.10);

  --c-bg:          #F0F5F7;
  --c-surface:     #FFFFFF;
  --c-surface2:    #F5F8FA;
  --c-surface3:    #E8EFF3;
  --c-border:      rgba(0, 0, 0, 0.06);
  --c-border-dk:   rgba(0, 0, 0, 0.11);

  --c-text:        #1A2B34;
  --c-text-mid:    #3D5261;
  --c-text-dim:    #8099A8;

  /* Typography */
  --f-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text",
            "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --f-head: -apple-system, BlinkMacSystemFont, "SF Pro Display",
            "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --f-mono: "SF Mono", "Fira Code", "Fira Mono", "Roboto Mono", monospace;
  --f-serif: Georgia, "Times New Roman", Times, serif;

  /* Radii */
  --r-xs:   4px;  --r-sm:   8px;  --r-md:  12px;
  --r-lg:  16px;  --r-xl:  20px;  --r-2xl: 28px;
  --r-3xl: 36px;  --r-full: 9999px;

  /* Shadows — keyed to SnapScan card elevation */
  --sh-0: 0 1px 2px rgba(0,0,0,0.04);
  --sh-1: 0 2px 8px rgba(0,0,0,0.07), 0 1px 2px rgba(0,0,0,0.04);
  --sh-2: 0 4px 16px rgba(0,0,0,0.09), 0 2px 4px rgba(0,0,0,0.05);
  --sh-3: 0 8px 32px rgba(0,0,0,0.13), 0 3px 8px rgba(0,0,0,0.07);
  --sh-primary: 0 4px 18px rgba(10,191,163,0.38);

  /* Layout metrics */
  --nav-h:   68px;
  --hdr-h:   56px;
  --panel-w: min(300px, 86vw);
  --safe-t:  env(safe-area-inset-top, 0px);
  --safe-b:  env(safe-area-inset-bottom, 0px);
  --safe-l:  env(safe-area-inset-left, 0px);
  --safe-r:  env(safe-area-inset-right, 0px);
}

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

html, body {
  height: 100%; width: 100%;
  font-family: var(--f-sans);
  font-size: 16px;
  line-height: 1.5;
  color: var(--c-text);
  background: var(--c-bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

input, button, textarea, select { font-family: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--c-primary); text-decoration: none; }

/* ─────────────────────────────────────────────
   APP ROOT SHELL
   ───────────────────────────────────────────── */
#tbp-root {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: var(--c-bg);
}

/* ─────────────────────────────────────────────
   SLIDE PANELS (left + right)
   ───────────────────────────────────────────── */
.tbp-panel {
  position: fixed;
  top: 0; bottom: 0;
  width: var(--panel-w);
  background: var(--c-surface);
  z-index: 1200;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  box-shadow: var(--sh-3);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1),
              visibility 0.28s;
  display: flex;
  flex-direction: column;
  visibility: hidden;
}
.tbp-panel-left  { left: 0;  transform: translateX(-100%); }
.tbp-panel-right { right: 0; transform: translateX(100%);  }
.tbp-panel-left.open  { transform: translateX(0); visibility: visible; }
.tbp-panel-right.open { transform: translateX(0); visibility: visible; }

.tbp-panel-overlay {
  position: fixed; inset: 0;
  background: rgba(26, 43, 52, 0.48);
  z-index: 1199;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.28s, visibility 0.28s;
  backdrop-filter: blur(1px);
}
.tbp-panel-overlay.visible { opacity: 1; visibility: visible; }

/* Panel header */
.tbp-panel-header {
  padding: calc(var(--safe-t) + 22px) 20px 20px;
  background: linear-gradient(145deg, var(--c-primary), var(--c-primary-dk));
  flex-shrink: 0;
}
.tbp-panel-avatar {
  width: 50px; height: 50px;
  border-radius: 50%;
  background: rgba(255,255,255,0.22);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 800; color: #fff;
  letter-spacing: -0.5px;
  margin-bottom: 10px;
}
.tbp-panel-name  { font-size: 17px; font-weight: 800; color: #fff; letter-spacing: -0.3px; }
.tbp-panel-sub   { font-size: 12px; color: rgba(255,255,255,0.65); margin-top: 2px; }
.tbp-panel-stats { display: flex; gap: 10px; margin-top: 14px; }
.tbp-panel-stat {
  background: rgba(255,255,255,0.16);
  border-radius: var(--r-md); padding: 8px 14px; flex: 1; text-align: center;
}
.tbp-panel-stat-val { font-size: 19px; font-weight: 800; color: #fff; line-height: 1.1; }
.tbp-panel-stat-lbl {
  font-size: 9px; color: rgba(255,255,255,0.65);
  font-weight: 700; text-transform: uppercase; letter-spacing: 0.6px;
  margin-top: 2px;
}

.tbp-panel-section { padding: 10px 0; border-bottom: 1px solid var(--c-border); }
.tbp-panel-section:last-child { border-bottom: none; }
.tbp-panel-section-title {
  font-size: 9.5px; font-weight: 800; color: var(--c-text-dim);
  text-transform: uppercase; letter-spacing: 0.9px;
  padding: 4px 20px 8px;
}
.tbp-panel-item {
  display: flex; align-items: center; gap: 13px;
  padding: 11px 20px; font-size: 14px; font-weight: 500;
  color: var(--c-text-mid); cursor: pointer;
  transition: background 0.12s;
  user-select: none;
}
.tbp-panel-item:active { background: var(--c-surface2); }
.tbp-panel-item-icon {
  width: 36px; height: 36px; border-radius: var(--r-md);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

/* Mode chips */
.tbp-mode-chips { display: flex; gap: 6px; padding: 10px 20px; }
.tbp-mode-chip {
  flex: 1; padding: 8px 4px; border-radius: var(--r-full);
  border: 1.5px solid var(--c-border-dk); background: var(--c-surface);
  font-size: 12px; font-weight: 700; color: var(--c-text-mid);
  text-align: center; transition: all 0.15s; cursor: pointer;
}
.tbp-mode-chip.active {
  background: var(--c-primary); border-color: var(--c-primary); color: #fff;
}

/* Right panel lib tabs */
.tbp-panel-right .tbp-panel-header-simple {
  padding: calc(var(--safe-t) + 16px) 16px 14px;
  font-size: 20px; font-weight: 800; color: var(--c-text);
  border-bottom: 1px solid var(--c-border);
  flex-shrink: 0;
  background: var(--c-surface);
}
.tbp-lib-search { padding: 10px 14px; background: var(--c-surface2); border-bottom: 1px solid var(--c-border); flex-shrink: 0; }
.tbp-lib-search input {
  width: 100%; background: var(--c-surface); border: 1.5px solid var(--c-border-dk);
  border-radius: var(--r-full); padding: 9px 14px; font-size: 13px;
  color: var(--c-text); outline: none;
  transition: border-color 0.15s;
}
.tbp-lib-search input:focus { border-color: var(--c-primary); }
.tbp-lib-tabs {
  display: flex; border-bottom: 1px solid var(--c-border);
  background: var(--c-surface); flex-shrink: 0;
}
.tbp-lib-tab {
  flex: 1; padding: 11px 4px; text-align: center;
  font-size: 11px; font-weight: 700; color: var(--c-text-dim);
  background: none; border: none;
  border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s;
  letter-spacing: 0.3px;
}
.tbp-lib-tab.active { color: var(--c-primary); border-bottom-color: var(--c-primary); }
.tbp-lib-content { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }

.tbp-lib-item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 16px; cursor: pointer; transition: background 0.12s;
}
.tbp-lib-item:active { background: var(--c-surface2); }
.tbp-lib-item-icon {
  width: 36px; height: 36px; border-radius: var(--r-md); flex-shrink: 0;
  background: var(--c-primary-lt); color: var(--c-primary);
  display: flex; align-items: center; justify-content: center;
}
.tbp-lib-item-info { flex: 1; min-width: 0; }
.tbp-lib-item-title {
  font-size: 13px; font-weight: 700; color: var(--c-text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tbp-lib-item-sub { font-size: 11px; color: var(--c-text-dim); margin-top: 1px; }
.tbp-lib-item-actions { display: flex; gap: 4px; flex-shrink: 0; }
.tbp-lib-act {
  width: 30px; height: 30px; border-radius: var(--r-sm);
  background: none; border: 1px solid var(--c-border-dk);
  display: flex; align-items: center; justify-content: center;
  color: var(--c-text-dim);
}
.tbp-lib-act:active { background: var(--c-surface2); }

/* ─────────────────────────────────────────────
   MAIN APP
   ───────────────────────────────────────────── */
.tbp-app {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  background: var(--c-bg);
}

/* ─────────────────────────────────────────────
   HEADER
   ───────────────────────────────────────────── */
.tbp-header {
  flex-shrink: 0;
  height: calc(var(--hdr-h) + var(--safe-t));
  padding-top: var(--safe-t);
  background: var(--c-surface);
  border-bottom: 1px solid var(--c-border);
  display: flex; align-items: center;
  padding-left: 2px; padding-right: 6px;
  gap: 6px; z-index: 100; position: relative;
}
.tbp-header-btn {
  width: 44px; height: 44px; border-radius: var(--r-md);
  background: none; border: none;
  display: flex; align-items: center; justify-content: center;
  color: var(--c-text-mid); flex-shrink: 0;
  transition: background 0.12s;
}
.tbp-header-btn:active { background: var(--c-surface2); }
.tbp-header-logo { display: flex; align-items: center; gap: 9px; flex: 1; min-width: 0; }
.tbp-logo-mark {
  width: 32px; height: 32px; border-radius: 10px;
  background: var(--c-primary);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; box-shadow: 0 2px 8px rgba(10,191,163,0.3);
}
.tbp-logo-text {
  font-family: var(--f-head); font-size: 19px; font-weight: 800;
  color: var(--c-text); letter-spacing: -0.5px;
}
.tbp-logo-text span { color: var(--c-primary); }
.tbp-header-right { display: flex; align-items: center; gap: 5px; flex-shrink: 0; }
.tbp-pts-badge {
  background: var(--c-primary-lt); border-radius: var(--r-full);
  padding: 5px 11px; display: flex; align-items: center; gap: 4px;
  font-size: 13px; font-weight: 800; color: var(--c-primary-dk);
  cursor: pointer;
}
.tbp-streak-badge {
  background: var(--c-amber-lt); border-radius: var(--r-full);
  padding: 5px 10px; font-size: 12px; font-weight: 800;
  color: #92400E;
}

/* Offline banner */
.tbp-offline-bar {
  flex-shrink: 0;
  background: var(--c-amber-lt);
  border-bottom: 1px solid rgba(245,158,11,0.25);
  padding: 6px 16px; font-size: 12px; font-weight: 700;
  color: #78350F; text-align: center; display: none;
}
.tbp-offline-bar.visible { display: block; }

/* AI loading bar */
.tbp-ai-loading-bar {
  flex-shrink: 0; height: 2px; background: var(--c-surface3); overflow: hidden; display: none;
}
.tbp-ai-loading-bar.visible { display: block; }
.tbp-ai-loading-fill {
  height: 100%; background: var(--c-primary);
  animation: aiLoadPulse 1.8s ease-in-out infinite;
  width: 40%;
}
@keyframes aiLoadPulse {
  0%   { transform: translateX(-100%); }
  100% { transform: translateX(400%); }
}

/* ─────────────────────────────────────────────
   TAB CONTENT
   ───────────────────────────────────────────── */
.tbp-tab-content { flex: 1; overflow: hidden; position: relative; }
.tbp-tab {
  position: absolute; inset: 0;
  overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  opacity: 0; pointer-events: none;
  /* Transform + opacity for directional slide transitions */
  transition: opacity 0.22s ease, transform 0.26s cubic-bezier(0.4, 0, 0.2, 1);
  padding-bottom: calc(var(--nav-h) + var(--safe-b) + 8px);
  /* Promote to GPU layer so slide + opacity never triggers layout */
  will-change: transform, opacity;
}
.tbp-tab.active { opacity: 1; pointer-events: auto; transform: translateX(0) !important; }

/* ─────────────────────────────────────────────
   FULLSCREEN READER MODE
   When a book is open, $app gets .tbp-reading.
   This hides the header and bottom nav so the
   reader shell fills the entire viewport.
   The AI panel, FAB and back button remain
   inside .tbp-reader-shell which already uses
   position:absolute;inset:0.
   ───────────────────────────────────────────── */
.tbp-reading .tbp-header        { display: none !important; }
.tbp-reading .tbp-offline-bar   { display: none !important; }
.tbp-reading .tbp-ai-loading-bar{ display: none !important; }
.tbp-reading .tbp-bottom-nav    { display: none !important; }
.tbp-reading .tbp-tab-content   { flex: 1 1 100%; overflow: hidden; }
.tbp-reading .tbp-tab           { padding-bottom: 0; }

/* On desktop grid layout, keep panels visible but collapse the chrome */
@media (min-width: 1024px) {
  .tbp-reading.tbp-app {
    grid-template-rows: 0 0 1fr 0 !important;
  }
  .tbp-reading .tbp-header,
  .tbp-reading .tbp-bottom-nav   { visibility: hidden; height: 0; overflow: hidden; }
}

/* ─────────────────────────────────────────────
   BOTTOM NAV
   ───────────────────────────────────────────── */
.tbp-bottom-nav {
  flex-shrink: 0;
  height: calc(var(--nav-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: var(--c-surface);
  border-top: 1px solid var(--c-border);
  display: grid;
  grid-template-columns: 1fr 68px 1fr;
  padding-left: var(--safe-l); padding-right: var(--safe-r);
  z-index: 100;
}
.tbp-nav-side {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 3px; padding: 8px 0;
  background: none; border: none; font-size: 10px;
  font-weight: 700; color: var(--c-text-dim);
  transition: color 0.15s; letter-spacing: 0.2px;
  text-transform: uppercase;
}
.tbp-nav-side.active { color: var(--c-primary); }
.tbp-nav-center-wrap {
  display: flex; align-items: center; justify-content: center; position: relative;
}
.tbp-nav-center-btn {
  width: 52px; height: 52px; border-radius: 50%;
  background: var(--c-primary); color: #fff; border: none;
  display: flex; align-items: center; justify-content: center;
  position: relative; top: -10px;
  box-shadow: var(--sh-primary);
  transition: transform 0.18s, box-shadow 0.18s, background 0.15s;
}
.tbp-nav-center-btn:active { transform: scale(0.91) translateY(-2px); box-shadow: 0 2px 8px rgba(10,191,163,0.3); }
.tbp-nav-center-btn.active { background: var(--c-primary-dk); }
.tbp-nav-center-label {
  font-size: 9px; font-weight: 800; color: var(--c-primary);
  text-transform: uppercase; letter-spacing: 0.5px;
  position: absolute; bottom: 5px; left: 50%; transform: translateX(-50%);
  white-space: nowrap;
}

/* ─────────────────────────────────────────────
   HOME TAB
   ───────────────────────────────────────────── */
.tbp-home-search { padding: 18px 16px 10px; }
.tbp-search-box {
  display: flex; align-items: center; gap: 10px;
  background: var(--c-surface); border-radius: var(--r-2xl);
  padding: 12px 14px; border: 1.5px solid var(--c-border-dk);
  box-shadow: var(--sh-1);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.tbp-search-box:focus-within {
  border-color: var(--c-primary);
  box-shadow: 0 0 0 3px var(--c-primary-lt), var(--sh-1);
}
.tbp-search-box input {
  flex: 1; background: none; border: none; outline: none;
  font-size: 15px; color: var(--c-text); font-weight: 500;
}
.tbp-search-box input::placeholder { color: var(--c-text-dim); font-weight: 400; }
.tbp-search-icon { color: var(--c-text-dim); display: flex; align-items: center; flex-shrink: 0; }
.tbp-search-scan-btn {
  background: var(--c-primary-lt); border: none;
  border-radius: var(--r-full); padding: 6px 13px;
  font-size: 11.5px; font-weight: 800; color: var(--c-primary); flex-shrink: 0;
  letter-spacing: 0.3px;
}
.tbp-search-scan-btn:active { background: var(--c-primary); color: #fff; }

/* Quick actions */
.tbp-quick-row {
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 9px; padding: 2px 16px 18px;
}
.tbp-quick-card {
  background: var(--c-surface); border-radius: var(--r-xl);
  padding: 13px 6px 11px; text-align: center; cursor: pointer;
  box-shadow: var(--sh-0); border: 1px solid var(--c-border);
  display: flex; flex-direction: column; align-items: center; gap: 7px;
  transition: transform 0.15s, box-shadow 0.15s;
  user-select: none;
}
.tbp-quick-card:active { transform: scale(0.93); box-shadow: none; }
.tbp-quick-icon {
  width: 40px; height: 40px; border-radius: var(--r-md);
  display: flex; align-items: center; justify-content: center;
}
.tbp-quick-label {
  font-size: 10.5px; font-weight: 800; color: var(--c-text-mid);
  letter-spacing: 0.2px;
}
.q-ai    .tbp-quick-icon { background: var(--c-primary-lt); color: var(--c-primary); }
.q-scan  .tbp-quick-icon { background: var(--c-blue-lt);    color: var(--c-blue);    }
.q-import .tbp-quick-icon { background: var(--c-accent-lt); color: var(--c-accent); }
.q-quiz  .tbp-quick-icon { background: var(--c-amber-lt);  color: #B45309;           }
/* Row 2 quick cards */
.q-teacher .tbp-quick-icon { background: var(--c-indigo-lt); color: var(--c-indigo); }
.q-parent  .tbp-quick-icon { background: var(--c-green-lt);  color: var(--c-green);  }
.q-models  .tbp-quick-icon { background: var(--c-primary-lt);color: var(--c-primary);}
.q-peers   .tbp-quick-icon { background: var(--c-blue-lt);   color: var(--c-blue);   }

/* Section */
.tbp-section { padding: 0 16px 2px; }
.tbp-section-hdr {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 11px; padding-top: 4px;
}
.tbp-section-ttl {
  font-size: 15px; font-weight: 800; color: var(--c-text);
  letter-spacing: -0.2px;
}
.tbp-section-link {
  font-size: 13px; font-weight: 700; color: var(--c-primary); cursor: pointer;
}

/* Book cards */
.tbp-book-card {
  background: var(--c-surface); border-radius: var(--r-xl);
  padding: 13px 14px; margin-bottom: 9px; cursor: pointer;
  box-shadow: var(--sh-1); border: 1px solid var(--c-border);
  display: flex; align-items: center; gap: 13px;
  transition: transform 0.15s, box-shadow 0.15s; user-select: none;
}
.tbp-book-card:active { transform: scale(0.97); box-shadow: var(--sh-0); }
.tbp-book-thumb {
  width: 42px; height: 56px; border-radius: var(--r-md); flex-shrink: 0;
  background: linear-gradient(145deg, var(--c-primary-lt), var(--c-primary-xl));
  border: 1px solid var(--c-primary-lt);
  display: flex; align-items: center; justify-content: center;
  color: var(--c-primary);
}
.tbp-book-info { flex: 1; min-width: 0; }
.tbp-book-title {
  font-size: 14px; font-weight: 800; color: var(--c-text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-bottom: 3px; letter-spacing: -0.2px;
}
.tbp-book-meta { font-size: 11px; color: var(--c-text-dim); margin-bottom: 6px; }
.tbp-book-prog { height: 3px; background: var(--c-surface3); border-radius: 2px; overflow: hidden; }
.tbp-book-prog-fill {
  height: 100%; background: var(--c-primary);
  transition: width 0.5s ease;
}
.tbp-book-cta {
  flex-shrink: 0; padding: 7px 13px; border-radius: var(--r-full);
  background: var(--c-primary-lt); border: none;
  font-size: 12px; font-weight: 800; color: var(--c-primary);
  transition: background 0.12s;
}
.tbp-book-cta:active { background: var(--c-primary); color: #fff; }

/* Empty state */
.tbp-empty {
  text-align: center; padding: 36px 20px;
  background: var(--c-surface); border-radius: var(--r-xl);
  border: 1.5px dashed var(--c-border-dk);
}
.tbp-empty-icon { margin-bottom: 10px; color: var(--c-text-dim); opacity: 0.6; }
.tbp-empty-title {
  font-size: 15px; font-weight: 800; color: var(--c-text-mid);
  margin-bottom: 5px; letter-spacing: -0.2px;
}
.tbp-empty-sub { font-size: 13px; color: var(--c-text-dim); margin-bottom: 16px; line-height: 1.5; }
.tbp-empty-btn {
  display: inline-block; padding: 10px 22px; border-radius: var(--r-full);
  background: var(--c-primary); color: #fff; border: none;
  font-size: 13px; font-weight: 800; letter-spacing: 0.2px;
}
.tbp-empty-btn:active { opacity: 0.85; }

/* ─────────────────────────────────────────────
   READER TAB
   ───────────────────────────────────────────── */
.tbp-reader-shell {
  display: flex; flex-direction: column;
  position: absolute; inset: 0; background: var(--c-bg);
}

/* Shelf (no book open) */
.tbp-reader-shelf { padding: 20px 16px; }
.tbp-reader-hero {
  background: linear-gradient(145deg, var(--c-primary), var(--c-primary-dk));
  border-radius: var(--r-2xl); padding: 26px 22px 24px;
  margin-bottom: 22px; text-align: center;
}
.tbp-reader-hero-icon { margin-bottom: 10px; color: rgba(255,255,255,0.9); }
.tbp-reader-hero-title {
  font-size: 21px; font-weight: 800; color: #fff;
  margin-bottom: 5px; letter-spacing: -0.4px;
}
.tbp-reader-hero-sub { font-size: 13px; color: rgba(255,255,255,0.72); margin-bottom: 18px; }
.tbp-reader-hero-btn {
  background: #fff; color: var(--c-primary-dk);
  border: none; border-radius: var(--r-full);
  padding: 11px 26px; font-size: 14px; font-weight: 800;
  box-shadow: 0 4px 14px rgba(0,0,0,0.15);
}
.tbp-reader-hero-btn:active { transform: scale(0.96); }

/* Reader top bar */
.tbp-reader-topbar {
  background: var(--c-surface);
  border-bottom: 1px solid var(--c-border);
  padding: 0 6px;
  /* In PWA standalone mode the status bar overlaps the top of the reader shell.
     env(safe-area-inset-top) pushes the topbar content below it. */
  padding-top: env(safe-area-inset-top, 0px);
  display: flex; align-items: center; gap: 4px;
  flex-shrink: 0; z-index: 10;
  height: calc(52px + env(safe-area-inset-top, 0px));
}
.tbp-reader-back {
  width: 40px; height: 40px; border-radius: var(--r-md);
  background: none; border: none; color: var(--c-text-mid);
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.tbp-reader-back:active { background: var(--c-surface2); }
.tbp-reader-book-title {
  flex: 1; font-size: 14px; font-weight: 700; color: var(--c-text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  letter-spacing: -0.1px;
}
.tbp-reader-tools { display: flex; gap: 1px; flex-shrink: 0; }
.tbp-reader-tool {
  height: 36px; border-radius: var(--r-md); border: none;
  background: none; color: var(--c-text-mid);
  display: flex; align-items: center; justify-content: center;
  font-size: 12.5px; font-weight: 800; padding: 0 8px; min-width: 36px;
}
.tbp-reader-tool:active { background: var(--c-surface2); }
.tbp-reader-tool.teal {
  background: var(--c-primary-lt); color: var(--c-primary);
  border-radius: var(--r-full); padding: 0 12px;
}
.tbp-reader-tool.teal:active { background: var(--c-primary); color: #fff; }

/* Progress bar */
.tbp-reader-progress { height: 2px; background: var(--c-surface3); flex-shrink: 0; }
.tbp-reader-progress-fill { height: 100%; background: var(--c-primary); transition: width 0.6s ease; }

/* Reader content area */
.tbp-reader-content {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  /* Momentum scrolling — essential on iOS/Android for inertia-scroll feel */
  -webkit-overflow-scrolling: touch;
  background: var(--c-surface);
  /* GPU compositing — promotes scroll container to its own layer so the
     browser can scroll without triggering layout or paint on every frame */
  will-change: scroll-position;
  /* Prevent rubber-band bleed-through to the shell behind the content */
  overscroll-behavior-y: contain;
  /* Isolate layout/paint so scrolling never triggers ancestor reflow */
  contain: layout style;
  /* Disable subpixel thinning inside the reader — keeps strokes solid on
     budget Android screens where antialiased causes a washed-out look. */
  -webkit-font-smoothing: auto;
  -moz-osx-font-smoothing: auto;
}

/* Text book content */
.tbp-reader-text {
  padding: 24px 22px 60px;
  /* System UI stack: crisp on every Android/iOS device, no web-font download.
     Falls back through common Android fonts before generic sans-serif. */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  font-size: 17px;
  /* 1.7 keeps lines readable without letting strokes bleed into whitespace */
  line-height: 1.7;
  font-weight: 400;
  /* Hard-coded so no variable indirection can accidentally lower contrast */
  color: #1A2B34;
  max-width: 680px; margin: 0 auto;
}
.tbp-reader-text p  { margin-bottom: 1.0em; text-indent: 1.4em; }
.tbp-reader-text p:first-child,
.tbp-reader-text h1 + p,
.tbp-reader-text h2 + p,
.tbp-reader-text h3 + p { text-indent: 0; }
.tbp-reader-text h1 { font-family: var(--f-head); font-size: 1.55em; font-weight: 800; color: #1A2B34; margin: 1.4em 0 0.5em; letter-spacing: -0.4px; line-height: 1.2; }
.tbp-reader-text h2 { font-family: var(--f-head); font-size: 1.25em; font-weight: 800; color: #1A2B34; margin: 1.3em 0 0.4em; letter-spacing: -0.3px; }
.tbp-reader-text h3 { font-family: var(--f-head); font-size: 1.08em; font-weight: 700; color: #1A2B34; margin: 1.2em 0 0.3em; }
.tbp-reader-text strong, .tbp-reader-text b { font-weight: 700; color: #1A2B34; }
.tbp-reader-text em, .tbp-reader-text i { font-style: italic; }
.tbp-reader-text code {
  font-family: var(--f-mono); font-size: 0.85em;
  background: var(--c-surface2); border-radius: var(--r-xs);
  padding: 1px 5px; color: var(--c-primary-dk);
}
.tbp-reader-text pre {
  background: var(--c-surface2); border-radius: var(--r-md);
  padding: 14px; overflow-x: auto; margin: 1em 0;
  font-family: var(--f-mono); font-size: 13px; line-height: 1.6;
}
.tbp-reader-text blockquote {
  border-left: 3px solid var(--c-primary); padding-left: 16px;
  margin: 1em 0; color: #3D5261; font-style: italic;
}
.tbp-reader-text ul, .tbp-reader-text ol { margin: 0.8em 0 0.8em 1.6em; }
.tbp-reader-text li { margin-bottom: 0.3em; text-indent: 0; color: #1A2B34; }
.tbp-reader-text hr { border: none; border-top: 1px solid var(--c-border-dk); margin: 1.5em 0; }
/* Links inside imported HTML books */
.tbp-reader-text a { color: var(--c-primary-dk); text-decoration: underline; }


/* Loading spinner while EPUB/DOCX is being parsed */
.tbp-reader-loading {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; height: 100%; gap: 16px;
  color: var(--c-text-mid); font-size: 14px;
}
.tbp-reader-spinner {
  width: 36px; height: 36px; border-radius: 50%;
  border: 3px solid var(--c-border-dk);
  border-top-color: var(--c-primary);
  animation: tbp-spin 0.8s linear infinite;
}
@keyframes tbp-spin { to { transform: rotate(360deg); } }

/* Persistent error banner (replaces 3s toast in reader) */
.tbp-reader-error-banner {
  margin: 24px 16px; border-radius: var(--r-lg);
  background: #FEF2F2; border: 1px solid #FECACA;
  padding: 18px 16px; display: flex; flex-direction: column; gap: 12px;
}
.tbp-reb-icon { display: flex; justify-content: center; }
.tbp-reb-title {
  font-size: 15px; font-weight: 800; color: #B91C1C; margin-bottom: 4px;
}
.tbp-reb-msg {
  font-size: 13px; color: #7F1D1D; line-height: 1.5;
  word-break: break-word;
}
.tbp-reb-actions {
  display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap;
}

/* Reader tab suppresses its own scroll — .tbp-reader-content handles it */
#tab-reader {
  overflow: hidden;
  padding-bottom: 0;
}

/* Chapter separator for multi-chapter EPUB rendering */
hr.tbp-chapter-break {
  border: none; border-top: 2px dashed var(--c-border-dk);
  margin: 2em 0; opacity: 0.5;
}

/* Reader AI FAB */
.tbp-reader-fab {
  position: absolute; bottom: 16px; right: 16px;
  width: 46px; height: 46px; border-radius: 50%;
  background: var(--c-primary); color: #fff; border: none;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--sh-primary); z-index: 20;
  transition: transform 0.18s, box-shadow 0.18s;
}
.tbp-reader-fab:active { transform: scale(0.88); }
.tbp-reader-fab.open { background: var(--c-primary-dk); }

/* ── Reader AI backdrop (tap-outside-to-close) ───────────────────── */
.tbp-rap-backdrop {
  position: absolute; inset: 0;
  background: rgba(26, 43, 52, 0.38);
  backdrop-filter: blur(1px);
  z-index: 29;
  opacity: 0; pointer-events: none;
  transition: opacity 0.28s ease;
}
.tbp-rap-backdrop.visible {
  opacity: 1; pointer-events: auto;
}

/* ─── In-reader AI panel (slide-up sheet) ─── */
.tbp-reader-ai-panel {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: var(--c-surface);
  border-radius: var(--r-2xl) var(--r-2xl) 0 0;
  box-shadow: 0 -6px 40px rgba(0,0,0,0.22);
  z-index: 30; display: flex; flex-direction: column;
  max-height: 78%;
  transform: translateY(100%);
  /* Use cubic-bezier that matches native iOS/Android sheet spring feel */
  transition: transform 0.34s cubic-bezier(0.32, 0.72, 0, 1);
  /* GPU layer so the sheet slide never janks during scroll-active state */
  will-change: transform;
}
.tbp-reader-ai-panel.open { transform: translateY(0); }

/* Top-bar: handle centered, title left, X button right */
.tbp-rap-topbar {
  display: flex; align-items: center;
  padding: 6px 12px 0;
  flex-shrink: 0; position: relative;
}
.tbp-rap-handle-wrap {
  position: absolute; left: 0; right: 0; top: 8px;
  display: flex; justify-content: center; pointer-events: none;
}
.tbp-rap-handle {
  width: 36px; height: 4px; border-radius: 2px;
  background: var(--c-border-dk);
  pointer-events: auto; cursor: pointer;
}
.tbp-rap-title {
  flex: 1; font-size: 14px; font-weight: 800; color: var(--c-text);
  padding: 14px 0 10px 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  letter-spacing: -0.2px;
}
.tbp-rap-close {
  width: 36px; height: 36px; border-radius: var(--r-md);
  background: var(--c-surface2); border: none;
  display: flex; align-items: center; justify-content: center;
  color: var(--c-text-mid); flex-shrink: 0;
  margin-top: 4px;
}
.tbp-rap-close:active { background: var(--c-surface3); }

/* Quick action chips */

/* ── Reader AI mode pills ────────────────────────────────────── */
.tbp-rdr-mode-pill {
  display: inline-flex; align-items: center; padding: 5px 14px;
  border-radius: var(--r-full); font-size: 12px; font-weight: 700;
  border: 1.5px solid var(--c-border); background: transparent;
  color: var(--c-text-dim); cursor: pointer; white-space: nowrap;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  flex-shrink: 0;
}
.tbp-rdr-mode-pill.active {
  background: var(--c-primary); color: #fff; border-color: var(--c-primary);
}
.tbp-rdr-mode-pill:active { opacity: 0.85; }

.tbp-rap-chips {
  display: flex; gap: 7px; padding: 2px 16px 14px;
  overflow-x: auto; flex-shrink: 0; scrollbar-width: none;
}
.tbp-rap-chips::-webkit-scrollbar { display: none; }
.tbp-rap-chip {
  flex-shrink: 0; background: var(--c-surface2);
  border: 1px solid var(--c-border-dk);
  border-radius: var(--r-full); padding: 8px 14px;
  font-size: 12px; font-weight: 700; color: var(--c-text-mid);
  cursor: pointer; white-space: nowrap;
  transition: all 0.12s;
}
.tbp-rap-chip:active, .tbp-rap-chip.active {
  background: var(--c-primary); border-color: var(--c-primary); color: #fff;
}
.tbp-rap-chip:disabled {
  opacity: 0.45; pointer-events: none;
}
/* Response area */
.tbp-rap-response {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 0 18px 8px; font-size: 14px; line-height: 1.65;
  color: var(--c-text); min-height: 80px;
}
.tbp-rap-response-text { white-space: pre-wrap; padding-bottom: 8px; }
.tbp-rap-response-empty {
  color: var(--c-text-dim); font-size: 13px; font-style: italic; padding: 4px 0;
}
/* Input row */
.tbp-rap-input-row {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 14px; border-top: 1px solid var(--c-border);
  flex-shrink: 0; padding-bottom: calc(10px + var(--safe-b));
}
.tbp-rap-input {
  flex: 1; background: var(--c-surface2); border: 1.5px solid var(--c-border-dk);
  border-radius: var(--r-2xl); padding: 10px 14px;
  font-size: 14px; color: var(--c-text); outline: none; resize: none;
  max-height: 100px; overflow-y: auto; min-height: 42px; line-height: 1.4;
  transition: border-color 0.15s;
}
.tbp-rap-input::placeholder { color: var(--c-text-dim); }
.tbp-rap-input:focus { border-color: var(--c-primary); }
.tbp-rap-send {
  width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0;
  background: var(--c-primary); color: #fff; border: none;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 8px rgba(10,191,163,0.35);
  transition: transform 0.15s, background 0.15s;
}
.tbp-rap-send:active { transform: scale(0.88); }
.tbp-rap-send:disabled { background: var(--c-surface3); box-shadow: none; }

/* FAB hidden state while panel is open */
.tbp-reader-fab.hidden { opacity: 0; pointer-events: none; transform: scale(0.75); }



/* Format pills for reader */
.tbp-format-badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: var(--r-full);
  font-size: 11px; font-weight: 700; margin-bottom: 8px;
}
.tbp-format-badge.pdf     { background: var(--c-red-lt);    color: #991B1B; }
.tbp-format-badge.epub    { background: var(--c-indigo-lt); color: #3730A3; }
.tbp-format-badge.docx    { background: var(--c-blue-lt);   color: #1e40af; }
.tbp-format-badge.text    { background: var(--c-primary-lt);color: var(--c-primary-dk); }
.tbp-format-badge.scan    { background: var(--c-green-lt);  color: #166534; }

/* ─────────────────────────────────────────────
   CHAT TAB
   ───────────────────────────────────────────── */
.tbp-chat-list-header {
  padding: 16px 16px 8px;
  display: flex; align-items: center; justify-content: space-between;
  background: var(--c-surface);
}
.tbp-chat-list-title {
  font-size: 22px; font-weight: 800; color: var(--c-text); letter-spacing: -0.5px;
}
.tbp-chat-hdr-btn {
  width: 38px; height: 38px; border-radius: var(--r-md);
  background: var(--c-surface2); border: 1px solid var(--c-border);
  display: flex; align-items: center; justify-content: center;
  color: var(--c-text-mid);
}
.tbp-chat-hdr-btn:active { background: var(--c-surface3); }

/* AI tutor pills */
.tbp-chat-ai-section { padding: 0 16px 6px; }
.tbp-chat-ai-lbl {
  font-size: 10px; font-weight: 800; color: var(--c-text-dim);
  text-transform: uppercase; letter-spacing: 0.8px; margin-bottom: 9px;
}
.tbp-chat-ai-row {
  display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px;
  scrollbar-width: none;
}
.tbp-chat-ai-row::-webkit-scrollbar { display: none; }
.tbp-ai-pill {
  flex-shrink: 0; background: var(--c-surface);
  border: 1px solid var(--c-border-dk); border-radius: var(--r-full);
  padding: 9px 14px; cursor: pointer;
  display: flex; align-items: center; gap: 7px;
  box-shadow: var(--sh-0);
  transition: transform 0.12s, box-shadow 0.12s;
  user-select: none;
}
.tbp-ai-pill:active { transform: scale(0.95); box-shadow: none; }
.tbp-ai-pill.main {
  background: linear-gradient(135deg, var(--c-primary), var(--c-primary-dk));
  border-color: transparent; box-shadow: var(--sh-primary);
}
.tbp-ai-pill-icon {
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--c-primary-lt); color: var(--c-primary);
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.tbp-ai-pill.main .tbp-ai-pill-icon { background: rgba(255,255,255,0.2); color: #fff; }
.tbp-ai-pill-name { font-size: 12.5px; font-weight: 800; color: var(--c-text-mid); white-space: nowrap; }
.tbp-ai-pill.main .tbp-ai-pill-name { color: #fff; }

/* Chat section headers */
.tbp-chat-rows { padding: 4px 0; }
.tbp-chat-row-section {
  font-size: 10px; font-weight: 800; color: var(--c-text-dim);
  text-transform: uppercase; letter-spacing: 0.8px;
  padding: 10px 16px 4px;
}
.tbp-chat-row {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 16px; cursor: pointer;
  transition: background 0.1s; user-select: none;
}
.tbp-chat-row:active { background: var(--c-surface2); }
.tbp-chat-avatar {
  width: 46px; height: 46px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 800; color: #fff; flex-shrink: 0;
  letter-spacing: -0.3px;
}
.tbp-chat-avatar.av-ai    { background: linear-gradient(135deg, var(--c-primary), var(--c-primary-dk)); }
.tbp-chat-avatar.av-group { background: linear-gradient(135deg, var(--c-blue), var(--c-indigo)); }
.tbp-chat-avatar.av-peer  { background: linear-gradient(135deg, var(--c-accent), #FF9A5C); }
.tbp-chat-row-info { flex: 1; min-width: 0; }
.tbp-chat-row-name {
  font-size: 15px; font-weight: 700; color: var(--c-text);
  margin-bottom: 2px; letter-spacing: -0.1px;
}
.tbp-chat-row-preview {
  font-size: 12.5px; color: var(--c-text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tbp-chat-row-time { font-size: 11px; color: var(--c-text-dim); flex-shrink: 0; align-self: flex-start; margin-top: 3px; }
.tbp-chat-unread {
  min-width: 20px; height: 20px; background: var(--c-primary);
  border-radius: var(--r-full); font-size: 11px; font-weight: 800;
  color: #fff; display: flex; align-items: center; justify-content: center;
  padding: 0 5px; flex-shrink: 0; align-self: flex-start; margin-top: 3px;
}

/* ─── Thread view ─── */
.tbp-thread {
  display: flex; flex-direction: column; height: 100%;
  position: absolute; inset: 0; background: var(--c-bg);
  transform: translateX(100%);
  transition: transform 0.26s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 10;
}
.tbp-thread.open { transform: translateX(0); }
.tbp-thread-header {
  background: var(--c-surface); border-bottom: 1px solid var(--c-border);
  padding: 0 6px; display: flex; align-items: center; gap: 8px;
  flex-shrink: 0; height: 54px;
}
.tbp-thread-back {
  width: 40px; height: 40px; border-radius: var(--r-md);
  background: none; border: none; color: var(--c-text-mid);
  display: flex; align-items: center; justify-content: center;
}
.tbp-thread-back:active { background: var(--c-surface2); }
.tbp-thread-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 800; color: #fff; flex-shrink: 0;
}
.tbp-thread-info { flex: 1; min-width: 0; }
.tbp-thread-name {
  font-size: 15px; font-weight: 800; color: var(--c-text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  letter-spacing: -0.2px;
}
.tbp-thread-status { font-size: 11px; color: var(--c-primary); font-weight: 600; }

.tbp-thread-messages {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 14px 12px; display: flex; flex-direction: column; gap: 5px;
  background: var(--c-bg);
}
.tbp-bubble {
  max-width: 80%; padding: 10px 13px; border-radius: 18px;
  font-size: 14px; line-height: 1.5; word-break: break-word;
  white-space: pre-wrap;
}
.tbp-bubble-out {
  align-self: flex-end; background: var(--c-primary); color: #fff;
  border-bottom-right-radius: 4px;
}
.tbp-bubble-in {
  align-self: flex-start; background: var(--c-surface); color: var(--c-text);
  border-bottom-left-radius: 4px; box-shadow: var(--sh-0);
}
.tbp-bubble-name { font-size: 11px; font-weight: 800; color: var(--c-primary); margin-bottom: 4px; }
.tbp-bubble-typing {
  display: flex; gap: 4px; align-items: center; padding: 12px 14px;
}
.tbp-bubble-typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--c-text-dim);
  animation: typingBounce 1.2s ease infinite;
}
.tbp-bubble-typing span:nth-child(2) { animation-delay: 0.2s; }
.tbp-bubble-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingBounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40%           { transform: translateY(-5px); opacity: 1; }
}

.tbp-thread-input-row {
  background: var(--c-surface); border-top: 1px solid var(--c-border);
  padding: 10px 12px calc(10px + var(--safe-b));
  display: flex; align-items: flex-end; gap: 8px; flex-shrink: 0;
}
.tbp-thread-input {
  flex: 1; background: var(--c-surface2); border: 1.5px solid var(--c-border-dk);
  border-radius: var(--r-2xl); padding: 10px 14px;
  font-size: 14px; color: var(--c-text); outline: none; resize: none;
  max-height: 120px; overflow-y: auto; min-height: 42px; line-height: 1.4;
  transition: border-color 0.15s;
}
.tbp-thread-input::placeholder { color: var(--c-text-dim); }
.tbp-thread-input:focus { border-color: var(--c-primary); }
.tbp-thread-send {
  width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0;
  background: var(--c-primary); color: #fff; border: none;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 8px rgba(10,191,163,0.3);
  transition: transform 0.15s;
}
.tbp-thread-send:active { transform: scale(0.88); }

/* ─────────────────────────────────────────────
   MODALS
   ───────────────────────────────────────────── */
.tbp-modal-overlay {
  position: fixed; inset: 0;
  background: rgba(26, 43, 52, 0.56);
  backdrop-filter: blur(4px);
  z-index: 2000; display: none;
  align-items: flex-end; justify-content: center;
}
.tbp-modal-overlay.active { display: flex; }
.tbp-modal {
  background: var(--c-surface);
  border-radius: var(--r-2xl) var(--r-2xl) 0 0;
  width: 100%; max-width: 600px;
  max-height: 90vh; overflow-y: auto;
  animation: slideUpModal 0.26s cubic-bezier(0.34, 1.2, 0.64, 1);
}
.tbp-modal-handle {
  width: 36px; height: 4px; border-radius: 2px;
  background: var(--c-border-dk); margin: 10px auto 0;
}
.tbp-modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 20px 10px;
}
.tbp-modal-title {
  font-size: 17px; font-weight: 800; color: var(--c-text); letter-spacing: -0.3px;
}
.tbp-modal-close {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--c-surface2); border: none;
  display: flex; align-items: center; justify-content: center;
  color: var(--c-text-mid); font-size: 18px; line-height: 1;
}
.tbp-modal-close:active { background: var(--c-surface3); }
.tbp-modal-body { padding: 4px 20px 16px; }
.tbp-modal-ds-footer {
  text-align: center; padding: 8px 20px 14px;
  font-size: 11px; font-weight: 700; color: var(--c-text-dim);
  border-top: 1px solid var(--c-border); margin-top: 4px;
  letter-spacing: 0.2px;
}
.tbp-modal-ds-footer span { font-weight: 500; opacity: 0.7; }
@keyframes slideUpModal {
  from { transform: translateY(60px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

/* ─────────────────────────────────────────────
   COMPONENT LIBRARY
   ───────────────────────────────────────────── */

/* Buttons */
.tbp-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 11px 22px; border-radius: var(--r-full);
  font-size: 14px; font-weight: 800; border: none; cursor: pointer;
  transition: opacity 0.15s, transform 0.12s;
  letter-spacing: 0.1px;
}
.tbp-btn:active { transform: scale(0.96); }
.tbp-btn:disabled { opacity: 0.45; pointer-events: none; }
.tbp-btn-primary   { background: var(--c-primary); color: #fff; }
.tbp-btn-secondary { background: var(--c-surface2); color: var(--c-text-mid); border: 1.5px solid var(--c-border-dk); }
.tbp-btn-ghost     { background: none; color: var(--c-primary); }
.tbp-btn-sm        { padding: 7px 16px; font-size: 12px; }
.tbp-btn-danger    { background: var(--c-red-lt); color: var(--c-red); }
.tbp-btn-full      { width: 100%; }

/* Cards */
.tbp-card {
  background: var(--c-surface); border-radius: var(--r-xl);
  padding: 15px; margin-bottom: 10px;
  box-shadow: var(--sh-1); border: 1px solid var(--c-border);
}

/* Forms */
.tbp-form-group { margin-bottom: 16px; }
.tbp-form-label {
  display: block; font-size: 11px; font-weight: 800;
  color: var(--c-text-dim); text-transform: uppercase;
  letter-spacing: 0.6px; margin-bottom: 6px;
}
.tbp-form-input {
  width: 100%; padding: 11px 13px; border-radius: var(--r-md);
  border: 1.5px solid var(--c-border-dk); background: var(--c-surface);
  font-size: 14px; color: var(--c-text); outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.tbp-form-input:focus {
  border-color: var(--c-primary);
  box-shadow: 0 0 0 3px var(--c-primary-lt);
}
select.tbp-form-input { cursor: pointer; }
textarea.tbp-form-input { resize: vertical; min-height: 80px; }

/* Progress */
.tbp-prog { height: 4px; background: var(--c-surface3); border-radius: 2px; overflow: hidden; }
.tbp-prog-fill {
  height: 100%; background: var(--c-primary); transition: width 0.4s ease;
}

/* Badges */
.tbp-badge {
  display: inline-flex; align-items: center;
  padding: 3px 9px; border-radius: var(--r-full);
  font-size: 11px; font-weight: 700; letter-spacing: 0.2px;
}
.tbp-badge-green  { background: var(--c-green-lt);  color: #166534; }
.tbp-badge-blue   { background: var(--c-blue-lt);   color: #1e40af; }
.tbp-badge-amber  { background: var(--c-amber-lt);  color: #92400E; }
.tbp-badge-teal   { background: var(--c-primary-lt); color: var(--c-primary-dk); }
.tbp-badge-red    { background: var(--c-red-lt);     color: #991B1B; }

/* Toast — slide-up enter, fade-out dismiss */
.tbp-toast {
  position: fixed; bottom: calc(var(--nav-h) + var(--safe-b) + 16px);
  left: 50%;
  /* Start below, invisible */
  transform: translateX(-50%) translateY(18px);
  background: var(--c-text); color: #fff;
  padding: 10px 20px; border-radius: var(--r-full);
  font-size: 13px; font-weight: 700; z-index: 9999;
  max-width: calc(100vw - 40px); text-align: center; white-space: nowrap;
  box-shadow: var(--sh-3); letter-spacing: 0.1px;
  opacity: 0; pointer-events: none;
  transition: opacity 0.22s ease, transform 0.26s cubic-bezier(0.34, 1.56, 0.64, 1);
  will-change: transform, opacity;
}
/* JS adds this class to trigger entrance */
.tbp-toast.tbp-toast-in {
  opacity: 1; transform: translateX(-50%) translateY(0);
}
/* JS adds this to trigger exit */
.tbp-toast.tbp-toast-out {
  opacity: 0; transform: translateX(-50%) translateY(-8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

/* Spinner */
.tbp-spinner {
  width: 30px; height: 30px;
  border: 2.5px solid var(--c-border-dk);
  border-top-color: var(--c-primary);
  border-radius: 50%;
  animation: spin 0.65s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Skeleton */
.tbp-skel {
  background: linear-gradient(90deg, var(--c-surface2) 25%, var(--c-surface3) 50%, var(--c-surface2) 75%);
  background-size: 200% 100%;
  animation: skelWave 1.6s ease infinite;
  border-radius: var(--r-md);
}
@keyframes skelWave { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* Quiz */
.tbp-quiz-q { padding: 14px 0; border-bottom: 1px solid var(--c-border); }
.tbp-quiz-q:last-child { border-bottom: none; }
.tbp-quiz-q-text { font-size: 15px; font-weight: 700; margin-bottom: 12px; color: var(--c-text); letter-spacing: -0.1px; }
.tbp-quiz-opt {
  display: flex; align-items: center; gap: 11px;
  padding: 10px 11px; border-radius: var(--r-md); cursor: pointer;
  transition: background 0.1s; margin-bottom: 5px;
}
.tbp-quiz-opt:active { background: var(--c-surface2); }
.tbp-quiz-opt-radio {
  width: 20px; height: 20px; border-radius: 50%; flex-shrink: 0;
  border: 2px solid var(--c-border-dk); background: var(--c-surface);
  transition: all 0.15s;
}
.tbp-quiz-opt.selected .tbp-quiz-opt-radio { background: var(--c-primary); border-color: var(--c-primary); }
.tbp-quiz-opt.correct  { background: var(--c-green-lt); border-radius: var(--r-md); }
.tbp-quiz-opt.wrong    { background: var(--c-red-lt);   border-radius: var(--r-md); }

/* Peer chips */
.tbp-peers { display: flex; flex-wrap: wrap; gap: 7px; padding: 10px 16px; }
.tbp-peer-chip {
  background: var(--c-surface); border: 1px solid var(--c-border);
  border-radius: var(--r-full); padding: 7px 13px;
  font-size: 12px; font-weight: 700; color: var(--c-text-mid); cursor: pointer;
}
.tbp-peer-chip.online { background: var(--c-green-lt); color: #166534; border-color: transparent; }

/* Scrollbar */
::-webkit-scrollbar { width: 3px; height: 3px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--c-border-dk); border-radius: 2px; }

/* PIN overlay */
.tbp-pin-overlay {
  position: fixed; inset: 0; background: rgba(26,43,52,0.76);
  backdrop-filter: blur(6px); z-index: 10000;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity 0.22s; padding: 20px;
}
.tbp-pin-overlay.active { opacity: 1; }
.tbp-pin-overlay.tbp-pin-dismissing { opacity: 0; transition: opacity 0.24s ease; }
.tbp-pin-sheet {
  background: var(--c-surface); border-radius: var(--r-2xl);
  width: 100%; max-width: 340px; padding: 28px 24px 20px;
  box-shadow: var(--sh-3);
  display: flex; flex-direction: column; align-items: center;
  transform: translateY(0); transition: transform 0.22s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.tbp-pin-overlay:not(.active) .tbp-pin-sheet { transform: translateY(24px); }
.tbp-pin-header { text-align: center; margin-bottom: 20px; }
.tbp-pin-icon {
  width: 52px; height: 52px; background: var(--c-primary-lt);
  border-radius: var(--r-xl); display: flex; align-items: center;
  justify-content: center; color: var(--c-primary); margin: 0 auto 14px;
}
.tbp-pin-title { font-size: 18px; font-weight: 800; color: var(--c-text); margin-bottom: 4px; letter-spacing: -0.3px; }
.tbp-pin-subtitle { font-size: 13px; color: var(--c-text-dim); }
.tbp-pin-dots { display: flex; gap: 14px; margin-bottom: 10px; }
.tbp-pin-dot {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--c-border-dk); background: transparent;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
.tbp-pin-dot.filled { background: var(--c-primary); border-color: var(--c-primary); transform: scale(1.15); }
.tbp-pin-error {
  font-size: 12px; font-weight: 700; color: var(--c-red);
  min-height: 18px; margin-bottom: 8px; opacity: 0; text-align: center;
  transition: opacity 0.2s;
}
.tbp-pin-error.visible { opacity: 1; }
@keyframes tbp-shake {
  0%,100% { transform: translateX(0); }
  20%     { transform: translateX(-6px); }
  40%     { transform: translateX(6px); }
  60%     { transform: translateX(-4px); }
  80%     { transform: translateX(4px); }
}
.tbp-pin-dots.shake { animation: tbp-shake 0.45s ease; }
.tbp-numpad { width: 100%; display: flex; flex-direction: column; gap: 6px; margin-top: 4px; }
.tbp-numpad-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.tbp-numpad-btn {
  aspect-ratio: 1.25; border: none; border-radius: var(--r-lg);
  background: var(--c-surface2); color: var(--c-text);
  font-size: 22px; font-weight: 600; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.1s, transform 0.08s;
  user-select: none; -webkit-tap-highlight-color: transparent;
}
.tbp-numpad-btn:active { background: var(--c-surface3); transform: scale(0.93); }
.tbp-numpad-action { font-size: 16px; color: var(--c-text-mid); }
.tbp-pin-cancel {
  margin-top: 14px; background: none; border: none;
  font-size: 13px; font-weight: 700; color: var(--c-text-dim); cursor: pointer;
  padding: 6px 14px; border-radius: var(--r-full); transition: color 0.15s;
}
.tbp-pin-cancel:active { color: var(--c-text-mid); }
.tbp-pin-ds-footer {
  margin-top: 12px; font-size: 11px; font-weight: 700;
  color: var(--c-text-dim); letter-spacing: 0.2px; text-align: center;
}
.tbp-pin-ds-footer span { font-weight: 500; opacity: 0.7; }

/* Branding */
.tbp-branding-tagline  { font-size: 12px; color: rgba(255,255,255,0.7); margin-top: 3px; }
.tbp-branding-version  { font-size: 11px; color: rgba(255,255,255,0.5); margin-top: 2px; }
.tbp-branding-preview-card { border-radius: var(--r-lg); overflow: hidden; border: 1px solid var(--c-border); box-shadow: var(--sh-1); }
.tbp-branding-preview-header { padding: 16px 18px; }
.tbp-branding-preview-name { font-size: 20px; font-weight: 800; color: #fff; }
.tbp-branding-preview-tagline { font-size: 12px; color: rgba(255,255,255,0.75); margin-top: 3px; }
.tbp-color-picker-row { display: flex; gap: 10px; align-items: center; }
.tbp-color-input { width: 48px; height: 48px; border: none; background: none; cursor: pointer; border-radius: var(--r-md); padding: 0; overflow: hidden; flex-shrink: 0; }
.tbp-color-preview { flex: 1; height: 48px; border-radius: var(--r-md); border: 1px solid var(--c-border); transition: background 0.2s; }
.tbp-color-presets { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.tbp-color-chip { width: 30px; height: 30px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; transition: transform 0.12s, box-shadow 0.12s; -webkit-tap-highlight-color: transparent; }
.tbp-color-chip:hover { transform: scale(1.18); box-shadow: 0 2px 8px rgba(0,0,0,0.2); }
.tbp-class-card { background: var(--c-surface); border-radius: var(--r-xl); padding: 15px; margin-bottom: 10px; box-shadow: var(--sh-1); border: 1px solid var(--c-border); }

/* ─────────────────────────────────────────────
   TABLET / DESKTOP  (>= 768px)
   ───────────────────────────────────────────── */
@media (min-width: 768px) {
  .tbp-modal { border-radius: var(--r-2xl); margin: auto; max-width: 560px; max-height: 82vh; }
  .tbp-modal-overlay { align-items: center; }
  .tbp-quick-row { gap: 12px; padding: 2px 20px 18px; }
  .tbp-section { padding: 0 20px 2px; }
  .tbp-home-search { padding: 18px 20px 10px; }
}

@media (min-width: 1024px) {
  :root { --panel-w: 300px; }
  .tbp-app {
    display: grid;
    grid-template-columns: 300px 1fr 300px;
    grid-template-rows: auto auto 1fr auto;
  }
  .tbp-header       { grid-column: 2; grid-row: 1; }
  .tbp-offline-bar  { grid-column: 2; grid-row: 2; }
  .tbp-ai-loading-bar { grid-column: 2; grid-row: 2; }
  .tbp-tab-content  { grid-column: 2; grid-row: 3; }
  .tbp-bottom-nav   { grid-column: 2; grid-row: 4; }
  .tbp-panel-left   { position: static; transform: none !important; visibility: visible !important; border-right: 1px solid var(--c-border); box-shadow: none; grid-column: 1; grid-row: 1 / 5; }
  .tbp-panel-right  { position: static; transform: none !important; visibility: visible !important; border-left: 1px solid var(--c-border); box-shadow: none; grid-column: 3; grid-row: 1 / 5; }
  .tbp-panel-overlay { display: none !important; }
  .tbp-reader-fab   { right: calc(300px + 16px); }
}

/* =================================================================
   READER ENGINES — PDF / EPUB / DOCX canvas & container styles
   ================================================================= */

/* ── PDF viewer (PDF.js canvas path + native bitmap path) ─────── */
.tbp-pdf-viewer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 16px 0 32px;
  width: 100%;
  box-sizing: border-box;
}

.tbp-pdf-page {
  width: 100%;
  max-width: 800px;
  background: #fff;
  border-radius: var(--r-md);
  box-shadow: 0 2px 12px rgba(0,0,0,.12);
  overflow: hidden;
  line-height: 0; /* remove gap under inline canvas/img */
}

/* PDF.js renders into <canvas> */
.tbp-pdf-canvas {
  display: block;
  width: 100%;
  height: auto;
}

/* Native bridge renders into <img> */
.tbp-pdf-img {
  display: block;
  width: 100%;
  height: auto;
  min-height: 40px;
  background: #f5f5f5;
}

/* Loading placeholder shimmer for lazy pages */
.tbp-pdf-img:not([src]),
.tbp-pdf-img[src=""] {
  background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: tbp-shimmer 1.4s infinite;
  min-height: 300px;
}

@keyframes tbp-shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ── EPUB viewer (native XHTML renderer — no iframe) ─────────────── */
/* .tbp-epub-viewer is no longer used; EPUB content renders into         */
/* .tbp-reader-text exactly like plain text / HTML books.               */
.tbp-epub-viewer {
  width: 100%;
  height: 100%;
  min-height: 600px;
  flex: 1;
  overflow: hidden;
}

/* ── Chapter separator (epub fallback + future) ─────────────────── */
.tbp-chapter-break {
  border: none;
  border-top: 1px solid var(--c-border);
  margin: 32px 0;
}

/* ─────────────────────────────────────────────
   PULL-TO-REFRESH
   The indicator lives inside .tbp-tab-content
   (the non-scrolling parent) so it floats above
   the tab scroll containers and is always visible.
   ───────────────────────────────────────────── */
.tbp-ptr {
  position: absolute; top: 0; left: 0; right: 0;
  height: 60px; display: flex; align-items: center;
  justify-content: center; pointer-events: none;
  z-index: 60; opacity: 0;
  /* opacity driven by JS via inline style during drag */
  transition: opacity 0.18s ease;
}
.tbp-ptr-inner {
  width: 36px; height: 36px;
  background: var(--c-surface); border-radius: 50%;
  box-shadow: var(--sh-2);
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.18s cubic-bezier(0.34,1.56,0.64,1),
              background 0.18s;
}
/* Arrow icon shown while dragging */
.tbp-ptr-arrow {
  width: 18px; height: 18px; color: var(--c-primary);
  transition: transform 0.22s cubic-bezier(0.34,1.56,0.64,1);
  /* uses currentColor stroke */
}
/* Spinner replaces arrow when refreshing */
.tbp-ptr-spinner {
  display: none;
  width: 18px; height: 18px; border-radius: 50%;
  border: 2.5px solid var(--c-primary-lt);
  border-top-color: var(--c-primary);
}
/* Threshold reached — pulse the indicator */
.tbp-ptr.tbp-ptr-ready .tbp-ptr-inner {
  transform: scale(1.12);
  background: var(--c-primary-lt);
}
.tbp-ptr.tbp-ptr-ready .tbp-ptr-arrow {
  transform: rotate(180deg); /* arrow points up = release to refresh */
}
/* Refreshing state */
.tbp-ptr.tbp-ptr-loading .tbp-ptr-inner { background: var(--c-surface); }
.tbp-ptr.tbp-ptr-loading .tbp-ptr-arrow  { display: none; }
.tbp-ptr.tbp-ptr-loading .tbp-ptr-spinner {
  display: block;
  animation: tbp-spin 0.65s linear infinite;
}

/* ─────────────────────────────────────────────
   TAB-CONTENT wrapper must be relative for PTR
   (already is position:relative from base rule,
   but explicit here for clarity)
   ───────────────────────────────────────────── */
.tbp-tab-content { position: relative; }
