:root {
  --ui-scale: 1;
  --bg: #1e1e1e;
  --page-bg: #3a3a3a;
  --fg: #dddddd;
  --dim: #999999;
  --accent: #7fd1ff;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

/* Scrolling page area; canvas centers horizontally but stays reachable when
   wider than the viewport (safe center). touch-action keeps native one-finger
   panning while setupTouch() owns multi-touch (pinch) and swipe page-turns. */
#viewport {
  flex: 1 1 auto;
  overflow: auto;
  background: var(--page-bg);
  display: flex;
  justify-content: safe center;
  align-items: flex-start;
  touch-action: pan-x pan-y;
}

#page {
  display: block;
}

/* Page color themes (global). Applied as a filter on the rendered page, plus a
   matching surround. White = default (no filter). */
body[data-page-theme="sepia"] #page {
  filter: sepia(0.35) brightness(0.92);
}
body[data-page-theme="sepia"] #viewport {
  background: #2e2a24;
}
body[data-page-theme="dark"] #page {
  /* invert(0.9) softens pure black/white; hue-rotate keeps colors sane. */
  filter: invert(0.9) hue-rotate(180deg);
}
body[data-page-theme="dark"] #viewport {
  background: #111111;
}

#hint {
  position: fixed;
  top: 24px;
  left: 50%;
  transform: translateX(-50%);
  color: var(--fg);
  font-size: calc(18px * var(--ui-scale));
  background: rgba(0, 0, 0, 0.35);
  padding: 10px 16px;
  border-radius: 8px;
  pointer-events: none;
}
#hint.hidden {
  display: none;
}

/* Bottom status line: filename · page · scale · cheats (single line). */
#status {
  flex: 0 0 auto;
  background: var(--bg);
  color: #cccccc;
  padding: 5px 10px;
  font-size: calc(13px * var(--ui-scale));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-top: 1px solid #000;
}

/* Touch toolbar — a floating pill above the status line. Hidden by default and
   revealed via .show (center tap, or auto-shown when no document is open). It
   overlays (position: fixed) so it never reshapes the #viewport render area,
   and it's purely additive: mouse/keyboard users can ignore it. */
#toolbar {
  position: fixed;
  left: 50%;
  bottom: 34px;
  transform: translateX(-50%) translateY(12px);
  display: flex;
  align-items: center;
  gap: 4px;
  max-width: 96vw;
  flex-wrap: wrap;
  justify-content: center;
  padding: 6px;
  background: rgba(43, 43, 43, 0.96);
  border: 1px solid #444;
  border-radius: 14px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  z-index: 8; /* below the TOC/Recent/Help overlays (10–11) so they cover it */
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}
#toolbar.show {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}
#toolbar button {
  min-width: calc(44px * var(--ui-scale));
  height: calc(44px * var(--ui-scale));
  padding: 0 10px;
  font-size: calc(18px * var(--ui-scale));
  color: var(--fg);
  background: transparent;
  border: none;
  border-radius: 9px;
  cursor: pointer;
}
#toolbar button:hover {
  background: #37414e;
}
#toolbar button:active {
  background: #3d6fa5;
}
#tb-page {
  font-size: calc(14px * var(--ui-scale)) !important;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.tb-sep {
  width: 1px;
  align-self: stretch;
  margin: 4px 2px;
  background: #555;
}

/* Overflow menu for the less-used actions. */
#tb-more {
  display: none;
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  flex-direction: column;
  min-width: 160px;
  padding: 6px;
  background: rgba(43, 43, 43, 0.98);
  border: 1px solid #444;
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}
#tb-more.show {
  display: flex;
}
#tb-more button {
  width: 100%;
  height: calc(44px * var(--ui-scale));
  justify-content: flex-start;
  text-align: left;
  font-size: calc(15px * var(--ui-scale));
}

/* Install button — shown only when the browser reports the app is installable. */
#install {
  position: fixed;
  top: 16px;
  right: 16px;
  display: none;
  padding: 8px 14px;
  font-size: calc(14px * var(--ui-scale));
  color: #fff;
  background: #3d6fa5;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  z-index: 9;
}
#install.show {
  display: block;
}
#install:hover {
  background: #4a82c0;
}

/* Help overlay */
#help {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  z-index: 10;
  padding: 24px;
}
#help.show {
  display: flex;
}
.help-card {
  background: #2b2b2b;
  border: 1px solid #444;
  border-radius: 12px;
  padding: 20px 24px;
  max-width: 90vw;
  max-height: 90vh;
  overflow: auto;
}
.help-card h2 {
  margin: 0 0 12px;
  font-size: calc(22px * var(--ui-scale));
}
.help-card table {
  border-collapse: separate;
  border-spacing: 12px 5px;
}
.help-card td {
  font-size: calc(16px * var(--ui-scale));
  vertical-align: top;
}
.help-card .keys {
  text-align: right;
  white-space: nowrap;
}
.help-card .dim {
  color: var(--dim);
  font-size: calc(13px * var(--ui-scale));
  max-width: 36em;
}

/* Table-of-contents and recent-files overlays (shared card styling) */
#toc,
#recent {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  z-index: 11;
  padding: 24px;
}
#toc.show,
#recent.show {
  display: flex;
}
.toc-card {
  background: #2b2b2b;
  border: 1px solid #444;
  border-radius: 12px;
  padding: 16px 12px 16px 16px;
  width: min(580px, 92vw);
  max-height: 86vh;
  display: flex;
  flex-direction: column;
}
.toc-card h2 {
  margin: 0 0 10px;
  padding: 0 8px;
  font-size: calc(20px * var(--ui-scale));
}
.toc-list {
  overflow: auto;
}
.toc-row {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding: 5px 8px;
  font-size: calc(15px * var(--ui-scale));
  color: #dddddd;
  white-space: nowrap;
  border-radius: 6px;
  cursor: pointer;
}
.toc-row:hover {
  background: #37414e;
}
.toc-row.sel {
  background: #3d6fa5;
  color: #ffffff;
}
.toc-pg {
  color: #88aabb;
  font-variant-numeric: tabular-nums;
}
.toc-row.sel .toc-pg {
  color: #cceeff;
}
.toc-card .dim {
  color: var(--dim);
  padding: 8px;
  font-size: calc(14px * var(--ui-scale));
}

/* Recent-files rows: filename on the left, size/meta on the right. */
.recent-row {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding: 6px 8px;
  font-size: calc(15px * var(--ui-scale));
  color: #dddddd;
  border-radius: 6px;
  cursor: pointer;
}
.recent-row:hover {
  background: #37414e;
}
.recent-row.sel {
  background: #3d6fa5;
  color: #ffffff;
}
.recent-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.recent-meta {
  color: #88aabb;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.recent-row.sel .recent-meta {
  color: #cceeff;
}

kbd {
  font-family: ui-monospace, "Cascadia Code", Menlo, monospace;
  font-size: 0.92em;
  color: var(--accent);
  background: #2b2b2b;
  border: 1px solid #444;
  border-radius: 4px;
  padding: 1px 6px;
}

body.dragging #viewport {
  outline: 3px dashed var(--accent);
  outline-offset: -8px;
}
