/* hygg-pwa — touch reader styling. Optimized for iPad Pro 10.5"
   (1112×834 CSS px) but fluid across phones/tablets. */

:root {
  --bar-h: 52px;
  --reader-fg: #e8e6e0;
  --reader-bg: #0b0b0b;
  --muted: #8a8a8a;
  --accent: #c8a26a;
  --card-bg: #161616;
  --border: #262626;
  --danger: #ff867a;
  --danger-ink: #180302;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas,
    monospace;
}

/* Themes (class on <html>). */
.theme-dark {
  --reader-fg: #e8e6e0;
  --reader-bg: #0b0b0b;
  --muted: #8a8a8a;
  --card-bg: #161616;
  --border: #262626;
  --danger: #ff867a;
  --danger-ink: #180302;
}
.theme-light {
  --reader-fg: #1b1b1b;
  --reader-bg: #fbfbf9;
  --muted: #6b6b6b;
  --card-bg: #ffffff;
  --border: #e2e2dc;
  --danger: #b42318;
  --danger-ink: #ffffff;
}
.theme-sepia {
  --reader-fg: #4a3f33;
  --reader-bg: #f4ecd8;
  --muted: #8a795f;
  --card-bg: #efe6cf;
  --border: #ddcfae;
  --danger: #a4402a;
  --danger-ink: #f7efdd;
}

* {
  box-sizing: border-box;
}

/* The viewport is zoomable (maximum-scale=5), so WebKit holds every tap
   ~350ms to disambiguate a double-tap zoom. Buttons and links are never
   zoom targets — drop the delay so taps (e.g. the jump toast) act
   immediately. */
button,
a {
  touch-action: manipulation;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--reader-bg);
  color: var(--reader-fg);
  font-family:
    system-ui,
    -apple-system,
    sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

.hidden {
  display: none;
}
.pad {
  padding: 24px;
  color: var(--muted);
}

/* Reader load failure (document not downloaded and the server fetch failed). */
.reader__error {
  color: var(--accent);
}

/* The link a server offers alongside a refusal to convert a format. Both the
   link's text and its target come from the server. */
.reader__action {
  display: inline-block;
  margin-top: 0.75rem;
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--accent);
  text-decoration: none;
}

/* ---------- Top bar ---------- */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: calc(var(--bar-h) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  display: grid;
  grid-template-columns: 96px 1fr 96px;
  align-items: center;
  background: color-mix(in srgb, var(--reader-bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  z-index: 20;
  transition: transform 0.25s ease;
}
.topbar--hidden {
  transform: translateY(-110%);
}
.topbar__title {
  text-align: center;
  font-weight: 600;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 4px;
}
.topbar__left,
.topbar__right {
  display: flex;
  align-items: center;
  gap: 4px;
}
.topbar__left {
  justify-content: flex-start;
}
.topbar__right {
  justify-content: flex-end;
}
.iconbtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  color: var(--reader-fg);
  text-decoration: none;
  border-radius: 12px;
  -webkit-tap-highlight-color: transparent;
  /* Neutralize the UA button chrome so the class works for both the gear
     (an <a>) and the read-aloud toggle (a <button>). */
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
}
/* Pointer feedback that reads on any backdrop (topbar over the page, or the
   close control sitting on a modal card). */
.iconbtn:hover {
  background: color-mix(in srgb, var(--reader-fg) 10%, transparent);
}
.iconbtn:active {
  background: color-mix(in srgb, var(--reader-fg) 16%, transparent);
}
/* A thin circular ring around the icon (the reader's read-aloud toggle). */
.iconbtn--ring {
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 50%;
}
/* Toggled-on icon button (e.g. the reader's read-aloud button while playing):
   tint the glyph and, on a ringed button, the ring too. */
.iconbtn--on {
  color: var(--accent);
}
.iconbtn--ring.iconbtn--on {
  border-color: var(--accent);
}

/* ---------- Home ---------- */
.home {
  max-width: 860px;
  margin: 0 auto;
  padding: calc(var(--bar-h) + env(safe-area-inset-top) + 20px) 20px 40px;
}
.home__import {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 22px;
  flex-wrap: wrap;
}
.home__status {
  color: var(--muted);
  font-size: 14px;
}
.home__empty {
  color: var(--muted);
  line-height: 1.6;
  margin-top: 40px;
}
.server-notice {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 0 0 22px;
  padding: 16px 18px;
  border-radius: 16px;
  border: 1px solid var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--reader-fg);
  text-decoration: none;
}
.server-notice strong {
  color: var(--accent);
}
.server-notice span {
  color: var(--muted);
  font-size: 13px;
}
.btn {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--reader-fg);
  padding: 12px 18px;
  border-radius: 14px;
  cursor: pointer;
}
.btn--primary {
  background: var(--accent);
  color: #1a1206;
  border-color: transparent;
  font-weight: 600;
}
/* A destructive action (the "Remove" confirm button). */
.btn--danger {
  background: var(--danger);
  color: var(--danger-ink);
  border-color: transparent;
  font-weight: 600;
}
.btn:hover {
  border-color: color-mix(in srgb, var(--reader-fg) 35%, var(--border));
}
.btn:active {
  transform: scale(0.98);
}

/* Summary stats, mirroring the server home dashboard. */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
  margin-bottom: 22px;
}
.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--card-bg);
}
.stat strong {
  font-size: 20px;
  font-weight: 700;
}
.stat span {
  color: var(--muted);
  font-size: 12px;
}

.library {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}
.card__bar {
  height: 5px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--muted) 28%, transparent);
  overflow: hidden;
}
.card__bar > span {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
}
.card__sub {
  color: var(--muted);
  font-size: 12px;
}
.card {
  position: relative;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  overflow: hidden;
}
.card__open {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 18px;
  min-height: 96px;
  color: var(--reader-fg);
  text-decoration: none;
}
.card__title {
  font-weight: 600;
  font-size: 16px;
  line-height: 1.3;
}
.card__meta {
  color: var(--muted);
  font-size: 12.5px;
}
/* The card's "more options" affordance (top-right); opens the sheet below. */
.card__menu {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: color-mix(in srgb, var(--reader-bg) 60%, transparent);
  color: var(--muted);
  cursor: pointer;
}
.card__menu:active {
  background: var(--card-bg);
}

/* ---------- Card "more options" sheet (modal) ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.55);
  animation: modal-fade 0.15s ease;
}
@keyframes modal-fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
.modal__card {
  width: 100%;
  max-width: 360px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.modal__head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}
.modal__title {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  font-size: 16px;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.modal__label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 8px;
}
.modal__sync {
  width: 100%;
  font: inherit;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--reader-bg);
  color: var(--reader-fg);
}
.modal__note {
  color: var(--muted);
  font-size: 12.5px;
  margin: 8px 0 0;
}
.modal__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  color: var(--reader-fg);
}
.modal__del {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--border));
  border-radius: 50%;
  background: transparent;
  color: var(--danger);
  cursor: pointer;
  flex: none;
}
.modal__del:hover {
  border-color: var(--danger);
  background: color-mix(in srgb, var(--danger) 14%, transparent);
}
.modal__del:active {
  background: color-mix(in srgb, var(--danger) 24%, transparent);
}

/* The remove confirmation, layered above the options sheet. */
.modal--confirm {
  z-index: 60;
}
.modal__text {
  color: var(--reader-fg);
  font-size: 14px;
  line-height: 1.5;
  margin: 0 0 20px;
}
.modal__actions {
  display: flex;
  gap: 12px;
}
.modal__actions .btn {
  flex: 1;
}

/* ---------- Reader ---------- */
.reader {
  height: 100dvh;
}
.reader__scroll {
  height: 100dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.reader__pad {
  position: relative;
  width: 100%;
}
/* The reading column: a fixed `col_w`-wide block (set inline) centered by the
   auto side margins, with its lines left-aligned inside. Centering the block as
   a whole — rather than each line by its own width — keeps the relative
   indentation of code blocks and ASCII art intact (mirrors the TUI's single
   left offset), and the width stays stable while scrolling so nothing jitters
   horizontally. */
.reader__win {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  margin: 0 auto;
  padding: calc(var(--bar-h) + env(safe-area-inset-top) + 8px) 0
    env(safe-area-inset-bottom);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
.rline {
  font-family: var(--font-mono);
  font-size: var(--fs);
  line-height: var(--lh);
  height: var(--lh);
  white-space: pre;
  color: var(--reader-fg);
  width: max-content;
  max-width: 100%;
}
.rline--ansi {
  letter-spacing: 0;
}
.rline--speaking {
  background: color-mix(in srgb, var(--accent) 24%, transparent);
  border-radius: 4px;
}
/* A rasterized figure/table in "Images" mode. Its inline height spans the lines
   it replaces (so scroll math is unchanged); its inline width is the column
   width, so it fills the block and lines up with the text. */
.rline-img {
  display: block;
  object-fit: contain;
  max-width: 100%;
}

/* Reading-progress percentage, bottom-right corner (mirrors the CLI status
   line). Non-interactive. */
.reader__progress {
  position: fixed;
  right: 16px;
  bottom: calc(32px + env(safe-area-inset-bottom));
  z-index: 30;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: color-mix(in srgb, var(--card-bg) 82%, transparent);
  color: var(--reader-fg);
  opacity: 0.85;
  font-family: var(--font-mono);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  user-select: none;
}

/* ---------- Settings ---------- */
.settings__body {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(var(--bar-h) + env(safe-area-inset-top) + 20px) 20px 60px;
}
.setting {
  padding: 18px 0;
  border-bottom: 1px solid var(--border);
}
.setting > label {
  display: block;
  font-weight: 600;
  margin-bottom: 12px;
}
.setting__row {
  display: flex;
  align-items: center;
  gap: 14px;
}
.setting__row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}
.setting__value {
  color: var(--muted);
  min-width: 72px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.setting__hint {
  color: var(--muted);
  font-size: 13px;
  margin: 10px 0 0;
}
.setting__text {
  width: 100%;
  font: inherit;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--reader-fg);
  margin-bottom: 10px;
}
.account-ok {
  flex: 1;
  color: var(--accent);
  font-weight: 600;
}
.toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  font-weight: 400;
  color: var(--reader-fg);
}
.toggle input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}
.chip {
  font: inherit;
  padding: 9px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--reader-fg);
  cursor: pointer;
}
.chip--on {
  background: var(--accent);
  color: #1a1206;
  border-color: transparent;
  font-weight: 600;
}

/* ---------- Install banner ---------- */
.install {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  max-width: 560px;
  margin: 0 auto;
  animation: install-rise 0.3s ease;
}
@keyframes install-rise {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
.install__text {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1;
  min-width: 0;
}
.install__text strong {
  font-size: 15px;
}
.install__text span {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}
.install__actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.install__dismiss {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  cursor: pointer;
  flex: none;
}

/* ---------- Live sync toast ---------- */
/* "Reading position updated on another device" — a bottom banner with a Jump
   action, shown while a peer is ahead of us in the open document. Scrolling
   dismisses it after a short grace (see reader_support::live); the ✕ dismisses
   without moving. Reuses the install banner's slide-up. */
.toast {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  z-index: 45;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 12px 12px 16px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  max-width: 460px;
  margin: 0 auto;
  animation: install-rise 0.3s ease;
}
.toast__text {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  line-height: 1.4;
  color: var(--reader-fg);
}
.toast__action {
  flex: none;
  padding: 8px 16px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #1a1206;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}
.toast__action:active {
  transform: scale(0.98);
}
.toast__dismiss {
  flex: none;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
}

/* ---------- About & Credits ---------- */
.about,
.credits {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(var(--bar-h) + env(safe-area-inset-top) + 20px) 20px 48px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.panel {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
}
.about__head h1 {
  font-size: 30px;
  margin: 0 0 4px;
}
.about__head p {
  color: var(--muted);
  margin: 0;
}
.about__info {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 0;
}
.about__info > div {
  display: flex;
  gap: 12px;
}
.about__info dt {
  color: var(--muted);
  font-weight: 600;
  width: 104px;
  flex: none;
  margin: 0;
}
.about__info dd {
  margin: 0;
  word-break: break-word;
}
.about__links,
.credits__footer {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.about__links .btn,
.credits__footer .btn,
.coffee {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
}
.muted {
  color: var(--muted);
}
.link {
  color: var(--accent);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  word-break: break-word;
}
.credits h2,
.credits h3 {
  margin: 0 0 8px;
}
.credits__author {
  display: flex;
  align-items: center;
  gap: 18px;
}
.credits__author h2 {
  font-size: 22px;
  margin: 0 0 2px;
}
.credits__author p {
  margin: 0 0 8px;
}
.credits__support p {
  margin: 0 0 14px;
}
.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--border);
  display: block;
}
.avatar--lg {
  width: 88px;
  height: 88px;
  flex: none;
}
.contributors {
  list-style: none;
  margin: 14px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 16px;
}
.contributors a {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--reader-fg);
  font-size: 12px;
}
.contributors span {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
