/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue: #4a6fa5;
  --blue-light: #eef3fb;
  --green: #277a55;
  --green-light: #edfaf3;
  --orange: #c07b1a;
  --orange-light: #fef6ee;
  --purple: #8b3a8c;
  --purple-light: #fdf0fb;
  --red: #dc2626;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-500: #6b7280;
  --gray-700: #374151;
  --gray-900: #111827;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.12);
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--gray-50);
  color: var(--gray-900);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* === Navbar === */
.navbar {
  background: var(--blue);
  color: #fff;
  padding: 0 1.5rem;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
}
.nav-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: 0.5px;
}
.nav-links { display: flex; gap: 0.25rem; }
.nav-link {
  color: rgba(255,255,255,0.8);
  text-decoration: none;
  padding: 0.4rem 0.8rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  transition: background 0.15s, color 0.15s;
}
.nav-link:hover, .nav-link.active {
  background: rgba(255,255,255,0.15);
  color: #fff;
}

/* === Container & Layout === */
.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem;
  flex: 1;
  width: 100%;
}

/* === Hero === */
.hero {
  text-align: center;
  padding: 2rem 0 1rem;
}
.hero h1 { font-size: 1.8rem; color: var(--gray-900); }
.hero-sub { color: var(--gray-500); margin-top: 0.25rem; }

/* === Stats === */
.stats-row {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  margin: 1.5rem 0;
}
.stat-card {
  flex: 1;
  min-width: 140px;
  background: #fff;
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: center;
  box-shadow: var(--shadow);
  border-top: 3px solid var(--blue);
}
.stat-card.accent-green { border-top-color: var(--green); }
.stat-card.accent-orange { border-top-color: var(--orange); }
.stat-card.accent-purple { border-top-color: var(--purple); }
.stat-card.accent-blue { border-top-color: var(--blue); }
.stat-value { display: block; font-size: 1.6rem; font-weight: 700; color: var(--gray-900); }
.stat-label { display: block; font-size: 0.8rem; color: var(--gray-500); margin-top: 0.25rem; }

/* === Sections === */
.section {
  margin: 2rem 0;
}
.section h2 {
  font-size: 1.2rem;
  margin-bottom: 1rem;
  color: var(--gray-700);
  border-bottom: 2px solid var(--gray-200);
  padding-bottom: 0.5rem;
}

/* === Station Grid === */
.station-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}
.station-card {
  background: #fff;
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  border-left: 4px solid var(--gray-300);
  transition: border-color 0.2s;
}
.station-card.online { border-left-color: var(--green); }
.station-card.offline { border-left-color: var(--gray-300); }
.station-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}
.station-header h3 { font-size: 1rem; }
.station-location { font-size: 0.85rem; color: var(--gray-500); margin-bottom: 0.75rem; }
.heartbeat { font-size: 0.8rem; color: var(--gray-500); margin-top: 0.5rem; }

/* === Water Level Bar === */
.water-level {
  background: var(--gray-200);
  border-radius: 99px;
  height: 20px;
  position: relative;
  overflow: hidden;
}
.water-level-bar {
  height: 100%;
  border-radius: 99px;
  transition: width 0.5s ease;
}
.water-level-bar.level-good { background: var(--green); }
.water-level-bar.level-low { background: var(--orange); }
.water-level-bar.level-critical { background: var(--red); }
.water-level-text {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--gray-700);
}
.water-level-mini {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.water-level-mini .water-level-bar {
  width: 60px;
  height: 8px;
  border-radius: 99px;
  display: inline-block;
}
.water-level-mini span { font-size: 0.85rem; }

/* === Dispense Feed === */
.dispense-feed {
  background: #fff;
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}
.dispense-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--gray-100);
  font-size: 0.9rem;
}
.dispense-item:last-child { border-bottom: none; }
.dispense-name { font-weight: 600; flex: 2; }
.dispense-station { color: var(--gray-500); flex: 1; }
.dispense-volume { font-weight: 600; color: var(--blue); flex: 1; text-align: right; }
.dispense-time { color: var(--gray-500); flex: 0; min-width: 50px; text-align: right; margin-left: 1rem; }

/* === Badges === */
.badge {
  display: inline-block;
  padding: 0.15rem 0.6rem;
  border-radius: 99px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.status-badge.online, .badge-online, .badge-active, .badge-completed { background: var(--green-light); color: var(--green); }
.status-badge.offline, .badge-offline, .badge-inactive { background: var(--gray-100); color: var(--gray-500); }
.badge-pending { background: var(--orange-light); color: var(--orange); }
.badge-cancelled { background: #fef2f2; color: var(--red); }

/* === Tables === */
.table-wrap { overflow-x: auto; }
.table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.table th, .table td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--gray-100);
  font-size: 0.9rem;
}
.table th {
  background: var(--gray-50);
  font-weight: 600;
  color: var(--gray-700);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.table tr:hover { background: var(--gray-50); }
.table code { background: var(--blue-light); padding: 0.1rem 0.4rem; border-radius: 4px; font-size: 0.85rem; }
.actions { white-space: nowrap; display: flex; gap: 0.25rem; }

/* === Forms & Inputs === */
.input {
  border: 1px solid var(--gray-300);
  border-radius: var(--radius);
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
  font-family: inherit;
}
.input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(74,111,165,0.1); }
.input-sm { padding: 0.35rem 0.5rem; font-size: 0.85rem; }
.input-narrow { max-width: 100px; }
.input-group {
  display: flex;
  gap: 0.5rem;
}
.input-group .input { flex: 1; }

.form-stack { display: flex; flex-direction: column; gap: 1rem; }
.form-group label { display: block; font-weight: 600; font-size: 0.85rem; margin-bottom: 0.25rem; color: var(--gray-700); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.form-actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 0.5rem; }

.inline-form { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.inline-fields { display: flex; gap: 0.5rem; flex-wrap: wrap; flex: 1; }
.inline-actions { display: flex; gap: 0.25rem; }

.filter-group { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.filter-group .input { max-width: 250px; }
.filter-group select.input { max-width: 200px; }

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: var(--radius);
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  text-decoration: none;
  font-family: inherit;
  white-space: nowrap;
}
.btn:hover { opacity: 0.9; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--blue); color: #fff; }
.btn-secondary { background: var(--gray-200); color: var(--gray-700); }
.btn-danger { background: var(--red); color: #fff; }
.btn-sm { padding: 0.3rem 0.6rem; font-size: 0.8rem; }
.btn-full { width: 100%; }

.btn-toggle-uin {
  background: none;
  border: none;
  outline: none;
  box-shadow: none;
  cursor: pointer;
  padding: 0 0 0 4px;
  color: #aaa;
  vertical-align: middle;
  line-height: 1;
  display: inline-flex;
  align-items: center;
}
.btn-toggle-uin:hover {
  color: #333;
}
.btn-toggle-uin:focus {
  outline: none;
  box-shadow: none;
}

.table code:has(.btn-toggle-uin) {
  background: none;
  padding: 0;
  border-radius: 0;
}

/* === Login === */
.login-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
}
.login-card {
  background: #fff;
  border-radius: 12px;
  padding: 2.5rem;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
}
.login-card h2 { font-size: 1.4rem; margin-bottom: 0.25rem; }
.login-sub { color: var(--gray-500); font-size: 0.9rem; margin-bottom: 1.5rem; }

/* === Toolbar === */
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.search-box { flex: 1; max-width: 400px; }
.add-form-inline { width: 100%; margin-top: 0.5rem; }

/* === Modal === */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.modal {
  background: #fff;
  border-radius: 12px;
  padding: 2rem;
  width: 90%;
  max-width: 500px;
  box-shadow: var(--shadow-lg);
}
.modal h3 { margin-bottom: 1.25rem; font-size: 1.2rem; }

/* === Alerts === */
.alert {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  margin-bottom: 1rem;
}
.alert-warning { background: var(--orange-light); color: var(--orange); border: 1px solid #e8c18f; }
.alert-info { background: var(--blue-light); color: var(--blue); border: 1px solid #b6c8e8; }

/* === Toast === */
#toast {
  position: fixed;
  top: 70px;
  right: 1rem;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.toast {
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  animation: slide-in 0.3s ease;
}
.toast.success { background: var(--green); color: #fff; }
.toast.error { background: var(--red); color: #fff; }
@keyframes slide-in {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* === Admin === */
.admin-header { margin-bottom: 1.5rem; }
.admin-header h1 { font-size: 1.5rem; margin-bottom: 0.75rem; }
.admin-nav { display: flex; gap: 0.25rem; border-bottom: 2px solid var(--gray-200); padding-bottom: 0; }
.admin-tab {
  padding: 0.5rem 1rem;
  text-decoration: none;
  color: var(--gray-500);
  font-size: 0.9rem;
  font-weight: 500;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color 0.15s, border-color 0.15s;
}
.admin-tab:hover { color: var(--gray-900); }
.admin-tab.active { color: var(--blue); border-bottom-color: var(--blue); }
.admin-logout { margin-left: auto; color: var(--red); }
.admin-logout:hover { color: var(--red); }

/* === API Key === */
.api-key-hidden { cursor: pointer; color: var(--gray-500); user-select: none; }
.api-key-wrap { display: flex; align-items: center; gap: 0.4rem; }
.api-key { cursor: pointer; font-size: 0.75rem; word-break: break-all; }
.btn-copy { flex-shrink: 0; }

/* === Editing Row === */
.editing-row { background: var(--blue-light) !important; }

/* === History === */
.history-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
}
.history-header h3 { font-size: 1.1rem; }

/* === Empty State === */
.empty-state {
  color: var(--gray-500);
  text-align: center;
  padding: 2rem;
  font-size: 0.95rem;
}

/* === HTMX Indicator === */
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: inline-block; }
.spinner { color: var(--gray-500); font-size: 0.85rem; margin-left: 0.5rem; }

/* === Footer === */
.footer {
  text-align: center;
  padding: 1.5rem;
  color: var(--gray-500);
  font-size: 0.8rem;
  border-top: 1px solid var(--gray-200);
  margin-top: 2rem;
}

/* === Responsive === */
@media (max-width: 640px) {
  .stats-row { flex-direction: column; }
  .station-grid { grid-template-columns: 1fr; }
  .form-row { grid-template-columns: 1fr; }
  .toolbar { flex-direction: column; }
  .search-box { max-width: 100%; }
  .filter-group { flex-direction: column; }
  .filter-group .input, .filter-group select.input { max-width: 100%; }
  .inline-fields { flex-direction: column; }
  .dispense-item { flex-wrap: wrap; gap: 0.25rem; }
}
