/* Base: reset, app shell, and the shared component classes every view uses.
   Views add their own files (library.css, reader.css, annotations.css) and should
   compose these classes rather than restyle buttons/menus/dialogs. */

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

html { height: 100%; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: var(--text-md);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

h1, h2, h3, h4, p, figure, ul, ol { margin: 0; }
ul, ol { padding: 0; list-style: none; }
img, svg, canvas { display: block; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; }
[hidden] { display: none !important; }

::selection { background: color-mix(in srgb, var(--accent) 60%, transparent); color: inherit; }

:focus { outline: none; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}

/* Thin, quiet scrollbars */
* { scrollbar-width: thin; scrollbar-color: var(--line-strong) transparent; }

/* ---------- Type helpers ---------- */
.display { font-family: var(--font-display); font-weight: 560; letter-spacing: -0.012em; line-height: 1.12; }
.eyebrow { font-size: var(--text-xs); font-weight: 650; letter-spacing: .09em; text-transform: uppercase; color: var(--ink-3); }
.muted { color: var(--ink-2); }
.faint { color: var(--ink-3); }
.num { font-variant-numeric: tabular-nums; }

/* The signature: a highlighter stroke behind text. Wrap text in <span class="mark">. */
.mark {
  background-image: linear-gradient(var(--mark), var(--mark));
  background-repeat: no-repeat;
  background-size: 100% 46%;
  background-position: 0 88%;
  border-radius: 2px;
  padding-inline: .12em;
  margin-inline: -.12em;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}

/* ---------- App shell ---------- */
#app {
  display: grid;
  /* The rail grows by the left safe-area inset (a phone held sideways), so its items clear the sensor housing. */
  grid-template-columns: calc(var(--rail-w) + env(safe-area-inset-left, 0px)) minmax(0, 1fr);
  height: 100dvh;
}
#app[data-route="reader"] { grid-template-columns: minmax(0, 1fr); }
#app[data-route="reader"] #rail { display: none; }

#view {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
}
#app[data-route="reader"] #view { overflow: hidden; }
/* The reader pads its own bars; every other screen keeps clear of the right safe-area inset here. */
#app:not([data-route="reader"]) #view { padding-right: env(safe-area-inset-right, 0px); }
/* Route changes move focus to the new screen's heading (src/main.js) so it is announced; it is not a control. */
#view:focus-visible, #view :is(h1, h2):focus-visible { outline: none; }

#rail {
  z-index: var(--z-rail);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--s-1);
  padding: var(--s-4) 0 calc(var(--s-3) + env(safe-area-inset-bottom)) env(safe-area-inset-left, 0px);
  border-right: 1px solid var(--line);
  background: var(--surface);
}
.rail__brand {
  display: grid; place-items: center;
  width: 40px; height: 40px; margin-bottom: var(--s-4);
  color: var(--ink);
}
.rail__spacer { flex: 1; }
.rail__item {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  width: 64px; padding: 9px 0 7px;
  border: 0; border-radius: var(--r-md); background: none;
  color: var(--ink-2); font-size: 11px; font-weight: 600; letter-spacing: .01em;
  text-decoration: none;
  transition: background var(--dur-1), color var(--dur-1);
}
.rail__item:hover { background: var(--hover); color: var(--ink); }
.rail__item[aria-current="page"] { color: var(--ink); }
.rail__item[aria-current="page"] .rail__label {
  background-image: linear-gradient(var(--mark), var(--mark));
  background-repeat: no-repeat; background-size: 100% 52%; background-position: 0 90%;
  padding-inline: 3px; border-radius: 2px;
}

@media (max-width: 720px) {
  #app { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  #rail {
    order: 2; flex-direction: row; justify-content: space-around; align-items: stretch;
    padding: 4px max(var(--s-2), env(safe-area-inset-right, 0px)) calc(4px + env(safe-area-inset-bottom)) max(var(--s-2), env(safe-area-inset-left, 0px));
    border-right: 0; border-top: 1px solid var(--line);
    min-height: var(--tabbar-h);
  }
  #app:not([data-route="reader"]) #view { padding-left: env(safe-area-inset-left, 0px); }
  .rail__brand, .rail__spacer { display: none; }
  .rail__item { flex: 1; max-width: 96px; justify-content: center; }
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  min-height: 38px; padding: 0 var(--s-4);
  border: 1px solid var(--line-strong); border-radius: var(--r-md);
  background: var(--raised); color: var(--ink);
  font-weight: 600; font-size: var(--text-md); white-space: nowrap; text-decoration: none;
  transition: background var(--dur-1), border-color var(--dur-1), transform var(--dur-1), box-shadow var(--dur-1);
  user-select: none;
}
.btn:hover { background: color-mix(in srgb, var(--raised) 88%, var(--ink)); }
.btn:active { transform: translateY(1px); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .5; pointer-events: none; }
.btn svg { flex: none; }

.btn--primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent-edge); box-shadow: var(--shadow-1); }
.btn--primary:hover { background: color-mix(in srgb, var(--accent) 88%, #fff); }
.btn--ghost { background: transparent; border-color: transparent; }
.btn--ghost:hover { background: var(--hover); }
.btn--danger { background: var(--danger); border-color: transparent; color: #fff; }
:root[data-theme="dark"] .btn--danger { color: #2B0704; }
.btn--danger:hover { background: color-mix(in srgb, var(--danger) 88%, #000); }
.btn--sm { min-height: 30px; padding: 0 var(--s-3); font-size: var(--text-sm); border-radius: var(--r-sm); }
.btn--lg { min-height: 46px; padding: 0 var(--s-6); font-size: 1rem; }
.btn--block { width: 100%; }

.icon-btn {
  display: inline-grid; place-items: center; flex: none;
  width: 38px; height: 38px; padding: 0;
  border: 0; border-radius: var(--r-md); background: transparent; color: var(--ink-2);
  transition: background var(--dur-1), color var(--dur-1);
}
.icon-btn:hover { background: var(--hover); color: var(--ink); }
.icon-btn:active { background: var(--pressed); }
.icon-btn[aria-pressed="true"], .icon-btn.is-active { color: var(--ink); background: var(--pressed); }
.icon-btn:disabled { opacity: .4; pointer-events: none; }
.icon-btn--sm { width: 30px; height: 30px; border-radius: var(--r-sm); }

/* ---------- Fields ---------- */
.field { display: grid; gap: 6px; }
.field__label { font-size: var(--text-sm); font-weight: 600; color: var(--ink-2); }
.field__hint { font-size: var(--text-xs); color: var(--ink-3); }

.input, .textarea, .select {
  width: 100%; min-height: 38px; padding: 8px 12px;
  border: 1px solid var(--line-strong); border-radius: var(--r-md);
  background: var(--raised); color: var(--ink);
  transition: border-color var(--dur-1), box-shadow var(--dur-1);
}
.input::placeholder, .textarea::placeholder { color: var(--ink-3); }
.input:focus-visible, .textarea:focus-visible, .select:focus-visible {
  outline: none; border-color: var(--focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 22%, transparent);
}
.textarea { min-height: 96px; resize: vertical; line-height: 1.5; }

.search {
  position: relative; display: flex; align-items: center;
}
.search > svg { position: absolute; left: 11px; color: var(--ink-3); pointer-events: none; }
.search > .input { padding-left: 36px; }

/* Segmented control: <div class="seg" role="radiogroup"><button role="radio" aria-checked>…</button></div> */
.seg {
  display: inline-flex; padding: 3px; gap: 2px;
  border-radius: var(--r-md); background: var(--surface-2);
}
.seg > button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 30px; padding: 0 12px;
  border: 0; border-radius: 7px; background: transparent;
  color: var(--ink-2); font-size: var(--text-sm); font-weight: 600;
  transition: background var(--dur-1), color var(--dur-1);
}
.seg > button:hover { color: var(--ink); }
.seg > button[aria-checked="true"], .seg > button[aria-pressed="true"] {
  background: var(--raised); color: var(--ink); box-shadow: var(--shadow-1);
}

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 28px; padding: 0 11px;
  border: 1px solid var(--line-strong); border-radius: var(--r-pill);
  background: transparent; color: var(--ink-2);
  font-size: var(--text-sm); font-weight: 600; white-space: nowrap;
  transition: background var(--dur-1), color var(--dur-1), border-color var(--dur-1);
}
button.chip:hover { background: var(--hover); color: var(--ink); }
/* Chosen: ink outline and the highlighter stroke under the words, as on the rail and the tabs. */
.chip[aria-pressed="true"], .chip.is-active {
  border-color: var(--ink); color: var(--ink);
  background: linear-gradient(var(--mark), var(--mark)) 50% 76% / calc(100% - 18px) 38% no-repeat;
}
button.chip[aria-pressed="true"]:hover, button.chip.is-active:hover { background-color: var(--hover); }

.kbd {
  display: inline-block; min-width: 1.6em; padding: 1px 5px;
  border: 1px solid var(--line-strong); border-bottom-width: 2px; border-radius: 5px;
  background: var(--surface); color: var(--ink-2);
  font-family: var(--font-mono); font-size: 0.6875rem; text-align: center;
}

/* Switch: <button class="switch" role="switch" aria-checked="true|false"></button> */
.switch {
  position: relative; flex: none; width: 40px; height: 24px; padding: 0;
  border: 0; border-radius: var(--r-pill); background: var(--line-strong);
  transition: background var(--dur-2);
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  border-radius: 50%; background: var(--raised); box-shadow: var(--shadow-1);
  transition: transform var(--dur-2) var(--ease);
}
.switch[aria-checked="true"] { background: var(--brand); }
.switch[aria-checked="true"]::after { transform: translateX(16px); }

/* Progress: a highlighter stroke filling a hairline. Set --value (0..1). */
.progress {
  position: relative; height: 5px; border-radius: 3px; background: var(--line); overflow: hidden;
}
.progress::after {
  content: ""; position: absolute; inset: 0; transform-origin: left;
  transform: scaleX(var(--value, 0)); background: var(--accent); border-radius: inherit;
  transition: transform var(--dur-3) var(--ease);
}
:root[data-theme="light"] .progress { background: var(--line-strong); }

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

/* ---------- Popovers & menus (created by src/lib/ui.js) ---------- */
.popover {
  position: fixed; inset: auto; margin: 0; padding: 0; border: 0;
  z-index: var(--z-popover);
  max-width: calc(100vw - 16px); max-height: calc(100dvh - 16px);
  overflow: auto;
  border-radius: var(--r-md);
  background: var(--raised); color: var(--ink);
  box-shadow: var(--shadow-3), 0 0 0 1px var(--line);
  animation: pop-in var(--dur-1) var(--ease);
}
@keyframes pop-in { from { opacity: 0; transform: translateY(-3px) scale(.98); } }

.menu { min-width: 200px; padding: 5px; }
.menu__item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  min-height: 36px; padding: 0 10px;
  border: 0; border-radius: 7px; background: none; color: var(--ink);
  font-size: var(--text-md); text-align: left; white-space: nowrap;
}
.menu__item svg { flex: none; color: var(--ink-2); }
.menu__item:hover, .menu__item:focus-visible { background: var(--hover); }
.menu__item:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
.menu__item[aria-disabled="true"] { opacity: .45; pointer-events: none; }
.menu__item--danger, .menu__item--danger svg { color: var(--danger); }
.menu__item--danger:hover, .menu__item--danger:focus-visible { background: var(--danger-soft); }
.menu__label { flex: 1; }
.menu__check { margin-left: auto; color: var(--brand) !important; }
.menu__hint { margin-left: auto; padding-left: 16px; color: var(--ink-3); font-size: var(--text-xs); }
.menu__sep { height: 1px; margin: 5px 6px; background: var(--line); }
.menu__heading { padding: 8px 10px 4px; font-size: var(--text-xs); font-weight: 650; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-3); }

/* ---------- Dialogs (created by src/lib/ui.js) ---------- */
dialog.dialog {
  --dialog-room: calc(100vw - 32px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
  width: min(480px, var(--dialog-room)); max-height: min(720px, calc(100dvh - 32px));
  padding: 0; border: 0; border-radius: var(--r-lg);
  background: var(--raised); color: var(--ink);
  box-shadow: var(--shadow-3), 0 0 0 1px var(--line);
  overflow: hidden;
}
dialog.dialog[open] { display: flex; flex-direction: column; animation: dialog-in var(--dur-2) var(--ease); }
dialog.dialog::backdrop { background: var(--scrim); animation: fade-in var(--dur-2) ease-out; }
dialog.dialog--wide { width: min(680px, var(--dialog-room)); }
@keyframes dialog-in { from { opacity: 0; transform: translateY(10px) scale(.985); } }
@keyframes fade-in { from { opacity: 0; } }

.dialog__header {
  display: flex; align-items: center; gap: var(--s-3);
  padding: var(--s-5) var(--s-5) var(--s-2) var(--s-6);
}
.dialog__title { flex: 1; min-width: 0; overflow-wrap: anywhere; font-family: var(--font-display); font-size: var(--text-xl); font-weight: 560; letter-spacing: -0.01em; line-height: 1.2; }
/* A long word (a file-name title, a URL) breaks where it must; it never makes the dialog scroll sideways. */
.dialog__body { flex: 1; min-height: 0; overflow: auto; padding: var(--s-2) var(--s-6) var(--s-5); overflow-wrap: anywhere; }
.dialog__body > p { color: var(--ink-2); }
.dialog__footer {
  display: flex; justify-content: flex-end; gap: var(--s-2); flex-wrap: wrap;
  padding: var(--s-3) var(--s-6) var(--s-5);
}

@media (max-width: 640px) {
  dialog.dialog {
    width: 100vw; max-width: 100vw; max-height: 92dvh;
    margin: auto 0 0; border-radius: var(--r-lg) var(--r-lg) 0 0;
    padding: 0 env(safe-area-inset-right, 0px) env(safe-area-inset-bottom) env(safe-area-inset-left, 0px);
  }
  dialog.dialog[open] { animation-name: sheet-in; }
  @keyframes sheet-in { from { transform: translateY(40px); opacity: 0; } }
  .dialog__footer .btn { flex: 1; }
}

/* ---------- Toasts ---------- */
.toasts {
  position: fixed; inset: auto 0 0 0; margin: 0;
  padding: 0 max(16px, env(safe-area-inset-right, 0px)) calc(20px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left, 0px));
  border: 0; background: none; overflow: visible;
  width: 100%; max-width: none;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  pointer-events: none; z-index: var(--z-toast);
}
.toasts:not(:popover-open):not(.toasts--fallback) { display: none; }
@media (max-width: 720px) { #app:not([data-route="reader"]) ~ .toasts { padding-bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom)); } }
/* In the reader the bottom bar holds the scrubber and the page number: toasts stay above it while it shows. */
#app[data-route="reader"]:not(:has(.reader--chrome-hidden)) ~ .toasts { padding-bottom: calc(var(--reader-bottombar-h) + 12px + env(safe-area-inset-bottom)); }
.toast {
  display: flex; align-items: center; gap: 12px;
  max-width: min(520px, 100%); padding: 10px 10px 10px 16px;
  border-radius: var(--r-md);
  background: var(--ink); color: var(--bg);
  font-size: var(--text-md); font-weight: 500;
  box-shadow: var(--shadow-3);
  pointer-events: auto;
  animation: toast-in var(--dur-2) var(--ease);
}
.toast--leaving { animation: toast-out var(--dur-2) var(--ease-in) forwards; }
/* The message gives way, never the action: a long title wraps (three lines at most) and Undo stays on screen. */
.toast__msg {
  flex: 1; min-width: 0; padding-right: 6px; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; line-clamp: 3; overflow: hidden;
}
.toast__action {
  flex: none; min-height: 30px; padding: 0 12px; border: 0; border-radius: 7px;
  background: var(--accent); color: var(--accent-ink); font-weight: 650; font-size: var(--text-sm);
}
.toast--error { background: var(--danger); color: #fff; }
:root[data-theme="dark"] .toast--error { color: #2B0704; }
@keyframes toast-in { from { opacity: 0; transform: translateY(12px); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px); } }

/* ---------- Empty states ---------- */
.empty-state {
  display: grid; justify-items: center; gap: var(--s-3);
  max-width: 420px; margin: 0 auto; padding: var(--s-16) var(--s-6);
  text-align: center; color: var(--ink-2);
}
.empty-state__title { font-family: var(--font-display); font-size: var(--text-xl); font-weight: 560; color: var(--ink); }

/* ---------- Boot ---------- */
.boot {
  position: absolute; inset: 0; display: grid; place-items: center; color: var(--ink-3);
}

/* ---------- Touch ---------- */
/* A fingertip needs about 44px. On touch screens the shared controls grow to that; the few that
   must stay small to keep their look (the switch) get a larger hit area instead. Views with their
   own small controls do the same in their own stylesheet. */
@media (pointer: coarse) {
  .btn, .input, .select { min-height: 44px; }
  .btn--sm { min-height: 40px; }
  .btn--lg { min-height: 48px; }
  .icon-btn { width: 44px; height: 44px; }
  .icon-btn--sm { width: 40px; height: 40px; }
  .seg > button, .chip, .toast__action { min-height: 40px; }
  .menu__item { min-height: 44px; }
  .switch::before { content: ""; position: absolute; inset: -10px -4px; }
}
/* The narrowest phones: rows of icon buttons (the reader's top bar) leave room for a title at 40px. */
@media (pointer: coarse) and (max-width: 380px) {
  .icon-btn { width: 40px; height: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after, ::backdrop {
    animation-duration: .01ms !important; animation-iteration-count: 1 !important;
    transition-duration: .01ms !important; scroll-behavior: auto !important;
  }
}
