:root {
  color-scheme: light;
  font-family: system-ui, "PingFang SC", "Microsoft YaHei UI", sans-serif;
  --ground: #f3f5f8;
  --surface: #ffffff;
  --raised: #f0f3f7;
  --ink: #1d1d1f;
  --muted: #505962;
  --line: #d8dee7;
  --input-line: #7e8997;
  --selected: #e6eef9;
  --action: #005fcb;
  --action-text: #ffffff;
  --focus: #005fcb;
  --error: #9a3422;
  --ocean: #e5edf8;
  --ocean-ink: #182b42;
  --ocean-muted: #384f68;
  --ocean-detail: #b9cbe1;
  --radius: 20px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ground: #151619;
  --surface: #202125;
  --raised: #2a2d34;
  --ink: #f5f5f7;
  --muted: #c3c8d0;
  --line: #414650;
  --input-line: #8998ab;
  --selected: #283b54;
  --action: #a8cdff;
  --action-text: #102b4d;
  --focus: #a8cdff;
  --error: #ffb299;
  --ocean: #24354c;
  --ocean-ink: #f3f7fd;
  --ocean-muted: #c7d7ef;
  --ocean-detail: #4b6383;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-block-size: 100svh;
  background: var(--ground);
  color: var(--ink);
  font-size: 1rem;
  line-height: 1.6;
}

.reader {
  inline-size: min(100%, 72rem);
  margin-inline: auto;
  padding: clamp(12px, 3.3vw, 40px);
  padding-block-end: max(20px, env(safe-area-inset-bottom));
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 24px;
  margin-block-end: clamp(16px, 3vw, 30px);
  padding-block-end: 14px;
  border-block-end: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  min-block-size: 44px;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  text-decoration: none;
  font-size: 1.15rem;
  font-weight: 700;
  white-space: nowrap;
}

.brand-mark {
  display: grid;
  place-items: center;
  inline-size: 48px;
  block-size: 44px;
  border-radius: 12px;
  background: var(--ocean);
}
.brand-mark img { display: block; inline-size: 48px; block-size: 32px; object-fit: contain; }

.header-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 12px; }
.logout-form .text-button {
  min-block-size: 44px;
  color: var(--muted);
  font-size: .875rem;
  white-space: nowrap;
}

.reader-nav { display: flex; align-items: center; gap: 4px; }
.reader-nav a {
  display: flex;
  align-items: center;
  justify-content: center;
  min-block-size: 44px;
  padding: 6px clamp(12px, 2vw, 20px);
  border-radius: 11px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.94rem;
  font-weight: 650;
  white-space: nowrap;
}
.reader-nav a:hover { background: var(--raised); color: var(--ink); }
.reader-nav a[aria-current] { background: var(--selected); color: var(--ink); }
.reader-nav a[aria-current]::after {
  content: "";
  inline-size: 5px;
  block-size: 5px;
  margin-inline-start: 8px;
  border-radius: 50%;
  background: currentColor;
}

:where(a, button, input, textarea, summary, select):focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 3px;
}

h1, h2, h3, p, figure { margin: 0; }
h1, h2, h3 { overflow-wrap: anywhere; line-height: 1.25; }
h1 { font-size: clamp(1.65rem, 3.2vw, 2.15rem); font-weight: 750; }

.login-page {
  display: grid;
  align-content: center;
  padding: clamp(16px, 5vw, 48px);
}
.login-layout { width: min(100%, 25rem); margin-inline: auto; }
.login-panel {
  padding: clamp(20px, 5vw, 32px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}
.login-header { display: flex; align-items: center; gap: 12px; margin-block-end: 24px; }
.login-header img { object-fit: contain; }
.login-header h1 { font-size: clamp(1.25rem, 4.4vw, 1.6rem); }
.login-form { display: grid; gap: 16px; }
.login-field { display: grid; gap: 6px; font-size: .875rem; font-weight: 600; }
.login-field input {
  width: 100%;
  min-block-size: 44px;
  padding: 8px 12px;
  border: 1px solid var(--input-line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
  font-weight: 400;
}
.remember-login {
  display: flex;
  min-block-size: 44px;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: .875rem;
  cursor: pointer;
}
.remember-login input { width: 18px; height: 18px; margin: 0; accent-color: var(--action); }
.login-error { color: var(--error); font-size: .875rem; }
h2 { font-size: 1.22rem; font-weight: 700; }
h3 { font-size: 1.16rem; font-weight: 700; }

.chat {
  display: grid;
  grid-template-columns: minmax(15rem, 0.82fr) minmax(0, 1.58fr);
  gap: clamp(14px, 2vw, 24px);
  scroll-margin-block-start: 8rem;
}

.chat-intro {
  position: relative;
  display: grid;
  grid-template-rows: auto 1fr;
  align-content: start;
  min-inline-size: 0;
  min-block-size: 0;
  overflow: clip;
  isolation: isolate;
  border-radius: var(--radius);
  background: var(--ocean);
  color: var(--ocean-ink);
}

.intro-copy { z-index: 1; padding: clamp(20px, 3.3vw, 36px); }
.intro-copy p { margin-block-start: 10px; max-inline-size: 18em; color: var(--ocean-muted); }

.fish-art {
  position: relative;
  display: grid;
  place-items: center;
  align-self: center;
  inline-size: min(107%, 27rem);
  aspect-ratio: 1;
  justify-self: center;
  isolation: isolate;
}
.fish-art::before, .fish-art::after {
  position: absolute;
  z-index: -1;
  content: "";
  inline-size: 87%;
  aspect-ratio: 1;
  border: 1px solid var(--ocean-detail);
  border-radius: 50%;
}
.fish-art::after { inline-size: 65%; }
.fish-art img {
  display: block;
  inline-size: 100%;
  block-size: auto;
  transform: scale(1.07);
}

.chat-workspace, .library, #book-panel, #reader-form {
  min-inline-size: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.chat-workspace {
  display: grid;
  grid-template-rows: auto minmax(12rem, 1fr) auto;
  block-size: clamp(29rem, 66dvh, 38rem);
}
.chat-header { padding: clamp(15px, 2.5vw, 23px); border-block-end: 1px solid var(--line); }
.chat-header p { margin-block-start: 4px; color: var(--muted); font-size: 0.875rem; }

.chat-feed {
  display: grid;
  align-content: start;
  min-block-size: 0;
  padding: clamp(14px, 2.6vw, 28px);
  overflow: auto;
  overscroll-behavior: contain;
}
.chat-feed:has(#chat-empty:not([hidden])) { align-content: center; }
#chat-empty h3 { margin-block-end: 12px; }
.starters { display: flex; flex-wrap: wrap; gap: 8px; }

button, textarea, select { font: inherit; }
button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-block-size: 44px;
  border: 1px solid transparent;
  border-radius: 11px;
  padding: 6px 13px;
  background: var(--raised);
  color: var(--ink);
  font-size: 0.92rem;
  font-weight: 650;
  cursor: pointer;
  touch-action: manipulation;
}
button:hover { background: var(--selected); }
button:disabled { opacity: .54; cursor: default; }
button:active:not(:disabled) { translate: 0 1px; }
.primary { background: var(--action); color: var(--action-text); }
.primary:hover:not(:disabled) { filter: brightness(.92); background: var(--action); }
.starter { border-color: var(--line); background: var(--surface); text-align: start; }
.starter::after { content: "↗"; color: var(--muted); }

#chat-messages { display: grid; gap: 14px; min-inline-size: 0; }
.chat-message {
  justify-self: start;
  max-inline-size: min(90%, 34rem);
  min-inline-size: 0;
  padding: 10px 14px;
  border-radius: 14px 14px 14px 4px;
  background: var(--raised);
}
.chat-message[data-role="user"] {
  justify-self: end;
  border-radius: 14px 14px 4px 14px;
  background: var(--selected);
}
.chat-sender { margin-block-end: 2px; color: var(--muted); font-size: 0.79rem; font-weight: 650; }
.chat-message p { white-space: pre-wrap; overflow-wrap: anywhere; }
button.chat-play {
  margin-block-start: 6px;
  background: var(--surface);
  border-color: var(--line);
}
button.chat-play:hover { background: var(--selected); }
.chat-play::before {
  content: "";
  inline-size: 0;
  block-size: 0;
  border-block: 5px solid transparent;
  border-inline-start: 8px solid currentColor;
}
.chat-play[data-playing]::before { inline-size: 8px; block-size: 8px; border: 0; background: currentColor; }

#chat-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: end;
  padding: clamp(12px, 2vw, 18px);
  border-block-start: 1px solid var(--line);
}
#chat-status { grid-column: 1 / -1; color: var(--muted); font-size: 0.875rem; overflow-wrap: anywhere; }
#chat-status:empty, #status:empty { display: none; }
#chat-status[data-error], #status[data-error] { color: var(--error); }

textarea, select {
  min-inline-size: 0;
  inline-size: 100%;
  border: 1px solid var(--input-line);
  border-radius: 11px;
  background: var(--surface);
  color: var(--ink);
}
textarea { display: block; padding: 11px 13px; resize: vertical; }
textarea::placeholder { color: var(--muted); opacity: 1; }
#chat-input { resize: none; min-block-size: 58px; line-height: 1.45; }
#chat-send { min-block-size: 48px; padding-inline: 15px; }
#chat-send span { font-size: 1.19rem; line-height: 1; }

#reading { scroll-margin-block-start: 8rem; }
.reading-header { margin-block-end: 16px; }
.reading-grid {
  display: grid;
  grid-template-columns: minmax(14rem, .9fr) minmax(0, 1.35fr);
  align-items: start;
  gap: clamp(14px, 2vw, 24px);
}
.reading-grid:has(#book-panel:not([hidden])) {
  grid-template-columns: minmax(0, 1fr);
  max-inline-size: 49rem;
}
.library, #book-panel, #reader-form { padding: clamp(14px, 2.4vw, 24px); }
.library-bar, .actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.library-bar { justify-content: space-between; }
.hint { margin-block: 8px 10px; color: var(--muted); font-size: 0.875rem; line-height: 1.55; }
#shelf { list-style: none; padding: 0; margin: 0; display: grid; gap: 7px; }
#shelf:not(:empty) { margin-block: 10px 12px; }
button.book-entry {
  inline-size: 100%;
  display: grid;
  justify-content: stretch;
  justify-items: start;
  gap: 2px;
  text-align: start;
  overflow-wrap: anywhere;
}
.book-entry span { color: var(--muted); font-size: 0.85rem; font-weight: 400; }
button.text-button { background: transparent; padding-inline: 0; color: var(--action); }
:root[data-theme="dark"] button.text-button { color: var(--ink); }
button.text-button:hover { text-decoration: underline; }
#book-panel .actions > button { flex: 1 0 auto; }
#preview {
  margin-block: 8px 14px;
  padding: 14px;
  border-radius: 11px;
  background: var(--raised);
  line-height: 1.75;
  overflow-wrap: anywhere;
}
summary { min-block-size: 44px; padding-block: 9px; color: var(--muted); cursor: pointer; }
.setting { display: grid; grid-template-columns: minmax(4rem, auto) minmax(0, 1fr); align-items: center; gap: 10px; margin-block: 8px; }
select { min-block-size: 44px; padding-inline: 8px; }
#reader-form { display: grid; gap: 12px; }
#reader-form:has(#words[hidden]) .reading-compose-title { display: none; }
#reader-form:has(#words[hidden]) { margin-block-start: -8px; }
#words { min-block-size: 13rem; line-height: 1.75; }
#play { min-block-size: 50px; }
.button-icon { border-block: 6px solid transparent; border-inline-start: 9px solid currentColor; }
#play[data-state="working"] .button-icon,
#play[data-state="playing"] .button-icon { inline-size: 10px; block-size: 10px; border: 0; border-radius: 2px; background: currentColor; }
#status { color: var(--muted); font-size: 0.875rem; }

.screen-reader {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

@media (max-width: 44rem) {
  .reader { padding-inline: max(14px, env(safe-area-inset-left), env(safe-area-inset-right)); }
  .chat, .reading-grid { grid-template-columns: minmax(0, 1fr); }
  .chat-intro { grid-template: auto / minmax(0, 1fr) minmax(92px, 35%); align-items: center; }
  .intro-copy { padding: 17px 0 17px 17px; }
  .intro-copy p { margin-block-start: 5px; font-size: 0.9rem; }
  .fish-art { inline-size: min(115%, 12rem); margin-inline-start: -5px; }
  .chat-workspace { block-size: clamp(21rem, 54dvh, 31rem); }
  .chat-header { padding-block: 13px; }
}
@media (max-width: 23rem) {
  .app-header { gap: 6px; }
  .brand { gap: 6px; font-size: 1rem; }
  .brand-mark { inline-size: 39px; }
  .brand-mark img { inline-size: 39px; }
  .reader-nav a { padding-inline: 8px; font-size: 0.86rem; }
  .reader-nav a[aria-current]::after { margin-inline-start: 4px; }
}


.site-footer { margin-block-start: 24px; text-align: center; font-size: .85rem; }
.site-footer a { color: var(--muted); }
