:root { color-scheme: light dark; }
html[data-theme="dark"] { color-scheme: dark; }
html[data-theme="light"] { color-scheme: light; }
/* Explicit theme choice overrides the OS preference. */
html[data-theme="dark"] body { background: #1a1a2e; color: #e8e8e8; }
html[data-theme="dark"] .sheet { background: #262640; }
html[data-theme="dark"] .sheet select { background: #1a1a2e; border-color: #444; }
html[data-theme="dark"] .card { background: #262640; }
html[data-theme="dark"] #search,
html[data-theme="dark"] #cat-filter,
html[data-theme="dark"] #country-filter { background: #262640; border-color: #444; }
html[data-theme="dark"] .pager button { background: #262640; border-color: #444; color: #e8e8e8; }
html[data-theme="light"] body { background: #faf7f0; color: #2c2c2c; }
html[data-theme="light"] .sheet { background: #fff; }
html[data-theme="light"] .sheet select { background: #faf7f0; border-color: #ddd; }
html[data-theme="light"] .card { background: #fff; }
html[data-theme="light"] #search,
html[data-theme="light"] #cat-filter,
html[data-theme="light"] #country-filter { background: #fff; border-color: #ddd; }
html[data-theme="light"] .pager button { background: #fff; border-color: #ccc; color: inherit; }
* { box-sizing: border-box; }
body {
  margin: 0; font-family: system-ui, -apple-system, "PingFang HK", "Noto Sans TC", sans-serif;
  background: #faf7f0; color: #2c2c2c;
}
@media (prefers-color-scheme: dark) {
  body { background: #1a1a2e; color: #e8e8e8; }
}
.topbar { text-align: center; padding: 1.2rem 1rem 0.6rem; position: relative; }
/* Story list header: align with the 900px content column (same idea as the
   reader bar), so the gear sits at the column edge, not the viewport edge. */
body > header.topbar:not(.reader-bar) { max-width: 900px; margin: 0 auto; }
.topbar h1 { margin: 0; font-size: 1.8rem; }
.brand-en { font-size: 0.85rem; letter-spacing: 0.35em; opacity: 0.55;
  margin: 0.15rem 0 0.3rem; font-weight: 600; }
.gear { position: absolute; top: 1rem; right: 1rem; font-size: 1.5rem;
  background: none; border: none; cursor: pointer; padding: 0.3rem; line-height: 1; }
.sheet-back { position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 40; }
.sheet { position: fixed; left: 50%; bottom: 0; transform: translateX(-50%);
  width: 100%; max-width: 480px; background: #fff; border-radius: 20px 20px 0 0;
  padding: 1.2rem 1.4rem 1.6rem; z-index: 41; box-shadow: 0 -4px 24px rgba(0,0,0,0.15); }
@media (prefers-color-scheme: dark) { .sheet { background: #262640; } }
.sheet h2 { margin: 0 0 1rem; font-size: 1.2rem; }
.sheet label { display: flex; flex-direction: column; gap: 0.3rem;
  font-size: 0.9rem; margin-bottom: 0.9rem; }
.sheet select { padding: 0.6rem; font-size: 1rem; border-radius: 12px;
  border: 1px solid #ddd; background: #faf7f0; color: inherit; }
@media (prefers-color-scheme: dark) { .sheet select { background: #1a1a2e; border-color: #444; } }
.sheet-note { font-size: 0.85rem; opacity: 0.65; margin: 0.4rem 0 1rem; }
.sheet-close { width: 100%; padding: 0.7rem; font-size: 1rem; border: none;
  border-radius: 12px; background: #6a5acd; color: #fff; cursor: pointer; }
.tagline { margin: 0.2rem 0 0; opacity: 0.7; }
.reader-bar { display: flex; align-items: center; gap: 0.8rem; text-align: left; padding: 0.8rem 1rem;
  max-width: 640px; margin: 0 auto; }
.reader-bar h1 { font-size: 1.2rem; margin: 0; flex: 1; }
.back { text-decoration: none; font-size: 1rem; color: inherit; opacity: 0.8; }
/* Brand: logo + app name, clickable to home */
.reader-bar .brand { display: flex; align-items: center; gap: 0.5rem; text-decoration: none; color: inherit; flex: 1; }
.reader-bar .brand .site-logo { width: 36px; height: 36px; }
.reader-bar .brand-name { font-size: 1.1rem; font-weight: 700; white-space: nowrap; }
/* Story title below header */
.story-title { font-size: 1.5rem; margin: 0 0 0.3rem; text-align: center; }
/* Introduction heading (h2) */
.intro-heading { font-size: 1.2rem; margin: 1rem 0 0.5rem; text-align: center; color: inherit; }
.intro-heading[hidden] { display: none; }

.grid {
  display: grid; gap: 1rem; padding: 1rem;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  max-width: 900px; margin: 0 auto;
}
.card { text-decoration: none; color: inherit; border-radius: 12px; overflow: hidden;
  background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
@media (prefers-color-scheme: dark) { .card { background: #262640; } }
.card img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; }
.card-title { padding: 0.6rem; font-weight: 600; text-align: center; }
.card-cat { padding: 0 0.6rem 0.25rem; font-size: 0.8rem; text-align: center; opacity: 0.6; }
.card-dur { padding: 0 0.6rem 0.7rem; font-size: 0.8rem; text-align: center; opacity: 0.75; }
.card-plays { padding: 0 0.6rem 0.7rem; font-size: 0.8rem; text-align: center; opacity: 0.75; }
.searchbar { display: flex; gap: 0.6rem; max-width: 900px; margin: 0 auto; padding: 0.8rem 1rem 0; }
#search { flex: 1; min-width: 0; padding: 0.6rem 0.9rem; font-size: 1rem;
  border-radius: 999px; border: 1px solid #ddd; background: #fff; color: inherit; }
#cat-filter, #country-filter { padding: 0.6rem; font-size: 0.95rem; border-radius: 12px;
  border: 1px solid #ddd; background: #fff; color: inherit; }
@media (prefers-color-scheme: dark) {
  #search, #cat-filter, #country-filter { background: #262640; border-color: #444; }
}
.empty { text-align: center; opacity: 0.7; padding: 2rem 0; grid-column: 1 / -1; }

.reader { max-width: 640px; margin: 0 auto; padding: 0 1rem 3rem; }
.page-art img { width: 100%; border-radius: 12px; display: block; }
.img-placeholder { width: 100%; aspect-ratio: 4/3; border-radius: 12px;
  background: linear-gradient(135deg, #2a2a3e, #1a1a2e);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 0.5rem; color: #fff; text-align: center; padding: 1rem; }
.img-placeholder[hidden] { display: none; }
.img-placeholder-title { font-size: 1.5rem; font-weight: bold; }
.img-placeholder-sub { font-size: 1rem; opacity: 0.7; }
.page-text { font-size: 1.25rem; line-height: 1.8; margin: 1rem 0; }
#page-audio { width: 100%; margin-bottom: 0.5rem; }
.pager { display: flex; justify-content: space-between; align-items: center; margin: 1rem 0; }
.pager button { font-size: 1rem; padding: 0.6rem 1.2rem; border-radius: 10px; border: 1px solid #ccc;
  background: #fff; cursor: pointer; }
.pager button:disabled { opacity: 0.4; cursor: default; }
@media (prefers-color-scheme: dark) { .pager button { background: #262640; border-color: #444; color: #e8e8e8; } }
.origin-card { background: linear-gradient(135deg, #6a5acd, #8a6acf); color: #fff;
  border-radius: 12px; padding: 1rem 1.2rem; margin-top: 1rem; }
.origin-card h3 { margin: 0 0 0.5rem; }
.origin-card p { margin: 0.3rem 0; }
.next-story { text-align: center; margin: 2rem auto; max-width: 640px; }
.next-story h3 { margin-bottom: 1rem; color: #5a4a9a; }
html[data-theme="dark"] .next-story h3 { color: #b8a9e8; }
.next-story-card {
  display: flex;
  gap: 0.8rem;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  padding: 0.5rem 1rem 1rem;
  scrollbar-width: thin;
}
.next-story-card .card {
  flex: 0 0 160px;
  max-width: 160px;
  scroll-snap-align: start;
}
.byline { font-size: 0.85rem; color: #888; margin: 0.4rem 0 0; }
/* ── byline + level picker on the same row (picker right-aligned) ── */
.reader-meta { display: flex; justify-content: space-between; align-items: center; gap: 0.8rem; }
.reader-meta .byline { margin: 0.4rem 0; flex: 1; }
/* ── story level picker (above the illustration) ── */
.level-picker { display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; margin: 0.6rem 0; }
.level-picker[hidden] { display: none; }
.level-picker .lp-label { font-size: 0.85rem; color: #888; margin-right: 0.2rem; white-space: nowrap; }
.level-picker select { border: 1px solid #ccc; background: #fff; border-radius: 8px;
  padding: 0.3rem 0.6rem; font-size: 0.9rem; cursor: pointer; max-width: 100%; }
/* dark mode: native dropdown */
html[data-theme="dark"] .level-picker select { background: #262640; border-color: #444; color: #e8e8e8; }
html[data-theme="dark"] .level-picker select option { background: #262640; color: #e8e8e8; }
/* dark mode via system preference (no data-theme attribute set) */
@media (prefers-color-scheme: dark) {
  html:not([data-theme]) .level-picker select { background: #262640; border-color: #444; color: #e8e8e8; }
  html:not([data-theme]) .level-picker select option { background: #262640; color: #e8e8e8; }
}
.loading, .error { text-align: center; padding: 3rem 1rem; }
.error { color: #c0392b; }
/* ── public issue report (2-click) ── */
#report-btn { position: absolute; top: 1rem; right: 3.2rem; }
.report-panel { position: absolute; top: 3.2rem; right: 0.8rem; z-index: 50;
  background: #fff; border-radius: 14px; box-shadow: 0 6px 24px rgba(0,0,0,0.18);
  padding: 0.6rem; min-width: 180px; display: flex; flex-direction: column; gap: 0.3rem; }
/* display:flex above overrides the UA [hidden]{display:none} — re-hide explicitly */
.report-panel[hidden] { display: none; }
.report-title { margin: 0.2rem 0.4rem 0.4rem; font-size: 0.85rem; font-weight: 600; opacity: 0.75; }
.report-opt { text-align: left; padding: 0.55rem 0.7rem; font-size: 0.95rem;
  background: none; border: none; border-radius: 10px; cursor: pointer; color: inherit; }
.report-opt:hover { background: #f0ece2; }
.report-toast { position: fixed; left: 50%; bottom: 2rem; transform: translateX(-50%);
  background: rgba(0,0,0,0.8); color: #fff; padding: 0.7rem 1.2rem; border-radius: 24px;
  font-size: 0.9rem; z-index: 60; white-space: nowrap; }
@media (prefers-color-scheme: dark) {
  .report-panel { background: #262640; }
  .report-opt:hover { background: #35355a; }
}

/* ── V3: hidden settings rows must stay hidden despite .sheet label flex ── */
.sheet label[hidden] { display: none; }

/* ── V3: collection shelves on the list page ── */
.shelf { padding: 0 1rem; }
.shelf-title { font-size: 1.15rem; margin: 1.2rem 0 0.6rem; }
.shelf-row { display: flex; gap: 1rem; overflow-x: auto; padding-bottom: 0.75rem; }
.shelf-row .card { flex: 0 0 150px; }

/* ── V3: cover placeholder tile when the API returns cover: null ── */
.card-cover-placeholder { width: 100%; aspect-ratio: 1; display: flex;
  align-items: center; justify-content: center; padding: 0.6rem; box-sizing: border-box;
  background: linear-gradient(135deg, rgba(106,90,205,.18), rgba(138,106,207,.18)); }
.card-cover-ph-title { font-weight: 600; text-align: center; font-size: 0.95rem;
  opacity: 0.85; display: -webkit-box; -webkit-line-clamp: 4;
  -webkit-box-orient: vertical; overflow: hidden; }

/* ── V3: real logo in the site header (both pages) ── */
#site-h1 { display: flex; align-items: center; justify-content: center; gap: 0.6rem; }
.site-logo { width: 44px; height: 44px; border-radius: 50%; object-fit: cover; flex: none; }
.back { display: inline-flex; align-items: center; gap: 0.5rem; }
.back .site-logo { width: 36px; height: 36px; }

/* ── Kid-friendly big play controls ── */
.kid-pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin: 1.5rem 0 0.5rem;
  flex-wrap: nowrap;
  padding: 0 0.5rem;
}
.kid-pager .kid-play-btn {
  margin: 0 auto;
}
.kid-play-btn {
  width: 90px;
  height: 90px;
  border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.9);
  background: rgba(255,255,255,0.12);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s, background 0.15s;
  line-height: 1;
  padding: 0;
}
.kid-play-btn svg { display: block; }
.kid-play-btn:active { transform: scale(0.94); background: rgba(255,255,255,0.25); }
.kid-nav-btn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.5);
  background: rgba(255,255,255,0.06);
  color: #fff;
  font-size: 2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s, background 0.15s;
  line-height: 1;
}
.kid-nav-btn:active { transform: scale(0.94); background: rgba(255,255,255,0.18); }
.kid-nav-btn:disabled { opacity: 0.25; cursor: default; }
.kid-page-num {
  text-align: center;
  color: rgba(255,255,255,0.7);
  font-size: 0.95rem;
  margin-bottom: 1rem;
}

.kid-bottom-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 1rem;
  padding: 0 0.5rem;
  position: relative;
  min-height: 1.5rem;
}
.kid-bottom-row .kid-page-num {
  margin-bottom: 0;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
}
.kid-timer {
  font-size: 0.85rem;
  color: rgba(255,255,255,0.75);
  background: rgba(255,255,255,0.08);
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
  white-space: nowrap;
}

.kid-rect-btn {
  padding: 0.8rem 1.6rem;
  min-width: 110px;
  border-radius: 10px;
  border: 2px solid rgba(255,255,255,0.5);
  background: rgba(255,255,255,0.08);
  color: #fff;
  font-size: 1.05rem;
  cursor: pointer;
  transition: transform 0.15s, background 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.kid-rect-btn:active { transform: scale(0.96); background: rgba(255,255,255,0.18); }
.kid-rect-btn:disabled { opacity: 0.25; cursor: default; }

@media (max-width: 600px) {
  .kid-rect-btn {
    min-width: 0;
    padding: 0.6rem 0.8rem;
    font-size: 0.9rem;
  }
  .kid-pager .kid-play-btn {
    width: 72px;
    height: 72px;
    font-size: 1.6rem;
  }
  .kid-pager { gap: 0.6rem; padding: 0 0.3rem; }
  /* Sticky player bar at bottom on mobile: always visible, no scrolling needed */
  .kid-pager {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    margin: 0;
    padding: 0.6rem 0.8rem calc(0.6rem + env(safe-area-inset-bottom));
    background: rgba(20, 20, 35, 0.92);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
  }
  html[data-theme="light"] .kid-pager {
    background: rgba(250, 247, 240, 0.95);
    border-top-color: rgba(0, 0, 0, 0.12);
  }
  /* Make room for the fixed bar so content isn't hidden behind it */
  .reader { padding-bottom: 120px; }
  .kid-bottom-row { margin-bottom: 0.5rem; }
  /* Stack byline + level picker vertically on mobile (not side-by-side) */
  .reader-meta { flex-direction: column; align-items: stretch; gap: 0.2rem; }
  .reader-meta .byline { flex: none; }
  .level-picker { margin: 0.3rem 0; }
  /* Hide sticky player bar when settings sheet is open */
  body.sheet-open .kid-pager { display: none; }
}
@media (max-width: 600px) and (prefers-color-scheme: light) {
  html:not([data-theme="dark"]) .kid-pager {
    background: rgba(250, 247, 240, 0.95);
    border-top-color: rgba(0, 0, 0, 0.12);
  }
}

/* Sticky player bar at bottom on desktop too: Play/Pause always visible (Robin 2026-10-10) */
@media (min-width: 601px) {
  .kid-pager {
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: min(720px, 100%);
    z-index: 100;
    margin: 0;
    padding: 0.6rem 1rem calc(0.6rem + env(safe-area-inset-bottom));
    background: rgba(20, 20, 35, 0.92);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    border-left: 1px solid rgba(255, 255, 255, 0.1);
    border-right: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px 12px 0 0;
  }
  html[data-theme="light"] .kid-pager {
    background: rgba(250, 247, 240, 0.95);
    border-top-color: rgba(0, 0, 0, 0.12);
    border-left-color: rgba(0, 0, 0, 0.12);
    border-right-color: rgba(0, 0, 0, 0.12);
  }
  /* Make room for the fixed bar so content isn't hidden behind it */
  .reader { padding-bottom: 140px; }
  /* Hide sticky player bar when settings sheet is open */
  body.sheet-open .kid-pager { display: none; }
}

/* Light mode: dark buttons on light background */
html[data-theme="light"] .kid-play-btn {
  border-color: #2c2c2c;
  background: rgba(0,0,0,0.06);
  color: #2c2c2c;
}
html[data-theme="light"] .kid-play-btn:active { background: rgba(0,0,0,0.12); }
html[data-theme="light"] .kid-rect-btn {
  border-color: rgba(0,0,0,0.3);
  background: rgba(0,0,0,0.04);
  color: #2c2c2c;
}
html[data-theme="light"] .kid-rect-btn:active { background: rgba(0,0,0,0.1); }
html[data-theme="light"] .kid-timer {
  color: rgba(0,0,0,0.6);
  background: rgba(0,0,0,0.06);
}
html[data-theme="light"] .kid-page-num { color: rgba(0,0,0,0.6); }

/* System light mode (when theme="system" and OS is light): same as explicit light */
@media (prefers-color-scheme: light) {
  html:not([data-theme="dark"]) .kid-play-btn {
    border-color: #2c2c2c;
    background: rgba(0,0,0,0.06);
    color: #2c2c2c;
  }
  html:not([data-theme="dark"]) .kid-play-btn:active { background: rgba(0,0,0,0.12); }
  html:not([data-theme="dark"]) .kid-rect-btn {
    border-color: rgba(0,0,0,0.3);
    background: rgba(0,0,0,0.04);
    color: #2c2c2c;
  }
  html:not([data-theme="dark"]) .kid-rect-btn:active { background: rgba(0,0,0,0.1); }
  html:not([data-theme="dark"]) .kid-timer {
    color: rgba(0,0,0,0.6);
    background: rgba(0,0,0,0.06);
  }
  html:not([data-theme="dark"]) .kid-page-num { color: rgba(0,0,0,0.6); }
}

/* Story loading state */
.story-loading {
  text-align: center;
  padding: 4rem 1rem;
  color: var(--muted, #888);
}
.story-loading .spinner {
  width: 40px;
  height: 40px;
  margin: 0 auto 1rem;
  border: 4px solid rgba(128,128,128,0.2);
  border-top-color: #8b5cf6;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.load-more-btn {
  display: block;
  margin: 24px auto;
  padding: 12px 32px;
  font-size: 16px;
  border: 2px solid #888;
  border-radius: 24px;
  background: transparent;
  cursor: pointer;
}
.load-more-btn:hover { background: rgba(128,128,128,0.15); }
.page-footer { text-align: center; padding: 24px; border-top: 1px solid #ddd; margin-top: 32px; font-size: 14px; }
.page-footer a { margin: 0 8px; color: inherit; text-decoration: none; }
.page-footer a:hover { text-decoration: underline; }
.back-to-top-btn {
  position: fixed;
  right: 16px;
  bottom: 80px;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: rgba(91, 75, 138, 0.9);
  color: #fff;
  font-size: 24px;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
.back-to-top-btn:hover { background: rgba(91, 75, 138, 1); }

/* Share buttons (Robin 2026-10-11) */
.story-title-row { display: flex; align-items: center; gap: 0.5em; }
.story-title-row .story-title { flex: 1; margin: 0; }
.share-btn {
  flex: 0 0 auto; font-size: 1.4em; background: none; border: none;
  cursor: pointer; padding: 0.25em; border-radius: 0.4em; line-height: 1;
}
.share-btn:hover { background: rgba(127,127,127,.15); }
.share-btn-end {
  display: block; margin: 1em auto 0; padding: 0.7em 1.4em;
  font-size: 1em; border-radius: 999px; border: none; cursor: pointer;
  background: #6c5ce7; color: #fff;
}
.share-btn-end:hover { filter: brightness(1.1); }
.share-toast {
  position: fixed; left: 50%; bottom: 5.5em; transform: translateX(-50%);
  background: rgba(0,0,0,.8); color: #fff; padding: 0.6em 1.2em;
  border-radius: 999px; font-size: 0.95em; z-index: 9999; pointer-events: none;
}
