/* ── Design tokens ────────────────────────────────────────────────── */
:root {
  --bg:          #0d1117;
  --surface:     #161b22;
  --surface2:    #1f2937;
  --border:      #30363d;
  --accent:      #00c896;
  --accent-dim:  #00a07a;
  --accent-modbus: #a78bfa;
  --danger:      #f87171;
  --success:     #4ade80;
  --text:        #e6edf3;
  --text-muted:  #8b949e;
  --topbar-h:    52px;
  --side-w:      280px;
  --radius:      8px;
  --font-mono:   'JetBrains Mono', 'Fira Code', monospace;

  /* Gauge colors */
  --gauge-track:  #1f2937;
  --gauge-needle: #ffffff;
  --gauge-pivot:  #374151;
  --btn-primary-text: #000000;
}

/* Light mode overrides */
[data-theme="light"] {
  --bg:          #f6f8fa;
  --surface:     #ffffff;
  --surface2:    #f0f2f5;
  --border:      #d0d7de;
  --accent:      #059669; /* beautiful emerald for light mode */
  --accent-dim:  #047857;
  --accent-modbus: #6366f1; /* beautiful indigo for light mode modbus */
  --danger:      #cf222e;
  --success:     #1a7f37;
  --text:        #24292f;
  --text-muted:  #57606a;

  /* Light mode gauge colors */
  --gauge-track:  #e5e7eb;
  --gauge-needle: #24292f;
  --gauge-pivot:  #d0d7de;
  --btn-primary-text: #ffffff;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Top bar ──────────────────────────────────────────────────────── */
.topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 16px;
  flex-shrink: 0;
  z-index: 1000;
}

.topbar-left { display: flex; align-items: baseline; gap: 8px; flex: 1; }

.device-label {
  font-weight: 600;
  font-size: 15px;
  color: var(--accent);
  letter-spacing: 0.01em;
}

.device-id {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
}

.topbar-nav { display: flex; gap: 4px; }

.nav-link {
  padding: 6px 14px;
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text-muted);
  font-size: 13px;
  transition: background 0.15s, color 0.15s;
}
.nav-link:hover  { background: var(--surface2); color: var(--text); }
.nav-link.active { background: var(--surface2); color: var(--accent); }

.status-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: background 0.4s;
  flex-shrink: 0;
}
.status-dot.live    { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.status-dot.offline { background: var(--danger); }

/* ── Main layout ──────────────────────────────────────────────────── */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr var(--side-w);
  overflow: hidden;
}

/* ── Map ──────────────────────────────────────────────────────────── */
.map-panel { position: relative; }
#map { width: 100%; height: 100%; }

/* Leaflet dark-mode tile filter */
.leaflet-tile { filter: brightness(0.85) saturate(0.9); }
[data-theme="light"] .leaflet-tile { filter: none; }

/* ── Side panel ───────────────────────────────────────────────────── */
.side-panel {
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  overflow-y: auto;
}

/* ── Gauge card ───────────────────────────────────────────────────── */
.gauge-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  text-align: center;
}

.gauge-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 4px;
}

#speedGauge { display: block; margin: 0 auto; }

.gauge-value {
  margin-top: 4px;
  font-size: 36px;
  font-weight: 700;
  font-family: var(--font-mono);
  color: var(--accent);
  line-height: 1;
}
.gauge-unit {
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 400;
  margin-left: 4px;
}

/* ── Info card ────────────────────────────────────────────────────── */
.info-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
}
.info-row:last-child { border-bottom: none; }

.info-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.info-value {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text);
}

/* ── Track controls ───────────────────────────────────────────────── */
.track-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 0;
}

.track-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-muted);
  user-select: none;
}

.btn-sm {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text-muted);
  font-size: 11px;
  padding: 4px 10px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.btn-sm:hover { border-color: var(--accent); color: var(--accent); }

/* ── Settings layout ──────────────────────────────────────────────── */
.settings-layout {
  flex: 1;
  overflow-y: auto;
  padding: 32px 16px;
  display: flex;
  justify-content: center;
}

.settings-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 32px;
  width: 100%;
  max-width: 620px;
  height: fit-content;
}

.settings-title {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 4px;
}

.settings-desc {
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 24px;
}

/* ── Alerts ───────────────────────────────────────────────────────── */
.alert {
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 20px;
}
.alert-success {
  background: #0d2818;
  border: 1px solid #16a34a;
  color: var(--success);
}
.alert-error {
  background: #2d1010;
  border: 1px solid #b91c1c;
  color: var(--danger);
}

[data-theme="light"] .alert-success {
  background: #dafbe1;
  border-color: #1a7f37;
  color: #1a7f37;
}
[data-theme="light"] .alert-error {
  background: #ffebe9;
  border-color: #cf222e;
  color: #cf222e;
}
.alert ul { margin: 6px 0 0 16px; }

/* ── Form fields ──────────────────────────────────────────────────── */
.field-group { margin-bottom: 18px; }

.field-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 4px;
}
.required { color: var(--danger); margin-left: 2px; }

.field-hint {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 6px;
  line-height: 1.5;
}

.field-input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  padding: 8px 10px;
  outline: none;
  transition: border-color 0.15s;
  font-family: inherit;
}
.field-input:focus { border-color: var(--accent); }
select.field-input { cursor: pointer; }

/* ── Toggle (boolean) ─────────────────────────────────────────────── */
.toggle-label {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
}
.toggle-input { display: none; }
.toggle-slider {
  width: 40px; height: 22px;
  background: var(--border);
  border-radius: 11px;
  position: relative;
  transition: background 0.2s;
  flex-shrink: 0;
}
.toggle-slider::after {
  content: '';
  position: absolute;
  top: 3px; left: 3px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: transform 0.2s, background 0.2s;
}
.toggle-input:checked ~ .toggle-slider { background: var(--accent); }
.toggle-input:checked ~ .toggle-slider::after {
  transform: translateX(18px);
  background: #fff;
}
.toggle-text { font-size: 13px; color: var(--text-muted); }

/* ── Form actions ─────────────────────────────────────────────────── */
.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 28px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.btn-primary {
  background: var(--accent);
  border: none;
  border-radius: 6px;
  color: var(--btn-primary-text);
  font-size: 13px;
  font-weight: 600;
  padding: 9px 20px;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-primary:hover { background: var(--accent-dim); }

.btn-secondary {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 13px;
  padding: 9px 20px;
  text-decoration: none;
  cursor: pointer;
  transition: border-color 0.15s;
}
.btn-secondary:hover { border-color: var(--text-muted); color: var(--text); }

/* ── Responsive — tablet (768px) ─────────────────────────────────── */
@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: 50vh 1fr;
  }
  .side-panel {
    border-left: none;
    border-top: 1px solid var(--border);
    flex-direction: row;
    flex-wrap: wrap;
    overflow-x: auto;
  }
  .gauge-card { min-width: 200px; flex: 1; }
  .info-card  { min-width: 220px; flex: 2; }
}

/* ── Responsive — mobile phone (480px) ───────────────────────────── */
/* Map hidden: only gauges + info visible, stacked vertically.        */
@media (max-width: 480px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;   /* single row — side-panel takes full height */
  }

  /* Hide the map entirely on mobile */
  .map-panel { display: none; }

  /* Side panel: full-page, vertical stack, scrollable */
  .side-panel {
    border: none;
    flex-direction: column;
    align-items: stretch;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 12px;
    gap: 10px;
  }

  /* Gauges side-by-side on small screens */
  .gauge-card {
    min-width: unset;
    flex: unset;
    width: 100%;
  }

  /* Make gauge canvas fill available width */
  #speedGaugeGps,
  #speedGaugeModbus {
    width: 100% !important;
    height: auto !important;
    max-height: 120px;
  }

  /* Info card full width */
  .info-card {
    min-width: unset;
    flex: unset;
    width: 100%;
  }

  /* Slightly larger text in info rows for finger-readability */
  .info-value { font-size: 13px; }
  .gauge-value { font-size: 28px; }

  /* Track controls compact */
  .track-controls {
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0;
  }
}

/* ── Dual gauge additions ─────────────────────────────────────────── */
.gauge-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 4px;
}

.gauge-badge {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.08em;
  padding: 2px 6px;
  border-radius: 4px;
  text-transform: uppercase;
}
.gauge-badge.gps    { background: #0d2818; color: var(--accent); border: 1px solid var(--accent); }
.gauge-badge.modbus { background: #1a1535; color: #a78bfa;       border: 1px solid #7c3aed; }

.gauge-sub {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
  text-align: center;
}

/* delta speed colouring */
.delta-positive { color: var(--danger); }
.delta-negative { color: var(--success); }
.delta-neutral  { color: var(--text-muted); }

/* ── Light mode gauge badge overrides ────────────────────────────── */
[data-theme="light"] .gauge-badge.gps {
  background: #dafbe1;
  color: #1a7f37;
  border-color: #1a7f37;
}
[data-theme="light"] .gauge-badge.modbus {
  background: #efe9ff;
  color: #6366f1;
  border-color: #818cf8;
}

/* ── Theme Toggle Button ────────────────────────────────────────── */
.theme-toggle-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}
.theme-toggle-btn:hover {
  background: var(--surface2);
  color: var(--text);
}
.theme-toggle-btn svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
}


/* ── Main layout ────────────────────────────────────────────────────────── */
.mob-layout {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display:    flex;
  flex-direction: column;
  gap:        10px;
  padding:    10px;
}

/* ── Widget card ─────────────────────────────────────────────────────────── */
.mob-widget {
  background:    var(--surface);
  border:        1px solid var(--border);
  border-radius: var(--radius);
  padding:       14px 12px 10px;
  display:       flex;
  flex-direction: column;
  align-items:   center;
  gap:           6px;
  /* Prevent widgets from being too small in portrait */
  flex-shrink:   0;
}

.mob-widget-header {
  width:       100%;
  display:     flex;
  align-items: center;
  justify-content: space-between;
}

.mob-widget-title {
  font-size:    11px;
  font-weight:  600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color:        var(--text-muted);
}

/* ── Badges ─────────────────────────────────────────────────────────────── */
.mob-badge {
  font-size:    9px;
  font-weight:  700;
  letter-spacing: 0.08em;
  padding:      2px 7px;
  border-radius: 4px;
  text-transform: uppercase;
}
.mob-badge.gps    { background: #0d2818; color: var(--accent);       border: 1px solid var(--accent); }
.mob-badge.modbus { background: #1a1535; color: var(--modbus-color); border: 1px solid #7c3aed; }

.mob-fix-badge {
  font-size:    9px;
  font-weight:  700;
  letter-spacing: 0.06em;
  padding:      2px 7px;
  border-radius: 4px;
  text-transform: uppercase;
  background:   #2d1010;
  color:        var(--danger);
  border:       1px solid var(--danger);
  transition:   background 0.3s, color 0.3s, border-color 0.3s;
}
.mob-fix-badge.ok {
  background:   #0d2818;
  color:        var(--success);
  border-color: var(--success);
}

/* ── Gauge canvas wrapper ────────────────────────────────────────────────── */
.mob-gauge-wrap {
  width:    100%;
  position: relative;
}

.mob-gauge-wrap canvas {
  display: block;
  width:   100% !important;
  height:  auto !important;
}

/* ── Speed display ───────────────────────────────────────────────────────── */
.mob-speed-display {
  display:     flex;
  align-items: baseline;
  gap:         5px;
  margin-top:  -4px;
}

.mob-speed-val {
  font-family: var(--font-mono);
  font-size:   84px;
  font-weight: 700;
  line-height: 1;
  color:       var(--accent);
}
.mob-speed-val.modbus-color { color: var(--modbus-color); }

.mob-speed-unit {
  font-size:  13px;
  color:      var(--text-muted);
  font-weight: 400;
}

.mob-reg-display {
  font-family: var(--font-mono);
  font-size:   48px;
  color:       var(--text-muted);
  text-align:  center;
}

/* ── Info grid ───────────────────────────────────────────────────────────── */
.mob-info-grid {
  width:                100%;
  display:              grid;
  grid-template-columns: 1fr 1fr;
  gap:                  1px;
  background:           var(--border);
  border-radius:        6px;
  overflow:             hidden;
}

.mob-info-item {
  background: var(--surface2);
  padding:    10px 12px;
  display:    flex;
  flex-direction: column;
  gap:        3px;
}

.mob-info-wide {
  grid-column: 1 / -1;   /* span both columns */
}

.mob-info-label {
  font-size:    9px;
  font-weight:  700;
  letter-spacing: 0.1em;
  color:        var(--text-muted);
  text-transform: uppercase;
}

.mob-info-val {
  font-family: var(--font-mono);
  font-size:   14px;
  color:       var(--text);
  line-height: 1.2;
}

/* Delta speed colour */
.delta-pos { color: var(--danger); }
.delta-neg { color: var(--accent); }
.delta-neu { color: var(--text-muted); }

/* ── LANDSCAPE: 3 kolom berdampingan ────────────────────────────────────── */
@media (orientation: landscape) {
  .mob-layout {
    flex-direction: row;
    align-items:   stretch;
    overflow-y:    hidden;
    overflow-x:    hidden;
    padding:       8px;
    gap:           8px;
  }

  .mob-widget {
    flex:      1;
    min-width: 0;
    /* Each widget fills equal height of viewport */
    overflow-y: auto;
  }

  /* Gauge canvas taller in landscape — parent is narrower */
  .mob-gauge-wrap canvas {
    max-height: 38vw;
  }

  .mob-speed-val { font-size: 64px; }

  /* Info grid: single column in landscape (widget is narrow) */
  .mob-info-grid {
    grid-template-columns: 1fr;
  }
  .mob-info-wide {
    grid-column: 1;
  }

  /* Compact topbar in landscape to save vertical space */
  .mob-topbar { height: 38px; min-height: 38px; }
  :root { --topbar-h: 38px; }
}

/* ── Very small screens (< 360px width) ─────────────────────────────────── */
@media (max-width: 360px) {
  .mob-speed-val { font-size: 68px; }
  .mob-info-val  { font-size: 12px; }
  .mob-nav-link  { padding: 4px 7px; font-size: 11px; }
}
