* {
  box-sizing: border-box;
  --bg: #111;
  --card: #1e1e1e;
  --cardAccent: #333;
  --primary: #4caf50;
  --primaryGlow: rgba(76, 175, 80, 0.25);
  --midPoint: #FFB41F;
  --accent: #EE4266;
  --text: #efefef;
}

html, body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background: var(--bg);
}

:root {
  --header-h: 56px;
  --collapsed-history-h: 80px; /* visible small history area when collapsed */
  --page-vertical-gap: 1em;
  background: var(--bg);
}

body {
  font-family: system-ui, sans-serif;
  background: var(--bg);
  /* leave horizontal padding, reserve top space for fixed header */
  padding: 0 1em 1em 1em;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 1em;
  overflow: scroll; /* enforce single non-scrollable page */
}

h1, h2 {
  margin-top: 0;
}

a {
  color: var(--primary);
  text-decoration: none;
}

#loginOverlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.login-box {
  background: var(--card);
  padding: 2em;
  border-radius: 16px;
  text-align: center;
  color: var(--text);
  max-width: 400px;
  aspect-ratio: 5/5;
  width: 80%;

  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 0.5em;
  justify-content: flex-start;
}

.login-box input {
  display: block;
  width: 100%;
  padding: 0.5em;
  border-radius: 8px;
  border: 2px solid var(--cardAccent);
  background: var(--card);
  color: var(--text);
  font-size: large;
}

.login-box button {
  padding: 0.5em 1em;
  background: var(--primary);
  border: none;
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  font-size: larger;
}

nav {
  display: flex;
  gap: 1em;
  align-items: flex-end;
  flex-direction: row;
}

nav > strong {
  font-size: 2em;
  position: relative;
  line-height: 1;
  bottom: -4px;
}

nav a {
  line-height: 1;
}

.card {
  background: var(--card);
  border-radius: 8px;
  padding: 1em;
  margin-bottom: 1em;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  flex: 0 1 auto;
  box-sizing: border-box;
  overflow: hidden;
}

.system-stats-card {
  max-height: 40vh; /* limit height so other cards remain visible */
  overflow: hidden;
}

.double-gauges {
  max-height: 80vh;
}

.double-gauges .gauge {
  aspect-ratio: 2 / 1;
}

#historyCard.history-collapsed {
  overflow: hidden;          /* ensure content is clipped */
  max-height: 65px;             /* collapsed state */
  transition: max-height 0.35s cubic-bezier(.4,0,.2,1); /* animate collapse */
}

#historyCard:not(.history-collapsed) {
            /* expanded state, adjust as needed */
  transition: max-height 0.35s cubic-bezier(.4,0,.2,1); /* smooth expand */
}

.history-canvas-wrap {
  width: 100%;
  min-height: 300px;
}

#historyCard {
  flex: 1 0 0;
  min-height: 0;
  overflow: hidden;
}
#historyCard.collapsed {
  flex: 0 0 auto;
}

/* Small visual gauges used in system card */
.gauge {
  width: 100%;
  aspect-ratio: 5 / 3;

  /* size limits */
  max-height: 140px;
  min-height: 60px;

  display: flex;
  align-items: center;
  justify-content: center;

  flex-shrink: 0;
  overflow: hidden;
}


/* Make sure any chart/canvas inside a gauge fits and doesn't overflow */
.gauge svg {
  width: 100%;
  height: 100%;
  display: block;
}


/* Keep canvas sizing too (history chart) */
.gauge > canvas, .gauge canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

/* Stats grid (top card) should stretch to fill the card vertically so gauges can grow */
.stats-grid {
  display: grid;
  grid-auto-rows: minmax(min-content, auto);
  align-items: stretch;
  /* allow the grid to size naturally to its content so parent .card can grow */
  height: auto;
  box-sizing: border-box;
}

@media (max-width: 420px) {
  .stats-grid > div {
    gap: 0.3em;
  }
}

.stats-grid > div {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 0.5em;
  min-height: max-content;
}


/* Allow direct child DIVs inside cards to grow/shrink with their content so
   the parent `.card` will expand to accommodate gauges when they grow. */
.card > div {
  flex: 0.7 1 auto;
}

/* Collapse button in history card */
.collapse-btn {
  float: right;
  background: transparent;
  border: 1px solid var(--cardAccent);
  cursor: pointer;
  color: var(--text);
  font-size: 0.85em;
  padding: 0.2em 0.6em;
  border-radius: 6px;
}
.collapse-btn:hover { background: var(--cardAccent); }

/* History canvas wrapper: provides bottom padding so x-axis isn't clipped */
.history-canvas-wrap {
  box-sizing: border-box;
  overflow: hidden;
  display: flex;
  flex-direction: row;
}

.history-canvas-wrap > div {
  width: 50%;
}

.grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.4em 1em;
  font-size: 0.9em;
  margin-top: 0.8em;
}

canvas {
  width: 100%;
  display: block;
}

@media (max-width: 768px), (max-height: 600px) {
  body {
    overflow-y: visible;
    height: auto;
    max-height: none;
  }

  .system-stats-card, .double-gauges {
    max-height: none; /* remove 40vh cap */
  }

  .stats-grid {
    gap: 1.2em;
  }

  .stats-grid > div {
    gap: 0.6em;
  }

  .history-canvas-wrap {
    flex-direction: column;
  }

  .history-canvas-wrap > div {
    width: 100%;
    flex: 0 1 auto;
  }
}

@media (min-width: 768px) {
  body, html {
    overscroll-behavior-y: none;
  }
}