/* ---- Material You base: neutral surfaces, one primary, outlines ---- */

:root {
  --surface: #ffffff;
  --on-surface: #1d1b20;
  --on-surface-variant: #49454f;
  --primary: #6750a4;
  --outline: #cac4cf;

  --icon-size: 18px;

  --bar-h: 56px;
}

[data-theme="dark"] {
  --surface: #17171a;
  --on-surface: #e6e0e9;
  --on-surface-variant: #cac4cf;
  --primary: #d0bcff;
  --outline: #48454e;
}

[data-theme="oled"] {
  --surface: #000000;
  --on-surface: #e6e0e9;
  --on-surface-variant: #cac4cf;
  --primary: #d0bcff;
  --outline: #34313a;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  display: flex;
  flex-direction: column;
  font-family: Roboto, system-ui, -apple-system, sans-serif;
  background: var(--surface);
  color: var(--on-surface);
  overflow: hidden;
}

svg { fill: currentColor; }

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }

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

/* ---- Floating app bar ---- */

.bar {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: var(--bar-h);
  padding-inline: 8px;
  flex-shrink: 0;
  transition: transform .22s ease;
}

.top-bar {
  position: absolute;
  top: env(safe-area-inset-top);
  left: 0;
  right: 0;
  z-index: 20;
  background: transparent;
}

/* All appbar controls share the nav-capsule proportions */
.top-bar .icon-btn {
  width: 32px;
  height: 34px;
  border-radius: 999px;
}
.top-bar .pill-btn { height: 34px; }

/* Controls are opaque so text scrolling underneath never bleeds through */
.top-bar .icon-btn,
.top-bar .pill-btn,
.top-bar .nav-group {
  background: var(--surface);
}
/* back/front buttons inherit their capsule's fill; they only carry hover tints */
.top-bar .nav-group .icon-btn { background: transparent; }

/* Immersive mode: bar slides off-screen, floating speech button sinks away */
.bars-hidden .top-bar {
  transform: translateY(calc(-100% - env(safe-area-inset-top)));
}
.bars-hidden .corner-left {
  transform: translateY(calc(100% + env(safe-area-inset-bottom)));
  opacity: 0;
  pointer-events: none;
}

/* Floating speech button: round outlined, sinks away in immersive mode */
.icon-btn.corner-left {
  position: fixed;
  z-index: 15; /* below the drawer (20) and menus (25), above page content */
  left: 10px;
  bottom: calc(env(safe-area-inset-bottom) + 10px);
  background: var(--surface); /* opaque: text scrolling underneath stays hidden */
  transition: transform .22s ease, opacity .22s ease, background-color .15s ease;
}

.titles {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  flex: 1;
  min-width: 0;
  margin-inline: 8px;
  padding: 5px 14px;
  line-height: 1.25;
  border: 1px solid var(--outline);
  border-radius: 999px;
  background: var(--surface);
}

.book-title    { font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.section-label {
  font-size: .75rem;
  color: var(--on-surface-variant);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ---- Buttons ---- */

.icon-btn {
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  flex-shrink: 0;
  transition: background-color .15s ease, color .15s ease;
}

.icon-btn:hover:not(:disabled) { background: color-mix(in srgb, var(--on-surface) 10%, transparent); }
.icon-btn:active:not(:disabled) { background: color-mix(in srgb, var(--on-surface) 16%, transparent); }
/* One icon size everywhere: buttons, pills, and menu rows all share it */
.icon-btn svg,
.pill-btn svg,
.menu-item svg {
  width: var(--icon-size);
  height: var(--icon-size);
}
.icon-btn:disabled { opacity: .38; cursor: default; }

/* Round outlined action buttons (contents drawer, fullscreen) */
.icon-btn.outlined { border: 1px solid var(--outline); }

/* Tall slim capsule (M3 Expressive-style more button) */
.pill-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 36px;
  border-radius: 999px;
  border: 1px solid var(--outline);
  flex-shrink: 0;
  transition: background-color .15s ease;
}
.pill-btn:hover { background: color-mix(in srgb, var(--on-surface) 8%, transparent); }

/* Back/front chapter buttons: snug outlined capsule, matching the more pill */
.nav-group {
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
  border: 1px solid var(--outline);
  border-radius: 999px;
}
.nav-group .icon-btn {
  width: 32px;
  height: 34px;
  border-radius: 999px;
}

/* ---- Drawer: same surface, hairline edge ---- */

.drawer {
  position: fixed;
  z-index: 20;
  top: calc(env(safe-area-inset-top) + 8px);
  bottom: calc(env(safe-area-inset-bottom) + 10px);
  left: 0;
  width: min(320px, 85vw);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--outline);
  transform: translateX(-100%);
  transition: transform .25s ease, top .25s ease, bottom .25s ease;
}
.drawer.open { transform: translateX(0); }
.bars-hidden .drawer {
  top: env(safe-area-inset-top);
}

.toc-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.toc-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 16px;
  border-radius: 999px;
  border: 1px solid transparent;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  transition: background-color .15s ease;
}
.toc-item:hover { background: color-mix(in srgb, var(--on-surface) 8%, transparent); }
.toc-item.current {
  color: var(--primary);
  border-color: var(--outline); /* selected = outlined pill, nothing more */
}

/* ---- Speech options menu: anchored to bottom-left corner ---- */

.menu {
  position: fixed;
  z-index: 25;
  left: 8px;
  bottom: calc(env(safe-area-inset-bottom) + 62px);
  width: min(300px, calc(100vw - 16px));
  padding: 12px 16px 16px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
  font-size: .85rem;
  background: var(--surface);
  border: 1px solid var(--outline);
  border-radius: 16px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .30);
}

[data-theme="dark"] .menu,
[data-theme="oled"] .menu { box-shadow: 0 4px 16px rgba(0, 0, 0, .55); }

/* ---- Display settings menu: anchored below top-right corner ---- */

.menu.top-end {
  left: auto;
  right: 8px;
  top: calc(var(--bar-h) + env(safe-area-inset-top) + 8px);
  bottom: auto;
}

.menu-heading {
  font-size: .75rem;
  font-weight: 500;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--on-surface-variant);
}

/* Overflow menu: no container padding, only full-bleed clickable rows */
#moreMenu {
  padding: 0;
  gap: 0;
  overflow: hidden;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 13px 18px;
  border-radius: 0; /* square: middle rows */
  text-align: left;
  transition: background-color .15s ease;
}
/* edge rows round only toward the menu's own corners */
#moreMenu .menu-item:first-child:not(:last-child) { border-radius: 12px 12px 0 0; }
#moreMenu .menu-item:last-child:not(:first-child) { border-radius: 0 0 12px 12px; }
#moreMenu .menu-item:only-child { border-radius: 12px; }
.menu-item:hover { background: color-mix(in srgb, var(--on-surface) 8%, transparent); }
.menu-item svg { flex-shrink: 0; color: var(--on-surface-variant); }
.menu-item.back svg { color: inherit; }

/* ---- Speech menu: mirrors the overflow menu — flush rows, edge-clipped hovers ---- */

#speechMenu {
  padding: 8px 0 10px;
  gap: 2px;
  overflow: hidden;
}

.speech-actions {
  display: flex;
  gap: 10px;
  padding: 2px 14px 10px;
}
.speech-actions .icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
}
.speech-actions .icon-btn.active { color: var(--primary); }
.speech-actions .icon-btn:disabled { opacity: .38; cursor: default; }

/* Setting rows: leading icon + control, styled like overflow-menu items */
.menu-row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 44px;
  padding: 6px 18px;
  cursor: pointer;
}
.menu-row:hover { background: color-mix(in srgb, var(--on-surface) 8%, transparent); }
.menu-row > svg { width: var(--icon-size); height: var(--icon-size); flex-shrink: 0; color: var(--on-surface-variant); }

.menu-row select {
  appearance: none;
  -webkit-appearance: none;
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: .9rem;
  color: var(--on-surface);
  padding-right: 26px; /* room for the chevron */
  text-overflow: ellipsis;
  cursor: pointer;
  min-height: 32px;
}
.menu-row > .chev {
  position: absolute;
  right: 16px;
  width: 16px;
  height: 16px;
  pointer-events: none;
}

#speechMenu .menu-row input[type="range"] { flex: 1; min-width: 0; }
#speechMenu .menu-row #rateVal {
  flex-shrink: 0;
  min-width: 3.2em;
  text-align: right;
  color: var(--on-surface-variant);
  font-variant-numeric: tabular-nums;
}

/* Settings panel inside the overflow menu keeps its own insets */
#moreSettings {
  display: grid;
  gap: 12px;
  padding-bottom: 14px;
}
#moreSettings .menu-heading,
#moreSettings .segmented { margin-inline: 18px; }

.segmented { display: flex; flex-wrap: wrap; gap: 6px; }

.seg-btn {
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid var(--outline);
  font-size: .85rem;
  transition: background-color .15s ease, color .15s ease, border-color .15s ease;
}
.seg-btn:hover { background: color-mix(in srgb, var(--on-surface) 8%, transparent); }
.seg-btn[aria-pressed="true"] {
  color: var(--primary);
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 12%, transparent);
}

input[type="range"] {
  accent-color: var(--primary);
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: color-mix(in srgb, var(--on-surface) 25%, transparent);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px; height: 18px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 18px; height: 18px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  cursor: pointer;
}

.voice-status {
  margin-inline: 18px;
  font-size: .8rem;
  line-height: 1.4;
  color: #b3261e;
}
[data-theme="dark"] .voice-status, [data-theme="oled"] .voice-status { color: #f2b8b5; }

/* ---- Reader ---- */

#viewerWrap {
  position: relative;
  flex: 1;
  min-height: 0;
}

#viewer {
  position: absolute;
  inset: 0;
}

/* Clearance for the floating app bar. Lives INSIDE the scroller (.epub-container),
   so it provides the default gap at rest but scrolls away — letting content pass
   under the bar once you start scrolling. Host-side, so book CSS can't override it. */
#viewer .epub-container {
  padding-top: 112px;
  padding-bottom: 96px;
}

#emptyState {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--outline);
  pointer-events: none;
  text-align: center;
  padding: 24px;
}
#emptyState svg { width: 64px; height: 64px; }

.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--surface) 80%, transparent);
  backdrop-filter: blur(4px);
}
.drop-overlay p {
  padding: 24px 40px;
  border: 2px dashed var(--primary);
  border-radius: 24px;
  color: var(--primary);
  font-size: 1.1rem;
}

@media (min-width: 900px) {
  :root { --bar-h: 64px; }
}
