:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --panel: #ffffff;
  --ink: #1b2733;
  --muted: #657282;
  --soft: #8a96a3;
  --line: #dbe2ea;
  --line-2: #edf1f5;
  --accent: #126c7d;
  --accent-ink: #0e5562;
  --accent-soft: #e3f1f4;
  --warn: #b7791f;
  --bad: #c0392b;
  --good: #1f7a4d;
  --shadow: 0 10px 26px rgba(22, 34, 45, 0.08);
  --shadow-lg: 0 18px 40px rgba(22, 34, 45, 0.18);
  --radius: 10px;
  --tap: 44px;
  --nav-w: 240px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }

/* ------- Buttons ------- */
button {
  border: 0;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  min-height: var(--tap);
  font-weight: 600;
  transition: background .15s, transform .05s;
}
button:active { transform: translateY(1px); }
button:hover { background: var(--accent-ink); }
button.secondary { background: #eef2f6; color: var(--ink); }
button.secondary:hover { background: #e2e8ee; }
button.ghost { background: transparent; color: var(--accent); padding: 8px 10px; min-height: 36px; }
button.ghost:hover { background: var(--accent-soft); }
button.danger { background: var(--bad); }
button.danger:hover { background: #a23121; }
button.icon { padding: 0; width: var(--tap); min-height: var(--tap); display: inline-flex; align-items: center; justify-content: center; }
button:disabled { opacity: .55; cursor: not-allowed; }

/* ------- Inputs ------- */
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 12px;
  background: #fff;
  min-height: var(--tap);
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}
textarea { min-height: 90px; resize: vertical; }
label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .02em;
}
label > input, label > select, label > textarea { color: var(--ink); font-weight: 400; text-transform: none; letter-spacing: 0; }
.inline-check {
  display: inline-flex;
  gap: 10px;
  align-items: center;
  color: var(--ink);
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  padding: 10px 0;
  min-height: var(--tap);
}
.inline-check input { width: 20px; height: 20px; min-height: 20px; }

/* ------- Login ------- */
.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  padding-top: max(24px, var(--safe-top));
}
.login-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
  display: grid;
  gap: 14px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; color: var(--muted); font-size: 13px; }
.login-card button { margin-top: 4px; }

/* ------- App shell ------- */
.app {
  display: grid;
  grid-template-columns: var(--nav-w) minmax(0, 1fr);
  min-height: 100vh;
}
.sidebar {
  background: #16252e;
  color: #dbe8ef;
  padding: 18px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.brand { font-size: 20px; font-weight: 750; margin: 4px 8px 0; color: #fff; }
.brand small { display: block; color: #8aa2af; font-size: 11px; font-weight: 500; letter-spacing: .04em; text-transform: uppercase; margin-top: 2px; }
.nav { display: grid; gap: 4px; }
.nav button {
  text-align: left;
  background: transparent;
  color: #cfdde5;
  padding: 10px 12px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 500;
  min-height: 40px;
}
.nav button:hover { background: #243a45; }
.nav button.active { background: #234c5a; color: #fff; }
.nav .ico { font-size: 16px; width: 20px; text-align: center; }
.sidebar .who {
  margin-top: auto;
  padding: 10px 12px;
  border-top: 1px solid #243a45;
  color: #8aa2af;
  font-size: 12px;
}
.sidebar .who strong { display: block; color: #dbe8ef; font-size: 13px; font-weight: 600; margin-bottom: 2px; }

.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar .title { font-size: 18px; font-weight: 700; min-width: 0; }
.topbar .title small { display: block; color: var(--muted); font-size: 12px; font-weight: 500; }
.topbar .actions { display: flex; gap: 8px; align-items: center; }
.menu-btn { display: none; }

.content { padding: 22px; display: grid; gap: 18px; flex: 1; }

/* ------- Panels ------- */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.panel-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.panel-head strong { font-size: 15px; }
.panel-body { padding: 16px; }
.panel-body + .panel-body { padding-top: 0; }

.toolbar {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
}
.toolbar input { min-height: var(--tap); }

/* ------- Filter bar ------- */
.filter-bar {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  display: grid;
  gap: 10px;
}
.filter-row {
  display: grid;
  grid-template-columns: minmax(220px, 2fr) repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  align-items: center;
}
.filter-row .filter-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  grid-column: -1 / -1;
}
.filter-row .filter-actions button { white-space: nowrap; }
.filter-row label.field {
  display: grid;
  gap: 4px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  font-weight: 700;
}
.filter-row label.field > input,
.filter-row label.field > select { min-height: 40px; padding: 8px 10px; }
.filter-row > input[type="search"],
.filter-row > input.search {
  min-height: 40px;
  padding: 8px 36px 8px 36px;
  background: #fff url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23657282' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><circle cx='11' cy='11' r='7'/><line x1='21' y1='21' x2='16.65' y2='16.65'/></svg>") 12px center no-repeat;
}
.chip-list { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  padding: 4px 6px 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.chip button {
  background: transparent;
  color: inherit;
  border-radius: 999px;
  font-size: 14px;
  padding: 0 6px;
  min-height: 22px;
  line-height: 1;
  font-weight: 700;
}
.chip button:hover { background: rgba(14, 85, 98, .1); }

/* ------- Summary strip ------- */
.summary {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 28px;
  padding: 12px 16px;
  background: #f7fafc;
  border-bottom: 1px solid var(--line);
}
.summary > div { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.summary > div strong { color: var(--ink); display: block; font-size: 17px; font-weight: 700; text-transform: none; letter-spacing: 0; margin-top: 2px; }

/* ------- Group sections ------- */
.group { margin-bottom: 14px; }
.group:last-child { margin-bottom: 0; }
.group-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 11px 16px;
  background: #eef3f6;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: var(--radius) var(--radius) 0 0;
  font-weight: 700;
  font-size: 14px;
  color: var(--ink);
}
.group-head .count {
  background: #fff;
  color: var(--muted);
  border: 1px solid var(--line);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
}
.group .table-wrap {
  border: 1px solid var(--line);
  border-top: 0;
  border-radius: 0 0 var(--radius) var(--radius);
  background: var(--panel);
}
.group .empty {
  border: 1px solid var(--line);
  border-top: 0;
  border-radius: 0 0 var(--radius) var(--radius);
  background: var(--panel);
}

/* ------- Dashboard sections ------- */
.section-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 4px 2px 2px;
}
.section-title h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
}
.section-title .hint { color: var(--muted); font-size: 12px; }

.attention {
  background: linear-gradient(180deg, #fff8f5, #fff);
  border: 1px solid #f4cdc4;
}
.attention .panel-head { background: #ffe5e0; border-bottom-color: #f4cdc4; color: var(--bad); }
.attention .panel-head strong { color: var(--bad); }

.alert-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
  padding: 14px 16px;
}
.alert-item {
  background: #fff;
  border: 1px solid var(--line);
  border-left: 3px solid var(--warn);
  border-radius: 8px;
  padding: 10px 12px;
  display: grid;
  gap: 4px;
}
.alert-item.bad { border-left-color: var(--bad); }
.alert-item .name { font-weight: 600; color: var(--ink); }
.alert-item .meta { color: var(--muted); font-size: 12px; }

.history-row .ent {
  display: inline-block;
  background: #eef2f6;
  color: var(--ink);
  padding: 1px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .02em;
}
.history-row .act { color: var(--muted); font-size: 12px; }
.history-row .when { color: var(--soft); font-size: 11px; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}
.form-grid.single { grid-template-columns: 1fr; }

/* ------- Metrics ------- */
.cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}
.metric {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.metric .lbl { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.metric .val { font-size: 28px; font-weight: 700; line-height: 1.1; }
.metric .sub { color: var(--muted); font-size: 12px; }

/* ------- Tables ------- */
.table-wrap { overflow: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; min-width: 640px; }
th, td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--line-2);
  text-align: left;
  vertical-align: middle;
}
th {
  color: #42505d;
  background: #f7fafc;
  font-weight: 650;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .02em;
  position: sticky;
  top: 0;
}
tbody tr:nth-child(even) { background: #fafcfe; }
tbody tr:hover { background: #eef5f8; }
tbody tr:last-child td { border-bottom: 0; }
.empty {
  padding: 36px 20px;
  text-align: center;
  color: var(--muted);
}
.empty strong { display: block; color: var(--ink); font-size: 15px; margin-bottom: 4px; }

/* ------- Status pills ------- */
.status {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 12px;
  font-weight: 650;
  white-space: nowrap;
}
.status.good { background: #dff3e8; color: var(--good); }
.status.warn { background: #fff0cf; color: var(--warn); }
.status.bad  { background: #ffe1dd; color: var(--bad); }
.tag {
  display: inline-block;
  background: #eef2f6;
  color: var(--ink);
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 600;
}

/* ------- Actions ------- */
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.row-actions {
  display: inline-flex;
  gap: 4px;
}
.row-actions button {
  min-height: 36px;
  padding: 6px 10px;
  border-radius: 6px;
}

/* ------- Messages / toasts ------- */
.message {
  padding: 11px 14px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-size: 13px;
}
.message.error { background: #ffe5e2; color: var(--bad); }
.message.warn  { background: #fff4dc; color: var(--warn); }
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  transform: translateX(-50%) translateY(20px);
  background: #1b2733;
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 200;
  max-width: 92vw;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.toast.error { background: var(--bad); }
.toast.ok    { background: var(--good); }

/* ------- Modal ------- */
.modal-shell {
  position: fixed;
  inset: 0;
  background: rgba(22, 34, 45, .45);
  display: grid;
  place-items: center;
  padding: 18px;
  z-index: 100;
}
.modal {
  width: min(640px, 100%);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.modal .panel-head { border-bottom: 1px solid var(--line); }
.modal .panel-body { overflow: auto; }
.modal .footer {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding: 14px 16px;
  border-top: 1px solid var(--line);
  background: #f9fbfc;
}

/* ------- Drawer (mobile sidebar) ------- */
.drawer-backdrop {
  position: fixed; inset: 0;
  background: rgba(22, 34, 45, .5);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
.drawer-open .drawer-backdrop { opacity: 1; pointer-events: auto; }

/* ------- Bottom nav (mobile) ------- */
.bottom-nav { display: none; }

/* ===================== Tablet ===================== */
@media (max-width: 900px) {
  :root { --nav-w: 0px; }
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 280px;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform .25s;
    padding-top: max(18px, var(--safe-top));
  }
  body.drawer-open .sidebar { transform: translateX(0); }
  .menu-btn {
    display: inline-flex;
    background: transparent;
    color: var(--ink);
    padding: 0;
    width: 40px;
    min-height: 40px;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    font-size: 22px;
    line-height: 1;
  }
  .menu-btn:hover { background: #eef2f6; }
  .topbar {
    padding: 12px 16px;
    padding-top: max(12px, var(--safe-top));
  }
  .topbar .actions button:not(.icon):not(.menu-btn) {
    padding: 8px 10px;
    min-height: 38px;
    font-size: 13px;
  }
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid { grid-template-columns: 1fr; }
}

/* ===================== Phone ===================== */
@media (max-width: 620px) {
  body { font-size: 14px; }
  .content {
    padding: 14px;
    padding-bottom: calc(80px + var(--safe-bottom));
    gap: 14px;
  }
  .topbar { padding: 10px 14px; padding-top: max(10px, var(--safe-top)); }
  .topbar .title { font-size: 16px; }
  .topbar .title small { font-size: 11px; }
  .topbar .actions .backup-btn { display: none; }

  .panel-head {
    padding: 12px 14px;
    flex-wrap: wrap;
  }
  .panel-body { padding: 12px 14px; }

  .toolbar {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  .toolbar button { width: 100%; }
  .filter-row { grid-template-columns: 1fr 1fr; }
  .filter-row > input[type="search"],
  .filter-row > input.search { grid-column: 1 / -1; }
  .filter-row .filter-actions { grid-column: 1 / -1; flex-wrap: wrap; }
  .filter-row .filter-actions button { flex: 1 1 calc(50% - 4px); }
  .summary { padding: 10px 14px; gap: 4px 18px; }
  .summary > div strong { font-size: 15px; }
  .alert-grid { grid-template-columns: 1fr; padding: 12px; }

  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .metric { padding: 13px; gap: 4px; }
  .metric .val { font-size: 22px; }
  .metric .lbl { font-size: 11px; }

  /* Card-list rendering for tables */
  table { min-width: 0; border-collapse: separate; border-spacing: 0; display: block; }
  thead { display: none; }
  tbody, tr, td { display: block; }
  tbody { padding: 8px 12px 12px; }
  tr {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 12px;
    margin-bottom: 10px;
    box-shadow: 0 1px 2px rgba(22,34,45,.04);
  }
  tr:hover { background: #fff; }
  td {
    border-bottom: 0;
    padding: 5px 0;
    display: grid;
    grid-template-columns: 110px 1fr;
    gap: 10px;
    align-items: baseline;
  }
  td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .03em;
    font-weight: 600;
  }
  td[colspan]::before, td.full::before { content: none; }
  td.full { grid-template-columns: 1fr; }
  td.actions-cell {
    grid-template-columns: 1fr;
    padding-top: 8px;
    margin-top: 6px;
    border-top: 1px dashed var(--line-2);
  }
  td.actions-cell::before { content: none; }
  .row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
  .row-actions button {
    flex: 1 1 auto;
    min-height: 38px;
  }
  .empty { padding: 24px 14px; }

  /* Full-screen modal */
  .modal-shell { padding: 0; align-items: stretch; }
  .modal {
    width: 100%;
    max-height: 100vh;
    height: 100vh;
    border-radius: 0;
  }
  .modal .panel-head { padding-top: max(14px, var(--safe-top)); }
  .modal .footer { padding-bottom: max(14px, var(--safe-bottom)); }

  /* Bottom navigation */
  .bottom-nav {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--panel);
    border-top: 1px solid var(--line);
    z-index: 30;
    padding-bottom: var(--safe-bottom);
    box-shadow: 0 -2px 10px rgba(22,34,45,.05);
  }
  .bottom-nav button {
    background: transparent;
    color: var(--muted);
    padding: 6px 4px;
    border-radius: 0;
    min-height: 56px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    font-size: 11px;
    font-weight: 600;
  }
  .bottom-nav button .ico { font-size: 20px; line-height: 1; }
  .bottom-nav button.active { color: var(--accent); }
  .bottom-nav button:hover { background: transparent; }

  .login { padding: 16px; }
  .login-card { padding: 22px; }
}
