:root {
  color-scheme: light;
  --bg: #f7f4ed;
  --surface: #fffdf8;
  --surface-alt: #edf4f2;
  --border: #d8d2c5;
  --text: #25231f;
  --muted: #6d675c;
  --accent: #1d6f78;
  --accent-strong: #15515a;
  --focus: #b35f2a;
  --shadow: 0 18px 40px rgba(36, 31, 24, 0.12);
  --reader-size: 19px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #171814;
  --surface: #22231e;
  --surface-alt: #1d2927;
  --border: #3d3c34;
  --text: #f1ecdf;
  --muted: #b8b09e;
  --accent: #79c5bd;
  --accent-strong: #a6ddd7;
  --focus: #e1a15d;
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
  overflow-x: hidden;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 68px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px);
}

.brand {
  min-width: 0;
  flex: 1;
}

.brand-title {
  font-size: 18px;
  font-weight: 700;
  line-height: 1.25;
}

.brand-subtitle {
  margin-top: 2px;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions,
.reader-actions,
.chapter-picker {
  display: flex;
  align-items: center;
  gap: 8px;
}

.select-label {
  color: var(--muted);
  font-size: 13px;
}

.select-control,
.search-input {
  min-height: 38px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
}

.select-control {
  padding: 0 34px 0 10px;
}

.search-input {
  width: 100%;
  padding: 0 12px;
}

.icon-button,
.text-button {
  min-height: 38px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.icon-button {
  min-width: 40px;
  padding: 0 10px;
}

.text-button {
  padding: 0 12px;
}

.reader-mode-select[hidden] {
  display: none;
}

.icon-button:hover,
.text-button:hover,
.book-button:hover,
.chapter-link:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.icon-button:focus-visible,
.text-button:focus-visible,
.book-button:focus-visible,
.chapter-link:focus-visible,
.select-control:focus-visible,
.search-input:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--focus) 35%, transparent);
  outline-offset: 2px;
}

.workspace {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  min-height: 0;
}

.sidebar {
  position: sticky;
  top: 68px;
  height: calc(100vh - 68px);
  border-right: 1px solid var(--border);
  background: var(--surface);
  overflow: auto;
}

.sidebar-header {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.book-list {
  display: grid;
  gap: 2px;
  padding: 8px;
}

.book-button {
  width: 100%;
  min-height: 38px;
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.book-button.active {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  background: var(--surface-alt);
  color: var(--accent-strong);
  font-weight: 700;
}

.reader-shell {
  min-width: 0;
  display: grid;
  grid-template-rows: auto 1fr;
}

.reader-toolbar {
  position: sticky;
  top: 68px;
  z-index: 10;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  backdrop-filter: blur(12px);
}

.reader {
  width: min(100%, 980px);
  margin: 0 auto;
  padding: 34px 28px 72px;
  font-family: Georgia, "Times New Roman", serif;
  font-size: var(--reader-size);
  line-height: 1.72;
}

.reader:focus {
  outline: none;
}

.reader h1,
.reader h2,
.reader h3 {
  font-family: Arial, Helvetica, sans-serif;
  line-height: 1.2;
}

.reader h1 {
  margin: 0 0 10px;
  font-size: 32px;
}

.reader h2 {
  margin: 34px 0 12px;
  font-size: 23px;
}

.reader h3 {
  margin: 28px 0 10px;
  font-size: 20px;
}

.reader p {
  margin: 0 0 18px;
}

.reader-parallel {
  width: min(100%, 1480px);
}

.parallel-reader {
  display: grid;
  gap: 18px;
}

.parallel-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 20px;
  padding: 16px 0;
  border-top: 1px solid var(--border);
}

.parallel-cell {
  min-width: 0;
}

.parallel-cell-en {
  color: var(--muted);
}

.parallel-cell p:last-child,
.parallel-cell blockquote:last-child,
.parallel-cell ul:last-child {
  margin-bottom: 0;
}

.reader-meta {
  margin-bottom: 24px;
  color: var(--muted);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 14px;
}

.chapter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(82px, 1fr));
  gap: 8px;
  margin: 18px 0 30px;
}

.chapter-link {
  min-height: 38px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  text-align: center;
  cursor: pointer;
}

.chapter-link.active {
  border-color: var(--accent);
  background: var(--surface-alt);
  color: var(--accent-strong);
  font-weight: 700;
}

.empty-state {
  padding-top: 24px;
  color: var(--muted);
  font-family: Arial, Helvetica, sans-serif;
}

.mobile-only {
  display: none;
}

.nav-backdrop {
  display: none;
}

@media (max-width: 820px) {
  .mobile-only {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .topbar {
    gap: 10px;
    padding: 10px 12px;
  }

  .topbar-actions {
    flex: 0 0 auto;
  }

  .icon-button {
    min-width: 38px;
  }

  .brand-title {
    font-size: 16px;
  }

  .select-label {
    display: none;
  }

  .workspace {
    display: block;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 40;
    width: min(86vw, 340px);
    height: 100vh;
    transform: translateX(-100%);
    transition: transform 160ms ease;
    box-shadow: var(--shadow);
  }

  body.nav-open .sidebar {
    transform: translateX(0);
  }

  body.nav-open .nav-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 30;
    background: rgba(0, 0, 0, 0.35);
  }

  .reader-toolbar {
    position: static;
    flex-direction: column;
    align-items: stretch;
    padding: 8px 12px;
  }

  .chapter-picker,
  .reader-actions {
    width: 100%;
  }

  .chapter-picker .select-control {
    min-width: 0;
    flex: 1;
  }

  .reader-actions {
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .reader {
    width: 100%;
    max-width: 100vw;
    padding: 24px 16px 56px;
    font-size: calc(var(--reader-size) - 1px);
  }

  .reader h1 {
    font-size: 26px;
  }
}

@media (max-width: 520px) {
  html,
  body,
  .app-shell,
  .workspace,
  .reader-shell,
  .reader-toolbar,
  .reader {
    max-width: 100vw;
    overflow-x: hidden;
  }

  .topbar {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 8px;
    width: 100vw;
    max-width: 100vw;
  }

  .brand {
    min-width: 0;
  }

  .topbar-actions {
    grid-column: 1 / -1;
    justify-content: start;
    min-width: 0;
  }

  #languageSelect {
    width: 150px;
    max-width: 150px;
  }

  #themeToggle {
    display: none;
  }

  .chapter-picker {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 0.7fr);
  }

  .reader-actions {
    display: grid;
    grid-template-columns: repeat(4, 40px);
    align-items: center;
    justify-content: start;
    gap: 6px;
  }

  .reader-actions.has-reader-mode {
    grid-template-columns: minmax(0, 104px) repeat(4, 40px);
  }

  .reader-actions .text-button,
  .reader-actions .icon-button {
    min-width: 0;
    width: 40px;
    padding: 0;
  }

  .reader-mode-select {
    min-width: 0;
    width: 100%;
  }

  .nav-toggle-button,
  #prevButton,
  #nextButton {
    font-size: 0;
  }

  .nav-toggle-button::before,
  #prevButton::before,
  #nextButton::before {
    font-size: 18px;
    line-height: 1;
  }

  .nav-toggle-button::before {
    content: "|||";
    display: block;
    letter-spacing: -2px;
    transform: rotate(90deg);
  }

  #prevButton::before {
    content: "<";
  }

  #nextButton::before {
    content: ">";
  }

  .chapter-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .chapter-link,
  .select-control {
    min-width: 0;
    width: 100%;
  }

  .parallel-row {
    grid-template-columns: minmax(0, 1fr);
    gap: 12px;
  }
}
