/* =========================================================================
   theme.css — design-token system, dark mode, shell components, state polish.
   Loaded AFTER style.css so token-based rules override the legacy hardcoded
   light values. Light tokens equal the original look (no visual regression);
   dark tokens remap the same surfaces.
   ========================================================================= */

:root {
  /* color — surfaces */
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --surface-3: #f7f8fa;
  /* color — text */
  --text: #1a1a2e;
  --text-2: #555b6b;
  --text-3: #8a909c;
  --muted: #6b7280;
  /* color — lines */
  --border: #e3e6ec;
  --border-2: #d8dde6;
  /* color — brand & accents */
  --accent: #e63946;
  --accent-600: #cf2f3c;
  --brand: #4a72ff;
  --brand-soft: #eef3ff;
  --ok: #15935a;
  --err: #dc2626;
  /* typography */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  /* radius */
  --r-sm: 8px;
  --r: 12px;
  --r-lg: 16px;
  /* spacing scale */
  --s-1: 4px; --s-2: 8px; --s-3: 12px; --s-4: 16px; --s-5: 24px; --s-6: 32px;
  /* shadows */
  --sh-1: 0 1px 6px rgba(16, 24, 40, 0.06);
  --sh-2: 0 8px 24px rgba(16, 24, 40, 0.10);
  --sh-3: 0 12px 40px rgba(16, 24, 40, 0.22);
  /* motion */
  --t: 0.15s ease;
}

html[data-theme="dark"] {
  --bg: #0e1320;
  --surface: #1a2031;
  --surface-2: #232b3e;
  --surface-3: #1f2738;
  --text: #e7eaf1;
  --text-2: #aeb6c7;
  --text-3: #7f889c;
  --muted: #8a93a6;
  --border: #2b3346;
  --border-2: #3a4459;
  --brand-soft: #1e2a48;
  --err: #f87171;
  --sh-1: 0 1px 6px rgba(0, 0, 0, 0.45);
  --sh-2: 0 8px 24px rgba(0, 0, 0, 0.5);
  --sh-3: 0 16px 48px rgba(0, 0, 0, 0.6);
}

/* ---- base: route core surfaces through tokens (both themes) ---- */
html { color-scheme: light; background: var(--bg); }
html[data-theme="dark"] { color-scheme: dark; }
html body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  transition: background var(--t), color var(--t);
}

.header { background: var(--surface); box-shadow: var(--sh-1); }
.nav a { color: var(--text-2); transition: color var(--t); position: relative; padding-bottom: 2px; }
.nav a::after {
  content: "";
  position: absolute; left: 0; bottom: -3px;
  width: 0; height: 2px;
  background: var(--accent);
  transition: width var(--t);
}
.nav a:hover { color: var(--accent); }
.nav a:hover::after { width: 100%; }
.hero p { color: var(--text-2); }
.cat-title { color: var(--muted); }
.tool { color: var(--text); transition: background var(--t), transform var(--t); }
.tool:hover { background: var(--surface-2); }
.footer { color: var(--text-3); }
.logo-owner { color: var(--text-3); }
.footer-founder { color: var(--text-2); }
.footer-founder a { color: var(--accent); }

.modal-box { background: var(--surface); color: var(--text); box-shadow: var(--sh-3); }
.modal-box p { color: var(--text-2); }
.modal-close { color: var(--text-3); }
.file-card { background: var(--surface); border-color: var(--border); }
.card-name { color: var(--text); }
.card-meta { color: var(--text-3); }
.thumb { background: var(--surface-3); }
.add-tile { border-color: var(--border-2); color: var(--brand); }
.add-tile:hover { background: var(--brand-soft); border-color: var(--brand); }

.btn-light { background: var(--surface-2); color: var(--text); transition: background var(--t), transform var(--t); }
.btn-light:hover { background: var(--border); }
.btn { background: var(--accent); transition: background var(--t), transform var(--t); }
.btn:hover { background: var(--accent-600); }
.btn:active, .btn-light:active { transform: translateY(1px); }
.btn:disabled { background: var(--border-2); }

.text-input, .anno-sel { background: var(--surface); border-color: var(--border-2); color: var(--text); }
.text-input:focus, .anno-sel:focus { border-color: var(--brand); }
.lbl { color: var(--text-2); }
.status { color: var(--text-2); }
.soon { color: var(--text-2); }

.anno-tool { background: var(--surface); border-color: var(--border-2); color: var(--text); transition: background var(--t), border-color var(--t); }
.anno-tool.active { background: var(--brand-soft); border-color: var(--brand); }
.anno-hint { color: var(--text-3); }

.ed2-scroll { background: var(--surface-2); }
.ed2-form, .ai-log, .ai-out { background: var(--surface-2); }
.ai-msg.bot { background: var(--surface); border-color: var(--border); }
.reader-pages { background: var(--surface-2); }
.reader-bar { background: var(--surface); border-bottom-color: var(--border); }
.sign-stage, .file-card, .page-tile { border-color: var(--border); }
.sign-pad { background: var(--surface-3); }
.page-tile { background: var(--surface); }
.page-num { color: var(--text-3); }

/* ---- accessibility & global polish ---- */
:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  border-radius: 4px;
}
button { font-family: var(--font); }

/* nicer scrollbars on scroll regions */
.file-grid, .ed2-scroll, .reader-pages, .ai-log, .ai-out, .modal-box, .ed2-form, .page-grid {
  scrollbar-width: thin;
  scrollbar-color: var(--border-2) transparent;
}
.file-grid::-webkit-scrollbar, .ed2-scroll::-webkit-scrollbar,
.reader-pages::-webkit-scrollbar, .modal-box::-webkit-scrollbar { width: 10px; height: 10px; }
.file-grid::-webkit-scrollbar-thumb, .ed2-scroll::-webkit-scrollbar-thumb,
.reader-pages::-webkit-scrollbar-thumb, .modal-box::-webkit-scrollbar-thumb {
  background: var(--border-2); border-radius: 6px;
}

/* ===== icon system (SVG sprite via <use>, replaces emoji icons) ===== */
.icon {
  display: inline-block;
  vertical-align: -3px;
  flex-shrink: 0;
}
.logo-ic { color: var(--accent); }

/* ===== app shell: header actions, theme button, search ===== */
.header-actions { display: flex; align-items: center; gap: var(--s-4); }
.theme-btn {
  border: 1px solid var(--border-2);
  background: var(--surface);
  color: var(--text-2);
  width: 38px; height: 38px;
  border-radius: 50%;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background var(--t), transform var(--t);
}
.theme-btn:hover { background: var(--surface-2); transform: rotate(-15deg); }
.theme-btn .icon { vertical-align: 0; }
html:not([data-theme="dark"]) .theme-btn .icon-moon-ic { display: none; }
html[data-theme="dark"] .theme-btn .icon-sun-ic { display: none; }

/* ===== account widget (Supabase Auth) ===== */
.auth-widget { position: relative; }
#authBtn.is-authed { border-color: var(--brand); color: var(--brand); }
#authBtn.is-authed .icon { display: none; }
.auth-btn-label { font-weight: 700; font-size: 0.85rem; }
.auth-btn-label.hidden { display: none; }
.auth-profile { display: flex; align-items: center; gap: var(--s-3); }
.auth-avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.auth-name { font-size: 0.9rem; font-weight: 600; color: var(--text); margin: 0; }
.auth-panel {
  position: absolute;
  top: 48px; right: 0;
  width: 240px;
  background: var(--surface);
  border: 1px solid var(--border-2);
  border-radius: var(--r);
  box-shadow: var(--sh-2);
  padding: var(--s-4);
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
  z-index: 100;
}
.auth-panel.hidden { display: none; }
.auth-panel .text-input { width: 100%; box-sizing: border-box; }
.auth-actions { display: flex; gap: var(--s-2); }
.auth-actions .btn, .auth-actions .btn-light { flex: 1; }
.auth-email {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  word-break: break-all;
  margin: 0;
}
.auth-panel .status { margin: 0; font-size: 0.82rem; }

/* ===== app gate: full-page sign-in wall shown when signed out ===== */
#appContent.hidden { display: none; }
.auth-gate.hidden { display: none; }
.auth-gate {
  min-height: 70vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--s-6) var(--s-4);
}
.auth-gate-box {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border-2);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-2);
  padding: var(--s-6);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
}
.auth-gate-box .icon { margin: 0 auto; color: var(--accent); }
.auth-gate-box h1 { font-size: 1.4rem; margin: 0; }
.auth-gate-box p { color: var(--text-2); margin: 0 0 var(--s-2); font-size: 0.9rem; }
.auth-gate-box .text-input { width: 100%; box-sizing: border-box; }
.auth-gate-box .status { margin: 0; font-size: 0.82rem; }

.search {
  max-width: 460px;
  margin: 22px auto 0;
  position: relative;
}
.search input {
  width: 100%;
  padding: 12px 16px 12px 42px;
  border: 1px solid var(--border-2);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  box-shadow: var(--sh-1);
  transition: border-color var(--t), box-shadow var(--t);
}
.search input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.search-ic {
  position: absolute;
  left: 15px; top: 50%; transform: translateY(-50%);
  color: var(--text-3);
}
.tool.hidden, .cat.hidden { display: none; }

/* ===== hero trust strip ===== */
.trust-strip {
  list-style: none;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px 20px;
  margin: 18px 0 0;
  padding: 0;
}
.trust-strip li {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-2);
}
.trust-strip .icon { color: var(--brand); }
.no-results {
  text-align: center;
  color: var(--text-3);
  padding: 40px 20px;
  font-size: 1rem;
}

/* ===== state polish: loading / success / error ===== */
.spinner {
  display: inline-block;
  width: 14px; height: 14px;
  border: 2px solid var(--border-2);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -2px;
  margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.status.is-ok { color: var(--ok); font-weight: 600; }
.status.is-err { color: var(--err); font-weight: 600; }
.status.is-busy { color: var(--text-2); }

/* toast (global save/success confirmation) */
#toastHost {
  position: fixed;
  left: 50%; bottom: 28px; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px;
  z-index: 200; pointer-events: none;
}
.toast {
  background: var(--text);
  color: var(--surface);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: var(--sh-2);
  opacity: 0; transform: translateY(10px);
  animation: toastIn 0.25s ease forwards;
}
.toast.ok { background: var(--ok); color: #fff; }
.toast.err { background: var(--err); color: #fff; }
.toast.out { animation: toastOut 0.3s ease forwards; }
@keyframes toastIn { to { opacity: 1; transform: translateY(0); } }
@keyframes toastOut { to { opacity: 0; transform: translateY(10px); } }

/* hover lift on interactive tiles */
.tool:hover { transform: translateX(2px); }
.conv-btn { transition: background var(--t), transform var(--t); }
.conv-btn:hover { transform: translateY(-2px); box-shadow: var(--sh-1); }

/* reduce motion preference */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ===== product: filters + tool card grid ===== */
.filters {
  max-width: 1120px;
  margin: 28px auto 0;
  padding: 0 24px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}
.pill {
  border: 1px solid var(--border-2);
  background: var(--surface);
  color: var(--text-2);
  padding: 7px 16px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--t), color var(--t), border-color var(--t);
}
.pill:hover { background: var(--surface-2); color: var(--text); }
.pill.active { background: var(--text); color: var(--surface); border-color: var(--text); }

/* override the old column layout — now a stack of card sections */
.tools {
  display: block;
  max-width: 1120px;
  margin: 8px auto 0;
  padding: 16px 24px 8px;
}
.tsection { margin: 30px 0; }
.tsection-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 14px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.tsection-title {
  font-size: 1.12rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.01em;
}
.tsection-blurb { font-size: 0.85rem; color: var(--text-3); }
.featured .tsection-title::before { content: "★ "; color: var(--accent); }

.tgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(232px, 1fr));
  gap: 14px;
}
.tcard {
  display: flex;
  align-items: center;
  gap: 14px;
  text-align: left;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  cursor: pointer;
  transition: transform var(--t), box-shadow var(--t), border-color var(--t);
}
.tcard:hover {
  transform: translateY(-3px);
  box-shadow: var(--sh-2);
  border-color: var(--brand);
}
.tcard-ic {
  width: 44px; height: 44px;
  font-size: 1.35rem;
  border-radius: 11px;
  flex-shrink: 0;
}
.tcard-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.tcard-name { font-weight: 650; font-size: 0.97rem; color: var(--text); }
.tcard-desc {
  font-size: 0.82rem; color: var(--text-3);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tcard.hidden, .tsection.hidden { display: none; }

/* ===== responsive ===== */
@media (max-width: 720px) {
  .header { padding: 12px 16px; }
  .header-actions { gap: var(--s-3); }
  .nav a { margin-left: 14px; }
  .hero { padding: 36px 16px 24px; }
  .hero h1 { font-size: 1.6rem; }
  .trust-strip { gap: 6px 14px; font-size: 0.8rem; }
  .search { margin-top: 16px; }
  .tools { padding: 16px; }
  .tgrid { grid-template-columns: 1fr; gap: 10px; }
  .filters { padding: 0 16px; justify-content: flex-start; overflow-x: auto; flex-wrap: nowrap; }
  .pill { flex: 0 0 auto; }
  .tsection { margin: 22px 0; }
  .modal-box { padding: 20px; }
}
@media (max-width: 480px) {
  .nav { display: none; } /* keep header clean on phones; tools are searchable */
}
