/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --green-dark:   #1a6b3a;
  --green-mid:    #2d9e5f;
  --green-light:  #4ecb82;
  --green-pale:   #d4f5e3;
  --soil:         #8b5e3c;
  --soil-light:   #c4936a;
  --water:        #2d7fc1;
  --water-dark:   #1a5a8f;
  --water-light:  #6db8f0;
  --amber:        #e8a020;
  --amber-light:  #f5c842;
  --red:          #d94040;
  --bg:           #f0f7f3;
  --surface:      #ffffff;
  --surface2:     #f6fbf8;
  --border:       #d6eadd;
  --text-primary: #1a2e22;
  --text-sec:     #5a7a66;
  --text-mute:    #94b0a0;
  --radius:       16px;
  --shadow:       0 2px 12px rgba(0,0,0,0.07);
  --trans:        160ms ease;
}

html { font-size: 16px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom, 16px);
}

/* ============================================================
   HEADER
   ============================================================ */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--green-dark);
  position: sticky;
  top: 0;
  z-index: 10;
  transition: background 0.4s ease;
}

/* Header biru saat menyiram */
header.watering {
  background: var(--water-dark);
}

.brand { display: flex; align-items: center; gap: 8px; }
.brand-icon { font-size: 20px; }
.brand-name {
  font-size: 16px;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.3px;
  margin: 0;
}

.status-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255,255,255,0.12);
  border-radius: 99px;
  padding: 5px 12px;
}
.status-pill .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--text-mute);
  transition: background var(--trans);
}

/* Dot hijau saat online */
.status-pill.online .dot {
  background: var(--green-light);
  box-shadow: 0 0 6px var(--green-light);
}

/* Dot biru animasi saat menyiram */
.status-pill.watering .dot {
  background: var(--water-light);
  box-shadow: 0 0 6px var(--water-light);
  animation: blink-water 1s infinite;
}

.status-pill.offline .dot { background: var(--red); }

@keyframes blink-water {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.3; }
}

#statusText {
  font-size: 12px;
  font-weight: 600;
  color: rgba(255,255,255,0.85);
}

/* ============================================================
   LAYOUT
   ============================================================ */
main {
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 640px;
  margin: 0 auto;
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.controls-col {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ============================================================
   CARD BASE
   ============================================================ */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.card-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-mute);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: 6px;
}

/* ============================================================
   METRIC CARDS
   ============================================================ */
.card-metric { text-align: left; }

.card-value {
  font-size: 28px;
  font-weight: 800;
  color: var(--text-primary);
  line-height: 1;
  letter-spacing: -1px;
  transition: color var(--trans);
}

.card-unit {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-sec);
  margin-top: 2px;
  margin-bottom: 8px;
}

.bar-track {
  height: 4px;
  background: var(--surface2);
  border-radius: 99px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.bar-fill {
  height: 100%;
  border-radius: 99px;
  transition: width 0.6s cubic-bezier(0.4,0,0.2,1);
}

.bar-temp  { background: linear-gradient(90deg, var(--amber), var(--red)); }
.bar-humid { background: linear-gradient(90deg, var(--water), var(--water-light)); }
.bar-soil  { background: linear-gradient(90deg, var(--soil), var(--soil-light)); }

@media (max-width: 380px) {
  .grid-3 { grid-template-columns: 1fr; }
  .card-value { font-size: 24px; }
}

/* ============================================================
   GAUGE (Tank)
   ============================================================ */
.card-tank { display: flex; flex-direction: column; }

.gauge-wrap { display: flex; flex-direction: column; align-items: center; margin-top: 4px; }

.gauge-svg { width: 100%; max-width: 130px; overflow: visible; }

.gauge-track { stroke: var(--surface2); }
.gauge-fill  {
  stroke: var(--water);
  transition: stroke-dashoffset 0.7s cubic-bezier(0.4,0,0.2,1), stroke 0.4s;
}

.gauge-text {
  font-size: 14px;
  font-weight: 800;
  fill: var(--text-primary);
  font-family: -apple-system, sans-serif;
}

.gauge-labels {
  display: flex;
  justify-content: space-between;
  width: 90%;
  font-size: 10px;
  color: var(--text-mute);
  font-weight: 600;
  margin-top: -4px;
}

/* ============================================================
   MODE TOGGLE
   ============================================================ */
.card-mode { height: auto; }

.mode-toggle {
  display: flex;
  background: var(--surface2);
  border-radius: 10px;
  padding: 3px;
  gap: 3px;
  border: 1px solid var(--border);
}

.mode-btn {
  flex: 1;
  border: none;
  background: transparent;
  border-radius: 8px;
  padding: 8px 6px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-sec);
  cursor: pointer;
  transition: background var(--trans), color var(--trans), box-shadow var(--trans);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

#btnAuto.active   { background: var(--green-mid); color: #fff; box-shadow: 0 2px 6px rgba(45,158,95,0.35); }
#btnManual.active { background: var(--amber); color: #1a2e22; box-shadow: 0 2px 6px rgba(232,160,32,0.35); }

/* ============================================================
   SIRAM BUTTON
   ============================================================ */
.card-siram { flex: 1; display: flex; flex-direction: column; }

.btn-siram {
  flex: 1;
  width: 100%;
  border: none;
  border-radius: 10px;
  padding: 0 8px;
  min-height: 52px;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.5px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--green-light);
  color: var(--green-dark);
  box-shadow: 0 3px 10px rgba(78,203,130,0.4);
  transition: background var(--trans), transform 80ms, box-shadow var(--trans);
  -webkit-tap-highlight-color: transparent;

  /* Hold-to-water: cegah teks terseleksi & scroll saat tahan */
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.btn-siram:active:not(:disabled) {
  transform: scale(0.96);
  background: var(--water);
  color: #fff;
  box-shadow: 0 3px 20px rgba(45,127,193,0.6);
  animation: none;
}

.btn-siram.pumping {
  background: var(--water);
  color: #fff;
  box-shadow: 0 3px 10px rgba(45,127,193,0.45);
  animation: pulse-pump 1.4s infinite;
}

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

@keyframes pulse-pump {
  0%, 100% { box-shadow: 0 3px 10px rgba(45,127,193,0.45); }
  50%       { box-shadow: 0 3px 20px rgba(45,127,193,0.75); }
}

/* ============================================================
   LED INDICATORS
   ============================================================ */
.leds-row {
  display: flex;
  gap: 20px;
  margin-top: 6px;
}

.led-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-sec);
}

.led {
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--surface2);
  border: 2px solid var(--border);
  transition: background var(--trans), box-shadow var(--trans);
}

.led.on.led-blue  { background: #4db8f0; border-color: #2d7fc1; box-shadow: 0 0 8px #4db8f0; }
.led.on.led-green { background: var(--green-light); border-color: var(--green-mid); box-shadow: 0 0 8px var(--green-light); }

/* ============================================================
   OFFLINE OVERLAY
   ============================================================ */
.offline-overlay {
  position: fixed; inset: 0;
  background: rgba(26,46,34,0.75);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.offline-box {
  background: var(--surface);
  border-radius: 20px;
  padding: 32px 28px;
  text-align: center;
  box-shadow: 0 8px 40px rgba(0,0,0,0.2);
  max-width: 280px;
  width: 90%;
}

.offline-icon  { font-size: 40px; margin-bottom: 12px; }
.offline-title { font-size: 18px; font-weight: 800; color: var(--text-primary); margin-bottom: 6px; }
.offline-sub   { font-size: 13px; color: var(--text-sec); margin-bottom: 20px; }

.offline-spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--green-mid);
  border-radius: 50%;
  margin: 0 auto;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ============================================================
   ALERT CARD
   ============================================================ */
.card-alert {
  background: #fdf2f2;
  border: 1px solid #f8b4b4;
  border-left: 5px solid var(--red);
  padding: 16px;
  animation: slide-in 0.3s ease;
}

.alert-content { display: flex; align-items: flex-start; gap: 12px; }
.alert-icon    { font-size: 24px; line-height: 1; }
.alert-text    { display: flex; flex-direction: column; gap: 4px; }

.alert-title {
  font-size: 14px;
  font-weight: 800;
  color: var(--red);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.alert-sub {
  font-size: 13px;
  color: var(--text-sec);
  line-height: 1.4;
}

@keyframes slide-in {
  from { opacity: 0; transform: translateY(-10px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 400px) {
  .grid-2    { grid-template-columns: 1fr; }
  .card-value { font-size: 26px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0ms !important; animation: none !important; }
}

/* ============================================================
   DARK MODE
   ============================================================ */
@media (prefers-color-scheme: dark) {
  :root {
    --bg:           #0a0f0d;
    --surface:      #121b16;
    --surface2:     #17231c;
    --border:       #1e3025;
    --text-primary: #ecf3ee;
    --text-sec:     #9bbfa9;
    --text-mute:    #658572;
    --shadow:       0 4px 20px rgba(0,0,0,0.4);
  }

  .card-alert {
    background: #2b1a1a;
    border-color: #5c2c2c;
  }
}