:root {
  --bg: #070a14;
  --bg2: #0d1224;
  --panel: #121830;
  --panel-2: #1a2142;
  --line: rgba(255, 255, 255, 0.08);
  --text: #e8ecf8;
  --muted: #8a93b2;
  --accent: #7c6cff;
  --accent-2: #22d3ee;
  --danger: #ff6b8a;
  --ok: #34d399;
  --warn: #fbbf24;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(120% 80% at 50% 0%, #141b3a 0%, transparent 60%),
    radial-gradient(100% 60% at 50% 100%, #0f1730 0%, transparent 70%),
    var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Ubuntu, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-top) + 10px) 16px calc(var(--safe-bottom) + 14px);
  max-width: 720px;
  margin: 0 auto;
}

/* Header */
.top { display: flex; align-items: center; justify-content: space-between; min-height: 44px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: 0.2px; font-size: 1.05rem; }
.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); box-shadow: 0 0 0 3px rgba(138, 147, 178, 0.15); transition: background .3s, box-shadow .3s; }
.conn[data-state="online"] { background: var(--ok); box-shadow: 0 0 0 3px rgba(52, 211, 153, 0.18); }
.conn[data-state="loading"] { background: var(--warn); box-shadow: 0 0 0 3px rgba(251, 191, 36, 0.18); animation: blink 1.2s infinite; }
.conn[data-state="offline"] { background: var(--danger); box-shadow: 0 0 0 3px rgba(255, 107, 138, 0.18); }
@keyframes blink { 50% { opacity: .35; } }
.top-actions { display: flex; gap: 4px; }
.icon-btn {
  width: 44px; height: 44px; display: grid; place-items: center;
  border: 0; border-radius: 12px; background: transparent; color: var(--muted);
}
.icon-btn:hover, .icon-btn:focus-visible { background: var(--line); color: var(--text); outline: none; }

.banner {
  margin: 8px 0 0; padding: 10px 14px; border-radius: 12px;
  background: rgba(251, 191, 36, 0.1); border: 1px solid rgba(251, 191, 36, 0.3);
  color: #fde68a; font-size: .9rem;
}
.banner a { color: #fff; }

/* Stage */
.stage {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; text-align: center;
}
.orb-btn {
  width: min(86vw, 46dvh, 420px); aspect-ratio: 1; max-width: 100%;
  border: 0; padding: 0; background: transparent; border-radius: 50%;
  -webkit-user-select: none; user-select: none;
}
.orb-btn:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 6px; }
.orb-btn canvas { width: 100%; height: 100%; display: block; }
.state { margin: 0; font-size: 1.05rem; color: var(--text); font-weight: 550; min-height: 1.5em; }
.captions { width: 100%; max-width: 560px; min-height: 5.6em; display: flex; flex-direction: column; gap: 6px; }
.captions p { margin: 0; }
.cap-user { color: var(--muted); font-size: .95rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.cap-user:not(:empty)::before { content: "Tú: "; color: var(--accent-2); }
.cap-bot {
  font-size: 1.08rem; color: var(--text);
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}
.metrics { margin: 4px 0 0; font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--muted); }

/* Composer */
.composer { display: flex; gap: 8px; margin-bottom: 10px; }
.composer input {
  flex: 1; min-width: 0; height: 48px; padding: 0 16px; border-radius: 24px;
  background: var(--panel); border: 1px solid var(--line); outline: none;
}
.composer input:focus { border-color: var(--accent); }
.send { width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; }

/* Controls */
.controls { display: flex; align-items: center; justify-content: center; gap: 28px; padding-top: 6px; }
.ctl {
  width: 54px; height: 54px; border-radius: 50%; display: grid; place-items: center;
  background: var(--panel); border: 1px solid var(--line); color: var(--text);
  transition: transform .12s, background .2s, opacity .2s;
}
.ctl:active { transform: scale(.94); }
.ctl:disabled { opacity: .35; }
.ctl.primary { width: 72px; height: 72px; background: linear-gradient(135deg, #6d5dfc, #22d3ee); border: 0; box-shadow: 0 10px 30px rgba(109, 93, 252, .35); }
.ctl.primary svg { width: 30px; height: 30px; }
.ctl.primary .mic-on { display: none; }
.ctl.primary[aria-pressed="true"] { background: linear-gradient(135deg, #ff6b8a, #7c6cff); }
.ctl.primary[aria-pressed="true"] .mic-on { display: block; }
.ctl.primary[aria-pressed="true"] .mic-off { display: none; }

/* Sheets */
.scrim { position: fixed; inset: 0; background: rgba(3, 5, 12, 0.6); backdrop-filter: blur(2px); z-index: 10; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 11;
  max-height: 82dvh; max-width: 720px; margin: 0 auto;
  display: flex; flex-direction: column;
  background: var(--bg2); border: 1px solid var(--line); border-bottom: 0;
  border-radius: 22px 22px 0 0;
  padding: 6px 16px calc(var(--safe-bottom) + 16px);
  animation: up .22s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet h2 { font-size: 1.1rem; margin: 10px 0; }
.messages { overflow-y: auto; display: flex; flex-direction: column; gap: 10px; padding: 4px 0 10px; }
.msg { max-width: 86%; padding: 10px 14px; border-radius: 16px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--panel-2); border-bottom-right-radius: 6px; }
.msg.assistant { align-self: flex-start; background: rgba(124, 108, 255, 0.14); border: 1px solid rgba(124, 108, 255, 0.25); border-bottom-left-radius: 6px; }
.msg.interrupted::after { content: " ⏹"; opacity: .5; }
.empty { color: var(--muted); text-align: center; }

.form { overflow-y: auto; display: flex; flex-direction: column; gap: 18px; padding-bottom: 6px; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field > span { display: flex; justify-content: space-between; font-weight: 550; }
.field output { color: var(--muted); font-weight: 400; }
.field small, .toggle small { color: var(--muted); font-size: .82rem; font-weight: 400; display: block; }
.field select {
  height: 46px; border-radius: 12px; padding: 0 12px;
  background: var(--panel); border: 1px solid var(--line);
}
input[type="range"] { width: 100%; accent-color: var(--accent); }
.toggle { display: flex; gap: 12px; align-items: flex-start; font-weight: 550; }
.toggle input { width: 22px; height: 22px; margin: 1px 0 0; accent-color: var(--accent); flex: none; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn { flex: 1; min-height: 46px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); padding: 0 14px; }
.btn.accent { background: var(--accent); border: 0; color: #fff; }
.info { color: var(--muted); font-size: .82rem; margin: 0; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 110px); transform: translateX(-50%);
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 12px; z-index: 20;
  background: #1f2747; border: 1px solid var(--line); box-shadow: 0 10px 30px rgba(0, 0, 0, .4);
  font-size: .92rem; text-align: center;
}

@media (min-width: 720px) {
  .orb-btn { width: min(54vh, 440px); }
}

/* First-run setup / download */
.setup {
  width: 100%; max-width: 460px; margin: 6px auto 0; padding: 16px;
  display: flex; flex-direction: column; gap: 14px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  text-align: left;
}
.setup p { margin: 0; color: var(--muted); font-size: .92rem; }
.setup strong { color: var(--text); }
.progress { display: flex; flex-direction: column; gap: 8px; }
.progress p { font-size: .85rem; text-align: center; }
.bar { height: 8px; border-radius: 4px; background: var(--panel-2); overflow: hidden; }
.bar > div { height: 100%; width: 0; background: linear-gradient(90deg, #6d5dfc, #22d3ee); transition: width .25s; }
.btn.danger { color: var(--danger); border-color: rgba(255, 107, 138, .3); background: transparent; flex: none; }
.btn:disabled { opacity: .5; }
.banner code, .setup code { font-size: .85em; background: rgba(255,255,255,.08); padding: 1px 5px; border-radius: 5px; }
.setup:not([hidden]) ~ .captions { display: none; }
.stage:has(.setup:not([hidden])) .orb-btn { width: min(58vw, 30dvh, 300px); }
