/* === BASE LAYOUT === */
* {
  box-sizing: border-box;
  font-family: Inter, system-ui, Arial, sans-serif;
}

:root {
  --bg: #f7fbff;
  --accent: #0b7285;
  --card: #fff;
  --muted: #cbd5e1;
}

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: #0f172a;
}

/* === DARK MODE THEME === */
body.dark {
  background: #0f172a;
  color: #e2e8f0;
}

body.dark .topbar {
  background: #1e293b;
  color: #e2e8f0;
  border-bottom: 1px solid #334155;
}

body.dark .toolbar button,
body.dark select {
  background: #334155;
  color: #e2e8f0;
  border: 1px solid #475569;
}

body.dark .toolbar button:hover {
  background: #2563eb;
  color: #fff;
}

body.dark .palette {
  background: #1e293b;
  color: #e2e8f0;
}

body.dark .stage {
  background: #0f172a;
}

body.dark .node {
  background: #1e293b;
  border: 1px solid #334155;
  color: #e2e8f0;
}

/* Popup & Modal (jadi putih di dark mode) */
body.dark .modal-box,
body.dark #mobileWarning .modal-box,
body.dark #desktopWelcome .modal-box,
body.dark #helpModal .modal-box {
  background: #1e293b;
  color: #fff;
}

body.dark .modal-box input,
body.dark .modal-box label {
  color: #fff;
}

body.dark .footer-credit .dev,
body.dark .credit .dev {
  color: #93c5fd;
}

body.dark .footer-credit .powered,
body.dark .credit .powered {
  color: #94a3b8;
}

body.dark button {
  background: linear-gradient(90deg, #2563eb, #1e40af);
  color: white;
}

/* === TOPBAR === */
.topbar {
  background: var(--accent);
  color: white;
  padding: 10px 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid #0b7285;
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 700;
}

/* === TOOLBAR & BUTTONS === */
.toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.toolbar button,
.toolbar select {
  margin-left: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  border: none;
  background: #fff;
  color: #0b7285;
  cursor: pointer;
  font-weight: 600;
  transition: 0.25s;
}

.toolbar button:hover {
  background: #0b7285;
  color: white;
}

/* === DARKMODE TOGGLE === */
.dark-toggle {
  background: #fff;
  color: #0b7285;
  border: none;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  transition: 0.2s ease;
}

.dark-toggle:hover {
  background: #0b7285;
  color: #fff;
}

body.dark .dark-toggle {
  background: #334155;
  color: #e2e8f0;
}

body.dark .dark-toggle:hover {
  background: #2563eb;
  color: #fff;
}

/* === LAYOUT UTAMA === */
.canvas-wrap {
  display: flex;
  flex: 1;
  height: calc(100vh - 56px);
  overflow: hidden;
}

.palette {
  width: 180px;
  background: #f1f5f9;
  padding: 12px;
  border-right: 1px solid #e6eef6;
  overflow: auto;
}

.pal-title {
  font-weight: 700;
  margin: 0 0 8px 0;
  text-align: center;
}

.pal-item {
  background: var(--card);
  padding: 8px;
  margin-bottom: 8px;
  border: 1px solid #e6eef6;
  border-radius: 8px;
  cursor: grab;
  text-align: center;
  transition: background 0.2s ease;
}

.pal-item:hover {
  background: #dbeafe;
}

/* === STAGE === */
.stage {
  flex: 1;
  position: relative;
  background: linear-gradient(180deg, #ffffff, #f2f9ff);
  overflow: auto;
}

body.dark .stage {
  background: #0f172a;
}

.wires-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* === NODE === */
.node {
  position: absolute;
  min-width: 140px;
  background: var(--card);
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(2, 6, 23, 0.08);
  border: 1px solid #e9eef6;
  padding: 8px;
}

.node-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.node-title {
  font-weight: 700;
  user-select: none;
}

.btn-del {
  background: #ff6b6b;
  border: none;
  color: white;
  border-radius: 6px;
  padding: 2px 6px;
  cursor: pointer;
  transition: 0.25s;
}

.btn-del:hover {
  background: #dc2626;
}

/* === PIN === */
.pins {
  display: flex;
  gap: 8px;
  align-items: center;
}

.pins .pin {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #e6eef6;
  border: 2px solid #cbd5e1;
  cursor: pointer;
  display: inline-block;
}

.pins .pin:hover {
  box-shadow: 0 0 10px #2563eb;
}

.in-pins {
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  margin-top: 4px;
}

.out-pins {
  justify-content: flex-end;
}

.node.dragging {
  opacity: 0.9;
  transform: scale(1.01);
}

/* === SWITCH INPUT === */
.pin-switch {
  width: 32px;
  height: 18px;
  border-radius: 10px;
  background: #e6eef6;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #334155;
  font-weight: 700;
  cursor: pointer;
  border: 2px solid #dbeafe;
}

/* === LAMPU OUTPUT === */
.lamp {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #ef4444;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
  display: inline-block;
  transition: background 0.25s ease, box-shadow 0.25s ease;
}

.lamp.on {
  background: #16a34a;
  box-shadow: 0 6px 18px rgba(22, 163, 74, 0.35);
}

/* === BLINK LAMP (Pulse) === */
.lamp.blink {
  animation: blinkLamp 1s infinite;
}

@keyframes blinkLamp {
  0%, 100% {
    background: #22c55e;
    box-shadow: 0 0 10px 3px rgba(34, 197, 94, 0.7);
  }
  50% {
    background: #15803d;
    box-shadow: 0 0 3px rgba(34, 197, 94, 0.2);
  }
}

/* === TIMER CONFIG === */
.timer-config {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 4px;
  position: relative;
  z-index: 10;
  background: rgba(255, 255, 255, 0.9);
  padding: 2px 4px;
  border-radius: 4px;
  pointer-events: auto;
}

.timer-config label,
.pulse-config label {
  font-size: 12px;
  color: #334155;
  font-weight: 600;
  white-space: nowrap;
}

body.dark .timer-config label,
body.dark .pulse-config label {
  color: #fff;
}

.timer-config input {
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  padding: 2px 4px;
  font-size: 12px;
  pointer-events: auto;
  background: #f8fafc;
  color: #0f172a;
}

body.dark .timer-config {
  background: rgba(30, 41, 59, 0.95);
}

body.dark .timer-config input {
  background: #334155;
  color: #fff;
  border: 1px solid #475569;
}

/* === TIMER PROGRESS BAR === */
.timer-progress {
  position: absolute;
  bottom: 2px;
  left: 2px;
  height: 5px;
  background: #22c55e;
  width: 0%;
  border-radius: 2px;
  transition: width 0.1s linear;
  pointer-events: none;
}

/* === PULSE CONFIG === */
.node[data-type="PULSE"] {
  border: 2px dashed #facc15;
  background: #fff9c2;
  box-shadow: 0 0 8px rgba(250, 204, 21, 0.3);
  position: relative;
  padding-bottom: 28px;
  overflow: visible;
}

.pulse-config {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 6px;
  margin-top: 6px;
  padding: 4px 6px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 6px;
  z-index: 10;
}

body.dark .pulse-config {
  background: rgba(30, 41, 59, 0.95);
}

.pulse-config input {
  width: 70px;
  font-size: 13px;
  padding: 3px 5px;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  background: #fff;
  color: #0f172a;
}

body.dark .pulse-config input {
  background: #334155;
  color: #fff;
  border: 1px solid #475569;
}

.node[data-type="PULSE"] .out-pins {
  margin-top: 10px;
  justify-content: flex-end;
}

/* === NODE KHUSUS === */
.node.PULSE { background:#fff9c2; border-color:#facc15; }
.node.COUNTER_UP, .node.COUNTER_DOWN { background:#ffe5d0; border-color:#e26d19; }
.node.TIMER_ON_DELAY, .node.TIMER_OFF_DELAY { background:#dbeafe; border-color:#2684ff; }

body.dark .node.PULSE { background:#453d00; border-color:#facc15; color:#fff; }
body.dark .node.COUNTER_UP, body.dark .node.COUNTER_DOWN { background:#4a2b00; border-color:#f97316; color:#fff; }
body.dark .node.TIMER_ON_DELAY, body.dark .node.TIMER_OFF_DELAY { background:#1e3a8a; border-color:#60a5fa; color:#fff; }

/* === RESPONSIVE === */
@media (max-width: 800px) {
  .palette {
    display: none;
  }
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  padding: 10px;
}

header button,
header select {
  margin: 5px 0;
  width: 100%;
  max-width: 150px;
}

@media (min-width: 600px) {
  header button,
  header select {
    width: auto;
  }
}

#wires {
  pointer-events: none;
}

/* === FIX TAMBAHAN DARK MODE === */

/* 1. Menu palette biar kontras di dark mode */
body.dark .palette .pal-item {
  background: #334155;
  color: #ffffff;
  border: 1px solid #475569;
}

body.dark .palette .pal-item:hover {
  background: #2563eb;
  color: #ffffff;
}

/* 2. Popup teks tetap putih */
body.dark .modal-box h1,
body.dark .modal-box h2,
body.dark .modal-box h3,
body.dark .modal-box p,
body.dark .modal-box span {
  color: #ffffff !important;
}

/* 3. Fix label "Preset" di Counter */
body.dark .node.COUNTER_UP label,
body.dark .node.COUNTER_DOWN label {
  color: #ffffff !important;
}

body.dark .node.COUNTER_UP input,
body.dark .node.COUNTER_DOWN input {
  color: #ffffff !important;
  background: #334155 !important;
  border: 1px solid #475569 !important;
}

body.dark .node.COUNTER_UP,
body.dark .node.COUNTER_DOWN {
  color: #ffffff !important;
}
