:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --surface-2: #f1eee6;
  --ink: #1d1b16;
  --ink-2: #5b574c;
  --ink-3: #8d887a;
  --line: #e4dfd3;
  --accent: #e8a317;
  --accent-ink: #4a3300;
  --accent-soft: #fbefd2;
  --ok: #2f7d4f;    --ok-soft: #e1f1e6;
  --warn: #b5462c;  --warn-soft: #f8e3dc;
  --info: #2c5f99;  --info-soft: #e2ecf7;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(29, 27, 22, .06), 0 2px 8px rgba(29, 27, 22, .04);
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15140f; --surface: #1e1c17; --surface-2: #26241d; --ink: #f1ede3; --ink-2: #bdb7a7; --ink-3: #8a8474;
    --line: #353228; --accent-soft: #3a2e12; --accent-ink: #1d1400;
    --ok: #6fc590; --ok-soft: #1d3326; --warn: #ef8c72; --warn-soft: #3d2219; --info: #8db8e8; --info-soft: #1c2a3b;
    --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.45; }
a { color: inherit; }
h1 { font-size: 26px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 17px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 0; }
.hidden { display: none !important; }
.muted { color: var(--ink-2); }
.faint { color: var(--ink-3); font-size: 13px; }
.error { color: var(--warn); min-height: 1em; margin: 0; font-size: 14px; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side { background: var(--surface); border-right: 1px solid var(--line); padding: 20px 14px; display: flex; flex-direction: column; gap: 22px; position: sticky; top: 0; height: 100vh; }
.side nav { display: flex; flex-direction: column; gap: 2px; }
.side nav a { display: flex; justify-content: space-between; align-items: center; padding: 9px 12px; border-radius: 9px; text-decoration: none; color: var(--ink-2); font-weight: 500; }
.side nav a:hover { background: var(--surface-2); color: var(--ink); }
.side nav a.active { background: var(--accent-soft); color: var(--ink); }
.logout { margin-top: auto; align-self: flex-start; }
.main { padding: 32px 40px 80px; max-width: 1100px; width: 100%; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 0 8px; }
.brand-lg { font-size: 22px; justify-content: center; margin-bottom: 8px; }
.logo { width: 22px; height: 24px; background: var(--accent); clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%); }

.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 24px; flex-wrap: wrap; }
.crumbs { font-size: 13px; color: var(--ink-3); margin-bottom: 6px; }
.crumbs a { color: var(--ink-2); text-decoration: none; }
.crumbs a:hover { text-decoration: underline; }
.section { margin-top: 32px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

/* ---------- Components ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.card-link { text-decoration: none; display: block; transition: border-color .15s; }
.card-link:hover { border-color: var(--accent); }

.btn { font: inherit; font-weight: 600; font-size: 14px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); padding: 8px 14px; border-radius: 9px; cursor: pointer; white-space: nowrap; }
a.btn { text-decoration: none; display: inline-flex; align-items: center; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--ink-2); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--warn); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

input, select, textarea { font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 9px; padding: 8px 11px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; min-height: 90px; }
textarea.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; min-height: 260px; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.form { display: grid; gap: 14px; }
.form-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; }

.pill { display: inline-block; font-size: 12px; font-weight: 700; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--ink-2); }
.pill:empty { display: none; }
.pill.warn { background: var(--warn-soft); color: var(--warn); }

.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 99px; background: var(--surface-2); color: var(--ink-2); white-space: nowrap; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .8; }
.b-enviado, .b-activa { background: var(--info-soft); color: var(--info); }
.b-abierto { background: var(--ok-soft); color: var(--ok); }
.b-respondido, .b-por_gestionar { background: var(--warn-soft); color: var(--warn); }
.b-cerrado, .b-completado, .b-finalizada { background: var(--surface-2); color: var(--ink-3); }
.b-baja { background: var(--surface-2); color: var(--ink-3); text-decoration: line-through; }
.b-sin_apertura, .b-pausada { background: var(--accent-soft); color: #9a6a00; }

.stats { display: flex; gap: 22px; margin-top: 14px; flex-wrap: wrap; }
.stat b { display: block; font-size: 22px; line-height: 1.1; }
.stat span { font-size: 12px; color: var(--ink-3); }
.stat.hot b { color: var(--warn); }

.banner { background: var(--accent-soft); border: 1px solid #f0d9a2; color: var(--ink); border-radius: var(--radius); padding: 11px 14px; font-size: 14px; margin-bottom: 22px; }
@media (prefers-color-scheme: dark) { .banner { border-color: #5a4720; } }

.empty { padding: 28px; text-align: center; color: var(--ink-3); border: 1px dashed var(--line); border-radius: var(--radius); }

/* ---------- Lists ---------- */
.list { display: flex; flex-direction: column; gap: 10px; }
.item { display: flex; gap: 14px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.item .who { flex: 1; min-width: 0; }
.item .who a { text-decoration: none; font-weight: 600; }
.item .who a:hover { text-decoration: underline; }
.item .what { font-size: 14px; color: var(--ink-2); margin-top: 2px; }
.item .actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.item.hot { border-left: 3px solid var(--warn); }

table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); font-size: 14px; vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-3); background: var(--surface-2); font-weight: 600; }
tr:last-child td { border-bottom: 0; }
td a { text-decoration: none; font-weight: 600; }
td a:hover { text-decoration: underline; }
.table-wrap { overflow-x: auto; border-radius: var(--radius); }

/* ---------- Sequence editor ---------- */
.seq { display: flex; flex-direction: column; gap: 0; }
.step { display: flex; gap: 12px; align-items: flex-start; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.step + .step { margin-top: 18px; position: relative; }
.step + .step::before { content: ""; position: absolute; left: 26px; top: -19px; height: 18px; border-left: 2px dotted var(--line); }
.step-num { width: 26px; height: 26px; border-radius: 50%; background: var(--accent-soft); color: #8a5c00; font-weight: 700; font-size: 13px; display: grid; place-items: center; flex: none; }
.step.wait .step-num { background: var(--surface-2); color: var(--ink-3); }
.step-body { flex: 1; display: grid; gap: 8px; }
.step-title { font-weight: 600; font-size: 14px; }
.branches { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.branch { border-radius: 9px; padding: 10px; background: var(--surface-2); display: grid; gap: 6px; font-size: 13px; font-weight: 600; }
.branch.yes { box-shadow: inset 3px 0 0 var(--ok); }
.branch.no { box-shadow: inset 3px 0 0 var(--warn); }
.step-tools { display: flex; gap: 2px; }
.add-steps { margin-top: 14px; display: flex; gap: 8px; flex-wrap: wrap; }
.days { width: 80px; }

/* ---------- Conversation ---------- */
.timeline { display: flex; flex-direction: column; gap: 10px; }
.ev { display: flex; gap: 10px; align-items: baseline; font-size: 14px; }
.ev time { color: var(--ink-3); font-size: 12px; width: 120px; flex: none; }
.ev.out .dot { color: var(--info); } .ev.in .dot { color: var(--warn); }
.continue { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.continue button { text-align: left; display: grid; gap: 3px; padding: 12px 14px; }
.continue button span { font-weight: 400; color: var(--ink-3); font-size: 13px; white-space: normal; }

.preview { border: 1px solid var(--line); border-radius: var(--radius); background: #fff; overflow: hidden; }
.preview .subj { padding: 10px 14px; border-bottom: 1px solid #eee; font-weight: 600; color: #222; background: #fafafa; font-size: 14px; }
.preview iframe { width: 100%; height: 300px; border: 0; display: block; }
.editor { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }

/* ---------- Conversación ---------- */
.thread { display: flex; flex-direction: column; gap: 10px; }
.msg { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; max-width: 86%; box-shadow: var(--shadow); }
.msg.in { align-self: flex-start; border-left: 3px solid var(--warn); }
.msg.out { align-self: flex-end; background: var(--info-soft); border-color: transparent; }
.msg.test { background: var(--accent-soft); }
.msg-head { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.msg-head time { color: var(--ink-3); font-size: 12px; }
.msg-subj { font-size: 13px; color: var(--ink-2); margin: 2px 0 8px; }
.msg-body { white-space: pre-wrap; font-size: 14px; line-height: 1.5; overflow-wrap: anywhere; }
.msg-body.quoted { color: var(--ink-3); font-size: 13px; border-left: 2px solid var(--line); padding-left: 10px; margin-top: 6px; }
.msg details summary { font-size: 12px; font-weight: 500; color: var(--ink-3); margin-top: 8px; }
.hot-card { border-left: 3px solid var(--warn); }
@media (max-width: 760px) { .msg { max-width: 100%; } }

/* ---------- Ajustes ---------- */
.form-3 { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 14px; }
.testmode { background: var(--accent-soft); border-radius: 9px; padding: 10px 12px; display: grid; gap: 10px; }
.testmode.live { background: var(--ok-soft); }
@media (max-width: 760px) { .form-3 { grid-template-columns: 1fr; } }

/* ---------- Contactos ---------- */
.search-row { display: flex; gap: 10px; }
.search-row input { flex: 1; font-size: 16px; padding: 10px 14px; }
.contact-item { align-items: flex-start; }
.contact-item > input[type=checkbox] { width: 18px; height: 18px; margin-top: 3px; flex: none; accent-color: var(--accent); }
.contact-item.unsub { opacity: .6; }
.memberships { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; padding: 3px 4px 3px 10px; border-radius: 99px; background: var(--surface-2); text-decoration: none; color: var(--ink-2); border: 1px solid var(--line); }
.chip:hover { border-color: var(--accent); color: var(--ink); }
.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.bulk { position: sticky; top: 8px; z-index: 3; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; background: var(--ink); color: var(--bg); padding: 8px 12px; border-radius: 12px; margin-bottom: 12px; box-shadow: 0 4px 14px rgba(0,0,0,.15); }
.bulk span { font-weight: 600; font-size: 14px; margin-right: 6px; }
.bulk .btn.ghost { color: var(--bg); }
@media (max-width: 760px) { .search-row { flex-direction: column; } .search-row select { width: 100% !important; } }

/* ---------- Mail editor ---------- */
.mail-editor { border: 1px solid var(--line); border-radius: var(--radius); overflow: visible; background: var(--surface); }
.toolbar { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; padding: 6px 8px; border-bottom: 1px solid var(--line); background: var(--surface-2); border-radius: var(--radius) var(--radius) 0 0; position: sticky; top: 0; z-index: 2; }
.toolbar select { width: auto; padding: 4px 8px; font-size: 13px; }
.tb { font: inherit; font-size: 13px; min-width: 30px; height: 30px; padding: 0 8px; border: 1px solid transparent; background: transparent; color: var(--ink); border-radius: 7px; cursor: pointer; }
.tb:hover { background: var(--surface); border-color: var(--line); }
.tb.on { background: var(--accent-soft); border-color: var(--accent); }
.tb-sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
.tb-color { position: relative; display: inline-block; }
.tb-pop { position: absolute; top: 34px; left: 0; display: grid; grid-template-columns: repeat(7, 22px); gap: 5px; padding: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 6px 20px rgba(0,0,0,.12); z-index: 5; }
.tb-pop button { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line); cursor: pointer; padding: 0; }
.wysiwyg { min-height: 280px; padding: 16px 18px; outline: none; font-family: -apple-system, "Segoe UI", Helvetica, Arial, sans-serif; font-size: 15px; line-height: 1.5; background: #fff; color: #222; border-radius: 0 0 var(--radius) var(--radius); }
.wysiwyg p { margin: 0 0 1em; }
.wysiwyg a { color: #2c5f99; }
.mail-editor .code { border: 0; border-radius: 0 0 var(--radius) var(--radius); min-height: 280px; }
.preview iframe { height: 360px; }

/* ---------- Login & toast ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 28px; display: grid; gap: 14px; box-shadow: var(--shadow); }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none; transition: .2s; z-index: 10; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--warn); color: #fff; }

details summary { cursor: pointer; font-weight: 600; font-size: 14px; color: var(--ink-2); }
details[open] summary { margin-bottom: 10px; }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .side { position: sticky; top: 0; z-index: 5; height: auto; flex-direction: row; align-items: center; padding: 10px 12px; gap: 8px; border-right: 0; border-bottom: 1px solid var(--line); overflow-x: auto; }
  .side .brand span:not(.logo), .brand { font-size: 0; padding: 0; }
  .side nav { flex-direction: row; }
  .side nav a { padding: 7px 10px; font-size: 14px; white-space: nowrap; }
  .logout { margin: 0 0 0 auto; }
  .main { padding: 20px 16px 60px; }
  h1 { font-size: 22px; }
  .item { flex-direction: column; align-items: stretch; }
  .item .actions { justify-content: flex-start; }
  .form-2, .branches, .editor { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
}
