/* =========================
   THEME TOKENS
   ========================= */

/* Light theme (default) */
:root {
  color-scheme: light;
  --bg: #f4f6f9;
  --panel: rgba(255,255,255,0.92);
  --panelSolid: #ffffff;
  --card: rgba(15,23,42,0.04);
  --card2: rgba(15,23,42,0.08);
  --border: rgba(15,23,42,0.12);
  --border2: rgba(15,23,42,0.10);
  --text: #0f172a;
  --muted: #475569;
  --accent: #2563eb;
  --shadow: 0 10px 30px rgba(15,23,42,0.15);
}

/* Dark theme */
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b0f14;
  --panel: rgba(20,22,28,0.92);
  --panelSolid: #111318;
  --card: rgba(255,255,255,0.06);
  --card2: rgba(255,255,255,0.10);
  --border: rgba(255,255,255,0.16);
  --border2: rgba(255,255,255,0.12);
  --text: #e5e7eb;
  --muted: #9ca3af;
  --accent: #60a5fa;
  --shadow: 0 10px 30px rgba(0,0,0,0.35);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --bg: #0b0f14;
    --panel: rgba(20,22,28,0.92);
    --panelSolid: #111318;
    --card: rgba(255,255,255,0.06);
    --card2: rgba(255,255,255,0.10);
    --border: rgba(255,255,255,0.16);
    --border2: rgba(255,255,255,0.12);
    --text: #e5e7eb;
    --muted: #9ca3af;
    --accent: #60a5fa;
    --shadow: 0 10px 30px rgba(0,0,0,0.35);
  }
}

:root {
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
}

/* =========================
   BASE
   ========================= */
*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  touch-action: manipulation;
}

.hiddenFile {
  position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0;
}

/* =========================
   TOP BAR
   ========================= */
.topbar {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  background: var(--panelSolid);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.brand .appLogo {
  width: 44px;
  height: 44px;
  object-fit: contain;
  border-radius: 10px;
  flex-shrink: 0;
}

.brandText { line-height: 1.2; }
.brandTitle { font-weight: 800; font-size: 17px; }
.brandSub { opacity: .65; font-size: 11px; margin-top: 2px; }

.topActions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.uploadBtn {
  font-weight: 700;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
  white-space: nowrap;
}

.pageInfo {
  opacity: .8;
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
}

.iconBtn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  font-size: 18px;
  line-height: 1;
  padding: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  flex-shrink: 0;
}

/* =========================
   DESKTOP LAYOUT (3-column)
   ========================= */
@media (min-width: 769px) {
  body { padding-bottom: 0; }

  main {
    display: grid;
    grid-template-columns: 300px 1fr 300px;
    gap: 12px;
    padding: 12px;
    height: calc(100vh - 65px);
  }

  .panel {
    background: var(--panelSolid);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 14px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0;
  }

  .viewer {
    background: var(--panelSolid);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px;
    overflow: auto;
  }

  canvas {
    width: 100%;
    height: auto;
    background: #fff;
    border-radius: 6px;
  }

  .sectionsList {
    max-height: 35vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #searchResults {
    max-height: 30vh;
    overflow-y: auto;
  }

  /* Hide mobile elements on desktop */
  #mobileActionBar,
  #sheetBackdrop,
  #ttsMiniBar,
  #librarySheet,
  #searchSheet,
  #askSheet {
    display: none !important;
  }
}

/* =========================
   SHARED COMPONENT STYLES
   ========================= */
.sectionHint {
  font-size: 12px;
  opacity: .7;
  margin: -4px 0 10px 0;
  line-height: 1.4;
}

.sectionMeta { font-size: 12px; opacity: .7; line-height: 1.35; }

hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }

input[type="text"], textarea, select {
  width: 100%;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 15px;
}

textarea { resize: vertical; }
select { -webkit-appearance: none; appearance: none; }

button {
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}

button:hover:not(:disabled) { background: var(--card2); }
button:disabled { opacity: .4; cursor: not-allowed; }

.results {
  margin-top: 10px;
  padding: 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  overflow-y: auto;
}

.answer {
  margin-top: 10px;
  white-space: pre-wrap;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  min-height: 60px;
  line-height: 1.4;
  font-size: 13px;
}

.safetyLine { font-size: 12px; opacity: .7; margin-top: 8px; }

.ttsControls {
  margin-top: 10px;
  background: var(--panel);
  border: 1px solid var(--border2);
  border-radius: 12px;
  padding: 10px;
}

.bestPlaceCard {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 10px;
}

.bestPlaceTitle { font-weight: 700; font-size: 13px; }
.bestPlaceExcerpt { opacity: .75; font-size: 12px; margin-top: 6px; line-height: 1.4; }
.bestPlaceActions { display: flex; gap: 8px; margin-top: 10px; align-items: center; flex-wrap: wrap; }
.bestPlaceMeta { font-size: 11px; opacity: .6; margin-left: auto; }
.bestPlaceBtn { font-size: 12px; padding: 6px 10px; }

.hit {
  padding: 8px;
  border-bottom: 1px solid var(--border2);
  cursor: pointer;
}
.hit:last-child { border-bottom: none; }
.hit:hover { background: var(--card); border-radius: 8px; }

/* =========================
   MOBILE LAYOUT
   ========================= */
@media (max-width: 768px) {

  body {
    padding-bottom: 0;
    overflow: visible;
    height: 100dvh;
  }

  /* Compact top bar */
  .topbar {
    padding: 8px 12px;
    padding-top: calc(env(safe-area-inset-top) + 8px);
    height: calc(env(safe-area-inset-top) + 52px);
  }

  .brand .appLogo { width: 32px; height: 32px; border-radius: 7px; }
  .brandTitle { font-size: 14px; }
  .brandSub { display: none; }
  .uploadBtn { display: none; } /* use action bar instead */
  .iconBtn { width: 34px; height: 34px; font-size: 16px; border-radius: 8px; }
  .pageInfo { font-size: 11px; }

  /* Main fills screen below topbar */
  main {
    display: block;
    padding: 0;
    height: calc(100dvh - env(safe-area-inset-top) - 52px);
    position: relative;
    overflow: visible;
  }

  /* Hide desktop panels */
  .panelLeft, .panelRight { display: none !important; }

  /* Viewer fills entire main area */
 .viewer {
    position: absolute;
    inset: 0;
    border-radius: 0;
    border: none;
    padding: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: #f5f5f5;
    z-index: 1;
    min-height: 100%;
  }

  canvas {
    width: 100%;
    height: auto;
    border-radius: 0;
    display: block;
  }

  /* ── Floating action bar ── */
  #mobileActionBar {
    position: fixed;
    bottom: calc(env(safe-area-inset-bottom) + 14px);
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(10, 12, 20, 0.90);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    border: 1px solid rgba(255,255,255,0.14);
    border-radius: 32px;
    padding: 7px 10px;
    z-index: 200;
    box-shadow: 0 8px 40px rgba(0,0,0,0.5);
    white-space: nowrap;
  }

  .mabBtn {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: #e9edf3;
    font-size: 17px;
    display: grid;
    place-items: center;
    cursor: pointer;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
    flex-shrink: 0;
  }

  .mabBtn:active { background: rgba(255,255,255,0.10); }
  .mabBtn:disabled { opacity: 0.28; }

  .mabDivider {
    width: 1px;
    height: 22px;
    background: rgba(255,255,255,0.14);
    flex-shrink: 0;
  }

  .mabUpload {
    background: #4da3ff;
    color: #fff;
    font-size: 12px;
    font-weight: 700;
    border-radius: 20px;
    padding: 0 13px;
    height: 36px;
    width: auto;
    border: none;
    white-space: nowrap;
    flex-shrink: 0;
  }

  .mabUpload:active { background: #3b8fe8; }

  /* ── Sheet backdrop ── */
  #sheetBackdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 499;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }

  #sheetBackdrop.backdropVisible {
    opacity: 1;
    pointer-events: auto;
  }

  /* ── Bottom sheets ── */
  .bottomSheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 500;
    border-radius: 22px 22px 0 0;
    background: var(--panelSolid);
    border-top: 1px solid var(--border);
    box-shadow: 0 -16px 48px rgba(0,0,0,0.35);
    transform: translateY(100%);
    transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1);
    max-height: 88dvh;
    display: flex;
    flex-direction: column;
  }

  .bottomSheet.sheetOpen { transform: translateY(0); }

  .sheetHandle {
    width: 36px;
    height: 4px;
    background: rgba(128,128,128,0.3);
    border-radius: 2px;
    margin: 10px auto 0;
    flex-shrink: 0;
  }

  .sheetHeader {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px 10px;
    flex-shrink: 0;
    border-bottom: 1px solid var(--border2);
  }

  .sheetTitle { font-size: 16px; font-weight: 700; color: var(--text); }

  .sheetClose {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: none;
    background: var(--card);
    color: var(--muted);
    font-size: 14px;
    display: grid;
    place-items: center;
    padding: 0;
  }

  .sheetContent {
    touch-action: pan-y;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 14px 16px;
    padding-bottom: calc(env(safe-area-inset-bottom) + 20px);
    flex: 1;
  }

  .sheetContent h2 {
    font-size: 14px;
    font-weight: 700;
    margin: 0 0 8px 0;
    color: var(--text);
  }

  .sheetContent input[type="text"],
  .sheetContent textarea {
    font-size: 16px; /* prevent iOS zoom */
  }

  .sheetSpeedRow {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 12px;
  }

  .sheetSpeedLabel {
    font-size: 12px;
    color: var(--muted);
    min-width: 44px;
  }

  /* ── TTS mini bar ── */
  #ttsMiniBar {
    position: fixed;
    top: calc(env(safe-area-inset-top) + 58px);
    left: 12px;
    right: 12px;
    background: rgba(10,12,20,0.92);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid rgba(77,163,255,0.3);
    border-radius: 14px;
    padding: 10px 14px;
    z-index: 300;
    display: none;
    align-items: center;
    gap: 10px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
  }

  #ttsMiniBar.ttsActive { display: flex; }

  .ttsBarLabel {
    flex: 1;
    font-size: 12px;
    color: #9aa4b2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .ttsBarBtn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: none;
    background: rgba(255,255,255,0.08);
    color: #e9edf3;
    font-size: 15px;
    display: grid;
    place-items: center;
    padding: 0;
    flex-shrink: 0;
  }

  .ttsBarBtnPrimary { background: #4da3ff; color: #fff; }
}

/* =========================
   WELCOME OVERLAY
   ========================= */
#welcomeOverlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  padding: 18px;
}

#welcomeOverlay[hidden] { display: none; }

#welcomeModal { width: min(480px, 100%); position: relative; }

.wSplash {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  animation: wSplashIn 0.5s ease forwards;
}

.wSplashLogo {
  width: 120px;
  height: 120px;
  border-radius: 26px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
}

.wSplashName { font-size: 28px; font-weight: 800; color: #fff; letter-spacing: 0.5px; }

@keyframes wSplashIn {
  from { opacity: 0; transform: scale(0.85); }
  to   { opacity: 1; transform: scale(1); }
}

.wSplash.wFadeOut { animation: wSplashOut 0.4s ease forwards; }

@keyframes wSplashOut {
  from { opacity: 1; transform: scale(1); }
  to   { opacity: 0; transform: scale(1.05); }
}

.wCard {
  display: none;
  border-radius: 20px;
  padding: 22px;
  position: relative;
  background: #1a1f2e;
  border: 1px solid rgba(255,255,255,0.14);
  box-shadow: 0 30px 80px rgba(0,0,0,0.6);
  color: #e9edf3;
}

.wCard.wVisible {
  display: block;
  animation: wCardIn 0.45s ease forwards;
}

@keyframes wCardIn {
  from { opacity: 0; transform: translateY(18px); }
  to   { opacity: 1; transform: translateY(0); }
}

.wClose {
  position: absolute; top: 12px; right: 12px;
  width: 34px; height: 34px; border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.08);
  color: #e9edf3; font-size: 16px;
  display: grid; place-items: center; cursor: pointer; padding: 0;
}

.wLogoRow { display: flex; align-items: center; gap: 14px; margin-bottom: 16px; }
.wCardLogo { width: 64px; height: 64px; border-radius: 14px; flex-shrink: 0; }
.wCardTitle { font-size: 20px; font-weight: 800; color: #fff; }
.wCardSub { font-size: 13px; color: #9aa4b2; margin-top: 3px; }
.wLead { font-size: 14px; line-height: 1.55; color: #c8d0dc; margin: 0 0 16px 0; }

.wFeatures { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }

.wFeature {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 10px 12px; border-radius: 12px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.08);
  font-size: 13px; color: #e9edf3;
}

.wFeaturePaid {
  border-color: rgba(77,163,255,0.25);
  background: rgba(77,163,255,0.06);
}

.wFeatureIcon { font-size: 20px; line-height: 1; flex-shrink: 0; margin-top: 1px; }
.wFeatureDesc { font-size: 12px; color: #9aa4b2; margin-top: 2px; line-height: 1.4; }

.wBadge {
  font-size: 10px; font-weight: 700; padding: 2px 6px;
  border-radius: 6px; background: #4da3ff; color: #fff;
  vertical-align: middle; margin-left: 4px; letter-spacing: 0.5px;
}

.wSafety {
  font-size: 12px; color: #f59e0b;
  background: rgba(245,158,11,0.10);
  border: 1px solid rgba(245,158,11,0.22);
  border-radius: 10px; padding: 10px 12px;
  line-height: 1.45; margin-bottom: 16px;
}

.wContinueBtn {
  width: 100%; padding: 14px; border-radius: 14px;
  font-size: 16px; font-weight: 800;
  background: #4da3ff; border: none; color: #fff;
  cursor: pointer; letter-spacing: 0.3px;
}

/* =========================
   PAYWALL OVERLAY
   ========================= */
#paywallOverlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.72);
  display: flex; align-items: center; justify-content: center;
  z-index: 10000; padding: 18px;
}

#paywallOverlay[hidden] { display: none; }

#paywallModal {
  width: min(440px, 100%);
  border-radius: 20px; padding: 22px; position: relative;
  background: #1a1f2e;
  border: 1px solid rgba(255,255,255,0.14);
  box-shadow: 0 30px 80px rgba(0,0,0,0.6);
  color: #e9edf3;
}

.paywallClose {
  position: absolute; top: 12px; right: 12px;
  width: 34px; height: 34px; border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.08);
  color: #e9edf3; font-size: 16px;
  display: grid; place-items: center; cursor: pointer; padding: 0;
}

.pwLogoRow { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.pwLogo { width: 56px; height: 56px; border-radius: 12px; flex-shrink: 0; }
.pwTitle { font-size: 18px; font-weight: 800; color: #fff; margin: 0; }
.pwSub { font-size: 12px; color: #9aa4b2; margin-top: 3px; }
.pwMsg { font-size: 13px; color: #c8d0dc; margin: 0 0 14px 0; line-height: 1.5; }

.pwFeatures { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.pwFeature {
  font-size: 13px; color: #e9edf3; padding: 8px 12px;
  border-radius: 10px; background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.08);
}

.pwPrice { font-size: 36px; font-weight: 900; color: #fff; text-align: center; letter-spacing: -1px; }
.pwPriceNote { font-size: 12px; color: #9aa4b2; text-align: center; margin-bottom: 18px; }

.pwActions { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }

.pwBuyBtn {
  display: block; width: 100%; padding: 14px; border-radius: 14px;
  font-size: 16px; font-weight: 800;
  background: #4da3ff; border: none; color: #fff;
  text-align: center; text-decoration: none; cursor: pointer;
}

.pwAlreadyBtn {
  background: transparent; border: 1px solid rgba(255,255,255,0.18);
  color: #9aa4b2; font-size: 13px; padding: 10px;
  border-radius: 12px; cursor: pointer; width: 100%;
}

.pwUnlockRow {
  display: flex; flex-direction: column; gap: 8px;
  margin-bottom: 12px; padding: 12px; border-radius: 12px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.10);
}

.pwUnlockLabel { font-size: 12px; color: #9aa4b2; font-weight: 600; }

.pwCodeInput {
  padding: 10px 12px; border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.18);
  background: rgba(255,255,255,0.06);
  color: #e9edf3; font-size: 15px; letter-spacing: 1px;
  width: 100%;
}

.pwUnlockBtn {
  padding: 10px; border-radius: 10px;
  background: #4da3ff; border: none; color: #fff;
  font-weight: 700; font-size: 14px; cursor: pointer; width: 100%;
}

.pwUnlockHint { font-size: 11px; color: #6b7280; line-height: 1.4; }
.pwFooter { font-size: 11px; color: #6b7280; text-align: center; line-height: 1.4; }