:root {
  --bg: #f3f6f4;
  --surface: #ffffff;
  --surface-alt: #f8faf9;
  --border: #e2e8e4;
  --text: #14221b;
  --text-muted: #5a6d62;
  --primary: #15803d;
  --primary-hover: #166534;
  --secondary: #0f172a;

  --ideal: #059669;
  --ideal-bg: #ecfdf5;
  --ideal-border: #a7f3d0;

  --warn: #d97706;
  --warn-bg: #fffbeb;
  --warn-border: #fde68a;

  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --danger-border: #fecaca;

  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 6px 20px rgba(15, 23, 42, 0.04);
  --radius: 14px;
}

[data-theme="dark"] {
  --bg: #0c1310;
  --surface: #131e19;
  --surface-alt: #182620;
  --border: #25382f;
  --text: #edf5f0;
  --text-muted: #93aba0;
  --primary: #22c55e;
  --primary-hover: #16a34a;
  --secondary: #e2e8f0;

  --ideal: #10b981;
  --ideal-bg: rgba(16, 185, 129, 0.14);
  --ideal-border: rgba(16, 185, 129, 0.35);

  --warn: #f59e0b;
  --warn-bg: rgba(245, 158, 11, 0.14);
  --warn-border: rgba(245, 158, 11, 0.35);

  --danger: #ef4444;
  --danger-bg: rgba(239, 68, 68, 0.16);
  --danger-border: rgba(239, 68, 68, 0.4);

  --shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
}

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

body {
  margin: 0;
  font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.app-shell {
  max-width: 1320px;
  margin: 0 auto;
  padding: 20px 18px 48px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* Top Header */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 14px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, #059669, #15803d);
  color: #ffffff;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.topbar-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text-muted);
}

.pill-online {
  background: var(--ideal-bg);
  border-color: var(--ideal-border);
  color: var(--ideal);
}

.pill-offline {
  background: var(--danger-bg);
  border-color: var(--danger-border);
  color: var(--danger);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
}

.pill-online .dot {
  animation: pulseDot 1.6s infinite;
}

@keyframes pulseDot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(1.25); }
}

.last-sync {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
  padding: 0 4px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 9px 14px;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.btn:active {
  transform: scale(0.98);
}

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

.btn-sm {
  padding: 6px 11px;
  font-size: 0.8rem;
}

.btn-icon {
  padding: 7px;
}

.btn-primary {
  background: var(--primary);
  color: #ffffff;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--surface-alt);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover {
  border-color: var(--text-muted);
}

.btn-danger-active {
  background: var(--danger);
  color: #ffffff;
  border-color: var(--danger);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: var(--border);
}

.btn-ghost:hover {
  color: var(--text);
  background: var(--surface-alt);
}

.btn-outline-light {
  background: rgba(255, 255, 255, 0.18);
  color: inherit;
  border-color: rgba(255, 255, 255, 0.35);
  backdrop-filter: blur(4px);
}

.btn-outline-light:hover {
  background: rgba(255, 255, 255, 0.28);
}

/* Main Status Banner */
.status-banner {
  border-radius: var(--radius);
  padding: 18px 22px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  transition: background 0.25s ease, border-color 0.25s ease;
}

.status-neutral {
  background: var(--surface);
  color: var(--text);
}

.status-ideal {
  background: linear-gradient(135deg, #047857, #059669);
  border-color: #047857;
  color: #ffffff;
}

.status-waspada {
  background: linear-gradient(135deg, #b45309, #d97706);
  border-color: #b45309;
  color: #ffffff;
}

.status-bahaya {
  background: linear-gradient(135deg, #b91c1c, #dc2626);
  border-color: #991b1b;
  color: #ffffff;
  animation: dangerPulse 2.4s infinite;
}

@keyframes dangerPulse {
  0%, 100% { box-shadow: 0 4px 20px rgba(220, 38, 38, 0.28); }
  50% { box-shadow: 0 4px 32px rgba(220, 38, 38, 0.55); }
}

.status-banner-left {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  flex: 1 1 360px;
}

.status-icon-wrap {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.16);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.status-neutral .status-icon-wrap {
  background: var(--surface-alt);
  color: var(--text-muted);
}

.status-kicker {
  font-size: 0.74rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.85;
}

.status-title-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 2px;
}

.status-title-row h2 {
  margin: 0;
  font-size: 1.55rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.buzzer-pill {
  font-size: 0.75rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.18);
}

.status-neutral .buzzer-pill {
  background: var(--surface-alt);
  color: var(--text-muted);
}

.status-reasons {
  margin: 8px 0 0;
  padding-left: 18px;
  font-size: 0.9rem;
  opacity: 0.95;
}

.status-reasons li {
  margin-bottom: 2px;
}

.status-banner-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.status-neutral .btn-outline-light {
  background: var(--surface-alt);
  color: var(--text);
  border-color: var(--border);
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.card-header-wrap {
  flex-wrap: wrap;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.card-sub {
  margin: 2px 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.sensor-chip {
  display: inline-block;
  margin-top: 3px;
  font-size: 0.72rem;
  font-family: 'JetBrains Mono', monospace;
  color: var(--text-muted);
}

/* Metrics Grid */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.level-badge {
  font-size: 0.72rem;
  font-weight: 800;
  padding: 4px 9px;
  border-radius: 999px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  flex-shrink: 0;
}

.level-neutral {
  background: var(--surface-alt);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.level-ideal {
  background: var(--ideal-bg);
  color: var(--ideal);
  border: 1px solid var(--ideal-border);
}

.level-waspada {
  background: var(--warn-bg);
  color: var(--warn);
  border: 1px solid var(--warn-border);
}

.level-bahaya,
.level-error {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger-border);
}

/* Semi-circular SVG Gauge */
.gauge-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 4px 0 12px;
}

.arc-gauge {
  width: 195px;
  height: 112px;
  overflow: visible;
}

.arc-track {
  fill: none;
  stroke: var(--border);
  stroke-width: 14;
  stroke-linecap: round;
}

.arc-fill {
  fill: none;
  stroke: var(--ideal);
  stroke-width: 14;
  stroke-linecap: round;
  transition: stroke-dashoffset 0.55s cubic-bezier(0.22, 1, 0.36, 1), stroke 0.25s ease;
}

.gauge-readout {
  position: absolute;
  bottom: 4px;
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.metric-value {
  font-family: 'JetBrains Mono', monospace;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.metric-unit {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-muted);
}

/* Visual Min - Ideal - Max Range Bar */
.range-visual {
  margin-top: 6px;
}

.range-bar {
  position: relative;
  display: grid;
  grid-template-columns: 15% 20% 30% 20% 15%;
  height: 10px;
  border-radius: 999px;
  overflow: visible;
  gap: 2px;
}

.seg {
  height: 100%;
  border-radius: 3px;
}

.seg:first-child { border-radius: 999px 3px 3px 999px; }
.seg:last-child { border-radius: 3px 999px 999px 3px; }

.seg-danger { background: rgba(220, 38, 38, 0.72); }
.seg-warn { background: rgba(217, 119, 6, 0.72); }
.seg-ideal { background: rgba(5, 150, 105, 0.85); }

.range-pin {
  position: absolute;
  top: -5px;
  width: 6px;
  height: 20px;
  border-radius: 3px;
  background: var(--text);
  border: 2px solid var(--surface);
  transform: translateX(-50%);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  transition: left 0.45s cubic-bezier(0.22, 1, 0.36, 1);
}

.range-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  font-size: 0.73rem;
  font-family: 'JetBrains Mono', monospace;
  color: var(--text-muted);
}

.label-ideal {
  color: var(--ideal);
  font-weight: 700;
}

/* Main Split: Chart + Control Panel */
.main-split {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.chart-container {
  position: relative;
  height: 360px;
  width: 100%;
}

.controls-inline {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.select-input {
  padding: 7px 10px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 600;
}

/* Hardware Controls */
.hardware-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
}

.hw-action-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 11px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
}

.hw-action-box strong {
  font-size: 0.88rem;
  display: block;
}

.muted-text {
  margin: 2px 0 0;
  font-size: 0.76rem;
  color: var(--text-muted);
}

/* Threshold Calibration Form */
.threshold-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.threshold-group {
  padding: 11px 12px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
}

.threshold-group-title {
  font-size: 0.82rem;
  font-weight: 700;
  margin-bottom: 8px;
}

.threshold-inputs {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.threshold-inputs label {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.threshold-inputs label span {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-muted);
}

.threshold-inputs input {
  width: 100%;
  padding: 7px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.84rem;
  font-weight: 600;
}

.threshold-inputs input:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.form-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 4px;
}

/* Simulation bar */
.sim-bar {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px dashed var(--border);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.sim-label {
  font-size: 0.76rem;
  font-weight: 600;
  color: var(--text-muted);
}

.sim-buttons {
  display: flex;
  gap: 6px;
}

.btn-sim {
  padding: 5px 9px;
  font-size: 0.72rem;
  font-weight: 700;
  border-radius: 8px;
}

.btn-sim-ideal {
  background: var(--ideal-bg);
  color: var(--ideal);
  border-color: var(--ideal-border);
}

.btn-sim-warn {
  background: var(--warn-bg);
  color: var(--warn);
  border-color: var(--warn-border);
}

.btn-sim-danger {
  background: var(--danger-bg);
  color: var(--danger);
  border-color: var(--danger-border);
}

/* Table */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.log-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
  min-width: 680px;
}

.log-table th,
.log-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.log-table th {
  background: var(--surface-alt);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  font-weight: 700;
}

.log-table tbody tr:hover {
  background: var(--surface-alt);
}

.log-table td.mono {
  font-family: 'JetBrains Mono', monospace;
  white-space: nowrap;
}

.empty-cell {
  text-align: center !important;
  color: var(--text-muted);
  padding: 28px 12px !important;
}

/* Toasts */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 999;
  max-width: 360px;
}

.toast {
  padding: 12px 16px;
  border-radius: 10px;
  background: var(--secondary);
  color: #ffffff;
  font-size: 0.85rem;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  animation: slideUp 0.22s ease;
}

[data-theme="dark"] .toast {
  background: #1f2937;
  border: 1px solid var(--border);
}

.toast-success { border-left: 4px solid var(--ideal); }
.toast-error { border-left: 4px solid var(--danger); }
.toast-info { border-left: 4px solid var(--warn); }

@keyframes slideUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Responsive */
@media (max-width: 1024px) {
  .metrics-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .main-split {
    grid-template-columns: 1fr;
  }
  .chart-container {
    height: 300px;
  }
}

@media (max-width: 640px) {
  .app-shell {
    padding: 12px 12px 36px;
  }
  .metrics-grid {
    grid-template-columns: 1fr;
  }
  .threshold-inputs {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .status-banner {
    padding: 16px;
  }
  .status-title-row h2 {
    font-size: 1.3rem;
  }
  .topbar {
    padding: 12px 14px;
  }
}
