:root,
[data-theme="light"] {
  --bg: #f4f7fb;
  --panel: #ffffff;
  --sidebar-bg: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --radius: 12px;
  --input-bg: #ffffff;
  --code-color: #2563eb;
  --map-bg: #eef2f7;
  --chart-tick: #64748b;
  --chart-grid: rgba(15, 23, 42, 0.06);
  --chart-legend: #334155;
  --tooltip-bg: #ffffff;
  --tooltip-text: #1e293b;
  --chart-line-h: 180px;
  --chart-donut-h: 170px;
  --chart-hbar-max: 240px;
  --chart-hbar-row: 26px;
  --map-h: 280px;
  --stripe-odd: #f1f5f9;
  --stripe-even: #ffffff;
}

[data-theme="dark"] {
  --bg: #0f1419;
  --panel: #1a2332;
  --sidebar-bg: #0b1017;
  --border: #2a3648;
  --text: #e8eef7;
  --muted: #8fa3bf;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #22c55e;
  --danger: #ef4444;
  --input-bg: #0f1419;
  --code-color: #93c5fd;
  --map-bg: #0b1220;
  --chart-tick: #8fa3bf;
  --chart-grid: rgba(255, 255, 255, 0.05);
  --chart-legend: #e8eef7;
  --tooltip-bg: #1a2332;
  --tooltip-text: #e8eef7;
  --stripe-odd: rgba(255, 255, 255, 0.04);
  --stripe-even: rgba(255, 255, 255, 0.01);
}

* { box-sizing: border-box; }
html {
  height: auto;
  overflow-y: scroll;
}
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  overflow-x: hidden;
  overflow-y: visible;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.layout {
  display: flex;
  align-items: flex-start;
  min-height: 100vh;
}
.sidebar {
  width: 220px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-height: 100vh;
  position: sticky;
  top: 0;
  align-self: flex-start;
  flex-shrink: 0;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 24px;
}
.brand-logo {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--primary);
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35);
}
.sidebar nav { display: flex; flex-direction: column; gap: 8px; }
.sidebar nav a {
  padding: 10px 12px;
  border-radius: 8px;
  color: var(--muted);
}
.sidebar nav a.active, .sidebar nav a:hover {
  background: var(--panel);
  color: var(--text);
  text-decoration: none;
}
.sidebar-foot {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
  padding-top: 16px;
  border-top: 1px solid var(--border);
  margin-top: auto;
}
.sidebar-foot-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.sidebar-section {
  margin-top: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}
.sidebar-section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 8px;
  padding: 0 12px;
}
.sidebar-site {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  margin-bottom: 4px;
}
.sidebar-site:hover, .sidebar-site.active {
  background: var(--panel);
  color: var(--text);
  text-decoration: none;
}
.sidebar-site.active .site-dot { background: #3b82f6; }
.site-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #475569;
  margin-top: 6px;
  flex-shrink: 0;
}
.site-nav-text { display: flex; flex-direction: column; min-width: 0; }
.site-nav-text strong {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.site-nav-text small {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.site-report-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 12px;
}
.site-report-head h1 { margin: 0 0 4px; }

.report-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}
.report-tabs a {
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  color: var(--muted);
  background: transparent;
  border: 1px solid transparent;
  text-decoration: none;
}
.report-tabs a:hover { color: var(--text); border-color: var(--border); text-decoration: none; }
.report-tabs a.active {
  color: #fff;
  background: var(--primary);
  border-color: var(--primary);
}

.live-status {
  font-size: 12px;
  color: var(--success);
  white-space: nowrap;
}
.live-status.is-error {
  color: var(--danger);
}
.value.live-flash {
  animation: liveFlash 0.7s ease;
}
@keyframes liveFlash {
  0% { color: var(--success); transform: scale(1.04); }
  100% { transform: scale(1); }
}

.theme-toggle {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--muted);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
}

.query-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 16px;
}
.query-box-title {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}
.query-box pre {
  margin: 0;
  font-size: 12px;
  color: var(--code-color);
  white-space: pre-wrap;
  font-family: ui-monospace, monospace;
}

.report-metric { margin: 0 0 12px; color: var(--muted); }
.report-metric strong { color: var(--text); font-size: 18px; }

.report-table .summary-row td { font-weight: 600; border-bottom-width: 2px; }

.report-links { margin-bottom: 20px; }
.grid-3 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
}
.report-link-card {
  display: block;
  padding: 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
}
.report-link-card:hover { border-color: var(--primary); text-decoration: none; }

.card.highlight { border-color: rgba(59,130,246,.45); }

.live-feed { max-height: 220px; overflow-y: auto; }
.live-item {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  align-items: center;
}
.live-time { color: var(--muted); font-family: ui-monospace, monospace; }
.live-item code { font-size: 12px; color: var(--code-color); }

.content {
  flex: 1;
  min-width: 0;
  width: 100%;
  padding: 20px 24px 32px;
  max-width: 1400px;
}
.content.is-loading {
  opacity: 0.55;
  pointer-events: none;
  transition: opacity 0.15s ease;
}
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 14px;
}
.page-header h1 { margin: 0 0 4px; font-size: 24px; }
.muted { color: var(--muted); font-size: 14px; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}
.cards-6 { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.cards-2 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.cards-4 { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.cards-sub { margin-top: -4px; margin-bottom: 16px; }
.card-accent {
  border-color: rgba(37, 99, 235, 0.25);
  background: linear-gradient(180deg, rgba(37, 99, 235, 0.04), var(--panel));
}
.card-accent .value { color: var(--primary); }
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.card .label { color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.card .value { font-size: 24px; font-weight: 700; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 14px;
}
.panel h2 { margin: 0 0 10px; font-size: 16px; }
.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 14px;
}
.grid-2 .panel { margin-bottom: 0; }
.grid-2 > section { margin-bottom: 14px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 10px 8px; border-bottom: 1px solid var(--border); text-align: left; }
th { color: var(--muted); font-weight: 600; }
.compact td, .compact th { padding: 8px 6px; }

.note-cell {
  min-width: 180px;
  max-width: 260px;
}
.site-note-input {
  width: 100%;
  min-width: 140px;
  max-width: 240px;
  padding: 8px 10px;
  font-size: 13px;
}
.note-save-status {
  display: block;
  min-height: 16px;
  margin-top: 4px;
  font-size: 11px;
  color: var(--muted);
}
.note-save-status.is-saving { color: var(--primary); }
.note-save-status.is-ok { color: var(--success); }
.note-save-status.is-error { color: var(--danger); }

.sites-sort-tip { margin: 0 0 10px; }

.sites-table {
  border-collapse: collapse;
}
.sites-table th,
.sites-table td {
  text-align: center;
  vertical-align: middle;
  border-bottom: 1px solid var(--border);
  background: transparent;
}
.sites-table thead th {
  background: var(--panel);
  border-bottom: 2px solid var(--border);
}
.sites-table tbody tr:nth-child(odd) {
  background: var(--stripe-odd);
}
.sites-table tbody tr:nth-child(even) {
  background: var(--stripe-even);
}
.sites-table tbody tr.is-dragging {
  opacity: 0.72;
  background: rgba(37, 99, 235, 0.12) !important;
}
.sites-table tbody tr.is-drag-over td {
  border-top: none;
}
.sites-table tbody tr.is-drag-over {
  box-shadow: inset 0 2px 0 var(--primary);
}
.sites-table .col-drag { width: 40px; }
.sites-table .note-cell {
  min-width: 160px;
  max-width: none;
}
.sites-table .site-note-input {
  display: block;
  margin: 0 auto;
  text-align: center;
}
.sites-table .note-save-status {
  text-align: center;
}
.sites-table .verify-cell {
  min-width: 120px;
}
.sites-table .verify-msg {
  margin-left: auto;
  margin-right: auto;
  text-align: center;
}
.sites-table .actions {
  justify-content: center;
  flex-wrap: wrap;
}
.sites-table code {
  word-break: break-all;
}

.drag-cell { width: 40px; text-align: center; vertical-align: middle; }
.drag-handle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  color: var(--muted);
  cursor: grab;
  user-select: none;
  touch-action: none;
  letter-spacing: -2px;
  font-weight: 700;
}
.drag-handle:hover {
  background: rgba(37, 99, 235, 0.08);
  color: var(--primary);
}
.drag-handle:active { cursor: grabbing; }
.order-save-status {
  min-height: 18px;
  margin-top: 8px;
  font-size: 12px;
  color: var(--muted);
}
.order-save-status.is-ok { color: var(--success); }
.order-save-status.is-error { color: var(--danger); }

.sites-memo-input {
  width: 100%;
  min-height: 320px;
  resize: vertical;
  padding: 16px 18px;
  font-size: 14px;
  line-height: 1.65;
  font-family: inherit;
  border-radius: 10px;
}
.memo-save-status {
  min-height: 18px;
  margin-top: 8px;
  font-size: 12px;
  color: var(--muted);
}
.memo-save-status.is-saving { color: var(--primary); }
.memo-save-status.is-ok { color: var(--success); }
.memo-save-status.is-error { color: var(--danger); }

.btn, button.btn {
  display: inline-block;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
}
.btn.primary, button.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn.primary:hover { background: var(--primary-hover); }
select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}

.form-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
}
label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
input, textarea {
  background: var(--input-bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  min-width: 200px;
}
.code-block {
  width: 100%;
  min-height: 70px;
  font-family: ui-monospace, monospace;
  font-size: 13px;
}

.alert {
  padding: 12px 14px;
  border-radius: 8px;
  margin-bottom: 16px;
}
.alert.error { background: rgba(239,68,68,.15); border: 1px solid rgba(239,68,68,.4); }
.alert.error code { color: #fca5a5; font-size: 12px; word-break: break-all; }
.alert.success { background: rgba(34,197,94,.12); border: 1px solid rgba(34,197,94,.35); }

.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.auth-card {
  width: 100%;
  max-width: 440px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
}
.auth-card h1 { margin: 0 0 8px; }
.auth-card form label { margin-bottom: 12px; }
.auth-card input { width: 100%; }
.auth-card button { width: 100%; margin-top: 8px; }

.actions { display: flex; gap: 12px; align-items: center; }
.actions form { margin: 0; }
button.link {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  padding: 0;
  font-size: 14px;
}
button.link.danger { color: var(--danger); }

.verify-cell { min-width: 140px; }
.verify-status {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
}
.verify-status.ok {
  color: #fff;
  background: #16a34a;
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.25);
}
.verify-status.fail {
  color: #fff;
  background: #dc2626;
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.25);
}
.verify-status.pending {
  color: var(--muted);
  background: transparent;
  box-shadow: none;
}
.verify-msg {
  font-size: 12px;
  margin-top: 6px;
  max-width: 220px;
  line-height: 1.4;
}
.btn-sm {
  padding: 6px 10px;
  font-size: 13px;
}
.verify-inline {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin: 12px 0 8px;
}
.verify-inline .verify-msg { margin-top: 0; max-width: none; flex: 1; min-width: 200px; }

.steps { padding-left: 20px; color: var(--muted); }
.steps li { margin-bottom: 8px; }

.panel-map-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.panel-map-head h2 { margin: 0; }
.world-choropleth-map {
  height: var(--map-h);
  width: 100%;
  border-radius: 10px;
  background: var(--map-bg);
  border: 1px solid var(--border);
  z-index: 1;
}
.world-choropleth-map .leaflet-container {
  background: var(--map-bg);
  font-family: inherit;
}
.leaflet-tooltip.map-tooltip,
.leaflet-tooltip {
  background: var(--tooltip-bg);
  color: var(--tooltip-text);
  border: 1px solid var(--border);
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
}
.leaflet-tooltip.map-tooltip:before,
.leaflet-tooltip:before {
  border-top-color: var(--tooltip-bg);
}
.map-tip { margin: 10px 0 0; }
.map-error {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--muted);
  padding: 20px;
  text-align: center;
}
.map-legend {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
}
.legend-scale {
  display: flex;
  height: 12px;
  width: 168px;
  border-radius: 4px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.legend-scale i {
  flex: 1;
  display: block;
}
.legend-max { margin-left: 4px; }

.chart-wrap {
  position: relative;
  width: 100%;
  overflow: hidden;
}
.chart-wrap-line { height: var(--chart-line-h); max-height: var(--chart-line-h); }
.chart-wrap-donut { height: var(--chart-donut-h); max-height: var(--chart-donut-h); }
.chart-wrap-hbar {
  max-height: var(--chart-hbar-max);
  overflow-y: auto;
  overflow-x: hidden;
}
.chart-wrap canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  max-height: 100%;
}

.panel > canvas {
  display: none;
}

.detail-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.detail-tab {
  display: inline-flex;
  align-items: center;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 14px;
}
.detail-tab:hover { text-decoration: none; border-color: var(--primary); }
.detail-tab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  background: rgba(37, 99, 235, 0.12);
  color: var(--primary);
}
.badge-success {
  background: rgba(22, 163, 74, 0.12);
  color: var(--success);
}

@media (max-width: 768px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; }
}
