/* TT CRM — móvil primero */
:root {
  --navy: #1d2c3d;
  --navy-2: #2a3d52;
  --ink: #18212b;
  --ink-2: #4a5866;
  --ink-3: #7b8794;
  --paper: #f3f1ec;
  --card: #ffffff;
  --line: #e2ded5;
  --amber: #e9a23b;
  --amber-ink: #8a5300;
  --amber-bg: #fdf1dc;
  --wa: #1f9d55;
  --red: #c2412d;
  --radius: 14px;
  --tap: 46px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color: var(--ink);
  background: var(--paper);
  padding-bottom: calc(84px + var(--safe-b));
}
a { color: inherit; }
code { font-size: .9em; background: #eee9df; padding: 1px 5px; border-radius: 5px; }
[hidden] { display: none !important; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--navy); color: #fff;
  min-height: 56px;
}
.topbar-title { font-size: 18px; margin: 0; flex: 1; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-back, .topbar-act {
  display: grid; place-items: center; width: 40px; height: 40px; margin-left: -8px;
  color: #fff; border-radius: 10px; text-decoration: none;
}
.topbar-act { margin: 0 -6px 0 0; }
.topbar-act svg { width: 24px; height: 24px; }
.topbar-back:active, .topbar-act:active { background: rgba(255,255,255,.12); }
.wa-icon { color: #6ee7a0; }
.brandmark {
  width: 30px; height: 30px; border-radius: 8px; flex: none;
  background: url(icon.svg) center/cover no-repeat;
}
.brandmark.lg { width: 52px; height: 52px; border-radius: 13px; }

html, body { overflow-x: clip; }
.wrap { max-width: 720px; margin: 0 auto; padding: 14px 16px 24px; }

/* ---------- Tabbar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: 1fr 1fr 72px 1fr 1fr; align-items: end;
  background: rgba(255,255,255,.96); backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
  padding: 6px 6px calc(6px + var(--safe-b));
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 11px; color: var(--ink-3); text-decoration: none; padding: 4px 0; font-weight: 500;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.on { color: var(--navy); font-weight: 700; }
.tabbar .fab {
  justify-self: center; width: 56px; height: 56px; border-radius: 18px;
  background: var(--amber); color: var(--navy); margin-top: -22px;
  display: grid; place-items: center; box-shadow: 0 6px 16px rgba(233,162,59,.45);
}
.tabbar .fab svg { width: 28px; height: 28px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; top: calc(64px + env(safe-area-inset-top)); transform: translateX(-50%);
  z-index: 50; background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 999px;
  font-size: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.2); max-width: calc(100% - 32px);
  animation: toast 3.2s ease forwards;
}
@keyframes toast { 0%{opacity:0;transform:translate(-50%,-8px)} 8%,80%{opacity:1;transform:translate(-50%,0)} 100%{opacity:0;visibility:hidden} }

/* ---------- Cards / forms ---------- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; margin-bottom: 14px;
}
.card-h { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.card h2, details > summary h2 { font-size: 15px; margin: 0; letter-spacing: -.01em; }
label { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); margin-bottom: 10px; }
input, textarea, select {
  display: block; width: 100%; margin-top: 4px;
  font: inherit; font-size: 16px; /* evita zoom en iOS */
  color: var(--ink); background: #fbfaf7;
  border: 1px solid var(--line); border-radius: 10px;
  padding: 11px 12px; min-height: var(--tap);
}
textarea { resize: vertical; line-height: 1.4; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--amber); outline-offset: 0; border-color: var(--amber); background: #fff; }
input[type=radio], input[type=checkbox] { width: auto; min-height: 0; display: inline; margin: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 10px; }
.grid2 .full, .full { grid-column: 1 / -1; }
.span2 { grid-column: span 2; }
label.check { display: flex; gap: 8px; align-items: center; font-weight: 500; }
.muted { color: var(--ink-3); }
.small { font-size: 13px; }
.hint { font-size: 13px; color: var(--ink-2); background: var(--amber-bg); border-radius: 10px; padding: 10px 12px; margin: 0 0 14px; }
.err, .err-text { color: var(--red); font-size: 14px; }
.alert { background: #fbe9e5; color: var(--red); border-radius: 10px; padding: 10px 12px; margin-bottom: 12px; font-size: 14px; }
.internal input { background: #fffaf0; border-style: dashed; }
.internal { color: var(--amber-ink); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); padding: 0 16px; border-radius: 12px;
  font: inherit; font-size: 15px; font-weight: 650; text-decoration: none; cursor: pointer;
  background: #fff; color: var(--navy); border: 1px solid var(--line);
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--navy); color: #fff; border-color: var(--navy); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn.block { display: flex; width: 100%; }
.btn.wa { background: var(--wa); border-color: var(--wa); color: #fff; }
.btn.ghost.wa { background: transparent; color: var(--wa); border-color: #bfe5cf; }
.btn[aria-disabled=true] { opacity: .4; pointer-events: none; }
.btn .when-busy, .is-busy .when-idle { display: none; }
.is-busy .when-busy { display: inline; }
.is-busy { opacity: .75; pointer-events: none; }
.linkish { background: none; border: 0; padding: 8px 0; color: var(--ink-2); font: inherit; font-size: 14px; text-decoration: underline; cursor: pointer; }
.linkish.danger { color: var(--red); }
.icon-btn { width: 40px; height: 40px; border: 0; background: transparent; color: var(--ink-3); border-radius: 10px; display: grid; place-items: center; flex: none; }
.row-btns { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 10px; }
.row-btns > .btn { flex: 1 1 auto; }

/* ---------- Chips / segmented ---------- */
.chips { display: flex; gap: 6px; margin: 0 -16px 10px; padding: 0 16px; }
.scroll-x { overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.scroll-x::-webkit-scrollbar { display: none; }
.chip {
  position: relative;
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 13px; border-radius: 999px; background: #fff; border: 1px solid var(--line);
  font-size: 14px; font-weight: 600; color: var(--ink-2); text-decoration: none; margin: 0; cursor: pointer;
}
.chip b { font-size: 12px; background: var(--paper); border-radius: 999px; padding: 0 7px; }
.chip.on { background: var(--navy); color: #fff; border-color: var(--navy); }
.chip.on b { background: rgba(255,255,255,.18); }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.seg { display: flex; background: #e9e5dc; border-radius: 12px; padding: 3px; margin-bottom: 14px; }
.seg a, .seg label { flex: 1; text-align: center; padding: 8px 6px; border-radius: 9px; font-size: 14px; font-weight: 600; text-decoration: none; color: var(--ink-2); margin: 0; cursor: pointer; }
.seg a.on, .seg label:has(input:checked) { background: #fff; color: var(--navy); box-shadow: 0 1px 3px rgba(0,0,0,.1); }
.seg-input input { position: absolute; opacity: 0; pointer-events: none; }

/* ---------- Badges ---------- */
.badge { display: inline-block; font-size: 11.5px; font-weight: 700; padding: 2px 8px; border-radius: 6px; background: #eee; color: var(--ink-2); letter-spacing: .01em; }
.tipo-compra { background: #e4ecf5; color: #1f4b7a; }
.tipo-venta { background: #e8f3ea; color: #23683a; }
.st-nuevo { background: #e6e9ee; color: #3b4652; }
.st-falta_info { background: #fdeccc; color: #8a5300; }
.st-por_cotizar { background: #fbe0d8; color: #a33a1e; }
.st-cotizado { background: #e4ecf5; color: #1f4b7a; }
.st-ganado { background: #dff1e4; color: #1d6a37; }
.st-perdido { background: #eee; color: #7b8794; }
.q-borrador { background: #eceae4; color: #5a6470; }
.q-enviada { background: #e4ecf5; color: #1f4b7a; }
.q-aceptada { background: #dff1e4; color: #1d6a37; }
.q-rechazada { background: #f6e1dc; color: #a33a1e; }
.chip.on[class*="st-"], .chip.on[class*="q-"] { color: #fff; }
.chip.on.st-falta_info { background: #c97b00; border-color: #c97b00; }
.chip.on.st-por_cotizar { background: #c2412d; border-color: #c2412d; }
.chip.on.st-ganado, .chip.on.q-aceptada { background: #1d6a37; border-color: #1d6a37; }
.chip.on.st-perdido { background: #7b8794; border-color: #7b8794; }
.chip.on.q-rechazada { background: #a33a1e; border-color: #a33a1e; }

/* ---------- Search ---------- */
.search { position: relative; margin-bottom: 12px; }
.search svg { position: absolute; left: 12px; top: 50%; width: 20px; height: 20px; transform: translateY(-50%); color: var(--ink-3); }
.search input { margin: 0; padding-left: 40px; background: #fff; border-radius: 12px; }

/* ---------- Lists ---------- */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.lead-row {
  display: grid; gap: 3px; text-decoration: none;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px;
}
.lead-row:active { background: #faf8f3; }
.lead-top { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.lead-top time { margin-left: auto; font-size: 12px; color: var(--ink-3); }
.folio { font: 700 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--ink-3); letter-spacing: .02em; }
.lead-name { font-size: 16px; margin-top: 4px; }
.lead-sum { font-size: 14px; color: var(--ink-2); }
.lead-miss { font-size: 13px; color: var(--amber-ink); }
.lead-q { font-size: 12px; color: var(--ink-3); }
.amount { font-weight: 750; font-variant-numeric: tabular-nums; margin-top: 2px; }
.margin { font-weight: 600; color: var(--amber-ink); font-size: 12px; }
.empty { text-align: center; padding: 40px 20px; color: var(--ink-2); }

/* ---------- Lead detail ---------- */
.lead-head { margin-bottom: 14px; }
.lead-title { font-size: 22px; margin: 8px 0 2px; letter-spacing: -.02em; line-height: 1.2; }
.lead-head .lead-sum { margin: 0 0 4px; font-size: 15px; }
.lead-tel { margin: 0 0 10px; }
.lead-tel a { color: var(--navy); font-weight: 600; text-decoration: none; }
.estado-chips { margin-top: 6px; }
.mini-list { list-style: none; margin: 0; padding: 0; }
.mini-list a { display: flex; gap: 8px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); text-decoration: none; }
.mini-list li:first-child a { border-top: 0; padding-top: 2px; }
.mini-list strong { margin-left: auto; font-variant-numeric: tabular-nums; }
.mini-list time { font-size: 12px; color: var(--ink-3); min-width: 48px; text-align: right; }
.msg { border-top: 1px solid var(--line); padding: 10px 0; }
.msg:first-of-type { border-top: 0; padding-top: 0; }
.msg time { font-size: 12px; color: var(--ink-3); }
.msg pre { white-space: pre-wrap; word-break: break-word; font: 14px/1.45 inherit; font-family: inherit; margin: 4px 0 0; color: var(--ink-2); }
details > summary { list-style: none; cursor: pointer; display: flex; align-items: center; justify-content: space-between; }
details > summary::-webkit-details-marker { display: none; }
details.paste > summary::after { content: "+"; font-size: 22px; color: var(--ink-3); line-height: 1; }
details.paste[open] > summary::after { content: "–"; }
details.paste[open] > summary { margin-bottom: 10px; }
.danger-zone { text-align: center; margin: 10px 0 20px; }
.only-venta { display: none; }
form:has(input[name=tipo][value=venta]:checked) .only-venta { display: block; }
form:has(input[name=tipo][value=venta]:checked) .only-compra { display: none; }

/* ---------- Nuevo ---------- */
.paste-label { font-size: 15px; color: var(--ink); margin-bottom: 0; }
.paste textarea { min-height: 150px; }
.tel-inline { display: flex; align-items: center; gap: 8px; margin: 0; flex: 1 1 180px; }
.tel-inline span { font-size: 13px; }
.tel-inline input { margin: 0; min-height: 40px; padding: 8px 10px; }
.paste .btn.primary.block { margin-top: 12px; }
.paste .linkish { display: block; margin: 6px auto 0; }
.dup { border-color: var(--amber); background: var(--amber-bg); }
.dup p { margin: 0 0 10px; font-size: 14px; }
.dup .seg { margin: 0; background: rgba(0,0,0,.06); }
.followup { border-color: #cfe3d6; background: #f6fbf8; }
.miss { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; font-size: 13px; font-weight: 700; color: var(--amber-ink); }
.miss input { margin: 0; min-height: 38px; padding: 7px 10px; font-size: 14px; background: #fff; }
.followup textarea { background: #fff; }
.sticky-save {
  position: sticky; bottom: calc(76px + var(--safe-b)); z-index: 10;
  padding: 10px 0 0; background: linear-gradient(transparent, var(--paper) 30%);
}
.flash-field { animation: flashf 1.6s ease; }
@keyframes flashf { 0% { background: #fff2cc; } 100% { background: #fbfaf7; } }

/* ---------- Cotización: edición ---------- */
.context-line { margin: 0 0 12px; }
.item { padding-top: 10px; }
.item-h { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.cat-pick { margin: 0; flex: 1; min-height: 40px; padding: 8px 10px; font-size: 15px; background: var(--paper); }
.item-total { display: flex; justify-content: space-between; align-items: baseline; font-size: 14px; color: var(--ink-2); border-top: 1px dashed var(--line); padding-top: 8px; }
.item-total strong { font-size: 16px; color: var(--ink); font-variant-numeric: tabular-nums; }
[data-add-item] { margin-bottom: 14px; border-style: dashed; }
.totals-bar {
  position: sticky; bottom: 0; z-index: 15;
  margin: 0 -16px; padding: 12px 16px calc(12px + var(--safe-b));
  background: #fff; border-top: 1px solid var(--line); box-shadow: 0 -6px 18px rgba(0,0,0,.06);
}
body:has(.totals-bar) { padding-bottom: 0; }
.totals { display: grid; grid-template-columns: repeat(3, minmax(0, auto)); gap: 4px 12px; justify-content: space-between; }
.totals div { display: grid; font-size: 12px; color: var(--ink-3); min-width: 0; }
.totals div.internal { grid-column: 1 / -1; display: flex; gap: 8px; align-items: baseline; }
.totals div.internal b { font-size: 13px; }
.totals b { font-size: 15px; color: var(--ink); font-variant-numeric: tabular-nums; }
.totals .grand b { font-size: 18px; color: var(--navy); }
.totals .internal b { color: var(--amber-ink); }
.totals-bar .row-btns { margin-top: 10px; }
.totals-bar .row-btns .btn { flex: 1; }

/* ---------- Cotización: vista / PDF ---------- */
.doc-toolbar { margin-bottom: 14px; }
.doc-toolbar .lead-top { margin-bottom: 4px; }
.doc-toolbar .chips { margin-top: 12px; margin-bottom: 0; }
.internal-note { font-size: 13px; color: var(--amber-ink); background: var(--amber-bg); padding: 8px 12px; border-radius: 10px; margin: 12px 0 0; }
.doc-frame { overflow: hidden; border-radius: 6px; box-shadow: 0 2px 14px rgba(0,0,0,.08); background: #fff; }
.doc-actions-bottom { display: flex; justify-content: space-between; align-items: center; margin: 14px 0; }

.doc { background: #fff; color: #1b2430; padding: 22px 18px; font-size: 13px; line-height: 1.45; }
.doc-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 14px; padding-bottom: 14px; border-bottom: 3px solid var(--navy); }
.doc-brand { display: flex; align-items: center; gap: 10px; }
.doc-brand img { max-height: 54px; max-width: 200px; }
.doc-brand strong { display: block; font-size: 17px; color: var(--navy); letter-spacing: -.01em; }
.doc-brand span { font-size: 10px; letter-spacing: .12em; text-transform: uppercase; color: #8a5300; font-weight: 700; }
.doc-mark { width: 42px; height: 42px; border-radius: 10px; display: block; }
.doc-meta h1 { font-size: 20px; margin: 0 0 4px; color: var(--navy); text-transform: uppercase; letter-spacing: .04em; }
.doc-meta dl { display: grid; grid-template-columns: auto auto; gap: 1px 10px; margin: 0; font-size: 12px; }
.doc-meta dt { color: #6b7684; }
.doc-meta dd { margin: 0; font-weight: 600; }
.doc-client { margin: 16px 0; }
.doc h2 { font-size: 10.5px; letter-spacing: .12em; text-transform: uppercase; color: #6b7684; margin: 0 0 4px; }
.doc-client p { margin: 0; }
.doc-items { width: 100%; border-collapse: collapse; margin-top: 4px; }
.doc-items th { text-align: left; font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; color: #fff; background: var(--navy); padding: 7px 8px; }
.doc-items td { padding: 9px 8px; border-bottom: 1px solid #e6e2d9; vertical-align: top; }
.doc-items td span { color: #6b7684; font-size: 12px; }
.doc-items .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.doc-totals { margin: 10px 0 0 auto; width: min(100%, 280px); }
.doc-totals div { display: flex; justify-content: space-between; padding: 4px 8px; font-variant-numeric: tabular-nums; }
.doc-totals .grand { background: var(--amber-bg); border-top: 2px solid var(--amber); font-size: 15px; padding: 8px; margin-top: 4px; }
.doc-totals .grand b { color: var(--navy); }
.doc-terms { margin-top: 20px; padding: 12px; background: #f7f5f0; border-radius: 6px; }
.doc-terms p { margin: 0 0 4px; font-size: 12px; color: #4a5866; }
.doc-foot { margin-top: 22px; padding-top: 10px; border-top: 1px solid #e6e2d9; display: flex; flex-wrap: wrap; gap: 4px 16px; justify-content: center; font-size: 11px; color: #6b7684; }

/* Modo PDF: ancho carta fijo (lo aplica app.js al clonar) */
.pdf-render { position: fixed; left: -10000px; top: 0; width: 816px; }
.pdf-render .doc { padding: 48px 52px; font-size: 13.5px; min-height: 1056px; display: flex; flex-direction: column; }
.pdf-render .doc-foot { margin-top: auto; }
.pdf-render .doc-head { flex-wrap: nowrap; }

@media print {
  body { background: #fff; padding: 0; }
  .topbar, .tabbar, .doc-toolbar, .doc-actions-bottom, .toast { display: none !important; }
  .wrap { padding: 0; max-width: none; }
  .doc-frame { box-shadow: none; }
  .doc { padding: 0; }
}

/* ---------- Catálogo ---------- */
.prod { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.prod.inactive { opacity: .55; }
.prod summary { gap: 10px; }
.prod summary strong { font-size: 15px; }
.prod-prices { text-align: right; font-size: 14px; font-weight: 650; font-variant-numeric: tabular-nums; display: grid; }
.prod[open] summary { margin-bottom: 12px; }
.prod.new summary strong { color: var(--navy); }

/* ---------- Ajustes ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; }
.stats b { display: block; font-size: 24px; color: var(--navy); }
.stats span { font-size: 12px; color: var(--ink-3); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 8px; font-size: 14px; }
.kv dt { color: var(--ink-3); }
.kv dd { margin: 0; font-weight: 600; }

/* ---------- Login ---------- */
.login-body { padding: 0; min-height: 100vh; display: grid; place-items: center; background: var(--navy); }
.login { width: min(100%, 380px); padding: 24px 16px; }
.login-brand { display: flex; gap: 12px; align-items: center; color: #fff; margin-bottom: 18px; }
.login-brand strong { display: block; font-size: 20px; }
.login-brand span { font-size: 13px; color: #b6c2cf; }
.login .card { padding: 18px; }
.login .btn { margin-top: 6px; }

@media (min-width: 760px) {
  .chips { margin: 0 0 10px; padding: 0; flex-wrap: wrap; }
  .doc { padding: 40px 44px; }
  .tabbar { grid-template-columns: repeat(2, 120px) 72px repeat(2, 120px); justify-content: center; }
}

@media (max-width: 480px) {
  .doc-frame { overflow-x: auto; }
  .doc { padding: 18px 14px; }
  .doc-items th, .doc-items td { padding: 7px 4px; font-size: 12px; }
  .doc-items th { letter-spacing: .03em; font-size: 9.5px; }
  .doc-items td span { font-size: 11px; }
}
.pdf-render .doc-items th, .pdf-render .doc-items td { padding: 9px 8px; font-size: 13px; }
.pdf-render .doc-items th { font-size: 10.5px; letter-spacing: .08em; }

/* ---------- Capturas de pantalla ---------- */
.shots { margin-top: 10px; }
.shot-btn { width: 100%; border-style: dashed; border-width: 1.5px; background: #fbfaf7; min-height: 52px; }
.shot-thumbs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 8px; }
.shot-thumbs:empty { display: none; }
.shot-thumb { position: relative; aspect-ratio: 9 / 16; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); background: #eee; }
.shot-thumb img { width: 100%; height: 100%; object-fit: cover; object-position: top; display: block; }
.shot-thumb button {
  position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; border-radius: 50%;
  border: 0; background: rgba(24,33,43,.75); color: #fff; font-size: 17px; line-height: 1; cursor: pointer;
}
.or { display: flex; align-items: center; gap: 10px; margin: 12px 0 2px; color: var(--ink-3); font-size: 12px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.paste p.paste-label { font-weight: 600; margin: 0; }
