:root {
  --ink: #161615;
  --paper: #f4eee0;
  --paper-2: #e1d7c2;
  --line: rgba(35, 31, 28, 0.16);
  --panel: rgba(251, 247, 238, 0.92);
  --panel-strong: rgba(255, 252, 244, 0.97);
  --teal: #1c6b68;
  --gold: #a77124;
  --red: #8d3232;
  --muted: #6d6359;
  --shadow: 0 18px 60px rgba(13, 11, 8, 0.24);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: "Microsoft YaHei", "PingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body::before {
  position: fixed;
  inset: 0;
  z-index: -1;
  content: "";
  background:
    linear-gradient(90deg, rgba(8, 7, 6, 0.78), rgba(26, 22, 18, 0.28) 45%, rgba(8, 7, 6, 0.72)),
    linear-gradient(180deg, rgba(12, 10, 8, 0.2), rgba(12, 10, 8, 0.72));
}

button,
a,
input,
textarea,
select {
  font: inherit;
}

button:disabled {
  cursor: wait;
  opacity: 0.72;
}

button,
a {
  transition: transform 160ms ease, border-color 160ms ease, background 160ms ease, color 160ms ease;
}

button:hover,
a:hover {
  transform: translateY(-1px);
}

.home-page {
  min-height: 100vh;
  background: #111 url("/static/assets/tedbear1.jpg") center / cover fixed no-repeat;
}

.agent-page {
  min-height: 100vh;
  overflow: hidden;
  background: #171513 url("/static/assets/bookworld-hero.png") center / cover fixed no-repeat;
}

.home-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: minmax(520px, 78vh) auto;
}

.home-shell.single-entry {
  display: grid;
  place-items: center;
  padding: 24px;
}

.book-entry-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: min(320px, 82vw);
  min-height: 86px;
  border: 1px solid rgba(255, 248, 236, 0.42);
  border-radius: 8px;
  padding: 0 42px;
  color: #fff8ec;
  background: rgba(18, 15, 12, 0.72);
  box-shadow: var(--shadow);
  text-decoration: none;
  font-size: 36px;
  font-weight: 800;
  backdrop-filter: blur(12px);
}

.book-entry-button:hover {
  border-color: rgba(216, 179, 109, 0.72);
  background: rgba(28, 107, 104, 0.78);
}

.home-hero {
  display: flex;
  align-items: center;
  max-width: 1160px;
  width: 100%;
  margin: 0 auto;
  padding: 48px 24px;
}

.home-copy {
  max-width: 620px;
  color: #fff8ec;
}

.eyebrow {
  margin: 0 0 10px;
  color: #d8b36d;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.home-copy h1 {
  margin: 0;
  font-size: clamp(48px, 8vw, 92px);
  line-height: 1;
  letter-spacing: 0;
}

.tagline {
  margin: 22px 0 34px;
  font-size: 22px;
  line-height: 1.6;
  color: rgba(255, 248, 236, 0.86);
}

.primary-link,
.full-button,
.continue-form button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  border: 1px solid #be8a3a;
  border-radius: 6px;
  padding: 0 18px;
  color: #fff8ec;
  background: linear-gradient(135deg, #936221, #1d6f69);
  text-decoration: none;
  cursor: pointer;
}

.agent-list {
  display: flex;
  align-items: flex-start;
  max-width: 1160px;
  width: 100%;
  margin: 0 auto;
  padding: 0 24px 42px;
}

.agent-tile {
  display: flex;
  align-items: center;
  gap: 16px;
  width: min(460px, 100%);
  padding: 18px;
  border: 1px solid rgba(255, 248, 236, 0.22);
  border-radius: 8px;
  color: #fff8ec;
  background: rgba(18, 15, 12, 0.68);
  box-shadow: var(--shadow);
  text-decoration: none;
  backdrop-filter: blur(12px);
}

.agent-mark {
  display: grid;
  place-items: center;
  width: 50px;
  height: 50px;
  border: 1px solid rgba(216, 179, 109, 0.42);
  border-radius: 6px;
  color: #f5d796;
  background: rgba(255, 248, 236, 0.08);
  font-size: 22px;
  font-weight: 800;
}

.agent-tile strong,
.agent-tile small {
  display: block;
}

.agent-tile small {
  margin-top: 6px;
  color: rgba(255, 248, 236, 0.72);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
  padding: 0 22px;
  border-bottom: 1px solid rgba(255, 248, 236, 0.14);
  color: #fff8ec;
  background: rgba(10, 9, 8, 0.74);
  backdrop-filter: blur(12px);
}

.brand {
  color: #fff8ec;
  font-weight: 800;
  text-decoration: none;
}

.session-strip {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  max-width: calc(100vw - 150px);
}

.session-strip span {
  min-height: 28px;
  padding: 5px 10px;
  border: 1px solid rgba(255, 248, 236, 0.16);
  border-radius: 999px;
  color: rgba(255, 248, 236, 0.82);
  background: rgba(255, 248, 236, 0.07);
  font-size: 13px;
}

.user-strip {
  display: flex;
  align-items: center;
  gap: 8px;
  color: rgba(255, 248, 236, 0.8);
  font-size: 13px;
}

.user-strip button,
.tiny-button {
  min-height: 28px;
  border: 1px solid rgba(255, 248, 236, 0.18);
  border-radius: 6px;
  padding: 0 9px;
  color: inherit;
  background: rgba(255, 248, 236, 0.07);
  cursor: pointer;
}

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 20px;
  background:
    linear-gradient(180deg, rgba(12, 10, 8, 0.72), rgba(12, 10, 8, 0.88)),
    url("/static/assets/bookworld-hero.png") center / cover no-repeat;
}

.auth-gate[hidden] {
  display: none;
}

.auth-card {
  width: min(420px, 100%);
  padding: 28px;
  border: 1px solid rgba(255, 248, 236, 0.24);
  border-radius: 8px;
  color: var(--ink);
  background: var(--panel-strong);
  box-shadow: var(--shadow);
}

.auth-card h1 {
  margin: 0 0 18px;
  font-size: 30px;
}

.auth-card label {
  display: grid;
  gap: 7px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

.auth-tabs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.auth-tabs button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.42);
  cursor: pointer;
}

.auth-tabs button.active {
  color: #fff8ec;
  border-color: var(--teal);
  background: var(--teal);
}

.auth-message {
  min-height: 22px;
  margin: 10px 0 0;
  color: var(--red);
  font-size: 13px;
}

.agent-layout {
  height: calc(100vh - 64px);
  display: grid;
  grid-template-columns: 340px minmax(360px, 1fr) 380px;
  gap: 14px;
  padding: 14px;
}

.left-panel,
.right-panel,
.story-panel {
  min-height: 0;
  border: 1px solid rgba(255, 248, 236, 0.18);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

.left-panel,
.right-panel {
  overflow: auto;
  padding: 16px;
}

.panel-block + .panel-block {
  margin-top: 20px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.panel-block h2 {
  margin: 0 0 12px;
  font-size: 17px;
}

.panel-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.panel-title-row h2 {
  margin: 0;
}

.upload-zone {
  display: grid;
  place-items: center;
  min-height: 98px;
  padding: 14px;
  border: 1px dashed rgba(28, 107, 104, 0.46);
  border-radius: 8px;
  color: var(--teal);
  background: rgba(28, 107, 104, 0.07);
  cursor: pointer;
  text-align: center;
  line-height: 1.5;
}

.upload-zone input {
  display: none;
}

.full-button {
  width: 100%;
  margin-top: 10px;
}

.ghost-button {
  min-height: 36px;
  border: 1px solid rgba(22, 22, 21, 0.18);
  border-radius: 6px;
  padding: 0 12px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.42);
  cursor: pointer;
}

.meta-list,
.small-list {
  margin-top: 14px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
}

.small-list strong {
  display: block;
  margin-bottom: 6px;
  color: var(--ink);
}

.pill-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 220px;
  overflow: auto;
}

.pill-list span {
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.35);
}

.full-list {
  max-height: 320px;
  margin: 0;
  padding-left: 22px;
  overflow: auto;
}

.full-list li {
  margin: 0 0 6px;
}

.save-list,
.book-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

.book-item,
.save-item {
  display: grid;
  gap: 7px;
}

.book-item {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  background: rgba(255, 255, 255, 0.42);
}

.shelf-item {
  grid-template-columns: minmax(0, 1fr) 52px;
  padding: 0;
  background: transparent;
  border: 0;
}

.book-item > div,
.book-open,
.save-open {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.book-item button,
.save-item button {
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
}

.book-item > button:not(.book-open),
.book-remove,
.save-delete {
  min-height: 34px;
  padding: 0 10px;
  color: var(--teal);
  background: rgba(28, 107, 104, 0.08);
  white-space: nowrap;
}

.book-open,
.save-open {
  width: 100%;
  padding: 9px 10px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.42);
  text-align: left;
}

.book-remove,
.save-delete {
  color: var(--red);
  background: rgba(141, 50, 50, 0.08);
}

.save-item {
  grid-template-columns: minmax(0, 1fr) 52px;
}

.book-item strong,
.book-item span,
.book-item small,
.save-item strong,
.save-item span,
.save-item small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.book-item span,
.book-item small,
.save-item span,
.save-item small {
  color: var(--muted);
}

.story-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  overflow: hidden;
  background: var(--panel-strong);
}

.story-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
}

.story-header h1 {
  margin: 0;
  font-size: 24px;
}

.story-output {
  overflow: auto;
  padding: 22px;
  color: #24201d;
  line-height: 1.9;
  white-space: pre-wrap;
}

.story-entry {
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--line);
}

.story-entry:last-child {
  border-bottom: 0;
}

.story-entry .user-line {
  margin-bottom: 10px;
  color: var(--teal);
  font-weight: 700;
}

.turn-result {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 0 0 12px;
  padding: 8px 10px;
  border-left: 3px solid var(--gold);
  background: rgba(167, 113, 36, 0.1);
  color: #4d3b22;
  white-space: normal;
}

.turn-result strong {
  color: var(--gold);
}

.state-grid {
  display: grid;
  gap: 8px;
}

.state-grid div {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 8px;
  align-items: start;
  font-size: 13px;
}

.state-grid span,
.mini-card small {
  color: var(--muted);
}

.state-grid strong {
  font-weight: 600;
}

.compact-stack {
  display: grid;
  gap: 8px;
}

.mini-card {
  display: grid;
  gap: 4px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.4);
  font-size: 13px;
  line-height: 1.45;
}

.dialogue-indicator {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  color: var(--teal);
  background: rgba(28, 107, 104, 0.08);
  font-size: 13px;
  line-height: 1.5;
}

.choice-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 18px;
  border-top: 1px solid var(--line);
}

.choice-bar button {
  border: 1px solid rgba(28, 107, 104, 0.28);
  border-radius: 999px;
  padding: 7px 11px;
  color: var(--teal);
  background: rgba(28, 107, 104, 0.08);
  cursor: pointer;
}

.continue-form {
  display: grid;
  grid-template-columns: 1fr 108px;
  gap: 10px;
  padding: 14px 18px 18px;
}

textarea,
input,
select {
  width: 100%;
  border: 1px solid rgba(22, 22, 21, 0.18);
  border-radius: 6px;
  padding: 10px 11px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.76);
  outline: none;
}

textarea:focus,
input:focus,
select:focus {
  border-color: rgba(28, 107, 104, 0.62);
  box-shadow: 0 0 0 3px rgba(28, 107, 104, 0.12);
}

.mode-tabs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.mode-tab {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.42);
  cursor: pointer;
}

.mode-tab.active {
  color: #fff8ec;
  border-color: var(--teal);
  background: var(--teal);
}

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

.field {
  display: grid;
  gap: 6px;
}

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

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
}

.checkbox-field input {
  width: auto;
}

.intervention-row {
  display: grid;
  grid-template-columns: 0.8fr 1fr 34px;
  gap: 6px;
}

.icon-button {
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--red);
  background: rgba(141, 50, 50, 0.08);
  cursor: pointer;
}

.muted-button {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--teal);
  background: rgba(28, 107, 104, 0.08);
  cursor: pointer;
}

.loading {
  cursor: progress;
}

.toast {
  color: var(--red);
}

.mobile-nav {
  display: none;
}

@media (max-width: 1180px) {
  .agent-layout {
    grid-template-columns: 280px minmax(360px, 1fr);
  }

  .right-panel {
    grid-column: 1 / -1;
    max-height: 360px;
  }
}

@media (max-width: 760px) {
  body.agent-page {
    min-height: 100svh;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    overflow: auto;
  }

  .agent-page::before {
    background:
      linear-gradient(180deg, rgba(11, 9, 8, 0.8), rgba(11, 9, 8, 0.5)),
      linear-gradient(90deg, rgba(8, 7, 6, 0.68), rgba(8, 7, 6, 0.42));
  }

  .topbar {
    height: auto;
    min-height: 50px;
    align-items: center;
    gap: 7px;
    padding: 8px 12px;
  }

  .brand {
    flex: 0 0 auto;
    font-size: 17px;
    line-height: 1;
  }

  .session-strip {
    flex: 1;
    justify-content: flex-end;
    gap: 3px;
    max-width: none;
    overflow: hidden;
  }

  .user-strip {
    flex: 0 0 auto;
    gap: 4px;
    font-size: 10px;
  }

  .user-strip span {
    display: none;
  }

  .user-strip button {
    min-height: 24px;
    border: 0;
    padding: 0 4px;
    background: transparent;
  }

  .session-strip span {
    min-height: 20px;
    max-width: 84px;
    overflow: hidden;
    border: 0;
    padding: 2px 4px;
    color: rgba(255, 248, 236, 0.72);
    background: transparent;
    font-size: 10px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .session-strip span + span::before {
    content: "· ";
    color: rgba(216, 179, 109, 0.75);
  }

  .agent-layout {
    height: calc(100svh - 50px);
    min-height: 0;
    display: block;
    padding: 8px 8px 58px;
    overflow: hidden;
  }

  .agent-page {
    background-attachment: scroll;
  }

  .left-panel,
  .right-panel,
  .story-panel {
    display: none;
    height: 100%;
    max-height: none;
    border-radius: 8px;
    box-shadow: 0 12px 38px rgba(13, 11, 8, 0.22);
  }

  body.mobile-view-story .story-panel {
    display: grid;
  }

  body.mobile-view-book .left-panel,
  body.mobile-view-state .right-panel,
  body.mobile-view-setup .right-panel {
    display: block;
  }

  body.mobile-view-state .right-panel .panel-block,
  body.mobile-view-setup .right-panel .panel-block {
    display: none;
  }

  body.mobile-view-state .right-panel .mobile-state-block,
  body.mobile-view-setup .right-panel .mobile-setup-block {
    display: block;
  }

  .left-panel,
  .right-panel {
    padding: 14px;
    overflow: auto;
  }

  .right-panel .panel-block {
    margin-top: 0;
    padding-top: 0;
    border-top: 0;
  }

  .right-panel .panel-block:not(:first-child) {
    margin-top: 16px;
    padding-top: 14px;
    border-top: 1px solid var(--line);
  }

  body.mobile-view-state .right-panel .mobile-state-block,
  body.mobile-view-setup .right-panel .mobile-setup-block {
    margin-top: 0;
    padding-top: 0;
    border-top: 0;
  }

  body.mobile-view-state .right-panel .mobile-state-block + .mobile-state-block,
  body.mobile-view-setup .right-panel .mobile-setup-block + .mobile-setup-block {
    margin-top: 16px;
    padding-top: 14px;
    border-top: 1px solid var(--line);
  }

  .panel-block + .panel-block {
    margin-top: 16px;
    padding-top: 14px;
  }

  .panel-block h2 {
    font-size: 16px;
  }

  .story-panel {
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    min-height: 0;
  }

  .continue-form {
    grid-template-columns: minmax(0, 1fr) 78px;
    align-items: end;
    gap: 7px;
    padding: 8px 10px 10px;
    background: rgba(255, 252, 244, 0.98);
    box-shadow: 0 -6px 16px rgba(13, 11, 8, 0.08);
  }

  .continue-form textarea {
    min-height: 44px;
    max-height: 86px;
    padding: 9px 10px;
    resize: none;
    font-size: 14px;
    line-height: 1.45;
  }

  .continue-form button {
    min-height: 44px;
    padding: 0 10px;
    font-size: 14px;
    white-space: nowrap;
  }

  .story-header {
    align-items: center;
    padding: 10px 12px 8px;
  }

  .story-header .eyebrow {
    margin-bottom: 3px;
    font-size: 10px;
  }

  .story-header h1 {
    display: -webkit-box;
    overflow: hidden;
    font-size: 16px;
    line-height: 1.35;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  .story-header .ghost-button {
    display: none;
  }

  .story-output {
    padding: 14px 14px 12px;
    font-size: 15px;
    line-height: 1.82;
  }

  .story-entry {
    margin-bottom: 14px;
    padding-bottom: 12px;
  }

  .story-entry .user-line {
    margin-bottom: 8px;
    font-size: 13px;
  }

  .turn-result {
    margin-bottom: 9px;
    padding: 7px 9px;
    font-size: 13px;
  }

  .choice-bar {
    display: grid;
    gap: 6px;
    max-height: 108px;
    overflow: auto;
    padding: 8px 10px;
    background: rgba(28, 107, 104, 0.04);
  }

  .choice-bar button {
    width: 100%;
    max-width: none;
    border-radius: 6px;
    padding: 7px 9px;
    font-size: 13px;
    line-height: 1.35;
    text-align: left;
    white-space: normal;
  }

  .pill-list,
  .full-list {
    max-height: 36dvh;
  }

  .auth-card {
    padding: 22px;
  }

  .auth-card h1 {
    font-size: 26px;
  }

  .save-list,
  .book-list {
    max-height: 34dvh;
    overflow: auto;
  }

  .book-item {
    grid-template-columns: minmax(0, 1fr);
    align-items: stretch;
  }

  .book-item > button:not(.book-open),
  .book-remove,
  .save-delete {
    min-height: 32px;
    padding: 0 8px;
  }

  .shelf-item,
  .save-item {
    grid-template-columns: minmax(0, 1fr) 48px;
  }

  .mode-tabs {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .mode-tab {
    min-height: 42px;
    padding: 0 4px;
    font-size: 13px;
  }

  .intervention-row {
    grid-template-columns: 1fr 1fr 34px;
  }

  .state-grid div {
    grid-template-columns: 72px 1fr;
  }

  .mobile-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 2px;
    padding: 5px 8px calc(8px + env(safe-area-inset-bottom));
    border-top: 1px solid rgba(255, 248, 236, 0.16);
    background: rgba(10, 9, 8, 0.88);
    backdrop-filter: blur(14px);
  }

  .mobile-nav button {
    min-height: 34px;
    border: 0;
    border-radius: 4px;
    color: rgba(255, 248, 236, 0.75);
    background: transparent;
    cursor: pointer;
    font-size: 13px;
  }

  .mobile-nav button.active {
    color: #fff8ec;
    background: rgba(28, 107, 104, 0.86);
  }

  @supports (-webkit-touch-callout: none) {
    .agent-layout {
      height: calc(100vh - 50px);
      padding-bottom: 74px;
    }

    .mobile-nav {
      padding-bottom: calc(18px + env(safe-area-inset-bottom));
    }
  }

  .home-copy h1 {
    font-size: 48px;
  }

  .tagline {
    font-size: 18px;
  }
}
