:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --border: #e3e6e1;
  --text: #1a1f1c;
  --muted: #6a716c;
  --primary: #1f6e2a;
  --primary-dim: #d7e8da;
  --accent: #c97a1a;
  --danger: #b3261e;
  --shadow: 0 1px 3px rgba(0,0,0,0.06);
}

* { box-sizing: border-box; }
html {
  /* Stop mobile Safari/Chrome from inflating text in "wide" blocks after a
     rotate — the layout below is already sized for a phone. */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body {
  margin: 0;
  /* Nothing here is wider than the viewport by design, so a stray overflow is
     a bug, not a scroll affordance. Clip it rather than let the whole page
     slide sideways under the reader's thumb. */
  overflow-x: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.4;
}

img, canvas, svg { max-width: 100%; }

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

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 600; }
.topbar .user { color: var(--muted); font-size: 0.9rem; }

.container { padding: 1.25rem; max-width: 1100px; margin: 0 auto; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.25rem;
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}
.card h2 { margin: 0 0 0.75rem; font-size: 1.05rem; }

.cards { display: grid; gap: 1rem; }
.cards.stats { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 0.25rem;
}
.stat span { color: var(--muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; }
.stat .stat-val { display: flex; align-items: baseline; gap: 0.35rem; }
.stat b    { font-size: 1.7rem; font-weight: 600; color: var(--primary); }
.stat i    { font-style: normal; color: var(--muted); font-size: 0.85rem; }

.controls {
  display: flex; flex-wrap: wrap; gap: 1rem; align-items: end;
}
.controls label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.9rem; color: var(--muted); }
.controls select { padding: 0.4rem 0.6rem; border: 1px solid var(--border); border-radius: 6px; min-width: 220px; max-width: 100%; }
.controls .last-sync { display: inline-flex; align-items: baseline; gap: 0.3rem; font-size: 0.8rem; color: var(--muted); }
.controls .last-sync b { font-weight: 600; color: var(--text); }
.controls .last-sync .rel { color: var(--muted); }

.range-buttons { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.range-buttons button {
  padding: 0.4rem 0.85rem; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); /* override the dark-on-green default button color */
  border-radius: 6px; cursor: pointer; font-size: 0.9rem;
}
.range-buttons button.on,
.range-buttons button:hover {
  background: var(--primary-dim); border-color: var(--primary); color: var(--primary);
}

button {
  background: var(--primary); color: white; border: none; padding: 0.45rem 0.9rem;
  border-radius: 6px; cursor: pointer; font-size: 0.9rem;
}
button:hover { filter: brightness(1.05); }
button.danger { background: var(--danger); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

input[type=text], input[type=password], input[type=email], input[type=number], input:not([type]), select {
  padding: 0.4rem 0.6rem; border: 1px solid var(--border); border-radius: 6px; font-size: 0.9rem;
}

.inline-form { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.inline-form input, .inline-form select { min-width: 0; }
.inline-form .check { display: inline-flex; align-items: center; gap: 0.3rem; color: var(--muted); }

table.grid { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
table.grid th, table.grid td { padding: 0.5rem 0.6rem; text-align: left; vertical-align: middle; border-bottom: 1px solid var(--border); }
table.grid th { background: #f1f3ef; color: var(--muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; }
table.grid .actions { white-space: nowrap; display: flex; gap: 0.4rem; align-items: center; }
table.grid input { font-size: 0.85rem; padding: 0.3rem 0.5rem; }
table.grid .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.82rem; }

.status-ok       { color: var(--primary); }
.status-error,
.status-db_error,
.status-invalid_json,
.status-missing_fields { color: var(--danger); }

.muted { color: var(--muted); font-size: 0.9rem; }
.error { color: var(--danger); font-weight: 500; margin: 0 0 0.75rem; }

/* Login screen */
body.auth { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: var(--bg); padding: 1rem; }
@supports (min-height: 100dvh) { body.auth { min-height: 100dvh; } }
.card.login { width: 320px; max-width: 100%; }
.card.login h1 { margin: 0 0 1rem; font-size: 1.3rem; color: var(--primary); text-align: center; }
.card.login label { display: flex; flex-direction: column; gap: 0.25rem; margin-bottom: 0.85rem; }
.card.login input { width: 100%; }
.card.login button { width: 100%; padding: 0.6rem; font-size: 1rem; }

/* Charts. Chart.js keeps the canvas's own width/height attribute ratio when
   `responsive` is on, which squashes a 300x120 canvas into a ~120px-tall strip
   on a phone. Give the canvas a sized wrapper instead and pair this with
   maintainAspectRatio:false so the chart fills a height we control per
   breakpoint. */
.chart-wrap        { position: relative; width: 100%; height: 300px; }
.chart-wrap.tall   { height: 360px; }
.chart-wrap canvas { display: block; }

/* Empty-state */
.empty { text-align: center; color: var(--muted); padding: 2rem; }

/* Wide tables: let the user scroll horizontally instead of squishing cells. */
.card.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.grid input, table.grid select { max-width: 100%; min-width: 0; }

/* ---------- Mobile reflow ---------- */
@media (max-width: 640px) {
  .container       { padding: 0.75rem; }
  .card            { padding: 0.75rem 0.85rem; border-radius: 6px; }

  .topbar          { flex-direction: column; align-items: flex-start; gap: 0.35rem; padding: 0.65rem 0.85rem; }
  .topbar .user    { font-size: 0.85rem; }

  .controls        { flex-direction: column; align-items: stretch; gap: 0.65rem; }
  .controls label  { width: 100%; }
  .controls select { width: 100%; min-width: 0; }
  .range-buttons   { width: 100%; }
  .range-buttons button { flex: 1 1 30%; min-width: 0; padding: 0.45rem 0.4rem; font-size: 0.85rem; }

  .cards.stats     { grid-template-columns: 1fr 1fr; gap: 0.65rem; }
  .stat            { padding: 0.75rem; }
  .stat b          { font-size: 1.35rem; }

  .card h2         { font-size: 1rem; }

  /* Tables that don't already opt-in to scroll-x: wrap their parent .card. */
  .card > table.grid { display: block; overflow-x: auto; white-space: nowrap; }
  .inline-form     { flex-direction: column; align-items: stretch; }
  /* Text fields fill the column, but a stretched checkbox renders its box
     centred in a full-width slot and shoves its label to the far edge. */
  .inline-form input:not([type=checkbox]):not([type=radio]),
  .inline-form button { width: 100%; }
  .inline-form .check { width: 100%; }

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

  /* The top bar's link run is a row of tap targets, not a sentence — let it
     wrap and give each link room for a fingertip. */
  .topbar .user    { display: flex; flex-wrap: wrap; align-items: center; gap: 0 0.3rem; }
  .topbar .user a  { padding: 0.25rem 0.1rem; }

  /* iOS Safari zooms the whole page in when a focused control's text is under
     16px, and never zooms back out. Every control on a phone gets 16px so
     tapping a field doesn't shove the layout sideways. */
  input:not([type=checkbox]):not([type=radio]), select, textarea,
  table.grid input, table.grid select,
  .field input, .field select { font-size: 16px; }

  /* Fingertip-sized hit areas. 2.4rem ~= 38px, close to the 44px guidance
     once the surrounding gap is counted. */
  button, .range-buttons button { min-height: 2.4rem; }
  input:not([type=checkbox]):not([type=radio]), select { min-height: 2.4rem; }
}
