:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #f0efec;
  --text: #141414;
  --muted: #5f5f5b;
  --border: #e2e1dc;
  --accent: #e4141a;
  --accent-ink: #ffffff;
  --accent-soft: #fde8e8;
  --error: #b3261e;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0f0f;
    --surface: #181818;
    --surface-2: #222221;
    --text: #f2f2f0;
    --muted: #a3a39e;
    --border: #2e2e2c;
    --accent: #ff3b40;
    --accent-ink: #ffffff;
    --accent-soft: #3a1414;
    --error: #ff8a80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }

.brand-mark {
  width: 44px; height: 44px; border-radius: 12px;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink);
  font-weight: 800; letter-spacing: .5px;
}
.brand-mark.small { width: 34px; height: 34px; border-radius: 9px; font-size: 13px; }

.btn-primary {
  background: var(--accent); color: var(--accent-ink);
  border: 0; border-radius: 10px; padding: 10px 18px;
  font-weight: 600; cursor: pointer;
}
.btn-primary:disabled { opacity: .55; cursor: default; }
.btn-ghost {
  background: transparent; border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 14px; cursor: pointer;
}
.btn-ghost:hover { background: var(--surface-2); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Sign-in */
.signin { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.signin-card {
  width: 100%; max-width: 400px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px; padding: 32px 28px;
  display: flex; flex-direction: column; gap: 10px;
}
.signin-card h1 { margin: 10px 0 0; font-size: 24px; }
.signin-card p { margin: 0 0 10px; }
.signin-card label { font-weight: 600; font-size: 14px; }
.signin-card input {
  padding: 11px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg);
}
.signin-card .btn-primary { margin-top: 8px; padding: 12px; }
.error { color: var(--error); font-size: 14px; margin: 0; }

/* App shell */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.title-main { font-weight: 700; }
.title-sub { font-size: 12px; color: var(--muted); }
.top-actions { display: flex; gap: 8px; }

.thread {
  flex: 1; overflow-y: auto;
  padding: 24px 16px 8px;
  display: flex; flex-direction: column; gap: 18px;
}
.thread > * { width: 100%; max-width: 780px; margin: 0 auto; }

.welcome h2 { margin: 24px 0 6px; font-size: 22px; }
.welcome p { margin: 0 0 18px; }
.starters { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.starters button {
  text-align: left; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface);
  cursor: pointer; font-size: 14px; line-height: 1.4;
}
.starters button:hover { border-color: var(--accent); }

.msg { display: flex; flex-direction: column; gap: 6px; }
.msg.user { align-items: flex-end; }
.bubble { padding: 12px 16px; border-radius: var(--radius); overflow-wrap: anywhere; }
.msg.user .bubble {
  background: var(--accent-soft); max-width: 85%;
  white-space: pre-wrap; border-bottom-right-radius: 4px;
}
.msg.assistant .bubble { background: var(--surface); border: 1px solid var(--border); }
.bubble p { margin: 0 0 10px; }
.bubble p:last-child, .bubble ul:last-child, .bubble ol:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.bubble li { margin: 3px 0; }
.bubble h3, .bubble h4 { margin: 14px 0 6px; font-size: 16px; }
.bubble code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 14px; }
.bubble .sources {
  margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border);
  font-size: 13px; color: var(--muted);
}
.bubble.error-bubble { border-color: var(--error); color: var(--error); }
.msg-actions { display: flex; gap: 8px; }
.msg-actions button {
  background: none; border: 0; color: var(--muted);
  font-size: 13px; cursor: pointer; padding: 2px 4px;
}
.msg-actions button:hover { color: var(--text); }

.status-line { font-size: 13px; color: var(--muted); padding-left: 4px; min-height: 1em; }
.msg-actions .cost { font-size: 12px; color: var(--muted); align-self: center; }
.session-cost { font-size: 12px; color: var(--muted); align-self: center; white-space: nowrap; }
.top-actions { align-items: center; }

.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* Mode switch */
.modebar {
  max-width: 780px; margin: 0 auto 8px;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px;
}
.modes { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.mode {
  border: 0; background: transparent; border-radius: 8px;
  padding: 6px 12px; font-size: 14px; cursor: pointer; color: var(--muted);
}
.mode.active { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.format { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.format select {
  padding: 6px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); font-size: 14px;
}
.msg-tag { font-size: 12px; color: var(--muted); }

/* Composer */
.composer-wrap {
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border); background: var(--surface);
}
.composer {
  max-width: 780px; margin: 0 auto;
  display: flex; gap: 8px; align-items: flex-end;
}
.composer textarea {
  flex: 1; resize: none; max-height: 200px;
  padding: 11px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg);
}
.composer .send { padding: 11px 18px; }
.composer .attach { padding: 9px 10px; display: inline-flex; align-items: center; }
.composer .attach.has-files { color: var(--accent); border-color: var(--accent); }
.fineprint { max-width: 780px; margin: 8px auto 0; font-size: 12px; color: var(--muted); text-align: center; }

/* Attached files (shown above the question box for the whole conversation) */
.attachments { max-width: 780px; margin: 0 auto 8px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.attachment {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  padding: 4px 6px 4px 10px; border-radius: 999px; font-size: 13px;
  border: 1px solid var(--border); background: var(--surface-2);
}
.attachment .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }
.attachment .meta { color: var(--muted); font-size: 12px; }
.attachment button {
  border: 0; background: none; cursor: pointer; line-height: 1; padding: 2px 5px; border-radius: 999px;
  color: var(--muted); font-size: 15px;
}
.attachment button:hover { background: var(--border); color: inherit; }
.attachments .attach-error { width: 100%; font-size: 13px; color: var(--accent); }
.attachments .attach-busy { font-size: 13px; color: var(--muted); }
.msg.user .files { font-size: 12px; color: var(--muted); margin: 4px 4px 0 0; text-align: right; }

/* Uploaded transcripts (super admin) */
.up-row, .learned-row {
  display: flex; flex-direction: column; gap: 6px;
  padding: 12px 0; border-top: 1px solid var(--border);
}
.up-row:first-child, .learned-row:first-child { border-top: 0; }
.up-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; flex-wrap: wrap; }
.up-title { font-weight: 600; }
.up-actions, .learned-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.badge.ok { color: #1b7f3b; border-color: #1b7f3b33; background: #1b7f3b12; }
.badge.warn { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.learned-row.undone .learned-text, .learned-row.rejected .learned-text { color: var(--muted); text-decoration: line-through; }
.learned-text { white-space: pre-wrap; }
.learned-why { font-size: 13px; color: var(--muted); }
.show-more { margin-top: 8px; }

@media (max-width: 560px) {
  .title-sub, .session-cost { display: none; }
  .btn-ghost { padding: 7px 10px; font-size: 14px; }
  .msg.user .bubble { max-width: 92%; }
  .starters { grid-template-columns: 1fr; }
}

/* Layout wrappers */
.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.signin-card .btn-ghost { margin-top: 2px; }
.small { font-size: 13px; }
.bubble blockquote {
  margin: 0 0 10px; padding: 6px 12px;
  border-left: 3px solid var(--accent); background: var(--surface-2); border-radius: 0 8px 8px 0;
}

/* Users (super admin) */
.admin-view { flex: 1; overflow-y: auto; padding: 24px 16px 40px; }
.admin-inner { max-width: 880px; margin: 0 auto; display: flex; flex-direction: column; gap: 16px; }
.admin-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.admin-head h2 { margin: 0; font-size: 22px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px 20px;
}
.card h3 { margin: 0 0 12px; font-size: 16px; }
.notice { border-color: var(--accent); background: var(--accent-soft); }
.notice p { margin: 0 0 8px; }
.pw-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 8px; }
.pw-row code {
  font-size: 17px; padding: 6px 10px; border-radius: 8px;
  background: var(--surface); border: 1px solid var(--border); user-select: all;
}
.add-user .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.add-user label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; }
.add-user input:not([type="checkbox"]) {
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); font-weight: 400;
}
.perm-set { border: 0; padding: 0; margin: 14px 0; display: flex; flex-wrap: wrap; gap: 18px; }
.perm-set legend { font-size: 14px; font-weight: 600; margin-bottom: 8px; padding: 0; }
.perm-set label { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

.user-list { display: flex; flex-direction: column; }
.user-row {
  display: grid; grid-template-columns: minmax(180px, 1.2fr) 2fr auto; gap: 12px; align-items: center;
  padding: 14px 0; border-top: 1px solid var(--border);
}
.user-row:first-child { border-top: 0; padding-top: 0; }
.user-row.inactive .user-name { text-decoration: line-through; color: var(--muted); }
.user-name { font-weight: 600; }
.user-perms { display: flex; flex-wrap: wrap; gap: 8px 16px; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; cursor: pointer; }
.toggle.locked { cursor: default; color: var(--muted); }
.user-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.btn-ghost.small { padding: 6px 10px; font-size: 13px; }
.btn-ghost.danger { color: var(--error); border-color: var(--error); }

@media (max-width: 720px) {
  .user-row { grid-template-columns: 1fr; }
  .user-actions { justify-content: flex-start; }
  .add-user .grid { grid-template-columns: 1fr; }
  .top-actions { flex-wrap: wrap; justify-content: flex-end; }
}

.msg .continue { align-self: flex-start; padding: 8px 16px; }

/* Admin tabs + AI settings */
.admin-tabs { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; flex-wrap: wrap; }
.admin-tab {
  border: 0; background: transparent; border-radius: 8px; padding: 7px 14px;
  font-size: 15px; cursor: pointer; color: var(--muted);
}
.admin-tab.active { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.admin-tabpanel { display: flex; flex-direction: column; gap: 16px; }
.admin-tabpanel[hidden] { display: none !important; }
.provider-pick { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.provider-card {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; cursor: pointer;
  border: 1px solid var(--border); border-radius: 12px; background: var(--bg);
}
.provider-card > span { display: flex; flex-direction: column; }
.provider-card.selected { border-color: var(--accent); background: var(--accent-soft); }
.provider-card input { accent-color: var(--accent); width: 18px; height: 18px; }
.ai-form { display: flex; flex-direction: column; gap: 18px; }
.ai-form h3 { margin-bottom: 0; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field-label { font-weight: 600; font-size: 14px; }
.field p { margin: 0; }
.key-status { font-size: 14px; padding: 8px 12px; border-radius: 8px; background: var(--surface-2); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.key-status.missing { font-family: inherit; color: var(--error); background: transparent; padding: 0; }
.inline-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.inline-row input, .inline-row select { flex: 1 1 260px; min-width: 0; }
.ai-form input:not([type="checkbox"]):not([type="radio"]), .ai-form select {
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); font-size: 15px;
}
.model-custom { width: 100%; }
.segmented { display: inline-flex; flex-wrap: wrap; gap: 4px; background: var(--surface-2); padding: 3px; border-radius: 10px; align-self: flex-start; }
.seg { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; cursor: pointer; font-size: 14px; color: var(--muted); }
.seg.active { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.check { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600; cursor: pointer; }
.sub-field { display: flex; flex-direction: column; gap: 8px; padding: 12px 14px; border-left: 3px solid var(--border); }
.price-row { display: grid; grid-template-columns: minmax(140px, 1.4fr) repeat(3, 1fr); gap: 8px; align-items: end; }
.price-model { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; padding-bottom: 10px; overflow-wrap: anywhere; }
.price { display: flex; flex-direction: column; gap: 4px; }
.price input { width: 100%; }
.ai-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.ai-status { margin: 0; font-size: 14px; min-height: 1.2em; }
.ai-status.good { color: #1e7d3c; }
.ai-status.bad { color: var(--error); }
@media (prefers-color-scheme: dark) { .ai-status.good { color: #6fd08c; } }
@media (max-width: 720px) {
  .provider-pick { grid-template-columns: 1fr; }
  .price-row { grid-template-columns: 1fr 1fr 1fr; }
  .price-model { grid-column: 1 / -1; padding-bottom: 0; }
}

/* Talk to Stallone */
.btn-talk {
  display: inline-flex; align-items: center; gap: 8px; white-space: nowrap;
  background: var(--text); color: var(--bg); border: 0; border-radius: 10px;
  padding: 11px 16px; font-weight: 600; cursor: pointer;
}
.btn-talk:hover { opacity: .9; }
@media (max-width: 560px) { .btn-talk span { display: none; } .btn-talk { padding: 11px 13px; } }

.voice-view {
  flex: 1; display: flex; flex-direction: column; min-height: 0;
  background: radial-gradient(ellipse at center, var(--accent-soft) 0%, var(--bg) 70%);
}
.voice-view[hidden] { display: none !important; }
.voice-top { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; }
.voice-title { font-weight: 700; }
.voice-stage { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 22px; padding: 16px; text-align: center; }
.voice-orb {
  --level: 0;
  position: relative; width: 200px; height: 200px; border-radius: 50%;
  border: 0; padding: 0; cursor: pointer; background: transparent;
}
.orb-core {
  position: absolute; inset: 30px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ff6a6e, var(--accent) 55%, #9d0b10);
  box-shadow: 0 12px 40px rgba(228, 20, 26, .35);
  transform: scale(calc(1 + var(--level) * .18));
  transition: transform .08s linear, filter .3s;
}
.orb-ring { position: absolute; inset: 10px; border-radius: 50%; border: 3px solid rgba(228, 20, 26, .25); }
.voice-orb[data-state="idle"] .orb-core { filter: saturate(.6) brightness(1.05); }
.voice-orb[data-state="listening"] .orb-ring,
.voice-orb[data-state="hearing"] .orb-ring { animation: orb-pulse 1.6s ease-out infinite; }
.voice-orb[data-state="transcribing"] .orb-ring,
.voice-orb[data-state="thinking"] .orb-ring {
  border-color: rgba(228, 20, 26, .15); border-top-color: var(--accent); animation: orb-spin 1s linear infinite;
}
.voice-orb[data-state="speaking"] .orb-core { animation: orb-talk .5s ease-in-out infinite alternate; }
@keyframes orb-pulse { 0% { transform: scale(.92); opacity: 1; } 100% { transform: scale(1.18); opacity: 0; } }
@keyframes orb-spin { to { transform: rotate(360deg); } }
@keyframes orb-talk { from { transform: scale(1); } to { transform: scale(1.07); } }
@media (prefers-reduced-motion: reduce) {
  .voice-orb .orb-ring, .voice-orb .orb-core { animation: none !important; }
}
.voice-state { font-size: 22px; font-weight: 600; min-height: 1.4em; }
.voice-hint { max-width: 420px; min-height: 1.3em; }
.voice-bottom { padding: 12px 16px calc(16px + env(safe-area-inset-bottom)); max-width: 780px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: 10px; }
.voice-text { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 4px; max-height: 30vh; overflow-y: auto; }
.voice-text[hidden] { display: none !important; }
.vt-label { margin-top: 6px; }
.vt-q, .vt-a { white-space: pre-wrap; }
@media (max-width: 560px) { .voice-orb { width: 160px; height: 160px; } .voice-state { font-size: 19px; } }

.btn-talk.needs-setup { background: var(--surface-2); color: var(--text); border: 1px dashed var(--border); }

.speed-slider { width: 100%; max-width: 420px; accent-color: var(--accent); }
.speed-scale { position: relative; display: flex; justify-content: space-between; max-width: 420px; }
/* 1.0x sits 60% along a 0.7-1.2 slider */
.speed-scale span:nth-child(2) { position: absolute; left: 60%; transform: translateX(-50%); }
