:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e5e9f2;
  --text: #1a2233;
  --text-dim: #66718a;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #e8f0ff;
  --success: #16a34a;
  --success-soft: #e7f6ec;
  --warn: #d97706;
  --warn-soft: #fdf1de;
  --danger: #dc2626;
  --danger-soft: #fdeaea;
  --shadow: 0 1px 3px rgba(16,24,40,.06), 0 1px 2px rgba(16,24,40,.04);
  --shadow-lg: 0 10px 30px rgba(16,24,40,.12);
  --radius: 12px;
  --radius-sm: 8px;
  --sidebar-w: 248px;
}

:root[data-theme="dark"] {
  --bg: #0e1320;
  --surface: #161d2e;
  --surface-2: #1c2436;
  --border: #263045;
  --text: #e8ecf5;
  --text-dim: #93a0bb;
  --primary: #4f86ff;
  --primary-dark: #3b6fe0;
  --primary-soft: #1c2b4d;
  --success: #34d399;
  --success-soft: #123024;
  --warn: #fbbf24;
  --warn-soft: #34260c;
  --danger: #f87171;
  --danger-soft: #3a1616;
  --shadow: 0 1px 3px rgba(0,0,0,.4);
  --shadow-lg: 0 12px 40px rgba(0,0,0,.5);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; }
.brand-logo { font-size: 28px; }
.brand-text strong { display: block; font-size: 16px; letter-spacing: -.2px; }
.brand-text small { color: var(--text-dim); font-size: 11px; }

.clinic-switcher { margin-bottom: 16px; padding: 0 6px; }
.clinic-switcher label { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-dim); margin-bottom: 5px; }
.clinic-switcher select {
  width: 100%; padding: 9px 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-size: 13px; cursor: pointer;
}

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 11px;
  padding: 10px 12px; border: none; background: transparent;
  color: var(--text-dim); font-size: 13.5px; font-weight: 500;
  border-radius: var(--radius-sm); cursor: pointer; text-align: left; width: 100%;
  transition: background .12s, color .12s;
}
.nav-item .ico { font-size: 15px; width: 18px; text-align: center; }
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }

.sidebar-footer { margin-top: auto; display: flex; align-items: center; gap: 8px; padding-top: 14px; border-top: 1px solid var(--border); }
.user-chip { display: flex; align-items: center; gap: 9px; flex: 1; min-width: 0; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--primary); color: #fff; display: grid; place-items: center; font-weight: 600; font-size: 12px; flex-shrink: 0; }
.user-meta { min-width: 0; }
.user-meta strong { display: block; font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-meta small { color: var(--text-dim); font-size: 10.5px; }
.theme-toggle { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); width: 34px; height: 34px; cursor: pointer; font-size: 15px; }

/* Main */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 14px 26px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.search { flex: 1; max-width: 460px; position: relative; }
.search-ico { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); opacity: .5; font-size: 13px; }
.search input {
  width: 100%; padding: 9px 12px 9px 34px; border-radius: 20px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-size: 13px;
}
.topbar-actions { display: flex; align-items: center; gap: 12px; margin-left: auto; }

.view { padding: 26px; max-width: 1240px; width: 100%; }

/* Headings */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { margin: 0 0 3px; font-size: 22px; letter-spacing: -.4px; }
.page-head p { margin: 0; color: var(--text-dim); font-size: 13px; }
.page-head-actions { display: flex; gap: 8px; }

/* Buttons */
.btn { padding: 9px 14px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 13px; font-weight: 600; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; transition: filter .12s, background .12s; }
.btn:hover { filter: brightness(.97); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-sm { padding: 6px 10px; font-size: 12px; }

/* Cards / grid */
.grid { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(4, 1fr); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 18px; }
.kpi { padding: 18px; }
.kpi .kpi-label { font-size: 12px; color: var(--text-dim); font-weight: 500; display: flex; align-items: center; gap: 6px; }
.kpi .kpi-value { font-size: 28px; font-weight: 700; letter-spacing: -.5px; margin-top: 6px; }
.kpi .kpi-sub { font-size: 11.5px; color: var(--text-dim); margin-top: 3px; }
.kpi-value.danger { color: var(--danger); }
.kpi-value.warn { color: var(--warn); }
.kpi-value.success { color: var(--success); }

.two-col { grid-template-columns: 1.5fr 1fr; align-items: start; }
.section-title { font-size: 14px; font-weight: 700; margin: 0; padding: 16px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-dim); font-weight: 600; padding: 11px 16px; border-bottom: 1px solid var(--border); white-space: nowrap; }
tbody td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
.item-name { font-weight: 600; }
.item-sub { color: var(--text-dim); font-size: 11.5px; }
.num { font-variant-numeric: tabular-nums; text-align: right; }
th.num { text-align: right; }

/* Badges & bars */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 20px; font-size: 11px; font-weight: 600; white-space: nowrap; }
.badge.ok { background: var(--success-soft); color: var(--success); }
.badge.low { background: var(--warn-soft); color: var(--warn); }
.badge.out { background: var(--danger-soft); color: var(--danger); }
.badge.neutral { background: var(--surface-2); color: var(--text-dim); }
.badge.info { background: var(--primary-soft); color: var(--primary); }
.dot { width: 7px; height: 7px; border-radius: 50%; }

.stockbar { height: 6px; border-radius: 4px; background: var(--surface-2); overflow: hidden; min-width: 90px; margin-top: 5px; }
.stockbar > i { display: block; height: 100%; border-radius: 4px; }

.chip { display: inline-block; padding: 2px 8px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; font-size: 11px; color: var(--text-dim); }

/* Alerts list */
.alert-row { display: flex; align-items: center; gap: 12px; padding: 12px 18px; border-bottom: 1px solid var(--border); }
.alert-row:last-child { border-bottom: none; }
.alert-icon { width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center; font-size: 15px; flex-shrink: 0; }
.alert-icon.warn { background: var(--warn-soft); }
.alert-icon.danger { background: var(--danger-soft); }
.alert-body { flex: 1; min-width: 0; }
.alert-body strong { font-size: 13px; }
.alert-body small { display: block; color: var(--text-dim); font-size: 11.5px; }

/* Forms */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 12px; font-weight: 600; color: var(--text-dim); }
.field input, .field select, .field textarea {
  padding: 9px 11px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font-size: 13px; font-family: inherit;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }

/* Modal */
#modalRoot:empty { display: none; }
.modal-overlay { position: fixed; inset: 0; background: rgba(10,15,25,.5); display: grid; place-items: center; z-index: 100; padding: 20px; }
.modal { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg); width: 100%; max-width: 560px; max-height: 90vh; overflow: auto; }
.modal.wide { max-width: 720px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 22px; }
.modal-foot { padding: 16px 22px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; position: sticky; bottom: 0; background: var(--surface); }
.icon-btn { background: transparent; border: none; font-size: 18px; cursor: pointer; color: var(--text-dim); line-height: 1; }

/* Scan */
.scan-layout { display: grid; grid-template-columns: 1.2fr 1fr; gap: 20px; align-items: start; }
.scanner-box { background: #0b1020; border-radius: var(--radius); overflow: hidden; position: relative; aspect-ratio: 4/3; display: grid; place-items: center; }
.scanner-box video { width: 100%; height: 100%; object-fit: cover; }
.scanner-placeholder { color: #9fb0d0; text-align: center; padding: 30px; }
.scan-frame { position: absolute; inset: 18% 14%; border: 2px solid rgba(79,134,255,.9); border-radius: 12px; box-shadow: 0 0 0 4000px rgba(0,0,0,.25); pointer-events: none; }
.scan-frame::after { content:""; position:absolute; left:0; right:0; top:50%; height:2px; background: rgba(79,134,255,.9); animation: scanline 2s ease-in-out infinite; }
@keyframes scanline { 0%,100%{ top:8%; } 50%{ top:92%; } }
.manual-scan { display: flex; gap: 8px; margin-top: 14px; }
.manual-scan input { flex: 1; padding: 11px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-size: 15px; font-variant-numeric: tabular-nums; letter-spacing: .5px; }
.scan-log { max-height: 340px; overflow: auto; }
.scan-hint { font-size: 12px; color: var(--text-dim); margin-top: 8px; line-height: 1.5; }

/* Reorder AI */
.confidence { display: inline-flex; align-items: center; gap: 6px; }
.spark { display:inline-block; height: 22px; }

/* Empty state */
.empty { text-align: center; padding: 50px 20px; color: var(--text-dim); }
.empty .emoji { font-size: 40px; display: block; margin-bottom: 10px; }

/* Toast */
.toast-root { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 200; }
.toast { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--primary); border-radius: var(--radius-sm); box-shadow: var(--shadow-lg); padding: 12px 16px; min-width: 240px; font-size: 13px; animation: slideIn .2s ease; }
.toast.success { border-left-color: var(--success); }
.toast.warn { border-left-color: var(--warn); }
.toast.danger { border-left-color: var(--danger); }
.toast strong { display: block; margin-bottom: 2px; }
@keyframes slideIn { from { transform: translateX(30px); opacity: 0; } }

/* Info callout */
.callout { display: flex; gap: 12px; padding: 14px 16px; border-radius: var(--radius-sm); background: var(--primary-soft); border: 1px solid transparent; font-size: 12.5px; line-height: 1.55; }
.callout .c-ico { font-size: 18px; }

.mt { margin-top: 16px; }
.muted { color: var(--text-dim); }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.flex { display: flex; align-items: center; gap: 8px; }
.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }

/* Paywall */
.paywall-overlay { position: fixed; inset: 0; z-index: 500; background: rgba(9,13,22,.72); backdrop-filter: blur(6px); overflow: auto; display: grid; place-items: center; padding: 30px 20px; }
.paywall-card { width: 100%; max-width: 920px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow-lg); overflow: hidden; }
.paywall-head { text-align: center; padding: 30px 30px 8px; }
.paywall-head .pw-logo { font-size: 40px; }
.paywall-head h2 { margin: 8px 0 4px; font-size: 24px; letter-spacing: -.4px; }
.paywall-head p { margin: 0 auto; color: var(--text-dim); max-width: 460px; font-size: 13.5px; }
.plans { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; padding: 24px 30px; }
.plan { border: 1px solid var(--border); border-radius: 14px; padding: 20px 18px; display: flex; flex-direction: column; background: var(--surface-2); position: relative; }
.plan.featured { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); background: var(--surface); }
.plan .plan-tag { position: absolute; top: -10px; left: 50%; transform: translateX(-50%); background: var(--primary); color: #fff; font-size: 10px; font-weight: 700; padding: 3px 10px; border-radius: 20px; letter-spacing: .05em; text-transform: uppercase; }
.plan h3 { margin: 0 0 2px; font-size: 15px; }
.plan .plan-price { font-size: 30px; font-weight: 800; letter-spacing: -1px; margin: 8px 0 2px; }
.plan .plan-price small { font-size: 13px; font-weight: 500; color: var(--text-dim); }
.plan ul { list-style: none; padding: 0; margin: 14px 0 18px; display: flex; flex-direction: column; gap: 8px; font-size: 12.5px; color: var(--text-dim); }
.plan ul li::before { content: "✓ "; color: var(--success); font-weight: 700; }
.plan .btn { margin-top: auto; width: 100%; justify-content: center; }
.paywall-foot { border-top: 1px solid var(--border); padding: 20px 30px; display: flex; align-items: center; gap: 14px; flex-wrap: wrap; justify-content: space-between; background: var(--surface-2); }
.pw-code { display: flex; align-items: center; gap: 8px; }
.pw-code input { padding: 9px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 13px; letter-spacing: .5px; width: 190px; }
.pw-err { color: var(--danger); font-size: 12px; display: none; }
.pw-note { font-size: 11.5px; color: var(--text-dim); max-width: 380px; }

@media (max-width: 1000px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .two-col, .scan-layout { grid-template-columns: 1fr; }
}
@media (max-width: 760px) { .plans { grid-template-columns: 1fr; } }
@media (max-width: 720px) {
  .sidebar { position: fixed; left: -260px; z-index: 50; transition: left .2s; }
  .form-grid { grid-template-columns: 1fr; }
}
