:root {
  --font-family: "BIZ UDPGothic", "Hiragino Kaku Gothic ProN", "Hiragino Sans",
    "Yu Gothic", "Meiryo", sans-serif;
  --font-size: 22px;
  --line-height: 1.8;

  --bg: #ffffff;
  --surface: #f8fafc;
  --surface-hover: #eef2f7;
  --text: #111827;
  --muted-text: #4b5563;
  --border: #cbd5e1;
  --accent: #1e3a8a;
  --accent-text: #ffffff;
  --highlight-bg: #dbeafe;
  --highlight-text: #111827;
  --highlight-underline: #1e3a8a;
  --focus-ring: #1e3a8a;
  --shadow: 0 4px 16px rgba(15, 23, 42, 0.08);
  --btn-border-width: 1px;
}

html[data-theme="standard"] {
  --bg: #ffffff;
  --surface: #f8fafc;
  --surface-hover: #eef2f7;
  --text: #111827;
  --muted-text: #4b5563;
  --border: #cbd5e1;
  --accent: #1e3a8a;
  --accent-text: #ffffff;
  --highlight-bg: #dbeafe;
  --highlight-text: #111827;
  --highlight-underline: #1e3a8a;
  --focus-ring: #1e3a8a;
}

html[data-theme="cream"] {
  --bg: #fbf7ee;
  --surface: #f3ecdc;
  --surface-hover: #e8dec8;
  --text: #1f1912;
  --muted-text: #5c4f41;
  --border: #d5c6ad;
  --accent: #7c2d12;
  --accent-text: #ffffff;
  --highlight-bg: #eadbc6;
  --highlight-text: #1f1912;
  --highlight-underline: #7c2d12;
  --focus-ring: #7c2d12;
}

html[data-theme="dark"] {
  --bg: #121212;
  --surface: #1e1e1e;
  --surface-hover: #2c2c2c;
  --text: #f9fafb;
  --muted-text: #d1d5db;
  --border: #404040;
  --accent: #93c5fd;
  --accent-text: #0f172a;
  --highlight-bg: #1e3a8a;
  --highlight-text: #f9fafb;
  --highlight-underline: #93c5fd;
  --focus-ring: #93c5fd;
  --shadow: 0 4px 18px rgba(0, 0, 0, 0.45);
}

html[data-theme="high-contrast"] {
  --bg: #000000;
  --surface: #121212;
  --surface-hover: #242424;
  --text: #ffffff;
  --muted-text: #ffeb3b;
  --border: #ffeb3b;
  --accent: #ffeb3b;
  --accent-text: #000000;
  --highlight-bg: #ffeb3b;
  --highlight-text: #000000;
  --highlight-underline: #000000;
  --focus-ring: #ffeb3b;
  --shadow: none;
  --btn-border-width: 2px;
}

html[data-theme="highlight"] {
  --bg: #ffffff;
  --surface: #f8fafc;
  --surface-hover: #f1f5f9;
  --text: #111827;
  --muted-text: #4b5563;
  --border: #cbd5e1;
  --accent: #1e3a8a;
  --accent-text: #ffffff;
  --highlight-bg: #fef3c7;
  --highlight-text: #111827;
  --highlight-underline: #b45309;
  --focus-ring: #1e3a8a;
}

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

html,
body {
  margin: 0;
  padding: 0;
  max-width: 100%;
  overflow-x: clip;
  background-color: var(--bg);
  color: var(--text);
  font-family: var(--font-family);
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

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

.skip-link {
  position: absolute;
  left: 8px;
  top: -120px;
  z-index: 200;
  background: var(--accent);
  color: var(--accent-text);
  padding: 12px 16px;
  border-radius: 8px;
  font-weight: 700;
  text-decoration: none;
  min-height: 48px;
  display: inline-flex;
  align-items: center;
}

.skip-link:focus {
  top: 8px;
}

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

button,
select,
summary,
a {
  font-family: inherit;
  color: inherit;
}

button,
select,
summary {
  min-width: 48px;
  min-height: 48px;
  cursor: pointer;
}

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

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  background-color: var(--surface);
  border-bottom: var(--btn-border-width) solid var(--border);
  padding: 8px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

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

.book-title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 700;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.header-page-label {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.875rem;
  font-weight: 700;
  background-color: var(--bg);
  border: var(--btn-border-width) solid var(--border);
  white-space: nowrap;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.header-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 10px;
  background-color: var(--bg);
  border: var(--btn-border-width) solid var(--border);
  font-size: 0.95rem;
  font-weight: 700;
  white-space: nowrap;
}

.header-btn:hover:not(:disabled) {
  background-color: var(--surface-hover);
}

/* Main Reader Area */
.reader-main {
  flex: 1 0 auto;
  width: 100%;
  max-width: 1320px;
  margin: 0 auto;
  padding: 16px 16px 32px;
}

.reader-main:focus {
  outline: none;
}

.notice-banner {
  margin-bottom: 16px;
  padding: 14px 16px;
  border-radius: 12px;
  border: 2px solid var(--accent);
  background-color: var(--surface);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.notice-banner[hidden] {
  display: none;
}

.autoplay-message {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.fallback-play-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  border-radius: 12px;
  border: 2px solid var(--accent);
  background-color: var(--accent);
  color: var(--accent-text);
  font-size: 1.05rem;
  font-weight: 700;
}

.book-stage {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}

@media (min-width: 960px) {
  html[data-view-mode="both"]:not([data-font-size="34"]) .book-stage {
    grid-template-columns: minmax(0, 1.1fr) minmax(0, 0.9fr);
    gap: 32px;
  }
}

html[data-view-mode="text"] .illustration-pane {
  display: none;
}

html[data-view-mode="text"] .text-pane {
  margin-inline: auto;
  width: 100%;
}

html[data-view-mode="image"] .text-pane {
  display: none;
}

html[data-view-mode="image"] .illustration-pane {
  max-width: 1040px;
  margin-inline: auto;
  width: 100%;
}

html[data-view-mode="image"] .page-image {
  max-height: 70dvh;
}

/* Illustration Pane */
.illustration-pane {
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.image-frame {
  background-color: var(--surface);
  border: var(--btn-border-width) solid var(--border);
  border-radius: 14px;
  padding: 10px;
  box-shadow: var(--shadow);
  display: flex;
  justify-content: center;
  align-items: center;
}

.page-image {
  display: block;
  width: 100%;
  height: auto;
  max-height: 62dvh;
  object-fit: contain;
  border-radius: 8px;
}

.alt-details {
  background-color: var(--surface);
  border: var(--btn-border-width) solid var(--border);
  border-radius: 10px;
  padding: 4px 12px;
}

.alt-summary {
  display: flex;
  align-items: center;
  font-size: 0.95rem;
  font-weight: 700;
}

.alt-caption {
  margin: 0 0 10px;
  font-size: 0.95rem;
  line-height: 1.7;
  color: var(--text);
}

/* Text Pane */
.text-pane {
  width: 100%;
  max-width: min(100%, 38em);
  margin-inline: auto;
}

.page-heading {
  margin: 0 0 14px;
  font-size: 1rem;
  font-weight: 700;
  color: var(--muted-text);
  border-bottom: var(--btn-border-width) solid var(--border);
  padding-bottom: 6px;
}

.audio-status-note {
  margin: 0 0 14px;
  padding: 10px 12px;
  border-radius: 8px;
  background-color: var(--surface);
  border: var(--btn-border-width) solid var(--border);
  font-size: 0.95rem;
}

.cue-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background-color: var(--surface);
  border: var(--btn-border-width) solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
}

.cue-item {
  width: 100%;
  text-align: left;
  background-color: transparent;
  color: var(--text);
  border: none;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: var(--font-size);
  line-height: var(--line-height);
  overflow-wrap: anywhere;
  transition: background-color 140ms ease;
}

.cue-item:hover {
  background-color: var(--surface-hover);
}

.cue-item.is-active {
  background-color: var(--highlight-bg);
  color: var(--highlight-text);
  text-decoration: underline;
  text-decoration-color: var(--highlight-underline);
  text-decoration-thickness: 3px;
  text-underline-offset: 0.25em;
  font-weight: 700;
}

html[data-theme="highlight"] .cue-item.is-active {
  text-decoration: none;
}

.cue-item ruby {
  ruby-position: over;
}

.cue-item rt {
  font-size: 0.55em;
}

/* Controls Footer */
.controls-bar {
  position: sticky;
  bottom: 0;
  z-index: 40;
  background-color: var(--surface);
  border-top: var(--btn-border-width) solid var(--border);
  box-shadow: 0 -2px 12px rgba(15, 23, 42, 0.08);
}

.progress-track {
  width: 100%;
  height: 5px;
  background-color: var(--border);
}

.progress-fill {
  width: 0%;
  height: 100%;
  background-color: var(--accent);
}

.controls-inner {
  max-width: 1320px;
  margin: 0 auto;
  padding: 8px 12px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.ctrl-btn,
.ctrl-select {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 10px;
  background-color: var(--bg);
  border: var(--btn-border-width) solid var(--border);
  font-size: 0.92rem;
  font-weight: 700;
  white-space: nowrap;
}

.ctrl-btn:hover:not(:disabled),
.ctrl-select:hover:not(:disabled) {
  background-color: var(--surface-hover);
}

.ctrl-btn-primary {
  background-color: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.ctrl-btn-primary:hover:not(:disabled) {
  filter: brightness(1.08);
  background-color: var(--accent);
}

.ctrl-toggle[aria-pressed="true"] {
  background-color: var(--highlight-bg);
  color: var(--highlight-text);
  border-color: var(--highlight-underline);
}

.page-indicator {
  min-width: 60px;
  text-align: center;
  font-size: 0.95rem;
  font-weight: 700;
  padding: 0 4px;
  white-space: nowrap;
}

/* Mobile Compact Responsive Bar (fits cleanly at 375px with >=48px targets) */
@media (max-width: 767px) {
  .app-header {
    padding: 8px 10px;
  }

  .book-title {
    font-size: 1rem;
  }

  .header-actions {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .header-btn {
    padding: 8px 6px;
    font-size: 0.85rem;
  }

  .reader-main {
    padding: 12px 12px 24px;
  }

  .controls-inner {
    padding: 8px 10px;
    gap: 6px;
    flex-direction: column;
    align-items: stretch;
  }

  .control-nav,
  .control-playback {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: 6px;
  }

  .control-options {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  .ctrl-btn,
  .ctrl-select {
    width: 100%;
    padding: 8px 6px;
    font-size: 0.85rem;
  }
}

/* Modals */
.modal-dialog {
  width: min(94vw, 680px);
  max-height: 88dvh;
  margin: auto;
  padding: 0;
  border-radius: 16px;
  border: 2px solid var(--border);
  background-color: var(--bg);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.modal-dialog[open] {
  display: flex;
  flex-direction: column;
}

.modal-dialog::backdrop {
  background-color: rgba(0, 0, 0, 0.65);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background-color: var(--surface);
  border-bottom: var(--btn-border-width) solid var(--border);
}

.modal-title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.modal-close-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 10px;
  background-color: var(--bg);
  border: var(--btn-border-width) solid var(--border);
  font-weight: 700;
}

.modal-body {
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
  padding: 12px 16px;
  background-color: var(--surface);
  border-top: var(--btn-border-width) solid var(--border);
}

.setting-group {
  margin: 0;
  padding: 12px;
  border-radius: 12px;
  border: var(--btn-border-width) solid var(--border);
  background-color: var(--surface);
}

.setting-legend {
  padding: 0 6px;
  font-weight: 700;
  font-size: 0.98rem;
}

.option-grid {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.option-grid-2 {
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

.option-grid-3 {
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
}

.option-grid-4 {
  grid-template-columns: repeat(auto-fit, minmax(125px, 1fr));
}

.option-btn {
  padding: 10px 12px;
  border-radius: 10px;
  background-color: var(--bg);
  border: var(--btn-border-width) solid var(--border);
  font-size: 0.95rem;
  font-weight: 700;
  text-align: center;
}

.option-btn[aria-pressed="true"] {
  background-color: var(--highlight-bg);
  color: var(--highlight-text);
  border: 2px solid var(--highlight-underline);
}

.primary-btn,
.secondary-btn {
  padding: 10px 18px;
  border-radius: 10px;
  font-weight: 700;
  font-size: 0.95rem;
}

.primary-btn {
  background-color: var(--accent);
  color: var(--accent-text);
  border: var(--btn-border-width) solid var(--accent);
}

.secondary-btn {
  background-color: var(--bg);
  border: var(--btn-border-width) solid var(--border);
}

/* TOC List */
.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toc-item-btn {
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  border-radius: 10px;
  background-color: var(--surface);
  border: var(--btn-border-width) solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.toc-item-btn:hover {
  background-color: var(--surface-hover);
}

.toc-item-btn[aria-current="page"] {
  background-color: var(--highlight-bg);
  color: var(--highlight-text);
  border: 2px solid var(--highlight-underline);
}

.toc-item-label {
  font-weight: 700;
  font-size: 0.98rem;
}

.toc-item-preview {
  font-size: 0.88rem;
  color: var(--muted-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

html[data-theme="high-contrast"] .toc-item-btn[aria-current="page"] .toc-item-preview {
  color: var(--highlight-text);
}

/* Help Modal */
.help-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.help-heading {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.help-list {
  margin: 0;
  padding-left: 20px;
  line-height: 1.7;
}

.shortcut-table {
  width: 100%;
  border-collapse: collapse;
}

.shortcut-table th,
.shortcut-table td {
  padding: 10px;
  border: var(--btn-border-width) solid var(--border);
  text-align: left;
}

.shortcut-table th {
  background-color: var(--surface);
}

kbd {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background-color: var(--surface);
  font-family: monospace;
  font-size: 0.9rem;
}

.help-meta {
  margin: 0;
  line-height: 1.7;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
