/* ---------- Base ---------- */
:root {
  --sidebar-bg: #21263c;
  --sidebar-fg: #aab0c6;
  --sidebar-active: #ffffff;
  --sidebar-accent: #408dfb;
  --accent: #408dfb;
  --accent-dark: #2f78e0;
  --bg: #f4f6f8;
  --panel: #ffffff;
  --border: #e3e6ec;
  --text: #21263c;
  --muted: #7a8099;
  --green: #2eb672;
  --orange: #f0913a;
  --red: #e5484d;
  --yellow: #d8a800;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(33, 38, 60, 0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Auth ---------- */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #21263c 0%, #2c3352 60%, #345 100%);
}
.auth-card {
  background: #fff;
  border-radius: 12px;
  padding: 36px;
  width: 380px;
  max-width: 92vw;
  box-shadow: 0 10px 40px rgba(0,0,0,.25);
}
.auth-logo { font-size: 22px; font-weight: 700; margin-bottom: 18px; }
.auth-card h2 { margin-bottom: 4px; }
.auth-card p { margin-bottom: 18px; }
.auth-card label { display: block; margin-bottom: 12px; font-weight: 600; font-size: 13px; }
.auth-card input {
  display: block; width: 100%; margin-top: 5px; padding: 9px 11px;
  border: 1px solid var(--border); border-radius: 6px; font-size: 14px;
}
.auth-card input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.auth-error {
  background: #fdecec; color: var(--red); border-radius: 6px;
  padding: 8px 11px; margin-bottom: 12px; font-size: 13px;
}

/* ---------- Layout ---------- */
.app-shell { display: flex; height: 100vh; overflow: hidden; }
.sidebar {
  width: 220px; min-width: 220px; background: var(--sidebar-bg);
  color: var(--sidebar-fg); display: flex; flex-direction: column; overflow-y: auto;
}
.sidebar-brand {
  color: #fff; font-weight: 700; font-size: 16px; padding: 18px 16px 14px;
  border-bottom: 1px solid rgba(255,255,255,.08); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.sidebar-nav { padding: 8px 0 24px; }
.nav-section-title {
  padding: 14px 16px 6px; font-size: 11px; text-transform: uppercase;
  letter-spacing: .08em; color: #6d7391; font-weight: 700;
}
.nav-link {
  display: block; padding: 8px 16px 8px 22px; color: var(--sidebar-fg);
  font-size: 13.5px; cursor: pointer; border-left: 3px solid transparent;
}
.nav-link:hover { color: #fff; background: rgba(255,255,255,.05); text-decoration: none; }
.nav-link.active {
  color: var(--sidebar-active); background: rgba(64,141,251,.12);
  border-left-color: var(--sidebar-accent); font-weight: 600;
}
.nav-link.top-level { padding-left: 16px; font-weight: 600; }

.main-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: 52px; background: var(--panel); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; padding: 0 18px; gap: 14px;
}
.topbar-search { flex: 1; max-width: 420px; }
.topbar-search input {
  width: 100%; padding: 7px 12px; border: 1px solid var(--border);
  border-radius: 20px; background: var(--bg); font-size: 13px;
}
.topbar-search input:focus { outline: 2px solid var(--accent); background: #fff; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.topbar-user {
  width: 30px; height: 30px; background: var(--accent); border-radius: 50%;
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 13px; cursor: default;
}
.main-content { flex: 1; overflow-y: auto; padding: 20px 24px 60px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid transparent;
  border-radius: 6px; padding: 8px 14px; font-size: 13.5px; font-weight: 600;
  cursor: pointer; background: #fff; color: var(--text); white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-dark); }
.btn-secondary { border-color: var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-danger { background: var(--red); color: #fff; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); }
.btn-sm { padding: 5px 11px; font-size: 12.5px; }
.btn-block { width: 100%; justify-content: center; }

/* ---------- Page header ---------- */
.page-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px; gap: 12px; flex-wrap: wrap;
}
.page-header h1 { font-size: 20px; font-weight: 700; }
.page-header .subtitle { font-size: 13px; color: var(--muted); margin-top: 2px; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Filter bar ---------- */
.filter-bar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; align-items: center; }
.filter-bar input[type="search"], .filter-bar select, .filter-bar input[type="date"] {
  padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; background: #fff;
}
.chip {
  padding: 5px 12px; border-radius: 16px; border: 1px solid var(--border);
  background: #fff; font-size: 12.5px; cursor: pointer; font-weight: 600; color: var(--muted);
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- Tables ---------- */
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.table-wrap { overflow-x: auto; }
table.data-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.data-table th {
  text-align: left; padding: 10px 14px; background: #fafbfc; color: var(--muted);
  font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em;
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
.data-table td { padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr.clickable { cursor: pointer; }
.data-table tbody tr.clickable:hover { background: #f6f9ff; }
.data-table td.num, .data-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.empty-state { padding: 48px 20px; text-align: center; color: var(--muted); }
.empty-state .big { font-size: 40px; margin-bottom: 10px; }

/* ---------- Status badges ---------- */
.badge {
  display: inline-block; padding: 3px 9px; border-radius: 10px;
  font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
}
.badge-gray { background: #eef0f4; color: var(--muted); }
.badge-blue { background: #e3edfd; color: var(--accent-dark); }
.badge-green { background: #e0f5ea; color: #1d8a55; }
.badge-orange { background: #fdeedd; color: #c76f1b; }
.badge-red { background: #fdecec; color: var(--red); }
.badge-yellow { background: #faf3d1; color: #93790a; }

/* ---------- Forms ---------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px 18px; }
.form-grid .full { grid-column: 1 / -1; }
.field label { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 5px; color: var(--text); }
.field label .req { color: var(--red); }
.field input, .field select, .field textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 6px; font-size: 13.5px; background: #fff; font-family: inherit;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--accent); border-color: transparent; }
.field textarea { resize: vertical; min-height: 64px; }
.form-section { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.form-section h3 { font-size: 14px; margin-bottom: 14px; color: var(--text); }
.form-footer { display: flex; gap: 10px; justify-content: flex-end; padding: 6px 0 30px; }

/* ---------- Line item editor ---------- */
.line-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.line-table th { text-align: left; padding: 8px 8px; font-size: 11.5px; color: var(--muted); text-transform: uppercase; border-bottom: 1px solid var(--border); }
.line-table td { padding: 6px 8px 6px 0; vertical-align: top; }
.line-table input, .line-table select { width: 100%; padding: 7px 9px; border: 1px solid var(--border); border-radius: 6px; font-size: 13.5px; }
.line-table .num-col { width: 92px; }
.line-table .amount-col { width: 110px; text-align: right; padding-top: 13px; font-variant-numeric: tabular-nums; }
.line-remove { color: var(--red); background: none; border: none; cursor: pointer; font-size: 16px; padding: 8px 4px; }
.totals-box { margin-left: auto; width: 320px; max-width: 100%; margin-top: 12px; }
.totals-box .row { display: flex; justify-content: space-between; padding: 6px 0; font-size: 13.5px; }
.totals-box .row.total { border-top: 1px solid var(--border); font-weight: 700; font-size: 15px; padding-top: 10px; margin-top: 4px; }
.totals-box input { width: 110px; padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; text-align: right; }

/* ---------- Detail view ---------- */
.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 14px; flex-wrap: wrap; margin-bottom: 14px; }
.detail-head h1 { font-size: 20px; }
.detail-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; align-items: start; }
@media (max-width: 900px) { .detail-grid { grid-template-columns: 1fr; } }
.kv-list { font-size: 13.5px; }
.kv-list .kv { display: flex; padding: 7px 0; border-bottom: 1px dashed var(--border); }
.kv-list .kv:last-child { border-bottom: none; }
.kv-list .k { width: 46%; color: var(--muted); }
.kv-list .v { width: 54%; font-weight: 500; word-break: break-word; }
.doc-panel { padding: 18px; }
.doc-panel h3 { font-size: 13px; text-transform: uppercase; color: var(--muted); letter-spacing: .05em; margin-bottom: 10px; }

/* ---------- Dashboard ---------- */
.dash-grid { display: grid; grid-template-columns: repeat(12, 1fr); gap: 16px; }
.dash-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.dash-card h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 14px; }
.span-12 { grid-column: span 12; }
.span-8 { grid-column: span 8; }
.span-6 { grid-column: span 6; }
.span-4 { grid-column: span 4; }
.span-3 { grid-column: span 3; }
.span-2 { grid-column: span 2; }
@media (max-width: 1100px) { .span-8, .span-6, .span-4 { grid-column: span 12; } .span-3, .span-2 { grid-column: span 4; } }
@media (max-width: 720px) { .span-3, .span-2 { grid-column: span 6; } }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }

/* ---------- Permission matrix ---------- */
.matrix-tools { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.matrix-table th.num, .matrix-table td.num { text-align: center; }
.matrix-table input[type=checkbox] { width: 17px; height: 17px; cursor: pointer; }
.matrix-group td { background: #f4f6f8; font-weight: 700; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.stat-row { display: flex; flex-wrap: wrap; gap: 0; }
.stat {
  flex: 1; min-width: 120px; text-align: center; padding: 8px 12px;
  border-right: 1px solid var(--border);
}
.stat:last-child { border-right: none; }
.stat .value { font-size: 26px; font-weight: 700; color: var(--accent); }
.stat .value.green { color: var(--green); }
.stat .value.orange { color: var(--orange); }
.stat .value.red { color: var(--red); }
.stat .label { font-size: 12px; color: var(--muted); margin-top: 4px; text-transform: uppercase; letter-spacing: .04em; }
.bar-chart { display: flex; align-items: flex-end; gap: 18px; height: 180px; padding: 10px 6px 0; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; }
.bar-col .bar { width: 70%; max-width: 54px; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 2px; }
.bar-col .bar-label { font-size: 11.5px; color: var(--muted); margin-top: 6px; }
.bar-col .bar-value { font-size: 11px; color: var(--text); margin-bottom: 4px; font-variant-numeric: tabular-nums; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20, 24, 40, .5);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 60px 16px; z-index: 100; overflow-y: auto;
}
.modal {
  background: #fff; border-radius: 10px; width: 560px; max-width: 100%;
  box-shadow: 0 18px 60px rgba(0,0,0,.3); animation: modal-in .15s ease-out;
}
.modal.wide { width: 760px; }
@keyframes modal-in { from { transform: translateY(-8px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-header { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-header h2 { font-size: 16px; }
.modal-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--muted); }
.modal-body { padding: 20px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--border); }

/* ---------- Toasts ---------- */
.toast-root { position: fixed; bottom: 22px; right: 22px; z-index: 200; display: flex; flex-direction: column; gap: 10px; }
.toast {
  background: #21263c; color: #fff; padding: 11px 16px; border-radius: 8px;
  font-size: 13.5px; box-shadow: 0 6px 24px rgba(0,0,0,.25); max-width: 360px;
  animation: toast-in .2s ease-out;
}
.toast.error { background: var(--red); }
.toast.success { background: var(--green); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- Barcode / scanning ---------- */
.scan-box { display: flex; flex-direction: column; gap: 12px; }
.scan-video { width: 100%; max-height: 260px; background: #000; border-radius: 8px; object-fit: cover; }
.barcode-mini { margin-top: 14px; text-align: center; }
.barcode-mini svg { max-width: 100%; height: auto; }
.label-preview { display: flex; justify-content: center; padding: 10px; }
.label-card {
  border: 1px dashed var(--border); border-radius: 8px; padding: 16px 22px;
  text-align: center; background: #fff; min-width: 260px;
}
.label-card .label-name { font-weight: 700; font-size: 15px; }
.label-card .label-sku { color: var(--muted); font-size: 12px; margin: 3px 0; }
.label-card .label-bc { margin: 8px 0; }
.label-card .label-bc svg { max-width: 100%; height: auto; }
.label-card .label-price { font-weight: 700; font-size: 17px; }

/* ---------- Attachments ---------- */
.attach-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.attach-row {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px; background: #fafbfc;
}
.attach-icon { font-size: 20px; }
.attach-meta { flex: 1; min-width: 0; }
.attach-name { font-weight: 600; word-break: break-word; }
.attach-del { border: none; background: none; color: var(--red); cursor: pointer; font-size: 15px; padding: 4px 8px; }
.attach-upload { display: inline-flex; align-items: center; cursor: pointer; }

/* ---------- Email preview ---------- */
.email-head { border-bottom: 1px solid var(--border); padding-bottom: 12px; margin-bottom: 12px; font-size: 13.5px; }
.email-head > div { padding: 2px 0; }
.email-body {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px;
  white-space: pre-wrap; word-break: break-word; background: var(--bg);
  padding: 14px; border-radius: 8px; line-height: 1.5;
}

/* ---------- Document templates ---------- */
.tpl-layout { display: grid; grid-template-columns: 340px 1fr; gap: 18px; align-items: start; }
@media (max-width: 900px) { .tpl-layout { grid-template-columns: 1fr; } }
.tpl-side { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.tpl-logo { margin: 12px 0; }
.tpl-logo-box { border: 1px dashed var(--border); border-radius: 8px; padding: 12px; text-align: center; min-height: 60px; display: flex; align-items: center; justify-content: center; }
.tpl-logo-box img { max-height: 60px; max-width: 100%; }
.tpl-group { margin-top: 14px; }
.tpl-group-title { font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 700; margin-bottom: 6px; }
.tpl-check { display: flex; align-items: center; gap: 8px; padding: 4px 0; font-size: 13px; }
.tpl-check input { width: auto; }
.tpl-preview-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.tpl-preview { width: 100%; height: 900px; border: 1px solid var(--border); border-radius: 6px; background: #fff; }

/* ---------- Misc ---------- */
.stock-pill { font-weight: 700; }
.stock-pill.low { color: var(--red); }
.text-right { text-align: right; }
.mb-16 { margin-bottom: 16px; }
.report-header-flex { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; margin-bottom: 12px; }
/* Hamburger is hidden on desktop; shown on small screens */
.hamburger { display: none; background: none; border: none; font-size: 22px; cursor: pointer; color: var(--text); padding: 4px 8px; }
.sidebar-backdrop { display: none; }

@media (max-width: 820px) {
  .hamburger { display: block; }
  /* Off-canvas drawer */
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 120;
    transform: translateX(-100%); transition: transform .22s ease; width: 258px; min-width: 258px;
  }
  .app-shell.drawer-open .sidebar { transform: translateX(0); box-shadow: 4px 0 24px rgba(0,0,0,.3); }
  .app-shell.drawer-open .sidebar-backdrop {
    display: block; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 110;
  }
  .main-content { padding: 14px; }
  .topbar { padding: 0 12px; gap: 8px; }
  .topbar-search { max-width: none; }
  .page-header h1 { font-size: 18px; }
  /* Comfortable touch targets */
  .btn { padding: 10px 14px; }
  .btn-sm { padding: 7px 11px; }
  .nav-link { padding: 12px 16px 12px 22px; }
  .field input, .field select, .field textarea { padding: 11px 12px; font-size: 15px; }
  .modal { width: 100%; }
  .modal-backdrop { padding: 12px; }
  .detail-head { flex-direction: column; }
  .page-actions { width: 100%; }
  .page-actions .btn { flex: 1; justify-content: center; }
}
@media (max-width: 520px) {
  .stat { min-width: 50%; }
  .data-table th, .data-table td { padding: 9px 8px; }
  .totals-box { width: 100%; }
}
