:root {
  --bg: #f3f6fb;
  --card: #ffffff;
  --text: #111827;
  --muted: #667085;
  --line: #d8dee9;
  --blue: #0f4c81;
  --green: #16a34a;
  --red: #dc2626;
  --yellow: #facc15;
  --shadow: 0 10px 25px rgba(15, 23, 42, 0.08);
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}
.topbar {
  background: linear-gradient(90deg, #082f49, #0f4c81);
  color: white;
  padding: 18px 28px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 20px;
}
h1 { margin: 0; font-size: 26px; letter-spacing: .2px; }
.subtitle { margin: 6px 0 0; color: #dbeafe; font-size: 14px; }
.connection-panel {
  min-width: 250px;
  padding: 12px 16px;
  border: 1px solid rgba(255,255,255,.25);
  border-radius: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(255,255,255,.08);
}
.conn-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #ef4444;
  box-shadow: 0 0 12px #ef4444;
}
.conn-dot.connected {
  background: #22c55e;
  box-shadow: 0 0 12px #22c55e;
}
.conn-text { font-weight: 700; }
.last-update { font-size: 12px; color: #dbeafe; margin-top: 3px; }
.top-nav {
  display: flex;
  gap: 6px;
  padding: 8px 28px;
  background: #ffffff;
  border-bottom: 1px solid var(--line);
}
.top-nav a {
  padding: 8px 14px;
  border-radius: 9px;
  color: var(--blue);
  font-weight: 700;
  text-decoration: none;
}
.top-nav a:hover,
.top-nav a.active {
  color: #ffffff;
  background: var(--blue);
}
main { padding: 22px 28px 30px; }
.toolbar {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px;
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}
.topic-box {
  padding: 8px 12px;
  border-radius: 10px;
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  font-size: 14px;
}
.topic-box span { color: var(--muted); margin-right: 6px; }
button {
  border: none;
  border-radius: 10px;
  padding: 9px 14px;
  cursor: pointer;
  color: white;
  background: #0f4c81;
  font-weight: 700;
}
button:hover { opacity: .9; }
.dashboard {
  display: grid;
  grid-template-columns: 1fr;
  gap: 18px;
}
.water-trend-panel {
  margin-top: 18px;
  padding: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
}
.water-trend-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.water-trend-header h2 {
  margin: 0;
  color: var(--blue);
  font-size: 20px;
}
.water-trend-ranges {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.water-trend-range {
  color: var(--blue);
  background: #ffffff;
  border: 1px solid var(--line);
}
.water-trend-range.active,
.water-trend-range:hover {
  color: #ffffff;
  background: var(--blue);
}
.water-trend-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(145px, 1fr));
  gap: 10px;
  margin-top: 14px;
}
.water-trend-stats div {
  padding: 10px 12px;
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
}
.water-trend-stats span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}
.water-trend-stats strong {
  display: block;
  margin-top: 4px;
  color: #0f172a;
  font-size: 18px;
}
.water-trend-chart-wrap {
  position: relative;
  height: 300px;
  margin-top: 14px;
  background: #fbfdff;
  border: 1px solid #dbe3ef;
  border-radius: 12px;
  overflow: hidden;
}
.water-trend-chart-wrap canvas {
  width: 100%;
  height: 100%;
  display: block;
}
.water-trend-message {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  color: var(--muted);
  text-align: center;
  pointer-events: none;
}
.water-trend-message[hidden] { display: none; }
.water-trend-notes {
  display: flex;
  gap: 8px 16px;
  flex-wrap: wrap;
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
}
.water-trend-warning {
  color: var(--red);
}
.water-trend-warning[hidden] { display: none; }
.class-section {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.class-header {
  padding: 14px 18px;
  background: #eaf2fb;
  border-bottom: 1px solid var(--line);
  font-size: 20px;
  font-weight: 800;
  color: #0f4c81;
}
.sub-grid {
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(310px, 1fr));
  gap: 14px;
}
.sub-card {
  border: 1px solid #dbe3ef;
  border-radius: 14px;
  background: #fbfdff;
  overflow: hidden;
}
.sub-title {
  padding: 12px 14px;
  font-weight: 800;
  background: #f1f5f9;
  border-bottom: 1px solid #dbe3ef;
}
.field-row {
  display: grid;
  grid-template-columns: minmax(145px, 1fr) minmax(115px, auto);
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid #edf2f7;
}
.field-row:last-child { border-bottom: none; }
.field-name {
  font-size: 14px;
  color: #334155;
  line-height: 1.25;
}
.field-id {
  font-size: 11px;
  color: #94a3b8;
  margin-top: 3px;
}
.value-badge {
  min-width: 100px;
  text-align: center;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  padding: 7px 10px;
  font-weight: 800;
  font-size: 14px;
  background: white;
}
.value-badge.number {
  font-size: 18px;
}
.blink {
  animation: blinkAnim 1s linear infinite;
}
@keyframes blinkAnim {
  0%, 45% { opacity: 1; }
  50%, 100% { opacity: .18; }
}
.raw-panel {
  margin-top: 18px;
  background: #111827;
  color: #e5e7eb;
  border-radius: 16px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.raw-panel h2 { font-size: 16px; margin: 0 0 10px; color: white; }
pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 13px;
}
.logs-panel {
  margin-top: 18px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
}
.log-category-tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 2px;
}
.log-category-tab {
  flex: 0 0 auto;
  color: var(--blue);
  background: #ffffff;
  border: 1px solid var(--line);
}
.log-category-tab.active,
.log-category-tab:hover {
  color: #ffffff;
  background: var(--blue);
}
.log-category-panel[hidden] { display: none; }
.log-category-panel > h2 {
  margin: 0;
  color: var(--blue);
  font-size: 20px;
}
.log-range-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 14px;
}
.log-range-btn {
  color: var(--blue);
  background: #ffffff;
  border: 1px solid var(--line);
}
.log-range-btn.active,
.log-range-btn:hover {
  color: #ffffff;
  background: var(--blue);
}
.log-placeholder {
  margin: 14px 0 0;
  padding: 18px;
  color: var(--muted);
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
}
.log-export-controls {
  display: flex;
  justify-content: flex-end;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 14px;
}
.log-export-controls label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}
.log-export-controls input {
  padding: 8px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  font-size: 14px;
}
.logs-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.logs-header h2 {
  margin: 0;
  color: var(--blue);
  font-size: 20px;
}
.logs-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.logs-actions input {
  padding: 8px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  font-size: 14px;
}
.logs-message {
  min-height: 18px;
  margin: 10px 0 4px;
  color: var(--muted);
  font-size: 13px;
}
.logs-message.error { color: var(--red); }
.logs-table-wrap { overflow-x: auto; }
.logs-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.logs-table th,
.logs-table td {
  padding: 10px 12px;
  border-bottom: 1px solid #e5e7eb;
  text-align: left;
}
.logs-table th {
  color: #334155;
  background: #f1f5f9;
}
.logs-empty {
  color: var(--muted);
  text-align: center !important;
}
.logs-pagination {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 12px;
  margin-top: 14px;
}
.logs-pagination span {
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}
.logs-pagination button:disabled {
  cursor: not-allowed;
  opacity: .45;
}
footer {
  padding: 14px 28px 24px;
  color: #667085;
  font-size: 13px;
}
@media (max-width: 720px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .connection-panel { width: 100%; }
  .top-nav { padding: 8px 16px; }
  main { padding: 16px; }
  .sub-grid { grid-template-columns: 1fr; }
  .logs-actions { width: 100%; }
}

.penstock-image-wrap {
  padding: 10px 12px 8px;
  border-bottom: 1px solid #edf2f7;
  background: #fbfdff;
}
.penstock-image {
  width: 100%;
  height: 118px;
  object-fit: contain;
  display: block;
  border-radius: 10px;
  background: #ffffff;
}
.penstock-image-caption {
  margin-top: 5px;
  font-size: 11px;
  color: #64748b;
  text-align: center;
}


.penstock-image-wrap img,
.penstock-position-image,
.penstock-img {
  width: 100%;
  height: 260px;
  object-fit: contain;
  display: block;
  margin: 0 auto;
  background: #ffffff;
}

.penstock-image-caption {
  text-align: center;
  font-size: 13px;
  color: #475569;
  margin-top: 6px;
}

.login-box {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.login-box select,
.login-box input {
  padding: 8px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  font-size: 14px;
}

.login-box input {
  width: 150px;
}

.login-box strong {
  color: #0f172a;
  background: #e2e8f0;
  padding: 7px 10px;
  border-radius: 10px;
}


.command-panel {
  margin: 10px 12px 14px;
  padding-top: 12px;
  border-top: 1px solid #e5e7eb;
}

.command-buttons.normal-command-buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 0;
}

.command-buttons.estop-command-buttons {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  margin-top: 10px;
}

.cmd-btn {
  width: 100%;
  height: 42px;
  border: none;
  border-radius: 10px;
  padding: 0 10px;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  letter-spacing: 0.3px;
}

.cmd-btn:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}

.cmd-open {
  background: #16a34a;
  color: #ffffff;
}

.cmd-close {
  background: #dc2626;
  color: #ffffff;
}

.cmd-stop {
  background: #f59e0b;
  color: #111827;
}

.cmd-estop {
  background: #991b1b;
  color: #facc15;
  height: 46px;
  font-size: 15px;
}

.control-status {
  margin-top: 8px;
  font-size: 13px;
  color: #475569;
}

.control-status.available {
  color: #16a34a;
  font-weight: 700;
}

.control-status.disabled {
  color: #dc2626;
  font-weight: 700;
}

.account-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}
.account-controls strong { color: #334155; font-size: 13px; }
.account-controls button { padding: 7px 10px; }
.auth-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(8, 47, 73, .82);
}
.auth-overlay[hidden] { display: none; }
.auth-card,
.auth-dialog {
  width: min(420px, 100%);
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #ffffff;
  box-shadow: var(--shadow);
}
.auth-card h2,
.auth-dialog h2 { margin: 0 0 16px; color: var(--blue); }
.auth-card label,
.auth-dialog label {
  display: grid;
  gap: 5px;
  margin-bottom: 12px;
  color: #334155;
  font-size: 13px;
  font-weight: 700;
}
.auth-card input,
.auth-dialog input,
.auth-dialog select {
  padding: 9px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 9px;
  font-size: 14px;
}
.auth-message { min-height: 18px; margin: 8px 0; color: var(--green); font-size: 13px; }
.auth-message.error { color: var(--red); }
.auth-dialog { width: min(520px, calc(100% - 30px)); }
.auth-dialog::backdrop { background: rgba(15, 23, 42, .55); }
.users-dialog { width: min(900px, calc(100% - 30px)); }
.dialog-header,
.dialog-actions,
.add-user-form,
.users-dialog td:last-child {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.dialog-header { justify-content: space-between; margin-bottom: 14px; }
.dialog-header h2 { margin: 0; }
.dialog-actions { justify-content: flex-end; margin-top: 14px; }
.add-user-form { margin-bottom: 10px; }
.add-user-form input { flex: 1 1 180px; }
.users-dialog td button { padding: 6px 8px; }

@media (max-width: 720px) {
  .top-nav { flex-wrap: wrap; }
  .account-controls { width: 100%; margin-left: 0; }
}
