:root {
  --primary: #991313; --primary-dark: #7a0f0f; --primary-soft: #f6e5e5;
  --bg: #f5f2e9; --surface: #ffffff; --ink: #221f1c; --muted: #6b665e; --line: #e4dfd2;
  --gold: #b8893b; --side: #221f1c; --side-ink: #d9d3c6;
  --ok: #2e7d4f; --warn: #b7791f; --bad: #b42318;
  --s-nuevo: #2563eb; --s-contactado: #d97706; --s-cotizado: #7c3aed; --s-negociacion: #ea580c; --s-ganado: #2e7d4f; --s-perdido: #7b7870;
  --r: 8px; --shadow: 0 1px 2px rgba(34, 31, 28, .06), 0 4px 14px rgba(34, 31, 28, .06);
  font-family: Inter, system-ui, sans-serif; color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); font-size: 14px; line-height: 1.45; }
h1, h2, h3 { font-family: Manrope, Inter, sans-serif; margin: 0; letter-spacing: -.01em; }
h1 { font-size: 22px; } h2 { font-size: 16px; } h3 { font-size: 14px; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }
.hidden { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 12px; } .right { text-align: right; } .nowrap { white-space: nowrap; }

/* Layout */
.app { display: grid; grid-template-columns: 230px 1fr; min-height: 100vh; }
.side { background: var(--side); color: var(--side-ink); padding: 18px 12px; display: flex; flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100vh; }
.side .brand { padding: 4px 10px 16px; }
.side .brand img { width: 150px; filter: brightness(0) invert(1); display: block; }
.side .brand span { display: block; font-size: 11px; letter-spacing: .14em; text-transform: uppercase; color: var(--gold); margin-top: 6px; }
.nav a { display: flex; gap: 10px; align-items: center; padding: 9px 12px; border-radius: var(--r); color: var(--side-ink); text-decoration: none; font-weight: 500; }
.nav a:hover { background: rgba(255, 255, 255, .07); }
.nav a.active { background: var(--primary); color: #fff; }
.nav a .ico { width: 18px; text-align: center; }
.side .who { margin-top: auto; padding: 10px; border-top: 1px solid rgba(255, 255, 255, .1); font-size: 12px; }
.side .who b { display: block; color: #fff; font-size: 13px; }
.side .who button { margin-top: 8px; }
.main { padding: 24px 28px 48px; min-width: 0; }
.head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.head .grow { flex: 1; }

/* Controles */
.btn { border: 1px solid var(--line); background: var(--surface); padding: 8px 14px; border-radius: var(--r); cursor: pointer; font-weight: 600; }
.btn:hover { border-color: #cfc8b6; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; border-color: transparent; color: inherit; }
.side .btn { background: transparent; border-color: rgba(255, 255, 255, .25); color: #fff; padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.input, select.input, textarea.input { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--r); background: #fff; }
.input:focus, .btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > label { font-weight: 600; font-size: 12px; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar .input { width: auto; min-width: 140px; }
.tabs { display: inline-flex; background: #ebe6d8; border-radius: var(--r); padding: 3px; }
.tabs button { border: 0; background: transparent; padding: 6px 14px; border-radius: 6px; cursor: pointer; font-weight: 600; color: var(--muted); }
.tabs button.on { background: #fff; color: var(--ink); box-shadow: var(--shadow); }

/* Tarjetas y tablas */
.card { background: var(--surface); border-radius: var(--r); box-shadow: var(--shadow); padding: 16px; }
.table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--r); overflow: hidden; box-shadow: var(--shadow); }
.table th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 10px 12px; background: #faf8f2; border-bottom: 1px solid var(--line); }
.table td { padding: 10px 12px; border-bottom: 1px solid #f0ece0; vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr.click { cursor: pointer; } .table tbody tr.click:hover { background: #fcfaf5; }
.empty { padding: 36px; text-align: center; color: var(--muted); background: var(--surface); border-radius: var(--r); }
.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 11px; font-weight: 700; background: #eee; }
.badge.nuevo { background: #dbe7fd; color: var(--s-nuevo); } .badge.contactado { background: #fdebc8; color: var(--s-contactado); }
.badge.cotizado { background: #e9defc; color: var(--s-cotizado); } .badge.negociacion { background: #fde1d1; color: var(--s-negociacion); }
.badge.ganado { background: #d6efdf; color: var(--s-ganado); } .badge.perdido { background: #e8e6e0; color: var(--s-perdido); }
.badge.cliente { background: #d6efdf; color: var(--ok); } .badge.prospecto { background: var(--primary-soft); color: var(--primary); }
.badge.aceptada { background: #d6efdf; color: var(--ok); } .badge.enviada { background: #dbe7fd; color: var(--s-nuevo); }
.badge.rechazada, .badge.vencida { background: #fbdcd9; color: var(--bad); } .badge.borrador { background: #e8e6e0; color: var(--muted); }
.tag { display: inline-block; background: #efe9d8; padding: 1px 8px; border-radius: 99px; font-size: 11px; margin-right: 4px; }

/* Kanban */
.kanban { display: grid; grid-template-columns: repeat(6, minmax(180px, 1fr)); gap: 12px; overflow-x: auto; padding-bottom: 8px; align-items: start; }
.col { background: #ece7d9; border-radius: var(--r); padding: 10px; min-height: 160px; border-top: 3px solid var(--c, #999); }
.col.over { outline: 2px dashed var(--primary); }
.col header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.col header b { font-family: Manrope, sans-serif; } .col header span { font-size: 12px; color: var(--muted); }
.opp { background: #fff; border-radius: var(--r); padding: 10px 12px; margin-bottom: 8px; box-shadow: var(--shadow); cursor: grab; border-left: 3px solid transparent; }
.opp.big { border-left-color: var(--primary); }
.opp .t { font-weight: 700; } .opp .c { color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.opp .row { display: flex; justify-content: space-between; align-items: center; font-size: 12px; }
.opp .amt { font-weight: 800; font-family: Manrope, sans-serif; }
.opp.dragging { opacity: .4; }

/* Calendario */
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 1px; background: var(--line); border-radius: var(--r); overflow: hidden; box-shadow: var(--shadow); }
.cal .dow { background: #faf8f2; padding: 6px; font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--muted); }
.cal .day { background: #fff; min-height: 96px; padding: 4px; }
.cal .day.out { background: #faf8f2; color: #b4aea0; } .cal .day.today { outline: 2px solid var(--primary); outline-offset: -2px; }
.cal .day .n { font-size: 11px; font-weight: 700; }
.cal .ev { display: block; width: 100%; text-align: left; border: 0; border-left: 3px solid var(--c, #999); background: #f7f4ec; font-size: 11px; padding: 2px 5px; margin-top: 3px; border-radius: 3px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* KPI y gráficas */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: #fff; border-radius: var(--r); box-shadow: var(--shadow); padding: 14px 16px; border-top: 3px solid var(--gold); }
.kpi .v { font-family: Manrope, sans-serif; font-size: 24px; font-weight: 800; } .kpi .l { color: var(--muted); font-size: 12px; }
.bars { display: flex; flex-direction: column; gap: 8px; }
.bar { display: grid; grid-template-columns: 110px 1fr 120px; gap: 10px; align-items: center; font-size: 12px; }
.bar .track { background: #efe9d8; border-radius: 99px; height: 14px; overflow: hidden; } .bar .fill { height: 100%; background: var(--c, var(--primary)); border-radius: 99px; }
.cols2 { display: grid; grid-template-columns: 1.3fr 1fr; gap: 16px; } .cols2 > .card + .card { margin: 0; }
.stack { display: flex; flex-direction: column; gap: 16px; }

/* Modal, toast, login */
.overlay { position: fixed; inset: 0; background: rgba(34, 31, 28, .55); display: flex; align-items: flex-start; justify-content: center; padding: 5vh 16px; overflow-y: auto; z-index: 50; }
.modal { background: #fff; border-radius: 12px; width: min(680px, 100%); padding: 22px; box-shadow: 0 20px 60px rgba(0, 0, 0, .35); }
.modal.wide { width: min(980px, 100%); }
.modal h2 { margin-bottom: 14px; } .modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 80; }
.toast { background: var(--ink); color: #fff; padding: 10px 16px; border-radius: var(--r); box-shadow: var(--shadow); } .toast.err { background: var(--bad); }
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: min(380px, 100%); padding: 28px; border-top: 4px solid var(--primary); }
.login img { width: 190px; display: block; margin: 0 auto 18px; }
.err-msg { color: var(--bad); font-size: 13px; margin-bottom: 10px; }
.timeline { display: flex; flex-direction: column; gap: 10px; }
.tl { border-left: 3px solid var(--line); padding: 2px 0 2px 12px; } .tl b { text-transform: capitalize; }
.soon { text-align: center; padding: 56px 24px; } .soon h2 { margin-bottom: 8px; }

/* Cotización (pantalla y PDF) */
.qdoc { background: #fff; padding: 28px; border: 1px solid var(--line); }
.qdoc .qh { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 3px solid var(--primary); padding-bottom: 12px; margin-bottom: 14px; }
.qdoc img.logo { width: 170px; }
.tot { margin-left: auto; width: 280px; margin-top: 12px; }
.tot div { display: flex; justify-content: space-between; padding: 3px 0; } .tot .grand { font-weight: 800; font-size: 16px; border-top: 2px solid var(--ink); margin-top: 4px; padding-top: 6px; }
.qdoc .terms { margin-top: 18px; font-size: 11px; color: var(--muted); white-space: pre-wrap; }

/* Bandeja WhatsApp */
.wa { display: grid; grid-template-columns: 320px minmax(0, 1fr) 290px; gap: 12px; height: calc(100vh - 215px); min-height: 520px; }
.wa-col, .wa-chat { background: var(--surface); border-radius: var(--r); box-shadow: var(--shadow); display: flex; flex-direction: column; min-height: 0; }
.wa-side { display: flex; flex-direction: column; gap: 12px; overflow-y: auto; } .wa-side .card { padding: 14px; }
.wa-filters { display: flex; flex-direction: column; gap: 6px; padding: 10px; border-bottom: 1px solid var(--line); }
.wa-list { overflow-y: auto; flex: 1; }
.wa-item { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid #f0ece0; background: transparent; padding: 10px 12px; cursor: pointer; }
.wa-item:hover { background: #fcfaf5; } .wa-item.on { background: var(--primary-soft); }
.wa-top, .wa-prev { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.wa-prev span:first-child { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wa-badge { background: var(--primary); color: #fff; border-radius: 99px; padding: 0 7px; font-size: 11px; font-weight: 700; min-width: 20px; text-align: center; line-height: 18px; }
.wa-badge[hidden] { display: none; }
.wa-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.wa-log { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; background: #efeadc; }
.wa-msg { max-width: 72%; padding: 8px 12px; border-radius: 12px; background: #fff; box-shadow: var(--shadow); white-space: pre-wrap; overflow-wrap: anywhere; }
.wa-msg.out { align-self: flex-end; background: #f3dcd9; } .wa-msg.in { align-self: flex-start; }
.wa-msg img { max-width: 100%; border-radius: 8px; display: block; margin-bottom: 4px; }
.wa-meta { text-align: right; margin-top: 2px; } .wa-read { color: var(--s-nuevo); font-weight: 700; }
.wa-compose { padding: 10px 12px; border-top: 1px solid var(--line); display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: end; }
.wa-tools { grid-column: 1 / -1; display: flex; gap: 6px; flex-wrap: wrap; } .wa-tools select { width: auto; }
.wa-empty { margin: auto; color: var(--muted); }
.wa-opp { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; } .wa-opp select { width: 130px; }
.wa-actions { display: flex; gap: 8px; flex-wrap: wrap; }
@media (max-width: 1100px) { .wa { grid-template-columns: 280px 1fr; } .wa-side { display: none; } }

/* Asistente de ayuda */
.help-fab { position: fixed; right: 18px; bottom: 18px; width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--primary); color: #fff; font: 800 22px Manrope, sans-serif; cursor: pointer; box-shadow: 0 6px 20px rgba(0, 0, 0, .3); z-index: 60; }
.help-fab:hover { background: var(--primary-dark); }
.help-panel { position: fixed; right: 18px; bottom: 78px; width: min(360px, calc(100vw - 36px)); height: min(480px, 70vh); background: #fff; border-radius: 12px; box-shadow: 0 16px 48px rgba(0, 0, 0, .35); display: flex; flex-direction: column; z-index: 60; overflow: hidden; }
.help-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; background: var(--side); color: #fff; } .help-head .btn { color: #fff; }
.help-log { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; background: var(--bg); }
.help-msg { padding: 8px 12px; border-radius: 12px; max-width: 90%; white-space: pre-wrap; overflow-wrap: anywhere; background: #fff; box-shadow: var(--shadow); }
.help-msg.me { align-self: flex-end; background: var(--primary-soft); }
.help-form { display: flex; gap: 6px; padding: 10px; border-top: 1px solid var(--line); }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; } .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; }
  .side .who { margin: 0; border: 0; } .main { padding: 16px; } .cols2, .grid2 { grid-template-columns: 1fr; }
}
@media print {
  body { background: #fff; } .app > .side, .head, .toolbar, .overlay .actions, .toasts { display: none !important; }
  .app { display: block; } .overlay { position: static; background: none; padding: 0; } .modal { box-shadow: none; width: 100%; padding: 0; }
  .main { padding: 0; } .qdoc { border: 0; padding: 0; }
}
