:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-600: #1d4ed8;
  --primary-soft: #eff6ff;
  --danger: #dc2626;
  --success: #16a34a;
  --warning: #d97706;
  --sidebar: #0f172a;
  --sidebar-text: #cbd5e1;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 3px rgba(15, 23, 42, .08);
  --shadow-lg: 0 10px 30px rgba(15, 23, 42, .18);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #0f172a;
    --border: #1f2937;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --primary: #3b82f6;
    --primary-600: #60a5fa;
    --primary-soft: #172554;
    --sidebar: #020617;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.hidden { display: none !important; }
.break { word-break: break-word; overflow-wrap: anywhere; }

/* ── Layout ─────────────────────────────────────── */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 232px; flex-shrink: 0; background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 18px 14px; color: #fff; font-weight: 700; font-size: 15px; }
.brand-logo { width: 32px; height: 32px; border-radius: 9px; background: var(--primary); display: grid; place-items: center; font-size: 13px; }
.nav { display: flex; flex-direction: column; padding: 8px; gap: 2px; flex: 1; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px;
  color: var(--sidebar-text); font-weight: 500;
}
.nav a:hover { background: rgba(255,255,255,.06); text-decoration: none; color: #fff; }
.nav a.active { background: rgba(59,130,246,.18); color: #fff; }
.nav svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav .badge-count { margin-left: auto; background: var(--primary); color: #fff; border-radius: 999px; font-size: 11px; padding: 1px 7px; }
.sidebar-foot { padding: 14px 18px; border-top: 1px solid rgba(255,255,255,.08); font-size: 12px; }
.sidebar-foot .who { color: #fff; font-weight: 600; }
.sidebar-foot button { margin-top: 8px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: none; position: sticky; top: 0; z-index: 30; background: var(--sidebar); color: #fff;
  padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top)); align-items: center; gap: 10px;
}
.topbar .title { font-weight: 700; flex: 1; }
.content { padding: 24px; max-width: 1400px; width: 100%; margin: 0 auto; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }

.bottom-nav { display: none; }

/* ── Components ─────────────────────────────────── */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.card-body { padding: 16px; }
.card + .card { margin-top: 16px; }
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.order-grid { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; align-items: start; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 600;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer; white-space: nowrap; min-height: 38px; text-decoration: none !important;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-600); }
.btn-danger { color: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-sm { padding: 5px 10px; min-height: 32px; font-size: 13px; }
.btn-icon { padding: 6px; min-width: 38px; }
.topbar .btn { background: transparent; color: #fff; border-color: rgba(255,255,255,.2); }
.sidebar .btn { background: transparent; color: var(--sidebar-text); border-color: rgba(255,255,255,.15); width: 100%; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; min-height: 38px; width: 100%;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
input[type="checkbox"] { width: 18px; height: 18px; min-height: 0; accent-color: var(--primary); cursor: pointer; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }

.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: #e2e8f0; color: #334155; white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .8; }
.s-pending        { background: #f1f5f9; color: #475569; }
.s-processing     { background: #dbeafe; color: #1d4ed8; }
.s-on-hold        { background: #fef3c7; color: #b45309; }
.s-completed      { background: #dcfce7; color: #15803d; }
.s-shipped        { background: #ede9fe; color: #6d28d9; }
.s-ready-to-ship, .s-ready-for-pickup, .s-printing, .s-in-production { background: #cffafe; color: #0e7490; }
.s-cancelled      { background: #fee2e2; color: #b91c1c; }
.s-refunded       { background: #fce7f3; color: #be185d; }
.s-failed         { background: #fee2e2; color: #991b1b; }
.s-checkout-draft { background: #f1f5f9; color: #64748b; }
@media (prefers-color-scheme: dark) {
  .badge { filter: saturate(1.1) brightness(.95); }
}

.kpi { padding: 16px; }
.kpi .label { color: var(--muted); font-size: 12.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.kpi .value { font-size: 26px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--muted); font-size: 12.5px; }

.status-chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-weight: 600; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.chip:hover { text-decoration: none; background: var(--surface-2); }
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip .n { background: rgba(100,116,139,.15); border-radius: 999px; padding: 0 7px; font-size: 11.5px; }
.chip.active .n { background: rgba(255,255,255,.25); }

/* Chart */
.bars { display: flex; align-items: flex-end; gap: 3px; height: 160px; padding-top: 10px; }
.bars .bar { flex: 1; background: var(--primary); border-radius: 3px 3px 0 0; min-height: 2px; opacity: .85; position: relative; }
.bars .bar:hover { opacity: 1; }
.bars-axis { display: flex; justify-content: space-between; color: var(--muted); font-size: 11px; margin-top: 6px; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 700; background: var(--surface-2); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
tr.selected td { background: var(--primary-soft); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.cell-main { font-weight: 600; }
.cell-sub { color: var(--muted); font-size: 12.5px; }

/* Order cards (mobile list) */
.order-cards { display: none; }
.ocard { display: block; padding: 14px; border-bottom: 1px solid var(--border); color: var(--text); }
.ocard:hover { text-decoration: none; background: var(--surface-2); }
.ocard-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.ocard-num { font-weight: 700; font-size: 15px; }
.ocard-total { font-weight: 700; font-variant-numeric: tabular-nums; }
.ocard-mid { margin-top: 4px; display: flex; justify-content: space-between; gap: 8px; }
.ocard-items { color: var(--muted); font-size: 12.5px; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ocard .sel { margin-right: 10px; }

/* Filters */
.filters { display: grid; grid-template-columns: 2fr repeat(5, minmax(0, 1fr)) auto; gap: 10px; padding: 14px 16px; align-items: center; }
.filters .check { justify-self: start; }
.filter-toggle { display: none; }
.bulkbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--primary-soft);
  border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.bulkbar select { width: auto; min-width: 180px; }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px 16px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.pager .btns { display: flex; gap: 6px; align-items: center; }

/* Order detail */
.order-title { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.order-meta-line { color: var(--muted); margin-top: 4px; }
.status-box { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.status-box select { width: auto; min-width: 190px; }
.item { display: flex; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.item:last-child { border-bottom: 0; }
.item-info { flex: 1; min-width: 0; }
.item-name { font-weight: 600; }
.item-price { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.item-thumb { width: 52px; height: 52px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border); flex-shrink: 0; display: grid; place-items: center; color: var(--muted); }
.item-thumb svg { width: 22px; height: 22px; }
.meta-list { margin: 8px 0 0; display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 3px 12px; font-size: 13px; }
.meta-list dt { color: var(--muted); font-weight: 600; }
.meta-list dd { margin: 0; overflow-wrap: anywhere; }
.totals { width: 100%; }
.totals td { border: 0; padding: 4px 0; }
.totals tr.grand td { font-weight: 700; font-size: 16px; border-top: 1px solid var(--border); padding-top: 10px; }
.addr { white-space: pre-line; }
.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

.attachments { display: flex; flex-direction: column; }
.att {
  display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.att:last-child { border-bottom: 0; }
.att-icon {
  width: 40px; height: 40px; border-radius: 8px; background: var(--primary-soft); color: var(--primary);
  display: grid; place-items: center; font-size: 10.5px; font-weight: 800; text-transform: uppercase; flex-shrink: 0;
}
.att-info { flex: 1; min-width: 0; }
.att-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-actions { display: flex; gap: 6px; }
.att-inline { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.att-pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; background: var(--primary-soft);
  font-size: 12.5px; font-weight: 600; max-width: 100%;
}
.att-pill span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-pill svg { width: 14px; height: 14px; flex-shrink: 0; }

.notes { display: flex; flex-direction: column; gap: 10px; }
.note { padding: 10px 12px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); }
.note.customer { background: #eef2ff; border-color: #c7d2fe; }
.note.system { background: var(--surface-2); }
@media (prefers-color-scheme: dark) { .note.customer { background: #1e1b4b; border-color: #3730a3; } }
.note-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.note-meta { color: var(--muted); font-size: 12px; margin-top: 4px; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 8px 0; border-bottom: 1px dashed var(--border); font-size: 13px; }
.timeline li:last-child { border-bottom: 0; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty svg { width: 36px; height: 36px; opacity: .5; }

.alert { padding: 12px 14px; border-radius: 10px; border: 1px solid; margin-bottom: 16px; }
.alert-warn { background: #fffbeb; border-color: #fde68a; color: #92400e; }
.alert-error { background: #fef2f2; border-color: #fecaca; color: #991b1b; }
.alert-ok { background: #f0fdf4; border-color: #bbf7d0; color: #166534; }
@media (prefers-color-scheme: dark) {
  .alert-warn { background: #422006; border-color: #92400e; color: #fde68a; }
  .alert-error { background: #450a0a; border-color: #991b1b; color: #fecaca; }
  .alert-ok { background: #052e16; border-color: #166534; color: #bbf7d0; }
}

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, #0f172a, #1e3a8a); }
.login-card { width: 100%; max-width: 380px; padding: 28px; }
.login-card .brand { color: var(--text); padding: 0 0 18px; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .55); z-index: 100; display: flex; align-items: center; justify-content: center; padding: 16px;
  animation: fade .15s ease;
}
.modal { width: 100%; max-width: 520px; max-height: calc(100vh - 32px); overflow: auto; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 18px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 18px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 18px; border-top: 1px solid var(--border); }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* Toasts */
#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 200; display: flex; flex-direction: column; gap: 8px; max-width: calc(100vw - 32px); }
.toast { background: #0f172a; color: #fff; padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow-lg); animation: fade .2s ease; max-width: 380px; }
.toast.error { background: var(--danger); }
.toast.ok { background: var(--success); }

.boot { min-height: 100vh; display: grid; place-items: center; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
.spinner.sm { width: 16px; height: 16px; border-width: 2px; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-block { padding: 40px; display: grid; place-items: center; }

.sync-pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); display: inline-block; }
.dot.warn { background: var(--warning); }
.dot.err { background: var(--danger); }
.dot.run { background: var(--primary); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

/* ── Responsive ─────────────────────────────────── */
@media (max-width: 1100px) {
  .order-grid { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters { grid-template-columns: 1fr 1fr 1fr; }
  .filters .search { grid-column: 1 / -1; }
}
@media (max-width: 800px) {
  body { font-size: 15px; }
  .sidebar { display: none; }
  .sidebar.open {
    display: flex; position: fixed; inset: 0 auto 0 0; z-index: 90; width: 260px; box-shadow: var(--shadow-lg);
    padding-top: env(safe-area-inset-top);
  }
  .sidebar-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 80; }
  .topbar { display: flex; }
  .content { padding: 14px 12px calc(84px + env(safe-area-inset-bottom)); }
  h1 { font-size: 19px; }
  .grid-2 { grid-template-columns: 1fr; }
  .kpi .value { font-size: 21px; }
  .kpi { padding: 12px; }
  .page-head { margin-bottom: 12px; }

  .bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; background: var(--surface); border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .bottom-nav a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 6px; font-size: 11px; color: var(--muted); font-weight: 600; }
  .bottom-nav a svg { width: 22px; height: 22px; }
  .bottom-nav a.active { color: var(--primary); }
  .bottom-nav a:hover { text-decoration: none; }

  .orders-table { display: none; }
  .order-cards { display: block; }
  .filter-toggle { display: inline-flex; }
  .filters { grid-template-columns: 1fr 1fr; }
  .filters.collapsed > :not(.search) { display: none; }
  .filters .search { display: flex; gap: 8px; }
  input, select, textarea { font-size: 16px; } /* prevents iOS zoom */
  .status-box { width: 100%; }
  .status-box select { flex: 1; min-width: 0; }
  .card-head, .card-body { padding: 12px; }
  .item { padding: 12px; }
  .att { padding: 10px 12px; }
  .modal-backdrop { align-items: flex-end; padding: 0; }
  .modal { border-radius: 16px 16px 0 0; max-height: 92vh; padding-bottom: env(safe-area-inset-bottom); }
  .hide-mobile { display: none !important; }
  .bulkbar select { flex: 1; min-width: 0; }
  #toasts { bottom: calc(76px + env(safe-area-inset-bottom)); left: 16px; right: 16px; }
}
@media (min-width: 801px) { .show-mobile { display: none !important; } }

/* ── Print (packing slip) ───────────────────────── */
@media print {
  .sidebar, .topbar, .bottom-nav, .no-print, #toasts, .btn { display: none !important; }
  body { background: #fff; color: #000; font-size: 12px; }
  .content { padding: 0; max-width: none; }
  .card { box-shadow: none; border-color: #ccc; break-inside: avoid; }
  .order-grid { grid-template-columns: 1fr 1fr; }
  a { color: #000; }
}

/* ── CRM status colours ─────────────────────────── */
.c-gray   { background: #f1f5f9; color: #475569; }
.c-blue   { background: #dbeafe; color: #1d4ed8; }
.c-cyan   { background: #cffafe; color: #0e7490; }
.c-amber  { background: #fef3c7; color: #b45309; }
.c-orange { background: #ffedd5; color: #c2410c; }
.c-purple { background: #ede9fe; color: #6d28d9; }
.c-green  { background: #dcfce7; color: #15803d; }
.c-red    { background: #fee2e2; color: #b91c1c; }
.c-pink   { background: #fce7f3; color: #be185d; }
.woo-badge {
  display: inline-block; font-size: 11px; font-weight: 600; color: var(--muted); border: 1px solid var(--border);
  border-radius: 999px; padding: 1px 8px; white-space: nowrap; background: var(--surface);
}

/* Status editor */
.st-row { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; background: var(--surface-2); }
.st-main { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.st-main .badge { width: 120px; justify-content: flex-start; overflow: hidden; text-overflow: ellipsis; }
.st-main .st-name { flex: 1; min-width: 140px; width: auto; }
.st-main .st-color { width: auto; }
.st-move { display: flex; gap: 4px; margin-left: auto; }
.st-map { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 8px; align-items: center; }
.st-map .check input { width: 15px; height: 15px; }
details.card > summary { list-style: none; }
details.card > summary::-webkit-details-marker { display: none; }
@media (max-width: 800px) {
  .st-main .badge { display: none; }
  .st-move { margin-left: 0; }
}

/* Grid/flex children may shrink below their content width (prevents sideways scrolling on phones). */
.grid > *, .order-grid > * { min-width: 0; }
.kpi .sub { overflow-wrap: anywhere; }
.grid > .card + .card { margin-top: 0; }
