:root {
  --bg: #0f1226;
  --bg-card: #1a1e3a;
  --accent: #7c8cff;
  --accent-soft: #4b5299;
  --text: #e8e9f5;
  --text-dim: #9497b8;
  --ok: #5fd08a;
  --warn: #f5a623;
  --danger: #ef5b6b;
  --border: #2a2f57;
}

* { box-sizing: border-box; }

/* The UA stylesheet's [hidden] { display: none } loses to any author rule
   that sets display — e.g. .btn { display: inline-block } — so the hidden
   attribute silently did nothing on buttons. Make it authoritative. */
[hidden] { display: none !important; }

html {
  background: var(--bg);
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(ellipse at top, #171b3a 0%, var(--bg) 60%);
  color: var(--text);
  min-height: 100vh;
}

.wrap {
  max-width: 960px;
  margin: 0 auto;
  /* env(safe-area-inset-top) keeps content clear of the status bar/notch when
     running as an installed iOS PWA with a translucent status bar. */
  padding: calc(24px + env(safe-area-inset-top)) 20px 60px;
}

header.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* Wraps as whole groups, not mid-text: if .brand and .header-actions don't
     both fit on one row, .header-actions drops to its own row below rather
     than either flex-shrinking (which — with white-space:nowrap below —
     let "Sleep Demon Guard" render past its own shrunk box, overlapping the
     buttons instead of actually getting smaller). */
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 32px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.25rem;
  letter-spacing: 0.01em;
  white-space: nowrap;
  flex-shrink: 0;
}

.brand .moon { font-size: 1.4rem; }
.brand-icon { width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0; }

.header-actions { display: flex; align-items: center; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }

.icon-btn-round {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.icon-btn-round:hover { border-color: var(--accent); background: rgba(124,140,255,0.08); }

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

.btn {
  display: inline-block;
  background: var(--accent);
  color: #0f1226;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  padding: 10px 18px;
  cursor: pointer;
  font-size: 0.95rem;
  white-space: nowrap;
}
.btn:hover { background: #949fff; }
.btn.secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
.btn.secondary:hover { border-color: var(--accent); background: rgba(124,140,255,0.08); }
.btn.danger { background: var(--danger); color: #fff; }

/* Below this, "Sleep Demon Guard" at full size plus the header buttons
   genuinely don't fit on one row on a phone — shrink instead of wrapping. */
@media (max-width: 430px) {
  .brand { font-size: 1.02rem; gap: 7px; }
  .brand-icon { width: 24px; height: 24px; }
  header.top .btn { padding: 8px 12px; font-size: 0.85rem; }
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  margin-bottom: 18px;
}

.device-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
}

.device-card {
  display: block;
  color: inherit;
}
.device-card:hover { text-decoration: none; }
.device-card .card { transition: border-color .15s; }
.device-card:hover .card { border-color: var(--accent); }

.status-dot {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 50%;
  margin-right: 6px;
}
.status-dot.online { background: var(--ok); }
.status-dot.offline { background: var(--danger); }

.metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 12px;
  margin: 16px 0;
}
.metric {
  background: rgba(255,255,255,0.03);
  border-radius: 10px;
  padding: 12px 14px;
}
.metric { position: relative; }
.metric .label-row { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.metric .label { color: var(--text-dim); font-size: 0.78rem; text-transform: uppercase; letter-spacing: .04em; min-width: 0; overflow-wrap: break-word; }
.metric .label-row .icon-btn { flex-shrink: 0; }
.metric .value-row { display: flex; align-items: baseline; gap: 6px; margin-top: 4px; }
.metric .value { font-size: 1.5rem; font-weight: 700; }
.metric .sub { font-size: 0.78rem; color: var(--text-dim); margin-top: 2px; }
.metric .status-icon { display: inline-flex; }
.metric .trend-icon { display: inline-flex; }

.icon-btn {
  background: transparent;
  border: none;
  padding: 2px;
  cursor: pointer;
  color: var(--text-dim);
  display: inline-flex;
  align-items: center;
  border-radius: 4px;
}
.icon-btn:hover { color: var(--text); background: rgba(255,255,255,0.06); }
.icon-btn:disabled { opacity: 0.3; cursor: not-allowed; background: transparent; }

.info-popover {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 6px;
  width: 240px;
  background: #0f1226;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.8rem;
  color: var(--text-dim);
  line-height: 1.4;
  box-shadow: 0 8px 20px rgba(0,0,0,0.5);
  z-index: 5;
}

.chart-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.chart-card-head h3 { margin: 0; }
.chart-card-head .actions { display: flex; gap: 4px; position: relative; }

.settings-popover {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 6px;
  width: 220px;
  background: #0f1226;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  z-index: 6;
  box-shadow: 0 8px 20px rgba(0,0,0,0.5);
}
.settings-popover label {
  display: block;
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-bottom: 4px;
}
.settings-popover select { width: 100%; margin-bottom: 12px; }
.settings-popover .checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; color: var(--text); }

form.stack { display: flex; flex-direction: column; gap: 12px; max-width: 360px; }
input, select {
  background: #0f1226;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.95rem;
}

.login-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(24px + env(safe-area-inset-top)) 20px calc(24px + env(safe-area-inset-bottom));
}

.login-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;
  max-width: 340px;
}
.login-logo {
  width: 112px;
  height: 112px;
  border-radius: 26px;          /* squircle-ish, echoes the home-screen icon */
  display: block;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
}
.login-title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  text-align: center;
}
.login-col .card {
  width: 100%;
  margin-bottom: 0;
}
.login-col form.stack { max-width: none; }

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

.chart-tooltip {
  position: absolute;
  background: #0f1226;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.8rem;
  pointer-events: none;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  z-index: 2;
}

.range-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.range-tabs button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 0.85rem;
  cursor: pointer;
}
.range-tabs button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #0f1226;
  font-weight: 600;
}

.chart-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

/* Lets the chart SVG (width:100%) stretch edge-to-edge within its card
   instead of sitting inside the card's normal text padding. */
.chart-bleed { margin: 0 -20px; }

/* --- Install guide -------------------------------------------------------
   iOS exposes no install API, so this is guidance, not automation: name the
   button, draw its glyph, and put the steps in order. */
.guide-overlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 8, 20, 0.72);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  animation: guideFade .18s ease-out;
}
@keyframes guideFade { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .guide-overlay { animation: none; }
}

.guide-sheet {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 22px 20px;
  width: 100%;
  max-width: 420px;
  max-height: 86vh;
  overflow-y: auto;
}
.guide-sheet h3 { margin: 0 0 8px; font-size: 1.1rem; }
.guide-sheet > .muted { margin: 0 0 16px; }

.guide-steps {
  list-style: none;
  counter-reset: step;
  margin: 0 0 18px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.guide-steps li {
  counter-increment: step;
  display: grid;
  grid-template-columns: 24px 30px 1fr;
  align-items: center;
  gap: 10px;
  font-size: 0.92rem;
  line-height: 1.4;
}
.guide-steps li::before {
  content: counter(step);
  width: 24px; height: 24px;
  border-radius: 50%;
  background: var(--accent);
  color: #0f1226;
  font-size: 0.78rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
/* Steps without a glyph keep the text aligned with the ones that have it. */
.guide-steps li > span:first-of-type:not(.guide-glyph) { grid-column: 2 / 4; }
.guide-glyph {
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.guide-copy {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 18px;
  flex-wrap: wrap;
}
.guide-copy code {
  flex: 1 1 150px;
  background: #0f1226;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 0.85rem;
  user-select: all;
}

details.data-table summary {
  cursor: pointer;
  color: var(--text-dim);
  font-size: 0.8rem;
  margin-top: 10px;
}
details.data-table[open] summary { margin-bottom: 8px; }

.site-footer {
  display: flex;
  justify-content: center;
  padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
}
.site-footer img {
  max-width: 100%;
  width: 420px;
  height: auto;
}

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
th { color: var(--text-dim); font-weight: 600; }
