/* =========================================================
   rail.pelectro.org — Bogie Monitor
   ========================================================= */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f0f2f5;
  --card-bg: #ffffff;
  --border: #d0d5dd;
  --text: #101828;
  --text-muted: #667085;
  --primary: #1e3a5f;
  --primary-hover: #162d4a;
  --danger: #b42318;
  --warning: #b54708;
  --success: #067647;
  --water-fill: #2563a8;
  --water-tank-bg: #dbeafe;
  --radius: 4px;
  --shadow: 0 1px 3px rgba(16,24,40,0.1);
  --shadow-lg: 0 4px 16px rgba(16,24,40,0.12);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 15px;
}

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

/* ---- Nav ---- */
nav {
  background: var(--primary);
  border-bottom: 1px solid #162d4a;
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 24px;
  height: 56px;
  position: sticky;
  top: 0;
  z-index: 100;
}
nav .brand {
  font-weight: 700;
  font-size: 1rem;
  color: #ffffff;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
nav .nav-links { display: flex; gap: 2px; }
nav .nav-links a {
  padding: 6px 14px;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 500;
  color: rgba(255,255,255,0.75);
  transition: background 0.15s, color 0.15s;
}
nav .nav-links a:hover,
nav .nav-links a.active {
  background: rgba(255,255,255,0.12);
  color: #ffffff;
  text-decoration: none;
}
nav .search-wrap { flex: 1; max-width: 380px; margin-left: auto; }
nav .search-wrap input {
  width: 100%;
  padding: 7px 14px 7px 36px;
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: var(--radius);
  font-size: 0.875rem;
  background: rgba(255,255,255,0.1) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='rgba(255,255,255,0.6)' viewBox='0 0 16 16'%3E%3Cpath d='M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z'/%3E%3C/svg%3E") no-repeat 11px center;
  color: #ffffff;
  outline: none;
  transition: border-color 0.15s;
}
nav .search-wrap input::placeholder { color: rgba(255,255,255,0.5); }
nav .search-wrap input:focus { border-color: rgba(255,255,255,0.6); }

/* ---- Page layout ---- */
.page { padding: 24px; max-width: 1400px; margin: 0 auto; }
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
.page-header h1 { font-size: 1.25rem; font-weight: 700; color: var(--text); }

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #921d12; }
.btn-outline {
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--text);
}
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-sm { padding: 5px 10px; font-size: 0.8rem; }
.btn-icon {
  width: 34px; height: 34px;
  padding: 0;
  border-radius: var(--radius);
  justify-content: center;
  background: var(--bg);
  border: 1.5px solid var(--border);
  color: var(--text-muted);
}
.btn-icon:hover { border-color: var(--primary); color: var(--primary); }

/* ---- Train card ---- */
.trains-list { display: flex; flex-direction: column; gap: 16px; }

.train-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.train-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: #f8f9fb;
}

.train-meta { flex: 1; }
.train-meta h2 { font-size: 1rem; font-weight: 700; }
.train-meta .sub {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 3px;
}

.map-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--primary);
}

.train-stats { display: flex; gap: 16px; font-size: 0.8rem; color: var(--text-muted); }
.train-stats span strong { color: var(--text); }

.conn-badge {
  padding: 2px 9px;
  border-radius: 3px;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.conn-badge.connected    { background: #d1fae5; color: #065f46; }
.conn-badge.disconnected { background: #fee2e2; color: #7f1d1d; }
.conn-badge.unknown      { background: #f3f4f6; color: #6b7280; }

/* ---- Train chain layout ---- */
.train-chain-wrap {
  display: flex;
  align-items: flex-end;
  padding: 16px 20px 10px;
  overflow-x: auto;
  gap: 0;
  background:
    linear-gradient(transparent calc(100% - 28px), #cbd5e1 calc(100% - 28px), #cbd5e1 calc(100% - 24px), transparent calc(100% - 24px));
}

.chain-loco {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-right: 0;
  position: relative;
  z-index: 1;
}

.chain-item {
  display: flex;
  align-items: flex-end;
  flex-shrink: 0;
}

.chain-coupler {
  width: 18px;
  height: 6px;
  background: #94a3b8;
  border-radius: 2px;
  margin-bottom: 26px;
  flex-shrink: 0;
}

.bogies-chain {
  display: flex;
  align-items: flex-end;
  gap: 0;
}

/* ---- Bogie card ---- */
.bogie-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  background: var(--card-bg);
  border: 1.5px solid var(--border);
  border-radius: var(--radius) var(--radius) 2px 2px;
  padding: 8px 8px 6px;
  width: 110px;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
  text-decoration: none;
  color: inherit;
  box-shadow: var(--shadow);
}
.bogie-card:hover {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(30,58,95,0.12), var(--shadow);
  text-decoration: none;
}

/* Bogie card stacked directly above wheels */
.chain-bogie-stack {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.bogie-wheels { margin-top: 0; }

.bogie-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  gap: 4px;
}

.bogie-id {
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.03em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  text-transform: uppercase;
}

.conn-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.conn-dot.connected    { background: var(--success); }
.conn-dot.disconnected { background: var(--danger); }
.conn-dot.unknown      { background: #d1d5db; }

.bogie-map-link {
  font-size: 0.62rem;
  color: var(--primary);
  text-decoration: none;
  margin-top: -2px;
  word-break: break-all;
  text-align: center;
  line-height: 1.3;
}
.bogie-map-link:hover { text-decoration: underline; }

.bogie-no-gps {
  font-size: 0.62rem;
  color: var(--text-muted);
  margin-top: -2px;
}

/* Last-updated row */
.bogie-updated {
  font-size: 0.62rem;
  color: var(--text-muted);
  text-align: center;
  width: 100%;
}
.bogie-updated.stale-yellow { color: #92400e; font-weight: 700; background: #fffbeb; border-radius: 2px; padding: 1px 3px; }
.bogie-updated.stale-red    { color: #7f1d1d; font-weight: 700; background: #fee2e2; border-radius: 2px; padding: 1px 3px; }

/* Live update notification */
#live-notif-container {
  position: fixed;
  top: 66px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 300;
  pointer-events: none;
}
.live-notif {
  background: var(--primary);
  color: #fff;
  padding: 7px 14px;
  border-radius: var(--radius);
  font-size: 0.78rem;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  animation: notif-in 0.2s ease;
  pointer-events: all;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
}
.live-notif .notif-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #7dd3fc;
  flex-shrink: 0;
  animation: pulse 1s infinite;
}
@keyframes notif-in  { from { transform: translateX(40px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes notif-out { from { opacity: 1; } to { opacity: 0; transform: translateX(20px); } }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }

/* ---- Water tank SVG ---- */
.water-tank-wrap { position: relative; }
.water-tank-wrap svg { display: block; }

/* ---- Level badge ---- */
.level-badge {
  padding: 3px 8px;
  border-radius: 3px;
  font-size: 0.75rem;
  font-weight: 700;
  color: #fff;
  width: 100%;
  text-align: center;
}
.level-badge.ok       { background: var(--success); }
.level-badge.warning  { background: var(--warning); }
.level-badge.critical { background: var(--danger); }

/* ---- Battery ---- */
.battery-row {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.72rem;
  color: var(--text-muted);
  width: 100%;
}
.battery-row .bat-label {
  font-size: 0.62rem;
  color: var(--text-muted);
  flex-shrink: 0;
}
.battery-bar {
  flex: 1;
  height: 5px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}
.battery-fill {
  height: 100%;
  border-radius: 2px;
  transition: width 0.4s ease;
}
.battery-fill.ok       { background: var(--success); }
.battery-fill.warning  { background: var(--warning); }
.battery-fill.critical { background: var(--danger); }

/* ---- Add Bogie / Add Train buttons ---- */
.add-bogie-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 12px 10px;
  min-width: 90px;
  min-height: 160px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  background: transparent;
  transition: border-color 0.15s, color 0.15s;
}
.add-bogie-card:hover { border-color: var(--primary); color: var(--primary); }
.add-bogie-card .plus { font-size: 1.4rem; font-weight: 300; }

.add-train-wrap {
  display: flex;
  justify-content: center;
  padding: 16px 0;
}

/* ---- Empty state ---- */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}
.empty-state .icon { font-size: 2rem; margin-bottom: 12px; color: var(--text-muted); }
.empty-state h3 { font-size: 1rem; margin-bottom: 6px; color: var(--text); font-weight: 600; }

/* ---- Bogie detail page ---- */
.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 16px;
}
@media (max-width: 640px) { .detail-grid { grid-template-columns: 1fr; } }

.detail-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  padding: 18px;
}
.detail-card h3 {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 12px;
  font-weight: 700;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.875rem;
}
.stat-row:last-child { border-bottom: none; }
.stat-row .label { color: var(--text-muted); }
.stat-row .value { font-weight: 600; }

.big-tank { display: flex; justify-content: center; padding: 12px 0; }

/* ---- Admin page ---- */
.admin-login {
  max-width: 360px;
  margin: 80px auto;
  background: var(--card-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  padding: 32px;
}
.admin-login h2 { font-size: 1.1rem; margin-bottom: 20px; text-align: center; font-weight: 700; }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 0.82rem; font-weight: 600; margin-bottom: 5px; }
.form-group input, .form-group select {
  width: 100%;
  padding: 8px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.15s;
}
.form-group input:focus, .form-group select:focus { border-color: var(--primary); }

.error-msg { color: var(--danger); font-size: 0.8rem; margin-top: 8px; }

.admin-panel { display: flex; gap: 16px; }
.admin-sidebar {
  width: 200px;
  flex-shrink: 0;
}
.admin-sidebar nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 0;
  height: auto;
  position: static;
}
.admin-sidebar nav a {
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-muted);
  display: block;
  transition: background 0.15s, color 0.15s;
}
.admin-sidebar nav a:hover,
.admin-sidebar nav a.active { background: #eff3f8; color: var(--primary); text-decoration: none; }
.admin-content { flex: 1; }

/* ---- Tables ---- */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}
.data-table th {
  text-align: left;
  padding: 9px 14px;
  border-bottom: 2px solid var(--border);
  font-weight: 700;
  color: var(--text-muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.data-table td {
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: #f8f9fb; }

/* ---- Modal ---- */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 200;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--card-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  padding: 28px;
  width: 100%;
  max-width: 420px;
}
.modal h3 { font-size: 1rem; margin-bottom: 18px; font-weight: 700; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; }

/* ---- Search results ---- */
.search-results {
  background: var(--card-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
  overflow: hidden;
}
.search-results-header {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.875rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.search-result-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  text-decoration: none;
  color: inherit;
  transition: background 0.1s;
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--bg); }
.result-type {
  padding: 2px 8px;
  border-radius: 3px;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.result-type.train { background: #dbeafe; color: #1e3a5f; }
.result-type.bogie { background: #d1fae5; color: #065f46; }

/* ---- Spinner / loading ---- */
.spinner {
  display: inline-block;
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-wrap { display: flex; align-items: center; justify-content: center; padding: 60px; gap: 12px; color: var(--text-muted); font-size: 0.875rem; }

/* ---- Toast ---- */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 300;
}
.toast {
  background: var(--text);
  color: #fff;
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 0.875rem;
  box-shadow: var(--shadow-lg);
  animation: slide-in 0.2s ease;
  max-width: 300px;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
@keyframes slide-in { from { transform: translateX(40px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ---- Bogies flat list ---- */
.bogies-flat-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

.bogie-flat-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  text-decoration: none;
  color: inherit;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.bogie-flat-card:hover {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(30,58,95,0.1), var(--shadow);
  text-decoration: none;
}
.bogie-flat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}
.bogie-flat-id {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0.03em;
  text-transform: uppercase;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.bogie-flat-train {
  font-size: 0.7rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bogie-flat-train span {
  background: #eff3f8;
  color: var(--primary);
  padding: 1px 6px;
  border-radius: 3px;
  font-weight: 600;
}
.bogie-flat-train.unassigned { color: var(--text-muted); font-style: italic; }
.bogie-flat-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.72rem;
  color: var(--text-muted);
}
.bogie-flat-gps {
  font-size: 0.68rem;
  color: var(--primary);
}

/* ---- Responsive ---- */
@media (max-width: 768px) {
  .train-header { flex-wrap: wrap; }
  .bogies-row { gap: 8px; }
  .bogie-card { min-width: 90px; }
  nav .search-wrap { display: none; }
  .admin-panel { flex-direction: column; }
  .admin-sidebar { width: 100%; }
}
