:root {
  --bg: #101118;
  --panel: rgba(16, 17, 24, 0.94);
  --line: rgba(255, 255, 255, 0.08);
  --text: #e9eaf0;
  --muted: #9a9db0;
  --accent: #7c8cff;
  --listen: #3ddc97;
  --speak: #ffb547;
  --think: #7c8cff;
  --error: #ff5d6c;
  --level: 0;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg);
  font: 15px/1.45 system-ui, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
}
button { font: inherit; }

/* ---------- Orbe ---------- */
#orb {
  position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom));
  width: 112px; height: 112px; margin-left: -56px; border-radius: 50%;
  cursor: pointer; touch-action: none; user-select: none;
}
#orb .core {
  position: absolute; inset: 12px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #a9b4ff, #5361e8 55%, #2a2f7a);
  box-shadow: 0 6px 26px rgba(40, 50, 160, 0.55), inset 0 -5px 12px rgba(0,0,0,.35);
  transition: background .25s, transform .12s;
}
#orb:active .core { transform: scale(.96); }
#orb .ring {
  position: absolute; inset: 3px; border-radius: 50%;
  border: 3px solid transparent;
  transition: border-color .2s;
}

.state-listening #orb .core { background: radial-gradient(circle at 35% 30%, #9ff5cf, #22b97a 55%, #0d5c3c); }
.state-listening #orb .ring {
  border-color: var(--listen);
  transform: scale(calc(1 + var(--level) * 0.35));
  box-shadow: 0 0 calc(6px + var(--level) * 30px) var(--listen);
}
.state-transcribing #orb .ring,
.state-thinking #orb .ring {
  border-color: var(--think) transparent var(--think) transparent;
  animation: spin 0.9s linear infinite;
}
.state-speaking #orb .core { background: radial-gradient(circle at 35% 30%, #ffe2a8, #f29a1f 55%, #7a4300); }
.state-speaking #orb .ring { border-color: var(--speak); animation: pulse 0.7s ease-in-out infinite alternate; }
.state-error #orb .core { background: radial-gradient(circle at 35% 30%, #ffb3ba, #e23a4b 55%, #6b0f19); }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { from { transform: scale(1); opacity: .9; } to { transform: scale(1.12); opacity: .4; } }

/* ---------- Panel (celular: pantalla completa) ---------- */
#panel {
  display: none;
  position: fixed; left: 0; right: 0; top: 0;
  bottom: calc(146px + env(safe-area-inset-bottom));
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
body.expanded #panel { display: flex; }

header {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px; border-bottom: 1px solid var(--line);
}
header button {
  background: transparent; border: 0; color: var(--muted);
  font-size: 18px; width: 40px; height: 40px; border-radius: 10px; cursor: pointer;
}
header button:hover { background: rgba(255,255,255,.08); color: var(--text); }
#btn-handsfree { opacity: .45; }
#btn-handsfree.on { opacity: 1; background: rgba(61, 220, 151, .14); }
#btn-close { display: none; }
#status { flex: 1; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; margin-left: 4px; }
.state-listening #dot { background: var(--listen); }
.state-thinking #dot, .state-transcribing #dot { background: var(--think); }
.state-speaking #dot { background: var(--speak); }
.state-error #dot { background: var(--error); }

#log {
  flex: 1; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 12px;
  overscroll-behavior: contain;
}
#log::-webkit-scrollbar { width: 6px; }
#log::-webkit-scrollbar-thumb { background: rgba(255,255,255,.12); border-radius: 3px; }

.msg-user {
  align-self: flex-end; max-width: 85%;
  background: rgba(124, 140, 255, 0.18); padding: 8px 12px; border-radius: 14px 14px 3px 14px;
  overflow-wrap: anywhere;
}
.msg-bot { display: flex; flex-direction: column; gap: 6px; overflow-wrap: anywhere; }
.msg-bot .spoken { font-weight: 600; }
.msg-bot .screen { color: #cfd1dc; }
.msg-bot .stream { color: var(--muted); white-space: pre-wrap; }
.msg-bot pre {
  background: rgba(0,0,0,.35); padding: 8px; border-radius: 8px; overflow-x: auto;
  font: 13px/1.4 ui-monospace, "Cascadia Code", Consolas, monospace; white-space: pre-wrap;
}
.msg-bot code { font: 13px ui-monospace, "Cascadia Code", Consolas, monospace; background: rgba(0,0,0,.3); padding: 1px 4px; border-radius: 4px; }
.msg-bot ul { margin: 0; padding-left: 20px; }
.msg-bot a { color: var(--accent); }
.msg-bot p { margin: 0 0 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chips:empty { display: none; }
.chip {
  font-size: 12px; color: var(--muted); border: 1px solid var(--line);
  padding: 1px 8px; border-radius: 999px;
}
.chip.learned { color: var(--listen); border-color: rgba(61, 220, 151, .4); }
.chip.model { color: var(--accent); border-color: rgba(124, 140, 255, .45); }
.meta { font-size: 12px; color: var(--muted); }
.msg-error { color: var(--error); font-size: 13px; }

#ask { padding: 10px 12px; border-top: 1px solid var(--line); }
#ask input {
  width: 100%; background: rgba(255,255,255,.06); border: 1px solid var(--line);
  color: var(--text); border-radius: 12px; padding: 10px 12px; font: inherit; outline: none;
}
#ask input:focus { border-color: var(--accent); }

#tap-hint {
  position: fixed; left: 50%; top: 45%; transform: translate(-50%, -50%);
  background: var(--accent); color: #fff; border: 0; border-radius: 999px;
  padding: 10px 18px; font-weight: 600; cursor: pointer; z-index: 5;
}

/* ---------- Acceso ---------- */
#login {
  position: fixed; inset: 0; z-index: 10; background: var(--bg);
  display: grid; place-items: center; padding: 16px;
}
#login[hidden] { display: none; }
#login form { width: min(340px, 100%); display: flex; flex-direction: column; gap: 12px; text-align: center; }
#login h1 { margin: 0; font-size: 22px; }
#login p { margin: 0; color: var(--muted); }
#login input {
  background: rgba(255,255,255,.06); border: 1px solid var(--line); color: var(--text);
  border-radius: 12px; padding: 12px; font: inherit; outline: none;
}
#login button {
  background: var(--accent); color: #fff; border: 0; border-radius: 12px; padding: 12px; font-weight: 600; cursor: pointer;
}
.login-orb {
  width: 72px; height: 72px; margin: 0 auto 4px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #a9b4ff, #5361e8 55%, #2a2f7a);
}

/* ---------- Modo orbe (escritorio, ventana flotante transparente) ---------- */
.orb-mode, .orb-mode body {
  background: transparent;
  font-size: 13px;
  user-select: none;
}
.orb-mode #orb {
  position: absolute; left: auto; right: 10px; bottom: 10px; margin: 0;
  width: 64px; height: 64px;
}
.orb-mode #orb .core { inset: 8px; }
.orb-mode #orb .ring { inset: 2px; border-width: 2px; }
.orb-mode #panel {
  position: absolute; left: 0; top: 0; right: 0; bottom: 92px; padding: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
  backdrop-filter: blur(14px);
}
.orb-mode header { -webkit-app-region: drag; padding: 8px 10px; }
.orb-mode header button { -webkit-app-region: no-drag; width: 26px; height: 26px; font-size: 14px; border-radius: 8px; }
.orb-mode #btn-close { display: inline-block; }
.orb-mode #log { padding: 10px 12px; gap: 10px; user-select: text; }
.orb-mode .msg-bot pre, .orb-mode .msg-bot code { font-size: 12px; }
.orb-mode .chip { font-size: 11px; }
.orb-mode #ask { padding: 8px 10px; }
.orb-mode #ask input { padding: 8px 10px; border-radius: 10px; }
.orb-mode #tap-hint { display: none; }
