/* ---------- Theme tokens ---------- */
:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e8ef;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #e8efff;
  --success: #16a34a;
  --success-hover: #15803d;
  --success-soft: #dcfce7;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --warn: #b45309;
  --neutral-soft: #eef2f6;
  --chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  --control-h: 38px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 1px 3px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 20px 50px rgba(15, 23, 42, 0.18);
  --radius: 10px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #0f172a;
  --border: #1f2a3c;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --primary-soft: #172554;
  --success: #22c55e;
  --success-hover: #4ade80;
  --success-soft: #14321f;
  --danger: #ef4444;
  --danger-hover: #f87171;
  --warn: #fbbf24;
  --neutral-soft: #1e293b;
  --chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.55);
  color-scheme: dark;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
/* Author display rules (grid/flex) would otherwise override the hidden attribute */
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  transition: background-color 0.2s, color 0.2s;
}
h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 16px; }
h3 { font-size: 16px; }
p { margin: 0; }
.muted { color: var(--muted); font-size: 13px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1280px; margin: 0 auto; padding: 12px 24px;
  display: flex; align-items: center; gap: 16px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; }
.brand-mark {
  display: grid; place-items: center; width: 32px; height: 32px; border-radius: 8px;
  background: var(--primary); color: #fff;
}
.topbar-actions { display: flex; align-items: center; gap: 10px; margin-left: auto; }

/* Business switcher */
.business-switcher { display: flex; align-items: center; gap: 10px; min-width: 0; }
.business-switcher .divider { color: var(--border); font-size: 22px; font-weight: 300; }
.business-switcher .select { --control-h: 34px; height: 34px; max-width: 260px; font-weight: 600; }
.business-switcher .btn { height: 34px; padding-top: 0; padding-bottom: 0; }

.empty-business { padding: 48px 20px; text-align: center; display: grid; gap: 10px; justify-items: center; }

/* Businesses dialog */
.business-list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.business-list li { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.business-list li:last-child { border-bottom: none; }
.business-list .biz-name { font-weight: 600; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.business-list .biz-meta { font-size: 12px; color: var(--muted); white-space: nowrap; }
.business-list .current { font-size: 11px; font-weight: 600; color: var(--primary); background: var(--primary-soft); padding: 1px 8px; border-radius: 999px; margin-left: 6px; }
.pill {
  padding: 5px 10px; border-radius: 999px; font-size: 12px; font-weight: 500;
  background: var(--neutral-soft); color: var(--muted); white-space: nowrap;
}
.pill.warn { color: var(--warn); }

/* ---------- Layout ---------- */
.container { max-width: 1280px; margin: 0 auto; padding: 24px; display: grid; gap: 20px; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 18px; display: flex; flex-direction: column; gap: 4px;
}
.stat-label { font-size: 12px; font-weight: 500; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.stat-value { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.stat-sub { font-size: 12px; color: var(--muted); }
.stat-accent { border-top: 3px solid var(--success); }
.stat-accent .stat-value { color: var(--success); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
.card-header {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.card-header h2 { margin-bottom: 2px; }
.toolbar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
/* Every control in a toolbar shares one height so they line up exactly */
.toolbar .input, .toolbar .btn { height: var(--control-h); }
.toolbar .btn { padding-top: 0; padding-bottom: 0; }

/* Label + control pair, label vertically centred on the control */
.control { display: inline-flex; align-items: center; gap: 8px; margin: 0; }
.control-label { font-size: 13px; font-weight: 500; color: var(--muted); white-space: nowrap; line-height: 1; }

/* Native select restyled to match text inputs in every browser.
   (.input.select so it beats the later .input background shorthand.) */
.input.select {
  appearance: none; -webkit-appearance: none;
  width: auto; min-width: 128px; padding: 0 34px 0 11px; cursor: pointer;
  background-image: var(--chevron); background-repeat: no-repeat; background-position: right 12px center;
  line-height: calc(var(--control-h) - 2px);
}
.input.select:disabled { opacity: 0.55; cursor: not-allowed; }

/* Section heading outside a card (Summary) */
.section-header {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  margin-bottom: -6px; /* sits a little closer to the cards it describes */
}

/* Business view keeps the page's vertical rhythm */
#business-content { display: grid; gap: 20px; }
.section-header h2 { margin-bottom: 2px; }

/* ---------- Controls ---------- */
.input {
  width: 100%; padding: 8px 11px; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.search { width: 240px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
  border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}
.btn:hover { background: var(--neutral-soft); }
.btn:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-success { background: var(--success); border-color: var(--success); color: #fff; }
.btn-success:hover { background: var(--success-hover); border-color: var(--success-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); border-color: var(--danger-hover); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); }
.btn-ghost.danger:hover { color: var(--danger); background: transparent; }
.btn-ghost.success { color: var(--success); }
.btn-ghost.success:hover { background: var(--success-soft); }
.btn-ghost:disabled:hover { background: transparent; color: var(--muted); }

.icon-btn {
  display: grid; place-items: center; width: 34px; height: 34px; padding: 0;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: var(--neutral-soft); }
.modal-header .icon-btn { border-color: transparent; color: var(--muted); }
/* Sun shows in dark mode (switch to day), moon in light mode */
.icon-sun { display: none; }
:root[data-theme="dark"] .icon-sun { display: block; }
:root[data-theme="dark"] .icon-moon { display: none; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; min-width: 820px; }
.table th {
  padding: 10px 12px; text-align: left; font-size: 12px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.04em; background: var(--surface-2);
  border-bottom: 1px solid var(--border); line-height: 1.3; vertical-align: bottom; white-space: normal;
}
.table td { padding: 12px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover td { background: var(--surface-2); }
.num { text-align: right !important; font-variant-numeric: tabular-nums; white-space: nowrap; }
.item-name { font-weight: 600; white-space: nowrap; }
.converted { color: var(--muted); }
.nowrap { white-space: nowrap; }
.markup-pos { color: var(--success); font-weight: 600; }
.markup-neg { color: var(--danger); font-weight: 600; }
.actions-col { width: 1%; }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; white-space: nowrap; }

.badge {
  display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 600; white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.in { background: var(--success-soft); color: var(--success); }
.badge.out { background: var(--neutral-soft); color: var(--muted); }

/* Out-of-stock rows are dimmed (actions stay fully visible) */
tr.out-of-stock td:not(.actions-cell) { opacity: 0.45; }

.empty { padding: 40px 20px; text-align: center; }

/* Weekly / monthly / yearly bracket headers */
/* Collapsible group rows */
.group-row td { cursor: pointer; user-select: none; }
.group-toggle {
  display: inline-grid; place-items: center; width: 20px; height: 20px; margin-right: 6px; padding: 0;
  border: none; background: transparent; color: var(--muted); cursor: pointer; border-radius: 4px;
  transition: transform 0.15s; vertical-align: -4px;
}
.group-toggle:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
.group-row.collapsed .group-toggle { transform: rotate(-90deg); }
.table tbody tr.group-row.level-1 td { background: var(--surface-2); padding-left: 36px; }
.table tbody tr.group-row.level-1:hover td { background: var(--surface-2); }
.group-row.level-1 .group-label { font-weight: 600; }
.table tbody tr.group-row td {
  background: var(--primary-soft); border-bottom: 1px solid var(--border);
  padding: 8px 12px;
}
.table tbody tr.group-row:hover td { background: var(--primary-soft); }
/* Summary sits next to the label so it stays visible even when the table scrolls sideways */
.group-head { display: flex; align-items: baseline; gap: 6px 16px; flex-wrap: wrap; }
.group-label { font-weight: 700; }
.group-summary { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.group-summary strong { color: var(--text); font-weight: 600; }
.group-summary .markup-pos, .group-summary .markup-neg { font-weight: 600; }
.empty-title { font-weight: 600; margin-bottom: 4px; }

/* ---------- Modals ---------- */
.modal {
  width: min(520px, calc(100vw - 32px)); padding: 0; border: 1px solid var(--border); border-radius: 14px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg);
}
.modal-sm { width: min(400px, calc(100vw - 32px)); }
.modal::backdrop { background: rgba(15, 23, 42, 0.5); backdrop-filter: blur(2px); }
.modal[open] { animation: pop 0.15s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(0.98); } to { opacity: 1; transform: none; } }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 20px; display: grid; gap: 14px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); background: var(--surface-2); }

.form-grid { grid-template-columns: 1fr 1fr; }
.span-2 { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 13px; font-weight: 500; }
.field-hint { font-size: 12px; color: var(--muted); }
.input-affix { position: relative; }
.input-affix > span { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.input-affix > .input { padding-left: 26px; }
.form-error { color: var(--danger); font-size: 13px; min-height: 0; }
.form-error:empty { display: none; }

.sell-item { font-size: 15px; font-weight: 600; }
.textarea { resize: vertical; min-height: 72px; line-height: 1.45; font: inherit; }
.field-label-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.optional { font-weight: 400; color: var(--muted); }
.char-count { font-size: 12px; font-weight: 400; color: var(--muted); font-variant-numeric: tabular-nums; }
.char-count.near-limit { color: var(--warn); }

/* Sale comment under the item name in Sales history */
.sale-comment {
  margin-top: 2px; font-size: 12.5px; color: var(--muted); max-width: 320px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sell-total {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 12px 14px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border);
}
.sell-total strong { font-size: 20px; color: var(--success); font-variant-numeric: tabular-nums; }

/* ---------- Sign-in / setup ---------- */
.auth-screen { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth-card {
  width: min(400px, 100%); display: grid; gap: 16px; padding: 28px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow-lg);
}
.brand-lg { font-size: 18px; }
.btn-block { width: 100%; padding: 10px 14px; }
.small { font-size: 12px; text-align: center; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; background: var(--neutral-soft); padding: 1px 5px; border-radius: 4px; }

/* ---------- Account menu ---------- */
.user-menu { position: relative; }
.user-menu > summary { list-style: none; }
.user-menu > summary::-webkit-details-marker { display: none; }
.menu {
  position: absolute; right: 0; top: calc(100% + 8px); min-width: 230px; padding: 8px; z-index: 20;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow-lg);
}
.menu-label { font-size: 11px; color: var(--muted); padding: 4px 8px 0; text-transform: uppercase; letter-spacing: 0.04em; }
.menu-email { font-weight: 600; padding: 0 8px 4px; overflow-wrap: anywhere; }
.menu hr { border: none; border-top: 1px solid var(--border); margin: 6px 0; }
.menu-item {
  display: block; width: 100%; text-align: left; padding: 8px; border: none; border-radius: 6px;
  background: transparent; color: var(--text); font: inherit; cursor: pointer;
}
.menu-item:hover { background: var(--neutral-soft); }
.menu-item.danger { color: var(--danger); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px);
  padding: 10px 16px; border-radius: 8px; background: var(--text); color: var(--bg);
  font-weight: 500; box-shadow: var(--shadow-lg); opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s; z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 600px) {
  .container, .topbar-inner { padding-left: 14px; padding-right: 14px; }
  .stats { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat-value { font-size: 20px; }
  .toolbar { width: 100%; }
  .search { flex: 1; width: auto; }
  .toolbar { flex-wrap: wrap; }
  .form-grid { grid-template-columns: 1fr; }
  #rate-pill { display: none; }
  .topbar-inner { flex-wrap: wrap; }
  .business-switcher { order: 3; width: 100%; }
  .business-switcher .divider { display: none; }
  .business-switcher .select { flex: 1; max-width: none; }
  .section-header .toolbar, .card-header .toolbar { width: 100%; }
  .control { flex: 1; }
  .control .select { flex: 1; }
}
