:root {
  --bg: #f2f8f4;
  --card: #ffffff;
  --line: #dbe9e0;
  --shadow: 0 1px 2px rgba(16, 64, 40, .06), 0 8px 24px rgba(16, 64, 40, .06);
  --green: #0c5a3a;
  --green-hover: #094730;
  --green-soft: #e4f1e9;
  --text: #17251d;
  --muted: #5c6f64;
  --danger: #a3271a;
  --danger-soft: #fbe9e6;
  --warn: #9a6400;
  --warn-soft: #fff4d6;
  --focus: #1c7d52;
  --radius: 14px;
  --ease: cubic-bezier(.2, .7, .3, 1);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  overflow-x: hidden;
}

h1, h2, h3 { color: var(--green); margin: 0; font-weight: 650; }
h1 { font-size: 28px; line-height: 1.2; }
h2 { font-size: 18px; }
h3 { font-size: 16px; }
p { margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }

a { color: var(--green); }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.skip-link {
  position: absolute; left: 12px; top: -48px; z-index: 100;
  background: var(--green); color: #fff; padding: 8px 12px; border-radius: 8px;
}
.skip-link:focus { top: 12px; }

.boot-note { padding: 32px; color: var(--muted); }

/* Buttons and inputs */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 16px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer;
  transition: background-color .14s var(--ease), border-color .14s var(--ease);
}
.btn:hover { border-color: #b9d4c3; background: #f7fbf8; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn-primary:hover { background: var(--green-hover); border-color: var(--green-hover); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); border-color: #e8b8af; }
.btn-small { min-height: 32px; padding: 4px 10px; font-size: 14px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field label, .label { font-weight: 600; }
.hint { color: var(--muted); font-size: 13px; }
.placeholder-list { list-style: none; margin: 8px 0; padding: 0; columns: 2 260px; }
.placeholder-list li { padding: 2px 0; break-inside: avoid; }
.placeholder-list code { font: inherit; font-weight: 600; color: var(--text); }
input[type=text], input[type=password], input[type=number], input[type=search], input[type=tel], input[type=date], input:not([type]), select { max-width: 560px; }
.field-error { color: var(--danger); font-size: 13px; }

input[type="text"], input[type="password"], input[type="number"], input[type="search"],
input[type="date"], select, textarea {
  width: 100%; min-height: 40px; padding: 8px 10px;
  border: 1px solid #c4d9cb; border-radius: 10px; background: #fff;
  color: var(--text); font: inherit;
}
textarea { min-height: 96px; resize: vertical; }
input[aria-invalid="true"], textarea[aria-invalid="true"] { border-color: var(--danger); }

.check-row { display: flex; align-items: center; gap: 10px; }
.check-row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--green); }

/* Switch */
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input {
  appearance: none; width: 44px; height: 24px; margin: 0; flex: none;
  border-radius: 999px; background: #b7c7bd; position: relative; cursor: pointer;
  transition: background-color .14s var(--ease);
}
.switch input::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  border-radius: 50%; background: #fff; transition: transform .14s var(--ease);
}
.switch input:checked { background: var(--green); }
.switch input:checked::after { transform: translateX(20px); }
.switch input:disabled { opacity: .5; cursor: not-allowed; }

/* Cards and layout */
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px;
}
.card { margin-bottom: 16px; }
.card .card { margin-bottom: 0; }
.row + .card { margin-top: 16px; }
.card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.grid-2 { display: grid; grid-template-columns: 1fr; gap: 16px; }
.stack > * + * { margin-top: 16px; }

.app { min-height: 100vh; }
.shell { display: grid; grid-template-columns: 240px minmax(0, 1fr); min-height: 100vh; }

/* Sidebar */
.sidebar {
  background: var(--green); color: #fff; padding: 20px 14px;
  display: flex; flex-direction: column; gap: 6px;
}
.brand { padding: 4px 10px 16px; }
.brand strong { display: block; font-size: 17px; font-weight: 650; }
.brand span { font-size: 13px; opacity: .8; }
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav a {
  color: #fff; text-decoration: none; padding: 9px 12px; border-radius: 10px;
  transition: background-color .14s var(--ease);
}
.nav a:hover { background: rgba(255, 255, 255, .12); }
.nav a[aria-current="page"] { background: rgba(255, 255, 255, .22); font-weight: 600; }
.sidebar-foot { margin-top: auto; padding: 12px 10px 0; font-size: 13px; opacity: .85; }

.content { min-width: 0; }
.main { min-width: 0; padding: 28px 32px 48px; }
.link-button {
  background: none; border: 0; padding: 0; font: inherit; color: var(--green);
  text-decoration: underline; cursor: pointer; text-align: left;
}
.picker { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin: 0 0 16px; min-width: 0; }
.picker legend { padding: 0 6px; font-weight: 600; }
.picker-list { display: grid; gap: 6px; max-height: 220px; overflow-y: auto; margin: 10px 0 6px; padding-right: 4px; }
.picker-list[hidden] { display: none; }
.status-item .row { margin-top: 10px; }
.status-item .hint { margin-top: 4px; }
.topbar { display: none; }
.menu-btn {
  background: transparent; border: 1px solid rgba(255, 255, 255, .4); color: #fff;
  border-radius: 10px; min-height: 40px; min-width: 44px; cursor: pointer; font: inherit;
}
.backdrop { display: none; }

/* Header user bar */
.userbar { display: flex; align-items: center; gap: 12px; justify-content: flex-end; margin-bottom: 8px; }

/* Status strip */
.status-strip { display: grid; grid-template-columns: 1fr; gap: 12px; }
.status-item { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; }
.status-item .label { color: var(--muted); font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.status-item .value { font-size: 17px; font-weight: 650; }
.notice { background: var(--warn-soft); color: var(--warn); border-radius: 10px; padding: 10px 14px; font-weight: 600; margin-bottom: 16px; }
.notice-error { background: var(--danger-soft); color: var(--danger); }

/* Counters */
.counters { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin: 16px 0; }
.counter { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.counter .figure { font-size: 32px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; color: var(--green); }
.counter .label { color: var(--muted); font-size: 13px; }

/* AQI chip */
.aqi-chip {
  display: inline-flex; align-items: center; justify-content: center; min-width: 56px;
  padding: 3px 10px; border-radius: 8px; font-weight: 700; font-variant-numeric: tabular-nums;
  background: var(--band); color: var(--band-ink);
}
.band-label { display: inline-block; margin-left: 8px; color: var(--muted); font-size: 14px; }
.band-line { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 13px; font-weight: 600;
  background: var(--green-soft); color: var(--green);
}
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-muted { background: #eef2ef; color: var(--muted); }

/* Tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); box-shadow: var(--shadow); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { position: sticky; top: 0; background: #f7fbf8; color: var(--muted); font-size: 13px; font-weight: 600; z-index: 1; }
tbody tr:last-child td { border-bottom: 0; }
tr.is-above td { background: #fff8ec; }
tr.is-clickable { cursor: pointer; }
tr.is-clickable:hover td { background: #f5faf7; }
td.cell-actions { text-align: right; }
@media (min-width: 721px) {
  td[data-label="Data hour"], td[data-label="Last alert"], td[data-label="Time"], td[data-label="Last sent"] { white-space: nowrap; }
}
td.cell-actions > * { margin: 2px 0 2px 6px; }

/* Table to stacked cards on phones */
@media (max-width: 720px) {
  .table-wrap { overflow: visible; border: 0; background: none; box-shadow: none; }
  table, tbody, tr, td { display: block; width: 100%; }
  thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  tr {
    background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 6px 0; margin-bottom: 12px;
  }
  tr.is-above { background: #fff8ec; }
  tr.is-above td { background: transparent; }
  td { border-bottom: 0; padding: 8px 14px; display: flex; gap: 12px; justify-content: space-between; align-items: center; text-align: right; }
  td::before { content: attr(data-label); color: var(--muted); font-weight: 600; text-align: left; flex: none; }
  td:empty { display: none; }
  .cell-actions { justify-content: flex-end; }
  td.cell-actions::before { content: none; }
}

.empty { text-align: center; padding: 28px 16px; color: var(--muted); }
.empty strong { display: block; color: var(--text); margin-bottom: 4px; }

.skeleton { height: 14px; border-radius: 6px; background: linear-gradient(90deg, #eef4f0, #e2ece5, #eef4f0); background-size: 200% 100%; animation: shimmer 1.2s linear infinite; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

.spinner {
  width: 18px; height: 18px; border-radius: 50%; border: 2px solid #cfe0d6; border-top-color: var(--green);
  animation: spin .8s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 380px; }
.login-card h1 { margin-bottom: 4px; }

/* Pagination */
.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-top: 14px; }

/* Chat preview */
.bubble {
  background: #dff3e5; color: #0b2a19; border-radius: 14px 14px 14px 4px; padding: 12px 14px;
  max-width: 480px; white-space: pre-wrap; overflow-wrap: anywhere; box-shadow: 0 1px 2px rgba(16, 64, 40, .08);
}
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 4px; }
.chip {
  border: 1px solid #b9d4c3; background: #f4faf6; color: var(--green); border-radius: 999px;
  padding: 3px 10px; font: inherit; font-size: 13px; cursor: pointer; min-height: 30px;
  transition: background-color .14s var(--ease);
}
.chip:hover { background: var(--green-soft); }

.legend { display: grid; grid-template-columns: 1fr; gap: 8px; }
.legend-item { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.swatch { width: 18px; height: 18px; border-radius: 5px; background: var(--band); flex: none; }

/* Dialogs */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(16, 40, 28, .4); display: grid; place-items: center; padding: 16px; z-index: 50;
}
.modal {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); width: 100%; max-width: 520px;
  max-height: calc(100vh - 32px); overflow: auto; padding: 22px;
}
.modal h2 { margin-bottom: 10px; }
.modal-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; margin-top: 18px; }

/* Toasts */
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; max-width: calc(100vw - 32px); }
.toast {
  background: #123d2a; color: #fff; border-radius: 10px; padding: 10px 14px; box-shadow: var(--shadow);
  animation: rise .16s var(--ease);
}
.toast-error { background: var(--danger); }
@keyframes rise { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Field-level text inside table cells */
.stack-tight { display: flex; flex-direction: column; gap: 2px; }
.pre-wrap { white-space: pre-wrap; overflow-wrap: anywhere; }
.detail-grid { display: grid; grid-template-columns: 1fr; gap: 8px 16px; margin-bottom: 14px; }
.detail-grid dt { color: var(--muted); font-size: 13px; font-weight: 600; }
.detail-grid dd { margin: 0; }

.qr { width: 280px; max-width: 100%; height: auto; background: #fff; border-radius: 10px; border: 1px solid var(--line); }
.big-status { font-size: 22px; font-weight: 650; color: var(--green); margin-bottom: 6px; }
.steps { padding-left: 20px; margin: 8px 0 0; }
.steps li { margin-bottom: 4px; }

/* Responsive */
@media (min-width: 560px) {
  .status-strip { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .counters { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: 1fr 1fr; }
  .detail-grid { grid-template-columns: 140px 1fr; }
}

@media (max-width: 899px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; width: 264px; z-index: 40;
    transform: translateX(-100%); transition: transform .16s var(--ease);
    overflow-y: auto;
  }
  .sidebar.is-open { transform: none; }
  .backdrop { display: block; position: fixed; inset: 0; background: rgba(16, 40, 28, .35); z-index: 35; }
  .backdrop[hidden] { display: none; }
  .topbar {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    background: var(--green); color: #fff; padding: 10px 16px; position: sticky; top: 0; z-index: 30;
  }
  .topbar strong { font-size: 16px; }
  .main { padding: 20px 16px 40px; }
  h1 { font-size: 26px; }
}

@media (min-width: 900px) {
  .counters { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .status-strip { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
