:root {
  --bg: #141414;
  --panel: #1e1e1e;
  --panel-2: #262626;
  --line: #333333;
  --text: #ededed;
  --muted: #9a9a9a;
  --accent: #d32f2f;
  --accent-strong: #b71c1c;
  --ok: #2ecc71;
  --warn: #f9a825;
  --danger: #e53935;
  --off: #4a4a4a;
  --radius: 12px;
  --gutter: 16px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; letter-spacing: .02em; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.danger-text { color: #ff8a80 !important; }

/* ───── Barra superior ───── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px var(--gutter);
  padding-top: max(10px, env(safe-area-inset-top));
  background: #171717;
  border-bottom: 3px solid var(--accent);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { width: 32px; height: 32px; border-radius: 8px; flex: none; display: block; }
.brand-text { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.user-name { display: none; }
.hub-state { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.hub-state .dot { background: var(--warn); }
.hub-state.ok .dot { background: var(--ok); }
.hub-state.ok .hub-label { display: none; }

main { flex: 1; width: 100%; max-width: 1400px; margin: 0 auto; padding: var(--gutter); }

/* ───── Controles ───── */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: #2f2f2f; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-sm { min-height: 34px; padding: 6px 12px; font-size: 14px; }
.btn-block { width: 100%; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-strong); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--panel-2); color: var(--text); }
.btn-danger { background: #8e1b1b; border-color: #c62828; color: #fff; }
.btn-danger:hover { background: #a82020; }
.btn-warn { background: #5c4300; border-color: #a77a00; color: #ffe08a; }
.btn-warn:hover { background: #6e5100; }
.btn-ok { background: #144d2c; border-color: #1f7a45; color: #b9f6ca; }
.btn-ok:hover { background: #18603a; }
.btn-outline-danger { background: transparent; border-color: #c62828; color: #ff8a80; }
.btn-outline-danger:hover { background: rgba(198, 40, 40, .12); }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.field input, .composer textarea {
  font: inherit;
  font-size: 16px; /* evita el zoom automático en iOS */
  color: var(--text);
  background: #121212;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
  width: 100%;
}
.field input:focus, .composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.inline { display: flex; gap: 8px; }
.form-error { color: #ff8a80; min-height: 1.2em; margin: 8px 0; font-size: 14px; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
}

.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--off); flex: none; display: inline-block; }

/* ───── Login ───── */
.view-login { display: grid; place-items: center; min-height: 70vh; }
.login-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; }
.login-card p { margin: 0; }

/* ───── Lista de sesiones ───── */
.sessions-layout { display: grid; gap: var(--gutter); }
#new-session-form { display: flex; flex-direction: column; gap: 14px; align-self: start; }
.form-grid { display: grid; gap: 12px; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.session-list { display: flex; flex-direction: column; gap: 10px; }
.session-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: grid;
  gap: 10px;
}
.session-card.fresh { border-color: var(--accent); }
.session-card .row { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.session-card .name { font-weight: 600; font-size: 16px; }
.session-card .meta { font-size: 13px; color: var(--muted); }
.session-card .actions { display: flex; flex-wrap: wrap; gap: 8px; }
.session-card .actions .btn { flex: 1 1 auto; }
.kiosk-mini { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.kiosk-mini.on .dot { background: var(--ok); }
.kiosk-mini.off .dot { background: var(--danger); }
.empty { color: var(--muted); padding: 18px; text-align: center; border: 1px dashed var(--line); border-radius: var(--radius); }
.history { margin-top: 18px; }
.history summary { cursor: pointer; color: var(--muted); padding: 8px 0; font-weight: 600; }
.history .session-list { margin-top: 8px; }
.copy { cursor: pointer; border-bottom: 1px dotted var(--muted); }

/* ───── Sesión ───── */
.session-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.session-title { flex: 1; min-width: 0; }
.session-title h2 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-title p { margin: 2px 0 0; font-size: 13px; }
.kiosk-state {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 12px; border-radius: 999px; font-size: 13px; font-weight: 600;
  background: var(--panel-2); border: 1px solid var(--line);
}
.kiosk-state.on .dot { background: var(--ok); }
.kiosk-state.off { color: #ff8a80; border-color: #7f1d1d; background: rgba(198, 40, 40, .12); }
.kiosk-state.off .dot { background: var(--danger); }

.plc-strip { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 10px; border-radius: 999px; font-size: 13px;
  background: var(--panel); border: 1px solid var(--line);
}
.pill.run .dot { background: var(--ok); }
.pill.stop .dot { background: var(--danger); }
.pill.warn .dot { background: var(--warn); }
.pill.bad .dot { background: var(--danger); }
.pill-stop { background: #8e1b1b; border-color: #c62828; color: #fff; font-weight: 700; letter-spacing: .03em; }

.tabs {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 4px;
  margin-bottom: 12px;
}
.tabs button {
  appearance: none; border: 0; background: transparent; color: var(--muted);
  font: inherit; font-weight: 600; padding: 9px 0; border-radius: 9px; cursor: pointer;
}
.tabs button.active { background: var(--panel-2); color: var(--text); }
.badge {
  display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 6px;
  border-radius: 999px; background: var(--accent); color: #fff; font-size: 12px; margin-left: 4px;
}

.session-grid { display: grid; gap: var(--gutter); }
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  min-width: 0;
}
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.panel-head h3 { text-transform: uppercase; color: var(--muted); font-size: 13px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Chat */
.panel-chat { display: none; flex-direction: column; }
.panel-chat.active { display: flex; height: calc(100dvh - 300px); min-height: 360px; }
.messages {
  flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px;
  padding: 4px 2px 8px; overscroll-behavior: contain;
}
.msg { max-width: 82%; display: flex; flex-direction: column; gap: 2px; }
.msg .bubble { padding: 8px 12px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg .meta { font-size: 11px; color: var(--muted); padding: 0 4px; }
.msg-tecnico { align-self: flex-end; align-items: flex-end; }
.msg-tecnico .bubble { background: #4a2323; border: 1px solid #6d2a2a; border-bottom-right-radius: 4px; }
.msg-operario { align-self: flex-start; }
.msg-operario .bubble { background: #2d3540; border: 1px solid #3c4756; border-bottom-left-radius: 4px; }
.msg-sistema { align-self: center; max-width: 92%; text-align: center; }
.msg-sistema .bubble { background: transparent; color: var(--muted); font-size: 13px; font-style: italic; padding: 2px 8px; }
.msg.pending .bubble { opacity: .6; }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); padding: 4px 0; }

.quick-replies {
  display: flex; gap: 6px; overflow-x: auto; padding: 8px 0; scrollbar-width: none;
}
.quick-replies::-webkit-scrollbar { display: none; }
.chip {
  appearance: none; flex: none; border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  border-radius: 999px; padding: 7px 12px; font: inherit; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.chip:hover { border-color: var(--accent); }
.composer { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; max-height: 140px; line-height: 1.35; }
.closed-note { color: var(--muted); text-align: center; margin: 8px 0 0; }

/* Control */
.panel-control .status-form { margin-bottom: 14px; }
.panel-control .status-form p { margin: 6px 0 0; }
.mode-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.mode-btn { display: flex; flex-direction: column; align-items: center; gap: 2px; min-height: 64px; }
.mode-btn small { font-weight: 400; font-size: 12px; opacity: .8; }
.mode-btn.active { outline: 2px solid #fff; outline-offset: 2px; }
.session-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 14px; flex-wrap: wrap; }

/* I/O */
.io-stale {
  background: rgba(249, 168, 37, .12); border: 1px solid #a77a00; color: #ffe08a;
  border-radius: 10px; padding: 8px 12px; font-weight: 600; text-align: center; margin-bottom: 10px;
}
.io-mirror.stale { opacity: .45; filter: grayscale(1); }
.io-group h4 { margin: 12px 0 6px; font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: .04em; }
.io-group:first-child h4 { margin-top: 0; }
.io-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 4px 10px; }
.io-item { display: flex; align-items: center; gap: 8px; min-width: 0; font-size: 13px; padding: 3px 0; }
.io-item .addr { font-family: Consolas, ui-monospace, monospace; font-weight: 600; flex: none; min-width: 3.2em; }
.io-item .lbl { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.io-item.on .dot { background: var(--ok); box-shadow: 0 0 6px rgba(46, 204, 113, .7); }
.io-reg { display: flex; justify-content: space-between; gap: 10px; padding: 5px 0; border-bottom: 1px solid #2a2a2a; font-size: 13px; }
.io-reg .val { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #2b2b2b; border: 1px solid var(--line); color: var(--text);
  padding: 10px 16px; border-radius: 10px; box-shadow: 0 6px 24px rgba(0, 0, 0, .4); z-index: 50;
  max-width: calc(100vw - 32px);
}
.toast.error { border-color: #c62828; color: #ffcdd2; }

/* ───── Escritorio ───── */
@media (min-width: 720px) {
  .form-grid { grid-template-columns: 1fr 1fr; }
  .user-name { display: inline; }
}

@media (min-width: 960px) {
  .sessions-layout { grid-template-columns: minmax(320px, 400px) 1fr; align-items: start; }
  .tabs { display: none; }
  .session-grid {
    grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
    grid-template-rows: auto 1fr;
    height: calc(100dvh - 210px);
    min-height: 520px;
  }
  .tab-panel, .tab-panel.active { display: block; }
  .panel-chat, .panel-chat.active { display: flex; grid-row: 1 / span 2; height: auto; min-height: 0; }
  .panel-io { overflow-y: auto; min-height: 0; }
}
