@import url('https://fonts.googleapis.com/css2?family=Inconsolata:wght@400;600&family=Inter:wght@400;500;600&family=Literata:opsz,wght@7..72,400;7..72,600&family=Noto+Serif:wght@400;600&family=Roboto:wght@400;500&display=swap');

:root {
  --background: #f5efe4;
  --surface: #fffaf1;
  --text: #29231e;
  --muted: #766b61;
  --line: #d9cec0;
  --accent: #83512f;
  --accent-hover: #683e24;
  --danger: #a33a33;
  --reader-size: 20px;
  --reader-width: 1000px;
  --reader-font: Literata, Georgia, serif;
}

[data-theme="dark"] {
  --background: #171614;
  --surface: #201e1b;
  --text: #e9e1d5;
  --muted: #aca096;
  --line: #3a3631;
  --accent: #d7a576;
  --accent-hover: #edbd90;
  --danger: #f08a81;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  color: var(--text);
  background: var(--background);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}
button, input, select { font: inherit; }
button { cursor: pointer; }

.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: 9px clamp(16px, 4vw, 48px) 12px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
}
.toolbar-primary { display: flex; align-items: center; gap: 12px; min-height: 48px; }
.toolbar-source { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1 1 auto; }
.header-load-form { display: grid; grid-template-columns: minmax(180px, 1fr) auto; gap: 10px; flex: 1 1 auto; }
.brand { flex: 0 0 auto; color: var(--text); text-decoration: none; font-weight: 750; letter-spacing: .02em; white-space: nowrap; }
.reading-controls { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }
.reading-controls form { display: contents; }
.reading-controls > button, .reading-controls > form button, .settings-wrapper > button, .chapter-navigation button {
  min-height: 38px;
  padding: 7px 11px;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 9px;
}
.reading-controls button:hover, .chapter-navigation button:hover { background: var(--surface); }
.settings-wrapper { position: relative; }
.settings-panel {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  z-index: 30;
  width: min(520px, calc(100vw - 24px));
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 18px 60px rgba(0, 0, 0, .24);
}
.settings-panel[hidden] { display: none; }
.settings-title { margin-bottom: 18px; font-size: 17px; font-weight: 750; }
.settings-group { display: block; margin: 0 0 18px; }
.settings-label { color: var(--muted); font-size: 13px; font-weight: 700; }
.settings-label-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 9px; }
.settings-label-row output { color: var(--muted); font-size: 13px; }
.font-options { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 9px; }
.font-options button, .stepper button, .theme-setting {
  min-height: 39px;
  padding: 7px 12px;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 9px;
}
.font-options button[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 22%, transparent); }
.font-options button[data-font="roboto"] { font-family: Roboto, Arial, sans-serif; }
.font-options button[data-font="literata"] { font-family: Literata, Georgia, serif; }
.font-options button[data-font="noto-serif"] { font-family: "Noto Serif", Georgia, serif; }
.font-options button[data-font="inconsolata"] { font-family: Inconsolata, Consolas, monospace; }
.settings-inline { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.stepper { display: flex; align-items: center; gap: 9px; }
.stepper output { min-width: 48px; color: var(--muted); text-align: center; font-size: 13px; }
.settings-range { width: 100%; min-height: 24px; padding: 0; background: transparent; border: 0; box-shadow: none; accent-color: var(--accent); }
.theme-setting { width: 100%; }

main { padding: 0 16px 80px; }
.source-feedback { max-width: 1180px; margin: 8px auto 0; }
input {
  width: 100%;
  min-height: 48px;
  padding: 10px 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
}
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent); }
.primary-button {
  min-height: 46px;
  padding: 10px 20px;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 10px;
  font-weight: 700;
}
.primary-button:hover { background: var(--accent-hover); }
.primary-button:disabled { opacity: .65; cursor: wait; }
.load-button { min-width: 74px; display: grid; place-items: center; }
.button-spinner {
  width: 22px;
  height: 22px;
  border: 3px solid rgba(255, 255, 255, .42);
  border-top-color: #fff;
  border-radius: 50%;
  animation: button-spin .7s linear infinite;
}
@keyframes button-spin { to { transform: rotate(360deg); } }
.status { min-height: 24px; margin: 10px 2px; color: var(--muted); font-size: 14px; }
.status.error { color: var(--danger); }

.reader { max-width: min(var(--reader-width), calc(100vw - 32px)); margin: 54px auto 0; }
.chapter-header { text-align: center; margin-bottom: 44px; }
.story-title { margin: 0 0 10px; color: var(--accent); font-size: 14px; font-weight: 750; letter-spacing: .08em; text-transform: uppercase; }
.chapter-header h1 { margin: 0; font-family: var(--reader-font); font-size: clamp(28px, 5vw, 42px); line-height: 1.2; }
.chapter-rule { width: 48px; height: 2px; margin: 24px auto 0; background: var(--accent); }
.chapter-content { font-family: var(--reader-font); font-size: var(--reader-size); line-height: 1.78; }
.chapter-content p { margin: 0 0 1.15em; overflow-wrap: anywhere; }
.story-catalog-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 11px; }
.story-card {
  min-height: 58px;
  padding: 12px 15px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  font: 600 16px/1.4 Inter, ui-sans-serif, system-ui, sans-serif;
  text-align: left;
}
.story-card:hover { color: var(--accent); border-color: var(--accent); transform: translateY(-1px); }
.chapter-picker { flex: 0 1 330px; min-width: 190px; }
.chapter-picker[hidden] { display: none; }
.chapter-picker select {
  width: 100%;
  min-height: 46px;
  padding: 8px 38px 8px 12px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 9px;
}
.chapter-picker select:focus { border-color: var(--accent); outline: 3px solid color-mix(in srgb, var(--accent) 20%, transparent); }
.chapter-navigation { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 58px; padding-top: 24px; border-top: 1px solid var(--line); }
.chapter-navigation button { width: 100%; justify-self: stretch; text-align: center; }
.chapter-navigation button:disabled { visibility: hidden; }

.login-body { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: min(100%, 390px); padding: 34px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 18px 55px rgba(50, 35, 22, .1); }
.brand-mark { display: grid; place-items: center; width: 44px; height: 44px; margin-bottom: 22px; color: white; background: var(--accent); border-radius: 12px; font: 700 24px Georgia, serif; }
.login-card h1 { margin: 0 0 6px; }
.muted { margin: 0 0 26px; color: var(--muted); }
.login-form { display: grid; gap: 10px; }
.login-form label { font-size: 14px; font-weight: 650; }
.login-form .primary-button { margin-top: 8px; }
.alert { margin: 0 0 18px; padding: 11px 12px; color: var(--danger); border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent); border-radius: 8px; }
.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: 900px) {
  .toolbar-primary { flex-wrap: wrap; }
  .brand { order: 1; }
  .reading-controls { order: 2; margin-left: auto; }
  .toolbar-source { order: 3; flex-basis: 100%; }
}

@media (max-width: 620px) {
  :root { --reader-size: 18px; }
  .toolbar { padding-inline: 12px; }
  .brand { font-size: 14px; }
  .settings-panel { position: fixed; top: 58px; right: 12px; left: 12px; width: auto; max-height: calc(100vh - 72px); overflow-y: auto; }
  .toolbar-source { align-items: stretch; flex-direction: column; }
  .header-load-form { width: 100%; }
  .chapter-picker { width: 100%; min-width: 0; flex-basis: auto; }
  .reader { margin-top: 38px; }
  .story-catalog-grid { grid-template-columns: 1fr; }
  .chapter-navigation { grid-template-columns: 1fr 1fr; }
}
