:root {
  --tinta: #141926;
  --plomo: #485058;
  --texto: #4f5562;
  --suave: #6d7380;
  --niebla: #dce0e6;
  --tiza: #f2f4f7;
  --mesa: #e6e9ee;
  --amarillo: #f7b917;
  --amarillo-2: #e5a80e;
  --ok: #1f7a4d;
  --error: #b42318;
  --r-sm: 8px;
  --r-md: 12px;
  --shadow-paper: 0 1px 2px rgba(20, 25, 38, .06), 0 8px 28px -6px rgba(20, 25, 38, .16);
  --ease: cubic-bezier(.22, 1, .36, 1);
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--tiza);
  color: var(--tinta);
  font: 400 14px/1.5 'Poppins', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
::selection { background: var(--amarillo); color: var(--tinta); }
button, input, select, textarea { font: inherit; color: inherit; }
button, a, label { touch-action: manipulation; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--tinta); outline-offset: 2px; }
svg.lucide { width: 18px; height: 18px; stroke-width: 1.75; flex: none; }

/* ---------- Botones ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  height: 38px; padding: 0 14px;
  border: 1px solid transparent; border-radius: var(--r-sm);
  background: none; cursor: pointer; white-space: nowrap;
  font-weight: 500; font-size: 13.5px; text-decoration: none; color: var(--tinta);
  transition: background-color .15s, border-color .15s, color .15s;
}
.btn--primary { background: var(--amarillo); }
.btn--primary:hover { background: var(--amarillo-2); }
.btn--line { border-color: var(--niebla); background: #fff; }
.btn--line:hover { border-color: var(--plomo); }
.btn--ghost:hover { background: var(--tiza); }
.btn--dark { background: var(--tinta); color: #fff; }
.btn--dark:hover { background: #262c3b; }
.btn--danger { color: var(--error); }
.btn--danger:hover { background: #fdecea; }
.btn--sm { height: 32px; padding: 0 10px; font-size: 13px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px;
  border: 0; border-radius: var(--r-sm); background: none; cursor: pointer; color: var(--plomo);
}
.icon-btn:hover { background: var(--tiza); color: var(--tinta); }

/* ---------- Barra superior ---------- */
.app { height: 100vh; height: 100dvh; display: grid; grid-template-rows: auto auto 1fr; }
.bar {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 20px;
  height: 62px; padding: 0 16px 0 20px;
  background: #fff; border-bottom: 1px solid var(--niebla);
}
.bar__brand { display: flex; align-items: center; gap: 12px; text-decoration: none; color: var(--tinta); }
.bar__brand img { height: 30px; width: auto; }
.bar__brand span { font-weight: 500; font-size: 15px; padding-left: 12px; border-left: 1px solid var(--niebla); }
.bar__quote { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.bar__num { font-weight: 600; font-size: 15px; white-space: nowrap; }
.bar__client { color: var(--suave); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.save-state { font-size: 12px; color: var(--suave); white-space: nowrap; }
.save-state.is-saving { color: var(--plomo); }
.bar__actions { display: flex; align-items: center; gap: 4px; }
.bar__sep { width: 1px; height: 24px; background: var(--niebla); margin: 0 8px; }

.views { display: none; }

/* ---------- Área de trabajo ---------- */
.work { display: grid; grid-template-columns: minmax(420px, 480px) 1fr; min-height: 0; }
.editor {
  background: #fff; border-right: 1px solid var(--niebla);
  overflow-y: auto; min-height: 0; display: flex; flex-direction: column;
  scrollbar-width: thin; scrollbar-color: var(--niebla) transparent;
}
.desk { position: relative; background: var(--mesa); min-height: 0; display: flex; flex-direction: column; }
.desk__scroll { flex: 1; overflow: auto; padding: 32px 24px 64px; scrollbar-width: thin; scrollbar-color: #c3c8d1 transparent; }
.desk__zoom {
  position: absolute; z-index: 2; right: 20px; bottom: 20px;
  display: flex; align-items: center; gap: 2px; padding: 3px;
  background: #fff; border-radius: 10px; box-shadow: var(--shadow-paper);
  font-size: 12.5px; color: var(--plomo);
}
.desk__zoom span { min-width: 62px; text-align: center; font-variant-numeric: tabular-nums; }
.pages { display: flex; flex-direction: column; align-items: center; gap: 24px; transform-origin: top center; }
.pages .page { box-shadow: var(--shadow-paper); flex: none; }

/* ---------- Editor ---------- */
.ed-sec { padding: 22px 24px 24px; border-bottom: 1px solid var(--tiza); }
.ed-sec__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.ed-sec h2 { margin: 0; font-size: 15.5px; font-weight: 600; letter-spacing: -.005em; }
.ed-sec h2 small { font-weight: 400; color: var(--suave); font-size: 13px; margin-left: 6px; }
.ed-hint { margin: -8px 0 14px; color: var(--suave); font-size: 13px; }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 12px; }
.grid--3 { grid-template-columns: 1fr 1fr 1fr; }
.span-2 { grid-column: span 2; }
.span-all { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > span { font-size: 12.5px; color: var(--plomo); font-weight: 500; }
.field > span em { font-style: normal; font-weight: 400; color: var(--suave); }
.input, .select, .textarea {
  width: 100%; height: 40px; padding: 0 12px;
  border: 1px solid var(--niebla); border-radius: var(--r-sm); background: #fff;
  transition: border-color .15s, box-shadow .15s;
}
.textarea { height: auto; min-height: 72px; padding: 10px 12px; resize: vertical; line-height: 1.45; }
.select { appearance: none; padding-right: 34px; background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='none' stroke='%23485058' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m3 4.5 3 3 3-3'/%3E%3C/svg%3E") no-repeat right 12px center; }
.input:hover, .select:hover, .textarea:hover { border-color: #c4c9d2; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--tinta); box-shadow: 0 0 0 3px rgba(20, 25, 38, .09); }
.input::placeholder, .textarea::placeholder { color: #7f8590; }
.input[aria-invalid="true"] { border-color: var(--error); }
.field__err { font-size: 12px; color: var(--error); }
.money { position: relative; }
.money .input { padding-left: 24px; text-align: right; font-variant-numeric: tabular-nums; }
.money::before { content: '$'; position: absolute; left: 12px; top: 50%; translate: 0 -50%; color: var(--suave); pointer-events: none; }
.suffix { position: relative; }
.suffix .input { padding-right: 30px; text-align: right; }
.suffix[data-suffix="días"] .input { padding-right: 48px; }
.suffix::after { content: attr(data-suffix); position: absolute; right: 12px; top: 50%; translate: 0 -50%; color: var(--suave); pointer-events: none; }

/* Selector segmentado */
.seg { display: flex; padding: 3px; gap: 2px; background: var(--tiza); border-radius: 10px; }
.seg button {
  flex: 1; height: 34px; padding: 0 8px; border: 0; border-radius: 7px; background: none; cursor: pointer;
  font-size: 13px; font-weight: 500; color: var(--plomo); white-space: nowrap;
  transition: background-color .15s, color .15s, box-shadow .15s;
}
.seg button:hover:not(:disabled) { color: var(--tinta); }
.seg button[aria-pressed="true"] { background: #fff; color: var(--tinta); box-shadow: 0 1px 2px rgba(20, 25, 38, .08), 0 2px 6px -2px rgba(20, 25, 38, .12); }
.seg button:disabled { opacity: .38; cursor: not-allowed; }

/* Tarjetas de ítem */
.items { display: flex; flex-direction: column; gap: 12px; }
.item { border: 1px solid var(--niebla); border-radius: var(--r-md); background: #fff; }
.item__head { display: grid; grid-template-columns: 64px 1fr auto; align-items: center; gap: 12px; padding: 12px 10px 12px 12px; border-bottom: 1px solid var(--tiza); }
.item__thumb { width: 64px; height: 48px; object-fit: contain; background: var(--tiza); border-radius: 6px; }
.item__thumb--icon { display: grid; place-items: center; color: var(--plomo); }
.item__title { font-weight: 600; font-size: 14px; line-height: 1.3; }
.item__sub { font-size: 12.5px; color: var(--suave); }
.item__tools { display: flex; gap: 2px; }
.item__body { padding: 14px 12px 14px; display: grid; gap: 12px; }
.item__row { display: grid; grid-template-columns: 92px 1fr; align-items: center; gap: 10px; }
.item__row > label, .item__row > span { font-size: 12.5px; color: var(--plomo); font-weight: 500; }
.price-line {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
  padding: 10px 12px; border-radius: var(--r-sm); background: var(--tiza); font-size: 13px;
}
.price-line s { color: var(--suave); }
.price-line strong { font-size: 15px; font-variant-numeric: tabular-nums; }
.price-line .tag { font-size: 12px; color: var(--plomo); }
.warn { display: flex; gap: 8px; align-items: flex-start; padding: 9px 12px; border-radius: var(--r-sm); background: #fff6dc; color: #6b4e00; font-size: 12.5px; }
.warn svg.lucide { width: 16px; height: 16px; margin-top: 2px; }

.add-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.add-btn {
  display: inline-flex; align-items: center; gap: 8px; height: 38px; padding: 0 14px;
  border: 1px dashed #b9bfc9; border-radius: var(--r-sm); background: #fff; cursor: pointer;
  font-size: 13.5px; font-weight: 500; color: var(--tinta);
}
.add-btn:hover { border-color: var(--tinta); border-style: solid; }
.empty-ed { padding: 18px; border: 1px dashed var(--niebla); border-radius: var(--r-md); color: var(--suave); font-size: 13px; text-align: center; }

.stepper { display: inline-flex; align-items: center; border: 1px solid var(--niebla); border-radius: var(--r-sm); height: 36px; }
.stepper button { width: 34px; height: 100%; border: 0; background: none; cursor: pointer; color: var(--plomo); display: grid; place-items: center; }
.stepper button:hover { color: var(--tinta); background: var(--tiza); }
.stepper input { width: 40px; height: 100%; border: 0; text-align: center; font-variant-numeric: tabular-nums; background: none; }
.stepper input:focus { outline: none; }

.checks { display: flex; flex-wrap: wrap; gap: 8px; }
.check { display: inline-flex; align-items: center; gap: 8px; height: 36px; padding: 0 12px; border: 1px solid var(--niebla); border-radius: var(--r-sm); cursor: pointer; font-size: 13px; }
.check input { accent-color: var(--tinta); width: 16px; height: 16px; margin: 0; }
.check:has(input:checked) { border-color: var(--tinta); }

/* Resumen fijo al pie del editor */
.summary {
  position: sticky; bottom: 0; margin-top: auto;
  padding: 16px 24px 18px; background: var(--tinta); color: #fff;
  display: grid; grid-template-columns: 1fr auto; gap: 2px 16px; align-items: end;
}
.summary__label { font-size: 12.5px; color: #c9ced8; }
.summary__total { grid-column: 1; font-size: 26px; font-weight: 600; letter-spacing: -.01em; font-variant-numeric: tabular-nums; line-height: 1.15; }
.summary__meta { grid-column: 2; grid-row: 1 / span 2; text-align: right; font-size: 12.5px; color: #c9ced8; line-height: 1.6; font-variant-numeric: tabular-nums; }
.summary__meta b { color: var(--amarillo); font-weight: 500; }

/* ---------- Panel lateral ---------- */
.sheet { position: fixed; inset: 0; z-index: 40; }
.sheet__scrim { position: absolute; inset: 0; background: rgba(20, 25, 38, .32); animation: fade .2s var(--ease); }
.sheet__panel {
  position: absolute; top: 0; right: 0; bottom: 0; width: min(640px, 100%);
  background: #fff; display: flex; flex-direction: column;
  box-shadow: -12px 0 40px -12px rgba(20, 25, 38, .3);
  animation: slide .32s var(--ease);
}
.sheet__head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 16px 24px; border-bottom: 1px solid var(--niebla); }
.sheet__head h2 { margin: 0; font-size: 17px; font-weight: 600; }
.sheet__body { flex: 1; overflow-y: auto; padding: 8px 0 32px; }
.sheet__body .ed-sec:last-child { border-bottom: 0; }
@keyframes fade { from { opacity: 0; } }
@keyframes slide { from { transform: translateX(28px); opacity: .6; } }

/* Lista de cotizaciones */
.search { margin: 16px 24px 4px; }
.qlist { list-style: none; margin: 0; padding: 8px 12px; }
.qrow {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 14px; align-items: center;
  padding: 12px; border-radius: var(--r-md); cursor: pointer;
}
.qrow:hover { background: var(--tiza); }
.qrow.is-current { background: var(--tiza); box-shadow: inset 0 0 0 1px var(--niebla); }
.qrow__main { min-width: 0; }
.qrow__t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.qrow__t span { color: var(--suave); font-weight: 400; margin-left: 6px; }
.qrow__s { font-size: 12.5px; color: var(--suave); }
.qrow__r { text-align: right; font-variant-numeric: tabular-nums; }
.qrow__r strong { display: block; font-size: 14px; }
.qrow__tools { grid-column: 1 / -1; display: flex; gap: 4px; flex-wrap: wrap; }
.status {
  display: inline-flex; align-items: center; gap: 6px; height: 24px; padding: 0 8px;
  border-radius: 6px; font-size: 12px; font-weight: 500; background: var(--tiza); color: var(--plomo);
  border: 0; cursor: pointer;
}
.status::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status[data-s="enviada"] { color: #1d4ed8; background: #eaf0ff; }
.status[data-s="aceptada"] { color: var(--ok); background: #e7f5ee; }
.status[data-s="perdida"] { color: #8a3b12; background: #fbeee6; }

.price-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.price-table th { text-align: left; font-weight: 500; color: var(--suave); padding: 8px 6px; border-bottom: 1px solid var(--niebla); font-size: 12px; }
.price-table td { padding: 6px; border-bottom: 1px solid var(--tiza); vertical-align: middle; }
.price-table td:first-child { font-weight: 500; }
.price-table td small { display: block; color: var(--suave); font-weight: 400; }
.price-table .money .input { height: 34px; width: 132px; }
.team { display: flex; flex-direction: column; gap: 10px; }
.team__row { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; padding: 12px; border: 1px solid var(--niebla); border-radius: var(--r-md); }

/* ---------- Aviso ---------- */
.toast {
  position: fixed; z-index: 60; left: 50%; bottom: 24px; translate: -50% 0;
  display: flex; align-items: center; gap: 10px; max-width: calc(100% - 32px);
  padding: 12px 16px; border-radius: 10px; background: var(--tinta); color: #fff; font-size: 13.5px;
  box-shadow: 0 10px 30px -8px rgba(20, 25, 38, .45);
  animation: toast .3s var(--ease);
}
.toast svg.lucide { color: var(--amarillo); }
@keyframes toast { from { translate: -50% 10px; opacity: 0; } }

/* ---------- Vista cliente ---------- */
.client { min-height: 100vh; background: var(--mesa); display: flex; flex-direction: column; }
.client__bar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 20px; padding: 12px 20px;
  background: #fff; border-bottom: 1px solid var(--niebla);
}
.client__bar img { height: 32px; width: auto; }
.client__meta { flex: 1; min-width: 0; font-size: 13.5px; color: var(--suave); }
.client__meta strong { color: var(--tinta); font-weight: 600; margin-right: 8px; }
.client__actions { display: flex; gap: 8px; }
.client__scroll { flex: 1; overflow-x: auto; padding: 28px 16px 64px; }
.pages--client { transform-origin: top center; }

/* Medición fuera de pantalla */
.measure { position: absolute; left: -10000px; top: 0; visibility: hidden; }
.print-root { display: none; }

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
  .bar__actions .btn span { display: none; }
  .bar__actions .btn { padding: 0 10px; }
}
@media (max-width: 960px) {
  .app { height: auto; min-height: 100vh; grid-template-rows: auto auto 1fr; }
  .bar { position: sticky; top: 0; z-index: 10; grid-template-columns: auto 1fr auto; gap: 10px; padding: 0 8px 0 16px; height: 56px; }
  .bar__brand span, .bar__client, .save-state, .bar__sep { display: none; }
  .bar__brand img { height: 26px; }
  .bar__actions { gap: 0; }
  .views {
    display: flex; gap: 4px; padding: 8px 16px; background: #fff; border-bottom: 1px solid var(--niebla);
    position: sticky; top: 56px; z-index: 9;
  }
  .views button { flex: 1; height: 36px; border: 0; border-radius: 8px; background: none; font-weight: 500; color: var(--plomo); cursor: pointer; }
  .views button[aria-selected="true"] { background: var(--tiza); color: var(--tinta); }
  .work { display: block; }
  .editor { border-right: 0; overflow: visible; }
  .work[data-view="edit"] .desk { display: none; }
  .work[data-view="doc"] .editor { display: none; }
  .desk { min-height: calc(100vh - 110px); }
  .desk__scroll { padding: 20px 16px 72px; }
  .ed-sec { padding: 20px 16px; }
  .summary { padding: 14px 16px 16px; }
  .input, .select, .textarea, .stepper input { font-size: 16px; }
  .icon-btn { width: 42px; height: 42px; }
  .seg button { height: 40px; }
  .btn { height: 42px; }
  .desk { min-height: calc(100dvh - 110px); }
  .client__bar { flex-wrap: wrap; gap: 10px 14px; padding: 10px 16px; }
  .client__meta { order: 3; flex-basis: 100%; }
  .client__actions { margin-left: auto; }
}
@media (max-width: 480px) {
  .grid, .grid--3 { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .item__row { grid-template-columns: 1fr; gap: 6px; }
  .client__actions .btn span { display: none; }
  .team__row { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- Impresión: solo las hojas ---------- */
@page { size: A4; margin: 0; }
@media print {
  html, body { background: #fff; height: auto; }
  .app, .sheet, .toast, .measure, .client__bar { display: none !important; }
  body.printing > *:not(.print-root) { display: none !important; }
  body.printing .print-root { display: block !important; }
  .client, .client__scroll { display: block; min-height: 0; padding: 0; background: #fff; overflow: visible; }
  .pages--client { display: block; zoom: 1 !important; }
  .page { break-after: page; box-shadow: none !important; margin: 0; }
  .page:last-child { break-after: auto; }
}
