:root {
  --paper: #fffdf8;
  --cream: #f7f1e5;
  --cream-2: #fff8ea;
  --ink: #24344f;
  --muted: #68748a;
  --line: #e7dfd0;
  --sky: #5f86d7;
  --sky-soft: #eaf1ff;
  --mint: #55b8a6;
  --mint-soft: #e7f7f3;
  --coral: #f47c67;
  --coral-soft: #fff0eb;
  --sun: #ffd35b;
  --shadow: 0 12px 32px rgba(43, 57, 82, .12);
  --radius-xl: 28px;
  --radius-lg: 20px;
  --radius-md: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 4%, rgba(255, 211, 91, .20), transparent 24rem),
    radial-gradient(circle at 92% 90%, rgba(85, 184, 166, .16), transparent 26rem),
    var(--cream);
  font-family: "Nunito", "Avenir Next", "Segoe UI", "PingFang SC", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, select, input { font: inherit; }
button, a, select, input[type="range"] { -webkit-tap-highlight-color: transparent; }
button:focus-visible, a:focus-visible, select:focus-visible, input:focus-visible {
  outline: 4px solid rgba(95, 134, 215, .28);
  outline-offset: 2px;
}
button { border: 0; cursor: pointer; color: inherit; }

.app-shell {
  width: min(1680px, calc(100% - 24px));
  height: calc(100vh - 24px);
  min-height: 650px;
  margin: 12px auto;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  background: var(--paper);
  border: 1px solid rgba(86, 75, 55, .10);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow);
}

.topbar {
  min-height: 66px;
  display: grid;
  grid-template-columns: minmax(120px, 1fr) minmax(0, 2fr) minmax(120px, 1fr);
  align-items: center;
  gap: 16px;
  padding: 10px 18px;
  background: rgba(255, 253, 248, .96);
  border-bottom: 1px solid var(--line);
  z-index: 4;
}
.brand-link, .settings-button {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  width: max-content;
  padding: 0 14px;
  border-radius: 14px;
  color: var(--ink);
  background: var(--cream-2);
  text-decoration: none;
  font-weight: 800;
}
.brand-link:hover, .settings-button:hover { background: #ffefc8; }
.book-heading { min-width: 0; text-align: center; }
.book-heading small {
  display: block;
  color: var(--mint);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: .12em;
  text-transform: uppercase;
}
.book-heading h1 {
  margin: 2px 0 0;
  overflow: hidden;
  color: var(--ink);
  font-size: clamp(17px, 1.7vw, 23px);
  line-height: 1.15;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.settings-wrap { position: relative; justify-self: end; }
.settings-button { justify-self: end; }
.settings-panel {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  z-index: 20;
  width: min(390px, calc(100vw - 24px));
  max-height: min(78vh, 700px);
  overflow-y: auto;
  padding: 16px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}
.settings-panel[hidden] { display: none; }
.setting-row {
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 15px;
  font-weight: 800;
}
.setting-row + .setting-row { border-top: 1px solid var(--line); }
.setting-row select {
  min-height: 38px;
  padding: 0 10px;
  color: var(--ink);
  background: white;
  border: 2px solid var(--line);
  border-radius: 10px;
  font-weight: 800;
}
.cloud-settings {
  margin-top: 8px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}
.cloud-settings summary {
  min-height: 42px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  color: #315f94;
  font-weight: 900;
  list-style: none;
}
.cloud-settings summary::-webkit-details-marker { display: none; }
.cloud-settings summary::after { content: "＋"; font-size: 20px; }
.cloud-settings[open] summary::after { content: "－"; }
.cloud-badge {
  margin-left: auto;
  margin-right: 10px;
  padding: 4px 8px;
  color: #7a5b15;
  background: #fff1c9;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 900;
}
.cloud-badge.ready { color: #24685d; background: var(--mint-soft); }
.cloud-form { display: grid; gap: 11px; padding: 7px 0 3px; }
.cloud-group {
  display: grid;
  gap: 9px;
  padding: 12px;
  background: #fbfaf6;
  border: 1px solid var(--line);
  border-radius: 14px;
}
.cloud-group-title { display: flex; align-items: center; justify-content: space-between; font-size: 14px; font-weight: 900; }
.cloud-field { display: grid; gap: 5px; color: #536077; font-size: 12px; font-weight: 850; }
.cloud-field input, .cloud-field select {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  color: var(--ink);
  background: white;
  border: 2px solid var(--line);
  border-radius: 10px;
  font-size: 13px;
  font-weight: 700;
}
.cloud-field input:focus, .cloud-field select:focus { border-color: #98b4e4; }
.provider-hint { margin: 0; color: #66758b; font-size: 11px; line-height: 1.45; }
.cloud-note { margin: 0; color: #8a6640; font-size: 11px; line-height: 1.5; }
.cloud-actions { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.cloud-save, .cloud-clear {
  min-height: 42px;
  padding: 0 13px;
  border-radius: 11px;
  font-size: 13px;
  font-weight: 900;
}
.cloud-save { color: white; background: var(--sky); }
.cloud-save:hover { background: #4d75c4; }
.cloud-clear { color: #7b5960; background: #f5ecec; }
.cloud-status { min-height: 18px; color: var(--muted); font-size: 11px; line-height: 1.4; }
.switch { position: relative; width: 48px; height: 28px; }
.switch input { width: 1px; height: 1px; opacity: 0; }
.switch span {
  position: absolute; inset: 0;
  background: #ccd3dc;
  border-radius: 99px;
  transition: .18s ease;
}
.switch span::after {
  content: "";
  position: absolute;
  width: 22px; height: 22px;
  left: 3px; top: 3px;
  background: white;
  border-radius: 50%;
  box-shadow: 0 2px 5px rgba(0,0,0,.18);
  transition: .18s ease;
}
.switch input:checked + span { background: var(--mint); }
.switch input:checked + span::after { transform: translateX(20px); }

.reader-grid {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(420px, 1.14fr) minmax(400px, .86fr);
  overflow: hidden;
}
.visual-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(16px, 2.2vw, 34px);
  overflow: hidden;
  background:
    radial-gradient(circle at 18px 18px, rgba(95, 134, 215, .10) 2px, transparent 2.5px) 0 0 / 36px 36px,
    var(--sky-soft);
  border-right: 1px solid #dce6f8;
}
.image-stage {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.book-image {
  display: block;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 18px;
  background: white;
  box-shadow: 0 14px 30px rgba(41, 63, 96, .16);
}
.page-badge {
  position: absolute;
  top: 18px;
  right: 18px;
  min-width: 66px;
  padding: 7px 12px;
  text-align: center;
  color: #43526a;
  background: rgba(255, 255, 255, .92);
  border: 1px solid rgba(95, 134, 215, .16);
  border-radius: 999px;
  box-shadow: 0 5px 15px rgba(42, 57, 83, .09);
  font-size: 14px;
  font-weight: 900;
}

.reading-panel {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: clamp(18px, 2vw, 30px);
  overflow: hidden;
  background: var(--paper);
}
.reading-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.reading-title h2 { margin: 0; font-size: clamp(19px, 1.7vw, 24px); }
.reading-title p { margin: 3px 0 0; color: var(--muted); font-size: 13px; font-weight: 700; }
.tip-chip {
  flex: 0 0 auto;
  padding: 7px 11px;
  color: #2d6f64;
  background: var(--mint-soft);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 900;
}
.subtitle-scroll {
  min-height: 0;
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  padding: 3px 8px 10px 3px;
}
.subtitle-scroll::-webkit-scrollbar { width: 9px; }
.subtitle-scroll::-webkit-scrollbar-thumb { background: #cbd8ef; border-radius: 99px; }
.subtitle-text { display: grid; gap: 11px; }
.sentence-card {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  align-items: start;
  gap: 12px;
  padding: 13px 15px 13px 11px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: 0 4px 12px rgba(53, 64, 85, .055);
  transition: border-color .16s ease, background .16s ease, transform .16s ease;
}
.sentence-card:hover { border-color: #c9d8f1; background: #fdfefe; }
.sentence-card.is-current { border-color: #9eb8e8; background: #f7faff; }
.sentence-play {
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  color: #345f9d;
  background: var(--sky-soft);
  border-radius: 13px;
  font-size: 18px;
  font-weight: 900;
}
.sentence-play:hover { color: white; background: var(--sky); }
.sentence-play.is-playing { color: white; background: var(--coral); }
.sentence-content {
  min-width: 0;
  padding-top: 3px;
  color: #293a55;
  font-family: "Atkinson Hyperlegible", "Avenir Next", "Segoe UI", sans-serif;
  font-size: clamp(21px, 1.7vw, 29px);
  font-weight: 650;
  line-height: 1.62;
  overflow-wrap: anywhere;
}
.word {
  display: inline;
  padding: 1px 2px 2px;
  border-radius: 6px;
  cursor: pointer;
  transition: color .13s ease, background .13s ease, box-shadow .13s ease;
}
.word:hover { color: #2f5f9e; background: var(--sky-soft); }
.word.active {
  color: #203049;
  background: var(--sun);
  box-shadow: 0 2px 0 rgba(208, 156, 22, .22);
}

.player-dock {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) auto minmax(180px, 1fr);
  align-items: center;
  gap: 20px;
  min-height: 106px;
  padding: 12px 22px 16px;
  background: #fffdfa;
  border-top: 1px solid var(--line);
  z-index: 5;
}
.progress-area { min-width: 0; }
.progress-labels {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}
.seek {
  width: 100%;
  height: 8px;
  accent-color: var(--sky);
  cursor: pointer;
}
.main-controls { display: flex; align-items: center; justify-content: center; gap: 10px; }
.control-button {
  min-width: 58px;
  min-height: 50px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 14px;
  background: var(--cream-2);
  border-radius: 15px;
  font-weight: 900;
}
.control-button:hover:not(:disabled) { background: #ffedc3; transform: translateY(-1px); }
.control-button:disabled { opacity: .38; cursor: not-allowed; }
.play-button {
  min-width: 132px;
  min-height: 58px;
  color: white;
  background: var(--coral);
  box-shadow: 0 8px 18px rgba(244, 124, 103, .25);
  font-size: 17px;
}
.play-button:hover:not(:disabled) { background: #e96d58; }
.play-button.is-playing { background: var(--mint); box-shadow: 0 8px 18px rgba(85, 184, 166, .25); }
.page-steps {
  min-width: 0;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 6px;
  overflow: hidden;
}
.page-dot {
  width: 11px; height: 11px;
  flex: 0 0 auto;
  padding: 0;
  background: #d6dbe3;
  border-radius: 99px;
  transition: width .16s ease, background .16s ease;
}
.page-dot:hover { background: #9db2d8; }
.page-dot.active { width: 28px; background: var(--sky); }

.word-popover[hidden] { display: none; }
.word-popover {
  position: absolute;
  left: clamp(18px, 8%, 90px);
  bottom: 18px;
  z-index: 12;
  width: min(450px, calc(100% - 36px));
  max-height: min(54%, 440px);
  overflow-y: auto;
  padding: 20px;
  background: rgba(255, 253, 248, .98);
  border: 2px solid rgba(95, 134, 215, .23);
  border-radius: 20px;
  box-shadow: 0 15px 38px rgba(39, 55, 82, .23);
}
.word-top { display: flex; align-items: flex-start; gap: 10px; }
.word-name { min-width: 0; flex: 1; }
.word-name h3 { margin: 0; font-size: clamp(27px, 3vw, 36px); line-height: 1.1; }
.word-ipa { min-height: 24px; margin-top: 6px; color: #4c6fa8; font: 19px Georgia, serif; }
.word-actions { display: flex; gap: 7px; }
.round-action, .close-word {
  width: 43px; height: 43px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border-radius: 13px;
  font-size: 19px;
}
.round-action { color: white; background: var(--sky); }
.round-action.explain { background: var(--mint); }
.close-word { color: #59667a; background: #f0ece4; }
.word-meta { margin-top: 2px; color: var(--muted); font-size: 13px; font-weight: 800; }
.word-definition, .word-explanation {
  margin-top: 14px;
  padding: 13px 14px;
  border-radius: 13px;
  line-height: 1.55;
}
.word-definition { background: var(--sky-soft); }
.word-explanation { background: var(--mint-soft); white-space: pre-wrap; }
.word-explanation[hidden] { display: none; }
.word-status { min-height: 18px; margin-top: 8px; color: var(--muted); font-size: 12px; }

.empty-reader {
  grid-column: 1 / -1;
  display: grid;
  place-items: center;
  padding: 40px;
  text-align: center;
}
.empty-reader h2 { margin-bottom: 6px; }
.empty-reader p { color: var(--muted); }
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0);
  white-space: nowrap; border: 0;
}

@media (max-width: 980px) {
  .app-shell { height: auto; min-height: calc(100vh - 16px); width: calc(100% - 16px); margin: 8px; }
  .reader-grid { grid-template-columns: 1fr; overflow: visible; }
  .visual-panel { min-height: 50vh; max-height: 62vh; border-right: 0; border-bottom: 1px solid #dce6f8; }
  .reading-panel { min-height: 440px; max-height: 62vh; }
  .player-dock { position: sticky; bottom: 0; grid-template-columns: 1fr; gap: 10px; padding: 10px 14px; }
  .progress-area { order: 1; }
  .main-controls { order: 2; }
  .page-steps { display: none; }
  .word-popover { position: fixed; left: 12px; right: 12px; bottom: 126px; width: auto; max-height: 45vh; }
}
@media (max-width: 640px) {
  .topbar { grid-template-columns: auto minmax(0, 1fr) auto; gap: 7px; padding: 8px; }
  .brand-link, .settings-button { width: 44px; padding: 0; justify-content: center; }
  .brand-link span, .settings-button span { display: none; }
  .reader-grid { display: block; }
  .visual-panel { min-height: 42vh; max-height: 50vh; padding: 12px; }
  .page-badge { top: 10px; right: 10px; }
  .reading-panel { min-height: 410px; max-height: none; padding: 15px 12px; }
  .reading-title { align-items: flex-start; }
  .tip-chip { display: none; }
  .sentence-card { grid-template-columns: 40px minmax(0, 1fr); gap: 9px; padding: 11px 10px 11px 9px; }
  .sentence-play { width: 40px; height: 40px; }
  .sentence-content { font-size: 21px; }
  .control-button { min-width: 48px; min-height: 46px; padding: 0 10px; }
  .control-button .label { display: none; }
  .play-button { min-width: 116px; min-height: 54px; }
  .play-button .label { display: inline; }
  .word-popover { bottom: 116px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; }
}
