/* TRẠM NƯỚC · ESP32 THIẾT BỊ THẬT · GIAO DIỆN HIỆN ĐẠI */
*, *::before, *::after {
  box-sizing: border-box;
}

:root {
  --navy-dark: #102e42;
  --navy-mid: #183348;
  --navy-light: #25465b;
  --teal-primary: #147e8d;
  --teal-dark: #0f5b66;
  --teal-light: #36baca;
  --teal-bg: #eaf5f7;
  --bg-page: #edf3f6;
  --bg-card: #ffffff;
  --border-card: #dde6ec;
  --border-input: #cbdbe5;
  --text-primary: #183348;
  --text-muted: #5e778a;
  --text-subtle: #7a94a6;
  --color-danger: #dc2626;
  --color-danger-bg: #fee2e2;
  --color-warn: #b45309;
  --color-warn-bg: #fef3c7;
  --color-success: #059669;
  --color-success-bg: #d1fae5;
  --radius-card: 16px;
  --radius-btn: 10px;
}

body {
  margin: 0;
  background-color: var(--bg-page);
  color: var(--text-primary);
  font: 15px/1.55 system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ==========================================
   HEADER
   ========================================== */
header {
  background: var(--navy-dark);
  color: #ffffff;
  padding: 18px max(24px, calc((100vw - 1220px) / 2));
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  border-bottom: 2px solid #1c4866;
}

.brand-title {
  font-size: 17px;
  font-weight: 800;
  letter-spacing: 2px;
  color: #ffffff;
}

.brand-sub {
  display: block;
  font-size: 10.5px;
  letter-spacing: 1.5px;
  color: #99bacd;
  font-weight: 600;
  margin-top: 2px;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid #48657d;
  border-radius: 20px;
  padding: 6px 14px;
  background: rgba(255, 255, 255, 0.05);
  color: #d1e2ec;
  transition: all 0.25s ease;
}

.badge-online {
  border-color: #10b981;
  background: rgba(16, 185, 129, 0.15);
  color: #a7f3d0;
}

.badge-stale {
  border-color: #f59e0b;
  background: rgba(245, 158, 11, 0.15);
  color: #fde68a;
}

.badge-offline {
  border-color: #ef4444;
  background: rgba(239, 68, 68, 0.15);
  color: #fca5a5;
}

/* ==========================================
   MAIN CONTAINER
   ========================================== */
main {
  max-width: 1220px;
  margin: 0 auto;
  padding: 28px 20px 48px;
}

/* ==========================================
   INTRO & WARNING
   ========================================== */
.intro {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
}

.eyebrow {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 2px;
  color: var(--teal-primary);
  margin: 0 0 6px 0;
  text-transform: uppercase;
}

h1 {
  font-size: clamp(24px, 3.2vw, 36px);
  line-height: 1.15;
  letter-spacing: -0.8px;
  margin: 0 0 8px 0;
  color: var(--navy-dark);
}

.intro-sub {
  margin: 0;
  font-size: 14px;
  color: var(--text-muted);
}

.updated-box {
  text-align: right;
  background: #ffffff;
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid var(--border-card);
}

.updated-label {
  display: block;
  font-size: 11px;
  color: var(--text-subtle);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.updated {
  font-size: 13px;
  font-weight: 600;
  color: var(--navy-mid);
}

.warning {
  background: var(--color-warn-bg);
  color: var(--color-warn);
  padding: 14px 18px;
  border-radius: 12px;
  margin: 18px 0;
  font-size: 14px;
  border-left: 4px solid var(--color-warn);
  line-height: 1.5;
}

.warning.error {
  background: var(--color-danger-bg);
  color: #991b1b;
  border-left-color: var(--color-danger);
  font-weight: 600;
}

/* ==========================================
   THẺ THÔNG SỐ (STATS)
   ========================================== */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin: 20px 0 24px;
}

.stat-card {
  padding: 18px 20px;
  background: var(--bg-card);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-card);
  box-shadow: 0 2px 6px rgba(16, 46, 66, 0.03);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.stat-card small {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 1.2px;
  color: var(--text-subtle);
  text-transform: uppercase;
}

.stat-card strong {
  display: block;
  font-size: 22px;
  font-weight: 800;
  margin: 10px 0 4px;
  color: var(--navy-dark);
  letter-spacing: -0.4px;
}

.stat-sub {
  font-size: 12px;
  color: var(--text-muted);
}

.stat-flow strong {
  color: var(--teal-primary);
}

.text-teal {
  color: var(--teal-primary) !important;
}

.text-blue {
  color: #0284c7 !important;
}

.text-purple {
  color: #7c3aed !important;
}

/* ==========================================
   PANEL CHUNG (WATER-PANEL)
   ========================================== */
.water-panel {
  background: var(--bg-card);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-card);
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 2px 8px rgba(16, 46, 66, 0.04);
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  border-bottom: 1px solid #edf2f6;
  padding-bottom: 14px;
  margin-bottom: 20px;
}

h2 {
  font-size: 18px;
  font-weight: 800;
  margin: 0;
  color: var(--navy-dark);
  letter-spacing: -0.3px;
}

.panel-sub {
  font-size: 12.5px;
  color: var(--text-subtle);
  margin-top: 3px;
  display: block;
}

/* ==========================================
   SƠ ĐỒ BỂ NƯỚC (TANKS)
   ========================================== */
.tank-legend {
  display: flex;
  gap: 8px;
}

.tank-pill {
  font-size: 11px;
  font-weight: 700;
  background: var(--teal-bg);
  color: var(--teal-dark);
  padding: 4px 10px;
  border-radius: 12px;
}

.tanks {
  display: grid;
  grid-template-columns: 1.3fr 0.28fr 1fr 1fr;
  gap: 24px;
  align-items: center;
}

.tank-col h3 {
  font-size: 16px;
  font-weight: 700;
  margin: 0 0 14px;
  color: var(--navy-mid);
}

.tank-col h3 small {
  color: var(--text-subtle);
  font-weight: normal;
  font-size: 12px;
  margin-left: 6px;
}

.tank {
  position: relative;
  height: 240px;
  background: #edf5f9;
  border: 3px solid #8db3c7;
  border-top: none;
  border-radius: 0 0 18px 18px;
  overflow: hidden;
  isolation: isolate;
  box-shadow: inset 0 2px 8px rgba(16, 46, 66, 0.05);
}

.tank.source {
  border-color: #7aa7bd;
  background: #e7f2f7;
}

.tank .fill {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(180deg, #36baca 0%, #138498 100%);
  width: 100%;
  height: 0;
  transition: height 0.8s ease-out;
  z-index: 1;
}

.tank strong {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 34px;
  font-weight: 800;
  z-index: 3;
  color: var(--navy-dark);
  text-shadow: 0 1px 10px rgba(255, 255, 255, 0.85);
  letter-spacing: -0.5px;
}

.line {
  position: absolute;
  bottom: var(--line, 0%);
  width: 100%;
  border-top: 1.5px dashed #486577;
  z-index: 2;
  pointer-events: none;
}

.line span {
  position: absolute;
  right: 6px;
  bottom: 3px;
  font-size: 10px;
  font-weight: 600;
  color: #1e3a4c;
  background: rgba(255, 255, 255, 0.75);
  padding: 1px 4px;
  border-radius: 4px;
}

.line.danger {
  border-color: var(--color-danger);
}

.line.danger span {
  color: var(--color-danger);
  background: rgba(254, 226, 226, 0.9);
}

.line.low {
  border-color: #0d9488;
}

.line.low span {
  color: #0f766e;
  background: rgba(204, 251, 241, 0.9);
}

.pipe {
  display: flex;
  justify-content: center;
  align-items: center;
}

.pipe-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #8da5b5;
  transition: color 0.3s ease;
}

.pump-icon {
  font-size: 36px;
  line-height: 1;
}

.pipe small {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 1px;
  margin-top: 4px;
}

.pipe-body.running {
  color: var(--teal-primary);
}

.pipe-body.running .pump-icon {
  filter: drop-shadow(0 0 8px rgba(20, 126, 141, 0.6));
}

.tank-vol {
  text-align: center;
  font-size: 13.5px;
  font-weight: 700;
  color: var(--navy-mid);
  margin: 12px 0 2px;
}

.tank-dist {
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
  margin: 0;
}

/* ==========================================
   KHU VỰC BIỂU ĐỒ LƯU LƯỢNG (FLOW PANEL)
   ========================================== */
.flow-panel {
  padding-bottom: 20px;
}

.flow-head {
  border-bottom: none;
  margin-bottom: 12px;
  padding-bottom: 0;
}

.flow-toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.range-selector {
  display: inline-flex;
  flex-wrap: wrap;
  max-width: 100%;
  background: #eef3f7;
  padding: 3px;
  border-radius: 10px;
  border: 1px solid var(--border-card);
}

.btn-range {
  background: transparent;
  border: none;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-range:hover {
  color: var(--navy-dark);
  background: rgba(255, 255, 255, 0.5);
}

.btn-range.active {
  background: var(--teal-primary);
  color: #ffffff;
  box-shadow: 0 1px 3px rgba(20, 126, 141, 0.3);
}

.smooth-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  padding: 5px 8px;
  border-radius: 8px;
  transition: background 0.2s ease;
}

.smooth-toggle:hover {
  background: #f1f5f9;
}

.smooth-toggle input {
  display: none;
}

.toggle-slider {
  width: 32px;
  height: 18px;
  background: #cbd5e1;
  border-radius: 18px;
  position: relative;
  transition: background 0.2s ease;
}

.toggle-slider::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  background: #ffffff;
  border-radius: 50%;
  transition: transform 0.2s ease;
}

.smooth-toggle input:checked + .toggle-slider {
  background: #0284c7;
}

.smooth-toggle input:checked + .toggle-slider::after {
  transform: translateX(14px);
}

/* Thanh chú giải biểu đồ */
.flow-meta-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: #f8fafc;
  border: 1px solid #eef2f6;
  border-radius: 10px;
  margin-bottom: 14px;
  font-size: 12px;
  flex-wrap: wrap;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.legend-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--navy-mid);
}

.swatch {
  display: inline-block;
  width: 14px;
  height: 3px;
  border-radius: 2px;
}

.swatch-raw {
  background: var(--teal-primary);
  height: 4px;
}

.swatch-smooth {
  background: #0284c7;
  height: 3px;
  border-top: 1px dashed #0284c7;
}

.swatch-pid {
  background: #7c3aed;
  height: 3px;
  border-top: 1px dashed #7c3aed;
}

.marker-sym {
  font-size: 11px;
}

.marker-pump {
  color: #059669;
}

.marker-done {
  color: var(--teal-primary);
}

.marker-fault {
  color: var(--color-danger);
}

.flow-status-pill {
  font-size: 11.5px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 12px;
  background: #e2e8f0;
  color: #475569;
}

.flow-status-pill.live {
  background: #ecfdf5;
  color: #047857;
}

.flow-status-pill.stale {
  background: #fffbeb;
  color: #b45309;
}

.flow-status-pill.empty {
  background: #f1f5f9;
  color: #64748b;
}

/* Khung Canvas */
.chart-wrapper {
  position: relative;
  width: 100%;
  height: 310px;
  background: #ffffff;
  border: 1px solid var(--border-card);
  border-radius: 12px;
  overflow: hidden;
}

#flowCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

/* Tooltip nổi */
.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(16, 46, 66, 0.94);
  color: #ffffff;
  padding: 12px 14px;
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  font-size: 12px;
  line-height: 1.45;
  min-width: 210px;
  z-index: 10;
  backdrop-filter: blur(4px);
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.tt-header {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  padding-bottom: 6px;
  margin-bottom: 6px;
  font-weight: 700;
}

.tt-time {
  color: #bae6fd;
}

.tt-tank {
  color: #fef08a;
}

.tt-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin: 3px 0;
}

.tt-k {
  color: #cbd5e1;
}

.tt-v {
  font-weight: 700;
  color: #ffffff;
}

.tt-event {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px dashed rgba(255, 255, 255, 0.2);
  color: #86efac;
}

.tt-event.tt-error {
  color: #fca5a5;
}

.tt-footer {
  font-size: 10px;
  color: #94a3b8;
  margin-top: 6px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  padding-top: 4px;
}

/* Overlay khi rỗng */
.chart-state-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(2px);
}

.chart-state-overlay[hidden], [hidden] {
  display: none !important;
}

.state-overlay-card {
  text-align: center;
  padding: 24px;
  max-width: 380px;
}

.overlay-icon {
  font-size: 32px;
  display: block;
  margin-bottom: 8px;
}

.state-overlay-card strong {
  display: block;
  font-size: 15px;
  color: var(--navy-dark);
  margin-bottom: 6px;
}

.state-overlay-card p {
  font-size: 13px;
  color: var(--text-muted);
  margin: 0;
}

/* Thẻ thống kê chu kỳ bơm từ firmware */
.flow-info-footer {
  margin-top: 14px;
}

.cycle-stats-banner {
  display: flex;
  gap: 14px;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: 12px;
  padding: 12px 16px;
  align-items: flex-start;
}

.cycle-stats-badge {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.5px;
  background: #15803d;
  color: #ffffff;
  padding: 3px 7px;
  border-radius: 6px;
  white-space: nowrap;
}

.cycle-stats-content strong {
  display: block;
  font-size: 13px;
  color: #166534;
  margin-bottom: 3px;
}

.cycle-stats-content p {
  margin: 0 0 4px;
  font-size: 12.5px;
  color: #14532d;
}

.cycle-stats-note {
  display: block;
  font-size: 11px;
  color: #15803d;
  opacity: 0.9;
}

/* ==========================================
   BẢNG ĐIỀU KHIỂN & NHẬT KÝ (BOTTOM)
   ========================================== */
.bottom {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  margin-top: 24px;
}

.controls, .logs-card {
  background: var(--bg-card);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-card);
  padding: 24px;
  box-shadow: 0 2px 8px rgba(16, 46, 66, 0.04);
}

.access-pill {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 12px;
  background: #e2e8f0;
  color: #475569;
}

.access-pill.unlocked {
  background: #ecfdf5;
  color: #059669;
}

.controls-desc, .logs-desc {
  font-size: 13px;
  color: var(--text-muted);
  margin: 0 0 16px;
}

/* Form nhập PIN */
form#login {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}

button, input, select {
  font: inherit;
  border: 1px solid var(--border-input);
  border-radius: var(--radius-btn);
  padding: 10px 14px;
}

input, select {
  width: 100%;
  background: #ffffff;
  color: var(--navy-dark);
}

input:focus, select:focus {
  outline: none;
  border-color: var(--teal-primary);
  box-shadow: 0 0 0 3px rgba(20, 126, 141, 0.15);
}

button {
  background: #eaf3f6;
  color: var(--navy-mid);
  cursor: pointer;
  font-weight: 700;
  font-size: 13px;
  transition: all 0.2s ease;
  white-space: nowrap;
}

button:hover {
  background: #d4e7ee;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

form#login button, .pump-form button {
  background: var(--teal-primary);
  color: #ffffff;
}

form#login button:hover, .pump-form button:hover {
  background: var(--teal-dark);
}

/* Nhóm nút vận hành */
.buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 18px 0;
}

.buttons button.stop {
  color: #b91c1c;
  background: #fee2e2;
  border-color: #fca5a5;
}

.buttons button.stop:hover {
  background: #fecaca;
}

/* Form bơm đến mức đích */
.pump-form {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 8px;
}

.input-pct-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-pct-wrapper input {
  padding-right: 26px;
  width: 90px;
}

.unit-pct {
  position: absolute;
  right: 10px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-subtle);
  pointer-events: none;
}

.result-box {
  border-left: 3px solid var(--teal-primary);
  padding: 8px 12px;
  min-height: 44px;
  background: #f8fafc;
  border-radius: 0 8px 8px 0;
  font-size: 13px;
  color: var(--navy-mid);
  margin: 16px 0 0;
}

/* Danh sách nhật ký */
.btn-refresh {
  font-size: 12px;
  padding: 6px 12px;
}

.history {
  max-height: 360px;
  overflow-y: auto;
  border: 1px solid #edf2f6;
  border-radius: 10px;
  padding: 6px 14px;
  background: #fafcfe;
}

.event {
  padding: 10px 0;
  border-bottom: 1px solid #eef2f6;
  font-size: 13px;
}

.event:last-child {
  border-bottom: none;
}

.event small {
  color: var(--text-subtle);
  display: block;
  font-size: 11px;
}

.event b {
  display: block;
  margin: 2px 0;
  color: var(--navy-dark);
}

.event.error {
  color: var(--color-danger);
}

.event.error b {
  color: var(--color-danger);
}

.hidden {
  display: none !important;
}

footer {
  text-align: center;
  color: var(--text-subtle);
  font-size: 12px;
  margin: 36px 0 10px;
}

/* ==========================================
   RESPONSIVE DESIGN (MOBILE & TABLET)
   ========================================== */
@media (max-width: 900px) {
  .tanks {
    grid-template-columns: 1fr 1fr;
    gap: 18px;
  }
  .tank-col:first-child {
    grid-column: 1 / -1;
  }
  .tank.source {
    height: 180px;
  }
  .pipe {
    display: none;
  }
  .bottom {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 650px) {
  header {
    padding: 14px 16px;
  }
  .brand-title {
    font-size: 15px;
  }
  .badge {
    font-size: 11.5px;
    padding: 5px 10px;
  }
  main {
    padding: 18px 12px 36px;
  }
  .intro {
    flex-direction: column;
    align-items: flex-start;
  }
  .updated-box {
    text-align: left;
    width: 100%;
  }
  .stats {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }
  .stat-card {
    padding: 14px 16px;
  }
  .stat-card strong {
    font-size: 18px;
  }
  .water-panel, .controls, .logs-card {
    padding: 16px;
  }
  .panel-head {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  .flow-toolbar {
    width: 100%;
    justify-content: space-between;
  }
  .chart-wrapper {
    height: 260px;
  }
  .tanks {
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }
  .tank {
    height: 190px;
  }
  .tank strong {
    font-size: 26px;
  }
  .pump-form {
    flex-wrap: wrap;
  }
  .buttons {
    grid-template-columns: 1fr;
  }
}
