:root {
  --bg: #f3f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --ink: #102a43;
  --ink-2: #334e68;
  --muted: #708090;
  --line: #dbe5ee;
  --primary: #0f766e;
  --primary-2: #0b5f59;
  --primary-soft: #e6f7f5;
  --blue: #2563eb;
  --blue-soft: #eaf1ff;
  --amber: #b7791f;
  --amber-soft: #fff7df;
  --red: #b42318;
  --red-soft: #fff0ee;
  --green: #157347;
  --green-soft: #eaf7ef;
  --shadow: 0 8px 24px rgba(16, 42, 67, 0.08);
  --radius: 16px;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.45;
}
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
code, pre { font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace; }

.app-shell { max-width: 1560px; margin: 0 auto; padding: 0 24px 48px; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 20px;
  min-height: 78px; padding: 12px 0;
  background: rgba(243, 246, 249, 0.94); backdrop-filter: blur(12px);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  display: grid; place-items: center; width: 44px; height: 44px;
  border-radius: 13px; background: var(--ink); color: white; font-weight: 900; font-size: 20px;
  box-shadow: var(--shadow);
}
.brand h1 { margin: 0; font-size: 18px; letter-spacing: .12em; }
.brand p { margin: 1px 0 0; color: var(--muted); font-size: 12px; }
.top-actions { display: flex; align-items: center; gap: 10px; }
.save-status { color: var(--muted); font-size: 12px; margin-right: 4px; }

.tabs {
  display: flex; gap: 6px; margin: 2px 0 20px; padding: 5px;
  border: 1px solid var(--line); background: var(--surface); border-radius: 14px; width: fit-content;
  box-shadow: 0 3px 12px rgba(16,42,67,.04);
}
.tab {
  border: 0; background: transparent; color: var(--ink-2); padding: 9px 16px; border-radius: 10px; font-weight: 700;
}
.tab:hover { background: var(--surface-2); }
.tab.is-active { color: white; background: var(--ink); }

.view { display: none; }
.view.is-active { display: block; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}
.hero-grid { display: grid; grid-template-columns: minmax(280px, .8fr) minmax(460px, 1.4fr); gap: 16px; margin-bottom: 16px; }
.order-meta-card, .config-quick-card { padding: 20px; }
.card-title-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.eyebrow { display: block; color: var(--primary); font-weight: 800; font-size: 11px; letter-spacing: .12em; margin-bottom: 4px; }
h2, h3 { margin: 0; }
h2 { font-size: 24px; letter-spacing: -.02em; }
h3 { font-size: 17px; }
.muted { color: var(--muted); }
.clamp-2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.mini-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 14px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; padding: 4px 9px; border-radius: 999px;
  background: #eef3f7; color: var(--ink-2); font-size: 11px; font-weight: 700;
}
.chip-blue { background: var(--blue-soft); color: var(--blue); }
.chip-amber { background: var(--amber-soft); color: var(--amber); }
.chip-green { background: var(--green-soft); color: var(--green); }
.chip-red { background: var(--red-soft); color: var(--red); }

.config-quick-grid { display: grid; grid-template-columns: 1fr 1fr 1.1fr; gap: 12px; }
label { display: flex; flex-direction: column; gap: 6px; color: var(--ink-2); font-size: 12px; font-weight: 700; }
input, select, textarea {
  width: 100%; border: 1px solid #cbd8e3; background: white; color: var(--ink); border-radius: 10px;
  padding: 10px 11px; outline: none; transition: .15s border, .15s box-shadow;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(15,118,110,.12); }
textarea { resize: vertical; }
.mixed-progress { margin-top: 14px; padding: 10px 12px; border-radius: 11px; background: var(--surface-2); color: var(--ink-2); font-size: 12px; }
.mixed-progress strong { color: var(--primary); }

.kpi-grid { display: grid; grid-template-columns: repeat(6, minmax(145px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { padding: 15px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 5px 18px rgba(16,42,67,.05); }
.kpi span { display: block; color: var(--muted); font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .08em; }
.kpi strong { display: block; margin-top: 4px; font-size: 24px; letter-spacing: -.03em; }
.kpi small { display: block; color: var(--muted); margin-top: 2px; }
.kpi-accent { background: linear-gradient(135deg, #0f766e, #115e59); color: white; border-color: transparent; }
.kpi-accent span, .kpi-accent small { color: rgba(255,255,255,.78); }

.toolbar-card { padding: 12px; margin-bottom: 16px; }
.toolbar { display: flex; align-items: flex-end; gap: 10px; flex-wrap: wrap; }
.search-wrap { position: relative; flex: 1 1 340px; }
.search-wrap input { padding-left: 36px; }
.search-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 18px; }
.compact-field { min-width: 175px; }
.check-control { flex-direction: row; align-items: center; gap: 8px; height: 42px; padding: 0 8px; white-space: nowrap; }
.check-control input { width: 17px; height: 17px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px; min-height: 40px; padding: 9px 13px;
  border-radius: 10px; border: 1px solid transparent; font-weight: 800; font-size: 13px; text-decoration: none;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-2); }
.btn-secondary { background: white; color: var(--ink-2); border-color: var(--line); }
.btn-secondary:hover { background: var(--surface-2); }
.btn-danger { background: var(--red); color: white; }
.btn-danger-ghost { background: transparent; color: var(--red); border-color: #f2c7c1; }
.icon-btn {
  width: 34px; height: 34px; display: grid; place-items: center; border: 1px solid var(--line); background: white; color: var(--ink-2); border-radius: 9px; font-size: 17px;
}
.icon-btn:hover { background: var(--surface-2); }

.table-card { overflow: hidden; }
.table-caption { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 17px 18px 13px; border-bottom: 1px solid var(--line); }
.table-caption p { margin: 4px 0 0; font-size: 12px; }
.table-wrap { overflow: auto; max-height: calc(100vh - 270px); }
.data-table { width: 100%; border-collapse: collapse; min-width: 1050px; }
.data-table th {
  position: sticky; top: 0; z-index: 4; background: #f7fafc; color: #52677b; text-align: left; font-size: 10px; letter-spacing: .07em; text-transform: uppercase;
  padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap;
}
.data-table td { padding: 10px 12px; border-bottom: 1px solid #edf2f6; vertical-align: middle; }
.data-table tr:hover td { background: #fbfdff; }
.data-table tr.is-selected td { background: #f7fffd; }
.product-main { min-width: 280px; }
.product-name { font-weight: 760; color: var(--ink); line-height: 1.25; }
.product-sub { display: flex; flex-wrap: wrap; gap: 5px 8px; margin-top: 4px; color: var(--muted); font-size: 11px; }
.product-warning { margin-top: 5px; color: var(--amber); font-size: 11px; font-weight: 700; }
.number { font-variant-numeric: tabular-nums; white-space: nowrap; }
.qty-input { width: 82px; text-align: right; padding: 8px 9px; }
.price-link { border: 0; padding: 0; background: transparent; color: var(--ink); font-weight: 800; text-decoration: underline dotted; text-underline-offset: 3px; }
.price-note { display: block; margin-top: 3px; font-size: 10px; color: var(--amber); white-space: nowrap; }
.row-actions { display: flex; gap: 5px; justify-content: flex-end; }
.empty-state { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 48px 20px; color: var(--muted); }
.hidden { display: none !important; }

.section-heading { display: flex; justify-content: space-between; align-items: flex-end; gap: 20px; margin-bottom: 16px; }
.section-heading p { margin: 4px 0 0; }
.catalog-table { min-width: 1150px; }
.source-badge { display: inline-flex; max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.settings-card { padding: 20px; }
.settings-card h3 { margin-bottom: 15px; }
.span-2-card { grid-column: 1 / -1; }
.form-grid { display: grid; gap: 13px; }
.two-col { grid-template-columns: 1fr 1fr; }
.span-2 { grid-column: 1 / -1; }
.rule-note { margin-top: 14px; padding: 11px 12px; border-radius: 10px; background: var(--amber-soft); color: #6d4b10; font-size: 12px; }
.live-conversion { margin-top: 16px; display: flex; align-items: center; justify-content: space-between; padding: 13px; background: var(--primary-soft); color: var(--primary-2); border-radius: 11px; }
.rules-summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.rule-pill { padding: 13px; border-radius: 11px; background: var(--surface-2); border: 1px solid var(--line); }
.rule-pill span { display: block; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .07em; font-weight: 800; }
.rule-pill strong { display: block; margin-top: 4px; }
.rule-pill.is-on { background: var(--green-soft); border-color: #cce8d7; }
.rule-pill.is-off { background: #f8fafc; }

.data-actions-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 16px; }
.data-action-card { padding: 20px; min-height: 190px; display: flex; flex-direction: column; align-items: flex-start; }
.data-action-card p { color: var(--muted); font-size: 13px; flex: 1; }
.danger-card { border-color: #f0cbc6; }
.button-stack { display: flex; gap: 8px; flex-wrap: wrap; }
.file-btn { position: relative; overflow: hidden; }
.schema-card { padding: 20px; }
.schema-card p { margin-top: 4px; }
.schema-card pre { overflow: auto; padding: 16px; margin: 15px 0 0; border-radius: 12px; background: #0b1f33; color: #d9e7f3; font-size: 12px; }

.modal { border: 0; padding: 0; background: transparent; max-width: min(720px, calc(100vw - 28px)); width: 100%; }
.modal::backdrop { background: rgba(5, 20, 35, .55); backdrop-filter: blur(3px); }
.modal-card { background: white; border-radius: 18px; padding: 20px; box-shadow: 0 24px 70px rgba(0,0,0,.22); }
.modal-small { max-width: 520px; margin: 0 auto; }
.modal-header { display: flex; justify-content: space-between; gap: 20px; align-items: flex-start; margin-bottom: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.modal-card > label { margin-top: 12px; }
.split-actions { justify-content: space-between; }
.split-actions > div { display: flex; gap: 8px; }
.price-compare { padding: 12px; margin-bottom: 12px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 11px; font-size: 13px; }

.toast {
  position: fixed; right: 22px; bottom: 22px; z-index: 100; max-width: 380px;
  transform: translateY(20px); opacity: 0; pointer-events: none;
  padding: 12px 14px; border-radius: 11px; background: var(--ink); color: white; box-shadow: 0 14px 34px rgba(0,0,0,.22);
  transition: .2s ease; font-size: 13px;
}
.toast.is-visible { transform: translateY(0); opacity: 1; }

@media (max-width: 1180px) {
  .kpi-grid { grid-template-columns: repeat(3, 1fr); }
  .data-actions-grid { grid-template-columns: repeat(2, 1fr); }
  .rules-summary { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 820px) {
  .app-shell { padding: 0 12px 34px; }
  .topbar { align-items: flex-start; }
  .top-actions { flex-wrap: wrap; justify-content: flex-end; }
  .save-status { display: none; }
  .tabs { width: 100%; overflow-x: auto; }
  .tab { flex: 0 0 auto; }
  .hero-grid, .settings-grid { grid-template-columns: 1fr; }
  .span-2-card { grid-column: auto; }
  .config-quick-grid { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .section-heading { align-items: flex-start; flex-direction: column; }
  .two-col { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .data-actions-grid { grid-template-columns: 1fr; }
  .data-action-card { min-height: auto; }
  .table-wrap { max-height: none; }
}
@media (max-width: 520px) {
  .brand p { display: none; }
  .top-actions .btn-secondary { display: none; }
  .kpi-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .kpi { padding: 12px; }
  .kpi strong { font-size: 20px; }
  .rules-summary { grid-template-columns: 1fr; }
  .split-actions { flex-direction: column; }
  .split-actions > div { width: 100%; }
  .split-actions .btn { flex: 1; }
}

@media print {
  body { background: white; font-size: 11px; }
  .app-shell { max-width: none; padding: 0; }
  .topbar { position: static; background: white; }
  .top-actions, .tabs, .toolbar-card, .row-actions, .icon-btn, #view-catalogo, #view-configuracion, #view-datos { display: none !important; }
  #view-pedido { display: block !important; }
  .hero-grid { grid-template-columns: 1fr 1fr; }
  .card, .kpi { box-shadow: none; break-inside: avoid; }
  .table-wrap { overflow: visible; max-height: none; }
  .data-table { min-width: 0; font-size: 9px; }
  .data-table th, .data-table td { padding: 5px 6px; }
  .qty-input { border: 0; width: 44px; padding: 0; background: transparent; }
  .price-link { text-decoration: none; }
  .order-table th:last-child, .order-table td:last-child { display: none; }
}

/* v3 · CUP, rentabilidad y pedido limpio */
.config-quick-grid.four-col { grid-template-columns: repeat(4, minmax(130px, 1fr)); }
.kpi-grid { grid-template-columns: repeat(6, minmax(145px, 1fr)); }
.order-table { min-width: 1580px; }
.catalog-table { min-width: 1320px; }

.profit-card { margin-bottom: 16px; overflow: hidden; }
.profit-caption { border-bottom: 1px solid var(--line); }
.profit-summary-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 0; }
.profit-stat { padding: 15px 18px; border-right: 1px solid var(--line); background: #fff; }
.profit-stat:last-child { border-right: 0; }
.profit-stat span { display: block; color: var(--muted); font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .07em; }
.profit-stat strong { display: block; margin-top: 4px; font-size: 20px; font-variant-numeric: tabular-nums; }
.profit-stat small { display: block; margin-top: 2px; color: var(--muted); }
.profit-positive { background: var(--green-soft); }
.negative { color: var(--red) !important; }

.product-cell { display: flex; align-items: flex-start; gap: 10px; }
.product-thumb { width: 72px; height: 54px; flex: 0 0 auto; object-fit: cover; border-radius: 9px; border: 1px solid var(--line); background: var(--surface-2); }
.product-thumb.small { width: 58px; height: 44px; }
.cell-note { display: block; color: var(--muted); font-size: 10px; margin-top: 3px; line-height: 1.25; white-space: normal; }
.price-note { max-width: 220px; white-space: normal; line-height: 1.25; }
.center-cell { text-align: center; }
.is-planning td { background: #fffaf0 !important; }
.is-inactive { opacity: .58; }

.sale-input { width: 112px; text-align: right; padding: 8px 9px; }
.sale-input:disabled { background: #f3f6f9; color: var(--muted); }
.gain-cell { min-width: 185px; }
.gain-stack { display: flex; flex-direction: column; gap: 2px; font-variant-numeric: tabular-nums; }
.gain-stack strong { font-size: 13px; }
.gain-stack small { font-size: 10px; }
.gain-positive strong { color: var(--green); }
.gain-negative strong { color: var(--red); }

.switch-check { display: inline-flex; align-items: center; justify-content: center; cursor: pointer; }
.switch-check input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-check span { width: 36px; height: 20px; border-radius: 999px; background: #cbd8e3; position: relative; transition: .15s; }
.switch-check span::after { content: ''; position: absolute; width: 16px; height: 16px; top: 2px; left: 2px; background: #fff; border-radius: 50%; box-shadow: 0 1px 3px rgba(0,0,0,.2); transition: .15s; }
.switch-check input:checked + span { background: var(--primary); }
.switch-check input:checked + span::after { left: 18px; }
.switch-check input:disabled + span { opacity: .45; cursor: not-allowed; }

.btn-mini { min-height: 32px; padding: 5px 9px; font-size: 11px; background: white; color: var(--primary); border-color: #b9ddd9; }
.btn-mini:hover { background: var(--primary-soft); }
.conversion-stack { align-items: flex-start; flex-direction: column; gap: 3px; }
.conversion-stack strong { font-size: 14px; }
.rules-summary { grid-template-columns: repeat(5, 1fr); }

.modal.modal-wide { max-width: min(1180px, calc(100vw - 28px)); }
.modal-subtitle { margin: 5px 0 0; font-size: 12px; }
.share-sheet { border: 1px solid var(--line); border-radius: 14px; overflow: hidden; background: white; }
.share-head { display: flex; justify-content: space-between; gap: 20px; align-items: flex-start; padding: 20px; border-bottom: 1px solid var(--line); }
.share-meta { text-align: right; display: flex; flex-direction: column; gap: 2px; color: var(--ink-2); }
.share-meta span { color: var(--muted); font-size: 12px; }
.supplier-note { margin: 14px 20px 0; padding: 10px 12px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; }
.share-summary { display: flex; gap: 8px; flex-wrap: wrap; padding: 14px 20px 0; color: var(--muted); font-size: 11px; }
.share-summary span { padding: 4px 8px; background: #eef3f7; border-radius: 999px; }
.share-table-wrap { overflow: auto; padding: 14px 20px 20px; }
.share-table { width: 100%; border-collapse: collapse; min-width: 760px; }
.share-table th { text-align: left; padding: 8px; color: #52677b; background: #f7fafc; font-size: 10px; text-transform: uppercase; letter-spacing: .06em; border-bottom: 1px solid var(--line); }
.share-table td { padding: 9px 8px; border-bottom: 1px solid #edf2f6; vertical-align: top; }
.share-actions { flex-wrap: wrap; }

@media (max-width: 1260px) {
  .config-quick-grid.four-col { grid-template-columns: repeat(2, 1fr); }
  .profit-summary-grid { grid-template-columns: repeat(3, 1fr); }
  .profit-stat:nth-child(3) { border-right: 0; }
  .profit-stat:nth-child(-n+3) { border-bottom: 1px solid var(--line); }
  .rules-summary { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 820px) {
  .config-quick-grid.four-col { grid-template-columns: 1fr 1fr; }
  .profit-summary-grid { grid-template-columns: repeat(2, 1fr); }
  .profit-stat { border-bottom: 1px solid var(--line); }
  .profit-stat:nth-child(2n) { border-right: 0; }
  .profit-stat:nth-last-child(-n+2) { border-bottom: 0; }
  .share-head { flex-direction: column; }
  .share-meta { text-align: left; }
  .rules-summary { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 520px) {
  #shareOrderBtn { display: inline-flex !important; }
  #exportOrderBtn { display: none; }
  .top-actions { gap: 6px; }
  .top-actions .btn { min-height: 36px; padding: 7px 9px; font-size: 11px; }
  .config-quick-grid.four-col { grid-template-columns: 1fr; }
  .profit-summary-grid { grid-template-columns: 1fr 1fr; }
  .rules-summary { grid-template-columns: 1fr; }
  .share-actions .btn { flex: 1 1 45%; }
}
