:root {
  --bg: #0f172a;
  --panel: #ffffff;
  --app-bg: #f1f5f9;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #2563eb;
  --brand-ink: #1d4ed8;
  --green: #16a34a;
  --amber: #d97706;
  --red: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15,23,42,.08), 0 1px 2px rgba(15,23,42,.06);
}
* { box-sizing: border-box; }
/* The hidden attribute must always win, even over classes that set display
   (e.g. .modal { display: grid }). Without this the modal shows on load. */
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--app-bg);
  color: var(--ink);
  min-height: 100vh;
}
.app-shell { display: flex; min-height: 100vh; }

/* Login */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 20px;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%); }
.login-card {
  background: #fff; width: 100%; max-width: 380px; border-radius: 16px; padding: 32px 28px;
  box-shadow: 0 20px 50px rgba(0,0,0,.35); display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.login-logo { width: 56px; height: 56px; border-radius: 14px; background: var(--brand); color: #fff;
  font-weight: 800; font-size: 28px; display: grid; place-items: center; margin: 0 auto 4px; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-sub { margin: 0 0 10px; color: var(--muted); font-size: 13px; }
.login-card .field { text-align: left; }
.login-btn { margin-top: 8px; padding: 12px; font-size: 15px; }
.login-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; border-radius: 9px; padding: 9px 12px; font-size: 13px; }

/* Topbar user area */
.topbar-right { display: flex; align-items: center; gap: 14px; }
.user-chip { display: flex; align-items: center; gap: 8px; }
.user-name { font-size: 14px; font-weight: 600; }
.menu-btn { display: none; margin-left: auto; background: transparent; border: 0; color: #fff; font-size: 24px; cursor: pointer; padding: 0 4px; }

/* Sidebar */
.sidebar {
  width: 248px;
  background: var(--bg);
  color: #cbd5e1;
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 4px 8px 20px; }
.brand-logo {
  width: 40px; height: 40px; border-radius: 10px;
  background: var(--brand); color: #fff; font-weight: 800; font-size: 20px;
  display: grid; place-items: center;
}
.brand-name { color: #fff; font-weight: 700; }
.brand-sub { font-size: 12px; color: #94a3b8; }
nav { display: flex; flex-direction: column; gap: 4px; }
nav button {
  text-align: left; background: transparent; border: 0; color: #cbd5e1;
  padding: 11px 12px; border-radius: 9px; cursor: pointer; font-size: 14px;
}
nav button:hover { background: rgba(255,255,255,.06); color: #fff; }
nav button.active { background: var(--brand); color: #fff; }
.sidebar-foot { margin-top: auto; font-size: 11px; color: #64748b; padding: 14px 8px 0; }

/* Main */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 28px; background: #fff; border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.topbar h1 { margin: 0; font-size: 20px; }
.content { padding: 24px 28px 48px; }

/* Buttons */
.btn {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 9px 14px; border-radius: 9px; cursor: pointer; font-size: 14px; font-weight: 600;
}
.btn:hover { background: #f8fafc; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-ink); }
.btn.sm { padding: 5px 10px; font-size: 12px; }
.btn.danger { color: var(--red); border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; }
.icon-btn { border: 0; background: transparent; font-size: 18px; cursor: pointer; color: var(--muted); }

/* KPI cards */
.kpi-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 14px; margin-bottom: 24px;
}
.kpi {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.kpi .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 26px; font-weight: 800; margin-top: 6px; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.warn .value { color: var(--amber); }
.kpi.bad .value { color: var(--red); }
.kpi.good .value { color: var(--green); }

/* Panels */
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); margin-bottom: 20px; overflow: hidden;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--line);
}
.panel-head h3 { margin: 0; font-size: 15px; }
.panel-head .count { font-size: 12px; color: var(--muted); }
.panel-body { padding: 4px 0; }
.alert-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 900px) { .alert-grid { grid-template-columns: 1fr; } }

/* Tables */
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 11px 18px; border-bottom: 1px solid var(--line); }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: #f8fafc; }
tbody tr:hover { background: #f8fafc; }
td .row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.empty { padding: 28px; text-align: center; color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Badges */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.gray { background: #f1f5f9; color: #475569; }
.badge.green { background: #dcfce7; color: #166534; }
.badge.amber { background: #fef3c7; color: #92400e; }
.badge.red { background: #fee2e2; color: #991b1b; }
.badge.blue { background: #dbeafe; color: #1e40af; }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal-card { background: #fff; border-radius: 14px; width: 100%; max-width: 560px; max-height: 90vh; overflow: auto; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 22px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-form { padding: 20px 22px; display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.modal-form .full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 13px; font-weight: 600; color: #334155; }
.field input, .field select, .field textarea {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; font-size: 14px; font-family: inherit;
}
.field textarea { resize: vertical; min-height: 60px; }
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--brand); border-color: var(--brand); }
.modal-actions { grid-column: 1 / -1; display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }

/* Toast */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 11px 18px; border-radius: 10px;
  font-size: 14px; z-index: 60; box-shadow: var(--shadow);
}

@media (max-width: 820px) {
  body { flex-direction: column; }

  /* Sidebar becomes a top bar; nav collapses behind a hamburger (discoverable) */
  .sidebar {
    width: 100%; height: auto; position: sticky; top: 0; z-index: 20;
    flex-direction: column; padding: 0;
    box-shadow: 0 2px 8px rgba(0,0,0,.25);
  }
  .brand { padding: 12px 16px; margin: 0; }
  .brand-logo { width: 34px; height: 34px; font-size: 17px; }
  .menu-btn { display: block; }
  /* Collapsed by default; .open reveals the full vertical menu with labels */
  nav {
    display: none; flex-direction: column; gap: 4px;
    padding: 6px 12px 12px; border-top: 1px solid rgba(255,255,255,.08);
  }
  .sidebar.open nav { display: flex; }
  nav button { font-size: 15px; padding: 13px 14px; }
  .sidebar-foot { display: none; }

  /* Topbar + content spacing */
  .topbar { padding: 12px 16px; position: static; flex-wrap: wrap; gap: 10px; }
  .topbar h1 { font-size: 18px; }
  .topbar-right { gap: 10px; flex-wrap: wrap; }
  .user-name { display: none; }
  .content { padding: 16px; }

  /* KPI cards: two per row on phones */
  .kpi-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .kpi { padding: 13px; }
  .kpi .value { font-size: 22px; }

  /* Dashboard mini-tables scroll horizontally instead of breaking layout */
  .panel-body { overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* Entity list tables (.panel > table) become stacked cards.
     Scope to panels whose direct child is a table so dashboard cards are untouched. */
  .panel:has(> table) { border: 0; background: transparent; box-shadow: none; overflow: visible; }
  .panel > table { background: transparent; }
  .panel > table thead { display: none; }
  .panel > table, .panel > table tbody { display: block; width: 100%; }
  .panel > table tr {
    display: block; background: var(--panel); border: 1px solid var(--line);
    border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 12px; padding: 4px 2px;
  }
  .panel > table td {
    display: flex; align-items: center; justify-content: space-between; gap: 14px;
    border: 0; border-bottom: 1px solid var(--line); padding: 10px 16px; text-align: right;
  }
  .panel > table tr td:last-child { border-bottom: 0; }
  .panel > table td::before {
    content: attr(data-label); font-size: 12px; font-weight: 600; color: var(--muted);
    text-transform: uppercase; letter-spacing: .03em; text-align: left; flex: 0 0 auto;
  }
  .panel > table td.actions-cell { justify-content: flex-end; padding-top: 12px; padding-bottom: 12px; }
  .panel > table td.actions-cell::before { display: none; }
  .panel > table td .row-actions { gap: 10px; }
  .panel > table td .row-actions .btn { padding: 8px 16px; font-size: 13px; }

  /* Forms stack to one column */
  .modal-form { grid-template-columns: 1fr; padding: 18px; }
  .modal-card { max-height: 92vh; }
}

@media (max-width: 380px) {
  .kpi-grid { grid-template-columns: 1fr; }
}
