/* Toshiko CGM dashboard styles.
   Deliberately plain and dependency-free: system fonts only, no web fonts, no
   framework. Light and dark both supported via prefers-color-scheme. */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #16191d;
  --text-secondary: #5c6470;
  --border: #dfe3e8;
  --accent: #1565c0;
  --accent-contrast: #ffffff;
  --danger: #c62828;
  --in-range: rgba(46, 125, 50, 0.12);
  --low: #e65100;
  --high: #ad1457;
  --grid: #e8ebef;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --surface: #1c2024;
    --text: #e8eaed;
    --text-secondary: #9aa3ad;
    --border: #2c3238;
    --accent: #64b5f6;
    --accent-contrast: #0d1114;
    --danger: #ef9a9a;
    --in-range: rgba(129, 199, 132, 0.14);
    --low: #ffb74d;
    --high: #f48fb1;
    --grid: #262b31;
  }
}

* {
  box-sizing: border-box;
}

/* The `hidden` attribute only sets `display: none` in the UA stylesheet, so ANY
   author-level display rule beats it. `.boot` and `.login-view` both set
   `display: grid`, which left the boot overlay painted over the whole dashboard
   even though the element was marked hidden. Author-level + !important is what
   makes `hidden` actually mean hidden. */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.muted {
  color: var(--text-secondary);
}
.small {
  font-size: 0.85rem;
}
.error {
  color: var(--danger);
  font-size: 0.9rem;
}

/* ─── Boot ─────────────────────────────────────────────────────────────── */

.boot {
  min-height: 100vh;
  display: grid;
  place-items: center;
  gap: 1rem;
  color: var(--text-secondary);
}

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

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

@media (prefers-reduced-motion: reduce) {
  .spinner {
    animation: none;
  }
}

/* ─── Cards and forms ──────────────────────────────────────────────────── */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.login-card {
  width: 100%;
  max-width: 22rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 1.75rem;
}

.login-card h1 {
  margin: 0;
  font-size: 1.35rem;
}

label {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 0.5rem;
}

input {
  font: inherit;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg);
  color: var(--text);
  min-width: 0;
}

input:focus-visible,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button {
  font: inherit;
  cursor: pointer;
  border-radius: 7px;
  border: 1px solid var(--border);
  padding: 0.5rem 0.85rem;
  background: var(--surface);
  color: var(--text);
}

button:disabled {
  opacity: 0.55;
  cursor: default;
}

button.primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: transparent;
  margin-top: 1rem;
  padding: 0.6rem;
  font-weight: 600;
}

button.ghost {
  background: transparent;
}

/* ─── Layout ───────────────────────────────────────────────────────────── */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand {
  font-weight: 700;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.tabs {
  display: flex;
  gap: 0.25rem;
  padding: 0.5rem 1rem 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.tab {
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: transparent;
  color: var(--text-secondary);
  padding: 0.5rem 0.75rem;
}

.tab.active {
  color: var(--text);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

.content {
  max-width: 68rem;
  margin: 0 auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.toolbar label {
  margin-top: 0;
}

/* Keeps a label and its input as one unwrappable unit. */
.field {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.field label {
  margin-top: 0;
  white-space: nowrap;
}

.field input {
  min-width: 0;
}

.range-buttons {
  display: flex;
  gap: 0.25rem;
  flex-wrap: wrap;
}

.range.active {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: transparent;
}

/* ─── Chart ────────────────────────────────────────────────────────────── */

.chart-card {
  padding: 0.75rem;
}

canvas {
  display: block;
  width: 100%;
  height: 340px;
  touch-action: none; /* we handle pan/zoom ourselves */
}

.chart-hint {
  margin: 0.5rem 0 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.6rem;
  flex-wrap: wrap;
}

button.tiny {
  padding: 0.3rem 0.6rem;
  font-size: 0.8rem;
}

/* Which gesture hint applies is a property of the input device, not the screen
   width — a touch laptop is wide and still cannot scroll-to-zoom. */
.hint-coarse {
  display: none;
}

@media (pointer: coarse) {
  .hint-fine {
    display: none;
  }
  .hint-coarse {
    display: inline;
  }
}

.latest {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.latest .value {
  font-size: 2rem;
  font-weight: 700;
}

.latest .value.low {
  color: var(--low);
}
.latest .value.high {
  color: var(--high);
}

.summary {
  display: flex;
  gap: 1.25rem;
  flex-wrap: wrap;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.6rem 0.9rem;
  min-width: 6.5rem;
}

.stat .label {
  font-size: 0.75rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.stat .figure {
  font-size: 1.25rem;
  font-weight: 600;
}

/* ─── Table ────────────────────────────────────────────────────────────── */

.table-card {
  padding: 0;
  overflow: auto;
  max-height: 22rem;
}

table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.9rem;
}

th,
td {
  text-align: left;
  padding: 0.45rem 0.9rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

th {
  position: sticky;
  top: 0;
  background: var(--surface);
  color: var(--text-secondary);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

tbody tr:last-child td {
  border-bottom: none;
}

.footer {
  text-align: center;
  padding: 1.5rem 1rem 2.5rem;
}

@media (max-width: 40rem) {
  canvas {
    height: 260px;
  }
  .latest .value {
    font-size: 1.6rem;
  }

  /* Left to wrap freely, the header broke into three ragged rows and ate ~150px
     above the fold. Two deliberate rows instead: identity, then controls. */
  .topbar {
    padding: 0.6rem 0.75rem;
    gap: 0.5rem;
  }
  .topbar-right {
    width: 100%;
    justify-content: space-between;
    flex-wrap: nowrap;
    gap: 0.5rem;
  }
  #account-email {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* The email is the only thing allowed to give up space; without this the
     buttons shrink until "Sign out" breaks across two lines. */
  .topbar-right button {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .content {
    padding: 0.75rem;
  }

  /* An even grid beats wrapping: six equal ranges, then a full-width Refresh,
     instead of five on one line and one orphaned below. */
  .range-buttons {
    flex: 1 1 100%;
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 0.25rem;
  }
  .range {
    padding: 0.5rem 0.2rem;
    text-align: center;
  }
  #reload {
    flex: 1 1 100%;
  }

  /* Date on its own row, From and To sharing the next, action last.
     Order matters here: .field and .field-date have equal specificity, so the
     wide-date rule has to come second or the 45% basis wins. */
  .field {
    flex: 1 1 45%;
  }
  .field-date,
  #day-reset {
    flex: 1 1 100%;
  }
  .field-date input {
    flex: 1;
  }

  /* Two columns, not three: a figure like "146 mg/dL" needs the width, and
     wrapping it left the tiles at ragged heights. */
  .summary {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
    gap: 0.5rem;
  }
  .stat {
    min-width: 0;
  }
  .stat .figure {
    white-space: nowrap;
  }

  /* Keep the table a contained panel rather than letting a 300-reading day turn
     into a 10 000px page. Sized against the viewport, so the cut-off row reads
     as "there is more below" instead of looking broken the way a fixed 22rem
     did. */
  .table-card {
    max-height: 70vh;
  }
  th,
  td {
    padding: 0.45rem 0.6rem;
  }
}
