:root {
  --bg: #090c14;
  --bg-elevated: #111726;
  --bg-soft: #171f31;
  --panel: rgba(18, 25, 40, 0.92);
  --line: rgba(148, 163, 184, 0.18);
  --line-strong: rgba(148, 163, 184, 0.32);
  --text: #e8edf7;
  --muted: #97a3ba;
  --accent: #7dd3fc;
  --accent-strong: #38bdf8;
  --accent-soft: rgba(56, 189, 248, 0.14);
  --gold: #fbbf24;
  --danger: #fb7185;
  --success: #34d399;
  --radius: 16px;
  --radius-small: 10px;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.38);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { min-height: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 12% 8%, rgba(56, 189, 248, 0.13), transparent 30rem),
    radial-gradient(circle at 90% 2%, rgba(167, 139, 250, 0.12), transparent 34rem),
    linear-gradient(180deg, #0b1020 0%, var(--bg) 55%, #070a11 100%);
  color: var(--text);
  font-family: "Avenir Next", "Segoe UI", system-ui, -apple-system, sans-serif;
  line-height: 1.55;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.35;
  background-image: linear-gradient(rgba(148, 163, 184, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(148, 163, 184, 0.045) 1px, transparent 1px);
  background-size: 52px 52px;
  mask-image: linear-gradient(180deg, black, transparent 80%);
}

h1, h2, h3, p { margin-top: 0; }

h1, h2, h3 {
  letter-spacing: -0.025em;
  line-height: 1.1;
}

h1 { font-size: clamp(2rem, 4vw, 3.5rem); }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.05rem; }

a { color: var(--accent); }

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

button {
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.72rem 1.08rem;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease, opacity 120ms ease;
}

button:hover { transform: translateY(-1px); }
button:active { transform: translateY(0); }
button:disabled { cursor: not-allowed; opacity: 0.5; transform: none; }

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-small);
  background: rgba(7, 10, 17, 0.72);
  padding: 0.7rem 0.85rem;
  outline: none;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

textarea { resize: vertical; }

label {
  display: grid;
  gap: 0.38rem;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.hidden { display: none !important; }

.eyebrow {
  margin-bottom: 0.55rem;
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.muted { color: var(--muted); }

.primary {
  background: linear-gradient(135deg, #67e8f9, #38bdf8 55%, #818cf8);
  color: #07101c;
  font-weight: 800;
}

.secondary {
  background: rgba(125, 211, 252, 0.12);
  border-color: rgba(125, 211, 252, 0.38);
  color: var(--accent);
  font-weight: 700;
}

.ghost {
  background: transparent;
  border-color: var(--line-strong);
  color: var(--muted);
}

.ghost:hover { color: var(--text); border-color: var(--accent); }

.small { padding: 0.48rem 0.82rem; font-size: 0.8rem; }

.toast {
  position: fixed;
  z-index: 100;
  top: 1rem;
  right: 1rem;
  max-width: min(30rem, calc(100vw - 2rem));
  padding: 0.9rem 1.1rem;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.96);
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(-10px);
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { border-color: rgba(251, 113, 133, 0.5); color: #fecdd3; }
.toast.success { border-color: rgba(52, 211, 153, 0.5); color: #a7f3d0; }
.toast.warning { border-color: rgba(251, 191, 36, 0.5); color: #fde68a; }

.auth-screen {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(22rem, 0.75fr);
  gap: clamp(2rem, 6vw, 6rem);
  align-items: center;
  width: min(1180px, calc(100vw - 2rem));
  min-height: 100vh;
  margin: 0 auto;
  padding: 3rem 0;
}

.auth-hero h1 span {
  background: linear-gradient(120deg, #7dd3fc, #c4b5fd 55%, #fbbf24);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.hero-copy {
  max-width: 38rem;
  color: var(--muted);
  font-size: 1.05rem;
}

.hero-points {
  display: grid;
  gap: 0.55rem;
  padding: 0;
  margin: 1.5rem 0 0;
  list-style: none;
  color: #c4cfe2;
}

.hero-points li::before {
  content: "◇";
  margin-right: 0.65rem;
  color: var(--accent);
}

.auth-card {
  border: 1px solid var(--line);
  border-radius: 22px;
  background: linear-gradient(180deg, rgba(23, 31, 49, 0.92), rgba(10, 15, 25, 0.94));
  box-shadow: var(--shadow);
  padding: 1.25rem;
  backdrop-filter: blur(18px);
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
  padding: 0.3rem;
  margin-bottom: 1.1rem;
  border-radius: 999px;
  background: rgba(7, 10, 17, 0.6);
}

.auth-tab {
  background: transparent;
  color: var(--muted);
}

.auth-tab.active {
  background: var(--bg-soft);
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--line);
}

.auth-form, .stack-form {
  display: grid;
  gap: 1rem;
}

.auth-form { padding: 0.35rem; }
.auth-form .primary { margin-top: 0.35rem; }

.form-note {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.76rem;
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.7rem;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.9rem;
}

.field-block {
  display: grid;
  gap: 0.38rem;
  min-width: 0;
}

.field-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
}

.field-heading label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.tiny {
  padding: 0.26rem 0.58rem;
  font-size: 0.68rem;
  font-weight: 700;
}

.prompt-studio {
  display: grid;
  gap: 0.65rem;
  margin: 1.35rem 0 1.2rem;
  padding: 1rem;
  border: 1px solid rgba(167, 139, 250, 0.28);
  border-radius: var(--radius);
  background:
    radial-gradient(circle at 100% 0%, rgba(167, 139, 250, 0.16), transparent 16rem),
    rgba(17, 23, 38, 0.58);
}

.prompt-studio-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
}

.prompt-studio-heading h2 { margin-bottom: 0.25rem; }

.prompt-status {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
}

.prompt-status.warn { color: var(--gold); }

.prompt-studio .form-note { margin-top: 0; }

.suggestion-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.suggestion-chips:empty { display: none; }

.chip {
  max-width: 100%;
  padding: 0.3rem 0.62rem;
  border: 1px solid rgba(148, 163, 184, 0.28);
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.08);
  color: #cbd5e1;
  font-size: 0.7rem;
  font-weight: 600;
  line-height: 1.25;
  text-align: left;
  white-space: normal;
}

.chip:hover {
  border-color: rgba(125, 211, 252, 0.55);
  background: rgba(56, 189, 248, 0.12);
  color: var(--text);
}

.chip.generated {
  border-color: rgba(251, 191, 36, 0.38);
  background: rgba(251, 191, 36, 0.09);
  color: #fde68a;
}

.chip.generated:hover {
  border-color: rgba(251, 191, 36, 0.7);
  background: rgba(251, 191, 36, 0.16);
}

.chip.loading {
  color: var(--muted);
  border-style: dashed;
}

.checkbox-row {
  display: flex;
  align-items: center;
  align-self: end;
  gap: 0.55rem;
  min-height: 2.6rem;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  color: var(--muted);
  font-size: 0.82rem;
}

.checkbox-row input { width: auto; }

.app-shell {
  position: relative;
  z-index: 1;
  min-height: 100vh;
}

.topbar {
  position: sticky;
  z-index: 20;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem clamp(1rem, 3vw, 2rem);
  border-bottom: 1px solid var(--line);
  background: rgba(9, 12, 20, 0.82);
  backdrop-filter: blur(18px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 12px;
  background: linear-gradient(135deg, #67e8f9, #818cf8);
  color: #07101c;
  font-weight: 900;
}

.brand strong { display: block; letter-spacing: -0.02em; }
.brand small { display: block; color: var(--muted); font-size: 0.68rem; }

.user-menu {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  color: var(--muted);
  font-size: 0.82rem;
}

.layout {
  display: grid;
  grid-template-columns: minmax(17rem, 21rem) minmax(0, 1fr);
  min-height: calc(100vh - 4.2rem);
}

.library {
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
  padding: 1.4rem;
  border-right: 1px solid var(--line);
  background: rgba(8, 12, 20, 0.5);
}

.library-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.7rem;
}

.library-heading h2 { margin-bottom: 0; }

.story-list {
  display: grid;
  gap: 0.55rem;
}

.story-card {
  display: grid;
  gap: 0.35rem;
  width: 100%;
  padding: 0.78rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  background: rgba(17, 23, 38, 0.6);
  text-align: left;
}

.story-card:hover, .story-card.active {
  border-color: rgba(125, 211, 252, 0.5);
  background: rgba(56, 189, 248, 0.1);
}

.story-card strong { font-size: 0.9rem; }
.story-card small { color: var(--muted); font-size: 0.72rem; }

.story-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.role-pill, .status-pill, .tag {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  padding: 0.18rem 0.52rem;
  border-radius: 999px;
  font-size: 0.66rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.role-pill { background: var(--accent-soft); color: var(--accent); }
.status-pill { border: 1px solid var(--line-strong); color: var(--muted); }

.open-story {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
}

.open-story label {
  margin-bottom: 0.45rem;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.inline {
  display: flex;
  gap: 0.45rem;
}

.inline input { min-width: 0; }

.main-panel {
  width: min(980px, 100%);
  padding: clamp(1.25rem, 3vw, 2.4rem);
}

.empty-state, .new-story-view {
  max-width: 44rem;
  padding-top: 7vh;
}

.empty-state p { max-width: 36rem; color: var(--muted); }
.empty-state .primary { margin-top: 0.8rem; }

.story-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1.2rem;
}

.story-premise {
  max-width: 48rem;
  color: #c4cfe2;
}

.story-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.4rem;
}

.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.8rem;
}

.tag {
  border: 1px solid rgba(167, 139, 250, 0.28);
  background: rgba(167, 139, 250, 0.1);
  color: #ddd6fe;
}

.meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem 1.1rem;
  margin: 1.1rem 0;
  padding: 0.8rem 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.78rem;
}

.branch-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.7rem;
  margin: 1.2rem 0;
  padding: 0.8rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  background: rgba(17, 23, 38, 0.55);
}

.branch-bar label { display: inline; }
.branch-bar select { width: auto; min-width: 13rem; }
.branch-hint { color: var(--muted); font-size: 0.78rem; }

.panel {
  margin: 1rem 0;
  padding: 1rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.8rem;
}

.panel-heading h2 { margin-bottom: 0; }

.inline-form {
  display: flex;
  align-items: end;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 0.9rem;
}

.inline-form label { min-width: 10rem; }

.team-list, .audit-list {
  display: grid;
  gap: 0.5rem;
}

.team-member, .audit-event {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  background: rgba(7, 10, 17, 0.45);
  font-size: 0.8rem;
}

.team-member small, .audit-event small { color: var(--muted); }

.chapters {
  display: grid;
  gap: 1rem;
  margin: 1.4rem 0;
}

.chapter {
  position: relative;
  padding: clamp(1rem, 2.4vw, 1.5rem);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(23, 31, 49, 0.82), rgba(12, 17, 28, 0.9));
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.2);
}

.chapter::before {
  content: "";
  position: absolute;
  top: 1.4rem;
  bottom: 1.4rem;
  left: 0;
  width: 2px;
  border-radius: 2px;
  background: linear-gradient(180deg, var(--accent), transparent);
}

.chapter-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}

.chapter h3 { margin-bottom: 0.2rem; font-size: 1.2rem; }
.chapter .chapter-meta { color: var(--muted); font-size: 0.7rem; }

.chapter-content {
  max-width: 70ch;
  color: #dbe4f3;
  font-family: Georgia, "Iowan Old Style", "Times New Roman", serif;
  font-size: 1.02rem;
}

.chapter-content p { margin-bottom: 0.85rem; }
.chapter-content p:last-child { margin-bottom: 0; }

.ending-badge {
  border-color: rgba(251, 191, 36, 0.38);
  background: rgba(251, 191, 36, 0.12);
  color: var(--gold);
}

.choices {
  display: grid;
  gap: 0.4rem;
  padding: 0;
  margin: 1rem 0 0;
  list-style: none;
}

.choices li {
  padding: 0.5rem 0.65rem;
  border-left: 2px solid var(--line-strong);
  color: var(--muted);
  font-size: 0.8rem;
}

.world-state {
  margin-top: 0.8rem;
  color: var(--muted);
  font-size: 0.75rem;
}

.world-state summary { cursor: pointer; }
.world-state pre {
  overflow: auto;
  padding: 0.7rem;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.3);
  font-size: 0.7rem;
}

.composer {
  margin-top: 2rem;
  padding: clamp(1rem, 2.4vw, 1.5rem);
  border: 1px solid rgba(125, 211, 252, 0.26);
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(56, 189, 248, 0.08), rgba(17, 23, 38, 0.72));
}

.composer-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.composer-heading h2 { margin-bottom: 0; }
.composer-heading h2 span { color: var(--accent); }
.stack-form.compact { gap: 0.8rem; }

.fork-box {
  margin-top: 1rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--line);
}

.fork-box summary {
  cursor: pointer;
  color: var(--accent);
  font-size: 0.84rem;
  font-weight: 700;
}

.fork-box .stack-form { margin-top: 0.8rem; }

.empty-list {
  padding: 0.8rem;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius-small);
  color: var(--muted);
  font-size: 0.8rem;
}

.audit-event {
  display: grid;
  gap: 0.15rem;
}

.audit-action { font-weight: 800; color: #dbe4f3; }

@media (max-width: 900px) {
  .auth-screen { grid-template-columns: 1fr; padding: 2rem 0 3rem; }
  .auth-hero h1 { font-size: clamp(2rem, 8vw, 3rem); }
  .layout { grid-template-columns: 1fr; }
  .library { border-right: 0; border-bottom: 1px solid var(--line); }
  .story-list { grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr)); }
  .main-panel { width: 100%; }
  .story-header { flex-direction: column; }
  .story-actions { justify-content: flex-start; }
  .form-grid { grid-template-columns: 1fr; }
  .open-story { margin-top: 0; }
}

@media (max-width: 560px) {
  .topbar { align-items: flex-start; }
  .brand small { display: none; }
  .user-menu { flex-direction: column; align-items: flex-end; }
  .inline-form { align-items: stretch; }
  .inline-form label { min-width: 100%; }
  .prompt-studio-heading { flex-direction: column; }
  .prompt-studio-heading .secondary { width: 100%; }
}
