:root {
  --bg: #0f172a;
  --bg2: #1e293b;
  --bg3: #273549;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --red: #ef4444;
  --red-dark: #dc2626;
  --bubble-ai: #1e293b;
  --bubble-user: #b91c1c;
  --radius: 18px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: 'Inter', ui-sans-serif, system-ui, -apple-system, sans-serif;
  overscroll-behavior-y: contain;
}

#app {
  display: flex; flex-direction: column;
  height: 100dvh; max-width: 760px; margin: 0 auto;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  background: rgba(15, 23, 42, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(148, 163, 184, 0.14);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1 {
  margin: 0; font-size: 19px; font-weight: 800; letter-spacing: 0.2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.orb {
  width: 34px; height: 34px; border-radius: 50%; flex: 0 0 auto;
  background: radial-gradient(circle at 35% 30%, #f87171, #dc2626 70%);
  display: flex; align-items: center; justify-content: center; gap: 3px;
  box-shadow: 0 2px 12px rgba(239, 68, 68, 0.45);
}
.orb .bar { width: 3px; background: #fff; border-radius: 2px; }
.orb .bar:nth-child(1) { height: 10px; }
.orb .bar:nth-child(2) { height: 16px; }
.orb .bar:nth-child(3) { height: 12px; }
.topbar-actions { display: flex; gap: 4px; }

.icon-btn {
  width: 44px; height: 44px; border: none; border-radius: 12px;
  background: transparent; color: var(--muted);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--bg2); color: var(--text); }
.icon-btn.active { color: var(--red); }
.icon-btn.hidden { display: none; }
.icon-btn.mic.listening { color: #fff; background: var(--red); animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(239,68,68,.5);} 50% { box-shadow: 0 0 0 8px rgba(239,68,68,0);} }

/* ---------- chat ---------- */
#chat {
  flex: 1; overflow-y: auto; padding: 16px 14px 8px;
  display: flex; flex-direction: column; gap: 12px;
  -webkit-overflow-scrolling: touch;
}
.msg { display: flex; flex-direction: column; max-width: 88%; animation: rise 0.22s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(8px);} to { opacity: 1; transform: none;} }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.ai { align-self: flex-start; align-items: flex-start; }
.bubble {
  padding: 11px 15px; border-radius: var(--radius);
  font-size: 15.5px; line-height: 1.55; word-wrap: break-word; white-space: pre-wrap;
}
.msg.user .bubble { background: var(--bubble-user); color: #fff; border-bottom-right-radius: 6px; }
.msg.ai .bubble { background: var(--bubble-ai); color: var(--text); border-bottom-left-radius: 6px; border: 1px solid rgba(148,163,184,0.12); }
.msg .who { font-size: 11px; color: var(--muted); margin: 0 4px 4px; font-weight: 600; letter-spacing: 0.4px; text-transform: uppercase; }
.msg.error .bubble { background: #451a1a; border: 1px solid #7f1d1d; color: #fca5a5; cursor: pointer; }

.typing { display: flex; gap: 5px; padding: 14px 16px; }
.typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0.25; transform: none;} 30% { opacity: 1; transform: translateY(-3px);} }

.empty-state { text-align: center; margin: auto; padding: 24px; color: var(--muted); max-width: 320px; }
.empty-state .orb { width: 64px; height: 64px; margin: 0 auto 16px; }
.empty-state .orb .bar:nth-child(1) { height: 18px; width: 5px; }
.empty-state .orb .bar:nth-child(2) { height: 30px; width: 5px; }
.empty-state .orb .bar:nth-child(3) { height: 22px; width: 5px; }
.empty-state h2 { color: var(--text); font-size: 20px; margin: 0 0 8px; }
.empty-state p { font-size: 14px; line-height: 1.6; margin: 0; }

/* ---------- composer ---------- */
#composerWrap {
  padding: 8px 12px calc(8px + var(--safe-bottom));
  background: rgba(15, 23, 42, 0.92); backdrop-filter: blur(12px);
  border-top: 1px solid rgba(148, 163, 184, 0.14);
}
#composer { display: flex; align-items: flex-end; gap: 8px; }
#composer .mic { flex: 0 0 auto; background: var(--bg2); border-radius: 14px; }
#input {
  flex: 1; resize: none; border: 1px solid rgba(148,163,184,0.2);
  background: var(--bg2); color: var(--text); border-radius: 14px;
  padding: 12px 14px; font-size: 16px; font-family: inherit; line-height: 1.4;
  max-height: 140px; outline: none;
}
#input:focus { border-color: var(--red); }
.send-btn {
  flex: 0 0 auto; width: 48px; height: 48px; border: none; border-radius: 14px;
  background: var(--red); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, transform 0.1s;
}
.send-btn:hover { background: var(--red-dark); }
.send-btn:active { transform: scale(0.94); }
.send-btn:disabled { opacity: 0.45; cursor: default; }
.powered { text-align: center; font-size: 11px; color: var(--muted); margin: 8px 0 0; letter-spacing: 0.3px; }

/* ---------- settings modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(2, 6, 23, 0.7); backdrop-filter: blur(4px);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal.hidden { display: none; }
.modal-card {
  width: 100%; max-width: 480px; background: var(--bg2);
  border-radius: 20px 20px 0 0; padding: 20px 20px calc(20px + var(--safe-bottom));
  border-top: 1px solid rgba(148,163,184,0.16);
  animation: slideup 0.25s ease-out; max-height: 88dvh; overflow-y: auto;
}
@keyframes slideup { from { transform: translateY(40px); opacity: 0;} to { transform: none; opacity: 1;} }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.modal-head h2 { margin: 0; font-size: 18px; }
.field { display: block; margin-bottom: 18px; }
.field > span { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.5px; }
.field.row { display: flex; align-items: center; justify-content: space-between; }
#nameInput {
  width: 100%; padding: 12px 14px; font-size: 16px; font-family: inherit;
  background: var(--bg); border: 1px solid rgba(148,163,184,0.2);
  border-radius: 12px; color: var(--text); outline: none;
}
#nameInput:focus { border-color: var(--red); }
.vibe-row { display: flex; gap: 8px; }
.vibe-pill {
  flex: 1; padding: 12px 8px; border-radius: 12px; font-size: 14px; font-weight: 600;
  background: var(--bg); color: var(--muted); border: 1px solid rgba(148,163,184,0.2);
  cursor: pointer; font-family: inherit;
}
.vibe-pill[aria-checked="true"] { background: rgba(239,68,68,0.15); color: #fca5a5; border-color: var(--red); }
.switch {
  width: 52px; height: 30px; border-radius: 15px; border: none; cursor: pointer;
  background: var(--bg3); position: relative; transition: background 0.2s; padding: 0;
}
.switch .knob {
  position: absolute; top: 3px; left: 3px; width: 24px; height: 24px;
  border-radius: 50%; background: #fff; transition: left 0.2s;
}
.switch[aria-checked="true"] { background: var(--red); }
.switch[aria-checked="true"] .knob { left: 25px; }
.hint { font-size: 13px; color: var(--muted); background: var(--bg); border-radius: 10px; padding: 10px 12px; margin-bottom: 16px; line-height: 1.5; }
.hint.hidden { display: none; }
.danger-btn {
  width: 100%; padding: 13px; border-radius: 12px; font-size: 15px; font-weight: 600;
  background: transparent; color: #fca5a5; border: 1px solid #7f1d1d; cursor: pointer; font-family: inherit;
}
.danger-btn:hover { background: rgba(127,29,29,0.25); }
.primary-btn {
  width: 100%; padding: 13px; border-radius: 12px; font-size: 15px; font-weight: 700;
  background: var(--red); color: #fff; border: none; cursor: pointer; font-family: inherit;
  margin-top: 4px;
}
.primary-btn:hover { filter: brightness(1.1); }
.primary-btn:disabled { opacity: 0.6; cursor: default; }
.err {
  font-size: 13px; color: #fca5a5; background: rgba(127,29,29,0.25);
  border-radius: 10px; padding: 10px 12px; margin-top: 10px; line-height: 1.5;
}
.err.hidden { display: none; }
.field input[type="email"], .field input[type="password"] {
  width: 100%; padding: 12px 14px; border-radius: 12px; font-size: 15px;
  background: var(--bg); border: 1px solid var(--bg3); color: var(--text); font-family: inherit;
  margin-bottom: 10px; box-sizing: border-box;
}
.field input[type="email"]:focus, .field input[type="password"]:focus {
  outline: none; border-color: var(--red);
}

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-bottom)); transform: translateX(-50%);
  background: var(--bg3); color: var(--text); padding: 12px 18px; border-radius: 12px;
  font-size: 14px; z-index: 60; box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  max-width: 88vw; text-align: center; animation: rise 0.2s ease-out;
}
.toast.hidden { display: none; }

@media (min-width: 640px) {
  .modal { align-items: center; padding: 24px; }
  .modal-card { border-radius: 20px; padding-bottom: 20px; }
  .msg { max-width: 75%; }
}

/* ---------- voice & language settings ---------- */
.select {
  width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg2); color: var(--text); font-size: 16px; min-height: 44px;
}
.select:focus { outline: none; border-color: var(--red); }
.gender-row { display: flex; gap: 8px; margin-bottom: 10px; }
.gender-row .vibe-pill { flex: 1; text-align: center; }
.voice-list {
  max-height: 220px; overflow-y: auto; border: 1px solid var(--line);
  border-radius: 12px; background: var(--bg2);
}
.voice-group-label {
  padding: 10px 12px 4px; font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted);
}
.voice-row {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  cursor: pointer; border-top: 1px solid var(--line); min-height: 44px;
}
.voice-row:first-of-type { border-top: none; }
.voice-row:hover { background: rgba(255,255,255,0.04); }
.voice-row.active { background: rgba(239,68,68,0.12); }
.voice-name { flex: 1; font-size: 14px; }
.voice-tag {
  font-size: 10px; padding: 2px 8px; border-radius: 10px; margin-left: 6px;
  background: rgba(255,255,255,0.08); color: var(--muted); text-transform: capitalize;
}
.voice-preview {
  flex-shrink: 0; padding: 8px 14px; min-height: 36px; border-radius: 10px;
  border: 1px solid var(--red); background: transparent; color: var(--red);
  font-size: 13px; font-weight: 600; cursor: pointer;
}
.voice-preview:hover { background: rgba(239,68,68,0.12); }
input[type="range"] {
  width: 100%; accent-color: var(--red); min-height: 44px; cursor: pointer;
}

/* ---------- live typewriter ---------- */
.type-cursor {
  display: inline-block; width: 2px; height: 1em; margin-left: 2px;
  background: var(--red); vertical-align: -0.15em;
  animation: blink 0.8s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.msg .bubble { cursor: default; }

/* ---------- replay button on companion messages ---------- */
.msg-actions { display: flex; justify-content: flex-start; margin-top: 2px; padding-left: 2px; }
.replay-btn {
  width: 30px; height: 30px; border-radius: 50%;
  background: transparent; border: 1px solid rgba(148,163,184,0.28);
  color: var(--muted); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity 0.18s, background 0.15s, color 0.15s, transform 0.1s;
  padding: 0;
}
.msg:hover .replay-btn, .msg:focus-within .replay-btn { opacity: 0.9; }
.replay-btn:hover { background: rgba(239,68,68,0.14); color: var(--text); border-color: var(--red); }
.replay-btn:active { transform: scale(0.92); }
@media (hover: none) { .replay-btn { opacity: 0.75; } }

/* ---------- busy notice retry ---------- */
.retry-btn {
  margin-top: 12px; padding: 10px 20px; border-radius: 12px;
  background: linear-gradient(135deg, var(--red), var(--red-dark));
  color: #fff; border: none; font-size: 14px; font-weight: 700;
  cursor: pointer; font-family: inherit; min-height: 44px;
  box-shadow: 0 4px 14px rgba(239,68,68,0.35);
  transition: filter 0.15s, transform 0.1s;
}
.retry-btn:hover { filter: brightness(1.12); }
.retry-btn:active { transform: scale(0.96); }

/* ---------- export dropdown ---------- */
.export-menu {
  position: absolute; top: 62px; right: 12px; z-index: 40;
  background: linear-gradient(160deg, #1e293b, #162032);
  border: 1px solid rgba(148,163,184,0.22);
  border-radius: 14px; padding: 6px; min-width: 210px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.55);
  animation: rise 0.18s ease-out;
}
.export-menu.hidden { display: none; }
.export-menu button {
  display: block; width: 100%; text-align: left;
  background: transparent; border: none; color: var(--text);
  padding: 12px 14px; border-radius: 10px; font-size: 14px; font-weight: 600;
  cursor: pointer; font-family: inherit; min-height: 44px;
}
.export-menu button:hover { background: rgba(239,68,68,0.12); }

/* ---------- premium color polish ---------- */
html, body {
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(239,68,68,0.07), transparent 60%),
    radial-gradient(900px 500px at 90% 110%, rgba(59,130,246,0.05), transparent 60%),
    var(--bg);
}
.topbar {
  background: linear-gradient(180deg, rgba(30,41,59,0.96), rgba(15,23,42,0.92));
  border-bottom: 1px solid transparent;
  border-image: linear-gradient(90deg, transparent, rgba(239,68,68,0.35), transparent) 1;
}
.brand h1 {
  background: linear-gradient(120deg, #fff 30%, #fca5a5 70%, #f87171);
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.orb {
  background: radial-gradient(circle at 35% 30%, #fca5a5, #ef4444 45%, #991b1b 90%);
  box-shadow: 0 2px 16px rgba(239,68,68,0.55), inset 0 1px 2px rgba(255,255,255,0.35);
}
.msg.ai .bubble {
  background: linear-gradient(165deg, #223148, #1a2438);
  border: 1px solid rgba(148,163,184,0.18);
  box-shadow: 0 4px 18px rgba(0,0,0,0.28);
}
.msg.user .bubble {
  background: linear-gradient(135deg, #dc2626, #991b1b);
  box-shadow: 0 4px 16px rgba(220,38,38,0.3);
}
.send-btn {
  background: linear-gradient(135deg, #f87171, var(--red-dark));
  box-shadow: 0 4px 14px rgba(239,68,68,0.4);
}
.send-btn:hover { background: linear-gradient(135deg, #ef4444, #b91c1c); filter: none; }
#composer .mic { border: 1px solid rgba(148,163,184,0.16); }
#input {
  background: linear-gradient(180deg, #1b2740, #162032);
  box-shadow: inset 0 2px 8px rgba(0,0,0,0.3);
}
#input:focus { border-color: var(--red); box-shadow: inset 0 2px 8px rgba(0,0,0,0.3), 0 0 0 3px rgba(239,68,68,0.18); }
.icon-btn:hover { background: rgba(239,68,68,0.1); }
.primary-btn {
  background: linear-gradient(135deg, #f87171, var(--red-dark));
  box-shadow: 0 4px 14px rgba(239,68,68,0.35);
}
.vibe-pill[aria-checked="true"] {
  background: linear-gradient(135deg, rgba(239,68,68,0.25), rgba(239,68,68,0.1));
  box-shadow: 0 0 0 1px var(--red), 0 2px 10px rgba(239,68,68,0.25);
}
.switch[aria-checked="true"] {
  background: linear-gradient(135deg, #f87171, var(--red-dark));
  box-shadow: 0 2px 10px rgba(239,68,68,0.4);
}
.empty-state .orb { animation: orbGlow 3s ease-in-out infinite; }
@keyframes orbGlow {
  0%, 100% { box-shadow: 0 2px 16px rgba(239,68,68,0.45); }
  50% { box-shadow: 0 2px 28px rgba(239,68,68,0.75); }
}

/* ---------- model/tier switcher ---------- */
.model-select {
  appearance: none; -webkit-appearance: none;
  background: rgba(239,68,68,0.10) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path d='M1 1l4 4 4-4' stroke='%2394a3b8' stroke-width='1.6' fill='none' stroke-linecap='round'/></svg>") no-repeat right 10px center;
  border: 1px solid rgba(239,68,68,0.28);
  color: var(--text); font-family: inherit;
  font-size: 13px; font-weight: 700;
  border-radius: 12px; padding: 9px 28px 9px 12px;
  cursor: pointer; min-height: 40px; flex: 0 0 auto;
  transition: background 0.15s, border-color 0.15s;
}
.model-select:hover { background-color: rgba(239,68,68,0.18); }
.model-select:focus { outline: none; border-color: var(--red); }
.model-select option { background: #1e293b; color: #f1f5f9; }

/* ---------- upgrade nudge (busy bubble) ---------- */
.upgrade-nudge {
  margin-top: 12px; padding-top: 12px;
  border-top: 1px solid rgba(148,163,184,0.18);
  font-size: 13px; color: var(--muted); line-height: 1.5;
}
.upgrade-btn {
  margin-top: 8px; padding: 9px 18px; border-radius: 12px;
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #fff; border: none; font-size: 13px; font-weight: 700;
  cursor: pointer; font-family: inherit; min-height: 40px;
  box-shadow: 0 4px 14px rgba(245,158,11,0.30);
  transition: filter 0.15s, transform 0.1s;
}
.upgrade-btn:hover { filter: brightness(1.1); }
.upgrade-btn:active { transform: scale(0.96); }

/* ---------- tiers modal ---------- */
.tier-list { display: flex; flex-direction: column; gap: 12px; }
.tier-card {
  background: rgba(30,41,59,0.7);
  border: 1px solid rgba(148,163,184,0.20);
  border-radius: 16px; padding: 16px 18px;
}
.tier-card h3 { margin: 0 0 4px; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.tier-price { margin: 0 0 10px; font-size: 14px; font-weight: 800; color: var(--red); }
.tier-card ul { margin: 0; padding-left: 18px; font-size: 13px; color: var(--muted); line-height: 1.9; }
.tier-card.coming { opacity: 0.75; border-style: dashed; }
.soon-badge {
  font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.6px;
  background: rgba(245,158,11,0.16); color: #fbbf24;
  border: 1px solid rgba(245,158,11,0.35);
  padding: 3px 8px; border-radius: 20px;
}
