:root {
  /* Paleta de marca */
  --red: #e63329;
  --red-dark: #c22219;
  --blue: #1a2b5e;
  --blue-mid: #2f8fdb;
  --blue-light: #73b6e1;
  --blue-dark: #014880;
  --blue-deep: #13296b;

  /* Tokens de UI derivados de la marca */
  --bg: #eef2f8;
  --panel: #ffffff;
  --border: #e2e8f2;
  --border-strong: #cbd5e6;
  --text: #17233f;
  --muted: #737f95;
  --primary: var(--blue-mid);
  --primary-dark: var(--blue-dark);
  --green: #159a55;
  --amber: #d98a00;
  --row-hover: #f3f8fd;
  --header-bg: var(--blue-deep);

  --shadow-sm: 0 1px 2px rgba(19, 41, 107, .06), 0 1px 3px rgba(19, 41, 107, .04);
  --shadow-md: 0 4px 16px rgba(19, 41, 107, .08);
  --shadow-lg: 0 12px 40px rgba(19, 41, 107, .18);
  --radius: 10px;

  font-family: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji",
    "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
  font-size: 14px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: 1px solid var(--border-strong);
  background: #fff;
  border-radius: 7px;
  padding: 7px 13px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  transition: background .12s, border-color .12s, box-shadow .12s, transform .04s;
}
button:hover { background: #f5f8fc; border-color: var(--blue-light); }
button:active { transform: translateY(1px); }
button.primary { background: var(--primary); color: #fff; border-color: transparent; box-shadow: 0 1px 2px rgba(47,143,219,.4); }
button.primary:hover { background: var(--blue-dark); border-color: transparent; }
button.danger { background: var(--red); color: #fff; border-color: transparent; }
button.danger:hover { background: var(--red-dark); }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover { background: rgba(0,0,0,.05); }
button.small { padding: 4px 9px; font-size: 12px; }

input, select, textarea {
  font-family: inherit;
  font-size: 13px;
  padding: 7px 9px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  transition: border-color .12s, box-shadow .12s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(47,143,219,.18);
}
label { font-weight: 500; }

.hidden { display: none !important; }

/* ---------- Login ---------- */
#login-screen {
  display: flex; align-items: center; justify-content: center;
  height: 100vh;
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(115,182,225,.35), transparent),
    linear-gradient(135deg, var(--blue-deep) 0%, var(--blue) 45%, var(--blue-dark) 100%);
}
.login-card {
  background: #fff; padding: 36px 34px; border-radius: 16px; width: 360px;
  box-shadow: var(--shadow-lg);
}
.login-card .logo { display:flex; align-items:center; justify-content:center; margin-bottom: 12px; }
.login-card .login-logo { height: 66px; width: auto; max-width: 100%; }
.login-card .app-title { text-align:center; font-size: 22px; font-weight: 800; letter-spacing: 3px;
  color: var(--blue); margin: 2px 0 2px; }
.login-card h1 { margin: 0; font-size: 21px; letter-spacing:-.2px; }
.login-card p { margin: 2px 0 22px; color: var(--muted); font-size: 13px; text-align:center; }
.login-card label { display: block; font-size: 12px; color: var(--muted); margin: 14px 0 5px; font-weight:600; }
.login-card input { width: 100%; }
.login-card button { width: 100%; margin-top: 22px; padding: 11px; font-size: 15px; }
.login-error { color: var(--red); font-size: 13px; margin-top: 12px; min-height: 16px; }
.login-hint { margin-top: 18px; font-size: 11px; color: var(--muted); text-align: center; }

/* ---------- App shell ---------- */
#app { display: none; flex-direction: column; height: 100vh; }
.topbar {
  background: linear-gradient(135deg, var(--blue-deep) 0%, var(--blue) 45%, var(--blue-dark) 100%);
  color: #fff; display: flex; align-items: center;
  padding: 0 18px; height: 60px; gap: 20px; flex-shrink: 0;
  box-shadow: var(--shadow-md); z-index: 10;
}
.topbar .brand { display:flex; align-items:center; gap:9px; }
.topbar .brand-logo { height: 42px; width: auto; display:block; }
.tabs { display: flex; gap: 2px; margin-left: 6px; }
.tab {
  background: transparent; border: none; color: #b9c4dd; padding: 8px 15px;
  border-radius: 7px; font-size: 14px; font-weight: 500; position: relative;
}
.tab:hover { background: rgba(255,255,255,.09); color: #fff; }
.tab.active { color: #fff; font-weight: 600; }
.tab.active::after {
  content: ""; position: absolute; left: 15px; right: 15px; bottom: -9px; height: 3px;
  background: var(--red); border-radius: 3px 3px 0 0;
}
.tab-badge {
  display:inline-block; background: var(--red); color:#fff; font-size:11px; font-weight:700;
  border-radius: 999px; padding: 1px 7px; margin-left: 3px; vertical-align: middle;
}
.topbar .spacer { flex: 1; }
.topbar .user { font-size: 13px; color: #b9c4dd; }
.topbar .user b { color: #fff; }

.view { flex: 1; overflow: auto; padding: 22px 24px; }
.view h2 { font-size: 20px; letter-spacing: -.3px; margin: 0 0 18px; }

.toolbar { display: flex; gap: 9px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar .grow { flex: 1; }
.toolbar input[type=search] { width: 280px; }

/* ---------- Tabla tipo planilla ---------- */
.grid-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow-sm); }
table.grid { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; }
table.grid thead th {
  position: sticky; top: 0; background: #f7f9fd; text-align: left;
  padding: 11px 12px; border-bottom: 1px solid var(--border-strong);
  font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: #5a6884; font-weight: 700; z-index: 1;
}
table.grid td { padding: 5px 12px; border-bottom: 1px solid var(--border); color: var(--text); }
table.grid tbody tr { transition: background .1s; }
table.grid tbody tr:hover { background: var(--row-hover); cursor: pointer; }
table.grid tbody tr:last-child td { border-bottom: none; }
table.grid td.editable { padding: 3px 6px; }
table.grid td.editable input, table.grid td.editable select {
  border: 1px solid transparent; background: transparent; width: 100%; padding: 5px 7px; border-radius: 6px; box-shadow:none;
}
table.grid td.editable input:hover, table.grid td.editable select:hover { border-color: var(--border-strong); background:#fff; }
table.grid td.editable input:focus, table.grid td.editable select:focus { background:#fff; border-color: var(--primary); }
table.grid td.num { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
table.grid select[data-field="stage"] { font-weight: 600; }
a.client-link { color: var(--blue-dark); font-weight: 600; cursor: pointer; text-decoration: none; }
a.client-link:hover { text-decoration: underline; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.stage-pendiente { background: #eef1f6; color: #5b6678; }
.stage-contactado { background: #e3f0fb; color: var(--blue-dark); }
.stage-interesado { background: #fff2d6; color: #a86e00; }
.stage-cotizado { background: #eae4ff; color: #6b45d9; }
.stage-en-negociacion { background: #ffe3d1; color: #c2510a; }
.stage-ganado { background: #d8f3e4; color: var(--green); }
.stage-perdido { background: #fce0de; color: var(--red-dark); }
.stage-no-interesado { background: #e8ebf0; color: #64748b; }

/* ---------- Panel de detalle (drawer) ---------- */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(19,41,107,.32); backdrop-filter: blur(1px); display: none; z-index: 50; }
.drawer-backdrop.open { display: block; }
.drawer {
  position: fixed; top: 0; right: 0; height: 100vh; width: 560px; max-width: 96vw;
  background: var(--panel); box-shadow: var(--shadow-lg); z-index: 51;
  transform: translateX(100%); transition: transform .22s cubic-bezier(.4,0,.2,1); display: flex; flex-direction: column;
}
.drawer.open { transform: translateX(0); }
.drawer header {
  padding: 18px 22px; border-bottom: 1px solid var(--border); display:flex; align-items:flex-start; gap:12px;
  background: linear-gradient(180deg, #f7f9fd, #fff);
}
.drawer header h2 { margin: 0 0 3px; font-size: 19px; letter-spacing:-.3px; }
.drawer header .sub { color: var(--muted); font-size: 13px; }
.drawer .body { flex: 1; overflow: auto; padding: 22px; }
.drawer .close { margin-left: auto; font-size: 16px; color: var(--muted); }

.field-row { display: flex; gap: 14px; margin-bottom: 12px; }
.field { flex: 1; }
.field label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 4px; text-transform: uppercase; letter-spacing:.4px; font-weight:700;}
.field input, .field select, .field textarea { width: 100%; }

.section-title { font-size: 12px; font-weight: 800; margin: 26px 0 12px; text-transform: uppercase; letter-spacing:.6px; color:#5a6884; display:flex; align-items:center; gap:8px;}
.section-title::after { content:""; flex:1; height:1px; background: var(--border); }

.action-buttons { display:flex; gap:10px; }
button.big { flex:1; padding:13px; font-size:14px; font-weight:600;
  display:inline-flex; align-items:center; justify-content:center; gap:8px; }
button.danger.big { box-shadow: 0 2px 8px rgba(230,51,41,.35); }
.channel-grid { display:flex; flex-wrap:wrap; gap:8px; }
.channel-grid .channel-pick { flex:1 1 45%; padding:14px 10px; font-size:14px; font-weight:500;
  display:inline-flex; align-items:center; justify-content:center; gap:8px; }
.channel-grid .channel-pick:hover { border-color: var(--primary); background:#f0f6ff; }

/* Íconos SVG en línea */
.ico { flex-shrink:0; }
h4 .ico { vertical-align: -3px; margin-right: 4px; }

/* Zona de borrado (solo admin) */
.danger-zone { margin-top: 26px; padding-top: 16px; border-top: 1px solid var(--border); }
.btn-delete {
  border: 1px solid #e9b3b0; color: var(--red-dark); background: #fff;
  font-weight: 600;
}
.btn-delete:hover { background: var(--red); color: #fff; border-color: var(--red); }

.quick-actions { display: flex; flex-wrap: wrap; gap: 7px; }
.quick-actions button { font-size: 12px; background:#f5f8fc; }
.quick-actions button:hover { background:#fff; border-color: var(--primary); color: var(--blue-dark); }

.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { padding: 12px 0 12px 20px; border-left: 2px solid var(--border); position: relative; }
.timeline li:last-child { border-left-color: transparent; }
.timeline li::before {
  content: ""; position: absolute; left: -6px; top: 16px; width: 10px; height: 10px;
  border-radius: 50%; background: var(--primary); box-shadow: 0 0 0 3px #fff, 0 0 0 4px var(--border);
}
.timeline .ev-type { font-weight: 600; font-size: 13px; }
.timeline .ev-meta { font-size: 11px; color: var(--muted); margin-top:1px; }
.timeline .ev-body { font-size: 13px; margin-top: 5px; white-space: pre-wrap; background:#f7f9fd; padding:8px 10px; border-radius:8px; border:1px solid var(--border); }

.playbook { background: linear-gradient(180deg, #f0f7fe, #f7fbff); border: 1px solid #d4e6f8; border-radius: 12px; padding: 16px 18px; margin-top:18px; }
.playbook h4 { margin: 0 0 8px; color: var(--blue-dark); font-size:14px; }
.playbook ol { margin: 0 0 10px 18px; padding: 0; }
.playbook li { margin-bottom: 5px; font-size: 13px; }
.playbook .svc { font-size: 12px; padding: 7px 0; border-top: 1px dashed #cfe0ff; }
.playbook .svc b { display:block; margin-bottom:2px; }

/* ---------- Formulario ramificado ---------- */
.form-q { margin-bottom: 11px; }
.form-q > label { display:block; font-weight:600; font-size:13px; margin-bottom:5px; }
.yesno { display: flex; gap: 8px; }
.yesno button { flex: 1; padding: 9px; }
.yesno button.sel-si { background:#d8f3e4; color: var(--green); border-color:#a9e3c2; box-shadow:none; font-weight:700; }
.yesno button.sel-no { background:#fce0de; color: var(--red-dark); border-color:#f3b7b3; box-shadow:none; font-weight:700; }

/* ---------- Métricas ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; margin-bottom: 20px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 16px 18px; box-shadow: var(--shadow-sm); position:relative; overflow:hidden; }
.card::before { content:""; position:absolute; left:0; top:0; bottom:0; width:4px; background: var(--primary); }
.card.accent-green::before { background: var(--green); }
.card.accent-red::before { background: var(--red); }
.card .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing:.5px; font-weight:700; }
.card .v { font-size: 28px; font-weight: 800; margin-top: 6px; letter-spacing:-.5px; }

.panel-block { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 18px 20px; margin-bottom: 18px; box-shadow: var(--shadow-sm); }
.panel-block h3 { margin: 0 0 16px; font-size: 15px; letter-spacing:-.2px; }

.funnel-bar { display:flex; align-items:center; gap:12px; margin-bottom:9px; }
.funnel-bar .lbl { width: 100px; font-size: 13px; font-weight:500; }
.funnel-bar .track { flex:1; background:#eef2f8; border-radius:6px; height: 28px; overflow:hidden; }
.funnel-bar .fill { height:100%; border-radius:6px; display:flex; align-items:center; padding-left:10px; color:#fff; font-size:12px; font-weight:700; min-width: 2px; transition: width .4s ease;
  background: linear-gradient(90deg, var(--blue-mid), var(--blue-light)); }
.funnel-bar .n { width: 40px; text-align:right; font-variant-numeric: tabular-nums; font-weight:600; }

.bars { display:flex; align-items:flex-end; gap:8px; height:150px; padding: 18px 0 22px; justify-content:flex-start; }
.bars .b { flex: 0 0 38px; max-width: 52px; background: linear-gradient(180deg, var(--blue-mid), var(--blue-dark)); border-radius: 5px 5px 0 0; position: relative; min-height: 3px; transition: height .3s; }
.bars .b:hover { filter: brightness(1.1); }
.bars .b span { position:absolute; bottom:-20px; left:-4px; right:-4px; text-align:center; font-size:10px; color:var(--muted); }
.bars .b em { position:absolute; top:-17px; left:0; right:0; text-align:center; font-size:11px; font-style:normal; color:var(--text); font-weight:600;}

.mtable { width:100%; border-collapse: collapse; font-size: 13px; }
.mtable th { text-align:left; padding:9px 12px; border-bottom: 2px solid var(--border-strong); font-size:11px; text-transform:uppercase; letter-spacing:.4px; color:#5a6884; }
.mtable td { text-align:left; padding:9px 12px; border-bottom: 1px solid var(--border); }
.mtable tr:last-child td { border-bottom:none; }
.mtable td.num { text-align:right; font-variant-numeric: tabular-nums; font-weight:600; }

/* ---------- Modales ---------- */
.modal-backdrop { position: fixed; inset:0; background: rgba(19,41,107,.4); display:none; align-items:center; justify-content:center; z-index:60; }
.modal-backdrop.open { display:flex; }
.modal { background:#fff; border-radius:14px; padding:24px; width: 440px; max-width: 94vw;
  box-shadow: var(--shadow-lg); max-height: 82vh; display:flex; flex-direction:column; overflow-y:auto; }
.modal h3 { margin:0 0 18px; font-size:17px; flex:0 0 auto; }
/* Zona con scroll para modales largos (ej: el formulario completo). */
.modal-scroll { overflow-y:auto; flex:1 1 auto; min-height:0; margin:0 -6px; padding:0 6px; }
.modal .actions { display:flex; justify-content:flex-end; gap:9px; margin-top:18px; flex:0 0 auto;
  border-top:1px solid var(--border); padding-top:16px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(10px);
  background: var(--blue); color: #fff; padding: 11px 20px; border-radius: 10px; font-size: 13px; font-weight:500;
  z-index: 100; opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; box-shadow: var(--shadow-md);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.date-late { color: var(--red); font-weight: 700; font-size: 12px; }
.date-today { background: var(--amber); color:#fff; font-weight:700; font-size:11px; padding:2px 8px; border-radius:999px; }
.quick-dates { display:flex; gap:7px; flex-wrap:wrap; align-items:center; margin: 10px 0 22px; }
.quick-dates button { font-size:12px; padding:6px 12px; }
td.editable input.overdue { color: var(--red); font-weight:600; }
td.editable input.due-today { color: var(--amber); font-weight:600; }

.empty { color: var(--muted); text-align:center; padding: 48px; font-size: 14px; }
