:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --line: #e3e6ea;
  --line-strong: #cfd4da;
  --text: #1b2027;
  --muted: #6b7480;
  --accent: #1f6feb;
  --accent-soft: #e8f0fe;
  --green: #12805c;
  --green-soft: #e2f5ee;
  --amber: #9a6200;
  --amber-soft: #fdf0da;
  --red: #b42318;
  --red-soft: #fdeceb;
  --purple: #6b3fd4;
  --purple-soft: #efeafc;
  --sidebar: #131a24;
  --sidebar-soft: #1d2733;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: 20px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
p { margin: 0; }
a { color: var(--accent); text-decoration: none; }

#app { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--sidebar);
  color: #aeb9c7;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; gap: 10px; align-items: center; padding: 18px 16px; border-bottom: 1px solid rgba(255,255,255,.07); }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
  background: linear-gradient(135deg, #1f6feb, #17b06b); color: #fff; font-weight: 700; font-size: 13px;
}
.brand-name { color: #fff; font-weight: 600; font-size: 14px; }
.brand-sub { font-size: 11px; color: #6f7d8d; }
.sidebar nav { padding: 10px 8px; overflow-y: auto; flex: 1; }
.nav-group { padding: 14px 10px 6px; font-size: 10.5px; text-transform: uppercase; letter-spacing: .09em; color: #5d6b7c; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: #aeb9c7; cursor: pointer; font-size: 13.5px; user-select: none;
}
.nav-item:hover { background: var(--sidebar-soft); color: #e6ecf3; }
.nav-item.active { background: #24344a; color: #fff; }
.nav-item .ico { width: 18px; text-align: center; opacity: .9; }
.sidebar-foot { padding: 12px 16px; border-top: 1px solid rgba(255,255,255,.07); font-size: 11px; color: #5d6b7c; }

/* ---------- Layout ---------- */
.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 16px;
  padding: 18px 24px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.view { padding: 20px 24px 60px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }
.muted { color: var(--muted); font-size: 12.5px; }
.grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.cols-3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.cols-4 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

/* ---------- Cards ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.card-body { padding: 16px; }
.card-body.tight { padding: 0; }

.stat { padding: 16px; }
.stat .label { font-size: 12px; color: var(--muted); }
.stat .value { font-size: 24px; font-weight: 650; letter-spacing: -.02em; margin-top: 4px; }
.stat .sub { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left; font-weight: 600; font-size: 11px; letter-spacing: .05em; text-transform: uppercase;
  color: var(--muted); padding: 10px 12px; background: var(--surface-2);
  border-bottom: 1px solid var(--line); white-space: nowrap; position: sticky; top: 0;
}
tbody td { padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--surface-2); }
tbody tr.dim td { color: var(--muted); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.empty { padding: 34px 16px; text-align: center; color: var(--muted); }
.tree-indent { display: inline-block; }

/* ---------- Controls ---------- */
button, .btn {
  font: inherit; font-size: 13px; padding: 7px 13px; border-radius: 8px; border: 1px solid var(--line-strong);
  background: var(--surface); color: var(--text); cursor: pointer; white-space: nowrap;
}
button:hover, .btn:hover { background: var(--surface-2); }
button:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #1a5fcc; }
.btn-danger { background: var(--surface); border-color: var(--line-strong); color: var(--red); }
.btn-danger:hover { background: var(--red-soft); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { padding: 4px 9px; font-size: 12px; border-radius: 7px; }

input, select, textarea {
  font: inherit; font-size: 13px; padding: 7px 10px; border: 1px solid var(--line-strong);
  border-radius: 8px; background: #fff; color: var(--text); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
input[type=checkbox] { width: auto; }
textarea { min-height: 68px; resize: vertical; }
label.field { display: block; }
[hidden] { display: none !important; }
label.field > span { display: block; font-size: 11.5px; color: var(--muted); margin-bottom: 4px; font-weight: 500; }
.search { max-width: 260px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.form-grid .full { grid-column: 1 / -1; }

/* ---------- Badges ---------- */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--line);
  white-space: nowrap;
}
.badge.green { background: var(--green-soft); color: var(--green); border-color: transparent; }
.badge.amber { background: var(--amber-soft); color: var(--amber); border-color: transparent; }
.badge.red { background: var(--red-soft); color: var(--red); border-color: transparent; }
.badge.blue { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.purple { background: var(--purple-soft); color: var(--purple); border-color: transparent; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; flex-wrap: wrap; }
.tab { padding: 8px 14px; border-radius: 8px 8px 0 0; cursor: pointer; font-size: 13px; color: var(--muted); border: 1px solid transparent; border-bottom: none; }
.tab:hover { color: var(--text); }
.tab.active { background: var(--surface); color: var(--text); font-weight: 600; border-color: var(--line); margin-bottom: -1px; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(16, 24, 40, .45); display: grid; place-items: center;
  padding: 24px; z-index: 100; overflow-y: auto;
}
.modal {
  background: var(--surface); border-radius: 14px; width: min(var(--w, 620px), 100%);
  box-shadow: 0 24px 64px rgba(16,24,40,.28); max-height: 92vh; display: flex; flex-direction: column;
}
.modal-head { padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; background: var(--surface-2); border-radius: 0 0 14px 14px; }

/* ---------- Toast ---------- */
#toast-root { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast {
  background: #1b2027; color: #fff; padding: 11px 15px; border-radius: 9px; font-size: 13px;
  box-shadow: 0 12px 32px rgba(16,24,40,.3); max-width: 380px; animation: pop .18s ease-out;
}
.toast.error { background: var(--red); }
.toast.success { background: var(--green); }
@keyframes pop { from { opacity: 0; transform: translateY(8px); } }

.line-table td { padding: 5px 6px; }
.line-table input, .line-table select { padding: 5px 8px; font-size: 12.5px; }
.line-table td.num input { text-align: right; }

.kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 14px; font-size: 13px; }
.kv dt { color: var(--muted); font-size: 12.5px; }
.kv dd { margin: 0; }

.bar { height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; min-width: 60px; }
.bar > span { display: block; height: 100%; background: var(--accent); }

.loading { padding: 40px; text-align: center; color: var(--muted); }

@media (max-width: 860px) {
  #app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  .sidebar nav { display: flex; flex-wrap: wrap; }
  .nav-group { width: 100%; }
}

@media print {
  .sidebar, .topbar, #page-actions, .no-print { display: none !important; }
  .view { padding: 0; }
  .card { box-shadow: none; border: 0; }
  .modal-backdrop { position: static; background: none; padding: 0; }
  .modal { box-shadow: none; max-height: none; width: 100%; }
  .modal-foot, .modal-head button { display: none; }
}

/* ---------- Sign in ---------- */
body.auth-mode #app { grid-template-columns: 1fr; }
body.auth-mode .sidebar, body.auth-mode .topbar { display: none; }
body.auth-mode .view { padding: 0; }

.login-wrap {
  min-height: 100vh; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(1200px 600px at 50% -10%, #e8eef7 0%, var(--bg) 60%);
}
.login-card {
  width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: var(--shadow); padding: 26px; display: grid; gap: 14px;
}
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.login-error {
  background: var(--red-soft); color: var(--red); padding: 9px 12px; border-radius: 8px; font-size: 12.5px;
}

/* Quick sign in for testing */
.login-wrap.with-quick { grid-auto-flow: column; grid-auto-columns: min(380px, 100%); gap: 20px; align-items: start; justify-content: center; }
@media (max-width: 820px) { .login-wrap.with-quick { grid-auto-flow: row; } }
.quick-card { gap: 10px; border-style: dashed; }
.quick-list { display: grid; gap: 6px; max-height: 60vh; overflow: auto; }
.quick-user {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; padding: 8px 10px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; cursor: pointer; font: inherit; color: inherit;
}
.quick-user:hover { border-color: var(--accent); background: var(--accent-soft); }
.quick-user.busy { opacity: 0.6; pointer-events: none; }
.quick-user .avatar {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: #24344a; color: #dce4ee; font-size: 11px; font-weight: 700;
}
.quick-text { display: grid; min-width: 0; flex: 1; }
.quick-name { font-size: 13px; font-weight: 600; }
.quick-role { font-size: 11.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.quick-go { color: var(--muted); font-size: 14px; }

/* ---------- Signed in user ---------- */
.user-chip { display: flex; align-items: center; gap: 10px; }
.user-chip .avatar {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: #24344a; color: #dce4ee; font-size: 11px; font-weight: 700;
}
.user-name { color: #e6ecf3; font-size: 12.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-role { font-size: 11px; color: #6f7d8d; }

/* ---------- Permission matrix ---------- */
.matrix td, .matrix th { text-align: center; }
.matrix td:first-child, .matrix th:first-child { text-align: left; }
.matrix select { width: auto; min-width: 92px; padding: 4px 8px; font-size: 12px; }
.access-none { color: var(--muted); }
.access-view { color: var(--accent); font-weight: 600; }
.access-edit { color: var(--green); font-weight: 600; }

/* ---------- Specifications ---------- */
.spec-ok { color: var(--green); font-weight: 600; }
.spec-bad { color: var(--red); font-weight: 600; }
.spec-missing { color: var(--amber); font-weight: 600; }
.spec-cell { display: flex; gap: 6px; align-items: center; justify-content: flex-end; }
tr.spec-deviation td { background: #fff7f6; }
tr.spec-missing-row td { background: #fffaf0; }
.compare-wrap table { min-width: 640px; }
.compare-wrap th.supplier-col { text-align: right; }

/* ---------- Code snippets ---------- */
.snippet {
  margin: 0; padding: 12px 14px; background: #131a24; color: #d6dee8; border-radius: 9px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px; line-height: 1.6;
  overflow-x: auto; white-space: pre;
}
.snippet .k { color: #7fd3a1; }

/* ---------- Top bar tools ---------- */
.bell { position: relative; font-size: 16px; padding: 4px 8px; }
.bell-count { position: absolute; top: -2px; right: -2px; background: var(--red); color: #fff; font-size: 10px; font-weight: 700; border-radius: 999px; padding: 1px 5px; min-width: 16px; text-align: center; }
.notice { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.notice:hover { background: var(--surface-2); }
.notice.read { opacity: .6; }
.notice:last-child { border-bottom: 0; }

/* ---------- Grids and misc ---------- */
.bucket-grid td.num { font-size: 12px; }
.bucket-grid td.neg { color: var(--red); font-weight: 600; }
.bucket-grid th.week { min-width: 62px; }
.tree { list-style: none; padding-left: 18px; margin: 0; }
.tree > li { padding: 3px 0; border-left: 1px solid var(--line); padding-left: 10px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; white-space: pre-wrap; }
.cap-cell { text-align: center; font-size: 11.5px; padding: 6px 4px !important; }
.cap-ok { background: var(--green-soft); }
.cap-warn { background: var(--amber-soft); }
.cap-over { background: var(--red-soft); color: var(--red); font-weight: 600; }
.op-card { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; background: var(--surface); display: grid; gap: 6px; }
.op-card.running { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.op-card.late { border-left: 4px solid var(--red); }
.big { font-size: 15px; font-weight: 600; }
.pill-list { display: flex; flex-wrap: wrap; gap: 6px; }
.recovery { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; font-family: ui-monospace, Menlo, monospace; font-size: 13px; }
.recovery span { background: var(--surface-2); padding: 6px 10px; border-radius: 6px; border: 1px solid var(--line); }
.stat.small .value { font-size: 18px; }

/* ---------- Procurement desk ---------- */
.qr-box { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 12px; background: #fff; border: 1px solid var(--line); border-radius: 10px; text-align: center; }
.qr-box img { image-rendering: pixelated; max-width: 100%; }
.bank-card { display: grid; grid-template-columns: 1fr auto; gap: 16px; align-items: start; padding: 12px 16px; }
.bank-card .kv { grid-template-columns: 120px 1fr; }
@media (max-width: 720px) { .bank-card { grid-template-columns: 1fr; } }

/* ---------- Workspaces, menus, form sections ---------- */
.workspace { padding: 0 28px; }
.ws-tabs { display: flex; gap: 2px; flex-wrap: wrap; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.ws-tab { padding: 9px 12px; font-size: 13px; color: var(--muted); text-decoration: none; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.ws-tab:hover { color: var(--text); }
.ws-tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.menu-wrap { position: relative; display: inline-block; }
.menu-list { position: absolute; right: 0; top: calc(100% + 6px); min-width: 240px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 12px 32px rgba(15, 23, 42, .16); padding: 6px; z-index: 60; display: flex; flex-direction: column; }
.menu-item { display: flex; gap: 10px; align-items: flex-start; width: 100%; text-align: left; background: none; border: none; padding: 8px 10px; border-radius: 7px; font: inherit; color: var(--text); cursor: pointer; }
.menu-item:hover { background: var(--bg); }
.menu-item.danger { color: var(--red); }
.menu-item .ico { width: 18px; text-align: center; }
.menu-text { display: flex; flex-direction: column; gap: 1px; }
.menu-hint { font-size: 11.5px; color: var(--muted); }
.form-section { grid-column: 1 / -1; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); padding-top: 10px; border-top: 1px solid var(--line); margin-top: 4px; }
.form-more { grid-column: 1 / -1; }
.form-more > button { background: none; border: none; color: var(--accent); font: inherit; font-size: 12.5px; cursor: pointer; padding: 4px 0; }
.form-advanced { display: contents; }
.attention { display: flex; flex-direction: column; }
.attention-row { display: flex; gap: 12px; align-items: center; padding: 10px 16px; border-top: 1px solid var(--line); cursor: pointer; }
.attention-row:first-child { border-top: none; }
.attention-row:hover { background: var(--bg); }
.attention-row .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--accent); }
.attention-row.warning .dot { background: var(--amber); }
.attention-row.danger .dot { background: var(--red); }
.attention-row .what { flex: 1; min-width: 0; }
.attention-row .what small { display: block; color: var(--muted); font-size: 11.5px; }
.quick-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; padding: 12px 16px; }
.quick-action { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); cursor: pointer; text-align: left; font: inherit; }
.quick-action:hover { border-color: var(--accent); }
.quick-action .ico { font-size: 18px; }
.quick-action small { display: block; color: var(--muted); font-size: 11px; }
