/* ==========================================================================
   Green Room — AI character chat
   Palette: warm ink + stage-amber. Display face: Fraunces. Body: Instrument Sans.
   ========================================================================== */

:root {
  --bg: #14161d;
  --bg-raise: #1b1e27;
  --bg-raise-2: #232733;
  --line: #2e3340;
  --text: #eae7de;
  --muted: #9b9890;
  --accent: #e9a83c;          /* stage amber */
  --accent-ink: #241a05;
  --accent-soft: rgba(233, 168, 60, 0.14);
  --user-bubble: #37404f;
  --bot-bubble: #1f232e;
  --danger: #e26d5a;
  --ok: #77b28c;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  --radius: 14px;
  --radius-sm: 9px;
  --font-body: "Instrument Sans", -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-display: "Fraunces", Georgia, serif;
  --font-mono: ui-monospace, "JetBrains Mono", "SF Mono", Menlo, monospace;
}

[data-theme="light"] {
  --bg: #f2efe7;
  --bg-raise: #fbfaf6;
  --bg-raise-2: #edeadf;
  --line: #ddd8ca;
  --text: #26241d;
  --muted: #6f6c61;
  --accent: #b0741a;
  --accent-ink: #fffaf0;
  --accent-soft: rgba(176, 116, 26, 0.12);
  --user-bubble: #e3ddcd;
  --bot-bubble: #fbfaf6;
  --danger: #bb4430;
  --ok: #3e7a56;
  --shadow: 0 10px 28px rgba(60, 50, 20, 0.14);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
  overflow: hidden;
}

h1, h2, h3 { font-family: var(--font-display); font-weight: 600; letter-spacing: 0.01em; }

button { font-family: inherit; }
input, textarea, select { font-family: inherit; font-size: 14px; color: var(--text); }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

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

/* ---------- layout ---------- */

.app { display: flex; height: 100vh; height: 100dvh; }

/* ---------- left rail ---------- */

.rail {
  width: 292px;
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--bg-raise);
  border-right: 1px solid var(--line);
  transition: margin-left 0.25s ease;
}
.rail.collapsed { margin-left: -292px; }

.rail-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 14px 10px;
}
.brand { display: flex; gap: 10px; align-items: center; }
.brand-mark { font-size: 26px; filter: saturate(0.9); }
.brand h1 { font-size: 21px; line-height: 1.1; }
.brand-sub { font-size: 11.5px; color: var(--muted); letter-spacing: 0.06em; text-transform: uppercase; }

.rail-search { padding: 4px 14px 12px; }
.rail-search input {
  width: 100%; padding: 8px 12px;
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius-sm);
}
.rail-search input:focus { border-color: var(--accent); outline: none; }

.cast { flex: 1; overflow-y: auto; padding: 2px 8px 8px; }

.cast-item { border-radius: var(--radius-sm); margin-bottom: 2px; }
.cast-item-main {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 8px 8px;
  background: none; border: none; border-radius: var(--radius-sm);
  color: var(--text); cursor: pointer; text-align: left;
}
.cast-item-main:hover { background: var(--bg-raise-2); }
.cast-item.active .cast-item-main {
  background: var(--accent-soft);
  box-shadow: inset 2px 0 0 var(--accent);
}
.cast-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cast-tag { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cast-item-info { min-width: 0; flex: 1; }
.cast-badge {
  font-size: 10px; padding: 1px 6px; border-radius: 20px;
  border: 1px solid var(--line); color: var(--muted); flex: 0 0 auto;
}

/* ---------- avatars: the signature ---------- */

.avatar {
  width: 36px; height: 36px; flex: 0 0 auto;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-display); font-weight: 600; font-size: 15px;
  color: #fff;
  background: linear-gradient(135deg, var(--av-a, #666), var(--av-b, #333));
  box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, 0.14);
  overflow: hidden;
  user-select: none;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.emoji { font-size: 20px; background: var(--bg-raise-2); box-shadow: inset 0 0 0 1.5px var(--line); }
.avatar-lg { width: 44px; height: 44px; font-size: 18px; }
.avatar-lg.emoji { font-size: 24px; }
.avatar-xl { width: 64px; height: 64px; font-size: 26px; }
.avatar-xl.emoji { font-size: 36px; }
.avatar-sm { width: 30px; height: 30px; font-size: 13px; }
.avatar-sm.emoji { font-size: 17px; }

/* ---------- rail footer ---------- */

.rail-foot { padding: 12px 14px 14px; border-top: 1px solid var(--line); display: grid; gap: 8px; }
.rail-foot-row { display: flex; gap: 6px; }
.rail-foot-row .btn { flex: 1; font-size: 12.5px; padding: 6px 4px; }

.health { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 auto; }
.dot-ok { background: var(--ok); }
.dot-bad { background: var(--danger); }
.dot-idle { background: var(--muted); }

/* ---------- buttons ---------- */

.btn {
  padding: 8px 16px; border-radius: var(--radius-sm);
  border: 1px solid transparent; cursor: pointer;
  font-weight: 600; font-size: 14px;
  transition: filter 0.15s, background 0.15s;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn-ghost:hover:not(:disabled) { background: var(--bg-raise-2); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-danger:hover:not(:disabled) { background: var(--danger); color: #fff; }
.btn-block { width: 100%; }
.btn-sm { padding: 4px 10px; font-size: 12.5px; }

.icon-btn {
  width: 32px; height: 32px; border-radius: var(--radius-sm);
  background: transparent; border: 1px solid var(--line);
  color: var(--text); cursor: pointer; font-size: 15px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background 0.15s;
}
.icon-btn:hover:not(:disabled) { background: var(--bg-raise-2); }
.icon-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.link-btn {
  background: none; border: none; color: var(--muted);
  cursor: pointer; font-size: 12px; text-decoration: underline; text-underline-offset: 2px;
}
.link-btn:hover:not(:disabled) { color: var(--accent); }
.link-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- stage ---------- */

.stage { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.stage-head {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-raise);
}
.stage-who { display: flex; align-items: center; gap: 12px; min-width: 0; flex: 1; }
.stage-titles { min-width: 0; }
.stage-titles h2 { font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stage-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stage-tools { display: flex; align-items: center; gap: 6px; }

.model-select {
  max-width: 240px; padding: 7px 8px;
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius-sm);
  cursor: pointer;
}
.model-select:focus { border-color: var(--accent); outline: none; }

.stage-body { flex: 1; display: flex; min-height: 0; }

/* conversations column */
.chats-col {
  width: 216px; flex: 0 0 auto;
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  background: color-mix(in srgb, var(--bg-raise) 55%, var(--bg));
}
.chats-col-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; font-size: 11.5px; letter-spacing: 0.07em;
  text-transform: uppercase; color: var(--muted);
}
.chats-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.chat-entry {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 8px; border-radius: var(--radius-sm);
  cursor: pointer; margin-bottom: 2px;
}
.chat-entry:hover { background: var(--bg-raise-2); }
.chat-entry.active { background: var(--accent-soft); }
.chat-entry-title { flex: 1; min-width: 0; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-entry-date { font-size: 10.5px; color: var(--muted); }
.chat-entry-del {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 13px; visibility: hidden; padding: 2px;
}
.chat-entry:hover .chat-entry-del { visibility: visible; }
.chat-entry-del:hover { color: var(--danger); }

/* messages */
.chat-col { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.messages {
  flex: 1; overflow-y: auto;
  padding: 22px 24px 12px;
  display: flex; flex-direction: column; gap: 14px;
}

.empty-stage {
  margin: auto; text-align: center; color: var(--muted); max-width: 420px;
}
.empty-mark { font-size: 44px; margin-bottom: 10px; opacity: 0.75; }
.empty-stage h3 { color: var(--text); margin-bottom: 6px; font-size: 22px; }
.empty-stage p { font-size: 13.5px; }

.msg { display: flex; gap: 10px; max-width: 78%; position: relative; }
.msg.user { margin-left: auto; flex-direction: row-reverse; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  border-top-left-radius: 4px;
  background: var(--bot-bubble);
  border: 1px solid var(--line);
  min-width: 0;
  overflow-wrap: anywhere;
}
.msg.user .msg-bubble {
  background: var(--user-bubble);
  border-top-left-radius: var(--radius);
  border-top-right-radius: 4px;
}
.msg-meta { font-size: 10.5px; color: var(--muted); margin-top: 4px; }

.msg-actions {
  position: absolute; top: -10px; right: 6px;
  display: flex; gap: 4px; opacity: 0;
  transition: opacity 0.15s;
}
.msg:hover .msg-actions, .msg:focus-within .msg-actions { opacity: 1; }
.msg.user .msg-actions { right: auto; left: 6px; }
.msg-action {
  width: 24px; height: 24px; font-size: 12px;
  border-radius: 6px; border: 1px solid var(--line);
  background: var(--bg-raise); color: var(--text); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.msg-action:hover { background: var(--accent-soft); border-color: var(--accent); }

/* streaming caret */
.msg-bubble.streaming::after {
  content: "▍"; color: var(--accent);
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: bounce 1s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }

/* markdown inside bubbles */
.msg-bubble p { margin: 0 0 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble code {
  font-family: var(--font-mono); font-size: 12.5px;
  background: rgba(127, 127, 127, 0.16);
  padding: 1px 5px; border-radius: 4px;
}
.msg-bubble pre {
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid var(--line);
  padding: 10px 12px; border-radius: var(--radius-sm);
  overflow-x: auto; margin: 8px 0;
}
[data-theme="light"] .msg-bubble pre { background: rgba(0, 0, 0, 0.05); }
.msg-bubble pre code { background: none; padding: 0; font-size: 12.5px; }
.msg-bubble blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 10px; margin: 8px 0; color: var(--muted);
}
.msg-bubble ul, .msg-bubble ol { padding-left: 20px; margin: 6px 0; }
.msg-bubble li { margin: 3px 0; }
.msg-bubble h1, .msg-bubble h2, .msg-bubble h3 { font-size: 1.05em; margin: 10px 0 6px; }
.msg-bubble a { color: var(--accent); }
.msg-bubble hr { border: none; border-top: 1px solid var(--line); margin: 10px 0; }
.msg-bubble strong { font-weight: 600; }

.msg-error { color: var(--danger); font-size: 13px; }

/* ---------- composer ---------- */

.composer { padding: 10px 24px 14px; border-top: 1px solid var(--line); background: var(--bg-raise); }
.composer-inner { display: flex; gap: 10px; align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none;
  max-height: 180px;
  padding: 10px 14px;
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius);
  line-height: 1.5;
}
.composer textarea:focus { border-color: var(--accent); outline: none; }
.send-btn { padding: 10px 20px; }
.composer-meta {
  display: flex; gap: 14px; align-items: center;
  font-size: 11.5px; color: var(--muted); padding: 6px 4px 0;
}
.composer-actions { margin-left: auto; }

/* ---------- drawer (character editor) ---------- */

.drawer-backdrop, .modal-backdrop {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(8, 8, 12, 0.55);
  backdrop-filter: blur(2px);
}
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 70;
  width: min(440px, 100vw);
  background: var(--bg-raise);
  border-left: 1px solid var(--line);
  box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  animation: slide-in 0.22s ease;
}
@keyframes slide-in { from { transform: translateX(30px); opacity: 0; } }

.drawer-head, .modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px 10px;
}
.drawer-head h2, .modal-head h2 { font-size: 20px; }
.drawer-body { flex: 1; overflow-y: auto; padding: 6px 18px 18px; display: grid; gap: 6px; align-content: start; }
.drawer-foot { display: flex; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); align-items: center; }
.spacer { flex: 1; }

.field-label { font-size: 12px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); margin-top: 10px; }
.field-label .hint { text-transform: none; font-weight: 400; letter-spacing: 0; }
.drawer-body input[type="text"], .drawer-body textarea, .drawer-body select {
  width: 100%; padding: 9px 12px;
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius-sm);
}
.drawer-body input:focus, .drawer-body textarea:focus, .drawer-body select:focus { border-color: var(--accent); outline: none; }
.drawer-body textarea { resize: vertical; line-height: 1.5; }

.avatar-row { display: flex; gap: 14px; align-items: center; margin-top: 6px; }
.avatar-controls { flex: 1; }
.avatar-inputs { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-top: 4px; }
.avatar-inputs input { width: 92px; padding: 7px 10px; background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius-sm); }

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

/* ---------- modal ---------- */

.modal-backdrop { display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal {
  width: min(680px, 100%); max-height: min(82vh, 760px);
  background: var(--bg-raise); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  animation: pop-in 0.18s ease;
}
.modal-sm { width: min(420px, 100%); }
@keyframes pop-in { from { transform: scale(0.97); opacity: 0; } }
.modal-note { padding: 0 18px 10px; font-size: 13px; color: var(--muted); }
.modal-body { overflow-y: auto; padding: 4px 18px 18px; }

.prov-group-title {
  font-size: 11.5px; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--muted); margin: 14px 0 6px;
}
.prov {
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  padding: 12px; margin-bottom: 8px;
  background: var(--bg);
}
.prov-head { display: flex; align-items: center; gap: 8px; }
.prov-name { font-weight: 600; }
.prov-status { font-size: 11px; padding: 1px 8px; border-radius: 20px; border: 1px solid var(--line); color: var(--muted); }
.prov-status.on { border-color: var(--ok); color: var(--ok); }
.prov-note { font-size: 12px; color: var(--muted); margin: 5px 0 8px; }
.prov-note a { color: var(--accent); }
.prov-form { display: flex; gap: 6px; flex-wrap: wrap; }
.prov-form input {
  flex: 1; min-width: 180px; padding: 7px 10px;
  background: var(--bg-raise); border: 1px solid var(--line); border-radius: var(--radius-sm);
  font-family: var(--font-mono); font-size: 12.5px;
}
.prov-form input:focus { border-color: var(--accent); outline: none; }
.prov-result { font-size: 12px; margin-top: 6px; }
.prov-result.ok { color: var(--ok); }
.prov-result.bad { color: var(--danger); }

/* ---------- toasts ---------- */

.toast-zone { position: fixed; top: 16px; right: 16px; z-index: 100; display: grid; gap: 8px; }
.toast {
  padding: 10px 16px; border-radius: var(--radius-sm);
  background: var(--bg-raise-2); border: 1px solid var(--line);
  color: var(--text); font-size: 13.5px; box-shadow: var(--shadow);
  animation: pop-in 0.18s ease;
}
.toast.ok { border-color: var(--ok); }
.toast.bad { border-color: var(--danger); }

/* ---------- scrollbars ---------- */

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ---------- responsive ---------- */

.only-mobile { display: none; }

@media (max-width: 900px) {
  .only-mobile { display: inline-flex; }
  .rail {
    position: fixed; inset: 0 auto 0 0; z-index: 50;
    box-shadow: var(--shadow);
    margin-left: -292px;
  }
  .rail.open { margin-left: 0; }
  .rail.collapsed { margin-left: -292px; }
  #btnCollapseRail { display: inline-flex; }
  .chats-col { display: none; }
  .msg { max-width: 92%; }
  .messages { padding: 16px 12px 8px; }
  .composer { padding: 8px 12px 12px; }
  .model-select { max-width: 140px; }
  .stage-sub { display: none; }
}

/* ===================================================================
   Accounts, auth, and cloud-sync UI (added with Supabase integration)
   =================================================================== */

.account { margin: 8px 0 6px; }

.acct {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 9px;
}
.acct-email {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.settings-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 14px;
  margin-bottom: 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(127, 127, 127, 0.07);
  font-size: 13px;
  color: var(--muted);
}
.settings-banner .btn { flex: 0 0 auto; }

.prov-disabled { opacity: 0.6; }

.cast-cloud {
  font-size: 11px;
  opacity: 0.7;
  margin-left: 2px;
}

/* The hidden attribute must always win over any display rule
   (e.g. .modal-backdrop { display:flex }) — without this, every
   modal renders open on page load. */
[hidden] { display: none !important; }

/* Auth modal form layout — .modal-body has no form styling of its own
   (the editor drawer gets it from .drawer-body), so mirror it here. */
#authBackdrop .modal-body {
  display: grid;
  gap: 6px;
  align-content: start;
  padding: 6px 18px 14px;
}
#authBackdrop .field-label { display: block; }
#authBackdrop input[type="email"],
#authBackdrop input[type="password"] {
  width: 100%;
  padding: 9px 12px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}
#authBackdrop input:focus { border-color: var(--accent); outline: none; }

/* ===== AI Characters rebrand: larger avatars + SVG icon sizing ===== */
.avatar { width: 42px; height: 42px; }
.avatar-sm { width: 38px; height: 38px; font-size: 14px; }
.avatar-lg { width: 52px; height: 52px; font-size: 20px; }
.brand-mark { display: inline-flex; color: var(--accent); filter: none; }
.brand-mark svg { width: 26px; height: 26px; }
.icon-btn svg { width: 16px; height: 16px; }
.btn svg.btn-ico { width: 14px; height: 14px; vertical-align: -2px; margin-right: 3px; }
.cast-cloud svg { width: 12px; height: 12px; vertical-align: -1px; }
.empty-mark svg { width: 56px; height: 56px; }
