:root {
  --bg: #0a0c10;
  --panel: #12151c;
  --key: #232834;
  --key-hi: #2e3444;
  --key-edge: #0b0d12;
  --txt: #eef1f7;
  --muted: #8a93a6;
  --blue: #3d7bff;
  --red: #ff4d57;
  --green: #35d07f;
  --gap: 6px;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--txt); overflow: hidden; overscroll-behavior: none;
  font: 15px -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}

/* ---------- estructura ---------- */
#app {
  position: fixed; left: 0; top: 0; width: 100vw; height: 100vh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) max(6px, env(safe-area-inset-bottom)) env(safe-area-inset-left);
  display: flex; flex-direction: column; gap: var(--gap);
}
#top {
  flex: 0 0 auto; display: flex; align-items: center; gap: 8px; height: 30px; padding: 0 10px;
  font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden;
}
#top .grow { flex: 1; }
#dot { width: 8px; height: 8px; border-radius: 50%; background: #666; flex: 0 0 auto; }
#dot.ok { background: var(--green); box-shadow: 0 0 8px var(--green); }
#dot.wait { background: #f5a524; }
#mac { color: var(--txt); font-weight: 600; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; min-width: 0; }
#front { overflow: hidden; text-overflow: ellipsis; min-width: 0; }
#front:not(:empty)::before { content: "→ "; }
#lat { font-variant-numeric: tabular-nums; }
#lat.good { color: #4ade80; } #lat.fair { color: #fbbf24; } #lat.bad { color: #f87171; }
#tv-btn { width: 40px; height: 30px; flex: 0 0 auto; min-height: 0; border-radius: 8px; font-size: 16px; padding: 0; background: transparent; box-shadow: none; }
#tv-btn.on { background: #2563eb; }
#tv-btn.wait { animation: tvpulse 1s ease-in-out infinite; }
@keyframes tvpulse { 50% { opacity: .35; } }
.tv-list { display: grid; gap: 6px; }
.tv-list .k { height: 48px; font-size: 14px; justify-content: flex-start; padding: 0 14px; text-align: left; }
.tv-list .k small { color: var(--muted); margin-left: 6px; font-weight: 400; }
.tv-list .k.on { background: #2563eb; color: #fff; }
.tv-list .k.on small { color: #dbeafe; }
.tv-empty { color: var(--muted); font-size: 13px; padding: 6px 2px; }
.tv-hint { color: var(--muted); font-size: 12.5px; line-height: 1.4; padding: 2px; }
#ai-btn { width: 44px; height: 30px; flex: 0 0 auto; min-height: 0; border-radius: 8px; font-size: 16px; padding: 0; background: transparent; box-shadow: none; }
#ai-btn.busy { animation: tvpulse 1s ease-in-out infinite; }
#ai-btn.asking { background: #b45309; animation: tvpulse 1.4s ease-in-out infinite; }
#menu-btn { width: 40px; height: 30px; flex: 0 0 auto; min-height: 0; border-radius: 8px; font-size: 18px; padding: 0; background: transparent; box-shadow: none; }

#stage {
  position: relative; flex: 0 0 auto; overflow: hidden; touch-action: none; background: #000;
  border-radius: 10px; margin: 0 6px;
}
#screen { position: absolute; left: 0; top: 0; transform-origin: 0 0; background: #111; }
#ring {
  position: absolute; width: 34px; height: 34px; margin: -17px 0 0 -17px; border-radius: 50%;
  border: 2px solid rgba(61,123,255,.95); box-shadow: 0 0 12px rgba(61,123,255,.6);
  pointer-events: none; opacity: 0; transition: opacity .12s;
}
#ring.on { opacity: 1; }
#ring.drag { background: rgba(61,123,255,.28); }
#toast {
  position: absolute; left: 50%; top: 8px; transform: translateX(-50%); max-width: 92%;
  background: rgba(12,14,20,.92); border: 1px solid #2a3040; color: var(--txt); border-radius: 12px;
  padding: 8px 12px; font-size: 14px; line-height: 1.35; pointer-events: none; z-index: 3;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
#toast.err { border-color: #7a2a30; color: #ffb3b8; }
#toast[hidden] { display: none; }   /* el display de arriba tapaba el atributo hidden: sin esto, el aviso no se iba nunca */

#controls { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; gap: var(--gap); padding: 0 6px; }
.row { display: flex; gap: var(--gap); flex: 0 0 auto; }
.row .k { flex: 1 1 0; }

/* ---------- teclas ---------- */
button { font: inherit; color: inherit; }
.k {
  position: relative; min-width: 0; height: 46px; border: 0; border-radius: 10px; padding: 0 4px;
  background: linear-gradient(#2a3040, var(--key)); color: var(--txt); font-size: 18px;
  box-shadow: 0 2px 0 var(--key-edge), inset 0 1px 0 rgba(255,255,255,.06);
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.05;
  touch-action: none; transition: transform .05s, filter .05s;
}
.k.down { transform: translateY(2px); box-shadow: 0 0 0 var(--key-edge); filter: brightness(1.35); }
.k .ic { font-size: 19px; }
.k .lb { font-size: 10.5px; color: var(--muted); margin-top: 3px; letter-spacing: .2px; }
.k.on { background: var(--blue); }
.k.lock { background: var(--blue); box-shadow: 0 0 0 2px #fff inset; }
.k.accent { background: linear-gradient(#4a86ff, #2f66e0); }
.k.accent .lb { color: #dbe6ff; }
.k.danger.armed { background: var(--red); animation: pulse .6s ease-in-out infinite alternate; }
.k.danger.armed .lb { color: #fff; }

.r-main .k { height: 58px; }
.r-main .mic { flex: 2.2 1 0; flex-direction: row; gap: 8px; background: linear-gradient(#3a4256, #2a3040); }
.r-main .mic .ic { font-size: 22px; }
.r-main .mic .lb { font-size: 16px; color: var(--txt); margin: 0; font-weight: 600; }
.mic.rec { background: linear-gradient(#ff5a63, #d93440) !important; animation: pulse .9s ease-in-out infinite alternate; }
.mic.busy { background: linear-gradient(#3d4a6b, #2c3650) !important; }
.mic.busy .ic { animation: spin 1s linear infinite; display: inline-block; }
@keyframes pulse { from { filter: brightness(1); } to { filter: brightness(1.25); } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- trackpad ---------- */
#pad { flex: 1 1 auto; min-height: 90px; display: flex; flex-direction: column; gap: 0; }
#pad-surface {
  flex: 1 1 auto; border-radius: 16px 16px 0 0; touch-action: none; position: relative;
  background: radial-gradient(120% 90% at 50% 0%, #1d222d 0%, #161a22 60%, #13161d 100%);
  border: 1px solid #262c39; border-bottom: 0;
  display: flex; align-items: center; justify-content: center;
}
#pad-surface span { color: #3a4152; font-size: 13px; letter-spacing: 2px; text-transform: uppercase; pointer-events: none; }
#pad-surface.active { background: radial-gradient(120% 90% at 50% 0%, #212838 0%, #181d27 60%, #151922 100%); }
#pad-buttons, #pad-quick { display: flex; gap: 1px; flex: 0 0 auto; }
#pad-buttons .k, #pad-quick .k { flex: 1; height: 44px; font-size: 13px; color: var(--muted); border-radius: 0; box-shadow: none;
  background: #1a1e27; border: 1px solid #262c39; }
#pad-buttons .k:first-child { flex: 2.2; }
#pad-quick { margin-top: 1px; margin-bottom: 10px; }   /* aire sobre la barra de inicio: ahí iOS se come los toques */
#pad-quick .k.accent { color: #dbe6ff; }
#pad-quick .k:first-child { border-radius: 0 0 0 16px; }
#pad-quick .k:last-child { border-radius: 0 0 16px 0; }
#pad-buttons .k.down, #pad-quick .k.down { background: #242a37; }

/* ---------- teclado abierto (vertical): se esconde lo que tapa el teclado ---------- */
#app.kb-open .r-nav, #app.kb-open #pad { display: none; }
#app.kb-open .r-main .k { height: 50px; }

/* ---------- horizontal: pantalla grande + columna de teclas ---------- */
#app.landscape {
  display: grid; grid-template-columns: 1fr minmax(170px, 23vw); grid-template-rows: auto 1fr;
  grid-template-areas: "stage top" "stage controls"; gap: var(--gap);
  padding-bottom: max(4px, env(safe-area-inset-bottom));
}
#app.landscape #stage { grid-area: stage; margin: 0; border-radius: 0; height: auto !important; }
#app.landscape #top { grid-area: top; height: 32px; padding: 4px 2px 0; }
#app.landscape #controls {
  grid-area: controls; display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 1fr;
  gap: 5px; padding: 0 2px 0 0; align-content: stretch;
}
#app.landscape .row { display: contents; }
#app.landscape .k { height: auto; min-height: 34px; font-size: 16px; }
#app.landscape .r-main .mic { grid-column: span 3; }
#app.landscape .r-main .k .lb { font-size: 9.5px; }
#app.landscape .r-main .k:not(.mic) .lb { display: none; }
#app.landscape #pad, #app.landscape #menu-btn, #app.landscape #tv-btn { display: none; }
#app.landscape #ai-btn { height: 28px; }
#app.landscape [data-act="kb"] { grid-column: span 1; }

/* ---------- overlay de conexión ---------- */
#overlay {
  position: absolute; inset: 0; display: none; align-items: center; justify-content: center; z-index: 5;
  background: rgba(0,0,0,.75); padding: 16px;
}
#overlay.show { display: flex; }
.card { background: #151923; border: 1px solid #262c3a; border-radius: 14px; padding: 16px 18px; max-width: 340px; text-align: center; }
#ov-title { font-weight: 600; font-size: 16px; }
#ov-msg { color: var(--muted); margin-top: 6px; line-height: 1.4; font-size: 13.5px; }
#ov-btn { margin-top: 12px; height: 40px; padding: 0 18px; border: 0; border-radius: 10px; background: var(--blue); }
.spinner { width: 24px; height: 24px; margin: 0 auto 10px; border: 3px solid #333a4b; border-top-color: var(--blue); border-radius: 50%; animation: spin .8s linear infinite; }
.spinner[hidden] { display: none; }

/* ---------- menú ---------- */
#sheet { position: fixed; inset: 0; z-index: 20; }
#sheet-bg { position: absolute; inset: 0; background: rgba(0,0,0,.5); }
#sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 86vh; overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--panel); border-radius: 18px 18px 0 0; border-top: 1px solid #262c39;
  padding: 12px max(14px, env(safe-area-inset-right)) calc(14px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  touch-action: pan-y;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head .k { width: 40px; height: 36px; flex: 0 0 auto; font-size: 15px; }
#sheet h4 { margin: 14px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); font-weight: 600; }
#sheet .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); gap: 6px; }
#sheet .grid .k, #sheet .seg .k { height: 42px; font-size: 13.5px; touch-action: manipulation; }
#sheet .seg { display: flex; gap: 6px; margin-bottom: 6px; }
#sheet .seg .k { flex: 1; }
#sheet .foot { margin-top: 14px; }
#volume { width: 100%; margin: 12px 0 2px; accent-color: var(--blue); }
#hud { margin-top: 10px; font: 11px ui-monospace, Menlo, monospace; color: #9fe8bd; white-space: pre; }
#hud:empty { display: none; }

/* ---------- asistente ---------- */
#ai { position: fixed; inset: 0; z-index: 25; }
#ai-bg { position: absolute; inset: 0; background: rgba(0,0,0,.55); }
#ai-panel {
  position: absolute; left: 0; right: 0; bottom: 0; top: max(40px, env(safe-area-inset-top)); overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--panel); border-radius: 18px 18px 0 0; border-top: 1px solid #262c39; touch-action: pan-y;
  padding: 12px max(14px, env(safe-area-inset-right)) calc(18px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}
#ai-panel .sheet-head { gap: 8px; }
#ai-panel .sheet-head .grow { flex: 1; }
#ai-panel .sheet-head .k.small { width: auto; padding: 0 10px; font-size: 12.5px; color: var(--muted); }
#ai-panel h4 { margin: 16px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); font-weight: 600; }
#ai-talk { width: 100%; height: 76px; margin-top: 12px; flex-direction: row; gap: 10px; font-size: 17px; font-weight: 600;
  background: linear-gradient(#3a4256, #2a3040); touch-action: manipulation; }
#ai-talk .ic { font-size: 26px; }
#ai-talk .lb { font-size: 16px; color: var(--txt); margin: 0; }
#ai-talk.rec { background: linear-gradient(#ff5a63, #d93440); animation: pulse .9s ease-in-out infinite alternate; }
#ai-talk.busy { background: linear-gradient(#3d4a6b, #2c3650); }
#ai-form { display: flex; gap: 6px; margin-top: 8px; }
#ai-text { flex: 1; min-width: 0; height: 42px; border-radius: 10px; border: 1px solid #2c3342; background: #10131a; color: var(--txt);
  font-size: 16px; padding: 0 12px; }
#ai-form .k { width: 84px; height: 42px; flex: 0 0 auto; font-size: 14px; }
.ai-opts { display: flex; gap: 6px; margin-top: 8px; }
.ai-opts .k { flex: 1; height: 38px; font-size: 13px; touch-action: manipulation; }
.ai-empty { color: var(--muted); font-size: 13.5px; line-height: 1.45; }
.task { background: #10131a; border: 1px solid #242a37; border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; }
.task .t-heard { font-size: 14.5px; line-height: 1.35; }
.task .t-steps { margin-top: 6px; font-size: 12.5px; color: var(--muted); line-height: 1.5; }
.task .t-reply { margin-top: 7px; font-size: 14.5px; line-height: 1.4; color: #dbe6ff; }
.task .t-meta { margin-top: 6px; font-size: 11.5px; color: var(--muted); display: flex; gap: 8px; align-items: center; }
.task.st-thinking .t-meta b, .task.st-working .t-meta b { color: #8fb3ff; }
.task.st-done .t-meta b { color: #7ee2a8; }
#toast.tap { pointer-events: auto; border-color: #b45309; }
.task.st-waiting { border-color: #b45309; }
.task.st-waiting .t-meta b { color: #fbbf24; }
.task .t-answer { display: flex; gap: 6px; margin-top: 8px; }
.task .t-answer .k { flex: 1; height: 44px; font-size: 15px; }
.task.st-error .t-meta b { color: #ff9aa2; }
.task .dotspin { width: 10px; height: 10px; border: 2px solid #333a4b; border-top-color: var(--blue); border-radius: 50%; animation: spin .8s linear infinite; }
.ai-sys { font-size: 13px; line-height: 1.6; color: var(--txt); }
.ai-sys .row2 { display: flex; justify-content: space-between; gap: 10px; color: var(--muted); font-size: 12.5px; }
.ai-sys .bar { height: 5px; border-radius: 3px; background: #242a37; overflow: hidden; margin: 2px 0 6px; }
.ai-sys .bar i { display: block; height: 100%; background: var(--blue); }

/* Campo invisible pero enfocable: abre el teclado del iPhone (incluye dictado de iOS) */
#kb { position: fixed; left: 0; bottom: 0; width: 2px; height: 2px; opacity: .01; font-size: 16px; border: 0; padding: 0;
  resize: none; color: transparent; background: transparent; caret-color: transparent; }
#haptic { position: fixed; left: -100px; top: 0; width: 1px; height: 1px; overflow: hidden; opacity: 0; }
