:root {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #1b2333;
  --muted: #5e6b80;
  --border: #e1e6ee;
  --primary: #1f5fbf;
  --primary-dark: #184c99;
  --primary-soft: #e8f0fc;
  --ok: #17784a;
  --ok-soft: #e3f5ec;
  --warn: #a35f00;
  --warn-soft: #fff2dc;
  --danger: #c0302f;
  --danger-soft: #fde8e8;
  --partial: #7a3fb5;
  --partial-soft: #f1e8fb;
  --sidebar: #14213d;
  --sidebar-text: #c7d1e3;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: var(--font); font-size: 15px; color: var(--text); background: var(--bg); line-height: 1.45; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }
svg.icon { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.boot { padding: 40px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.hidden { display: none !important; }
.mono { font-family: Consolas, "Courier New", monospace; font-size: .9em; }
.pre { white-space: pre-line; }

/* ---------- layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; background: var(--sidebar); color: var(--sidebar-text);
  position: fixed; inset: 0 auto 0 0; display: flex; flex-direction: column; z-index: 50;
  transition: transform .2s ease;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 20px; color: #fff; font-weight: 700; font-size: 1.05rem; }
.brand svg.icon { width: 26px; height: 26px; color: #6ea8ff; }
.logo { width: 44px; height: 44px; border-radius: 10px; flex: none; object-fit: cover; box-shadow: 0 0 0 1px rgba(255, 255, 255, .12); }
.topbar-logo { display: none; width: 36px; height: 36px; border-radius: 8px; }
.nav { flex: 1; overflow-y: auto; padding: 6px 10px; }
.nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 8px;
  color: var(--sidebar-text); font-weight: 500; margin-bottom: 2px;
}
.nav a:hover { background: rgba(255, 255, 255, .07); text-decoration: none; color: #fff; }
.nav a.active { background: var(--primary); color: #fff; }
.nav .count { margin-left: auto; background: var(--danger); color: #fff; font-size: .72rem; border-radius: 10px; padding: 1px 7px; }
.side-user { border-top: 1px solid rgba(255, 255, 255, .1); padding: 14px 18px; font-size: .85rem; }
.side-user strong { color: #fff; display: block; }
.side-user button { margin-top: 10px; width: 100%; }

.main { flex: 1; margin-left: 240px; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 30; height: 58px; background: var(--surface);
  border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 12px; padding: 0 24px;
}
.topbar h1 { font-size: 1.15rem; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .menu-btn { display: none; }
.content { padding: 24px; max-width: 1400px; width: 100%; margin: 0 auto; }
.backdrop { position: fixed; inset: 0; background: rgba(10, 15, 30, .45); z-index: 45; }

.bell { position: relative; }
.bell .dot {
  position: absolute; top: 2px; right: 2px; background: var(--danger); color: #fff; font-size: .68rem;
  min-width: 18px; height: 18px; border-radius: 9px; display: grid; place-items: center; padding: 0 4px;
}
.notif-panel {
  position: absolute; right: 16px; top: 54px; width: min(380px, calc(100vw - 32px)); max-height: 70vh; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 10px 30px rgba(0, 0, 0, .15); z-index: 60;
}
.notif-panel header { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.notif-item { display: block; padding: 10px 14px; border-bottom: 1px solid var(--border); color: var(--text); }
.notif-item:hover { background: var(--surface-2); text-decoration: none; }
.notif-item.unread { background: var(--primary-soft); }
.notif-item .when { font-size: .78rem; color: var(--muted); }

/* ---------- componentes ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 500;
  cursor: pointer; white-space: nowrap; min-height: 38px;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn-success:hover { background: #12603b; }
.btn-danger { background: var(--surface); border-color: #f0b8b8; color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { padding: 4px 10px; min-height: 30px; font-size: .85rem; }
.btn-icon { padding: 6px; min-height: 36px; min-width: 36px; }
.sidebar .btn { background: transparent; color: var(--sidebar-text); border-color: rgba(255, 255, 255, .2); }
.sidebar .btn:hover { background: rgba(255, 255, 255, .08); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-body { padding: 18px; }
.card-header { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.stack > * + * { margin-top: 18px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin-bottom: 16px; }
.toolbar .grow { flex: 1 1 220px; }
.toolbar .spacer { flex: 1; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-info { background: var(--primary-soft); color: var(--primary); }
.badge-partial { background: var(--partial-soft); color: var(--partial); }
.badge-muted { background: #eceff3; color: var(--muted); }

.alert { padding: 12px 14px; border-radius: 8px; border: 1px solid; display: flex; gap: 10px; align-items: flex-start; }
.alert-warn { background: var(--warn-soft); border-color: #f3d39a; color: #6b3f00; }
.alert-danger { background: var(--danger-soft); border-color: #f2b9b9; color: #7d1d1d; }
.alert-info { background: var(--primary-soft); border-color: #bcd3f5; color: #173f7c; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; }
.kpi { padding: 16px 18px; display: flex; gap: 14px; align-items: center; color: var(--text); }
a.kpi:hover { text-decoration: none; border-color: var(--primary); }
.kpi .ico { width: 42px; height: 42px; border-radius: 10px; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); }
.kpi .ico.warn { background: var(--warn-soft); color: var(--warn); }
.kpi .ico.danger { background: var(--danger-soft); color: var(--danger); }
.kpi .ico.ok { background: var(--ok-soft); color: var(--ok); }
.kpi .val { font-size: 1.6rem; font-weight: 700; line-height: 1.1; }
.kpi .lbl { color: var(--muted); font-size: .85rem; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 18px; }

/* ---------- tabelas ---------- */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: var(--surface-2); font-weight: 600; white-space: nowrap; }
.table tbody tr:hover { background: #fafbfd; }
.table tr.clickable { cursor: pointer; }
.table td.num, .table th.num { text-align: right; white-space: nowrap; }
.table .empty { text-align: center; color: var(--muted); padding: 28px; }
.table tr.row-low td:first-child { box-shadow: inset 3px 0 0 var(--warn); }
.table tr.row-zero td:first-child { box-shadow: inset 3px 0 0 var(--danger); }
.qty-pos { color: var(--ok); font-weight: 600; }
.qty-neg { color: var(--danger); font-weight: 600; }
.thumb { width: 40px; height: 40px; border-radius: 6px; object-fit: cover; background: var(--surface-2); border: 1px solid var(--border); display: grid; place-items: center; color: #aab4c3; }
.thumb svg.icon { width: 20px; height: 20px; }
.actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.reversed { text-decoration: line-through; opacity: .6; }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; padding: 12px; }

/* ---------- formulários ---------- */
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field label, .label { font-size: .82rem; font-weight: 600; color: #3a4558; }
.field .hint { font-size: .78rem; color: var(--muted); }
.req::after { content: " *"; color: var(--danger); }
input, select, textarea {
  font: inherit; color: var(--text); background: #fff; border: 1px solid #cfd6e2; border-radius: 8px;
  padding: 8px 10px; min-height: 38px; width: 100%;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(31, 95, 191, .25); border-color: var(--primary); }
input[type=checkbox] { width: 18px; height: 18px; min-height: 0; accent-color: var(--primary); }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 14px; }
.form-grid .span-2 { grid-column: span 2; }
.form-grid .span-all { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin-top: 18px; }

.items { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.item-row { display: grid; gap: 10px; padding: 12px; border-bottom: 1px solid var(--border); align-items: end; background: var(--surface); }
.item-row:last-child { border-bottom: 0; }
.item-row.entry { grid-template-columns: minmax(220px, 3fr) minmax(90px, 1fr) minmax(130px, 1.4fr) auto; }
.item-row.order { grid-template-columns: minmax(240px, 4fr) minmax(110px, 1fr) auto; }
.item-row .info { grid-column: 1 / -1; font-size: .82rem; }
.items-footer { padding: 10px 12px; background: var(--surface-2); }

.photo-box { display: flex; gap: 14px; align-items: center; }
.photo-box img, .photo-box .thumb { width: 84px; height: 84px; }
.select-add { display: flex; gap: 6px; }
.select-add select, .select-add input { flex: 1; min-width: 0; }
.photo-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.field .photo-actions label.btn { cursor: pointer; color: var(--text); font-weight: 500; }
.field .photo-actions label.btn-primary { color: #fff; }

/* ---------- detalhe ---------- */
.info-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px 22px; }
.info-grid dt { font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.info-grid dd { margin: 2px 0 0; font-weight: 500; word-break: break-word; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 16px 22px; border-left: 2px solid var(--border); margin-left: 6px; }
.timeline li:last-child { border-left-color: transparent; padding-bottom: 0; }
.timeline li::before { content: ""; position: absolute; left: -7px; top: 3px; width: 12px; height: 12px; border-radius: 50%; background: var(--primary); border: 2px solid #fff; box-shadow: 0 0 0 1px var(--border); }
.timeline .when { font-size: .8rem; color: var(--muted); }
.product-head { display: flex; gap: 18px; align-items: flex-start; flex-wrap: wrap; }
.product-head img, .product-head .thumb { width: 120px; height: 120px; border-radius: 10px; }
.big-balance { font-size: 2rem; font-weight: 700; }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs button { border: 0; background: none; padding: 10px 14px; font: inherit; font-weight: 500; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); }

.report-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin-bottom: 18px; }
.report-card { text-align: left; cursor: pointer; padding: 14px 16px; border: 1px solid var(--border); background: var(--surface); border-radius: var(--radius); font: inherit; color: var(--text); }
.report-card strong { display: block; margin-bottom: 4px; }
.report-card span { font-size: .85rem; color: var(--muted); }
.report-card.active { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(31, 95, 191, .2); }

details.json summary { cursor: pointer; color: var(--primary); font-size: .85rem; }
details.json pre { background: var(--surface-2); padding: 8px; border-radius: 6px; font-size: .78rem; white-space: pre-wrap; max-width: 520px; }

/* ---------- modal e toast ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(10, 15, 30, .5); z-index: 100; display: grid; place-items: center; padding: 16px; }
.modal { background: var(--surface); border-radius: 12px; width: min(560px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(0, 0, 0, .25); }
.modal.wide { width: min(860px, 100%); }
.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; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }

#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 200; display: flex; flex-direction: column; gap: 8px; max-width: calc(100vw - 32px); }
.toast { background: #1b2333; color: #fff; padding: 12px 16px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, .2); max-width: 420px; white-space: pre-line; animation: in .2s ease; }
.toast.ok { background: var(--ok); }
.toast.error { background: var(--danger); }
.toast.warn { background: var(--warn); }
@keyframes in { from { transform: translateY(10px); opacity: 0; } }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, #14213d 0%, #1f5fbf 100%); }
.login { width: min(400px, 100%); padding: 32px 28px; }
.login .brand { color: var(--text); padding: 0 0 6px; font-size: 1.3rem; justify-content: center; }
.login-logo { display: block; width: 120px; height: 120px; margin: 0 auto 16px; border-radius: 20px; box-shadow: 0 6px 20px rgba(0, 0, 0, .25); }
.login > p.muted { text-align: center; }
.login .brand svg.icon { color: var(--primary); }
.login form > * + * { margin-top: 14px; }
.login .btn { width: 100%; }
.password-wrap { position: relative; }
.password-wrap input { padding-right: 44px; }
.login .password-wrap .btn { position: absolute; top: 50%; right: 2px; transform: translateY(-50%); width: auto; min-height: 34px; min-width: 34px; color: var(--muted); }

/* ---------- responsivo ---------- */
@media (max-width: 960px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .4); }
  .main { margin-left: 0; }
  .topbar .menu-btn { display: inline-flex; }
  .topbar-logo { display: block; }
  .topbar { padding: 0 12px; }
  .content { padding: 16px; }
  .grid-2 { grid-template-columns: 1fr; }
  .item-row.entry, .item-row.order { grid-template-columns: 1fr 1fr; }
  .item-row .product-field { grid-column: 1 / -1; }
}

@media (max-width: 700px) {
  .form-grid .span-2 { grid-column: auto; }
  .kpis { grid-template-columns: 1fr 1fr; }
  .kpi { padding: 12px; gap: 10px; }
  .kpi .ico { width: 34px; height: 34px; }
  .kpi .val { font-size: 1.3rem; }
  .toolbar > * { flex: 1 1 140px; }
  /* tabelas viram cartões */
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr { border-bottom: 1px solid var(--border); padding: 8px 4px; }
  table.cards td { border: 0; padding: 4px 10px; text-align: right; overflow: hidden; }
  table.cards td::before { content: attr(data-label); float: left; margin-right: 12px; font-weight: 600; color: var(--muted); font-size: .8rem; text-align: left; }
  table.cards td[data-label=""]::before { content: none; }
  table.cards td.num { text-align: right; }
  table.cards td .actions { justify-content: flex-end; }
  table.cards td.empty { display: block; text-align: center; }
  table.cards td.empty::before { content: none; }
  .modal-bg { padding: 0; place-items: end stretch; }
  .modal, .modal.wide { width: 100%; border-radius: 14px 14px 0 0; max-height: 92vh; }
  .item-row.entry, .item-row.order { grid-template-columns: 1fr 1fr; }
}

/* ---------- impressão ---------- */
.print-only { display: none; }
@media print {
  body { background: #fff; font-size: 12px; }
  .sidebar, .topbar, .no-print, .btn, #toasts { display: none !important; }
  .main { margin: 0; }
  .content { padding: 0; max-width: none; }
  .card { box-shadow: none; border-color: #ccc; }
  .print-only { display: block; }
}
