* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --accent-soft: #eff6ff;
  --green: #16a34a; --green-bg: #dcfce7;
  --amber: #d97706; --amber-bg: #fef3c7;
  --red: #dc2626; --red-bg: #fee2e2;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 1px 3px rgba(15, 23, 42, .07);
  --shadow-lg: 0 8px 25px rgba(15, 23, 42, .14);
}
html, body { height: 100%; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
  font-size: 14px; background: var(--bg); color: var(--text);
}
a { color: var(--accent); }
.app { display: flex; flex-direction: column; min-height: 100vh; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center;
  justify-content: space-between; gap: 12px; height: 60px; padding: 0 20px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 12px; }
.brand { font-weight: 700; font-size: 17px; letter-spacing: .3px; display: flex; align-items: center; gap: 9px; }
.brand::before {
  content: ""; width: 11px; height: 11px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #06b6d4);
  box-shadow: 0 0 0 4px var(--accent-soft);
}
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: var(--bg); }
.user-chip { display: flex; align-items: center; gap: 9px; }
.user-avatar {
  width: 34px; height: 34px; border-radius: 50%; display: flex; align-items: center;
  justify-content: center; background: linear-gradient(135deg, var(--accent), #4f46e5);
  color: #fff; font-weight: 700; font-size: 13px;
}
.user-name { font-weight: 600; font-size: 13px; line-height: 1.1; }
.user-role { display: block; font-size: 11px; color: var(--muted); }
.top-link { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); text-decoration: none; font-size: 13px; padding: 6px 10px; border-radius: 8px; }
.top-link:hover { background: var(--bg); color: var(--text); }

/* ---------- Layout ---------- */
.layout { display: flex; flex: 1; }
.sidebar {
  width: 240px; flex-shrink: 0; background: var(--surface);
  border-right: 1px solid var(--border); padding: 14px 12px 16px;
  display: flex; flex-direction: column; gap: 2px;
}
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar a { text-decoration: none; color: #334155; }
.sidebar .nav-group {
  margin: 14px 10px 4px; font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .8px; color: #94a3b8;
}
.sidebar nav > a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  border-radius: 9px; font-size: 13.5px; font-weight: 500;
}
.sidebar nav > a:hover { background: var(--bg); color: var(--text); }
.sidebar nav > a.active { background: var(--accent-soft); color: var(--accent-dark); font-weight: 600; }
.sidebar nav > a.active .ic { color: var(--accent); }
.sidebar-footer { margin-top: auto; border-top: 1px solid var(--border); padding-top: 10px; display: flex; flex-direction: column; gap: 2px; }
.sidebar-footer a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 9px; font-size: 13px; color: var(--muted); }
.sidebar-footer a:hover { background: var(--bg); color: var(--text); }
.main { flex: 1; padding: 24px 28px 40px; min-width: 0; }
.main-standalone { padding: 0; }

/* ---------- Icons (inline SVG) ---------- */
.ic { width: 18px; height: 18px; stroke: currentColor; stroke-width: 2; fill: none; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }

/* ---------- Typography ---------- */
h1 { font-size: 21px; margin-bottom: 4px; color: var(--text); letter-spacing: -.2px; }
h2 { font-size: 15.5px; margin: 18px 0 12px; color: var(--text); }
p { line-height: 1.55; }
pre { background: #0f172a; color: #e2e8f0; padding: 12px 14px; border-radius: 10px; font-size: 12.5px; overflow: auto; margin: 8px 0; }
code { background: #eef2f7; padding: 1px 6px; border-radius: 6px; font-size: 12.5px; }

/* ---------- Cards / KPIs ---------- */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--border); padding: 18px; margin-bottom: 16px; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 16px; }
.chart-box { position: relative; width: 100%; height: 300px; }
.chart-box canvas { width: 100% !important; height: 100% !important; max-height: 300px; }
.kpi { display: block; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; text-decoration: none; color: inherit; }
.kpi-link:hover { border-color: var(--accent); }
.kpi .label { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; font-weight: 700; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; }
.kpi .value.danger { color: var(--red); }
.kpi .value.warn { color: var(--amber); }
.kpi .value.info { color: var(--accent); }
.kpi .sub { margin-top: 4px; font-size: 12px; color: var(--muted); }
.up { color: var(--green); } .down { color: var(--red); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 9px 11px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
thead th { background: #f8fafc; color: #475569; font-weight: 700; font-size: 11.5px; text-transform: uppercase; letter-spacing: .4px; position: sticky; top: 0; }
tbody tr:hover td { background: #f8fafc; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr td { color: #1e293b; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; white-space: nowrap; }
.badge.ok { background: var(--green-bg); color: var(--green); }
.badge.ok::before { content: "✓ "; }
.badge.warn { background: var(--amber-bg); color: var(--amber); }
.badge.err { background: var(--red-bg); color: var(--red); }
.badge.err::before { content: "✕ "; }
.badge.info { background: var(--accent-soft); color: var(--accent-dark); }

/* ---------- Flash ---------- */
.flash { padding: 11px 14px; border-radius: 10px; margin-bottom: 12px; font-size: 13.5px; border: 1px solid transparent; }
.flash-success { background: var(--green-bg); color: #166534; border-color: #bbf7d0; }
.flash-danger { background: var(--red-bg); color: #b91c1c; border-color: #fecaca; }
.flash-warning { background: var(--amber-bg); color: #92400e; border-color: #fde68a; }
.flash-info { background: var(--accent-soft); color: #1e40af; border-color: #bfdbfe; }

/* ---------- Toasts ---------- */
.toasts { position: fixed; top: 72px; right: 18px; z-index: 100; display: flex; flex-direction: column; gap: 10px; max-width: 380px; }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  box-shadow: var(--shadow-lg); padding: 12px 16px; border-radius: 10px; font-size: 13.5px;
  animation: slideIn .25s ease;
}
.toast-success { border-left-color: var(--green); }
.toast-danger { border-left-color: var(--red); }
.toast-warning { border-left-color: var(--amber); }
.toast-info { border-left-color: var(--accent); }
.toast.leaving { opacity: 0; transform: translateX(20px); transition: all .3s; }
@keyframes slideIn { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: none; } }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 15px; border-radius: 9px;
  border: 1px solid transparent; font-size: 13px; font-weight: 600; cursor: pointer;
  text-decoration: none; background: var(--accent); color: #fff; transition: background .15s;
}
.btn:hover { background: var(--accent-dark); color: #fff; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-secondary:hover { background: var(--bg); color: var(--text); }
.btn-danger { background: #dc2626; }
.btn-danger:hover { background: #b91c1c; }
.btn-danger.btn-secondary { background: var(--surface); color: var(--red); border-color: #fecaca; }
.btn-danger.btn-secondary:hover { background: var(--red-bg); }
.btn-warn { background: #d97706; }
.btn-warn:hover { background: #b45309; }
.btn-warn.btn-secondary { background: var(--surface); color: var(--amber); border-color: #fde68a; }
.btn-warn.btn-secondary:hover { background: var(--amber-bg); }

/* ---------- Forms ---------- */
.form-row { margin-bottom: 13px; }
.form-row label { display: block; font-size: 12px; color: #475569; margin-bottom: 5px; font-weight: 600; }
.form-row input, .form-row select, .form-row textarea,
.toolbar input, .toolbar select {
  width: 100%; padding: 8px 11px; border: 1px solid var(--border-strong); border-radius: 9px;
  font-size: 13.5px; background: var(--surface); color: var(--text);
}
.form-row input:focus, .form-row select:focus, .form-row textarea:focus,
.toolbar input:focus, .toolbar select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.form-actions { margin-top: 16px; display: flex; gap: 10px; }
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar input, .toolbar select { width: auto; }

/* ---------- Utility ---------- */
.flex { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.mt { margin-top: 14px; }
.muted { color: var(--muted); font-size: 12.5px; }
.tabs a { margin-right: 10px; text-decoration: none; font-size: 13px; color: var(--accent); }
.pagination { display: flex; align-items: center; justify-content: flex-end; gap: 12px; margin-top: 14px; }
.edit-box { margin-top: 14px; border-top: 1px dashed var(--border); padding-top: 8px; }
.edit-box summary { cursor: pointer; font-size: 13px; color: var(--accent); font-weight: 600; }
.edit-box summary:hover { text-decoration: underline; }
.error-code { font-size: 54px; font-weight: 800; color: var(--accent); line-height: 1; }

/* ---------- Login ---------- */
.login-wrap { margin: auto; display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; background: radial-gradient(1000px 500px at 20% -10%, #dbeafe, transparent), linear-gradient(160deg, #f8fafc, #e0e7ff); }
.login-box { background: var(--surface); padding: 36px; border-radius: 16px; width: 360px; box-shadow: var(--shadow-lg); border: 1px solid var(--border); }
.login-box h1 { font-size: 21px; margin-bottom: 4px; display: flex; align-items: center; gap: 10px; }
.login-box p.sub { color: var(--muted); font-size: 13px; margin-bottom: 22px; }
.login-box .form-row input { padding: 11px 12px; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .sidebar { position: fixed; top: 60px; bottom: 0; left: 0; z-index: 40; transform: translateX(-102%); transition: transform .2s; box-shadow: var(--shadow-lg); }
  .sidebar.open { transform: none; }
  .main { padding: 18px 16px 32px; }
  .hide-sm { display: none; }
}
@media print {
  .topbar, .sidebar, .toolbar, .btn, .pagination, .toasts { display: none !important; }
  .main { padding: 0; }
  .card { box-shadow: none; border: 1px solid #ddd; }
  body { background: #fff; }
  .layout { display: block; }
}