:root {
  --bg: #f5f7f4;
  --surface: #ffffff;
  --surface-2: #f0f3ee;
  --ink: #1f2a1c;
  --ink-soft: #5b6b54;
  --muted: #8a9a83;
  --line: #e4eadf;

  --green: #4caf50;
  --green-deep: #2e7d32;
  --amber: #f9a825;
  --red: #e53935;
  --grey: #9aa6a1;

  --n: #2e7d32;
  --p: #6a1b9a;
  --k: #c62828;

  --sky: #e8f4ec;

  --radius: 16px;
  --shadow: 0 2px 14px rgba(31, 42, 28, 0.06);

  font-family: 'Sarabun', system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.45;
  padding-bottom: 40px;
}

h2 { margin: 0 0 6px; font-size: 22px; font-weight: 700; }
h3 { margin: 0 0 4px; font-size: 17px; font-weight: 700; }
.muted { color: var(--muted); font-size: 14px; margin: 0; }
.bold { font-weight: 700; }

/* ---------------- Topbar ---------------- */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 18px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-icon { font-size: 36px; }
.brand-title { font-weight: 800; font-size: 22px; }
.brand-sub { font-size: 13px; color: var(--ink-soft); }
.topbar-right { display: flex; align-items: center; gap: 10px; }

#nodeSelect {
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-family: inherit;
  font-size: 16px;
  color: var(--ink);
  font-weight: 600;
  min-width: 130px;
}
.ghost-btn {
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-family: inherit;
  font-size: 15px;
  color: var(--ink);
  cursor: pointer;
}
.ghost-btn:hover { background: var(--surface-2); }

/* ---------------- Hero status banner ---------------- */
.hero {
  display: flex;
  align-items: center;
  gap: 18px;
  margin: 18px 24px;
  padding: 22px 24px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border-left: 8px solid var(--grey);
}
.hero.ok      { border-left-color: var(--green); background: #ebf6ec; }
.hero.caution { border-left-color: var(--amber); background: #fff8e3; }
.hero.warn    { border-left-color: var(--red);   background: #fdeded; }
.hero.offline { border-left-color: var(--red);   background: #fdeded; }
.hero-icon { font-size: 56px; line-height: 1; }
.hero-text { flex: 1; }
.hero-title { font-size: 26px; font-weight: 800; }
.hero-sub { font-size: 15px; color: var(--ink-soft); margin-top: 2px; }
.hero-meta { text-align: right; }
.hero-meta .bold { font-size: 16px; }

/* ---------------- Tree visualization ---------------- */
.tree-section {
  margin: 18px 24px;
  padding: 22px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.tree-title { font-size: 22px; }
.tree-caption { margin-bottom: 12px; }
.tree-viz {
  display: flex;
  justify-content: center;
  margin: 8px auto 16px;
  max-width: 640px;
}
.tree-viz svg {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 12px;
  overflow: hidden;
}

.tree-viz .layer-label {
  fill: rgba(255,255,255,0.95);
  font-family: 'Sarabun', sans-serif;
  font-size: 17px;
  font-weight: 700;
  paint-order: stroke fill;
  stroke: rgba(0,0,0,0.55);
  stroke-width: 0.6px;
}
.tree-viz .layer-sublabel {
  fill: rgba(255,255,255,0.85);
  font-family: 'Sarabun', sans-serif;
  font-size: 13px;
  font-weight: 600;
  paint-order: stroke fill;
  stroke: rgba(0,0,0,0.45);
  stroke-width: 0.4px;
}
.tree-viz .layer-value {
  fill: white;
  font-family: 'Sarabun', sans-serif;
  font-size: 24px;
  font-weight: 800;
  paint-order: stroke fill;
  stroke: rgba(0,0,0,0.6);
  stroke-width: 0.7px;
}
.tree-viz .layer-temp {
  fill: rgba(255,255,255,0.95);
  font-family: 'Sarabun', sans-serif;
  font-size: 14px;
  font-weight: 600;
  paint-order: stroke fill;
  stroke: rgba(0,0,0,0.45);
  stroke-width: 0.4px;
}

.averages {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin: 12px 0 4px;
}
.avg-pill {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  padding: 10px 18px;
  background: var(--surface-2);
  border-radius: 999px;
  font-weight: 600;
}
.avg-pill .avg-icon { font-size: 18px; align-self: center; }
.avg-pill .avg-label { color: var(--ink-soft); font-size: 14px; }
.avg-pill .avg-value { font-size: 22px; font-weight: 800; color: var(--ink); }
.avg-pill .avg-unit { font-size: 14px; color: var(--ink-soft); font-weight: 600; }

.legend {
  margin: 14px auto 0;
  max-width: 420px;
}
.legend-bar {
  height: 12px;
  border-radius: 6px;
  background: linear-gradient(to right,
    #c8a878 0%,
    #a67c52 20%,
    #6b6b2e 40%,
    #3a7d44 55%,
    #1f6b7a 80%,
    #1f4b8a 100%);
}
.legend-labels {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--ink-soft);
  margin-top: 4px;
  font-weight: 600;
}

/* ---------------- Advice list ---------------- */
.advice-section {
  margin: 18px 24px;
  padding: 22px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.advice-list {
  list-style: none;
  padding: 0;
  margin: 12px 0 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.advice-list li {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  background: var(--surface-2);
  border-radius: 12px;
  font-size: 17px;
  border-left: 6px solid var(--grey);
}
.advice-list li.ok      { border-left-color: var(--green); background: #eaf6ec; }
.advice-list li.caution { border-left-color: var(--amber); background: #fff7df; }
.advice-list li.warn    { border-left-color: var(--red);   background: #fdebeb; }
.advice-list li .advice-icon { font-size: 28px; line-height: 1; }
.advice-list li .advice-text { flex: 1; line-height: 1.4; }

/* ---------------- NPK cards ---------------- */
.npk-section {
  margin: 18px 24px;
  padding: 22px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.npk-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-top: 12px;
}
.npk-card {
  padding: 18px;
  background: var(--surface-2);
  border-radius: 14px;
  text-align: center;
  border-top: 5px solid var(--grey);
}
.npk-card.n { border-top-color: var(--n); }
.npk-card.p { border-top-color: var(--p); }
.npk-card.k { border-top-color: var(--k); }
.npk-letter {
  font-size: 38px;
  font-weight: 800;
  line-height: 1;
  margin-bottom: 4px;
}
.npk-card.n .npk-letter { color: var(--n); }
.npk-card.p .npk-letter { color: var(--p); }
.npk-card.k .npk-letter { color: var(--k); }
.npk-name { font-size: 14px; color: var(--ink-soft); }
.npk-value {
  margin-top: 8px;
  font-size: 26px;
  font-weight: 800;
}
.npk-unit { font-size: 13px; color: var(--ink-soft); font-weight: 600; margin-left: 2px; }

/* ---------------- History toggle / charts ---------------- */
.history-section { margin: 18px 24px; }
.history-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  color: var(--ink-soft);
  cursor: pointer;
}
.history-toggle:hover { background: var(--surface-2); }
.history-toggle .caret {
  font-size: 12px;
  transition: transform 0.2s ease;
  display: inline-block;
}
.history-toggle.open .caret { transform: rotate(180deg); }

.history-content { margin-top: 14px; }
.range-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.range-tab {
  padding: 10px 18px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  color: var(--ink-soft);
  cursor: pointer;
}
.range-tab.active {
  background: var(--green-deep);
  color: #fff;
  border-color: var(--green-deep);
}

.chart-block {
  margin-bottom: 14px;
  padding: 18px 18px 8px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.chart-block h3 { margin-bottom: 2px; }
.chart-block p { margin-bottom: 12px; }
.chart-wrap { position: relative; height: 280px; }

.footer { text-align: center; margin-top: 28px; }

/* ---------------- Mobile ---------------- */
@media (max-width: 720px) {
  .topbar { padding: 14px 16px; gap: 10px; }
  .brand-title { font-size: 18px; }
  .brand-sub { font-size: 12px; }
  .brand-icon { font-size: 28px; }
  #nodeSelect, .ghost-btn { padding: 10px 12px; font-size: 14px; }
  #nodeSelect { min-width: 100px; }

  .hero, .tree-section, .advice-section, .npk-section { margin-left: 12px; margin-right: 12px; padding: 16px; }
  .hero { gap: 12px; padding: 16px; }
  .hero-icon { font-size: 44px; }
  .hero-title { font-size: 20px; }
  .hero-meta { text-align: right; min-width: 90px; }

  .history-section { margin-left: 12px; margin-right: 12px; }
  .npk-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .npk-card { padding: 12px 8px; }
  .npk-letter { font-size: 28px; }
  .npk-value { font-size: 20px; }

  .advice-list li { padding: 12px; font-size: 16px; }
  .advice-list li .advice-icon { font-size: 24px; }

  .chart-wrap { height: 240px; }
}

@media (max-width: 420px) {
  .topbar { flex-wrap: wrap; }
  .topbar-right { width: 100%; justify-content: flex-end; }
  .hero { flex-wrap: wrap; }
  .hero-meta { width: 100%; text-align: left; padding-left: 68px; }
}
