:root {
  --bg: #041622;
  --card: #0b2333;
  --accent: #7ed957;
  --accent2: #00ccff;
  --danger: #ff5b5b;
  --text: #e6f5ff;
  --muted: #9bb5c7;
}

body { background: var(--bg); color: var(--text); }

.wrap { margin-left: 240px; padding: 24px; }

.health-bar { position: sticky; top: 0; z-index: 5; background: rgba(11,35,51,0.8); backdrop-filter: blur(6px); border-radius: 12px; padding: 8px 12px; display: flex; gap: 14px; align-items: center; box-shadow: 0 0 18px rgba(0,0,0,0.35); }
.health-item { display: flex; align-items: center; gap: 8px; color: var(--muted); font-weight: 600; }
.health-dot { width: 10px; height: 10px; border-radius: 50%; box-shadow: 0 0 10px rgba(0,0,0,0.5); }
.health-item[data-status="ok"] .health-dot { background: #28c76f; box-shadow: 0 0 10px rgba(40,199,111,0.7); }
.health-item[data-status="late"] .health-dot { background: #ffcc00; box-shadow: 0 0 10px rgba(255,204,0,0.7); }
.health-item[data-status="down"], .health-item[data-status="invalid"] .health-dot { background: var(--danger); box-shadow: 0 0 10px rgba(255,91,91,0.7); }

.cards { display: flex; gap: 16px; flex-wrap: wrap; }
.card {
  background: var(--card);
  padding: 16px 18px;
  border-radius: 14px;
  box-shadow: 0 0 16px rgba(0,0,0,0.45);
  min-width: 200px;
  flex: 1;
  opacity: 0; transform: translateY(8px); animation: cardIn 0.4s ease forwards;
}
.card h4 { margin: 0 0 8px; color: var(--accent2); font-weight: 600; }
.card .value { font-size: 28px; font-weight: 700; color: var(--text); }

.filters { margin: 18px 0; display: flex; gap: 12px; align-items: center; }
.filters select, .filters input {
  background: #10314a; color: var(--text);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 10px; padding: 8px 10px;
}

.table-card { background: var(--card); border-radius: 14px; padding: 12px; box-shadow: 0 0 16px rgba(0,0,0,0.45); }
table.meters { width: 100%; border-collapse: collapse; }
table.meters th, table.meters td { padding: 10px 12px; border-bottom: 1px solid rgba(255,255,255,0.08); text-align: left; }
table.meters th { color: var(--accent2); background: #0f2a3f; }
table.meters tr:hover { background: rgba(255,255,255,0.03); }

.muted { color: var(--muted); }
.status-cell { text-align: center; }
.status-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; box-shadow: 0 0 8px rgba(0,0,0,0.5); }

#metersTable.loading { opacity: 0.6; filter: grayscale(0.2); }

/* Charts */
.charts { display: grid; grid-template-columns: repeat(auto-fit,minmax(280px,1fr)); gap: 16px; }
.chart-card { background: var(--card); border-radius: 14px; padding: 12px; box-shadow: 0 0 16px rgba(0,0,0,0.45); }

/* Finance */
.finance { background: var(--card); border-radius: 14px; box-shadow: 0 0 16px rgba(0,0,0,0.45); overflow: hidden; }
.finance header { padding: 12px 16px; cursor: pointer; display: flex; justify-content: space-between; align-items: center; color: var(--accent2); background: #0f2a3f; }
.finance .content { padding: 12px 16px; display: none; }
.finance.open .content { display: block; }

/* Alerts */
table.alerts { width: 100%; border-collapse: collapse; }
table.alerts th, table.alerts td { padding: 8px 10px; border-bottom: 1px solid rgba(255,255,255,0.08); }
table.alerts th { color: var(--accent2); background: #0f2a3f; text-align: left; }

.alerts-section table tbody tr {
  cursor: pointer;
  transition: background 0.15s;
}

.alerts-section table tbody tr:hover {
  background: rgba(0, 204, 255, 0.08);
}

.alert-badge {
  padding: 3px 7px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  color: #000;
}

.alert-green  { background:#48e072; }
.alert-yellow { background:#ffcc00; }
.alert-red    { background:#ff4d4f; }

.alert-type-icon {
  font-size: 18px;
  margin-right:4px;
}

.alert-row.expanded td {
  background: #112233;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.alert-message {
  padding:10px 14px;
  font-size:14px;
  color:#9bc4ff;
  line-height:1.4;
}

/* Activity */
.activity { background: var(--card); border-radius: 14px; padding: 12px; box-shadow: 0 0 16px rgba(0,0,0,0.45); }
.activity .time { color: var(--muted); margin-right: 8px; font-family: monospace; }

/* Tools Drawer */
.tools-btn { position: fixed; right: 20px; bottom: 20px; background: var(--accent2); color: #00131f; border: none; border-radius: 50%; width: 56px; height: 56px; box-shadow: 0 8px 18px rgba(0,0,0,0.4); cursor: pointer; font-size: 22px; }
.tools-drawer { position: fixed; right: 20px; bottom: 90px; width: 320px; background: rgba(11,35,51,0.9); backdrop-filter: blur(8px); border: 1px solid rgba(255,255,255,0.08); border-radius: 14px; box-shadow: 0 8px 20px rgba(0,0,0,0.45); transform: translateY(10px); opacity: 0; pointer-events: none; transition: all .25s ease; }
.tools-drawer.open { transform: translateY(0); opacity: 1; pointer-events: auto; }
.tools-drawer header { padding: 10px 12px; color: var(--accent2); font-weight: 600; border-bottom: 1px solid rgba(255,255,255,0.08); }
.tools-drawer .content { padding: 10px 12px; }
.tools-drawer .content button { margin: 6px 0; width: 100%; background: #10314a; color: var(--text); border: 1px solid rgba(255,255,255,0.15); border-radius: 10px; padding: 8px 10px; cursor: pointer; }
.tools-drawer .content button:hover { background: #163d5a; }

@keyframes cardIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.4); display: none; align-items: center; justify-content: center; backdrop-filter: blur(2px); }
.modal.open { display: flex; }
.modal .modal-content { background: rgba(11,35,51,0.95); border: 1px solid rgba(255,255,255,0.08); border-radius: 14px; padding: 16px; min-width: 320px; max-width: 640px; color: var(--text); box-shadow: 0 8px 20px rgba(0,0,0,0.45); }

/* Range bar */
.range-bar input, .range-bar button { background: #10314a; color: var(--text); border: 1px solid rgba(255,255,255,0.15); border-radius: 8px; padding: 6px 8px; }
.range-bar button { cursor: pointer; }

/* Light theme overrides */
body.light { --bg: #f5fbff; --card: #ffffff; --text: #0b2333; --muted: #5c7384; }
body.light .health-bar { background: rgba(255,255,255,0.9); }
body.light .tools-btn { color: #00131f; }
body.light table.meters th, body.light table.alerts th { background: #eef7ff; color: #0b2333; }

@media (max-width: 900px) {
  .wrap { margin-left: 80px; }
}

async function fetchAlerts() {
  const body = qs('#alertsBody');
  if (!body) return;
  try {
    const rows = await fetchJSON('/core/db_functions.php?action=alerts');
    const arr = Array.isArray(rows) ? rows : (rows.rows || []);
    if (!arr.length) {
      body.innerHTML = '<tr><td colspan="6" class="muted">No alerts</td></tr>';
      return;
    }

    body.innerHTML = arr.map(a => `
      <tr>
        <td>${a.type || ''}</td>
        <td>${a.meter || ''}</td>
        <td>${a.property || ''}</td>
        <td>${a.time || ''}</td>
        <td>${a.status || ''}</td>
        <td class="muted small" style="max-width:300px; white-space:normal; line-height:1.3;">${a.message || ''}</td>
      </tr>
    `).join('');
  } catch (err) {
    console.error('fetchAlerts failed', err);
    body.innerHTML = '<tr><td colspan="6" class="muted">Error loading alerts</td></tr>';
  }
}

/* === INSIGHTS CARDS === */
.cards.insights {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
}

.cards.insights .card {
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(0, 204, 255, 0.05);
  border-radius: 12px;
  padding: 18px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.4);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.cards.insights .card:hover {
  transform: translateY(-3px);
  box-shadow: 0 0 20px rgba(0, 204, 255, 0.15);
}

.cards.insights .card h4 {
  color: #00ccff;
  font-size: 1rem;
  margin-bottom: 6px;
  font-weight: 600;
}

.cards.insights .value {
  font-size: 1.8rem;
  font-weight: 700;
  color: #e6f5ff;
  transition: color 0.3s ease;
}

.cards.insights .value.accent {
  color: #7ed957;
}

.cards.insights .value.alert {
  color: #ff5b5b;
}

.cards.insights .value.warning {
  color: #ffc400;
}

.cards.insights .subtext {
  display: block;
  font-size: 0.8rem;
  color: #9bb5c7;
  margin-top: 4px;
  opacity: 0.8;
}

.status-card::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(255,91,91,0.05), rgba(255,255,255,0.02));
  z-index: 0;
  pointer-events: none;
}

.pulse {
  animation: pulseFlash 0.6s ease;
}

@keyframes pulseFlash {
  0% { background-color: rgba(126,217,87,0.2); }
  100% { background-color: transparent; }
}

.card.clickable {
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.card.clickable:hover {
  transform: translateY(-3px);
  box-shadow: 0 0 15px rgba(0, 204, 255, 0.4);
}

/* ===============================
   Analytics Panel — Refined Glass Look
   =============================== */
.analytics-panel {
  background: linear-gradient(180deg, rgba(255,255,255,0.03) 0%, rgba(255,255,255,0.015) 100%);
  border-radius: var(--radius);
  box-shadow: 0 0 20px rgba(0,0,0,0.4), inset 0 0 20px rgba(255,255,255,0.02);
  padding: 22px;
  margin: 22px 0;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.analytics-panel header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}

.analytics-panel h3 {
  color: var(--accent2);
  font-weight: 600;
  font-size: 1.2em;
  margin: 0;
  text-shadow: 0 0 6px rgba(0,204,255,0.3);
}

.analytics-panel .date-range {
  display: flex;
  align-items: center;
  gap: 6px;
}

.analytics-panel .date-range input {
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.1);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.9em;
}

.analytics-panel .date-range span {
  color: var(--muted);
  font-weight: 400;
}

.analytics-panel .date-range button {
  background: var(--accent2);
  color: #000;
  font-weight: 600;
  border: none;
  border-radius: 6px;
  padding: 7px 14px;
  cursor: pointer;
  transition: 0.3s ease;
  box-shadow: 0 0 10px rgba(0,204,255,0.4);
}

.analytics-panel .date-range button:hover {
  background: var(--accent);
  box-shadow: 0 0 14px rgba(0,204,255,0.6);
  transform: translateY(-1px);
}

.analytics-panel .charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 22px;
}

.analytics-panel .chart-card {
  background: radial-gradient(circle at top left, rgba(255,255,255,0.05), rgba(255,255,255,0.015));
  border-radius: var(--radius);
  padding: 18px 16px 20px;
  box-shadow: inset 0 0 12px rgba(0,0,0,0.25);
  transition: 0.3s ease;
}

.analytics-panel .chart-card:hover {
  box-shadow: inset 0 0 18px rgba(0,204,255,0.15), 0 0 10px rgba(0,204,255,0.15);
}

.analytics-panel .chart-card h4 {
  color: var(--accent2);
  margin-bottom: 8px;
  font-weight: 600;
  text-shadow: 0 0 4px rgba(0,204,255,0.3);
}

.analytics-panel .mini-metrics {
  font-size: 0.85em;
  color: var(--muted);
  display: flex;
  gap: 14px;
  margin-bottom: 6px;
}

.analytics-panel .mini-metrics b {
  color: var(--accent2);
  font-weight: 600;
}

.dot.water { background:#4bc0c0; }

.health-info {
  display: inline-block;
  margin-left: 12px;
  font-size: 0.85em;
  opacity: 0.75;
  vertical-align: middle;
}
.health-info:hover {
  opacity: 1;
}

.health-info span {
  color: var(--accent2);
  cursor: pointer;
}

.section-block {
  margin: 14px 0;
}

.section-heading {
  color: var(--accent2);
  margin: 6px 0 10px;
}

.activity-list {
  margin: 0;
  padding-left: 18px;
}

#finNet {
  float: right;
  color: var(--accent2);
}

.tools-note {
  margin-top: 8px;
}
