/* SampleChat - mobile-first styling, no framework, no build step. */

:root {
  --bg: #0b0b14;
  --bg-raised: #14141f;
  --bg-input: #1b1b28;
  --border: #262635;
  --border-strong: #35354a;
  --text: #edecf5;
  --text-dim: #9b9aad;
  --text-faint: #6f6e82;
  --accent: #7c5cff;
  --accent-strong: #9277ff;
  --accent-soft: rgba(124, 92, 255, 0.14);
  --danger: #ff6b6b;
  --danger-soft: rgba(255, 107, 107, 0.12);
  --radius: 16px;
  --radius-sm: 11px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, sans-serif;
}

* { box-sizing: border-box; }

/* The UA's `hidden` rule is only display:none, so any author `display` below
   would silently beat it. Everything here toggles visibility through the hidden
   attribute, so make it win outright. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

/* Radial glow behind everything, keeps the dark theme from feeling flat. */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  background:
    radial-gradient(80% 55% at 50% -10%, rgba(124, 92, 255, 0.22), transparent 70%),
    radial-gradient(60% 40% at 100% 100%, rgba(64, 196, 255, 0.08), transparent 70%);
  pointer-events: none;
  z-index: 0;
}

/* ------------------------------------------------------------------ Views -- */

.view {
  display: none;
  position: relative;
  z-index: 1;
  min-height: 100%;
  padding: calc(var(--safe-top) + 12px) 18px calc(var(--safe-bottom) + 24px);
}

.view--active { display: block; }

.view--chat.view--active {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-height: 0;
  padding: calc(var(--safe-top) + 8px) 0 0;
}

/* ------------------------------------------------------------------- Auth -- */

.auth {
  max-width: 440px;
  margin: 0 auto;
  padding-top: 8vh;
}

.brand { text-align: center; margin-bottom: 30px; }

.brand__mark {
  width: 62px;
  height: 62px;
  margin: 0 auto 16px;
  border-radius: 19px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 22px;
  letter-spacing: 0.5px;
  color: #fff;
  background: linear-gradient(145deg, var(--accent), #4d7cff);
  box-shadow: 0 12px 32px rgba(124, 92, 255, 0.4);
}

.brand__name {
  margin: 0;
  font-size: 27px;
  font-weight: 650;
  letter-spacing: -0.4px;
}

.brand__tag {
  margin: 6px 0 0;
  color: var(--text-dim);
  font-size: 14.5px;
}

/* ------------------------------------------------------------------ Cards -- */

.card {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.stack {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ------------------------------------------------------------------ Forms -- */

.form { display: flex; flex-direction: column; gap: 18px; }

.field { display: flex; flex-direction: column; gap: 7px; }

.field__label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: 0.2px;
}

.field__help {
  font-size: 12.5px;
  color: var(--text-faint);
  line-height: 1.45;
}

.field__counter {
  font-size: 11.5px;
  color: var(--text-faint);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.field__input {
  width: 100%;
  padding: 13px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-input);
  color: var(--text);
  font-family: inherit;
  font-size: 16px; /* 16px keeps iOS Safari from zooming on focus. */
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field__input::placeholder { color: var(--text-faint); }

.field__input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.field__input--area {
  resize: vertical;
  min-height: 150px;
  line-height: 1.55;
}

/* ---------------------------------------------------------------- Buttons -- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 13px 18px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 15.5px;
  font-weight: 600;
  color: var(--text);
  background: var(--bg-input);
  cursor: pointer;
  transition: transform 0.08s ease, filter 0.15s ease, background 0.15s ease;
}

.btn:active:not(:disabled) { transform: scale(0.985); }

.btn:disabled { opacity: 0.55; cursor: default; }

.btn--primary {
  background: linear-gradient(145deg, var(--accent), #5b6bff);
  color: #fff;
  box-shadow: 0 8px 22px rgba(124, 92, 255, 0.32);
}

.btn--primary:hover:not(:disabled) { filter: brightness(1.07); }

.btn--ghost {
  background: transparent;
  border-color: var(--border-strong);
  color: var(--text-dim);
}

.btn--block { width: 100%; }

.btn--small { padding: 8px 13px; font-size: 13.5px; }

.btn--danger { background: var(--danger-soft); color: var(--danger); }

/* Inline spinner shown while a button's action is in flight. */
.btn.is-busy { position: relative; color: transparent; }

.btn.is-busy .btn__label { visibility: hidden; }

.btn.is-busy::after {
  content: "";
  position: absolute;
  width: 17px;
  height: 17px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

.iconbtn {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--bg-raised);
  color: var(--text-dim);
  cursor: pointer;
}

.iconbtn:active { transform: scale(0.94); }

.iconbtn svg { width: 19px; height: 19px; }

/* ----------------------------------------------------------------- Topbar -- */

.topbar {
  max-width: 640px;
  margin: 0 auto 20px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.topbar--chat {
  max-width: none;
  margin: 0;
  padding: 0 14px 10px;
  border-bottom: 1px solid var(--border);
}

.topbar__titles { flex: 1 1 auto; min-width: 0; }

.topbar__title {
  margin: 0;
  font-size: 20px;
  font-weight: 650;
  letter-spacing: -0.3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__subtitle {
  margin: 2px 0 0;
  font-size: 12.5px;
  color: var(--text-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ------------------------------------------------------------------ Tiles -- */

.tile {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 18px;
  text-align: left;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.08s ease, border-color 0.15s ease;
}

.tile:active { transform: scale(0.99); }

.tile:hover { border-color: var(--border-strong); }

.tile__icon {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 13px;
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.tile__icon svg { width: 22px; height: 22px; }

.tile__body { flex: 1 1 auto; min-width: 0; display: block; }

.tile__title { display: block; font-size: 16.5px; font-weight: 600; }

.tile__hint {
  display: block;
  margin-top: 3px;
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.4;
}

.tile__chevron { color: var(--text-faint); font-size: 22px; line-height: 1; }

/* ------------------------------------------------------------- Story list -- */

.story {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 15px 16px;
}

.story__main {
  flex: 1 1 auto;
  min-width: 0;
  text-align: left;
  background: none;
  border: 0;
  padding: 0;
  color: inherit;
  font-family: inherit;
  cursor: pointer;
}

.story__title {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.story__meta { margin-top: 3px; font-size: 12.5px; color: var(--text-faint); }

/* ----------------------------------------------------------------- Alerts -- */

.alert {
  margin: 0;
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  line-height: 1.45;
}

.alert--error {
  background: var(--danger-soft);
  border: 1px solid rgba(255, 107, 107, 0.28);
  color: #ffb3b3;
}

.status {
  margin: 4px 0;
  color: var(--text-faint);
  font-size: 13.5px;
  text-align: center;
}

.empty { padding: 40px 20px; text-align: center; color: var(--text-dim); }

.empty__title { font-size: 16px; font-weight: 600; color: var(--text); }

.empty__hint { margin-top: 6px; font-size: 13.5px; }

/* ------------------------------------------------------ Story settings -- */

/* One card per editor (system prompt, story context, key story points) on
   their own full screen, rather than all three stacked in a popup within the
   chat view -- which is what got cramped once there were three of them. */
.settings-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.settings-card__title {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--text-faint);
}

.settings-card__help {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.45;
  color: var(--text-faint);
}

.settings-card__help code {
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--bg-input);
  color: var(--accent-strong);
  font-size: 11.5px;
}

.settings-card__actions {
  display: flex;
  align-items: center;
  gap: 9px;
  flex-wrap: wrap;
}

.settings-card__status {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 12px;
  line-height: 1.35;
  color: var(--text-faint);
}

.settings-card__status--saved { color: #6fd18a; }

.settings-card__status--warn { color: #e8c46a; }

/* --------------------------------------------------------------- Messages -- */

.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.bubble {
  max-width: 86%;
  padding: 11px 14px;
  border-radius: 17px;
  font-size: 15.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  animation: rise 0.18s ease-out;
}

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

.bubble--user {
  align-self: flex-end;
  background: linear-gradient(145deg, var(--accent), #5b6bff);
  color: #fff;
  border-bottom-right-radius: 6px;
}

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

/* *Asterisked* passages, rendered as italics by setBubbleText. */
.bubble em {
  font-style: italic;
  opacity: 0.92;
}

/* The user bubble whose text is currently loaded in the composer. */
.bubble--editing {
  outline: 2px dashed rgba(255, 255, 255, 0.55);
  outline-offset: 2px;
  opacity: 0.75;
}

.bubble--intro {
  align-self: center;
  max-width: 100%;
  background: transparent;
  border: 1px dashed var(--border-strong);
  color: var(--text-dim);
  font-size: 13.5px;
  text-align: center;
}

/* Typing indicator, shown until the first token arrives. */
.typing { display: inline-flex; gap: 4px; padding: 3px 0; }

.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-faint);
  animation: bounce 1.15s infinite ease-in-out;
}

.typing span:nth-child(2) { animation-delay: 0.16s; }
.typing span:nth-child(3) { animation-delay: 0.32s; }

/* Elapsed-time note shown while the model is still reasoning. */
.typing__note {
  display: block;
  margin-top: 5px;
  font-size: 12px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.recall {
  align-self: flex-start;
  font-size: 11.5px;
  color: var(--text-faint);
  padding-left: 4px;
}

/* Sits under the newest user bubble, the only one that can be rewritten. */
.msg-actions {
  align-self: flex-end;
  display: flex;
  gap: 6px;
  margin-top: -4px;
  padding-right: 4px;
}

.editbtn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.editbtn svg { width: 12px; height: 12px; }

.editbtn:hover:not(:disabled) { color: var(--text); border-color: var(--accent); }

.editbtn:disabled { opacity: 0.4; cursor: default; }

.chat-error { flex: 0 0 auto; margin: 0 14px 8px; }

/* --------------------------------------------------------------- Composer -- */

.composer {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 9px;
  padding: 9px 14px calc(var(--safe-bottom) + 10px);
  border-top: 1px solid var(--border);
  background: rgba(11, 11, 20, 0.92);
  backdrop-filter: blur(12px);
}

.composer__row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

/* Per-turn reasoning effort. */
.pick {
  display: flex;
  align-items: center;
  gap: 7px;
  align-self: flex-start;
}

.pick__label {
  font-size: 11.5px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.pick__select {
  appearance: none;
  padding: 5px 26px 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--bg-input)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238a8aa3' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E")
    no-repeat right 7px center / 13px 13px;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
}

.pick__select:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.pick__select:disabled { opacity: 0.55; cursor: default; }

/* Banner shown while the composer holds an edit rather than a new message. */
.editing {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0;
  font-size: 12.5px;
  color: var(--accent-strong);
}

.editing > span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.linkbtn {
  flex: 0 0 auto;
  padding: 0;
  border: 0;
  background: none;
  color: var(--text-dim);
  font: inherit;
  font-size: 12.5px;
  text-decoration: underline;
  cursor: pointer;
}

.linkbtn:hover { color: var(--text); }

.composer__input {
  flex: 1 1 auto;
  max-height: 132px;
  padding: 11px 14px;
  border-radius: 20px;
  border: 1px solid var(--border-strong);
  background: var(--bg-input);
  color: var(--text);
  font-family: inherit;
  font-size: 16px;
  line-height: 1.45;
  resize: none;
  overflow-y: auto;
}

.composer__input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.composer__input:disabled { opacity: 0.6; }

.composer__send {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: linear-gradient(145deg, var(--accent), #5b6bff);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(124, 92, 255, 0.35);
  transition: transform 0.08s ease, opacity 0.15s ease;
}

.composer__send svg { width: 20px; height: 20px; }

.composer__send:active:not(:disabled) { transform: scale(0.93); }

.composer__send:disabled {
  opacity: 0.45;
  box-shadow: none;
  cursor: default;
}

/* ----------------------------------------------------------------- Larger -- */

@media (min-width: 720px) {
  .topbar--chat { padding-left: 24px; padding-right: 24px; }
  .messages { padding: 24px max(24px, calc(50% - 380px)); }
  .composer { padding-left: max(24px, calc(50% - 380px)); padding-right: max(24px, calc(50% - 380px)); }
  .bubble { max-width: 78%; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
