:root {
  color-scheme: light;
  --bg: #e9e2d2;
  --surface: #f7f0e3;
  --surface-strong: #fffaf1;
  --paper: #fff8e8;
  --paper-strong: #fffdf6;
  --paper-text: #1d1a14;
  --text: #1b1914;
  --muted: #766f62;
  --line: rgba(38, 32, 22, 0.14);
  --primary: #536a43;
  --primary-strong: #425834;
  --primary-text: #fff9ec;
  --accent: #a9823f;
  --green: #647b52;
  --danger: #ff7c6f;
  --shadow: 0 22px 54px rgba(76, 57, 27, 0.15);
  --paper-shadow: 0 22px 60px rgba(85, 62, 28, 0.18);
  --radius: 8px;
  --shell-pad-x: 16px;
  --bottom-h: 88px;
  --content-max: 880px;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}

body {
  height: 100dvh;
  touch-action: pan-y;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  height: 100dvh;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) var(--bottom-h);
  background:
    linear-gradient(180deg, rgba(255, 251, 243, 0.66), rgba(233, 226, 210, 0.2) 34%, transparent),
    repeating-linear-gradient(90deg, rgba(58, 45, 26, 0.018) 0, rgba(58, 45, 26, 0.018) 1px, transparent 1px, transparent 9px),
    var(--bg);
}

.topbar {
  position: relative;
  z-index: 10;
  display: grid;
  grid-template-columns: minmax(160px, 1fr) auto minmax(120px, 1fr);
  align-items: center;
  gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) var(--shell-pad-x) 10px;
  border-bottom: 1px solid var(--line);
  background: rgba(247, 240, 227, 0.9);
  backdrop-filter: blur(16px) saturate(1.04);
  -webkit-backdrop-filter: blur(16px) saturate(1.04);
}

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

.brand__mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #fff8e9;
  background: linear-gradient(135deg, #2c3324, var(--primary));
  font-weight: 900;
  box-shadow: inset 0 0 0 1px rgba(255, 248, 233, 0.24);
}

.brand__text {
  min-width: 0;
  display: grid;
  gap: 1px;
}

.brand strong {
  font-size: 18px;
  line-height: 1;
  letter-spacing: 0;
}

.brand small {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-status {
  justify-self: center;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  background: rgba(255, 250, 241, 0.74);
  font-size: 13px;
}

.account-chip {
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.account-chip span,
.account-chip button,
.ghost-button {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 250, 241, 0.78);
  color: var(--text);
  padding: 9px 11px;
  white-space: nowrap;
}

.workspace {
  min-height: 0;
  overflow: hidden;
  padding: 14px var(--shell-pad-x);
}

.reader-stage {
  height: 100%;
  min-height: 0;
  max-width: var(--content-max);
  margin: 0 auto;
  overflow: hidden;
}

.setup-panel,
.loading-panel,
.feed-panel {
  height: 100%;
  min-height: 0;
}

.setup-panel {
  overflow-y: auto;
  display: grid;
  gap: 14px;
  align-content: start;
  padding-bottom: 16px;
  scrollbar-gutter: stable;
}

.topic-field,
.auth-form label {
  display: grid;
  gap: 8px;
}

.topic-field span,
.auth-form span {
  color: var(--muted);
  font-size: 13px;
}

.topic-field textarea,
.auth-form input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 250, 241, 0.9);
  color: var(--text);
  outline: none;
  padding: 15px;
  font-size: 16px;
  resize: vertical;
}

.topic-field textarea:focus,
.auth-form input:focus {
  border-color: color-mix(in srgb, var(--primary) 70%, var(--line));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}

.control-block {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 250, 241, 0.74);
  padding: 14px;
  box-shadow: 0 16px 38px rgba(83, 61, 28, 0.08);
}

.control-block--split {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 14px;
}

.control-block__head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.control-block__head strong {
  font-size: 14px;
}

.control-block__head span {
  color: var(--muted);
  font-size: 13px;
}

.segmented,
.rail-options {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.segmented button,
.rail-options button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 253, 246, 0.72);
  color: var(--text);
  padding: 9px 12px;
  font-size: 14px;
}

.segmented button.is-active,
.rail-options button.is-active {
  background: var(--primary);
  color: var(--primary-text);
  border-color: var(--primary);
}

.loading-panel {
  display: grid;
  place-items: center;
  align-content: center;
  gap: 12px;
  text-align: center;
  background: var(--paper);
  color: var(--paper-text);
  border-radius: var(--radius);
  box-shadow: var(--paper-shadow);
  padding: 28px;
}

.loading-panel h2 {
  margin: 0;
  font-size: 24px;
}

.loading-panel p {
  margin: 0;
  color: color-mix(in srgb, var(--paper-text) 68%, transparent);
}

.spinner {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 3px solid color-mix(in srgb, var(--paper-text) 15%, transparent);
  border-top-color: var(--primary);
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.feed-panel {
  overflow: hidden;
}

.feed-track {
  --feed-card-min-h: 560px;
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior-y: contain;
  scroll-snap-type: y mandatory;
  display: grid;
  gap: 14px;
  grid-auto-rows: max-content;
  padding: 0 0 14px;
  scrollbar-gutter: stable;
  overflow-anchor: none;
}

.feed-track--reading {
  scroll-snap-type: none;
}

.feed-track--reading .read-card {
  scroll-snap-align: none;
  scroll-snap-stop: normal;
}

.read-card {
  width: min(100%, 700px);
  justify-self: center;
  min-height: var(--feed-card-min-h);
  scroll-snap-align: start;
  scroll-snap-stop: always;
  border-radius: var(--radius);
  background: var(--paper);
  color: var(--paper-text);
  box-shadow: var(--paper-shadow);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: hidden;
  overflow-anchor: none;
}

.read-card:not(.is-expanded) {
  height: var(--feed-card-min-h);
  max-height: var(--feed-card-min-h);
}

.read-card.is-expanded {
  height: auto;
  overflow: visible;
}

.read-card__top {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: color-mix(in srgb, var(--paper-text) 64%, transparent);
  font-size: 13px;
}

.read-card__top span {
  border: 1px solid color-mix(in srgb, var(--paper-text) 14%, transparent);
  border-radius: var(--radius);
  padding: 5px 8px;
}

.read-card h2 {
  margin: 0;
  font-size: 27px;
  line-height: 1.08;
  letter-spacing: 0;
}

.read-card__preview {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  align-content: start;
  gap: 11px;
  overflow: hidden;
}

.read-card__preview::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 58px;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(255, 248, 232, 0), var(--paper) 82%);
}

.read-card p,
.read-card__preview p {
  margin: 0;
  line-height: 1.48;
}

.read-card__lead {
  font-weight: 650;
  color: color-mix(in srgb, var(--paper-text) 78%, transparent);
}

.read-card__source {
  color: color-mix(in srgb, var(--paper-text) 58%, transparent);
  font-size: 13px;
}

.read-card details {
  flex: 0 0 auto;
  margin-top: auto;
  display: grid;
  gap: 14px;
  overflow: visible;
}

.read-card.is-expanded .read-card__preview {
  display: none;
}

.read-card.is-expanded details {
  margin-top: 0;
}

.read-card summary {
  min-height: 44px;
  width: 100%;
  list-style: none;
  border: 1px solid color-mix(in srgb, var(--paper-text) 16%, transparent);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--paper) 88%, white 12%);
  padding: 11px 14px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  text-align: center;
}

.read-card summary::-webkit-details-marker {
  display: none;
}

.read-card summary::before {
  content: ">";
  display: inline-block;
  margin-right: 8px;
  font-size: 0.9em;
}

.read-card details[open] summary::before {
  content: "v";
}

.read-card__body {
  font-size: 18px;
  line-height: 1.68;
  display: grid;
  gap: 16px;
}

.read-card__body p {
  margin: 0;
}

.read-card__verse {
  white-space: pre-line;
}

.read-card--poems .read-card__body,
.read-card--poems .read-card__preview {
  max-width: 560px;
}

.read-card--poems .read-card__body {
  font-size: 19px;
  line-height: 1.78;
}

.read-card__footer {
  border-top: 1px solid color-mix(in srgb, var(--paper-text) 13%, transparent);
  padding-top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: space-between;
  color: color-mix(in srgb, var(--paper-text) 58%, transparent);
  font-size: 13px;
}

.read-card__footer a {
  color: #72521d;
  font-weight: 800;
}

.auth-panel {
  min-height: 0;
  overflow: auto;
  padding: 22px;
}

.auth-box {
  max-width: 460px;
  margin: 6vh auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 250, 241, 0.9);
  padding: 24px;
  box-shadow: var(--shadow);
}

.auth-box h1 {
  margin: 0 0 8px;
  font-size: 26px;
}

.auth-box p {
  margin: 0 0 18px;
  color: var(--muted);
}

.auth-form {
  display: grid;
  gap: 12px;
}

.auth-form button,
.primary-action {
  border: 0;
  border-radius: var(--radius);
  background: var(--primary);
  color: var(--primary-text);
  padding: 14px 18px;
  font-weight: 900;
}

.primary-action:disabled {
  opacity: 0.7;
  cursor: wait;
}

.inline-error {
  min-height: 20px;
  color: var(--danger);
}

.bottom-panel {
  position: relative;
  z-index: 10;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(180px, 280px);
  gap: 12px;
  align-items: center;
  padding: 10px var(--shell-pad-x) max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: rgba(247, 240, 227, 0.92);
  backdrop-filter: blur(16px) saturate(1.03);
  -webkit-backdrop-filter: blur(16px) saturate(1.03);
}

.bottom-meta {
  color: var(--muted);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--bottom-h) + 18px);
  transform: translateX(-50%);
  z-index: 20;
  max-width: min(420px, calc(100vw - 28px));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-strong);
  color: var(--text);
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

[hidden] {
  display: none !important;
}

@media (max-width: 720px) {
  :root {
    --shell-pad-x: 12px;
    --bottom-h: 86px;
  }

  .topbar {
    grid-template-columns: minmax(132px, 1fr) auto;
  }

  .header-status {
    display: none;
  }

  .brand__mark {
    width: 34px;
    height: 34px;
  }

  .brand small {
    max-width: 190px;
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
    line-height: 1.15;
  }

  .account-chip button {
    display: none;
  }

  .control-block--split {
    grid-template-columns: 1fr;
  }

  .segmented button,
  .rail-options button {
    flex: 1 1 auto;
  }

  .bottom-panel {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .bottom-meta {
    text-align: center;
  }

  .read-card h2 {
    font-size: 34px;
  }
}

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