:root {
  --bg: #f4f6f9; --panel: #fff; --text: #1f2937; --muted: #6b7280; --line: #e5e7eb;
  --brand: #1d4ed8; --brand-dark: #1e3a8a; --danger: #dc2626; --ok: #059669; --warn: #d97706;
  --sample: #b45309;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; font: 14px/1.45 "Segoe UI", system-ui, sans-serif; color: var(--text); background: var(--bg); display: flex; min-height: 100vh; }
a { color: var(--brand); text-decoration: none; }

.sidebar { width: 230px; background: var(--brand-dark); color: #fff; padding: 18px 12px; flex-shrink: 0; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; margin-bottom: 22px; line-height: 1.2; }
.brand img { width: 40px; height: 40px; object-fit: contain; background: #fff; border-radius: 6px; padding: 2px; }
.brand img:not([src]), .brand img[src=""] { display: none; }
.sidebar nav a { display: block; color: #dbe4ff; padding: 9px 12px; border-radius: 6px; margin-bottom: 2px; }
.sidebar nav a:hover { background: rgba(255,255,255,.08); }
.sidebar nav a.active { background: rgba(255,255,255,.18); color: #fff; font-weight: 600; }

main { flex: 1; padding: 24px 28px 60px; min-width: 0; }
h1 { font-size: 22px; margin: 0 0 18px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
h2 { font-size: 15px; margin: 0 0 12px; }
h1 .btn { font-size: 14px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 18px; margin-bottom: 18px; }
.muted { color: var(--muted); }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }

.grid { display: grid; gap: 12px 14px; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
.grid.two { grid-template-columns: 1fr 1fr; }
label { display: block; font-size: 12px; font-weight: 600; color: #374151; }
label > input, label > select, label > textarea { margin-top: 4px; }
input, select, textarea { width: 100%; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 6px; font: inherit; background: #fff; color: inherit; }
input:focus, select:focus, textarea:focus { outline: 2px solid #93c5fd; border-color: var(--brand); }
textarea { resize: vertical; min-height: 60px; }
input[type=checkbox], input[type=radio] { width: auto; margin-right: 6px; }
.check { display: flex; align-items: center; font-weight: 500; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 6px; border: 1px solid #d1d5db; background: #fff; color: var(--text); font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; }
.btn:hover { background: #f3f4f6; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--danger); border-color: #fca5a5; }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: default; }

table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: #f9fafb; }
td.num, th.num { text-align: right; }
tr.click { cursor: pointer; }
tr.click:hover td { background: #f8fafc; }
.table-wrap { overflow-x: auto; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .3px; }
.badge.original { background: #dbeafe; color: #1e40af; }
.badge.sample { background: #fef3c7; color: var(--sample); }
.badge.unpaid { background: #fee2e2; color: #b91c1c; }
.badge.paid { background: #d1fae5; color: #065f46; }
.badge.cancelled { background: #e5e7eb; color: #4b5563; text-decoration: line-through; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 18px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 16px; }
.card .k { font-size: 12px; color: var(--muted); font-weight: 600; }
.card .v { font-size: 22px; font-weight: 700; margin-top: 4px; }
.card .s { font-size: 12px; color: var(--muted); }

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

/* bill form */
table.items { min-width: 960px; }
.items td { padding: 4px; border-bottom: 0; }
.items td input { padding: 6px 8px; }
.items .sn { width: 36px; text-align: center; color: var(--muted); padding-top: 10px; }
.items .amt { text-align: right; padding-top: 10px; font-weight: 600; white-space: nowrap; }
.totals { width: 330px; margin-left: auto; }
.totals td { border: 0; padding: 4px 8px; }
.totals tr.grand td { font-size: 17px; font-weight: 700; border-top: 2px solid var(--text); padding-top: 8px; }
.words { font-style: italic; color: var(--muted); margin-top: 6px; text-align: right; }
.type-banner { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; font-weight: 600; }
.type-banner.original { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }
.type-banner.sample { background: #fffbeb; color: var(--sample); border: 1px solid #fde68a; }
.helper { font-size: 11px; color: var(--muted); }
.sticky-actions { position: sticky; bottom: 0; background: var(--bg); padding: 12px 0; display: flex; gap: 10px; justify-content: flex-end; border-top: 1px solid var(--line); }

/* view */
.view-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 18px; align-items: start; }
.pdf-frame { width: 100%; height: calc(100vh - 150px); min-height: 600px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.side-list { list-style: none; padding: 0; margin: 0; font-size: 13px; }
.side-list li { padding: 8px 0; border-bottom: 1px solid var(--line); }
.side-list li:last-child { border: 0; }
.actions-col { display: flex; flex-direction: column; gap: 8px; }
.actions-col .btn { justify-content: center; }

/* modal + toast */
.modal-bg { position: fixed; inset: 0; background: rgba(15, 23, 42, .5); display: flex; align-items: flex-start; justify-content: center; padding: 50px 16px; z-index: 50; overflow-y: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 560px; padding: 22px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal.wide { max-width: 1000px; }
.modal h2 { font-size: 18px; }
.modal .foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
#toast { position: fixed; bottom: 20px; right: 20px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
#toast div { background: #111827; color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.25); max-width: 380px; }
#toast div.err { background: var(--danger); }

/* ---- login & users ---- */
body.locked .sidebar, body.locked main { display: none; }
#auth:empty { display: none; }
#auth { flex: 1; min-height: 100vh; display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px 40px;
  background: linear-gradient(160deg, var(--brand-dark) 0%, #2746a8 55%, var(--bg) 55%); }
.auth-card { background: #fff; width: 100%; max-width: 430px; border-radius: 14px; padding: 26px 28px 18px; box-shadow: 0 20px 50px rgba(15,23,42,.25); }
.auth-card h1 { font-size: 22px; margin: 18px 0 6px; }
.auth-card form label { margin-top: 12px; }
.auth-brand { display: flex; align-items: center; gap: 12px; font-weight: 700; line-height: 1.2; color: var(--brand-dark); }
.auth-brand img { width: 46px; height: 46px; object-fit: contain; }
.auth-brand span { display: block; font-weight: 500; font-size: 12px; color: var(--muted); }
.btn.wide { width: 100%; justify-content: center; margin-top: 16px; padding: 10px; font-size: 15px; }
button.link { background: none; border: 0; padding: 0; color: var(--brand); font: inherit; cursor: pointer; text-decoration: underline; }
form button.link { display: block; margin: 12px auto 0; }
.auth-err { background: #fef2f2; border: 1px solid #fca5a5; color: #991b1b; border-radius: 8px; padding: 8px 12px; margin-top: 12px; font-weight: 600; font-size: 13px; }
.auth-alt { margin-top: 16px; font-size: 13px; border-top: 1px solid var(--line); padding-top: 12px; }
.auth-alt ul { margin: 4px 0 8px; padding-left: 18px; }
.auth-foot { display: flex; justify-content: space-between; margin-top: 18px; font-size: 12px; color: var(--muted); }
.pw-wrap { position: relative; display: block; margin-top: 4px; }
.pw-wrap input { margin-top: 0; padding-right: 58px; }
.pw-show { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); border: 0; background: #f1f5f9; border-radius: 4px; font-size: 11px; padding: 3px 7px; cursor: pointer; color: var(--muted); }
.recovery-code { font-family: Consolas, monospace; font-size: 24px; font-weight: 700; letter-spacing: 2px; text-align: center; background: #fffbeb;
  border: 2px dashed #f59e0b; border-radius: 10px; padding: 14px; margin: 14px 0; color: #78350f; }
.recovery-code.small { font-size: 16px; padding: 4px 10px; margin: 0; display: inline-block; }
.code-input { font-family: Consolas, monospace; font-size: 18px; letter-spacing: 3px; }
table.cred td { border: 0; padding: 6px 8px; }
.user-box { margin-top: 18px; border-top: 1px solid rgba(255,255,255,.15); padding-top: 14px; }
.user-name { display: flex; gap: 10px; align-items: center; font-size: 13px; }
.user-name #userRole { font-size: 11px; color: #c7d2fe; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: rgba(255,255,255,.2); display: grid; place-items: center; font-weight: 700; font-size: 13px; flex-shrink: 0; }
.user-actions { display: flex; gap: 6px; margin-top: 10px; }
.user-actions button { flex: 1; background: rgba(255,255,255,.1); border: 1px solid rgba(255,255,255,.25); color: #fff; border-radius: 6px; padding: 6px; font: inherit; font-size: 12px; cursor: pointer; }
.user-actions button:hover { background: rgba(255,255,255,.2); }
body.staff .admin-only, body.staff [data-wizard] { display: none !important; }
body:not(.super) .super-only, body.no-company .co-only { display: none !important; }
.super-banner { background: #eef2ff; border: 1px solid #c7d2fe; color: #1e3a8a; border-radius: 8px; padding: 8px 14px; margin-bottom: 14px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.auth-foot span { color: var(--muted); }
.wz-steps { display: flex; gap: 6px; flex-wrap: wrap; margin: -4px 0 16px; font-size: 12px; font-weight: 600; }
.wz-steps span { padding: 4px 10px; border-radius: 999px; background: #f1f5f9; color: var(--muted); }
.wz-steps span.on { background: var(--brand); color: #fff; }
.wz-steps span.done { background: #d1fae5; color: #065f46; }
.wz-choices { display: grid; gap: 10px; }
.wz-choice { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 14px 16px; border: 2px solid var(--line); border-radius: 10px; background: #fff; font: inherit; text-align: left; cursor: pointer; }
.wz-choice:hover, .wz-choice.on { border-color: var(--brand); background: #eff6ff; }
.wz-help { background: #f8fafc; border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; margin-top: 12px; font-size: 13px; }
.wz-help ol { margin: 6px 0; padding-left: 20px; }
.wz-help li { margin-bottom: 6px; }
.em-table td { vertical-align: middle; }
.em-ok { color: var(--ok); font-weight: 700; font-size: 12px; }
h3.sub { font-size: 13px; margin: 18px 0 10px; color: var(--brand-dark); text-transform: uppercase; letter-spacing: .4px; }
h3.sub:first-of-type { margin-top: 4px; }
details.help { background: #f8fafc; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; margin-bottom: 12px; }
details.help summary { cursor: pointer; font-weight: 600; color: var(--brand); }
details.help ol { margin: 8px 0 4px; padding-left: 20px; }
details.help li { margin-bottom: 4px; }
.close-app { margin-top: 18px; width: 100%; padding: 9px 12px; border-radius: 6px; border: 1px solid rgba(255,255,255,.3); background: transparent; color: #fecaca; font: inherit; font-weight: 600; cursor: pointer; text-align: left; }
.close-app:hover { background: rgba(255,255,255,.08); }
.closed-screen { margin: 12vh auto; max-width: 480px; text-align: center; padding: 0 16px; }
.closed-screen img:not([src]), .closed-screen img[src=""] { display: none; }
.closed-screen h1 { justify-content: center; }
.warn-banner { background: #fef2f2; border: 1px solid #fca5a5; color: #991b1b; padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; font-weight: 600; }
.bal-in { color: var(--ok); font-weight: 700; }
.bal-out { color: var(--danger); font-weight: 700; }
.badge.kind { background: #ede9fe; color: #5b21b6; text-transform: none; }
tfoot th { background: #f9fafb; color: var(--text); border-top: 2px solid var(--line); }
.print-only { display: none; }
@media print {
  .sidebar, .no-print, #toast { display: none !important; }
  .print-only { display: block; margin-bottom: 10px; }
  body { background: #fff; display: block; }
  main { padding: 0; }
  .panel, .card { border-color: #ccc; break-inside: avoid; }
  .view-layout { display: block; }
}
.img-prev { max-height: 70px; max-width: 200px; border: 1px dashed #cbd5e1; padding: 4px; border-radius: 6px; background: #fff; display: block; margin: 6px 0; }
.empty { text-align: center; color: var(--muted); padding: 30px; }

@media (max-width: 900px) {
  body { flex-direction: column; }
  .sidebar { width: auto; height: auto; position: static; }
  .sidebar nav { display: flex; flex-wrap: wrap; gap: 4px; }
  .view-layout, .grid.two { grid-template-columns: 1fr; }
  main { padding: 16px; }
  .totals { width: 100%; }
}
