:root {
  --bg: #f6f5f2; --panel: #ffffff; --ink: #1d1b18; --muted: #6d6860; --line: #e4e1da;
  --accent: #c2410c; --accent-ink: #ffffff; --accent-soft: #fdeee6;
  --ok: #15803d; --ok-soft: #e7f6ec; --warn: #a16207; --warn-soft: #fdf6e3; --bad: #b91c1c; --bad-soft: #fdecec;
  --bubble-in: #efede8; --bubble-out: #c2410c; --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(0,0,0,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161513; --panel: #1f1d1a; --ink: #ece9e3; --muted: #a29d93; --line: #34312c;
    --accent: #f97316; --accent-ink: #1a0f06; --accent-soft: #3a2416;
    --ok: #4ade80; --ok-soft: #15291c; --warn: #facc15; --warn-soft: #2e2a12; --bad: #f87171; --bad-soft: #351919;
    --bubble-in: #2c2a26; --bubble-out: #ea580c; --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); }
h1 { font-size: 1.4rem; margin: 0 0 4px; }
h2 { font-size: 1.1rem; margin: 0 0 10px; }
h3 { font-size: .95rem; margin: 0 0 6px; }
p { margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }

/* Layout */
.layout { display: grid; grid-template-columns: 230px 1fr; min-height: 100vh; }
.side { background: var(--panel); border-right: 1px solid var(--line); padding: 16px 12px; display: flex; flex-direction: column; gap: 14px; }
.brand { font-weight: 700; font-size: 1.15rem; padding: 0 8px; }
.brand span { color: var(--accent); }
.side nav { display: flex; flex-direction: column; gap: 2px; }
.side nav a { padding: 8px 10px; border-radius: 8px; color: var(--ink); text-decoration: none; }
.side nav a:hover { background: var(--bg); }
.side nav a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side .bottom { margin-top: auto; display: flex; flex-direction: column; gap: 6px; }
main { padding: 24px 28px; max-width: 1200px; width: 100%; }
.head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.banner { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 14px; }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.bad { background: var(--bad-soft); color: var(--bad); }
.banner.ok { background: var(--ok-soft); color: var(--ok); }

/* Componentes */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
button, .btn { font: inherit; border: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: 7px 13px; border-radius: 8px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
button:hover, .btn:hover { border-color: var(--muted); }
button.primary, .btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--bad); }
button.link { border: 0; background: none; color: var(--accent); padding: 2px 4px; }
button:disabled { opacity: .5; cursor: default; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .88rem; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--ink); }
input, select, textarea { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; width: 100%; }
input[type=checkbox], input[type=radio] { width: auto; }
textarea { resize: vertical; min-height: 64px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.pill { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: .78rem; background: var(--bg); border: 1px solid var(--line); color: var(--muted); }
.pill.on { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.pill.off { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.pill.acc { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.stats { display: flex; gap: 16px; }
.stat b { display: block; font-size: 1.2rem; }
.stat span { font-size: .78rem; color: var(--muted); }
table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); font-size: .9rem; vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: .8rem; }
tr:last-child td { border-bottom: 0; }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: inline-flex; align-items: center; justify-content: center; font-weight: 700; object-fit: cover; flex: none; }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.toast { position: fixed; bottom: 18px; right: 18px; background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 8px; z-index: 50; max-width: 420px; }
.toast.bad { background: var(--bad); color: #fff; }

/* Modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; z-index: 40; }
.modal { background: var(--panel); border-radius: 12px; padding: 20px; width: 100%; max-width: 640px; }

/* Editor de flujos */
.node { border-left: 4px solid var(--line); }
.node.message { border-left-color: var(--accent); }
.node.ask { border-left-color: #2563eb; }
.node.tags { border-left-color: #7c3aed; }
.node.condition { border-left-color: #0d9488; }
.node.handover { border-left-color: var(--muted); }
.node-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.node-id { font-weight: 700; font-family: ui-monospace, monospace; background: var(--bg); border: 1px solid var(--line); padding: 1px 8px; border-radius: 6px; }
.btnrow { display: grid; grid-template-columns: 1fr 110px 1.3fr auto; gap: 6px; align-items: center; }
.cols2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; max-height: 300px; overflow-y: auto; }
.media { border: 2px solid var(--line); border-radius: 8px; overflow: hidden; cursor: pointer; font-size: .75rem; background: var(--bg); }
.media.sel { border-color: var(--accent); }
.media img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; }
.media div { padding: 4px 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.media .ph { aspect-ratio: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); }

/* Bandeja y simulador */
.inbox { display: grid; grid-template-columns: 300px 1fr; gap: 0; height: calc(100vh - 120px); background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.convs { border-right: 1px solid var(--line); overflow-y: auto; }
.conv { display: flex; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.conv:hover, .conv.on { background: var(--bg); }
.conv .t { overflow: hidden; min-width: 0; }
.conv .t div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread { display: flex; flex-direction: column; min-height: 0; }
.thread-head { padding: 10px 14px; border-bottom: 1px solid var(--line); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.msgs { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 72%; padding: 8px 12px; border-radius: 16px; background: var(--bubble-in); white-space: pre-wrap; word-wrap: break-word; }
.msg.out { align-self: flex-end; background: var(--bubble-out); color: #fff; }
.msg .meta { font-size: .7rem; opacity: .75; margin-top: 3px; }
.msg .opts { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; }
.msg .opts button { background: rgba(255,255,255,.18); color: inherit; border-color: rgba(255,255,255,.4); justify-content: center; }
.msg .opts.qr { flex-direction: row; flex-wrap: wrap; }
.compose { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }
.phone { max-width: 420px; height: 620px; border: 1px solid var(--line); border-radius: 22px; display: flex; flex-direction: column; overflow: hidden; background: var(--panel); }
.sim { display: grid; grid-template-columns: 1fr 420px; gap: 18px; align-items: start; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .side { border-right: 0; border-bottom: 1px solid var(--line); }
  .side nav { flex-direction: row; flex-wrap: wrap; }
  main { padding: 16px; }
  .inbox, .sim { grid-template-columns: 1fr; height: auto; }
  .convs { max-height: 260px; }
  .btnrow, .cols2 { grid-template-columns: 1fr; }
}

/* Login */
.center { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login { width: 100%; max-width: 380px; }
.qr { display: block; margin: 10px auto; width: 200px; height: 200px; background: #fff; padding: 6px; border-radius: 8px; }
code { font-family: ui-monospace, monospace; background: var(--bg); padding: 1px 5px; border-radius: 4px; word-break: break-all; }
