:root {
  --bg: #f5f7f5; --surface: #ffffff; --border: #e1e7e3; --grid: #edf1ee;
  --text: #17221c; --muted: #5d6b63; --accent: #1f7a4d; --accent-ink: #ffffff; --accent-soft: #e5f2ea;
  --good: #0ca30c; --warning: #fab219; --serious: #ec835a; --critical: #d03b3b;
  --error-bg: #fdecec; --error-text: #a12626; --ok-bg: #e5f2ea; --ok-text: #14603a;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1512; --surface: #161e1a; --border: #26322b; --grid: #1e2822;
    --text: #e6ece8; --muted: #93a29a; --accent: #4cbf86; --accent-ink: #0b1a12; --accent-soft: #1a2e23;
    --error-bg: #3a1a1a; --error-text: #f2a7a7; --ok-bg: #1a2e23; --ok-text: #8fdcb2;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1512; --surface: #161e1a; --border: #26322b; --grid: #1e2822;
  --text: #e6ece8; --muted: #93a29a; --accent: #4cbf86; --accent-ink: #0b1a12; --accent-soft: #1a2e23;
  --error-bg: #3a1a1a; --error-text: #f2a7a7; --ok-bg: #1a2e23; --ok-text: #8fdcb2;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); }
.wrap { max-width: 1100px; margin: 0 auto; padding: 24px 16px 48px; }

/* top bar */
.top { display: flex; align-items: center; gap: 12px; margin-bottom: 24px; flex-wrap: wrap; }
.logo {
  width: 40px; height: 40px; border-radius: 10px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-weight: 700; font-size: 20px; flex: none;
}
.top h1 { font-size: 20px; margin: 0; }
.sub { color: var(--muted); font-size: 13px; }
.top nav { margin-left: auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.top nav .who { color: var(--muted); font-size: 13px; }

/* auth pages */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth .card { width: 100%; max-width: 400px; padding: 28px 24px; }
.auth .brand { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.auth h1 { font-size: 20px; margin: 0; }
.auth h2 { font-size: 16px; margin: 0 0 16px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
label { display: block; font-size: 13px; font-weight: 600; margin: 14px 0 6px; }
input[type=email], input[type=password], input[type=text] {
  width: 100%; padding: 10px 12px; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
}
input:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input.otp { letter-spacing: .3em; font-variant-numeric: tabular-nums; font-size: 18px; }
.btn {
  display: inline-block; padding: 9px 16px; border-radius: 8px; border: 1px solid var(--accent);
  background: var(--accent); color: var(--accent-ink); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn.block { width: 100%; margin-top: 18px; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); font-weight: 500; }
.btn:disabled { opacity: .6; cursor: wait; }
.tabs { display: flex; gap: 4px; background: var(--bg); border: 1px solid var(--border); border-radius: 9px; padding: 3px; margin-top: 16px; }
.tabs button {
  flex: 1; padding: 7px 8px; border: 0; border-radius: 6px; background: transparent; color: var(--muted);
  font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
}
.tabs button[aria-selected=true] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.msg { margin-top: 14px; padding: 10px 12px; border-radius: 8px; font-size: 13px; display: none; }
.msg.error { display: block; background: var(--error-bg); color: var(--error-text); }
.msg.ok { display: block; background: var(--ok-bg); color: var(--ok-text); }
.links { margin-top: 16px; font-size: 13px; display: flex; justify-content: space-between; gap: 8px; }
.hint { color: var(--muted); font-size: 12px; margin-top: 6px; }

/* dashboard */
h2.section { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 28px 0 12px; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi .value { font-size: 26px; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .note { color: var(--muted); font-size: 12px; margin-top: 2px; }
.bar { display: flex; height: 14px; gap: 2px; margin: 12px 0 10px; }
.bar span { display: block; height: 100%; }
.bar span:first-child { border-radius: 4px 0 0 4px; }
.bar span:last-child { border-radius: 0 4px 4px 0; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.legend b { color: var(--text); font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 8px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
.pill { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; font-size: 12px; }
.pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--dot, var(--muted)); }
.pill.late { --dot: var(--critical); }
.pill.soon { --dot: var(--warning); }
.pill.ok { --dot: var(--good); }
.empty { color: var(--muted); font-size: 13px; padding: 8px 0; }
.links-grid a.card { color: inherit; text-decoration: none; display: block; }
.links-grid a.card:hover { border-color: var(--accent); }
.links-grid h3 { margin: 0 0 4px; font-size: 15px; }
.links-grid p { margin: 0; color: var(--muted); font-size: 13px; }
.stamp { color: var(--muted); font-size: 12px; margin-top: 32px; text-align: center; }

/* settings */
.settings { max-width: 560px; }
.settings .card { margin-bottom: 16px; padding: 20px; }
.settings h2 { font-size: 16px; margin: 0 0 4px; }
.qr { background: #fff; padding: 12px; border-radius: 8px; display: inline-block; margin: 12px 0; }
.qr svg { display: block; }
code.secret { font-size: 14px; word-break: break-all; background: var(--bg); padding: 4px 6px; border-radius: 4px; }

/* ---------- app shell: sidebar + main ---------- */
:root { --sb-w: 240px; --sb-rail: 60px; }
.shell { display: flex; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; flex: none; width: var(--sb-w);
  display: flex; flex-direction: column; background: var(--surface); border-right: 1px solid var(--border);
  overflow: hidden; transition: width .18s ease;
}
.sb-resizing .sidebar, .sb-resizing .resizer { transition: none; }
.sb-resizing, .sb-resizing * { cursor: col-resize !important; user-select: none; }
.sb-resizing iframe { pointer-events: none; }
.sb-collapsed .sidebar { width: var(--sb-rail); }
.resizer {
  flex: none; width: 6px; margin-left: -3px; margin-right: -3px; cursor: col-resize; position: relative; z-index: 5;
  touch-action: none;
}
.resizer::after { content: ""; position: absolute; inset: 0 2px; border-radius: 2px; transition: background .15s; }
.resizer:hover::after, .resizer:focus-visible::after, .sb-resizing .resizer::after { background: var(--accent); }
.resizer:focus-visible { outline: none; }
.main { flex: 1; min-width: 0; }

.sb-head { display: flex; align-items: center; gap: 8px; padding: 14px 12px; min-height: 64px; }
.sb-brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 700; min-width: 0; flex: 1; }
.logo.sm { width: 32px; height: 32px; border-radius: 8px; font-size: 16px; }
.sb-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sb-btn {
  flex: none; display: grid; place-items: center; width: 34px; height: 34px; border-radius: 8px; border: 0;
  background: transparent; color: var(--muted); cursor: pointer;
}
.sb-btn:hover { background: var(--bg); color: var(--text); }
.sb-nav { flex: 1; overflow-y: auto; padding: 4px 8px; display: flex; flex-direction: column; gap: 2px; }
.sb-nav a {
  display: flex; align-items: center; gap: 12px; padding: 9px 10px; border-radius: 8px;
  color: var(--muted); text-decoration: none; font-weight: 500; font-size: 14px;
}
.sb-nav a svg { flex: none; }
.sb-nav a:hover { background: var(--bg); color: var(--text); }
.sb-nav a[aria-current=page] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sb-nav a[hidden] { display: none; }
.sb-foot { display: flex; align-items: center; gap: 8px; padding: 12px; border-top: 1px solid var(--border); }
.sb-user { flex: 1; min-width: 0; }
.sb-email { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.role-badge {
  display: inline-block; margin-top: 2px; font-size: 11px; font-weight: 600; padding: 1px 8px; border-radius: 999px;
  background: var(--bg); color: var(--muted); border: 1px solid var(--border);
}
.role-badge[data-role=admin] { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

/* collapsed rail: icons only, brand logo becomes the expand target */
.sb-collapsed .sb-text, .sb-collapsed .sb-user { display: none; }
.sb-collapsed .sb-head { flex-direction: column; padding: 12px 0; gap: 6px; }
.sb-collapsed .sb-brand { flex: none; }
.sb-collapsed .sb-nav a { justify-content: center; padding: 9px 0; }
.sb-collapsed .sb-foot { justify-content: center; padding: 12px 0; }

.mobile-bar { display: none; }
.sb-scrim { display: none; }
.page { max-width: 1100px; margin: 0 auto; padding: 24px 24px 48px; }
.page-head { margin-bottom: 20px; }
.page-head h1 { font-size: 22px; margin: 0; }

@media (max-width: 767px) {
  .resizer { display: none; }
  .sidebar { position: fixed; left: 0; top: 0; z-index: 40; width: min(280px, 85vw) !important; transform: translateX(-100%); transition: transform .2s ease; }
  .sb-collapsed .sb-text, .sb-collapsed .sb-user { display: block; }
  .sb-collapsed .sb-head { flex-direction: row; padding: 14px 12px; }
  .sb-collapsed .sb-nav a { justify-content: flex-start; padding: 9px 10px; }
  .sb-collapse { display: none; }
  .sb-mobile-open .sidebar { transform: none; }
  .sb-mobile-open .sb-scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 30; }
  .mobile-bar {
    display: flex; align-items: center; gap: 10px; padding: 10px 16px; position: sticky; top: 0; z-index: 20;
    background: var(--surface); border-bottom: 1px solid var(--border);
  }
  .page { padding: 16px 16px 40px; }
}

/* full-bleed embedded page (PO form) */
.embed { display: flex; flex-direction: column; height: 100vh; }
.embed iframe { flex: 1; width: 100%; border: 0; background: var(--bg); }
@media (max-width: 767px) { .embed { height: 100dvh; } }

/* users page */
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.danger { background: transparent; color: var(--critical); border-color: var(--border); font-weight: 500; }
select {
  padding: 8px 10px; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px;
}
select.sm { padding: 4px 8px; font-size: 13px; }
.form-row { display: grid; gap: 12px; grid-template-columns: 2fr 1.5fr 1fr auto; align-items: end; }
.form-row label { margin-top: 0; }
@media (max-width: 767px) { .form-row { grid-template-columns: 1fr; } }
.link-box { display: flex; gap: 8px; margin-top: 12px; }
.link-box input { flex: 1; font-size: 13px; }
.roles-help { font-size: 13px; color: var(--muted); margin: 0; padding-left: 18px; }
.roles-help li { margin: 2px 0; }
.form-row select { padding: 10px 12px; }
