:root {
  --bg: #0b1118;
  --bg-elev: #121a24;
  --bg-soft: #182230;
  --line: rgba(255, 255, 255, 0.08);
  --text: #eef3f7;
  --muted: #8b9aab;
  --accent: #5ec4a2;
  --accent-2: #d4a35c;
  --danger: #ff7b72;
  --user: #243447;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
  --radius: 20px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "DM Sans", system-ui, sans-serif;
}

body.gate-open {
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
}

h1, h2 {
  font-family: "Instrument Serif", Georgia, serif;
  font-weight: 400;
  letter-spacing: -0.02em;
  margin: 0;
}

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

.glow {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(700px 380px at 15% -5%, rgba(94, 196, 162, 0.16), transparent 60%),
    radial-gradient(560px 320px at 95% 0%, rgba(212, 163, 92, 0.12), transparent 55%),
    linear-gradient(180deg, #0d141d 0%, #0b1118 45%, #0a0f15 100%);
  z-index: 0;
}

.gate, .shell { position: relative; z-index: 1; }

/* class display rules beat the UA [hidden] stylesheet — force hide */
.gate[hidden],
.shell[hidden] {
  display: none !important;
}

.gate {
  position: fixed;
  inset: 0;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  display: grid;
  place-items: center;
  padding: max(16px, var(--safe-t)) 18px max(16px, var(--safe-b));
  background: var(--bg);
}

.card {
  background: rgba(18, 26, 36, 0.88);
  border: 1px solid var(--line);
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow);
}

.gate-card {
  width: min(100%, 400px);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  border-radius: 28px;
  padding: 28px 24px;
  overscroll-behavior: contain;
}

.brand {
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
}

.lede, .help {
  color: var(--muted);
}

.lede { margin: 10px 0 0; line-height: 1.5; }

.field, .slider-field {
  display: grid;
  gap: 8px;
  margin-top: 18px;
}

.field span, .slider-head span, .section-label {
  font-size: 13px;
  font-weight: 600;
}

.field em { font-style: normal; font-weight: 500; color: var(--muted); }

.field input,
.field textarea,
.composer textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 13px 14px;
  background: var(--bg-soft);
  outline: none;
}

.field input:focus,
.field textarea:focus,
.composer textarea:focus {
  border-color: rgba(94, 196, 162, 0.55);
  box-shadow: 0 0 0 3px rgba(94, 196, 162, 0.14);
}

.btn {
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  cursor: pointer;
  font-weight: 600;
}

.btn.primary {
  background: linear-gradient(180deg, #6fd0ad, #49b48f);
  color: #062018;
}

.btn.primary:disabled { opacity: 0.45; cursor: not-allowed; }

.btn.cancel {
  background: linear-gradient(180deg, #3a4656, #2a3442);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.btn.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
}

.btn.wide {
  width: 100%;
  margin-top: 10px;
}

.btn.ghost:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.model-panel {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.model-panel .section-label { margin: 0 0 6px; }

.model-panel .help { margin-top: 8px; line-height: 1.45; }

.model-panel .help.ok { color: var(--accent); }

.model-panel .help.err { color: var(--danger); }

.error { color: var(--danger); font-size: 14px; margin-top: 12px; }

.shell {
  max-width: 780px;
  margin: 0 auto;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + var(--safe-t)) 14px 10px;
  position: sticky;
  top: 0;
  background: rgba(11, 17, 24, 0.82);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  z-index: 2;
}

.bar-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.bar-title strong {
  font-family: "Instrument Serif", Georgia, serif;
  font-size: 26px;
  font-weight: 400;
}

.pill {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 5px 9px;
  border-radius: 999px;
  background: rgba(94, 196, 162, 0.12);
  color: var(--accent);
  border: 1px solid rgba(94, 196, 162, 0.25);
}

.pill.busy {
  background: rgba(212, 163, 92, 0.14);
  color: var(--accent-2);
  border-color: rgba(212, 163, 92, 0.28);
}

.bar-actions { display: flex; gap: 6px; }

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.icon-btn:active { transform: scale(0.97); }

.thread {
  overflow: auto;
  padding: 16px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.empty {
  margin: auto;
  text-align: center;
  max-width: 28ch;
  color: var(--muted);
  padding: 48px 12px;
  line-height: 1.55;
}

.empty strong {
  display: block;
  color: var(--text);
  font-family: "Instrument Serif", Georgia, serif;
  font-size: 28px;
  font-weight: 400;
  margin-bottom: 8px;
}

.bubble {
  max-width: min(100%, 620px);
  padding: 12px 14px;
  border-radius: 18px;
  line-height: 1.55;
  word-break: break-word;
  animation: rise 200ms ease-out;
}

.bubble.user {
  align-self: flex-end;
  background: var(--user);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-bottom-right-radius: 6px;
}

.bubble.assistant {
  align-self: flex-start;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
}

.bubble .who {
  display: block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 6px;
}

.bubble .md {
  min-width: 0;
}

.bubble .md > *:first-child { margin-top: 0; }
.bubble .md > *:last-child { margin-bottom: 0; }

.bubble .md p,
.bubble .md ul,
.bubble .md ol,
.bubble .md blockquote,
.bubble .md pre {
  margin: 0.55em 0;
}

.bubble .md h1,
.bubble .md h2,
.bubble .md h3,
.bubble .md h4,
.bubble .md h5,
.bubble .md h6 {
  font-family: "Instrument Serif", Georgia, serif;
  font-weight: 400;
  line-height: 1.25;
  margin: 0.7em 0 0.35em;
}

.bubble .md h1 { font-size: 1.45em; }
.bubble .md h2 { font-size: 1.3em; }
.bubble .md h3 { font-size: 1.15em; }

.bubble .md ul,
.bubble .md ol {
  padding-left: 1.35em;
}

.bubble .md li { margin: 0.2em 0; }

.bubble .md blockquote {
  border-left: 3px solid rgba(94, 196, 162, 0.45);
  padding: 0.15em 0 0.15em 0.85em;
  color: var(--muted);
}

.bubble .md hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 0.9em 0;
}

.bubble .md a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.bubble .md code {
  font-family: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 6px;
  padding: 0.1em 0.35em;
}

.bubble .md pre {
  overflow-x: auto;
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 12px 14px;
  -webkit-overflow-scrolling: touch;
}

.bubble .md pre code {
  background: transparent;
  border: 0;
  padding: 0;
  font-size: 0.86em;
  line-height: 1.45;
  white-space: pre;
}

.bubble .md del { opacity: 0.7; }

.composer-wrap {
  padding: 8px 14px calc(10px + var(--safe-b));
  position: sticky;
  bottom: 0;
  background: linear-gradient(180deg, transparent, rgba(11, 17, 24, 0.92) 28%);
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
  border-radius: 22px;
  padding: 10px;
}

.composer textarea {
  resize: none;
  min-height: 44px;
  max-height: 150px;
  border: 0;
  background: transparent;
  padding: 10px 8px;
  box-shadow: none;
}

.composer textarea:focus { box-shadow: none; border: 0; }

.send { min-width: 84px; height: 44px; }

dialog.sheet {
  border: 0;
  padding: 0;
  background: transparent;
  width: min(94vw, 420px);
  margin: auto;
}

dialog.sheet::backdrop {
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
}

.sheet-body {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 28px;
  padding: 18px 18px 16px;
  box-shadow: var(--shadow);
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.sheet-head h2 { font-size: 28px; }

.slider-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.slider-head output {
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  font-weight: 700;
}

.slider-field input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.help { margin: 0; font-size: 12px; }

.sheet-actions {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-top: 20px;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 640px) {
  .bar-title strong { font-size: 22px; }
  .send { min-width: 72px; }
}
