/* ===========================================================================
   ReadMyBaby — Analytics (internal admin tool)
   Self-contained styles. Matches the marketing site "Cozy nightlight" system:
   warm clay + sage + slate + honey. Baloo 2 (display) + Nunito (body).
   Dark is the DEFAULT here (internal tool) but we honor prefers-color-scheme.
   All text/background pairings pin explicit colors for WCAG-AA contrast.
   =========================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Baloo+2:wght@500;600;700;800&family=Nunito:wght@400;500;600;700;800&display=swap');

/* ---- Tokens: DARK is the default theme ---- */
:root {
  /* brand */
  --clay: #C15F3C;
  --clay-bright: #D9744F;
  --clay-soft: #E89A78;
  --sage: #407753;
  --sage-soft: #9CC4A9;
  --slate: #3D698F;
  --slate-soft: #9BC0E0;
  --honey: #B07A2E;
  --honey-soft: #E7C078;

  /* dark surfaces (default) */
  --bg: #1D1611;
  --bg-tint: #251C16;
  --surface: #2A201A;
  --surface-2: #34281F;
  --on-bg: #F1ECE6;
  --on-muted: #C8B9A9;
  --border: rgba(255, 255, 255, 0.08);
  --shadow: 0 24px 50px -24px rgba(0, 0, 0, 0.8);
  --shadow-sm: 0 12px 28px -18px rgba(0, 0, 0, 0.7);

  --primary: #EAA686;
  --on-primary: #4A2311;

  /* chart series + danger (pinned, theme-aware below) */
  --series-events: #E89A78;   /* clay-soft */
  --series-devices: #9BC0E0;  /* slate-soft */
  --danger: #E06A4F;
  --on-danger: #FFFFFF;
  --ok: #9CC4A9;

  --radius-lg: 22px;
  --radius: 16px;
  --radius-sm: 12px;
  --maxw: 1180px;
  --font-display: 'Baloo 2', system-ui, sans-serif;
  --font-body: 'Nunito', system-ui, sans-serif;
  color-scheme: dark;
}

/* ---- Light theme: only when the user explicitly prefers light ---- */
@media (prefers-color-scheme: light) {
  :root {
    --bg: #FAF6EC;
    --bg-tint: #F3E9DA;
    --surface: #FFFDF8;
    --surface-2: #F4ECDE;
    --on-bg: #2D2925;
    --on-muted: #6B6057;
    --border: rgba(45, 41, 37, 0.10);
    --shadow: 0 18px 40px -22px rgba(80, 50, 30, 0.45);
    --shadow-sm: 0 8px 20px -14px rgba(80, 50, 30, 0.4);

    --primary: #AE5129;
    --on-primary: #FFFFFF;

    /* darker series so they read on cream */
    --series-events: #AE5129;
    --series-devices: #3D698F;
    --danger: #B23A22;
    --ok: #2F6B43;
    color-scheme: light;
  }
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-font-smoothing: antialiased; }

body {
  font-family: var(--font-body);
  background:
    radial-gradient(700px 420px at 92% -8%, rgba(232,154,120,0.16), transparent 60%),
    radial-gradient(620px 460px at -8% 12%, rgba(64,119,83,0.12), transparent 55%),
    var(--bg);
  color: var(--on-bg);
  line-height: 1.5;
  font-weight: 500;
  min-height: 100vh;
}

h1, h2, h3, h4 { font-family: var(--font-display); font-weight: 700; line-height: 1.14; color: var(--on-bg); }
.muted { color: var(--on-muted); }
.mono { font-variant-numeric: tabular-nums; }

/* ---------- Top bar / header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 40;
  backdrop-filter: blur(12px);
  background: color-mix(in srgb, var(--bg) 84%, transparent);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: var(--maxw); margin: 0 auto; padding: 14px 22px;
  display: flex; align-items: center; gap: 18px 22px; flex-wrap: wrap;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  font-family: var(--font-display); font-weight: 800; font-size: 20px; color: var(--on-bg);
  white-space: nowrap;
}
.brand .dot { font-size: 22px; line-height: 1; }
.brand .sep { color: var(--on-muted); font-weight: 700; }

.controls { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-left: auto; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > label {
  font-size: 11px; font-weight: 800; letter-spacing: .4px; text-transform: uppercase;
  color: var(--on-muted);
}
.input {
  font-family: var(--font-body); font-weight: 600; font-size: 14px;
  color: var(--on-bg); background: var(--surface);
  border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  padding: 9px 12px; min-width: 180px; outline: none;
  transition: border-color .12s ease, box-shadow .12s ease;
}
.input::placeholder { color: var(--on-muted); opacity: .8; }
.input:focus { border-color: var(--clay-soft); box-shadow: 0 0 0 3px rgba(232,154,120,0.22); }
.input--wide { min-width: 240px; }

/* segmented range control */
.segmented { display: inline-flex; background: var(--surface); border: 1.5px solid var(--border); border-radius: 50px; padding: 3px; }
.segmented button {
  font-family: var(--font-display); font-weight: 700; font-size: 14px;
  color: var(--on-muted); background: none; border: none; cursor: pointer;
  padding: 7px 14px; border-radius: 50px; transition: color .12s ease;
}
.segmented button[aria-pressed="true"] {
  color: var(--on-primary);
  background: linear-gradient(150deg, var(--clay-soft), var(--clay-bright));
  box-shadow: 0 8px 18px -10px rgba(193,95,60,0.6);
}

/* pill buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font-family: var(--font-display); font-weight: 700; font-size: 15px;
  padding: 10px 18px; border-radius: 50px; border: none; cursor: pointer; white-space: nowrap;
  transition: transform .12s ease, box-shadow .12s ease, opacity .12s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:disabled { opacity: .6; cursor: not-allowed; transform: none; }
.btn-primary {
  color: var(--on-primary);
  background: linear-gradient(150deg, var(--clay-soft), var(--clay-bright));
  box-shadow: 0 16px 30px -14px rgba(193,95,60,0.6);
}
.btn-ghost { color: var(--on-bg); background: var(--surface-2); box-shadow: inset 0 0 0 1.5px var(--border); }
.btn-danger { color: var(--on-danger); background: linear-gradient(150deg, #E06A4F, #B23A22); box-shadow: 0 14px 26px -14px rgba(178,58,34,0.7); }
.btn-sm { font-size: 13px; padding: 8px 14px; }

/* ---------- Page shell ---------- */
.page { max-width: var(--maxw); margin: 0 auto; padding: 26px 22px 60px; }
.section { margin-top: 30px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.section-head h2 { font-size: 22px; }
.eyebrow { font-family: var(--font-display); font-weight: 700; color: var(--clay-soft); letter-spacing: .4px; font-size: 13px; text-transform: uppercase; }
@media (prefers-color-scheme: light) { .eyebrow { color: var(--primary); } }

/* global banner (errors / unauthorized / info) */
.banner {
  display: none; gap: 12px; align-items: flex-start;
  background: var(--surface); color: var(--on-bg);
  border: 1px solid var(--border); border-left: 5px solid var(--on-muted);
  border-radius: var(--radius); padding: 14px 16px; margin-bottom: 18px;
  font-weight: 600; box-shadow: var(--shadow-sm);
}
.banner.show { display: flex; }
.banner.error { border-left-color: var(--danger); }
.banner.warn  { border-left-color: var(--honey-soft); }
.banner.ok    { border-left-color: var(--ok); }
.banner strong { font-family: var(--font-display); }
.banner .b-close { margin-left: auto; background: none; border: none; color: var(--on-muted); font-size: 20px; cursor: pointer; line-height: 1; }

/* ---------- Pebble cards ---------- */
.pebble {
  background: var(--surface); border-radius: var(--radius-lg);
  padding: 22px; box-shadow: var(--shadow); border: 1px solid var(--border);
}

.cards { display: grid; gap: 16px; grid-template-columns: repeat(4, 1fr); }
.stat .ic { width: 44px; height: 44px; border-radius: 13px; display: grid; place-items: center; margin-bottom: 12px; font-size: 22px; }
.ic-clay { background: rgba(193,95,60,0.18); }
.ic-slate { background: rgba(61,105,143,0.20); }
.ic-sage { background: rgba(64,119,83,0.20); }
.ic-honey { background: rgba(176,122,46,0.20); }
.stat .label { font-size: 13px; font-weight: 700; color: var(--on-muted); text-transform: uppercase; letter-spacing: .4px; }
.stat .value { font-family: var(--font-display); font-weight: 800; font-size: 38px; line-height: 1.1; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat .hint { font-size: 12.5px; color: var(--on-muted); font-weight: 600; margin-top: 4px; }

/* ---------- Charts grid ---------- */
.grid-2 { display: grid; gap: 16px; grid-template-columns: 1.3fr 1fr; }
.grid-bars { display: grid; gap: 16px; grid-template-columns: repeat(2, 1fr); }
.card-title { font-size: 16px; margin-bottom: 4px; }
.card-sub { font-size: 13px; color: var(--on-muted); font-weight: 600; margin-bottom: 14px; }

/* canvas wrapper keeps a stable height for the hand-rolled chart */
.chart-wrap { position: relative; width: 100%; height: 280px; }
.chart-wrap canvas { width: 100%; height: 100%; display: block; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 12px; font-size: 13px; font-weight: 700; color: var(--on-muted); }
.legend span { display: inline-flex; align-items: center; gap: 7px; }
.legend i { width: 12px; height: 12px; border-radius: 4px; display: inline-block; }

/* horizontal CSS bar lists */
.barlist { display: flex; flex-direction: column; gap: 11px; }
.barlist .row { display: grid; grid-template-columns: 1fr; gap: 5px; }
.barlist .row .top { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.barlist .row .name { font-weight: 700; font-size: 14px; color: var(--on-bg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.barlist .row .n { font-weight: 800; font-size: 14px; color: var(--on-muted); font-variant-numeric: tabular-nums; }
.barlist .track { height: 10px; border-radius: 50px; background: var(--surface-2); overflow: hidden; }
.barlist .fill { height: 100%; border-radius: 50px; background: linear-gradient(90deg, var(--slate-soft), var(--slate)); min-width: 3px; }
.barlist.clay .fill  { background: linear-gradient(90deg, var(--clay-soft), var(--clay)); }
.barlist.sage .fill  { background: linear-gradient(90deg, var(--sage-soft), var(--sage)); }
.barlist.honey .fill { background: linear-gradient(90deg, var(--honey-soft), var(--honey)); }

/* state messages (loading / empty / error) reused everywhere */
.state { padding: 22px 4px; text-align: center; color: var(--on-bg); font-weight: 700; }
.state small { display: block; margin-top: 4px; color: var(--on-muted); font-weight: 600; }
.state.err { color: var(--danger); }
.spinner {
  width: 20px; height: 20px; border-radius: 50%; margin: 0 auto 10px;
  border: 3px solid var(--surface-2); border-top-color: var(--clay-soft);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Devices table ---------- */
.table-wrap { overflow-x: auto; border-radius: var(--radius); }
table.devices { width: 100%; border-collapse: collapse; font-size: 14px; min-width: 720px; }
table.devices th, table.devices td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); }
table.devices th { font-family: var(--font-display); font-weight: 700; color: var(--on-muted); font-size: 12.5px; text-transform: uppercase; letter-spacing: .3px; position: sticky; top: 0; background: var(--surface); }
table.devices tbody tr { cursor: pointer; transition: background .12s ease; }
table.devices tbody tr:hover { background: var(--surface-2); }
table.devices td.num { text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; }
.tag {
  display: inline-block; padding: 3px 9px; border-radius: 50px; font-size: 12px; font-weight: 800;
  background: var(--surface-2); color: var(--on-bg); box-shadow: inset 0 0 0 1px var(--border);
}
.code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12.5px; color: var(--on-bg); }

/* ---------- Device drawer ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 60;
  opacity: 0; visibility: hidden; transition: opacity .2s ease, visibility .2s ease;
}
.overlay.open { opacity: 1; visibility: visible; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 61;
  width: min(520px, 100%); background: var(--bg); border-left: 1px solid var(--border);
  box-shadow: -30px 0 60px -30px rgba(0,0,0,0.6);
  transform: translateX(100%); transition: transform .24s ease;
  display: flex; flex-direction: column;
}
.drawer.open { transform: translateX(0); }
.drawer-head { display: flex; align-items: flex-start; gap: 12px; padding: 20px 22px; border-bottom: 1px solid var(--border); }
.drawer-head h2 { font-size: 18px; }
.drawer-head .sub { font-size: 13px; color: var(--on-muted); font-weight: 700; margin-top: 3px; word-break: break-all; }
.drawer-head .x { margin-left: auto; background: var(--surface-2); border: none; color: var(--on-bg); width: 34px; height: 34px; border-radius: 10px; font-size: 18px; cursor: pointer; flex: 0 0 auto; }
.drawer-body { padding: 20px 22px; overflow-y: auto; flex: 1; }
.drawer-foot { padding: 16px 22px; border-top: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; gap: 12px; }

.minis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 20px; }
.mini { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.mini .label { font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: .3px; color: var(--on-muted); }
.mini .value { font-family: var(--font-display); font-weight: 800; font-size: 26px; margin-top: 2px; font-variant-numeric: tabular-nums; }

.subhead { font-family: var(--font-display); font-weight: 700; font-size: 14px; text-transform: uppercase; letter-spacing: .4px; color: var(--on-muted); margin: 18px 0 10px; }

/* recent-events timeline */
.timeline { display: flex; flex-direction: column; gap: 0; }
.tl-item { display: grid; grid-template-columns: 84px 1fr; gap: 12px; padding: 10px 0; border-bottom: 1px dashed var(--border); }
.tl-item:last-child { border-bottom: none; }
.tl-time { font-size: 12px; font-weight: 700; color: var(--on-muted); font-variant-numeric: tabular-nums; }
.tl-main .type { display: inline-block; padding: 2px 8px; border-radius: 50px; font-size: 11.5px; font-weight: 800; background: var(--surface-2); color: var(--on-bg); box-shadow: inset 0 0 0 1px var(--border); }
.tl-main .name { font-weight: 700; font-size: 14px; margin-top: 4px; word-break: break-word; }
.tl-main .val { font-size: 13px; color: var(--on-muted); font-weight: 600; }

.kv { display: grid; grid-template-columns: minmax(120px, auto) 1fr; gap: 6px 14px; font-size: 13px; }
.kv dt { font-weight: 800; color: var(--on-muted); }
.kv dd { color: var(--on-bg); word-break: break-word; }

.footnote { margin-top: 26px; font-size: 12.5px; color: var(--on-muted); font-weight: 600; text-align: center; }

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
  .cards { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .controls { margin-left: 0; width: 100%; }
  .controls .field, .controls .input { width: 100%; }
  .input, .input--wide { min-width: 0; }
  .grid-bars { grid-template-columns: 1fr; }
  .cards { grid-template-columns: 1fr; }
}
