/*
 * Gaya tambahan di atas Bootstrap 5.3.
 *
 * Warna ditulis sebagai VARIABEL CSS (--app-..., --chart-...) dengan dua set
 * nilai: satu untuk mode terang, satu untuk mode gelap. Atribut
 * <html data-bs-theme="..."> (dipasang static/js/theme.js) menentukan set
 * mana yang berlaku. Elemen cukup memakai var(--nama), tanpa peduli modenya.
 */

/* ---------- Token warna ---------- */
:root,
[data-bs-theme="light"] {
  --app-bg: #f4f6f9;              /* latar halaman */
  --app-surface: #ffffff;         /* card */
  --app-surface-2: #f8f9fb;       /* bagian dalam card, kepala tabel */
  --app-border: #e4e7ec;
  --app-muted: #667085;
  --app-shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
  --app-shadow-hover: 0 6px 16px rgba(16, 24, 40, 0.08);
  --app-accent: #2a78d6;
  --app-accent-soft: rgba(42, 120, 214, 0.1);
  --app-nav-bg: rgba(255, 255, 255, 0.85);

  /* Chart: biru seri-1 dan tinta sumbu dari palet yang sudah divalidasi. */
  --chart-series: #2a78d6;
  --chart-fill: rgba(42, 120, 214, 0.1);
  --chart-ink: #52514e;
  --chart-grid: #ebebe8;
  --chart-tooltip-bg: #ffffff;
  --chart-tooltip-text: #0b0b0b;
  --chart-tooltip-border: #e4e7ec;
}

[data-bs-theme="dark"] {
  --app-bg: #0f1115;
  --app-surface: #171a20;
  --app-surface-2: #1d2128;
  --app-border: #2a2f38;
  --app-muted: #9aa3b2;
  --app-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  --app-shadow-hover: 0 6px 18px rgba(0, 0, 0, 0.5);
  --app-accent: #3987e5;
  --app-accent-soft: rgba(57, 135, 229, 0.16);
  --app-nav-bg: rgba(23, 26, 32, 0.85);

  /* Mode gelap memakai langkah warna tersendiri, bukan sekadar dibalik. */
  --chart-series: #3987e5;
  --chart-fill: rgba(57, 135, 229, 0.18);
  --chart-ink: #c3c2b7;
  --chart-grid: #2c2f35;
  --chart-tooltip-bg: #22262e;
  --chart-tooltip-text: #ffffff;
  --chart-tooltip-border: #343a45;

  /* Sesuaikan beberapa warna dasar Bootstrap agar serasi dengan latar di atas. */
  --bs-body-bg: #171a20;
  --bs-secondary-bg: #1d2128;
  --bs-tertiary-bg: #1a1e24;
  --bs-border-color: #2a2f38;
}

/* ---------- Dasar ---------- */
body {
  background-color: var(--app-bg);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, .h1, h2, .h2, h3, .h3 {
  font-weight: 650;
  letter-spacing: -0.01em;
}

.text-muted {
  color: var(--app-muted) !important;
}

/* ---------- Kerangka: side menu + isi ---------- */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.app-sidebar {
  --bs-offcanvas-width: 270px;
  --bs-offcanvas-bg: var(--app-surface);
  background: var(--app-surface);
  border-right: 1px solid var(--app-border);
}

/* Layar lebar: side menu tetap di kiri setinggi layar. */
@media (min-width: 992px) {
  .app-sidebar {
    position: sticky;
    top: 0;
    width: 248px;
    height: 100vh;
    flex-shrink: 0;
  }
  .app-sidebar .offcanvas-body {
    flex-direction: column;
    height: 100%;
    padding: 1.1rem 0.9rem !important;
    overflow-y: auto;
  }
}

.sidebar-body {
  display: flex;
  flex-direction: column;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.01em;
  color: var(--bs-body-color);
  text-decoration: none;
}

.app-sidebar .sidebar-brand.d-lg-flex {
  padding: 0 0.5rem 0.75rem;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  color: #fff;
  background: linear-gradient(135deg, #2a78d6, #1baf7a);
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar-section {
  margin: 1rem 0.75rem 0.35rem;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--app-muted);
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.5rem 0.75rem;
  border-radius: 0.55rem;
  color: var(--app-muted);
  font-weight: 500;
  text-decoration: none;
}

.sidebar-link:hover {
  color: var(--bs-body-color);
  background: var(--app-surface-2);
}

.sidebar-link.active {
  color: var(--app-accent);
  background: var(--app-accent-soft);
}

.sidebar-footer {
  margin-top: auto;
  padding: 1rem 0.5rem 0;
  border-top: 1px solid var(--app-border);
}

.app-main {
  flex: 1;
  min-width: 0;   /* tabel lebar tidak mendorong halaman melebihi layar */
}

.app-content {
  max-width: 1440px;
  padding: 1.5rem 1rem 3rem;
}

@media (min-width: 992px) {
  .app-content {
    padding: 2rem 2.25rem 3rem;
  }
}

/* Bilah atas di layar kecil, dan bilah atas halaman publik. */
.app-topbar,
.public-topbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  position: sticky;
  top: 0;
  z-index: 1020;
  padding: 0.6rem 1rem;
  background: var(--app-nav-bg);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--app-border);
}

.public-topbar {
  margin-bottom: 2rem;
  padding: 0.75rem 0;
}

.user-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  min-width: 0;
  font-size: 0.9rem;
}

/* Di sidebar, user-chip adalah tautan ke halaman profil. */
a.user-chip {
  margin: -0.25rem -0.4rem;
  padding: 0.25rem 0.4rem;
  border-radius: 0.5rem;
  color: inherit;
  text-decoration: none;
}

a.user-chip:hover,
a.user-chip.active {
  background: var(--app-surface-2);
}

.user-chip .avatar {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--app-accent);
  background: var(--app-accent-soft);
}

/* Saklar tema: tiga tombol ikon yang menyatu. */
.theme-switch {
  padding: 2px;
  border: 1px solid var(--app-border);
  border-radius: 0.6rem;
  background: var(--app-surface-2);
}

.theme-switch .btn {
  border: 0;
  border-radius: 0.45rem !important;
  color: var(--app-muted);
  padding: 0.2rem 0.45rem;
  line-height: 1;
}

.theme-switch .btn:hover {
  color: var(--bs-body-color);
}

.theme-switch .btn.active {
  color: var(--app-accent);
  background: var(--app-accent-soft);
}

/* ---------- Card ---------- */
.card {
  --bs-card-bg: var(--app-surface);
  --bs-card-border-color: var(--app-border);
  --bs-card-border-radius: 0.85rem;
  --bs-card-inner-border-radius: calc(0.85rem - 1px);
}

.card.shadow-sm {
  box-shadow: var(--app-shadow) !important;
}

/* Card nilai terkini */
.value-card {
  transition: box-shadow 0.2s, transform 0.2s;
}

.value-card:has(.stretched-link):hover {
  box-shadow: var(--app-shadow-hover) !important;
  transform: translateY(-2px);
}

.value-card .value {
  font-size: 2.1rem;
  font-weight: 650;
  line-height: 1.1;
  margin: 0.35rem 0 0.25rem;
  font-variant-numeric: tabular-nums;
}

.value-card .key {
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: 0.72rem;
  font-weight: 600;
}

.value-card .unit {
  font-size: 1.05rem;
  font-weight: 500;
  color: var(--app-muted);
}

/* Sorotan singkat pada card yang baru diperbarui lewat WebSocket. */
.live-flash {
  box-shadow: 0 0 0 3px var(--app-accent-soft), var(--app-shadow-hover) !important;
  transition: box-shadow 0.3s;
}

/* ---------- Tabel ---------- */
.table {
  --bs-table-bg: transparent;
}

.table > thead th {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
  color: var(--app-muted);
  white-space: nowrap;
}

.table > :not(caption) > * > * {
  padding-top: 0.55rem;
  padding-bottom: 0.55rem;
}

.table td {
  font-variant-numeric: tabular-nums;
}

/* Kelas "light" Bootstrap tetap terang di mode gelap; samakan dengan permukaan gelap. */
.table-light {
  --bs-table-color: var(--bs-body-color);
  --bs-table-bg: var(--app-surface-2);
  --bs-table-border-color: var(--app-border);
}

[data-bs-theme="dark"] .text-bg-light {
  color: var(--bs-body-color) !important;
  background-color: var(--app-surface-2) !important;
}

[data-bs-theme="dark"] .alert-light {
  --bs-alert-color: var(--bs-body-color);
  --bs-alert-bg: var(--app-surface-2);
  --bs-alert-border-color: var(--app-border);
}

/* ---------- Chart ---------- */
.chart-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.chart-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.chart-tools .form-select {
  min-width: 9.5rem;
}

.chart-tools .btn svg {
  vertical-align: -2px;
}

/* Tinggi area chart; Chart.js mengisi kotak ini (maintainAspectRatio: false). */
.chart-box {
  position: relative;
  height: 300px;
}

/* ---------- Peta ---------- */
/* Kotak peta Leaflet; tingginya harus ditentukan, kalau tidak peta tidak tampil. */
.map-box {
  height: 360px;
  border-radius: 0.6rem;
}

/* Peta OpenStreetMap hanya punya versi terang; di mode gelap ubinnya digelapkan. */
[data-bs-theme="dark"] .leaflet-tile {
  filter: invert(1) hue-rotate(180deg) brightness(0.85) contrast(0.9);
}

/* ---------- Dashboard ---------- */
.stat-tile .stat-label {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--app-muted);
}

.stat-tile .stat-value {
  font-size: 1.9rem;
  font-weight: 650;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.stat-tile .stat-note {
  font-size: 0.8rem;
  color: var(--app-muted);
}

.dash-location-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.dash-location-header .pin {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  color: var(--app-accent);
  background: var(--app-accent-soft);
}

.dash-device > .card-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  background: transparent;
  border-bottom-color: var(--app-border);
}

/* Card variabel di dalam card device: permukaan sedikit berbeda. */
.dash-var {
  position: relative;
  height: 100%;
  padding: 0.85rem 1rem;
  border: 1px solid var(--app-border);
  border-radius: 0.7rem;
  background: var(--app-surface-2);
  transition: box-shadow 0.2s, transform 0.2s;
}

.dash-var:has(.stretched-link):hover {
  box-shadow: var(--app-shadow-hover);
  transform: translateY(-2px);
}

.dash-var .value {
  font-size: 1.7rem;
}

/* Penanda status device: titik warna SELALU disertai teks. */
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.15rem 0.6rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--app-muted);
  background: var(--app-surface-2);
  border: 1px solid var(--app-border);
}

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

.status-pill.online {
  color: #0ca30c;
  border-color: rgba(12, 163, 12, 0.35);
  background: rgba(12, 163, 12, 0.08);
}

/* Halaman Customize: anak dari lokasi/device yang disembunyikan tampak redup. */
.settings-children.is-muted {
  opacity: 0.45;
}

.settings-device + .settings-device {
  border-top: 1px dashed var(--app-border);
  padding-top: 0.75rem;
  margin-top: 0.75rem;
}

/* Lencana protokol terakhir (HTTP / MQTT / CoAP). */
.protocol-badge {
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--app-accent);
  background: var(--app-accent-soft);
}

/* ---------- Rule engine (tahap 7) ---------- */
.model-badge {
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--app-accent);
  background: var(--app-accent-soft);
}

.rule-state {
  color: var(--app-muted);
  background: var(--app-surface-2);
  border: 1px solid var(--app-border);
}

.rule-state-pending {
  color: #a15c00;
  background: rgba(240, 160, 0, 0.14);
  border-color: rgba(240, 160, 0, 0.4);
}

.rule-state-active {
  color: #0b8a0b;
  background: rgba(12, 163, 12, 0.1);
  border-color: rgba(12, 163, 12, 0.35);
}

/* Jadwal yang tiba tetapi tidak dijalankan (tahap 14). */
.rule-state-skipped {
  font-style: italic;
  border-style: dashed;
}

.schedule-box {
  padding: 0.75rem 1rem;
  border: 1px solid var(--app-border);
  border-radius: 0.5rem;
  background: var(--app-surface-2);
}

[data-bs-theme="dark"] .rule-state-pending { color: #f2b544; }
[data-bs-theme="dark"] .rule-state-active { color: #4cc94c; }

/* Keparahan alarm: warna SELALU disertai teks. */
.severity { color: #fff; }
.severity-critical { background: #c62828; }
.severity-major { background: #e4572e; }
.severity-minor { background: #d19a00; }
.severity-warning { background: #6b7280; }

.model-card.is-planned { opacity: 0.7; }
.model-card.is-planned .card-body { background: var(--app-surface-2); border-radius: inherit; }

.model-example {
  white-space: normal;
  color: inherit;
  font-size: 0.78rem;
}

.eca-heading {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.eca-step {
  display: inline-grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  font-size: 0.8rem;
  color: var(--app-accent);
  background: var(--app-accent-soft);
}

.action-row {
  background: var(--app-surface-2);
}

.eca-steps li + li { margin-top: 0.6rem; }

.eca-key {
  display: inline-block;
  min-width: 3.5rem;
  font-weight: 650;
  color: var(--app-accent);
}

.sidebar-link .badge { font-size: 0.7rem; }

/* ---------- Editor kondisi "OR berisi AND" ---------- */
.cond-group {
  position: relative;
  padding: 0.75rem 0.9rem 0.4rem;
  border: 1px solid var(--app-border);
  border-radius: 0.7rem;
  background: var(--app-surface-2);
}

/* Tulisan OR di antara kotak grup. */
.cond-group + .cond-group {
  margin-top: 2.2rem;
}

.cond-group + .cond-group::before {
  content: "OR";
  position: absolute;
  top: -1.75rem;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.1rem 0.7rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--app-accent);
  background: var(--app-accent-soft);
}

.cond-group:last-child { margin-bottom: 0.75rem; }

.cond-group-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--app-muted);
}

/* Tulisan AND di antara baris dalam satu grup. */
.cond-row + .cond-row::before {
  content: "AND";
  display: block;
  margin: 0.15rem 0 0.35rem;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--app-muted);
}

.cond-fields {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 5.5rem 7rem auto;
  gap: 0.5rem;
  align-items: center;
}

@media (max-width: 575.98px) {
  .cond-fields { grid-template-columns: minmax(0, 1fr) 4.5rem 5.5rem auto; }
}

/* Tabel "apa yang dilihat mesin" di halaman detail aturan. */
.truth { font-weight: 600; }
.truth-true { color: #0b8a0b; }
.truth-false { color: var(--app-muted); }
.truth-unknown { color: #a15c00; }
[data-bs-theme="dark"] .truth-true { color: #4cc94c; }
[data-bs-theme="dark"] .truth-unknown { color: #f2b544; }

/* Tautan ke halaman variabel/device di halaman aturan: tidak bergaris, tetap terlihat. */
.var-link {
  text-decoration: none;
  border-bottom: 1px dotted currentColor;
}

.var-link:hover { border-bottom-style: solid; }

.expr-op {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--app-muted);
}

/* ---------- Rule chain / Flow (tahap 8) ---------- */
/* Warna kategori node: filter (ungu), enrichment (biru), transformation (teal), action (oranye). */
:root, [data-bs-theme="light"] {
  --flow-filter: #7c4dff;
  --flow-enrichment: #2a78d6;
  --flow-transformation: #0f9488;
  --flow-action: #e4572e;
  --flow-true: #0b8a0b;
  --flow-false: #8a94a6;
  --flow-success: #2a78d6;
  --flow-failure: #c62828;
}
[data-bs-theme="dark"] {
  --flow-filter: #a283ff;
  --flow-enrichment: #5aa0f0;
  --flow-transformation: #2cc4b5;
  --flow-action: #ff7a52;
  --flow-true: #4cc94c;
  --flow-false: #8a94a6;
  --flow-success: #5aa0f0;
  --flow-failure: #ff6b6b;
}

.rule-state-listening {
  color: var(--app-accent);
  background: var(--app-accent-soft);
  border-color: rgba(42, 120, 214, 0.35);
}

.flow-diagram-wrap { overflow-x: auto; padding-bottom: 0.25rem; }
.flow-diagram { display: block; font-family: inherit; }
.flow-diagram text { fill: var(--bs-body-color); }
.flow-node rect { fill: var(--app-surface); stroke: var(--app-border); stroke-width: 1.5; }
.flow-node .flow-node-stripe { stroke: none; }
.flow-node-filter .flow-node-stripe { fill: var(--flow-filter); }
.flow-node-enrichment .flow-node-stripe { fill: var(--flow-enrichment); }
.flow-node-transformation .flow-node-stripe { fill: var(--flow-transformation); }
.flow-node-action .flow-node-stripe { fill: var(--flow-action); }
.flow-node-input rect { fill: var(--app-surface-2); }
.flow-node.is-walked rect:first-of-type { stroke: var(--app-accent); stroke-width: 2.5; }
.flow-node.is-unreachable rect:first-of-type { stroke-dasharray: 5 4; }
.flow-node.is-unreachable text { opacity: 0.55; }
.flow-node-name { font-size: 13px; font-weight: 600; }
.flow-node-kind { font-size: 11px; fill: var(--app-muted) !important; }
.flow-node-summary { font-size: 11px; font-family: var(--bs-font-monospace); }
.flow-node-result { font-weight: 600; fill: var(--app-accent) !important; }

.flow-link { fill: none; stroke: var(--app-border); stroke-width: 2; }
.flow-arrowhead { fill: var(--app-muted); }
.flow-link.is-walked { stroke: var(--app-accent); stroke-width: 3; }
.flow-label rect { fill: var(--app-surface); stroke-width: 1.2; }
.flow-label text { font-size: 10.5px; font-weight: 600; }
.flow-label-true rect { stroke: var(--flow-true); } .flow-label-true text { fill: var(--flow-true); }
.flow-label-false rect { stroke: var(--flow-false); } .flow-label-false text { fill: var(--flow-false); }
.flow-label-success rect { stroke: var(--flow-success); } .flow-label-success text { fill: var(--flow-success); }
.flow-label-failure rect { stroke: var(--flow-failure); } .flow-label-failure text { fill: var(--flow-failure); }
.flow-label.is-walked rect { stroke-width: 2.4; }

.flow-legend { display: flex; flex-wrap: wrap; gap: 1rem; margin-top: 0.4rem; }
.flow-swatch { display: inline-block; width: 0.7rem; height: 0.7rem; border-radius: 2px; margin-right: 0.3rem; vertical-align: -0.05rem; }
.flow-swatch.flow-node-filter { background: var(--flow-filter); }
.flow-swatch.flow-node-enrichment { background: var(--flow-enrichment); }
.flow-swatch.flow-node-transformation { background: var(--flow-transformation); }
.flow-swatch.flow-node-action { background: var(--flow-action); }

/* Hasil node (True/False/Success/Failure) dan kategori node dalam teks. */
.flow-result { font-weight: 600; }
.flow-result-true { color: var(--flow-true); }
.flow-result-false { color: var(--flow-false); }
.flow-result-success { color: var(--flow-success); }
.flow-result-failure { color: var(--flow-failure); }
.flow-cat { font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; }
.flow-cat-filter { color: var(--flow-filter); }
.flow-cat-enrichment { color: var(--flow-enrichment); }
.flow-cat-transformation { color: var(--flow-transformation); }
.flow-cat-action { color: var(--flow-action); }

/* Form rule chain */
.node-row { border-left: 3px solid var(--app-border); }
.link-head, .link-row {
  display: grid;
  grid-template-columns: minmax(7rem, 1fr) 7.5rem 1.2rem minmax(7rem, 1fr) 2rem;
  gap: 0.5rem;
  align-items: center;
}
.link-row { margin-bottom: 0.4rem; }
.link-arrow { color: var(--app-muted); text-align: center; }
.flow-trace td { vertical-align: top; }

/* ---------- Firmware OTA (tahap 9) ---------- */
.ota-status { font-weight: 600; border: 1px solid transparent; }
.ota-status-queued, .ota-status-canceled { color: var(--app-muted); background: var(--app-surface-2); border-color: var(--app-border); }
.ota-status-downloading, .ota-status-downloaded, .ota-status-verified, .ota-status-updating {
  color: var(--app-accent); background: var(--app-accent-soft); border-color: rgba(42, 120, 214, 0.35);
}
.ota-status-updated { color: #0b8a0b; background: rgba(12, 163, 12, 0.1); border-color: rgba(12, 163, 12, 0.35); }
.ota-status-failed { color: #fff; background: #c62828; }
[data-bs-theme="dark"] .ota-status-updated { color: #4cc94c; }

/* Langkah pembaruan: Queued › Downloading › ... › Updated */
.ota-steps { display: flex; flex-wrap: wrap; gap: 0.4rem 1.1rem; list-style: none; padding: 0; margin: 0 0 0.75rem; }
.ota-step {
  position: relative;
  font-size: 0.78rem; padding: 0.2rem 0.6rem; border-radius: 999px;
  border: 1px solid var(--app-border); color: var(--app-muted); background: var(--app-surface);
}
/* Tanda › di ANTARA dua langkah, di luar kotaknya. */
.ota-step + .ota-step::before {
  content: "›"; position: absolute; left: -0.8rem; top: 50%; transform: translateY(-55%);
  color: var(--app-muted); font-weight: 400;
}
.ota-step.is-done { color: #0b8a0b; border-color: rgba(12, 163, 12, 0.35); background: rgba(12, 163, 12, 0.08); }
.ota-step.is-current { color: var(--app-accent); border-color: var(--app-accent); background: var(--app-accent-soft); font-weight: 600; }
.ota-step.is-failed { color: #c62828; border-color: rgba(198, 40, 40, 0.5); background: rgba(198, 40, 40, 0.08); font-weight: 600; }
[data-bs-theme="dark"] .ota-step.is-done { color: #4cc94c; }
[data-bs-theme="dark"] .ota-step.is-failed { color: #ff6b6b; }
.ota-progress { height: 1.1rem; }
.ota-timeline li + li { margin-top: 0.45rem; }

/* Kartu JSON di halaman aturan ECA (tahap 13). */
.rule-json > summary {
  cursor: pointer;
  list-style: none;
}

.rule-json > summary::-webkit-details-marker {
  display: none;
}

.rule-json-title::before {
  content: "▸";
  margin-right: 0.35rem;
}

.rule-json[open] .rule-json-title::before {
  content: "▾";
}

.rule-json-code {
  max-height: 28rem;
  overflow: auto;
  padding: 0.75rem;
  border: 1px solid var(--app-border);
  border-radius: 0.5rem;
  background: var(--app-surface-2);
  font-size: 0.8rem;
}

/* ---------- Penampil JSON (static/js/json_viewer.js) ---------- */
:root,
[data-bs-theme="light"] {
  --jv-key: #8b1e1e;
  --jv-string: #1d4f91;
  --jv-number: #23784a;
  --jv-literal: #1e3a8a;
  --jv-guide: #d0d5dd;
}

[data-bs-theme="dark"] {
  --jv-key: #f2a7a0;
  --jv-string: #8ab4f8;
  --jv-number: #81c995;
  --jv-literal: #a5b4fc;
  --jv-guide: #3a404b;
}

.jv-toolbar {
  display: flex;
  gap: 1rem;
  margin-bottom: 0.35rem;
}

/* Kotak bergulir: tinggi maksimal, gulir vertikal di dalam kotak. */
.jv-view {
  max-height: 32rem;
  overflow: auto;
  padding: 0.5rem 0.75rem 0.5rem 0;
  border: 1px solid var(--app-border);
  border-radius: 0.5rem;
  background: var(--app-surface);
  font-family: var(--bs-font-monospace, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
  font-size: 0.82rem;
  line-height: 1.6;
}

/* Satu baris: nomor | tombol lipat | garis kedalaman | isi.
   Isi yang panjang dibungkus di kolomnya sendiri, rata dengan awal isi. */
.jv-line {
  display: flex;
  align-items: stretch;
}

.jv-line:hover {
  background: var(--app-surface-2);
}

.jv-num {
  flex: 0 0 3.25rem;
  padding-right: 0.75rem;
  text-align: right;
  color: var(--app-muted);
  user-select: none;
}

.jv-fold {
  flex: 0 0 1.1rem;
  user-select: none;
}

.jv-guides {
  display: flex;
  flex: 0 0 auto;
}

/* Garis tegak per tingkat kedalaman, seperti di editor kode. */
.jv-guide {
  width: 1.75ch;
  margin-left: 0.25ch;
  border-left: 1px solid var(--jv-guide);
}

.jv-code {
  flex: 1 1 auto;
  min-width: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.jv-key { color: var(--jv-key); }
.jv-string { color: var(--jv-string); }
.jv-number { color: var(--jv-number); }
.jv-literal { color: var(--jv-literal); font-weight: 700; }

.jv-toggle {
  width: 1.1rem;
  padding: 0;
  border: 0;
  background: none;
  color: var(--app-muted);
  line-height: inherit;
  cursor: pointer;
}

.jv-toggle::before { content: "▾"; }
.jv-collapsed > .jv-line .jv-toggle::before { content: "▸"; }
.jv-toggle:hover { color: var(--bs-body-color); }

/* Terlipat: anak-anak dan baris penutup disembunyikan, ringkasan "…} 5 keys" tampil. */
.jv-summary { display: none; }
.jv-collapsed > .jv-line .jv-summary { display: inline; }
.jv-collapsed > .jv-children,
.jv-collapsed > .jv-close { display: none; }

.jv-count {
  padding: 0 0.35rem;
  border-radius: 0.5rem;
  background: var(--app-surface-2);
  color: var(--app-muted);
  font-family: var(--bs-body-font-family);
  font-size: 0.72rem;
}

/* ---------- Editor JSON di halaman impor (static/js/json_editor.js) ---------- */
.je-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.je-status {
  margin-left: auto;
}

/* Kotak editor: gutter nomor baris + area teks, tinggi tetap, gulir di dalam textarea. */
.je-edit {
  display: flex;
  height: 28rem;
  border: 1px solid var(--app-border);
  border-radius: 0.5rem;
  background: var(--app-surface);
  /* clip (bukan hidden): kotak ini tidak boleh ikut tergulir saat browser
     mengejar posisi kursor; yang menggulir hanya textarea. */
  overflow: clip;
}

.je-edit[hidden] {
  display: none;
}

.je-edit:focus-within {
  border-color: var(--bs-primary);
  box-shadow: 0 0 0 0.2rem rgba(var(--bs-primary-rgb, 13, 110, 253), 0.15);
}

.je-edit.je-drop {
  border-style: dashed;
  border-color: var(--bs-primary);
}

/* Font, ukuran, tinggi baris, dan padding HARUS sama untuk gutter, <pre>, dan
   textarea, supaya teks berwarna tepat berada di bawah teks textarea. */
.je-gutter,
.je-highlight,
.je-input {
  margin: 0;
  padding: 0.5rem 0.75rem;
  font-family: var(--bs-font-monospace, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
  font-size: 0.82rem;
  line-height: 1.6;
  tab-size: 2;
}

.je-gutter {
  flex: 0 0 auto;
  min-width: 3.25rem;
  overflow: hidden;
  border-right: 1px solid var(--app-border);
  background: var(--app-surface-2);
  color: var(--app-muted);
  text-align: right;
  user-select: none;
}

.je-error-line {
  color: var(--bs-danger);
  font-weight: 700;
}

.je-area {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  overflow: clip;
}

.je-highlight,
.je-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  white-space: pre;
  overflow: auto;
  border: 0;
}

.je-highlight {
  overflow: hidden;
  color: var(--bs-body-color);
  pointer-events: none;
}

/* Teks textarea transparan; yang terlihat adalah <pre> berwarna di belakangnya. */
.je-input {
  resize: none;
  outline: none;
  background: transparent;
  color: transparent;
  caret-color: var(--bs-body-color);
}

.je-input::placeholder {
  color: var(--app-muted);
}

.je-input::selection {
  background: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.25);
  color: transparent;
}

.je-preview-empty {
  padding: 1rem;
  border: 1px solid var(--app-border);
  border-radius: 0.5rem;
}

/* Tombol Format di editor JSON: ikon { } + teks, keterangan singkat di sampingnya. */
.je-format {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.je-format:disabled {
  cursor: not-allowed;
}

.je-hint {
  transition: color 0.2s;
}

/* Log aktivitas (tahap 18): level selalu ditulis sebagai teks, warna hanya penegas. */
.activity-level { font-weight: 600; border: 1px solid transparent; }
.activity-level-info { color: var(--app-muted); background: var(--app-surface-2); border-color: var(--app-border); }
.activity-level-warning { color: #8a5300; background: rgba(240, 160, 0, 0.14); border-color: rgba(240, 160, 0, 0.4); }
.activity-level-error { color: #b42318; background: rgba(220, 38, 38, 0.1); border-color: rgba(220, 38, 38, 0.35); }
[data-bs-theme="dark"] .activity-level-warning { color: #f2b544; }
[data-bs-theme="dark"] .activity-level-error { color: #ff8a80; }
.rule-search { flex: 1 1 18rem; max-width: 32rem; }

/* ---------- State machine (tahap 20) ---------- */
/* Warna keadaan (tone) dan warna pemicu transisi. */
:root, [data-bs-theme="light"] {
  --sm-neutral: #8a94a6;
  --sm-ok: #0b8a0b;
  --sm-info: #2a78d6;
  --sm-warning: #d48a00;
  --sm-danger: #c62828;
  --sm-condition: #2a78d6;
  --sm-timeout: #7c4dff;
  --sm-silent: #d48a00;
  --sm-manual: #0f9488;
}
[data-bs-theme="dark"] {
  --sm-neutral: #a2abbb;
  --sm-ok: #4cc94c;
  --sm-info: #5aa0f0;
  --sm-warning: #f2b544;
  --sm-danger: #ff6b6b;
  --sm-condition: #5aa0f0;
  --sm-timeout: #a283ff;
  --sm-silent: #f2b544;
  --sm-manual: #2cc4b5;
}
.machine-state rect { fill: var(--app-surface); stroke-width: 2; }
.machine-tone-neutral rect { stroke: var(--sm-neutral); }
.machine-tone-ok rect { stroke: var(--sm-ok); }
.machine-tone-info rect { stroke: var(--sm-info); }
.machine-tone-warning rect { stroke: var(--sm-warning); }
.machine-tone-danger rect { stroke: var(--sm-danger); }
.machine-state.is-occupied rect { stroke-width: 4; fill: var(--app-surface-2); }
.machine-start { fill: var(--bs-body-color); }
.machine-link { fill: none; stroke: var(--app-border); stroke-width: 2; }
.machine-link-condition { stroke: var(--sm-condition); }
.machine-link-timeout { stroke: var(--sm-timeout); stroke-dasharray: 6 4; }
.machine-link-silent, .machine-link-offline { stroke: var(--sm-silent); stroke-dasharray: 2 4; }
.machine-link-manual { stroke: var(--sm-manual); }
.machine-label rect { fill: var(--app-surface); stroke: var(--app-border); stroke-width: 1.2; }
.machine-label text { font-size: 10.5px; font-weight: 600; }
.machine-label-condition text { fill: var(--sm-condition) !important; }
.machine-label-timeout text { fill: var(--sm-timeout) !important; }
.machine-label-silent text, .machine-label-offline text { fill: var(--sm-silent) !important; }
.machine-label-manual text { fill: var(--sm-manual) !important; }
.machine-swatch { display: inline-block; width: 0.9rem; height: 0.2rem; border-radius: 2px; margin-right: 0.3rem; vertical-align: 0.2rem; }
.machine-trigger-condition { background: var(--sm-condition); }
.machine-trigger-timeout { background: var(--sm-timeout); }
.machine-trigger-silent { background: var(--sm-silent); }
.machine-trigger-manual { background: var(--sm-manual); }
.state-badge { border: 1px solid currentColor; background: transparent; font-weight: 600; }
.state-badge-neutral { color: var(--sm-neutral); }
.state-badge-ok { color: var(--sm-ok); }
.state-badge-info { color: var(--sm-info); }
.state-badge-warning { color: var(--sm-warning); }
.state-badge-danger { color: var(--sm-danger); }
.state-row { display: grid; grid-template-columns: minmax(8rem, 1fr) minmax(10rem, 2fr) minmax(7rem, 1fr) auto auto; gap: 0.5rem; align-items: end; margin-bottom: 0.5rem; }
.transition-head, .transition-row { display: grid; grid-template-columns: minmax(14rem, 2fr) minmax(14rem, 1.4fr) minmax(14rem, 3fr) auto; gap: 0.5rem; align-items: start; }
.transition-row { margin-bottom: 0.5rem; padding-bottom: 0.5rem; border-bottom: 1px dashed var(--app-border); }
.transition-ends { display: flex; align-items: center; gap: 0.35rem; }
.transition-detail { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.transition-detail > input { flex: 1 1 14rem; }
.transition-seconds { width: 11rem; }
@media (max-width: 767.98px) {
  .state-row, .transition-head, .transition-row { grid-template-columns: 1fr; }
  .transition-head { display: none; }
}
