/* ── Design tokens ───────────────────────────────────────────────────────── */
:root {
  --bg:          #0d1411;
  --bg-alt:      #10181a;
  --surface:     #161f1c;
  --surface-hi:  #1d2825;
  --surface-hov: #243330;
  --border:      #263432;
  --border-hi:   #334641;
  --text:        #e8efe8;
  --text-dim:    #8ea098;
  --text-muted:  #5f736d;

  --leaf:        #7cd992;
  --leaf-deep:   #2f7a45;
  --sun:         #ffd166;
  --earth:       #d19a6a;
  --water:       #6bcff0;
  --heat:        #ff8a4c;

  --ok:          #7cd992;
  --warn:        #ffb86b;
  --danger:      #ff6b6b;

  --sidebar-w: 256px;
  --radius:    10px;
  --radius-sm:  6px;
  --transition: 0.2s ease;

  /* Gauge ring geometry (circumference of r=62 circle) */
  --circ: 389.56;
}

/* ── Reset ───────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  overflow: hidden;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; border: none; background: none; }
input, select { font-family: inherit; }

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-alt);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow-y: auto;
  padding-bottom: 16px;
  border-right: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 22px 18px 20px;
}
.brand-icon { font-size: 26px; }
.brand-title { font-size: 14px; font-weight: 700; color: var(--text); letter-spacing: .05em; }
.brand-sub   { font-size: 11px; color: var(--text-dim); margin-top: 2px; }

.divider { height: 1px; background: var(--border); margin: 0 18px; }

/* Connection card */
.connection-card {
  background: var(--surface);
  border-radius: var(--radius-sm);
  margin: 14px;
  padding: 14px;
}
.conn-row { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.conn-label  { font-size: 12px; font-weight: 600; }
.conn-broker { font-size: 10px; color: var(--text-dim); margin-bottom: 2px; }
.conn-last   { font-size: 10px; color: var(--text-muted); }

/* Status dot */
.status-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
  box-shadow: 0 0 0 0 var(--text-muted);
  transition: background var(--transition);
}
.status-dot.connected    { background: var(--ok);   animation: pulse-ok   2s infinite; }
.status-dot.connecting   { background: var(--warn); animation: pulse-warn 1s infinite; }
.status-dot.disconnected { background: var(--text-muted); animation: none; }
.status-dot.error        { background: var(--danger); animation: pulse-err 0.6s infinite; }

@keyframes pulse-ok   { 0%,100%{box-shadow:0 0 0 0 rgba(124,217,146,.4)}  50%{box-shadow:0 0 0 6px transparent} }
@keyframes pulse-warn { 0%,100%{box-shadow:0 0 0 0 rgba(255,184,107,.4)}  50%{box-shadow:0 0 0 6px transparent} }
@keyframes pulse-err  { 0%,100%{box-shadow:0 0 0 0 rgba(255,107,107,.4)}  50%{box-shadow:0 0 0 6px transparent} }

/* Nav */
.nav-label {
  font-size: 9px; font-weight: 700; color: var(--text-muted);
  letter-spacing: .08em; padding: 14px 22px 6px;
}
.nav { display: flex; flex-direction: column; gap: 2px; padding: 0 10px; }
.nav-btn {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 11px 14px;
  font-size: 12px; font-weight: 600;
  color: var(--text-dim);
  border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
  text-align: left;
}
.nav-icon { font-size: 15px; }
.nav-btn:hover  { background: var(--surface); color: var(--text); }
.nav-btn.active { background: var(--surface-hi); color: var(--text); }
.nav-btn.active .nav-icon { color: var(--leaf); }

/* LED section */
.led-section { padding: 14px 18px 0; }
.led-label { font-size: 9px; font-weight: 700; color: var(--text-muted); letter-spacing: .08em; margin-bottom: 8px; }
.led-row { display: flex; align-items: center; gap: 8px; }
.led { width: 12px; height: 12px; border-radius: 50%; background: var(--text-muted); transition: background .3s; }
.led.on-ok   { background: var(--ok);    box-shadow: 0 0 8px var(--ok); }
.led.on-err  { background: var(--danger); box-shadow: 0 0 8px var(--danger); }
.led-text { font-size: 10px; color: var(--text-dim); margin-right: 12px; }

.sidebar-footer { padding: 16px 18px 0; font-size: 9px; color: var(--text-muted); line-height: 1.8; }

/* ── Main layout ─────────────────────────────────────────────────────────── */
.main {
  flex: 1;
  height: 100vh;
  overflow-y: auto;
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 0;
}

/* ── Pages ───────────────────────────────────────────────────────────────── */
.page { display: none; flex-direction: column; gap: 20px; }
.page.active { display: flex; }

.page-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  margin-bottom: 4px;
}
.page-title { font-size: 26px; font-weight: 700; line-height: 1.2; }
.page-sub   { font-size: 11px; color: var(--text-dim); margin-top: 4px; }

.history-range { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.range-label { font-size: 11px; color: var(--text-dim); }
select {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 5px 10px; font-size: 11px; outline: none;
  cursor: pointer;
}
select:focus { border-color: var(--leaf); }

/* ── Alert strip ─────────────────────────────────────────────────────────── */
#alertStrip { display: flex; flex-direction: column; gap: 6px; }
.alert-card {
  background: var(--surface); border-radius: var(--radius-sm);
  padding: 12px 18px; display: flex; align-items: center; gap: 12px;
  border: 1px solid var(--border);
}
.alert-card.ok     { border-color: var(--border); }
.alert-card.warn   { border-color: var(--warn); }
.alert-card.danger { border-color: var(--danger); }
.alert-icon  { font-size: 14px; }
.alert-title { font-size: 13px; font-weight: 600; }
.alert-detail{ font-size: 10px; color: var(--text-dim); margin-left: 8px; }

/* ── Gauge grid ──────────────────────────────────────────────────────────── */
.gauge-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.gauge-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px 16px 16px;
  display: flex; flex-direction: column; align-items: center;
  position: relative;
  transition: transform var(--transition), box-shadow var(--transition);
}
.gauge-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 32px rgba(0,0,0,.4);
}

.gauge-svg {
  width: 130px; height: 130px;
  transform: rotate(-225deg);   /* start arc at 7 o'clock */
  margin-bottom: -100px;        /* overlay value text */
  position: relative; z-index: 0;
}
.gauge-track {
  fill: none; stroke: var(--border); stroke-width: 10;
  stroke-dasharray: var(--circ);
  stroke-dashoffset: calc(var(--circ) * 0.25);  /* 270° of 360° */
  stroke-linecap: round;
}
.gauge-fill {
  fill: none; stroke-width: 10;
  stroke-dasharray: var(--circ);
  stroke-dashoffset: var(--circ);              /* starts at 0% */
  stroke-linecap: round;
  transition: stroke-dashoffset 0.6s cubic-bezier(0.4,0,0.2,1), stroke 0.3s;
}

.gauge-icon  { font-size: 18px; color: var(--text-dim); margin-bottom: 2px; z-index: 1; }
.gauge-value { font-size: 34px; font-weight: 700; z-index: 1; }
.gauge-unit  { font-size: 12px; color: var(--text-dim); z-index: 1; }
.gauge-label { font-size: 9px; font-weight: 700; color: var(--text-muted); letter-spacing: .1em; margin-top: 8px; }

.gauge-pill {
  margin-top: 10px;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 9px; font-weight: 700; letter-spacing: .08em;
  border: 1px solid var(--text-muted);
  color: var(--text-muted);
  transition: color .3s, border-color .3s;
}
.gauge-pill.ok     { border-color: var(--ok);     color: var(--ok); }
.gauge-pill.warn   { border-color: var(--warn);   color: var(--warn); }
.gauge-pill.danger { border-color: var(--danger); color: var(--danger); }

/* ── Cards ───────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border-radius: var(--radius);
}
.card-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 18px 20px 0;
}
.card-title { font-size: 14px; font-weight: 700; }
.card-sub   { font-size: 10px; color: var(--text-dim); margin-top: 2px; }
.card-body  { padding: 16px 20px 20px; }

/* Chart */
.chart-card { flex: 1; }
.chart-wrap { padding: 8px 16px 16px; height: 300px; }

.series-toggles { display: flex; gap: 10px; }
.toggle-btn {
  display: flex; align-items: center; gap: 6px;
  font-size: 10px; font-weight: 600;
  color: var(--text-dim); padding: 4px 0;
  transition: color var(--transition); opacity: .5;
}
.toggle-btn.active { color: var(--text); opacity: 1; }
.toggle-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

/* ── Controls page ───────────────────────────────────────────────────────── */
.controls-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 16px;
  align-items: start;
}

.big-number {
  font-size: 52px; font-weight: 700; color: var(--leaf);
  text-align: center; line-height: 1;
  background: var(--bg-alt); border-radius: var(--radius-sm);
  padding: 18px; margin-bottom: 12px;
}
.big-unit { font-size: 18px; color: var(--text-dim); font-weight: 400; }

.slider {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 6px; border-radius: 3px;
  background: var(--bg-alt); outline: none; margin-bottom: 6px;
}
.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--leaf); cursor: pointer;
  box-shadow: 0 0 0 3px rgba(124,217,146,.2);
  transition: box-shadow .2s;
}
.slider::-webkit-slider-thumb:hover { box-shadow: 0 0 0 6px rgba(124,217,146,.2); }

.slider-labels { display: flex; justify-content: space-between; font-size: 10px; color: var(--text-muted); margin-bottom: 14px; }

.preset-row { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.preset-label { font-size: 11px; color: var(--text-muted); }

.checkbox-row { margin-bottom: 14px; }
.checkbox-label { display: flex; align-items: center; gap: 8px; font-size: 11px; color: var(--text-dim); cursor: pointer; }
.checkbox-label input { accent-color: var(--leaf); width: 14px; height: 14px; }

.primary-btn {
  display: block; width: 100%;
  background: var(--leaf); color: #0b140f;
  font-size: 12px; font-weight: 700;
  padding: 12px 0; border-radius: var(--radius-sm);
  transition: opacity var(--transition), transform var(--transition);
  text-align: center;
}
.primary-btn:hover  { opacity: .88; }
.primary-btn:active { transform: scale(.98); }
.primary-btn:disabled { opacity: .4; cursor: not-allowed; }

.water-btn { font-size: 14px; padding: 14px 0; }

.ghost-btn {
  border: 1px solid var(--border-hi); color: var(--text-dim);
  border-radius: var(--radius-sm); padding: 6px 14px;
  font-size: 11px; font-weight: 600;
  transition: background var(--transition), color var(--transition);
}
.ghost-btn:hover { background: var(--surface-hov); color: var(--text); }

.pump-feedback { font-size: 11px; color: var(--text-dim); margin-top: 10px; min-height: 18px; }
.pump-feedback.ok  { color: var(--ok); }
.pump-feedback.err { color: var(--danger); }

/* Config fields */
.field-label { font-size: 10px; font-weight: 700; color: var(--text-muted); letter-spacing: .08em; margin-bottom: 4px; display: block; }
.field-desc  { font-size: 11px; color: var(--text-dim); margin-bottom: 8px; }
.field-hint  { font-size: 10px; color: var(--text-muted); margin-bottom: 14px; }

.input-row { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.text-input {
  background: var(--bg-alt); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 12px; width: 90px; font-size: 13px;
  outline: none;
}
.text-input:focus { border-color: var(--leaf); }
.input-suffix { font-size: 12px; color: var(--text-dim); }

.threshold-row { display: flex; align-items: baseline; gap: 6px; margin-bottom: 6px; }
.threshold-val { font-size: 28px; font-weight: 700; color: var(--earth); }
.threshold-max { font-size: 13px; color: var(--text-muted); }

/* ── System page ─────────────────────────────────────────────────────────── */
.system-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; flex: 1; }

.muted-text { font-size: 12px; color: var(--text-muted); line-height: 1.8; }

.health-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 0; border-bottom: 1px solid var(--border);
}
.health-key { font-size: 10px; font-weight: 700; color: var(--text-muted); letter-spacing: .08em; }
.health-val { font-size: 13px; font-weight: 600; }

.log-card { display: flex; flex-direction: column; }
.log-body {
  flex: 1; overflow-y: auto; font-family: 'JetBrains Mono', monospace;
  font-size: 10px; line-height: 1.8; padding: 10px 16px 16px;
  max-height: 460px;
}
.log-line { display: flex; gap: 8px; }
.log-ts   { color: var(--text-muted); flex-shrink: 0; }
.log-rx   { color: var(--water); }
.log-tx   { color: var(--leaf); }
.log-info { color: var(--text-dim); }
.log-warn { color: var(--warn); }
.log-err  { color: var(--danger); }

/* ── Scrollbar ───────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-hi); border-radius: 3px; }

/* ── Auth Overlay ─────────────────────────────────────────────────────────── */
.auth-overlay {
  position: fixed; inset: 0; z-index: 9999;
  background: rgba(10,18,15,.92);
  backdrop-filter: blur(12px);
  display: flex; align-items: center; justify-content: center;
}
.auth-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 32px; width: 100%; max-width: 400px;
  box-shadow: 0 24px 80px rgba(0,0,0,.6);
}
.auth-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 24px; }
.auth-tabs { display: flex; gap: 4px; margin-bottom: 20px; }
.auth-tab {
  flex: 1; padding: 10px; text-align: center;
  font-size: 12px; font-weight: 700; color: var(--text-dim);
  background: var(--bg-alt); border: 1px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer;
  transition: all var(--transition);
}
.auth-tab.active { background: var(--surface-hi); color: var(--leaf); border-color: var(--leaf); }
.auth-tab:hover:not(.active) { background: var(--surface-hov); }
.auth-input { width: 100%; margin-top: 4px; margin-bottom: 4px; }
.auth-feedback { font-size: 11px; margin-top: 10px; min-height: 16px; color: var(--text-dim); }
.auth-feedback.ok  { color: var(--ok); }
.auth-feedback.err { color: var(--danger); }

/* ── Device List ──────────────────────────────────────────────────────────── */
.device-list { max-height: 280px; overflow-y: auto; }
.device-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; margin-bottom: 6px;
  background: var(--bg-alt); border: 1px solid var(--border);
  border-radius: var(--radius-sm); transition: all var(--transition);
}
.device-item:hover { border-color: var(--border-hi); background: var(--surface-hov); }
.device-item.online { border-left: 3px solid var(--ok); }
.device-item-info { display: flex; align-items: center; gap: 10px; }
.device-item-id { font-size: 12px; font-weight: 600; color: var(--text); word-break: break-all; }
.device-item-label { font-size: 10px; color: var(--text-muted); margin-top: 2px; }
.device-item-actions { display: flex; align-items: center; gap: 6px; }

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 1200px) {
  .gauge-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 900px) {
  .controls-grid, .system-grid { grid-template-columns: 1fr; }
}
@media (max-width: 700px) {
  .sidebar { display: none; }
  .gauge-grid { grid-template-columns: repeat(2, 1fr); }
}
