:root {
  color-scheme: light;
  --bg: #f3f6f5;
  --surface: #ffffff;
  --surface-2: #f7faf9;
  --text: #12211f;
  --muted: #5b6b68;
  --border: #dbe4e2;
  --primary: #0f766e;
  --primary-hover: #115e59;
  --primary-soft: #e0f2ef;
  --on-primary: #ffffff;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #92400e;
  --warn-soft: #fef3c7;
  --ok: #166534;
  --ok-soft: #dcfce7;
  --chart-bar: #0f766e;
  --chart-grid: #e3eae8;
  --shadow: 0 1px 2px rgba(16, 40, 36, .06), 0 4px 14px rgba(16, 40, 36, .05);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d1514;
    --surface: #152220;
    --surface-2: #192927;
    --text: #e6f0ee;
    --muted: #9db1ad;
    --border: #263836;
    --primary: #2fb5a5;
    --primary-hover: #4cc9ba;
    --primary-soft: #16302d;
    --on-primary: #06201d;
    --danger: #ff8a7d;
    --danger-soft: #3a1c19;
    --warn: #fbbf77;
    --warn-soft: #3a2b12;
    --ok: #7ee2a0;
    --ok-soft: #14301f;
    --chart-bar: #2fb5a5;
    --chart-grid: #263836;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.1rem; }
p { margin: 0; }
.muted { color: var(--muted); }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.shell { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex; flex-direction: column; gap: 22px;
  position: sticky; top: 0; height: 100vh;
}
.brand { display: block; padding: 0 2px; }
.brand-logo { width: 34px; height: 34px; border-radius: 9px; background: var(--primary); color: var(--on-primary); display: grid; place-items: center; }
.nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 9px;
  color: var(--text); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.nav svg { width: 18px; height: 18px; flex: none; }
.me { border-top: 1px solid var(--border); padding-top: 14px; display: grid; gap: 10px; }
.me-name { font-weight: 600; }
.me-role { color: var(--muted); font-size: .85rem; }

.content { padding: 28px 32px 48px; min-width: 0; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { color: var(--muted); margin-top: 4px; }

@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 10px 12px; gap: 8px; }
  .nav { flex-direction: row; flex-wrap: wrap; flex-basis: 100%; order: 3; }
  .nav a { padding: 8px 10px; }
  .me { border: 0; padding: 0; margin-left: auto; display: flex; align-items: center; gap: 10px; }
  .me-role { display: none; }
  .content { padding: 18px 16px 40px; }
}

/* ---------- Componentes ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 18px 20px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 16px 20px 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 16px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 5px 10px; font-size: .85rem; }
.btn.block { width: 100%; }
.icon-btn { border: 0; background: transparent; color: var(--muted); font-size: 1.1rem; cursor: pointer; padding: 6px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 9px; padding: 9px 12px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
input[type="checkbox"], input[type="radio"] { width: auto; }
textarea { resize: vertical; min-height: 70px; }
label.field { display: grid; gap: 5px; font-weight: 600; font-size: .88rem; }
label.field .hint { font-weight: 400; color: var(--muted); font-size: .8rem; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
@media (max-width: 560px) { .form-grid { grid-template-columns: 1fr; } }
.form-error { background: var(--danger-soft); color: var(--danger); padding: 10px 12px; border-radius: 9px; font-weight: 500; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
fieldset.box { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin: 0; display: grid; gap: 12px; }
fieldset.box legend { font-weight: 600; font-size: .88rem; padding: 0 6px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin-bottom: 16px; }
.toolbar .grow { flex: 1 1 240px; }
.toolbar label.field { min-width: 150px; }
.toolbar .spacer { flex: 1; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .78rem; font-weight: 600; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.bad { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.info { background: var(--primary-soft); color: var(--primary); border-color: transparent; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 11px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.is-blocked td { background: var(--danger-soft); }
tr.detail td { background: var(--surface-2); padding: 12px 14px 16px 40px; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
.empty { padding: 36px 20px; text-align: center; color: var(--muted); }
.cell-desc { color: var(--muted); font-size: .85rem; max-width: 320px; }
.actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }

.pager { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 16px; border-top: 1px solid var(--border); color: var(--muted); font-size: .88rem; }
.pager .btns { display: flex; gap: 8px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 18px; }
.tab { border: 0; background: transparent; font: inherit; font-weight: 600; color: var(--muted); padding: 10px 16px; cursor: pointer; border-bottom: 3px solid transparent; margin-bottom: -1px; }
.tab.active { color: var(--primary); border-color: var(--primary); }

/* Interruptor bloquear/desbloquear */
.switch { position: relative; display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-weight: 600; font-size: .85rem; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .track { width: 40px; height: 22px; border-radius: 999px; background: var(--ok); position: relative; transition: background .15s; flex: none; }
.switch .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + .track { background: var(--danger); }
.switch input:checked + .track::after { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- Dashboard ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin-bottom: 20px; }
.kpi { padding: 18px 20px; }
.kpi-label { color: var(--muted); font-size: .85rem; font-weight: 600; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.kpi-value { font-size: 1.85rem; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.kpi-sub { color: var(--muted); font-size: .82rem; margin-top: 2px; }
.dash-grid { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 16px; align-items: start; }
@media (max-width: 1050px) { .dash-grid { grid-template-columns: 1fr; } }
.chart-box { position: relative; height: 300px; padding: 12px 16px 16px; }

/* ---------- Generar venta ---------- */
.sell-grid { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 1000px) { .sell-grid { grid-template-columns: 1fr; } }
.result { display: grid; grid-template-columns: 1fr auto; gap: 6px 14px; padding: 12px 16px; border-bottom: 1px solid var(--border); align-items: center; }
.result:last-child { border-bottom: 0; }
.result-name { font-weight: 600; }
.result-meta { color: var(--muted); font-size: .85rem; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.result-add { display: flex; gap: 8px; align-items: center; }
.result-add input { width: 76px; text-align: center; }
.cart-line { padding: 12px 16px; border-bottom: 1px solid var(--border); display: grid; gap: 8px; }
.cart-line.over { background: var(--danger-soft); }
.cart-line-top { display: flex; justify-content: space-between; gap: 10px; align-items: start; }
.cart-line-name { font-weight: 600; }
.cart-line-bottom { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.qty { display: inline-flex; align-items: center; gap: 6px; }
.qty input { width: 72px; text-align: center; font-weight: 600; }
.qty button { width: 34px; height: 36px; padding: 0; }
.line-error { color: var(--danger); font-weight: 600; font-size: .85rem; }
.line-subtotal { font-weight: 700; font-variant-numeric: tabular-nums; }
.order-total { padding: 18px 20px; text-align: center; background: var(--primary-soft); border-radius: var(--radius) var(--radius) 0 0; }
.order-total .label { color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .06em; font-size: .78rem; }
.order-total .amount { font-size: 3rem; font-weight: 800; color: var(--primary); font-variant-numeric: tabular-nums; line-height: 1.1; margin-top: 4px; }
.pay { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.pay label { position: relative; }
.pay input { position: absolute; opacity: 0; }
.pay span { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 14px 10px; border: 2px solid var(--border); border-radius: 10px; font-weight: 700; cursor: pointer; background: var(--surface); }
.pay input:checked + span { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); }
.pay input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: 2px; }
.alert { padding: 10px 14px; border-radius: 9px; font-weight: 600; }
.alert.error { background: var(--danger-soft); color: var(--danger); }
.alert.info { background: var(--primary-soft); color: var(--primary); }
.stack { display: grid; gap: 14px; }

/* ---------- Auth ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 380px; padding: 32px 28px; }
.login-card .brand { padding: 0; margin-bottom: 6px; font-size: 1.4rem; }
.login-card p { color: var(--muted); margin-bottom: 22px; }

/* ---------- Modal y toasts ---------- */
dialog.modal { border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--surface); color: var(--text); width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 32px); box-shadow: 0 20px 50px rgba(0, 0, 0, .25); }
dialog.modal.wide { width: min(760px, calc(100vw - 24px)); }
dialog.modal::backdrop { background: rgba(8, 20, 18, .55); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 20px; overflow-y: auto; max-height: calc(100vh - 120px); }

#toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 10px; z-index: 100; max-width: min(420px, calc(100vw - 32px)); }
.toast { padding: 12px 16px; border-radius: 10px; background: var(--text); color: var(--bg); font-weight: 600; box-shadow: 0 8px 24px rgba(0, 0, 0, .25); animation: pop .15s ease-out; }
.toast.error { background: var(--danger); color: #fff; }
.toast.success { background: var(--ok); color: #fff; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .toast.error, :root:not([data-theme="light"]) .toast.success { color: #0d1514; } }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .toast { animation: none; } .switch .track, .switch .track::after { transition: none; } }

/* ---------- Corrección de ventas ---------- */
tr.is-deleted td { color: var(--muted); }
tr.is-deleted .order-amount { text-decoration: line-through; }
.history { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--border); display: grid; gap: 10px; font-size: .88rem; }
.history-item { display: grid; gap: 2px; }
.search-box { position: relative; }
.search-results { position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 5; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); max-height: 220px; overflow-y: auto; }
.search-item { display: flex; justify-content: space-between; gap: 10px; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--border); background: transparent; color: var(--text); font: inherit; padding: 10px 14px; cursor: pointer; }
.search-item:last-child { border-bottom: 0; }
.search-item:hover, .search-item:focus-visible { background: var(--primary-soft); }

/* ---------- Logo ---------- */
/* El logo tiene fondo blanco y letras negras: va sobre una ficha blanca para verse igual en modo claro y oscuro. */
.brand-img, .login-logo { display: block; height: auto; background: #fff; border-radius: 12px; }
.brand-img { width: 100%; max-width: 212px; padding: 8px 10px; }
.login-logo { width: min(300px, 100%); margin: 0 auto 16px; padding: 10px 14px; }
@media (max-width: 820px) { .brand-img { width: 150px; padding: 6px 8px; } }
.login-card > p { text-align: center; }
@media (max-width: 480px) {
  .brand-img { width: 128px; }
  .me-name { max-width: 115px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}
