:root {
  --bg: #f4efe4;
  --ink: #1c1914;
  --muted: #5c564c;
  --paper: #fffdf8;
  --line: #d9d0c0;
  --accent: #b45309;
  --accent-ink: #fff7ed;
  --ready: #3f6212;
  --speaking: #b45309;
  --shadow: 0 18px 50px rgba(48, 32, 8, 0.1);
  color-scheme: light;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

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

body {
  font-family: "Segoe UI", "Noto Sans", "Noto Sans CJK TC", "Noto Sans Arabic",
    "Hiragino Sans", "Apple SD Gothic Neo", system-ui, sans-serif;
  background:
    radial-gradient(1200px 500px at 10% -10%, #fde68a55, transparent 55%),
    radial-gradient(900px 400px at 100% 0%, #fdba7455, transparent 50%),
    var(--bg);
  color: var(--ink);
  line-height: 1.5;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

img {
  max-width: 100%;
  height: auto;
}

.wrap {
  width: min(1080px, calc(100% - 32px));
  margin: 0 auto;
  padding: max(24px, env(safe-area-inset-top)) 0
    max(120px, calc(96px + env(safe-area-inset-bottom)));
}

.view[hidden] {
  display: none !important;
}

.view-reader:not([hidden]) {
  display: block;
}

[hidden] {
  display: none !important;
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
  flex: 1 1 auto;
}

.reader-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.reader-status {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  font-size: 0.92rem;
  font-weight: 650;
  color: var(--ready);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 16px;
}

.page-label {
  margin: 0;
  min-width: 7rem;
  text-align: center;
  font-size: 0.88rem;
  font-weight: 650;
  color: var(--muted);
}

.toc-modal-card {
  width: min(440px, 100%);
}

.toc-nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: min(60dvh, 420px);
  overflow: auto;
}

.account {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-chip {
  max-width: 9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.78rem;
  font-weight: 650;
  color: var(--muted);
}

.brand-link {
  flex-shrink: 0;
  display: block;
  line-height: 0;
  border-radius: 18px;
}

.brand-logo {
  display: block;
  width: clamp(52px, 10vw, 72px);
  height: auto;
  aspect-ratio: 1;
  object-fit: contain;
  object-position: center;
  background: transparent;
}

.toolbar {
  display: flex;
  align-items: stretch;
  gap: 8px;
  flex-shrink: 0;
}

.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;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px 16px;
}

.settings-grid .field {
  min-width: 0;
}

.settings-grid select {
  width: 100%;
  min-width: 0;
}

.icon-btn,
label.icon-btn {
  min-width: 44px;
  width: 44px;
  height: 44px;
  min-height: 44px;
  max-width: none;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--paper);
  color: var(--ink);
  padding: 0;
  display: inline-flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 0;
  box-shadow: 0 1px 0 rgba(48, 32, 8, 0.04);
  cursor: pointer;
}

.file-icon {
  margin: 0;
  position: relative;
}

.file-icon input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.icon-btn svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn span {
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1.2;
  color: var(--muted);
  text-align: center;
}

.icon-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.icon-btn[aria-expanded="true"],
.icon-btn[aria-current="page"] {
  border-color: var(--accent);
  color: var(--accent);
}

.icon-btn[aria-expanded="true"] span {
  color: var(--accent);
}

h1 {
  font-size: clamp(1.35rem, 3.6vw, 2.4rem);
  font-weight: 720;
  letter-spacing: -0.03em;
  margin: 0;
  line-height: 1.15;
  min-width: 0;
  overflow-wrap: anywhere;
}

.field label {
  font-size: 0.8rem;
  font-weight: 650;
  color: var(--muted);
  letter-spacing: 0.02em;
}

select,
textarea,
button {
  font: inherit;
}

select {
  appearance: none;
  background: var(--paper)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%231c1914' stroke-width='1.6'/%3E%3C/svg%3E")
    no-repeat right 12px center;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 34px 8px 12px;
  min-height: 44px;
  color: var(--ink);
}

.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 22px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

textarea {
  width: 100%;
  min-height: 220px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px 16px;
  background: #fff;
  color: var(--ink);
}

textarea:focus,
select:focus,
button:focus-visible {
  outline: 3px solid #fbbf2488;
  outline-offset: 1px;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

.modal[hidden] {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(28, 25, 20, 0.32);
}

.modal-card {
  position: relative;
  z-index: 1;
  width: min(400px, 100%);
  max-height: min(92dvh, 100%);
  overflow: auto;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 18px 18px 20px;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.modal-head h2 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
}

#settings-close {
  width: 44px;
  height: 44px;
  border-radius: 999px;
  background: #ece7dc;
  color: var(--ink);
  font-size: 1.25rem;
  line-height: 1;
  padding: 0;
}

.panel .field + .field {
  margin-top: 14px;
}

.player-dock {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 30;
  width: min(640px, calc(100% - 16px));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 12px 8px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
}

.player-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.player-dock .icon-btn {
  flex: 0 0 44px;
}

.player-status {
  width: 100%;
  min-width: 0;
  margin: 0;
  font-size: 0.82rem;
  font-weight: 650;
  color: var(--ready);
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.play-btn,
.player-dock[data-state="speaking"] #speak {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
}

.player-dock[data-state="speaking"] {
  border-color: var(--accent);
}

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.page-head h2 {
  margin: 0;
  font-size: 1.15rem;
}

.search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1 1 auto;
  max-width: 280px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 12px;
  background: #fff;
  min-height: 44px;
}

.search-wrap svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
}

.search-wrap input {
  border: 0;
  background: transparent;
  width: 100%;
  font: inherit;
  color: var(--ink);
  min-height: 40px;
}

.search-wrap input:focus {
  outline: none;
}

.library-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.lib-row {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 8px 10px;
}

.lib-open {
  flex: 1 1 auto;
  min-width: 0;
  text-align: start;
  background: transparent;
  color: var(--ink);
  border-radius: 8px;
  padding: 6px 8px;
  font-weight: 650;
}

.lib-title-input {
  flex: 1 1 auto;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  min-height: 40px;
  font: inherit;
}

#reader-hint {
  margin-top: 0;
}

button {
  border: 0;
  border-radius: 999px;
  padding: 10px 18px;
  min-height: 44px;
  cursor: pointer;
  font-weight: 650;
}

#stop {
  background: #ece7dc;
  color: var(--ink);
}

#status {
  margin: 16px 0 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--ready);
}

#status[data-state="speaking"] {
  color: var(--speaking);
}

#status[data-state="empty"],
#status[data-state="stopped"] {
  color: var(--muted);
}

.toc {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 16px 14px;
  position: sticky;
  top: 16px;
}

.toc h2 {
  margin: 0 0 10px;
  font-size: 0.82rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

#toc,
#library-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 70vh;
  overflow: auto;
}

#library-list {
  max-height: 28vh;
}

#library-label {
  margin: 16px 0 10px;
  font-size: 0.82rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

#toc button,
#library-list button {
  background: transparent;
  color: var(--ink);
  border-radius: 8px;
  padding: 6px 8px;
  text-align: start;
  font-weight: 600;
  font-size: 0.88rem;
}

#toc button[aria-current="true"] {
  background: #fde68a66;
}

.import-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.file-btn {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px;
  min-height: 44px;
  font-size: 0.82rem;
  font-weight: 650;
  cursor: pointer;
  background: #fff;
}

.file-btn input {
  display: none;
}

#text {
  min-height: 120px;
}

.novel {
  margin-top: 14px;
  max-width: 42rem;
  margin-inline: auto;
  line-height: 1.85;
  font-size: 1.08rem;
}

.novel h3 {
  font-size: 1.15rem;
  margin: 1.4em 0 0.6em;
}

.novel p {
  margin: 0 0 1em;
  padding: 0.15em 0.4em;
  border-radius: 8px;
  border-left: 3px solid transparent;
}

.novel p.active {
  background: #fde68a55;
  border-left-color: var(--accent);
}

.empty-book {
  color: var(--muted);
}

html[data-font="serif"] .novel {
  font-family: "Iwanami Mincho", "Source Han Serif", "Noto Serif CJK TC",
    "Songti TC", Georgia, "Times New Roman", serif;
}

html[data-theme="dark"] {
  --bg: #161410;
  --ink: #f5efe4;
  --muted: #b7ad9e;
  --paper: #221e18;
  --line: #3a342b;
  --accent: #f59e0b;
  --accent-ink: #1c1408;
  --ready: #a3e635;
}

html[data-theme="sepia"] {
  --bg: #efe4c8;
  --ink: #3f2f16;
  --muted: #7a6542;
  --paper: #f7eed6;
  --line: #e0d0a8;
}

html[data-theme="dark"] .file-btn,
html[data-theme="dark"] textarea,
html[data-theme="dark"] select {
  background: #2a251e;
  color: var(--ink);
}

html[data-theme="dark"] #stop,
html[data-theme="dark"] #prev,
html[data-theme="dark"] #next,
html[data-theme="dark"] #pause,
html[data-theme="dark"] #settings-close,
html[data-theme="dark"] .icon-btn {
  background: #2a251e;
  color: var(--ink);
}

html[data-theme="dark"] .play-btn {
  background: var(--accent);
  color: var(--accent-ink);
}

html[data-theme="dark"] .search-wrap {
  background: #2a251e;
}

.hint {
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.88rem;
}

html[dir="rtl"] select {
  background-position: left 12px center;
  padding: 8px 12px 8px 34px;
}

@media (max-width: 860px) {
  .wrap {
    width: min(1080px, calc(100% - 24px));
  }

  .view-reader:not([hidden]) {
    display: block;
  }

  .toc {
    position: static;
  }

  #toc {
    max-height: 180px;
    flex-direction: row;
    flex-wrap: wrap;
  }

  .brand-logo {
    width: clamp(48px, 12vw, 64px);
  }
}

@media (max-width: 640px) {
  .wrap {
    padding-top: max(16px, env(safe-area-inset-top));
  }

  .top {
    align-items: center;
    gap: 10px;
  }

  .brand {
    gap: 10px;
  }

  .toolbar {
    justify-content: flex-end;
    flex-shrink: 0;
  }

  .icon-btn,
  label.icon-btn {
    min-width: 44px;
    width: 44px;
  }

  .user-chip {
    max-width: 5.5rem;
  }

  .page-head {
    flex-direction: column;
    align-items: stretch;
  }

  .search-wrap {
    max-width: none;
  }

  .settings-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px 10px;
  }

  .settings-grid .field {
    gap: 4px;
  }

  .settings-grid .field label {
    font-size: 0.72rem;
  }

  .settings-grid select {
    min-height: 38px;
    padding: 6px 28px 6px 8px;
    font-size: 0.88rem;
  }

  html[dir="rtl"] .settings-grid select {
    padding: 6px 8px 6px 28px;
  }

  .modal {
    align-items: flex-end;
    padding: 0;
  }

  .modal-card {
    width: 100%;
    border-radius: 20px 20px 0 0;
    max-height: min(88dvh, 100%);
    overflow: auto;
    padding: 12px 12px max(16px, env(safe-area-inset-bottom));
  }

  .modal-head {
    margin-bottom: 10px;
  }

  #voice-hint {
    margin-top: 8px;
    font-size: 0.78rem;
  }

  .import-row {
    flex-wrap: wrap;
  }
}

@media (max-width: 420px) {
  .wrap {
    width: calc(100% - 16px);
  }

  .card,
  .toc {
    padding: 14px;
    border-radius: 16px;
  }

  .brand-logo {
    width: 48px;
  }

  h1 {
    font-size: clamp(1.15rem, 6.4vw, 1.55rem);
  }

  .player-dock {
    width: calc(100% - 12px);
    padding: 8px 8px 6px;
  }

  .player-status {
    display: block;
    font-size: 0.75rem;
  }
}

@media (max-height: 520px) and (orientation: landscape) {
  .modal {
    align-items: stretch;
  }

  .modal-card {
    max-height: 100dvh;
    border-radius: 0;
    padding: 8px 12px max(8px, env(safe-area-inset-bottom));
  }

  .wrap {
    padding-top: 12px;
    padding-bottom: 16px;
  }

  .brand-logo {
    width: 40px;
  }

  h1 {
    font-size: 1.2rem;
  }

  textarea {
    min-height: 96px;
  }
}
