:root {
  --ink: #10131a;
  --muted: #677083;
  --line: #d9dee8;
  --paper: #f5f7fb;
  --panel: #ffffff;
  --teal: #107c83;
  --coral: #df4d3f;
  --gold: #d4941e;
  --green: #2c7a4b;
  --blue: #2869d8;
  --shadow: 0 18px 50px rgba(14, 20, 34, 0.14);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

a {
  color: inherit;
  text-decoration: none;
}

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

main {
  min-height: calc(100vh - 66px);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 66px;
  padding: 10px clamp(16px, 4vw, 38px);
  background: rgba(245, 247, 251, 0.9);
  border-bottom: 1px solid rgba(16, 19, 26, 0.08);
  backdrop-filter: blur(16px);
}

.brand,
.nav,
.nav form {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand {
  min-width: 0;
  font-weight: 800;
}

.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background:
    radial-gradient(circle at 30% 30%, #fff 0 10%, transparent 12%),
    conic-gradient(from 40deg, var(--coral), var(--gold), var(--teal), var(--blue), var(--coral));
  box-shadow: 0 6px 18px rgba(16, 19, 26, 0.22);
}

.nav {
  gap: 6px;
  font-size: 0.95rem;
  font-weight: 700;
}

.nav a,
.link-button {
  padding: 8px 10px;
  color: #253044;
  border-radius: 8px;
}

.nav a:hover,
.link-button:hover {
  background: rgba(16, 19, 26, 0.06);
}

.link-button {
  background: transparent;
  border: 0;
  cursor: pointer;
}

.flash-stack {
  position: fixed;
  top: 76px;
  right: 16px;
  z-index: 40;
  display: grid;
  gap: 8px;
  width: min(360px, calc(100vw - 32px));
}

.flash {
  padding: 12px 14px;
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
  border-left: 5px solid var(--teal);
}

.flash.error {
  border-color: var(--coral);
}

.flash.success {
  border-color: var(--green);
}

.primary-button,
.ghost-button,
.choice-button,
.icon-button {
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
}

.primary-button {
  padding: 11px 18px;
  color: #fff;
  background: linear-gradient(135deg, #111827, #26364f);
  box-shadow: 0 10px 26px rgba(17, 24, 39, 0.22);
}

.primary-button:hover {
  transform: translateY(-1px);
}

.primary-button.compact {
  min-height: 38px;
  padding: 9px 13px;
  white-space: nowrap;
}

.ghost-button {
  padding: 10px 14px;
  color: #fff;
  background: rgba(255, 255, 255, 0.17);
  border: 1px solid rgba(255, 255, 255, 0.28);
}

.ghost-button.dark {
  color: var(--ink);
  background: #fff;
  border-color: var(--line);
}

.dashboard-shell,
.form-shell,
.wizard-shell,
.journal-shell,
.shared-shell {
  width: min(1160px, calc(100vw - 32px));
  margin: 0 auto;
  padding: clamp(22px, 5vw, 54px) 0;
}

.form-shell.wide {
  width: min(1320px, calc(100vw - 32px));
}

.section-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: end;
  margin-bottom: 22px;
}

.section-head.lower {
  margin-top: 34px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--teal);
  font-size: 0.78rem;
  font-weight: 900;
  text-transform: uppercase;
}

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

h1 {
  margin-bottom: 0;
  font-size: clamp(2rem, 5vw, 4.4rem);
  line-height: 0.95;
}

h2 {
  margin-bottom: 12px;
  font-size: clamp(1.35rem, 3vw, 2.2rem);
  line-height: 1.05;
}

.quiet,
small {
  color: var(--muted);
}

.auth-shell {
  min-height: calc(100vh - 66px);
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(140deg, rgba(16, 124, 131, 0.1), transparent 38%),
    linear-gradient(320deg, rgba(223, 77, 63, 0.11), transparent 42%),
    var(--paper);
}

.auth-panel,
.settings-panel,
.journal-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.auth-panel {
  width: min(440px, 100%);
  padding: clamp(22px, 5vw, 36px);
}

.stack-form,
.wizard-fields,
.pill-groups,
.provider-list {
  display: grid;
  gap: 16px;
}

label,
fieldset {
  min-width: 0;
}

label span,
legend {
  display: block;
  margin-bottom: 7px;
  color: #293246;
  font-weight: 800;
}

input,
textarea,
select {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  color: var(--ink);
  background: #fff;
  border: 1px solid #c9d1df;
  border-radius: 8px;
  outline: none;
}

textarea {
  resize: vertical;
  line-height: 1.45;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(16, 124, 131, 0.18);
}

.adventure-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 16px;
}

.adventure-card,
.empty-state,
.template-mini,
.template-choice,
.shared-hero,
.scene-panel {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  isolation: isolate;
}

.adventure-card {
  min-height: 260px;
  color: #fff;
  box-shadow: var(--shadow);
}

.card-shade,
.scene-overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.06), rgba(0, 0, 0, 0.72));
  z-index: -1;
}

.card-content {
  position: absolute;
  inset: auto 0 0;
  padding: 22px;
}

.card-content span,
.template-mini span,
.template-choice small {
  display: block;
  margin-bottom: 6px;
  font-size: 0.78rem;
  font-weight: 900;
  text-transform: uppercase;
}

.card-content h2 {
  margin-bottom: 8px;
}

.card-content p {
  margin-bottom: 0;
  color: rgba(255, 255, 255, 0.86);
}

.empty-state {
  min-height: 300px;
  display: grid;
  align-content: end;
  justify-items: start;
  gap: 10px;
  padding: 26px;
  color: #fff;
  box-shadow: var(--shadow);
}

.template-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
}

.template-mini {
  min-height: 130px;
  display: flex;
  flex-direction: column;
  justify-content: end;
  padding: 14px;
  color: #fff;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
}

.settings-grid.tight {
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.settings-panel {
  display: grid;
  gap: 16px;
  padding: 20px;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.toggle-row span {
  margin: 0;
}

.toggle-row small {
  display: block;
  margin-top: 3px;
  font-weight: 500;
}

.toggle-row input,
.mini-check input {
  width: 22px;
  min-height: 22px;
  flex: 0 0 auto;
}

.provider-row {
  display: grid;
  grid-template-columns: minmax(210px, 1.2fr) minmax(150px, 1fr) minmax(170px, 1fr) minmax(170px, 1fr) 90px 100px;
  gap: 12px;
  align-items: end;
  padding: 14px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.provider-toggle {
  margin: 0;
}

.mini-check {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
}

.mini-check span {
  margin: 0;
}

.wizard-form {
  display: grid;
  gap: 18px;
}

.wizard-band {
  display: grid;
  grid-template-columns: minmax(170px, 0.34fr) minmax(0, 1fr);
  gap: 18px;
  padding: 18px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 32px rgba(16, 19, 26, 0.06);
}

.wizard-copy h2 {
  position: sticky;
  top: 88px;
  font-size: 1.35rem;
}

.template-select-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 12px;
}

.template-choice {
  min-height: 210px;
  cursor: pointer;
  color: #fff;
}

.template-choice input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.template-choice input:checked + .choice-overlay {
  outline: 4px solid #fff;
  box-shadow: inset 0 0 0 6px rgba(16, 124, 131, 0.72);
}

.template-choice input:focus-visible + .choice-overlay {
  outline: 4px solid rgba(255, 255, 255, 0.95);
}

.choice-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: end;
  margin-bottom: 0;
  padding: 16px;
  color: #fff;
  background:
    linear-gradient(180deg, rgba(0, 0, 0, 0.22), rgba(0, 0, 0, 0.9)),
    linear-gradient(90deg, rgba(0, 0, 0, 0.72), rgba(0, 0, 0, 0.08));
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.95);
}

.choice-overlay strong {
  color: #fff;
  font-size: 1.18rem;
  line-height: 1.05;
}

.choice-overlay em {
  margin-top: 8px;
  font-style: normal;
  color: rgba(255, 255, 255, 0.94);
  line-height: 1.35;
}

.choice-overlay small {
  color: rgba(255, 255, 255, 0.78);
}

fieldset {
  padding: 0;
  border: 0;
}

.pill-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.pill {
  cursor: pointer;
}

.pill input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.pill span {
  display: block;
  margin: 0;
  padding: 9px 12px;
  background: #edf2f7;
  border: 1px solid #d6deea;
  border-radius: 8px;
  font-weight: 800;
}

.pill input:checked + span {
  color: #fff;
  background: var(--teal);
  border-color: var(--teal);
}

.wizard-actions {
  display: flex;
  justify-content: end;
}

.story-layout {
  height: calc(100vh - 66px);
  display: grid;
  grid-template-columns: minmax(300px, 0.42fr) minmax(0, 1fr);
  overflow: hidden;
}

.scene-panel {
  min-height: 100%;
  color: #fff;
  border-radius: 0;
}

.scene-content {
  position: absolute;
  inset: auto 0 0;
  display: grid;
  gap: 12px;
  padding: clamp(20px, 4vw, 34px);
}

.scene-content h1 {
  font-size: clamp(2rem, 4vw, 4.2rem);
}

.scene-content p {
  max-width: 62ch;
  color: rgba(255, 255, 255, 0.86);
}

.scene-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chat-shell {
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto auto;
  background: #f8fafc;
}

.message-list {
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: clamp(16px, 3vw, 30px);
}

.message {
  width: min(780px, 94%);
  padding: 14px 16px;
  border-radius: 8px;
  box-shadow: 0 10px 24px rgba(16, 19, 26, 0.06);
}

.message.assistant {
  align-self: flex-start;
  background: #fff;
  border: 1px solid var(--line);
}

.message.user {
  align-self: flex-end;
  color: #fff;
  background: linear-gradient(135deg, var(--teal), #155e75);
}

.message-meta {
  margin-bottom: 6px;
  font-size: 0.78rem;
  font-weight: 900;
  text-transform: uppercase;
  color: var(--muted);
}

.message.user .message-meta {
  color: rgba(255, 255, 255, 0.76);
}

.message-body {
  white-space: pre-line;
  line-height: 1.58;
}

.choice-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 12px clamp(16px, 3vw, 30px);
  border-top: 1px solid var(--line);
}

.choice-button {
  flex: 0 0 auto;
  max-width: min(320px, 80vw);
  padding: 9px 12px;
  color: #203047;
  background: #fff;
  border: 1px solid #cbd5e1;
  white-space: normal;
}

.choice-button:hover {
  border-color: var(--teal);
}

.composer {
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) auto;
  gap: 10px;
  padding: 12px clamp(16px, 3vw, 30px) calc(12px + env(safe-area-inset-bottom));
  background: #fff;
  border-top: 1px solid var(--line);
}

.composer textarea {
  max-height: 160px;
}

.icon-button {
  width: 48px;
  padding: 0;
  color: #fff;
  background: var(--coral);
  text-transform: uppercase;
  font-size: 0.72rem;
}

.icon-button[disabled] {
  opacity: 0.35;
  cursor: not-allowed;
}

.journal-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(280px, 0.9fr);
  gap: 16px;
}

.journal-panel {
  padding: 20px;
}

.state-list {
  display: grid;
  gap: 10px;
}

.state-list dt {
  font-weight: 900;
  color: var(--teal);
}

.state-list dd {
  margin: 0 0 12px;
  line-height: 1.5;
}

.state-list ul {
  margin: 0;
  padding-left: 20px;
}

.character-list {
  display: grid;
  gap: 12px;
}

.character-card {
  padding: 14px;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.character-card h3 {
  margin-bottom: 6px;
}

.character-card p {
  margin-bottom: 8px;
  line-height: 1.45;
}

.character-card span {
  font-weight: 900;
}

.journal-actions,
.inline-form,
.save-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.journal-actions {
  margin-top: 18px;
}

.inline-form input {
  width: min(240px, 100%);
}

.save-strip {
  margin-top: 18px;
}

.save-chip {
  display: grid;
  gap: 3px;
  padding: 10px 12px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.section-head.mini {
  margin-bottom: 14px;
}

.section-head.mini h2 {
  font-size: 1.35rem;
}

.admin-test-panel,
.admin-log-panel {
  display: grid;
  gap: 14px;
  margin-top: 26px;
  padding: 18px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 32px rgba(16, 19, 26, 0.06);
}

.admin-test-controls {
  display: grid;
  grid-template-columns: minmax(180px, 0.4fr) minmax(220px, 1fr);
  gap: 12px;
}

.admin-test-log {
  min-height: 220px;
  max-height: 360px;
  overflow-y: auto;
  display: grid;
  align-content: start;
  gap: 10px;
  padding: 12px;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.admin-test-log:empty::before {
  content: "No test messages yet.";
  color: var(--muted);
  font-weight: 700;
}

.admin-test-message {
  max-width: min(720px, 100%);
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
}

.admin-test-message.user {
  justify-self: end;
  color: #fff;
  background: linear-gradient(135deg, var(--teal), #155e75);
  border-color: transparent;
}

.admin-test-message.user .message-meta {
  color: rgba(255, 255, 255, 0.74);
}

.admin-test-message.error {
  border-color: rgba(223, 77, 63, 0.45);
  background: #fff5f4;
}

.admin-test-composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
}

.admin-log-path {
  min-height: 20px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
  word-break: break-word;
}

.admin-log-output {
  min-height: 260px;
  max-height: 460px;
  overflow: auto;
  margin: 0;
  padding: 14px;
  color: #182033;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: 0.86rem/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  white-space: pre-wrap;
}

.shared-hero {
  min-height: 360px;
  display: flex;
  align-items: end;
  padding: clamp(22px, 5vw, 44px);
  color: #fff;
  box-shadow: var(--shadow);
}

.shared-hero h1 {
  margin-bottom: 12px;
}

.shared-story {
  display: grid;
  gap: 14px;
  padding: 22px 0;
}

.visual-starship {
  background-image:
    linear-gradient(180deg, rgba(5, 12, 24, 0.08), rgba(5, 12, 24, 0.72)),
    url("../img/setting-starship.png");
}

.visual-greece {
  background-image:
    linear-gradient(180deg, rgba(27, 45, 74, 0.02), rgba(31, 20, 12, 0.68)),
    url("../img/setting-greece.png");
}

.visual-dino {
  background-image:
    linear-gradient(180deg, rgba(8, 31, 23, 0.02), rgba(8, 31, 23, 0.75)),
    url("../img/setting-dino.png");
}

.visual-wizard {
  background-image:
    linear-gradient(180deg, rgba(25, 13, 38, 0.05), rgba(25, 13, 38, 0.74)),
    url("../img/setting-wizard.png");
}

.visual-superhero {
  background-image:
    linear-gradient(180deg, rgba(10, 32, 68, 0.02), rgba(10, 32, 68, 0.72)),
    url("../img/setting-superhero.png");
}

.visual-western {
  background-image:
    linear-gradient(180deg, rgba(43, 22, 11, 0.02), rgba(43, 22, 11, 0.72)),
    url("../img/setting-western.png");
}

.visual-custom {
  background:
    radial-gradient(circle at 24% 24%, rgba(223, 77, 63, 0.48), transparent 18%),
    radial-gradient(circle at 78% 42%, rgba(16, 124, 131, 0.55), transparent 22%),
    linear-gradient(135deg, #111827 0%, #334155 52%, #2c7a4b 100%);
}

.visual-starship,
.visual-greece,
.visual-dino,
.visual-wizard,
.visual-superhero,
.visual-western,
.visual-custom {
  background-size: cover;
  background-position: center;
}

@media (max-width: 980px) {
  .provider-row,
  .wizard-band,
  .journal-grid,
  .admin-test-controls {
    grid-template-columns: 1fr;
  }

  .wizard-copy h2 {
    position: static;
    margin-bottom: 0;
  }

  .story-layout {
    height: auto;
    min-height: calc(100vh - 66px);
    grid-template-columns: 1fr;
    grid-template-rows: minmax(210px, 34vh) minmax(0, 1fr);
    overflow: visible;
  }

  .scene-panel {
    min-height: 230px;
  }

  .chat-shell {
    min-height: calc(66vh - 66px);
  }
}

@media (max-width: 660px) {
  .topbar {
    align-items: start;
    gap: 8px;
    flex-direction: column;
  }

  main {
    min-height: calc(100vh - 104px);
  }

  .nav {
    width: 100%;
    overflow-x: auto;
    padding-bottom: 2px;
  }

  .section-head {
    align-items: start;
    flex-direction: column;
  }

  h1 {
    font-size: clamp(2rem, 13vw, 3.2rem);
  }

  .story-layout {
    min-height: calc(100vh - 104px);
    grid-template-rows: 210px minmax(0, 1fr);
  }

  .composer {
    grid-template-columns: 42px minmax(0, 1fr);
  }

  .send-button {
    grid-column: 1 / -1;
  }

  .message {
    width: 100%;
  }

  .admin-test-composer {
    grid-template-columns: 1fr;
  }
}
