:root {
    --bg: #f5f7fa;
    --surface: #ffffff;
    --border: #e2e8f0;
    --text: #16202c;
    --muted: #64748b;
    --primary: #0f766e;
    --primary-dark: #115e59;
    --normal: #16a34a;
    --warning: #d97706;
    --danger: #dc2626;
    --offline: #64748b;
    --ph: #7c3aed;
    --suhu: #ea580c;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 12px rgba(15, 23, 42, .04);
}

* { box-sizing: border-box; }

/* Aturan bawaan [hidden] kalah oleh .btn/.pill/.badge yang menyetel display,
   padahal JavaScript live memakai atribut ini untuk menyembunyikan elemen. */
[hidden] { display: none !important; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.wrap { max-width: 1180px; margin: 0 auto; padding: 0 20px; }

.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner { display: flex; align-items: center; gap: 24px; height: 60px; }
.brand { font-weight: 700; font-size: 17px; color: var(--text); }
.brand:hover { text-decoration: none; }
.nav { display: flex; gap: 4px; margin-right: auto; flex-wrap: wrap; }
.nav a { padding: 7px 12px; border-radius: 8px; color: var(--muted); font-weight: 500; }
.nav a:hover { background: #eef2f6; text-decoration: none; }
.nav a.active { background: #e6f4f1; color: var(--primary-dark); }
.logout { display: flex; align-items: center; gap: 10px; }
.user { color: var(--muted); font-size: 13px; }

.main { padding: 26px 20px 40px; }
.footer { padding: 0 20px 30px; color: var(--muted); font-size: 12px; }

h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0 0 14px; }
.page-head { display: flex; align-items: flex-start; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .spacer { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.subtitle { color: var(--muted); font-size: 13px; margin: 0; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card + .card { margin-top: 18px; }
.grid { display: grid; gap: 16px; }
.grid-5 { grid-template-columns: repeat(5, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
@media (max-width: 1100px) { .grid-5 { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 900px) { .grid-5, .grid-4, .grid-3, .grid-2 { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 560px) { .grid-5, .grid-4, .grid-3, .grid-2 { grid-template-columns: 1fr; } }

.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 26px; font-weight: 650; margin-top: 6px; }
.stat .unit { font-size: 13px; color: var(--muted); font-weight: 400; }

.device-card { display: block; color: inherit; }
.device-card:hover { text-decoration: none; border-color: #cbd5e1; }
.device-card .top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.device-card .reading { font-size: 30px; font-weight: 650; margin: 10px 0 2px; }
.device-card .meta { color: var(--muted); font-size: 12px; }

.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill-normal { background: #dcfce7; color: #15803d; }
.pill-low { background: #fef3c7; color: #b45309; }
.pill-high { background: #fee2e2; color: #b91c1c; }
.pill-offline { background: #e2e8f0; color: #475569; }
.pill-inactive { background: #f1f5f9; color: #94a3b8; }
.pill-warning { background: #fef3c7; color: #b45309; }
.pill-critical { background: #fee2e2; color: #b91c1c; }

.badge { display: inline-block; min-width: 18px; padding: 1px 6px; border-radius: 999px; font-size: 11px; font-weight: 700; text-align: center; }
.badge-danger { background: var(--danger); color: #fff; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #f8fafc; }
.table-wrap { overflow-x: auto; }
.num { font-variant-numeric: tabular-nums; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 8px; border: 1px solid transparent; background: var(--primary); color: #fff; font-size: 14px; font-weight: 550; cursor: pointer; }
.btn:hover { background: var(--primary-dark); text-decoration: none; color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: #eef2f6; color: var(--text); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #b91c1c; }

form.inline { display: inline; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field label { font-size: 13px; font-weight: 600; }
.field .hint { font-size: 12px; color: var(--muted); }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], select, textarea {
    padding: 8px 11px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; background: #fff; color: var(--text); font-family: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #99f6e4; border-color: var(--primary); }
.error-text { color: var(--danger); font-size: 12px; }

.flash { padding: 12px 14px; border-radius: var(--radius); margin-bottom: 18px; font-size: 14px; }
.flash-success { background: #dcfce7; color: #14532d; }
.flash-error { background: #fee2e2; color: #7f1d1d; }
.flash ul { margin: 6px 0 0 18px; padding: 0; }

.chart-wrap { position: relative; }
.chart { width: 100%; height: auto; display: block; touch-action: pan-y;
         /* Titik sorot pada pembacaan pertama dan terakhir berada tepat di
            tepi; tanpa ini separuhnya terpotong. */
         overflow: visible; }
.chart .axis-text { fill: var(--muted); font-size: 11px; }
.chart-guide { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .panel-dasar { stroke: var(--border); stroke-width: 1; }
.chart .panel-nama { font-size: 11px; font-weight: 700; }
.chart .panel-nama.seri-tds { fill: var(--primary); }
.chart .panel-nama.seri-ph { fill: var(--ph); }
.chart .panel-nama.seri-suhu { fill: var(--suhu); }
.chart .panel-rentang { fill: var(--muted); font-weight: 400; }

/* Pita zona aman dibuat sangat samar supaya menjadi latar, bukan elemen yang
   bersaing dengan garis datanya. */
.chart .pita-aman { opacity: .08; }
.chart .pita-aman.seri-tds { fill: var(--primary); }
.chart .pita-aman.seri-ph { fill: var(--ph); }
.chart .pita-aman.seri-suhu { fill: var(--suhu); }

.chart .garis-batas { stroke-width: 1; stroke-dasharray: 5 4; opacity: .55; }
.chart .garis-batas.seri-tds { stroke: var(--primary); }
.chart .garis-batas.seri-ph { stroke: var(--ph); }
.chart .garis-batas.seri-suhu { stroke: var(--suhu); }
.chart .teks-batas { font-size: 9.5px; opacity: .75; }
.chart .teks-batas.seri-tds { fill: var(--primary); }
.chart .teks-batas.seri-ph { fill: var(--ph); }
.chart .teks-batas.seri-suhu { fill: var(--suhu); }

.chart .line { fill: none; stroke-width: 1.8; stroke-linejoin: round; stroke-linecap: round; }
.chart .line.seri-tds { stroke: var(--primary); }
.chart .line.seri-ph { stroke: var(--ph); }
.chart .line.seri-suhu { stroke: var(--suhu); }
.chart .dot-aktif { fill: #fff; stroke-width: 2.5; }
.chart .dot-aktif.seri-tds { stroke: var(--primary); }
.chart .dot-aktif.seri-ph { stroke: var(--ph); }
.chart .dot-aktif.seri-suhu { stroke: var(--suhu); }

.chart-tooltip { position: absolute; pointer-events: none; background: #0f172a; color: #f8fafc;
                 padding: 7px 11px; border-radius: 7px; font-size: 12px; line-height: 1.45;
                 white-space: nowrap; box-shadow: 0 4px 12px rgba(15, 23, 42, .25); z-index: 5; }
.chart-tooltip .waktu { display: block; color: #94a3b8; font-size: 11px; margin-bottom: 2px; }
.chart-tooltip .nilai { display: block; font-weight: 650; font-variant-numeric: tabular-nums; }
.chart-tooltip .nilai .nama { display: inline-block; min-width: 34px; font-weight: 400; color: #94a3b8; }
.chart-tooltip .nilai-ph { color: #c4b5fd; }
.chart-tooltip .nilai-suhu { color: #fdba74; }
.btn-lokal { background: #1d4ed8; }
.btn-lokal:hover { background: #1e40af; }
.chip-controller { display: inline-block; padding: 2px 8px; border-radius: 999px;
                   background: #e6f4f1; color: #0f766e; font-size: 11px; font-weight: 600; }
.chip-worker { display: inline-block; padding: 2px 8px; border-radius: 999px;
               background: #ede9fe; color: #6d28d9; font-size: 11px; font-weight: 600; }
.chip-lokal { display: inline-block; padding: 2px 8px; border-radius: 999px;
              background: #dbeafe; color: #1d4ed8; font-size: 11px; font-weight: 600; }

.reading-ph { font-size: 15px; font-weight: 600; color: var(--ph); margin-left: 10px; white-space: nowrap; }

.empty { padding: 34px; text-align: center; color: var(--muted); }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; background: #f1f5f9; padding: 2px 6px; border-radius: 6px; word-break: break-all; }
pre.code-block { background: #0f172a; color: #e2e8f0; padding: 14px; border-radius: var(--radius); overflow-x: auto; font-size: 12.5px; line-height: 1.5; }

.pagination { display: flex; gap: 4px; margin-top: 16px; flex-wrap: wrap; font-size: 13px; }
.pagination a, .pagination span { padding: 6px 10px; border: 1px solid var(--border); border-radius: 7px; background: #fff; }
.pagination .active span { background: var(--primary); color: #fff; border-color: var(--primary); }

.auth-page { min-height: 76vh; display: flex; align-items: center; justify-content: center; }
.auth-card { width: 100%; max-width: 380px; }
.auth-card h1 { margin-bottom: 16px; }

/* --- Panel kendali relay ------------------------------------------------ */

/* Kedelapan kanal muat dalam satu baris di layar lebar; tiap sel sekitar
   135 px, jadi isinya dibuat seminimal mungkin dan form pengaturannya
   dipindah keluar dari sel. */
.relay-grid { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr));
              gap: 8px; margin-top: 12px; }
@media (max-width: 1080px) { .relay-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
@media (max-width: 560px)  { .relay-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

.relay-sel { border: 1px solid var(--border); border-radius: 9px; padding: 8px;
             background: #fff; display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.relay-sel.relay-nyala { border-color: #fca5a5; background: #fff7f7; }
.relay-sel.relay-menunggu { border-style: dashed; }

.relay-kepala { display: flex; align-items: center; gap: 5px; }
.relay-ch { font-size: 11px; font-weight: 700; color: var(--muted); letter-spacing: .03em; }
.relay-risiko { font-size: 11px; color: var(--warning); cursor: help; }

/* Lampu memakai warna sekaligus isian, supaya tetap terbaca oleh mata yang
   sulit membedakan merah dan hijau. */
.relay-lampu { width: 10px; height: 10px; border-radius: 50%; margin-left: auto; flex: none;
               border: 2px solid #cbd5e1; background: transparent; }
.relay-lampu.nyala { background: var(--danger); border-color: var(--danger);
                     box-shadow: 0 0 0 3px rgba(220, 38, 38, .18); }

.relay-nama { font-size: 12.5px; font-weight: 600; line-height: 1.25;
              overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.relay-tombol { width: 100%; justify-content: center; padding: 5px 6px; font-size: 12px; }
.relay-nonaktif { font-size: 11px; color: var(--muted); text-align: center; padding: 5px 0; }

.relay-keadaan { font-size: 11px; font-weight: 700; color: var(--muted); letter-spacing: .03em; }
.relay-nyala .relay-keadaan { color: var(--danger); }
.relay-catatan { font-size: 10.5px; color: var(--warning); line-height: 1.3; }
.relay-mode { font-size: 10.5px; color: var(--ph); }

.relay-pesan { margin: 10px 0 0; padding: 8px 11px; border-radius: 8px; font-size: 13px;
               background: #f1f5f9; color: var(--text); }
.relay-pesan.sukses { background: #dcfce7; color: #14532d; }
.relay-pesan.gagal { background: #fee2e2; color: #7f1d1d; }

.relay-tambahan { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 10px; }
.relay-tambahan > details { flex: 1; min-width: 280px; }
.relay-tambahan summary { font-size: 12px; color: var(--muted); cursor: pointer; padding: 4px 0; }
.relay-setel-tabel th, .relay-setel-tabel td { padding: 5px 6px; }
.relay-setel-tabel input[type=text] { width: 100%; min-width: 90px; }
.relay-setel-tabel input[type=number] { width: 72px; }
.relay-setel-tabel select { max-width: 140px; }
