/* STIM Control Center — V1. Sin dependencias externas (CSP 'self'). */
:root {
  --bg: #f4f5f7; --surface: #ffffff; --surface-2: #f0f2f5; --border: #dde1e7; --text: #1b1f24; --muted: #5f6b7a;
  --accent: #2f5bea; --accent-ink: #ffffff;
  --ok: #1f8a4c; --ok-bg: #e5f5ec; --warn: #b26a00; --warn-bg: #fff3dc; --fail: #c62828; --fail-bg: #fde8e8;
  --unk: #6b7280; --unk-bg: #eef0f3; --info: #2f5bea; --info-bg: #e8eefe;
  --stim-bg: #fbfbfd; --me-bg: #e8eefe; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.05);
  --radius: 10px; --topbar-h: 52px; --tabs-h: 44px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216; --surface: #171b21; --surface-2: #1d222a; --border: #2a313b; --text: #e6e9ee; --muted: #94a0b0;
    --accent: #6d8cff; --accent-ink: #0f1216;
    --ok: #4cc27f; --ok-bg: #12301f; --warn: #f0b04a; --warn-bg: #33270f; --fail: #ff6b6b; --fail-bg: #3a1717;
    --unk: #9aa3af; --unk-bg: #232830; --info: #8aa4ff; --info-bg: #1c2540;
    --stim-bg: #13171c; --me-bg: #1c2540; --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  --bg: #0f1216; --surface: #171b21; --surface-2: #1d222a; --border: #2a313b; --text: #e6e9ee; --muted: #94a0b0;
  --accent: #6d8cff; --accent-ink: #0f1216; --stim-bg: #13171c; --me-bg: #1c2540;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; }
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; border-radius: 8px; border: 1px solid transparent; padding: 6px 12px;
  background: var(--accent); color: var(--accent-ink); }
button.ghost { background: transparent; color: var(--text); border-color: var(--border); }
button.icon { padding: 4px 10px; }
button:focus-visible, textarea:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; display: grid; gap: 14px; box-shadow: var(--shadow); }
.login-brand { font-weight: 700; letter-spacing: .08em; display: flex; align-items: center; gap: 8px; }
.login-card label { display: grid; gap: 6px; color: var(--muted); font-size: 13px; }
.login-card input { font: inherit; padding: 9px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); }
.login-error { color: var(--fail); min-height: 1em; margin: 0; }

/* ---------- estructura */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar { height: var(--topbar-h); display: flex; align-items: center; gap: 16px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border); flex: none; }
.brand { font-weight: 700; letter-spacing: .08em; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--unk); display: inline-block; }
.dot.live { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-bg); }
.dot.down { background: var(--fail); }
.alert-summary { display: flex; gap: 6px; background: transparent; color: var(--text); padding: 4px; border: 0; }
.sev { padding: 3px 10px; border-radius: 999px; font-size: 12.5px; white-space: nowrap; }
.sev b { font-variant-numeric: tabular-nums; }
.sev-critical { background: var(--fail-bg); color: var(--fail); }
.sev-warning { background: var(--warn-bg); color: var(--warn); }
.sev-info { background: var(--info-bg); color: var(--info); }
.sev.zero { background: var(--surface-2); color: var(--muted); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.live { color: var(--muted); font-size: 12.5px; white-space: nowrap; }
.stim-toggle.on { background: var(--surface-2); color: var(--text); border-color: var(--border); }

.tabs { height: var(--tabs-h); display: flex; gap: 2px; padding: 0 12px; background: var(--surface);
  border-bottom: 1px solid var(--border); overflow-x: auto; flex: none; scrollbar-width: none; }
.tabs button { background: transparent; color: var(--muted); border-radius: 0; border: 0;
  border-bottom: 2px solid transparent; padding: 0 14px; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tabs .tab-count { margin-left: 6px; font-size: 11px; padding: 1px 6px; border-radius: 999px; }

.workspace { flex: 1; display: flex; min-height: 0; position: relative; }
.dashboard { flex: 1; min-width: 0; overflow: auto; padding: 20px 24px 40px; }
.resizer { width: 6px; cursor: col-resize; flex: none; background: transparent; border-left: 1px solid var(--border); }
.resizer:hover, .resizer.dragging { background: var(--accent); opacity: .35; }
.stim-panel { width: var(--stim-w, 420px); flex: none; display: flex; flex-direction: column; min-height: 0;
  background: var(--stim-bg); }
.workspace.stim-closed .stim-panel, .workspace.stim-closed .resizer { display: none; }
.stim-handle { position: fixed; right: 0; top: 50%; transform: translateY(-50%); writing-mode: vertical-rl;
  border-radius: 8px 0 0 8px; padding: 14px 7px; letter-spacing: .12em; font-weight: 700; z-index: 5;
  box-shadow: var(--shadow); }
.stim-fab { display: none; }

/* ---------- panel STIM */
.stim-head { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--border);
  background: var(--surface); flex: none; }
.stim-title { display: flex; align-items: center; gap: 8px; }
.stim-title small { display: block; color: var(--muted); font-size: 11.5px; }
.stim-avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-weight: 700; }
.ctx-chip { margin-left: auto; font-size: 12px; padding: 3px 10px; border-radius: 999px; background: var(--info-bg);
  color: var(--info); white-space: nowrap; }
.stim-bg { padding: 6px 12px; font-size: 12.5px; color: var(--muted); border-bottom: 1px solid var(--border); }
.stim-messages { flex: 1; overflow-y: auto; padding: 14px 12px; display: flex; flex-direction: column; gap: 10px;
  overscroll-behavior: contain; }
.msg { max-width: 92%; padding: 9px 12px; border-radius: 12px; white-space: normal; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--me-bg); border-bottom-right-radius: 4px; }
.msg.stim { align-self: flex-start; background: var(--surface); border: 1px solid var(--border);
  border-bottom-left-radius: 4px; }
.msg.alert { align-self: flex-end; background: var(--warn-bg); border: 1px solid var(--warn); }
.msg.notice { align-self: center; background: transparent; color: var(--muted); font-size: 12.5px; text-align: center; }
.msg .meta { display: flex; gap: 6px; align-items: center; font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.msg .scope-tag { padding: 0 6px; border-radius: 999px; background: var(--surface-2); }
.msg p { margin: 0 0 6px; } .msg p:last-child { margin: 0; }
.msg ul { margin: 4px 0 6px; padding-left: 18px; }
.msg code { background: var(--surface-2); padding: 1px 4px; border-radius: 4px; font-size: 12.5px; }
.stim-indicator { margin: 0 12px 8px; padding: 8px 10px; border-radius: 10px; background: var(--surface);
  border: 1px solid var(--border); font-size: 12.5px; }
.stim-indicator .phase { display: flex; align-items: center; gap: 8px; }
.pulse { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite; }
@keyframes pulse { 0%, 100% { opacity: .3 } 50% { opacity: 1 } }
.stim-indicator button { background: none; color: var(--accent); padding: 0; margin-left: auto; border: 0; font-size: 12px; }
.stim-indicator ol { margin: 6px 0 0; padding-left: 18px; color: var(--muted); }
.stim-composer { padding: 10px 12px 12px; border-top: 1px solid var(--border); background: var(--surface); flex: none; }
.stim-composer textarea { width: 100%; resize: vertical; min-height: 64px; max-height: 40vh; font: inherit; padding: 9px 10px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); }
.composer-row { display: flex; align-items: center; justify-content: space-between; margin-top: 6px; }
.hint { color: var(--muted); }

/* ---------- dashboard */
.dash-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.dash-head h1 { font-size: 20px; margin: 0; }
.updated { color: var(--muted); font-size: 12.5px; }
.summary-line { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 18px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; margin-bottom: 22px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px;
  box-shadow: var(--shadow); }
.sys-card { cursor: pointer; display: grid; gap: 8px; text-align: left; color: var(--text); }
.sys-card:hover { border-color: var(--accent); }
.sys-card h3 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.sys-card .row { display: flex; justify-content: space-between; color: var(--muted); font-size: 12.5px; }
.badge { font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; letter-spacing: .03em; }
.st-OK, .st-ok { background: var(--ok-bg); color: var(--ok); }
.st-DEGRADED, .st-warn, .st-WARNING { background: var(--warn-bg); color: var(--warn); }
.st-DOWN, .st-fail, .st-CRITICAL { background: var(--fail-bg); color: var(--fail); }
.st-UNKNOWN, .st-unknown { background: var(--unk-bg); color: var(--unk); }
.st-INFO { background: var(--info-bg); color: var(--info); }
.lab-tag { font-size: 10.5px; padding: 1px 6px; border-radius: 4px; background: var(--surface-2); color: var(--muted); }
section.block { margin-bottom: 22px; }
section.block h2 { font-size: 14px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 8px; }
table.list { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; }
table.list th, table.list td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.list th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); }
table.list tr:last-child td { border-bottom: 0; }
table.list tr.clickable { cursor: pointer; } table.list tr.clickable:hover td { background: var(--surface-2); }
.empty { color: var(--muted); padding: 12px 14px; background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-bottom: 22px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; }
.tile .k { color: var(--muted); font-size: 12px; } .tile .v { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile .s { color: var(--muted); font-size: 12px; }
.muted { color: var(--muted); }
.flash { animation: flash 1.2s ease-out; }
@keyframes flash { from { box-shadow: 0 0 0 3px var(--accent); } to { box-shadow: var(--shadow); } }
.unmeasured li { color: var(--muted); }

/* ---------- diálogo de alerta */
.alert-dialog { border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(620px, calc(100% - 32px));
  background: var(--surface); color: var(--text); }
.alert-dialog::backdrop { background: rgba(0,0,0,.35); }
.dialog-body { padding: 18px 20px; }
.dialog-head { display: flex; align-items: center; gap: 10px; } .dialog-head h3 { margin: 0; font-size: 16px; }
.dlg-meta { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; font-size: 12.5px; }
.dlg-meta dt { color: var(--muted); } .dlg-meta dd { margin: 0; }
.alert-dialog pre { background: var(--surface-2); padding: 10px; border-radius: 8px; max-height: 220px; overflow: auto;
  font-size: 12px; white-space: pre-wrap; }
.alert-dialog h4 { margin: 14px 0 6px; font-size: 12.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }

/* ---------- móvil */
@media (max-width: 767px) {
  .topbar { gap: 8px; padding: 0 12px; }
  .brand span:last-child { display: none; }
  .alert-summary .sev { font-size: 11.5px; padding: 2px 7px; }
  .live, #stimToggle { display: none; }
  .dashboard { padding: 14px 16px 90px; }
  .resizer { display: none !important; }
  .stim-panel { position: fixed; inset: 0; width: 100% !important; z-index: 20; transform: translateX(100%);
    transition: transform .22s ease; display: flex !important; }
  .workspace.stim-open-mobile .stim-panel { transform: none; }
  .stim-handle { display: none !important; }
  .stim-fab { display: block; position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
    z-index: 10; border-radius: 999px; padding: 12px 18px; font-weight: 700; box-shadow: var(--shadow); }
  .workspace.stim-open-mobile ~ .stim-fab { display: none; }
  table.list th:nth-child(n+4), table.list td:nth-child(n+4) { display: none; }
}

/* ---------- V2: pestañas de trabajo (Claude · Terminal) */
.tabs .tab-work { margin-left: 2px; }
.tabs .tab-work::before { content: "▸ "; color: var(--muted); font-size: 11px; }
.terms { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; background: #0b0f14; }
.term-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.term-bar { display: flex; align-items: center; gap: 12px; padding: 6px 12px; background: var(--surface); border-bottom: 1px solid var(--border); flex: none; font-size: 12.5px; }
.term-title { font-weight: 600; }
.term-state { color: var(--muted); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.term-state.ok { color: var(--ok); } .term-state.down { color: var(--fail); } .term-state.wait { color: var(--warn); }
.term-notice { padding: 6px 12px; font-size: 12.5px; color: var(--warn); background: var(--warn-bg); border-bottom: 1px solid var(--border); flex: none; }
.term-host { flex: 1; min-height: 0; padding: 6px 4px 4px 8px; }
.term-host .xterm { height: 100%; }
.term-host .xterm-viewport { overflow-y: auto !important; }

/* ---------- V2: adjuntos en la conversación */
.attach-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); }
.chip small { color: var(--muted); }
.chip .chip-x { background: transparent; border: 0; color: var(--muted); padding: 0 2px; font-size: 13px; line-height: 1; }
.chip.st-ok { border-color: var(--ok); } .chip.st-fail { border-color: var(--fail); color: var(--fail); }
.msg-attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.attach-btn { font-size: 12px; padding: 4px 10px; border: 1px solid var(--border); border-radius: 8px; cursor: pointer; color: var(--text); background: transparent; }
.attach-btn:hover { border-color: var(--accent); }

/* ---------- V2: taller */
.grid.machines { grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); }
.card.machine { display: grid; gap: 6px; }
.card.machine h3 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.card.machine .row { display: flex; justify-content: space-between; color: var(--muted); font-size: 12.5px; }
.card.machine details { font-size: 12.5px; } .card.machine summary { cursor: pointer; color: var(--accent); }
.card.machine ul { margin: 6px 0; padding-left: 18px; color: var(--muted); }
.small { font-size: 12px; }

@media (max-width: 767px) {
  .terms { padding-bottom: 0; }
  .term-bar .term-restart { display: none; }
}

/* ---------- Claude: conversación directa (chat, NO terminal) */
.claude-chat { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--bg); }
.cc-head { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface);
  border-bottom: 1px solid var(--border); flex: none; }
.cc-title { display: flex; align-items: center; gap: 8px; }
.cc-title small { display: block; color: var(--muted); font-size: 11.5px; }
.cc-avatar { width: 30px; height: 30px; border-radius: 50%; background: #d97757; color: #fff; display: grid;
  place-items: center; font-weight: 700; }
.cc-state { margin-left: auto; color: var(--warn); font-size: 12.5px; }
.cc-messages { flex: 1; overflow-y: auto; padding: 18px max(16px, calc((100% - 820px) / 2)); display: flex;
  flex-direction: column; gap: 12px; overscroll-behavior: contain; }
.cc-empty { color: var(--muted); background: var(--surface); border: 1px dashed var(--border);
  border-radius: var(--radius); padding: 16px; max-width: 760px; }
.claude-chat .msg { max-width: min(90%, 760px); padding: 10px 13px; border-radius: 14px; white-space: normal;
  overflow-wrap: anywhere; }
.claude-chat .msg.user { align-self: flex-end; background: var(--me-bg); border-bottom-right-radius: 4px; }
.claude-chat .msg.claude { align-self: flex-start; background: var(--surface); border: 1px solid var(--border);
  border-bottom-left-radius: 4px; }
.claude-chat .msg.degradado { border-color: var(--warn); }
.claude-chat .msg .meta { display: flex; gap: 6px; align-items: center; font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.claude-chat .msg p { margin: 0 0 6px; } .claude-chat .msg p:last-child { margin: 0; }
.claude-chat .msg ul { margin: 4px 0 6px; padding-left: 18px; }
.claude-chat .msg code { background: var(--surface-2); padding: 1px 4px; border-radius: 4px; font-size: 12.5px; }
.cc-indicator { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12.5px;
  padding: 0 max(16px, calc((100% - 820px) / 2)) 4px; }
.cc-composer { flex: none; display: flex; gap: 8px; align-items: flex-end; padding: 12px max(16px, calc((100% - 820px) / 2));
  border-top: 1px solid var(--border); background: var(--surface); }
.cc-composer textarea { flex: 1; resize: none; max-height: 200px; min-height: 44px; font: inherit; font-size: 15px;
  padding: 11px 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); }
.cc-composer textarea:focus { outline: none; border-color: var(--accent); }
.cc-composer button { align-self: stretch; padding: 0 18px; border-radius: 12px; }
.cc-composer button:disabled { opacity: .5; cursor: default; }

@media (max-width: 767px) {
  .cc-messages, .cc-composer, .cc-indicator { padding-left: 14px; padding-right: 14px; }
}

/* ---------- STIM: tres modos (oculto / lateral / grande) */
.workspace.stim-hidden .stim-panel, .workspace.stim-hidden .resizer { display: none; }
.workspace.stim-large .dashboard, .workspace.stim-large .terms, .workspace.stim-large .resizer { display: none; }
.workspace.stim-large .stim-panel { width: auto; flex: 1; }
.workspace.stim-large .stim-messages { padding-left: max(14px, calc((100% - 860px) / 2)); padding-right: max(14px, calc((100% - 860px) / 2)); }
.workspace.stim-large .stim-composer { padding-left: max(12px, calc((100% - 860px) / 2)); padding-right: max(12px, calc((100% - 860px) / 2)); }
/* burbuja en streaming (deltas del Agent SDK) */
.stim-panel .msg.streaming::after { content: "▍"; color: var(--muted); animation: cc-blink 1s steps(2) infinite; }
@keyframes cc-blink { 50% { opacity: 0; } }

@media (max-width: 767px) {
  /* En móvil sólo hay oculto o grande (pantalla completa). */
  .workspace.stim-large .stim-panel { transform: none; }
  .workspace.stim-side .stim-panel, .workspace.stim-hidden .stim-panel { transform: translateX(100%); }
  .workspace.stim-large .stim-messages, .workspace.stim-large .stim-composer { padding-left: 12px; padding-right: 12px; }
}
