/* Magic Chat: herramienta de trabajo. Barra superior, contenido en una columna de hasta 1040px, filas en lugar de tarjetas sueltas. */
:root {
  --bg: #F5F6F4;
  --surface: #FFFFFF;
  --ink: #17201D;
  --muted: #5E6A65;
  --line: #DCE1DE;
  --accent: #0E6E62;
  --accent-ink: #FFFFFF;
  --accent-soft: #DDF0EC;
  --ok: #1D7A46; --ok-soft: #DFF2E6;
  --warn: #9A5B00; --warn-soft: #FBEFD9;
  --bad: #B3261E; --bad-soft: #FBE4E2;
  --bubble: #EEF1EF;
  --font-ui: "Instrument Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-display: "Bricolage Grotesque", "Instrument Sans", system-ui, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111614; --surface: #19201D; --ink: #E6ECE9; --muted: #98A39E; --line: #2C3531;
    --accent: #4CC2B0; --accent-ink: #0B1513; --accent-soft: #173430;
    --ok: #5FD08F; --ok-soft: #173323; --warn: #F0B357; --warn-soft: #3A2C14; --bad: #F2867E; --bad-soft: #3D1C1A;
    --bubble: #232B28;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 var(--font-ui); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
img { max-width: 100%; }
h1, h2, h3 { font-family: var(--font-display); line-height: 1.15; margin: 0; text-wrap: balance; }
h1 { font-size: clamp(24px, 3vw, 30px); font-weight: 700; letter-spacing: -0.01em; }
h2 { font-size: 19px; font-weight: 600; }
h3 { font-size: 16px; font-weight: 600; }
p { margin: 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.wrap { max-width: 1040px; margin: 0 auto; padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }

/* Barra superior */
.topbar { background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar .wrap { display: flex; align-items: center; gap: 20px; min-height: 60px; flex-wrap: wrap; padding-block: 8px; }
.brand { display: flex; align-items: center; gap: 9px; font-family: var(--font-display); font-weight: 700; font-size: 18px; color: var(--ink); text-decoration: none; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 15px; }
.nav { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
.nav a { color: var(--muted); text-decoration: none; padding: 6px 10px; border-radius: 8px; font-weight: 500; }
.nav a:hover { color: var(--ink); background: var(--bg); }
.nav a[aria-current="page"] { color: var(--ink); background: var(--accent-soft); }
.who { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13.5px; }
.who img { width: 26px; height: 26px; border-radius: 50%; }

main { padding-block: 28px 64px; display: grid; gap: 22px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.page-head p { color: var(--muted); margin-top: 6px; max-width: 62ch; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* Botones */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 600; font-size: 14px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); text-decoration: none; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.danger.solid { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.linkish { background: none; border: 0; padding: 0; font: inherit; color: var(--muted); cursor: pointer; text-decoration: underline; }

/* Avisos */
.flash { padding: 11px 14px; border-radius: 8px; border: 1px solid; font-size: 14px; }
.flash.success { background: var(--ok-soft); border-color: color-mix(in srgb, var(--ok) 35%, transparent); color: var(--ok); }
.flash.warning { background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn) 35%, transparent); color: var(--warn); }
.flash.error { background: var(--bad-soft); border-color: color-mix(in srgb, var(--bad) 35%, transparent); color: var(--bad); }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.panel-body { padding: 20px; display: grid; gap: 14px; }

/* Lista de automatizaciones */
.auto-list { display: grid; }
.auto { display: grid; grid-template-columns: 64px minmax(0, 1fr) auto; gap: 16px; padding: 18px 20px; border-top: 1px solid var(--line); align-items: start; }
.auto:first-child { border-top: 0; }
.auto.paused { opacity: .72; }
.thumb { width: 64px; height: 64px; border-radius: 8px; object-fit: cover; background: var(--bubble); display: grid; place-items: center; color: var(--muted); font-size: 12px; text-align: center; line-height: 1.2; }
.auto-main { min-width: 0; display: grid; gap: 7px; }
.auto-title { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.auto-title a { color: var(--ink); text-decoration: none; }
.auto-title a:hover { text-decoration: underline; }
.meta { color: var(--muted); font-size: 13.5px; }
.chips { display: flex; gap: 5px; flex-wrap: wrap; }
.chip { font-size: 12.5px; padding: 2px 8px; border-radius: 999px; background: var(--bubble); font-weight: 500; }
.pill { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; }
.pill.on { background: var(--ok-soft); color: var(--ok); }
.pill.off { background: var(--bubble); color: var(--muted); }
.pill.err { background: var(--bad-soft); color: var(--bad); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.stats { display: flex; gap: 18px; flex-wrap: wrap; font-variant-numeric: tabular-nums; font-size: 13.5px; }
.stats b { font-size: 16px; font-family: var(--font-display); margin-right: 3px; }
.stats .failed b { color: var(--bad); }
.conflict { font-size: 13px; color: var(--warn); background: var(--warn-soft); padding: 6px 10px; border-radius: 6px; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.row-actions form { margin: 0; }
details.confirm { position: relative; }
details.confirm summary { list-style: none; }
details.confirm summary::-webkit-details-marker { display: none; }
details.confirm .confirm-box { position: absolute; right: 0; top: calc(100% + 6px); z-index: 5; width: 250px; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 12px; box-shadow: 0 8px 24px rgb(0 0 0 / .12); display: grid; gap: 10px; font-size: 13.5px; }

.empty { padding: 36px 24px; display: grid; gap: 14px; justify-items: start; }
.empty p { color: var(--muted); max-width: 56ch; }

/* Formularios */
form.stack { display: grid; gap: 22px; }
.field { display: grid; gap: 6px; }
.field > label, .legend { font-weight: 600; font-size: 14px; }
.hint { color: var(--muted); font-size: 13px; }
.error-text { color: var(--bad); font-size: 13px; font-weight: 500; }
input[type=text], input[type=email], input[type=password], input[type=url], textarea {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; width: 100%;
}
textarea { min-height: 110px; resize: vertical; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.has-error input, .has-error textarea { border-color: var(--bad); }
.radio-row { display: flex; gap: 8px; flex-wrap: wrap; }
.radio-card { display: flex; gap: 8px; align-items: flex-start; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; cursor: pointer; flex: 1 1 240px; background: var(--surface); }
.radio-card:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.radio-card input { margin-top: 3px; accent-color: var(--accent); }
.radio-card small { display: block; color: var(--muted); font-size: 12.5px; }

.form-grid { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 24px; align-items: start; }
.section { display: grid; gap: 16px; padding: 20px; }
.section + .section { border-top: 1px solid var(--line); }
.section-head { display: grid; gap: 2px; }

.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; max-height: 420px; overflow-y: auto; padding: 2px; }
.media-option { position: relative; cursor: pointer; border-radius: 8px; overflow: hidden; border: 2px solid transparent; background: var(--bubble); aspect-ratio: 1; }
.media-option img { width: 100%; height: 100%; object-fit: cover; display: block; }
.media-option input { position: absolute; opacity: 0; pointer-events: none; }
.media-option .tag { position: absolute; left: 4px; bottom: 4px; font-size: 11px; font-weight: 600; background: rgb(0 0 0 / .62); color: #fff; padding: 1px 6px; border-radius: 4px; }
.media-option .nocap { position: absolute; inset: 0; display: grid; place-items: center; padding: 6px; font-size: 11.5px; color: var(--muted); text-align: center; }
.media-option:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.media-option:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
.media-option:has(input:checked)::after { content: "✓"; position: absolute; top: 5px; right: 5px; width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 13px; font-weight: 700; }

.buttons-editor { display: grid; gap: 8px; }
.button-row { display: grid; grid-template-columns: 170px minmax(0, 1fr); gap: 8px; }
.counter { font-variant-numeric: tabular-nums; }
.counter.over { color: var(--bad); font-weight: 600; }
.vars { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.vars button { font: 12.5px var(--font-mono); padding: 2px 8px; border-radius: 6px; border: 1px dashed var(--line); background: var(--surface); color: var(--ink); cursor: pointer; }

/* Vista previa del DM */
.preview { position: sticky; top: 16px; display: grid; gap: 10px; }
.phone { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 16px 14px 20px; display: grid; gap: 10px; }
.phone-head { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.phone-head img, .phone-head .avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--accent-soft); }
.bubble { background: var(--bubble); border-radius: 16px 16px 16px 4px; padding: 10px 12px; font-size: 14px; white-space: pre-wrap; overflow-wrap: anywhere; max-width: 92%; }
.bubble-buttons { display: grid; gap: 1px; background: var(--line); border-radius: 0 0 14px 14px; overflow: hidden; max-width: 92%; margin-top: -9px; }
.bubble-buttons span { background: var(--bubble); text-align: center; padding: 8px; font-size: 13.5px; font-weight: 600; color: var(--accent); }
.mine { justify-self: end; background: var(--accent); color: var(--accent-ink); border-radius: 16px 16px 4px 16px; padding: 8px 12px; font-size: 14px; max-width: 80%; }

.sticky-save { position: sticky; bottom: 0; background: var(--surface); border-top: 1px solid var(--line); padding: 14px 20px; display: flex; gap: 10px; justify-content: flex-end; border-radius: 0 0 var(--radius) var(--radius); }

/* Tablas */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; padding: 10px 14px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 11px 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
td.num { font-variant-numeric: tabular-nums; text-align: right; }
.nowrap { white-space: nowrap; }

.log { display: grid; }
.log-item { display: grid; grid-template-columns: 150px 90px minmax(0, 1fr); gap: 12px; padding: 11px 16px; border-top: 1px solid var(--line); font-size: 14px; }
.log-item:first-child { border-top: 0; }
.log-item time { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tabs a { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); text-decoration: none; color: var(--muted); font-size: 13.5px; font-weight: 500; background: var(--surface); }
.tabs a[aria-current="page"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }

/* Cuenta */
.account-card { display: grid; grid-template-columns: 64px minmax(0, 1fr) auto; gap: 16px; align-items: center; }
.account-card img, .account-card .avatar { width: 64px; height: 64px; border-radius: 50%; background: var(--accent-soft); }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1px; background: var(--line); border-top: 1px solid var(--line); }
.facts div { background: var(--surface); padding: 14px 20px; display: grid; gap: 2px; }
.facts dt { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
.facts dd { margin: 0; font-weight: 500; overflow-wrap: anywhere; }
.steps { margin: 0; padding-left: 20px; display: grid; gap: 8px; color: var(--ink); }
code { font-family: var(--font-mono); font-size: 13px; background: var(--bubble); padding: 1px 5px; border-radius: 4px; overflow-wrap: anywhere; }

/* Páginas públicas */
.public { min-height: 100vh; display: grid; place-items: start center; padding: 56px max(16px, env(safe-area-inset-right)) 56px max(16px, env(safe-area-inset-left)); }
.public-card { width: min(420px, 100%); display: grid; gap: 22px; }
.public-doc { width: min(720px, 100%); display: grid; gap: 16px; }
.public-doc h2 { margin-top: 12px; }
.public-doc p, .public-doc li { color: var(--ink); max-width: 68ch; }
.public-doc ul { margin: 0; padding-left: 20px; display: grid; gap: 6px; }
.footer-links { display: flex; gap: 14px; font-size: 13px; color: var(--muted); }
.footer-links a { color: var(--muted); }

@media (max-width: 820px) {
  .form-grid { grid-template-columns: 1fr; }
  .preview { position: static; }
  .auto { grid-template-columns: 48px minmax(0, 1fr); }
  .thumb { width: 48px; height: 48px; }
  .row-actions { grid-column: 1 / -1; justify-content: flex-start; }
  .log-item { grid-template-columns: 1fr; gap: 4px; }
  .button-row { grid-template-columns: 1fr; }
  .account-card { grid-template-columns: 48px minmax(0, 1fr); }
  .account-card img, .account-card .avatar { width: 48px; height: 48px; }
  .account-card .actions { grid-column: 1 / -1; }
  details.confirm .confirm-box { left: 0; right: auto; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
