:root {
  --ink: #16211f;
  --muted: #4c5a56;
  --paper: #f8f5ef;
  --card: rgba(255, 252, 246, 0.88);
  --line: rgba(29, 38, 35, 0.16);
  --accent: #d65a2c;
  --accent-2: #1a7f6b;
  --accent-3: #2f5a95;
  --ok: #2d7b48;
  --warn: #b35223;
  --danger: #a6352a;
  --shadow: 0 20px 42px rgba(16, 20, 18, 0.13);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: "Avenir Next", "Gill Sans", "Segoe UI", sans-serif;
  background: radial-gradient(1200px 500px at 0% 0%, #f6bfa8 0%, transparent 60%),
    radial-gradient(850px 450px at 100% 0%, #9adbc5 0%, transparent 56%),
    linear-gradient(180deg, #f8f3e8 0%, #eef4ef 100%);
  min-height: 100vh;
}

.bg-layer {
  position: fixed;
  inset: 0;
  background-image: linear-gradient(rgba(22, 33, 31, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(22, 33, 31, 0.03) 1px, transparent 1px);
  background-size: 24px 24px;
  pointer-events: none;
  z-index: -1;
}

.shell {
  width: min(1420px, calc(100% - 2rem));
  margin: 0 auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: end;
  padding: 1.5rem 0 1rem;
}

.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
}

h1 {
  margin: 0.1rem 0 0.4rem;
  font-size: clamp(1.8rem, 3vw, 2.8rem);
  line-height: 1.04;
  font-family: "Avenir Next Condensed", "Gill Sans", "Segoe UI", sans-serif;
}

.meta {
  margin: 0;
  color: var(--muted);
}

.file-controls {
  display: flex;
  gap: 0.6rem;
  align-items: center;
}

#csvInput {
  display: none;
}

.btn {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.55rem 1rem;
  font-size: 0.92rem;
  cursor: pointer;
  transition: transform 120ms ease, background-color 120ms ease, border-color 120ms ease;
}

.btn:hover {
  transform: translateY(-1px);
}

.btn-primary {
  background: linear-gradient(145deg, #d65a2c 0%, #be4720 100%);
  color: #fff;
  border-color: rgba(151, 54, 23, 0.9);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.7);
  color: var(--ink);
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(4px);
}

.month-nav {
  margin-top: 0.2rem;
  display: grid;
  grid-template-columns: auto minmax(220px, 1fr) auto 1fr;
  gap: 0.8rem;
  align-items: end;
  padding: 1rem;
}

.select-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  color: var(--muted);
  font-size: 0.85rem;
}

#monthSelect {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.45rem 0.6rem;
  background: #fff;
  color: var(--ink);
}

.coverage {
  margin: 0;
  color: var(--muted);
  justify-self: end;
  text-align: right;
}

.stats-grid {
  margin-top: 1rem;
  display: grid;
  grid-template-columns: repeat(7, minmax(120px, 1fr));
  gap: 0.8rem;
}

.stat-card {
  padding: 0.95rem 1rem;
}

.stat-card h2 {
  margin: 0;
  color: var(--muted);
  font-size: 0.88rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-card p {
  margin: 0.28rem 0 0;
  font-size: 1.55rem;
  font-weight: 700;
}

.stat-card small {
  color: var(--muted);
}

.content-grid {
  margin-top: 1rem;
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.8rem;
  margin-bottom: 0.8rem;
}

.section-head h2,
.section-head h3 {
  margin: 0;
}

.section-head p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  text-align: right;
}

.table-wrap {
  max-height: 520px;
  overflow: auto;
  border-radius: 12px;
  border: 1px solid var(--line);
}

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

thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: linear-gradient(180deg, #eef5f1, #e5f0ea);
  color: #25332f;
  text-align: left;
  padding: 0.62rem 0.5rem;
  border-bottom: 1px solid var(--line);
}

tbody td {
  padding: 0.52rem 0.5rem;
  border-bottom: 1px solid rgba(29, 38, 35, 0.08);
  white-space: nowrap;
}

tbody td.value-high {
  background: rgba(166, 53, 42, 0.18);
  color: #7e271f;
  font-weight: 700;
}

tbody td.value-low {
  background: rgba(42, 109, 163, 0.18);
  color: #20517d;
  font-weight: 700;
}

tbody tr {
  cursor: pointer;
  transition: background-color 100ms ease;
}

tbody tr:hover {
  background: rgba(35, 90, 149, 0.06);
}

tbody tr.selected {
  background: rgba(214, 90, 44, 0.18);
}

tbody tr.day-empty {
  color: #7d8a85;
}

.stack-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 0.4rem;
  max-height: 220px;
  overflow: auto;
}

.stack-list li {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.5rem 0.65rem;
  background: rgba(255, 255, 255, 0.7);
  line-height: 1.35;
}

.detail-panel {
  margin-top: 1rem;
  padding: 1rem;
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

.detail-bottom-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-top: 1rem;
}

.detail-head {
  margin-bottom: 0.7rem;
}

h3 {
  margin: 0.8rem 0 0.45rem;
}

.chart-box {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: linear-gradient(180deg, #fffdf8 0%, #f5faf8 100%);
  min-height: 300px;
  display: grid;
  align-items: center;
  position: relative;
}

.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(20, 34, 48, 0.9);
  color: #f8fbff;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 8px;
  padding: 0.28rem 0.46rem;
  font-size: 0.8rem;
  line-height: 1.2;
  white-space: nowrap;
  transform: translate(-50%, -118%);
  z-index: 5;
  box-shadow: 0 8px 18px rgba(8, 14, 20, 0.25);
}

.chart-empty {
  text-align: center;
  color: var(--muted);
  padding: 1rem;
}

.chart-legend {
  margin-top: 0.55rem;
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 0.86rem;
}

.legend-swatch {
  width: 14px;
  height: 10px;
  display: inline-block;
  margin-right: 0.35rem;
  border-radius: 2px;
  vertical-align: middle;
}

.legend-line {
  background: var(--accent-3);
}

.legend-scan {
  background: #325f9a;
  border-radius: 50%;
}

.legend-fast {
  background: #df6a36;
}

.legend-depot {
  background: #2d8f74;
}

.legend-insulin-line {
  width: 14px;
  height: 0;
  border-top: 3px dashed #d06a37;
  margin-top: 5px;
}

.legend-note-line {
  width: 14px;
  height: 0;
  border-top: 3px dashed #7a2f6d;
  margin-top: 5px;
}

@media (max-width: 1160px) {
  .stats-grid {
    grid-template-columns: repeat(4, minmax(120px, 1fr));
  }

  .content-grid,
  .detail-grid,
  .detail-bottom-grid {
    grid-template-columns: 1fr;
  }

  .table-wrap {
    max-height: 440px;
  }
}

@media (max-width: 760px) {
  .topbar {
    flex-direction: column;
    align-items: flex-start;
  }

  .file-controls {
    width: 100%;
    flex-wrap: wrap;
  }

  .month-nav {
    grid-template-columns: 1fr;
  }

  .coverage {
    justify-self: start;
    text-align: left;
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(130px, 1fr));
  }
}
