:root {
  --bg: #f4f2ef;
  --surface: #ffffff;
  --border: #e4ddd3;
  --ink: #2b2420;
  --ink-soft: #6b6058;
  /* Impoexpor brand colors, sampled directly from the brand logo/color guide. */
  --accent: #d62e1c;
  --accent-ink: #ffffff;
  --accent-soft: #fbe6e2;
  --brand-blue: #0000b6;

  --ok-bg: #e6f4ea;
  --ok-ink: #1e7a3c;
  --warn-bg: #fdf1dc;
  --warn-ink: #9a6400;
  --warn2-bg: #fff4d6;
  --warn2-ink: #8a6d00;
  --bad-bg: #fbe6e6;
  --bad-ink: #b3261e;

  --radius: 12px;
  --shadow: 0 1px 2px rgba(30, 20, 10, 0.06), 0 8px 24px rgba(30, 20, 10, 0.06);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo {
  width: 38px; height: 38px; border-radius: 9px;
  background: var(--surface); border: 1px solid var(--border);
  object-fit: contain; padding: 3px; flex-shrink: 0;
}
.brand h1 { font-size: 18px; margin: 0; line-height: 1.2; font-weight: 700; }
.brand p { font-size: 11.5px; margin: 0; color: var(--ink-soft); }

.search-wrap { flex: 1; min-width: 200px; max-width: 380px; }
#search, #quickSearch {
  width: 100%; padding: 10px 14px; border-radius: 10px;
  border: 1px solid var(--border); font-size: 14px; background: var(--bg); color: var(--ink);
}
#search:focus, #quickSearch:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn {
  border: none; border-radius: 10px; padding: 10px 16px;
  font-size: 13px; font-weight: 600; cursor: pointer;
  transition: transform 0.05s ease, opacity 0.15s ease;
  white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { opacity: 0.92; }
.btn-secondary { background: var(--bg); color: var(--ink); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--accent-soft); }
.btn-danger { background: var(--bad-bg); color: var(--bad-ink); }
.btn-danger:hover { opacity: 0.85; }
.btn-danger-ghost { background: transparent; color: var(--bad-ink); border: 1px solid var(--bad-ink); }
.btn-danger-ghost:hover { background: var(--bad-bg); }
.icon-btn {
  border: 1px solid transparent; background: transparent; cursor: pointer; border-radius: 6px;
  padding: 3px 6px; font-size: 12px; color: var(--ink-soft);
}
.icon-btn:hover { background: var(--accent-soft); color: var(--accent); border-color: var(--border); }

/* Tab strip */
.tabstrip {
  display: flex; align-items: center; gap: 4px; padding: 8px 28px 0;
  overflow-x: auto; background: var(--surface); border-bottom: 1px solid var(--border);
  scrollbar-width: thin;
}
.tab {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 16px; border-radius: 10px 10px 0 0; font-size: 13px; font-weight: 600;
  color: var(--ink-soft); cursor: pointer; white-space: nowrap; border: 1px solid transparent;
  border-bottom: none; flex-shrink: 0;
}
.tab:hover { background: var(--bg); }
.tab.active { background: var(--bg); color: var(--accent); border-color: var(--border); position: relative; top: 1px; }
.tab .tab-dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.tab .tab-close {
  border: none; background: transparent; color: var(--ink-soft); border-radius: 50%;
  width: 18px; height: 18px; font-size: 12px; cursor: pointer; line-height: 1;
}
.tab .tab-close:hover { background: var(--accent-soft); color: var(--accent); }
.tab.tab-maestra { font-weight: 700; }

.content { padding: 20px 28px 40px; }

.table-wrap {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: auto; box-shadow: var(--shadow);
}

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--ink-soft); padding: 11px 14px; border-bottom: 1px solid var(--border);
  background: #faf8f6; white-space: nowrap; position: sticky; top: 0;
}
th.num, td.num { text-align: right; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--accent); }
tbody tr { border-bottom: 1px solid var(--border); }
tbody tr:hover { background: #fbf9f7; }
tbody tr:last-child { border-bottom: none; }
td { padding: 9px 14px; vertical-align: middle; }
td.name-cell { font-weight: 600; cursor: pointer; }
td.name-cell:hover { color: var(--accent); text-decoration: underline; }
td.sub { color: var(--ink-soft); font-size: 11.5px; font-weight: 400; margin-top: 1px; }

tr.row-inactive { opacity: 0.55; }
tr.row-inactive:hover { opacity: 0.85; }
.badge-inactive {
  display: inline-block; font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em;
  background: var(--bad-bg); color: var(--bad-ink); padding: 1px 7px; border-radius: 999px; margin-left: 6px;
}
.inactive-toggle {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--ink-soft);
  margin-bottom: 10px; cursor: pointer; user-select: none;
}
.inactive-toggle input { width: auto; }
td.col-actions { white-space: nowrap; width: 1%; }
td.col-actions .icon-btn { font-size: 14px; }

tfoot .totals-row td { font-weight: 700; background: #faf8f6; border-top: 2px solid var(--border); }
tfoot .totals-row td:nth-child(3) { color: var(--ink-soft); font-weight: 600; text-transform: uppercase; font-size: 11px; letter-spacing: 0.03em; }

.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot-disponible { background: var(--ok-ink); }
.dot-stock_bajo { background: var(--warn-ink); }
.dot-por_vencer { background: var(--warn2-ink); }
.dot-vencido, .dot-sin_stock { background: var(--bad-ink); }

.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 11px; font-weight: 700;
  margin-right: 4px; margin-top: 4px;
}
.badge-disponible { background: var(--ok-bg); color: var(--ok-ink); }
.badge-stock_bajo { background: var(--warn-bg); color: var(--warn-ink); }
.badge-por_vencer { background: var(--warn2-bg); color: var(--warn2-ink); }
.badge-vencido, .badge-sin_stock { background: var(--bad-bg); color: var(--bad-ink); }

/* Login screen */
.login-page { display: flex; min-height: 100vh; align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 100%; max-width: 360px; }
.login-header { display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center; margin-bottom: 20px; }
.login-header img { height: 34px; width: auto; object-fit: contain; }
.login-header h1 { font-size: 16px; font-weight: 700; margin: 0; }
.badge-bad { display: block; background: var(--bad-bg); color: var(--bad-ink); border-radius: 8px; padding: 9px 12px; font-size: 13px; margin-bottom: 14px; }
.badge-bad[hidden] { display: none; }
.login-remember { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--ink); margin: 2px 0 14px; }
.login-remember input { width: auto; }

.empty-state { padding: 40px; text-align: center; color: var(--ink-soft); }

/* Inline-editable cells (Maestra tab) */
.edit-cell {
  border: 1px solid transparent; border-radius: 6px; padding: 4px 6px; font: inherit;
  color: inherit; background: transparent; width: 100%; min-width: 70px;
}
.edit-cell:hover { border-color: var(--border); }
.edit-cell:focus { border-color: var(--accent); background: var(--surface); outline: none; }
.edit-cell.obs { min-width: 260px; }
.edit-cell.saved { animation: flashSaved 0.9s ease; }
td.num .edit-cell { text-align: right; }

/* Conforme / No conforme: narrow, fixed-width columns so the room they give
   up goes to Observaciones instead. */
th.col-narrow, td.col-narrow { width: 92px; max-width: 92px; }
th.col-narrow .edit-cell, td.col-narrow .edit-cell { min-width: 0; }
th.col-obs, td.col-obs { width: auto; }
@keyframes flashSaved { 0% { background: var(--ok-bg); } 100% { background: transparent; } }

/* "No aplica" sits visually over an empty date input; clicking it opens the
   real date picker underneath (see the click handler in app.js) - the plain
   `hidden` attribute alone isn't enough to hide it once a date is set,
   because this rule's own `display` would otherwise out-rank the browser's
   default [hidden] rule at equal specificity, so it's overridden explicitly. */
td.date-cell { position: relative; }
td.date-cell .no-aplica {
  position: absolute; inset: 1px; display: flex; align-items: center; padding: 4px 6px 4px 7px;
  color: var(--ink-soft); font-size: 13.5px; font-style: italic; cursor: pointer; background: var(--surface);
  border-radius: 6px;
}
td.date-cell .no-aplica[hidden] { display: none; }

/* Product tab view */
.product-view { max-width: 1400px; }
.product-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 16px; }
.p-title { margin: 0; font-size: 21px; }
.p-code { color: var(--ink-soft); font-size: 13px; margin: 2px 0 10px; }
.product-header-actions { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
.product-header-actions .btn { font-size: 12px; padding: 7px 12px; white-space: nowrap; }

/* Left: stock summary + forms. Right: kardex history, stretched to match
   the left column's height so it reads as "the history sits beside it". */
.product-main { display: grid; grid-template-columns: minmax(320px, 440px) 1fr; gap: 18px; align-items: stretch; }
.product-left { display: flex; flex-direction: column; gap: 18px; }
.product-right { display: flex; flex-direction: column; }

.stock-hero {
  display: flex; gap: 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow);
  flex-wrap: wrap;
}
.stock-hero .stat { min-width: 110px; }
.stock-hero .big { font-size: 24px; font-weight: 800; }
.stock-hero .lbl { font-size: 11px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.04em; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.panel-title { font-size: 12.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-soft); margin: 0 0 12px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-grid label { font-size: 12px; color: var(--ink-soft); display: block; margin-bottom: 4px; }
input, select {
  width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); font-size: 13.5px;
  background: var(--surface); color: var(--ink);
}
.field { display: flex; flex-direction: column; }
.field.full { grid-column: 1 / -1; }

.tab-toggle { display: flex; gap: 6px; margin-bottom: 10px; }
.tab-toggle button {
  flex: 1; padding: 9px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); font-weight: 700; font-size: 13px; cursor: pointer; color: var(--ink-soft);
}
.tab-toggle button.active.tipo-ingreso { background: var(--ok-bg); color: var(--ok-ink); border-color: var(--ok-ink); }
.tab-toggle button.active.tipo-salida { background: var(--bad-bg); color: var(--bad-ink); border-color: var(--bad-ink); }

.kardex-wrap {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column; height: 100%;
}
.kardex { flex: 1; min-height: 320px; overflow-y: auto; }
.kardex table { font-size: 12.5px; }
.kardex thead th { position: sticky; top: 0; }
.pos { color: var(--ok-ink); font-weight: 600; }
.neg { color: var(--bad-ink); font-weight: 600; }
.kardex .src-manual { font-size: 10px; background: var(--accent-soft); color: var(--accent); padding: 1px 6px; border-radius: 999px; }

.hint { font-size: 11.5px; color: var(--ink-soft); margin-top: 4px; }

/* Quick "registrar movimiento" search modal */
.overlay { position: fixed; inset: 0; background: rgba(20, 14, 10, 0.35); z-index: 30; }
.quick-modal {
  position: fixed; top: 14vh; left: 50%; transform: translateX(-50%);
  width: min(480px, 92vw); background: var(--surface); border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,0.25); z-index: 31; padding: 14px;
}
.quick-results { max-height: 320px; overflow-y: auto; margin-top: 8px; }
.quick-result {
  padding: 10px 12px; border-radius: 8px; cursor: pointer; display: flex; justify-content: space-between; gap: 8px;
}
.quick-result:hover { background: var(--accent-soft); }
.quick-result .qr-name { font-weight: 600; font-size: 13.5px; }
.quick-result .qr-sub { font-size: 11.5px; color: var(--ink-soft); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 10px; font-size: 13px; z-index: 40;
}

.mov-edit-modal { top: 8vh; }
.mov-edit-title { margin: 0 0 12px; font-size: 15px; }
.mov-edit-actions { display: flex; gap: 8px; justify-content: space-between; }
.mov-edit-actions .btn { flex: 1; }

/* Dashboard */
.totals-strip {
  display: flex; gap: 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow); flex-wrap: wrap;
}
.totals-strip .totals-item { min-width: 160px; }
.totals-strip .big { font-size: 24px; font-weight: 800; color: var(--accent); }
.totals-strip .big.tone-blue { color: var(--ink); }
.totals-strip .big.tone-ok { color: var(--ink); }
.totals-strip .big.tone-bad { color: var(--bad-ink); }
.totals-strip .lbl { font-size: 11px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.04em; }

.stat-cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.stat-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.stat-card .sc-count { font-size: 28px; font-weight: 800; }
.stat-card .sc-label { font-size: 12px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.03em; margin-bottom: 8px; }
.stat-card.sc-vencido .sc-count { color: var(--bad-ink); }
.stat-card.sc-sin_stock .sc-count { color: var(--bad-ink); }
.stat-card.sc-stock_bajo .sc-count { color: var(--warn-ink); }
.stat-card.sc-por_vencer .sc-count { color: var(--warn2-ink); }
.sc-list { margin-top: 10px; display: flex; flex-direction: column; gap: 4px; }
.sc-list a { font-size: 12.5px; color: var(--ink); text-decoration: none; cursor: pointer; }
.sc-list a:hover { color: var(--accent); text-decoration: underline; }
.sc-more { font-size: 11.5px; color: var(--ink-soft); }
.sc-empty { font-size: 12px; color: var(--ink-soft); }

.dash-panels { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; }
.dash-panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.dash-panel .panel-title { margin-bottom: 10px; }
.activity-row { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 12.5px; }
.activity-row:last-child { border-bottom: none; }
.activity-row .a-name { font-weight: 600; cursor: pointer; }
.activity-row .a-name:hover { color: var(--accent); text-decoration: underline; }
.activity-row .a-meta { color: var(--ink-soft); font-size: 11px; }
.mover-row { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px solid var(--border); font-size: 12.5px; }
.mover-row:last-child { border-bottom: none; }
.mover-row .m-name { cursor: pointer; }
.mover-row .m-name:hover { color: var(--accent); text-decoration: underline; }
.mover-row .m-count { font-weight: 700; color: var(--accent); }

@media (max-width: 1000px) {
  .stat-cards { grid-template-columns: repeat(2, 1fr); }
  .dash-panels { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
  .product-main { grid-template-columns: 1fr; }
  .kardex-wrap { height: auto; }
  .kardex { max-height: 420px; }
}
@media (max-width: 720px) {
  .search-wrap { order: 3; max-width: none; width: 100%; }
  td.hide-mobile, th.hide-mobile { display: none; }
}
