:root {
  --bg: #05070d;
  --surface: #0a1224;
  --surface-2: #0e1a33;
  --line: rgba(120, 180, 255, 0.14);
  --blue: #2f6bff;
  --cyan: #22d3ee;
  --text: #e8f4ff;
  --muted: #8fa3bf;
  --danger: #ff5d73;
  --warn: #f5b84a;
  --ok: #34d399;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tab-h: 64px;
  --level: 0;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: .02em; }
h2 { font-size: 20px; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .12em; color: var(--cyan); margin: 18px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.error-text { color: var(--danger); min-height: 1.4em; margin: 8px 0 0; }

/* Splash */
.splash {
  position: fixed; inset: 0; z-index: 50; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px; background: var(--bg);
  transition: opacity .5s ease;
}
.splash.out { opacity: 0; pointer-events: none; }
.splash-logo { width: 96px; height: 96px; filter: drop-shadow(0 0 18px rgba(34, 211, 238, .55)); animation: breathe 2.4s ease-in-out infinite; }
.splash-text { letter-spacing: .5em; font-weight: 600; color: var(--muted); padding-left: .5em; }

/* Layout */
#app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-top) + 10px) 16px 8px;
}
.brand { display: flex; align-items: center; gap: 10px; letter-spacing: .3em; font-weight: 600; font-size: 14px; }
.pill {
  display: flex; align-items: center; gap: 8px; background: var(--surface); color: var(--muted);
  border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; font-size: 12px; font-family: inherit;
}
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill[data-ok="true"] .dot { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.pill[data-ok="false"] .dot { background: var(--warn); box-shadow: 0 0 8px var(--warn); }
.pill[data-ok="offline"] .dot { background: var(--danger); box-shadow: 0 0 8px var(--danger); }

.view { flex: 1; overflow-y: auto; padding: 8px 16px calc(var(--tab-h) + var(--safe-bottom) + 16px); -webkit-overflow-scrolling: touch; }
.view-head { display: flex; align-items: center; gap: 12px; justify-content: space-between; margin: 6px 0 12px; }
#view-home { display: flex; flex-direction: column; align-items: center; justify-content: space-between; }
#view-thread { display: flex; flex-direction: column; padding-bottom: calc(var(--tab-h) + var(--safe-bottom) + 8px); }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; height: calc(var(--tab-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom); display: flex; background: rgba(5, 7, 13, .86);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); border-top: 1px solid var(--line); z-index: 10;
}
.tab {
  flex: 1; background: none; border: 0; color: var(--muted); font: inherit; font-size: 11px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; position: relative;
}
.tab .ti { font-size: 20px; line-height: 1; }
.tab.active { color: var(--cyan); }
.badge { position: absolute; top: 8px; left: 56%; min-width: 16px; height: 16px; border-radius: 8px; background: var(--warn); color: #000; font-style: normal; font-size: 10px; display: grid; place-items: center; padding: 0 4px; }

/* Buttons & inputs */
.btn {
  font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 16px; min-height: 44px;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: linear-gradient(180deg, #2f6bff, #1f4fd1); border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.small { padding: 8px 14px; min-height: 38px; font-size: 14px; }
.btn.danger { color: var(--danger); border-color: rgba(255, 93, 115, .4); }
.btn.icon { padding: 0; width: 46px; min-height: 46px; font-size: 18px; }
.btn[disabled] { opacity: .5; }
input, select {
  width: 100%; font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 14px; min-height: 46px; outline: none; font-size: 16px; /* 16px evita lo zoom su iOS */
}
input:focus, select:focus { border-color: var(--cyan); box-shadow: 0 0 0 3px rgba(34, 211, 238, .15); }
input[type="checkbox"] { width: 22px; height: 22px; min-height: 0; accent-color: var(--cyan); flex: none; }
input[type="range"] { padding: 0; min-height: 30px; }
label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0; }
label input, label select { margin-top: 6px; color: var(--text); }
.row-check { display: flex; align-items: center; gap: 10px; color: var(--text); font-size: 15px; }
.row-check input { margin: 0; }
.search { margin-bottom: 10px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 8px 16px 16px; margin: 12px 0; }
.card > .btn { margin: 6px 8px 0 0; }
.card h3:first-child { margin-top: 10px; }

.composer { display: flex; gap: 8px; align-items: center; margin: 8px 0; width: 100%; }
.composer select { width: 112px; flex: none; }
.composer.sticky { position: sticky; bottom: 0; background: linear-gradient(transparent, var(--bg) 30%); padding-top: 14px; }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.list.tight { gap: 4px; margin: 8px 0; }
.item { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; display: flex; gap: 10px; align-items: center; }
.item .grow { flex: 1; min-width: 0; text-align: left; background: none; border: 0; color: inherit; font: inherit; padding: 0; }
.item .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .meta { font-size: 12px; color: var(--muted); }
.item .tag { font-size: 11px; color: var(--cyan); letter-spacing: .08em; text-transform: uppercase; }
.item .x { background: none; border: 0; color: var(--muted); font-size: 18px; padding: 6px 10px; }
.item .ok { background: none; border: 0; color: var(--ok); font-size: 18px; padding: 6px 10px; }
.prov { display: flex; justify-content: space-between; font-size: 14px; color: var(--text); }
.prov span:last-child { color: var(--muted); }

/* Thread */
.thread { flex: 1; display: flex; flex-direction: column; gap: 10px; padding-bottom: 8px; }
.bubble { max-width: 86%; padding: 10px 14px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.bubble.user { align-self: flex-end; background: linear-gradient(180deg, #1c3f9e, #173480); border-bottom-right-radius: 6px; }
.bubble.assistant { align-self: flex-start; background: var(--surface-2); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.bubble.error { align-self: center; background: rgba(255, 93, 115, .1); border: 1px solid rgba(255, 93, 115, .35); color: #ffb3bd; font-size: 14px; }
.bubble.typing { color: var(--muted); }

/* Auth */
.auth { display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 6px; padding-bottom: 32px; }
.auth form { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }
.auth-logo { width: 88px; height: 88px; filter: drop-shadow(0 0 16px rgba(34, 211, 238, .5)); }
.auth h1 { letter-spacing: .4em; padding-left: .4em; }
.auth .card { text-align: left; max-width: 340px; width: 100%; }

/* ---------- ATLAS CORE ---------- */
.core-wrap { display: flex; flex-direction: column; align-items: center; gap: 18px; margin-top: 4vh; }
.core { position: relative; width: min(66vw, 280px); aspect-ratio: 1; display: grid; place-items: center; --c: var(--cyan); --speed: 4s; }
.core > * { position: absolute; border-radius: 50%; }
.halo { inset: -18%; background: radial-gradient(circle, color-mix(in srgb, var(--c) 40%, transparent) 0%, transparent 62%); opacity: .55; animation: breathe var(--speed) ease-in-out infinite; }
.orb {
  inset: 12%; display: grid; place-items: center;
  background: radial-gradient(circle at 35% 30%, rgba(80, 140, 255, .55), rgba(10, 24, 60, .95) 62%);
  border: 1px solid color-mix(in srgb, var(--c) 55%, transparent);
  box-shadow: 0 0 40px color-mix(in srgb, var(--c) 40%, transparent), inset 0 0 40px rgba(20, 60, 160, .5);
  animation: breathe var(--speed) ease-in-out infinite;
  transform: scale(calc(1 + var(--level) * .08));
}
.orb-logo { width: 46%; height: 46%; opacity: .95; }
.orbit { inset: 2%; border: 1px solid color-mix(in srgb, var(--c) 28%, transparent); animation: spin 14s linear infinite; }
.orbit-dot { position: absolute; top: -4px; left: calc(50% - 4px); width: 8px; height: 8px; border-radius: 50%; background: var(--c); box-shadow: 0 0 12px var(--c); }
.wave { inset: 12%; border: 1.5px solid var(--c); opacity: 0; }

.core[data-state="idle"] { --speed: 4.5s; }
.core[data-state="listening"] { --c: #5ec8ff; --speed: 1.6s; }
.core[data-state="listening"] .wave { animation: wave 2.4s ease-out infinite; }
.core[data-state="listening"] .w2 { animation-delay: .8s; }
.core[data-state="listening"] .w3 { animation-delay: 1.6s; }
.core[data-state="thinking"] { --speed: 1.1s; }
.core[data-state="thinking"] .orbit { animation-duration: 1.8s; border-color: color-mix(in srgb, var(--c) 60%, transparent); }
.core[data-state="speaking"] { --c: #4aa3ff; --speed: 1.4s; }
.core[data-state="speaking"] .wave { animation: wave 1.8s ease-out infinite; }
.core[data-state="speaking"] .w2 { animation-delay: .6s; }
.core[data-state="speaking"] .w3 { animation-delay: 1.2s; }
.core[data-state="acting"] { --c: #7c9bff; --speed: 1.2s; }
.core[data-state="acting"] .orbit { animation-duration: 3s; border-style: dashed; }
.core[data-state="error"] { --c: #ff8a5c; --speed: 6s; }
.core[data-state="error"] .orbit { animation-play-state: paused; }
.core[data-state="error"] .halo { opacity: .35; }

.core-label { color: var(--muted); letter-spacing: .14em; text-transform: uppercase; font-size: 12px; min-height: 1.4em; text-align: center; max-width: 80vw; }
.core[data-state="error"] + .core-label { color: #ffb08f; }

.last-message {
  width: 100%; max-width: 520px; text-align: left; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 10px 14px; margin: 14px 0;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}
.lm-who { display: block; font-size: 10px; letter-spacing: .2em; color: var(--cyan); margin-bottom: 2px; }
#last-message-text { white-space: pre-wrap; }

.dock { width: 100%; max-width: 520px; display: flex; flex-direction: column; align-items: center; }
.controls { display: flex; align-items: center; justify-content: space-between; width: 100%; gap: 10px; }
.controls .btn.ghost { flex: 1; }
.btn.mic {
  width: 76px; height: 76px; border-radius: 50%; padding: 0; display: grid; place-items: center; flex: none;
  background: radial-gradient(circle at 35% 30%, #3b7bff, #1740b8); border: 0;
  box-shadow: 0 0 26px rgba(47, 107, 255, .55);
}
.btn.mic.active { background: radial-gradient(circle at 35% 30%, #ff6d7f, #c4243a); box-shadow: 0 0 30px rgba(255, 93, 115, .6); }
.voice-note { color: var(--muted); font-size: 12px; text-align: center; min-height: 1.2em; margin: 8px 0 0; }

/* Toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tab-h) + var(--safe-bottom) + 14px);
  max-width: 88vw; background: var(--surface-2); border: 1px solid var(--line); color: var(--text);
  padding: 10px 16px; border-radius: 14px; font-size: 14px; z-index: 40; box-shadow: 0 8px 30px rgba(0, 0, 0, .5);
}

@keyframes breathe { 0%, 100% { opacity: .75; transform: scale(calc(1 + var(--level) * .08)); } 50% { opacity: 1; transform: scale(calc(1.035 + var(--level) * .08)); } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes wave { 0% { transform: scale(1); opacity: .55; } 100% { transform: scale(1.55); opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  .halo, .orb, .orbit, .wave, .splash-logo { animation: none !important; }
}
@media (min-width: 720px) {
  .view { max-width: 640px; margin: 0 auto; width: 100%; }
  .topbar { max-width: 640px; margin: 0 auto; width: 100%; }
}
