:root {
  --bg: #1a1712;
  --bg-deep: #100e0b;
  --card: #29241e;
  --ink: #f5f0e1;
  --muted: #ada38f;
  --amber: #e8ad47;
  --amber-dim: #b8842a;
  --line: rgba(245, 240, 225, 0.08);
  --radius: 14px;
  /* Home-screen / standalone PWAs draw under the status bar when
     apple-mobile-web-app-status-bar-style is black-translucent. */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --player-h: 8.25rem;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(120% 80% at 10% -10%, #3a2c18 0%, transparent 55%),
    radial-gradient(90% 60% at 100% 0%, #2a2218 0%, transparent 50%),
    var(--bg-deep);
  color: var(--ink);
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua",
    Georgia, serif;
}

body {
  min-height: 100dvh;
  min-height: 100svh;
}

#app {
  max-width: 720px;
  margin: 0 auto;
  padding:
    0
    calc(1rem + var(--safe-r))
    calc(var(--player-h) + var(--safe-b) + 1rem)
    calc(1rem + var(--safe-l));
}

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  /* Keep title/buttons below notch / status bar, including when sticky. */
  padding: calc(0.65rem + var(--safe-t)) 0 0.85rem;
  position: sticky;
  top: 0;
  z-index: 5;
  background: linear-gradient(
    to bottom,
    var(--bg-deep) 0%,
    var(--bg-deep) 88%,
    transparent 100%
  );
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.brand-mark {
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 0.55rem;
  flex: 0 0 auto;
  display: block;
  object-fit: cover;
  background: #100e0b;
  box-shadow: 0 0 0 1px rgba(245, 240, 225, 0.14);
}

.brand h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-actions {
  display: flex;
  gap: 0.4rem;
  flex: 0 0 auto;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.shelf-tools {
  flex: 1 1 100%;
}

.shelf-search {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem 0.7rem;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--card) 92%, transparent);
}

.shelf-search:focus-within {
  border-color: rgba(232, 173, 71, 0.45);
}

.search-icon {
  width: 1.1rem;
  height: 1.1rem;
  flex: 0 0 auto;
  fill: var(--muted);
}

.shelf-search input[type="search"] {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
  outline: none;
}

.shelf-search input[type="search"]::placeholder {
  color: var(--muted);
}

.shelf-search input[type="search"]::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
}

.search-clear {
  flex: 0 0 auto;
  width: 1.7rem;
  height: 1.7rem;
  padding: 0;
  border-radius: 50%;
  border-color: transparent;
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
  font-size: 1.05rem;
  line-height: 1;
}

.shelf-count {
  margin: 0.45rem 0 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.shelf-empty {
  margin: 0.35rem 0 0;
  color: var(--muted);
  line-height: 1.45;
}

button {
  font: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
  color: var(--ink);
  background: transparent;
}

button:focus-visible,
.shelf-search:focus-within,
input[type="range"]:focus-visible {
  outline: 2px solid var(--amber);
  outline-offset: 3px;
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

button.primary {
  background: linear-gradient(180deg, #f0c36a, var(--amber-dim));
  color: #1a1208;
  font-weight: 700;
  border-color: rgba(255, 220, 150, 0.35);
}

button.ghost,
a.ghost {
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: var(--ink);
}

a.ghost {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
  font: inherit;
}

button.large {
  padding: 0.75rem 1.2rem;
  font-size: 1rem;
}

button.icon {
  width: 2.6rem;
  height: 2.6rem;
  padding: 0;
  border-radius: 50%;
  border-color: var(--line);
  display: inline-grid;
  place-items: center;
  color: var(--ink);
}

button.icon svg {
  width: 1.15rem;
  height: 1.15rem;
  fill: currentColor;
  display: block;
}

button.icon.play {
  width: 3.15rem;
  height: 3.15rem;
  background: linear-gradient(180deg, #f0c36a, var(--amber-dim));
  color: #1a1208;
  border: none;
  box-shadow: 0 1px 0 rgba(255, 230, 170, 0.25) inset;
}

button.icon.play svg {
  width: 1.35rem;
  height: 1.35rem;
}

button.icon.play .icon-pause {
  display: none;
}

button.icon.play.is-paused .icon-play {
  display: block;
  /* optical center: triangles read left-heavy */
  transform: translateX(1px);
}

button.icon.play.is-paused .icon-pause {
  display: none;
}

button.icon.play:not(.is-paused) .icon-play {
  display: none;
}

button.icon.play:not(.is-paused) .icon-pause {
  display: block;
}

.panel {
  background: color-mix(in srgb, var(--card) 88%, transparent);
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 4px);
  padding: 1.4rem 1.25rem 1.5rem;
}

.onboarding h2 {
  margin: 0 0 0.6rem;
  font-size: 1.55rem;
  line-height: 1.25;
}

.onboarding p,
.onboarding li {
  color: var(--muted);
  line-height: 1.5;
}

.onboarding ol {
  margin: 1rem 0 1.25rem;
  padding-left: 1.2rem;
}

.onboarding code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.86em;
  color: var(--ink);
}

.cta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.cta-stack {
  flex-direction: column;
  align-items: stretch;
}

.cta-stack button.large {
  width: 100%;
}

button.text-link {
  width: auto;
  align-self: center;
  padding: 0.35rem 0.2rem;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.92rem;
  text-decoration: underline;
  text-underline-offset: 0.16em;
}

button.text-link:hover,
button.text-link:focus-visible {
  color: var(--amber);
  background: none;
}

.cta-stack button.text-link {
  width: auto;
}

.hint {
  margin: 1rem 0 0;
  font-size: 0.9rem;
}

.library-status {
  margin: 1rem 0 0;
  padding: 0.85rem 0.95rem;
  border-radius: 12px;
  border: 1px solid rgba(232, 173, 71, 0.35);
  background: color-mix(in srgb, var(--card) 82%, #4a2a12);
  color: var(--ink);
  white-space: pre-wrap;
  line-height: 1.45;
  font-size: 0.92rem;
}

.library-status:empty {
  display: none;
}

body.is-scanning {
  cursor: progress;
}

body.is-scanning .primary,
body.is-scanning .text-link,
body.is-scanning #btn-change,
body.is-scanning #btn-google,
body.is-scanning #btn-pick-main,
body.is-scanning #btn-pick-files {
  opacity: 0.65;
  pointer-events: none;
}

body.is-scanning #capability-hint,
body.is-scanning .hint {
  color: var(--amber);
}

.hidden {
  display: none !important;
}

.shelf {
  display: grid;
  gap: 0.75rem;
}

.shelf-group-title {
  margin: 0.75rem 0 0.15rem;
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.continue-card {
  display: grid;
  grid-template-columns: 4.5rem 1fr;
  gap: 0.9rem;
  align-items: center;
  width: 100%;
  text-align: left;
  padding: 0.85rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid rgba(232, 173, 71, 0.45);
  background:
    linear-gradient(135deg, rgba(232, 173, 71, 0.16), transparent 55%),
    color-mix(in srgb, var(--card) 92%, transparent);
  color: inherit;
  margin-bottom: 0.35rem;
}

.continue-card .continue-label {
  margin: 0;
  color: var(--amber);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.continue-card .title {
  margin: 0.15rem 0 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.continue-card .progress {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.save-banner {
  padding: 0.9rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--card) 90%, transparent);
  margin-bottom: 0.35rem;
}

.save-banner p {
  margin: 0 0 0.7rem;
  color: var(--muted);
  line-height: 1.4;
  font-size: 0.95rem;
}

.book-card {
  display: grid;
  grid-template-columns: 4.5rem 1fr;
  gap: 0.9rem;
  align-items: center;
  width: 100%;
  text-align: left;
  padding: 0.75rem;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--card) 90%, transparent);
  color: inherit;
}

.book-card:hover {
  border-color: rgba(232, 173, 71, 0.35);
}

.book-card .title-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.55rem;
  margin: 0;
}

.book-card .title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  min-width: 0;
}

.shelf-badge {
  flex: 0 0 auto;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 0.12rem 0.42rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  line-height: 1.2;
}

.shelf-badge-both {
  color: var(--amber);
  border-color: rgba(232, 173, 71, 0.45);
}

.shelf-badge-read {
  color: var(--ink);
  border-color: rgba(245, 240, 225, 0.28);
}

.book-card .meta,
.book-card .progress {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.book-card .chapter-hits {
  margin: 0.3rem 0 0;
  color: var(--amber);
  font-size: 0.85rem;
  line-height: 1.35;
}

.progress-track {
  margin-top: 0.45rem;
  height: 3px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.08);
  overflow: hidden;
}

.progress-track > span {
  display: block;
  height: 100%;
  background: var(--amber);
  width: 0%;
}

.cover {
  width: 4.5rem;
  height: 4.5rem;
  border-radius: 0.55rem;
  background: #221c16;
  background-size: cover;
  background-position: center;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
  position: relative;
  overflow: hidden;
  flex: 0 0 auto;
}

.cover.large {
  width: 7rem;
  height: 7rem;
}

.cover.has-image {
  background-color: #111;
}

.cover.placeholder {
  display: grid;
  place-items: center;
  padding: 0.35rem;
  text-align: center;
}

.cover.placeholder::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(160deg, var(--cover-a, #5a4324), var(--cover-b, #1a1712));
  opacity: 1;
}

.cover.placeholder .cover-initials {
  position: relative;
  z-index: 1;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--ink);
  line-height: 1.15;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
}

.cover.large.placeholder .cover-initials {
  font-size: 1.55rem;
}

.book-screen .back {
  margin-bottom: 0.75rem;
}

.book-hero {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 1rem;
  align-items: center;
  margin-bottom: 1rem;
}

.book-meta .collection {
  margin: 0;
  color: var(--amber);
  font-size: 0.8rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.book-meta h2 {
  margin: 0.2rem 0;
  font-size: 1.55rem;
  line-height: 1.2;
}

.book-meta .author,
.book-meta .subtitle {
  margin: 0.15rem 0 0;
  color: var(--muted);
}

.offline-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.75rem;
  margin: -0.25rem 0 1rem;
}

.offline-row .hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.book-card.unavailable .cover,
.book-card.unavailable .title {
  opacity: 0.6;
}

.chapter-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.35rem;
}

.chapter-list li button {
  width: 100%;
  display: grid;
  grid-template-columns: 2rem 1fr auto;
  gap: 0.65rem;
  align-items: center;
  text-align: left;
  border-radius: 0.7rem;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.02);
  padding: 0.7rem 0.8rem;
}

.chapter-list li button.active {
  border-color: rgba(232, 173, 71, 0.55);
  background: rgba(232, 173, 71, 0.1);
}

.chapter-list .idx {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.chapter-list .ch-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chapter-list .ch-time {
  color: var(--muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

.book-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0 0 0.85rem;
}

.book-tabs button[aria-pressed="true"] {
  border-color: rgba(232, 173, 71, 0.55);
  background: rgba(232, 173, 71, 0.12);
}

.concept-list {
  display: grid;
  gap: 0.85rem;
}

.concept-note {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.concept-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: #f3efe4;
  color: #241c14;
}

.concept-card img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1;
  object-fit: cover;
  background: #efe6d4;
}

.concept-card h3,
.concept-card p {
  margin: 0.35rem 0.95rem;
}

.concept-card h3 {
  margin-top: 0.85rem;
  font-size: 1.2rem;
}

.concept-card .concept-en {
  color: #6d5848;
  font-size: 0.86rem;
  letter-spacing: 0.02em;
}

.concept-card p:last-child {
  margin-bottom: 0.95rem;
  line-height: 1.5;
}

.reader {
  display: grid;
  gap: 0.55rem;
  margin-top: 0.35rem;
}

.reader-bar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.45rem;
  align-items: center;
}

.reader-label {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.reader-frame {
  width: 100%;
  height: min(70vh, 36rem);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #f3efe4;
  display: block;
}

.reader .hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.player-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: 3rem 1fr auto;
  grid-template-areas:
    "cover text controls"
    "time time time";
  gap: 0.55rem 0.75rem;
  align-items: center;
  padding:
    0.7rem
    calc(1rem + var(--safe-r))
    calc(0.7rem + var(--safe-b))
    calc(1rem + var(--safe-l));
  background: rgba(16, 14, 11, 0.92);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(14px);
  z-index: 10;
}

.player-cover {
  grid-area: cover;
  width: 3rem;
  height: 3rem;
  border-radius: 0.4rem;
  background: linear-gradient(160deg, #5a4324, #1a1712);
  background-size: cover;
  background-position: center;
}

.player-text {
  grid-area: text;
  min-width: 0;
}

.player-title-row {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  min-width: 0;
}

.player-title-row .player-title {
  flex: 1;
  min-width: 0;
}

.player-title,
.player-chapter {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-title {
  font-weight: 700;
}

.player-sub {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  min-width: 0;
}

.player-sub .player-chapter {
  flex: 1;
  min-width: 0;
}

a.read-this-book {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 1.85rem;
  padding: 0 0.55rem;
  border: 1px solid rgba(232, 173, 71, 0.55);
  border-radius: 999px;
  background: rgba(232, 173, 71, 0.12);
  color: var(--amber);
  font-size: 0.75rem;
  font-weight: 700;
  line-height: 1;
  letter-spacing: 0.02em;
  text-decoration: none;
  white-space: nowrap;
}

.book-meta a.read-this-book {
  margin-top: 0.7rem;
  height: 2.15rem;
  padding: 0 0.9rem;
  font-size: 0.95rem;
}

.player-chapter {
  color: var(--muted);
  font-size: 0.88rem;
}

button.skip.rate,
button.skip.sleep {
  flex: 0 0 auto;
  min-width: 3.15rem;
}

button.skip.sleep.is-on {
  border-color: rgba(232, 173, 71, 0.55);
  color: var(--amber);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--player-h) + var(--safe-b) + 0.75rem);
  transform: translateX(-50%);
  z-index: 20;
  max-width: min(92vw, 28rem);
  padding: 0.7rem 1rem;
  border-radius: 999px;
  border: 1px solid rgba(232, 173, 71, 0.35);
  background: rgba(20, 17, 13, 0.94);
  color: var(--ink);
  font-size: 0.92rem;
  line-height: 1.3;
  text-align: center;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.player-controls {
  grid-area: controls;
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.player-time {
  grid-area: time;
  display: grid;
  grid-template-columns: auto auto 1fr auto auto;
  gap: 0.45rem;
  align-items: center;
  font-variant-numeric: tabular-nums;
  font-size: 0.82rem;
  color: var(--muted);
}

button.skip {
  min-width: 2.55rem;
  height: 1.85rem;
  padding: 0 0.4rem;
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: var(--ink);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.player-time input[type="range"] {
  width: 100%;
  accent-color: var(--amber);
}

@media (min-width: 640px) {
  .player-bar {
    left: 50%;
    transform: translateX(-50%);
    width: min(720px, 100%);
    border-radius: 1rem 1rem 0 0;
  }
}

.sheet {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: end center;
  padding: 1rem;
  padding-bottom: calc(1rem + var(--safe-b));
  background: rgba(8, 7, 5, 0.62);
}

.sheet-card {
  width: min(32rem, 100%);
  max-height: min(80vh, 36rem);
  overflow: auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 4px);
  padding: 1.15rem 1.1rem 1.2rem;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
}

.sheet-card h2 {
  margin: 0 0 0.4rem;
  font-size: 1.2rem;
}

.sheet-card .hint {
  margin: 0 0 0.85rem;
}

.source-list {
  list-style: none;
  margin: 0 0 0.9rem;
  padding: 0;
  display: grid;
  gap: 0.45rem;
}

.source-list:empty {
  display: none;
}

.source-list button {
  width: 100%;
  text-align: left;
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.03);
  border-radius: 12px;
  padding: 0.7rem 0.85rem;
}

.source-advanced {
  margin: 0.15rem 0 0;
  border-top: 1px solid var(--line);
  padding-top: 0.7rem;
}

.source-advanced summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 0.88rem;
  list-style: none;
}

.source-advanced summary::-webkit-details-marker {
  display: none;
}

.source-advanced summary::after {
  content: "";
  display: inline-block;
  margin-left: 0.35rem;
  border: 0.28rem solid transparent;
  border-top-color: currentColor;
  transform: translateY(0.12rem);
}

.source-advanced[open] summary {
  margin-bottom: 0.55rem;
  color: var(--ink);
}

.source-advanced[open] summary::after {
  transform: translateY(-0.06rem) rotate(180deg);
}

#drive-status:empty {
  display: none;
}

#drive-status.drive-error {
  color: var(--amber);
  white-space: pre-wrap;
}
