:root {
  --ink: #1A1A1A;
  --muted: #555555;
  --paper: #FFFFFF;
  --card: #FFFFFF;
  --line: #888888;
  --accent: #8B0000;
  --accent-2: #A11212;
  --accent-3: #D4AF37;
  --ok: #2E8B57;
  --warn: #E68A00;
  --danger: #B22222;
  --shadow: 5px 5px 1px rgba(0, 0, 0, 0.16);
}

* {
  box-sizing: border-box;
  border-radius: 2px !important;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: 'Inter', Arial, Helvetica, sans-serif;
  background: #FFFFFF;
  min-height: 100vh;
}

.bg-layer {
  display: none !important;
}

h1, h2, h3, h4, h5, h6 {
  font-family: 'Gidole', 'Inter', Arial, Helvetica, sans-serif !important;
  color: #8B0000 !important;
}

.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);
}

.disclaimer {
  max-width: 820px;
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.35;
}

.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:disabled,
.btn:disabled:hover {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.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);
}

.export-dialog {
  width: min(560px, calc(100vw - 2rem));
  max-height: min(720px, calc(100vh - 2rem));
  padding: 0;
  color: var(--ink);
  background: #fffdf8;
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: 0 24px 60px rgba(16, 20, 18, 0.24);
}

.export-dialog::backdrop {
  background: rgba(22, 33, 31, 0.38);
  backdrop-filter: blur(2px);
}

.export-dialog form {
  display: grid;
  gap: 1rem;
  padding: 1rem;
}

.dialog-head {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: start;
}

.dialog-head h2 {
  margin: 0.1rem 0 0;
  font-size: 1.4rem;
}

.dialog-icon-btn {
  width: 2rem;
  height: 2rem;
  display: grid;
  place-items: center;
  padding: 0;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--line);
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.2rem;
  line-height: 1;
}

.dialog-actions,
.dialog-footer,
.dialog-footer > div {
  display: flex;
  gap: 0.55rem;
  align-items: center;
  flex-wrap: wrap;
}

.dialog-footer {
  justify-content: space-between;
}

.dialog-footer p {
  margin: 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.month-check-list {
  max-height: 360px;
  overflow: auto;
  display: grid;
  gap: 0.35rem;
  padding: 0.35rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.62);
}

.month-check {
  display: flex;
  gap: 0.55rem;
  align-items: center;
  padding: 0.45rem 0.55rem;
  border-radius: 8px;
}

.month-check:hover {
  background: rgba(26, 127, 107, 0.08);
}

.month-check input {
  width: 1rem;
  height: 1rem;
  accent-color: var(--accent-2);
}

.month-nav {
  margin-top: 0.2rem;
  display: grid;
  grid-template-columns: auto minmax(220px, 1fr) auto auto 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));
  }
}

body.pdf-exporting .table-wrap,
body.pdf-exporting .stack-list {
  max-height: none !important;
  overflow: visible !important;
}

.pdf-export-root {
  overflow-x: hidden;
}

.pdf-export-root * {
  max-width: 100%;
}

.pdf-export-root .shell {
  width: 100%;
  margin: 0;
}

.pdf-export-root .topbar {
  padding: 0 0 0.6rem;
}

.pdf-export-root h1 {
  font-size: 2rem;
}

.pdf-export-root .meta,
.pdf-export-root .coverage,
.pdf-export-root .section-head p {
  font-size: 0.78rem;
}

.pdf-export-root .card {
  border-radius: 8px;
  box-shadow: none;
  backdrop-filter: none;
  background: #ffffff;
}

.pdf-export-root .month-nav,
.pdf-export-root .stats-grid,
.pdf-export-root .content-grid,
.pdf-export-root .detail-panel {
  margin-top: 0.55rem;
}

.pdf-export-root .month-nav {
  grid-template-columns: 1fr;
  padding: 0.55rem;
}

.pdf-export-root .stats-grid {
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 0.35rem;
}

.pdf-export-root .stat-card {
  padding: 0.45rem 0.5rem;
}

.pdf-export-root .stat-card h2 {
  font-size: 0.62rem;
}

.pdf-export-root .stat-card p {
  font-size: 1.02rem;
}

.pdf-export-root .stat-card small {
  font-size: 0.64rem;
}

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

.pdf-export-root .table-wrap,
.pdf-export-root .stack-list,
.pdf-export-root .chart-box {
  max-height: none !important;
  overflow-x: hidden !important;
  overflow-y: visible !important;
}

.pdf-export-root table {
  table-layout: fixed;
  font-size: 0.7rem;
}

.pdf-export-root th:nth-child(1),
.pdf-export-root td:nth-child(1) {
  width: 8%;
}

.pdf-export-root th:nth-child(2),
.pdf-export-root td:nth-child(2),
.pdf-export-root th:nth-child(3),
.pdf-export-root td:nth-child(3),
.pdf-export-root th:nth-child(4),
.pdf-export-root td:nth-child(4),
.pdf-export-root th:nth-child(5),
.pdf-export-root td:nth-child(5),
.pdf-export-root th:nth-child(6),
.pdf-export-root td:nth-child(6),
.pdf-export-root th:nth-child(7),
.pdf-export-root td:nth-child(7) {
  width: 6%;
}

.pdf-export-root th:nth-child(8),
.pdf-export-root td:nth-child(8) {
  width: 7%;
}

.pdf-export-root th:nth-child(9),
.pdf-export-root td:nth-child(9) {
  width: 9%;
}

.pdf-export-root th:nth-child(10),
.pdf-export-root td:nth-child(10) {
  width: 40%;
  white-space: normal;
  overflow-wrap: normal;
  word-break: normal;
}

.pdf-export-root thead th {
  position: static;
}

.pdf-export-root th,
.pdf-export-root td {
  padding: 0.24rem 0.18rem;
  white-space: normal;
  overflow-wrap: anywhere;
  vertical-align: top;
}

.pdf-export-root .section-head {
  margin-bottom: 0.45rem;
}

.pdf-export-root .detail-panel {
  break-before: page;
  page-break-before: always;
  break-inside: avoid;
  page-break-inside: avoid;
  padding: 0.55rem;
}

.pdf-export-root h3 {
  margin: 0.45rem 0 0.25rem;
  font-size: 0.9rem;
}

.pdf-export-root .chart-box {
  display: block;
  min-height: 0;
}

.pdf-export-root .chart-box svg {
  display: block;
  width: 100%;
  height: auto;
}

.pdf-export-root .monthly-pdf-chart {
  min-height: 0;
}

.pdf-export-root .chart-legend,
.pdf-export-root .stack-list,
.pdf-export-root .stack-list li {
  font-size: 0.78rem;
}

.legend-month-min,
.legend-month-median,
.legend-month-max {
  height: 0;
  border-radius: 0;
  margin-bottom: 3px;
}

.legend-month-min {
  border-top: 3px solid #1a7f6b;
}

.legend-month-median {
  border-top: 3px solid #2f5a95;
}

.legend-month-max {
  border-top: 3px solid #a6352a;
}

.legend-month-target {
  background: rgba(45, 123, 72, 0.2);
  border: 1px solid rgba(45, 123, 72, 0.35);
}

.pdf-report-root {
  box-sizing: border-box;
  overflow: hidden;
  color: #16211f;
  background: #ffffff;
  font-family: "Avenir Next", "Gill Sans", "Segoe UI", sans-serif;
}

.pdf-report-root * {
  box-sizing: border-box;
}

.pdf-report-header {
  border-bottom: 1px solid rgba(29, 38, 35, 0.18);
  margin-bottom: 0.55rem;
  padding-bottom: 0.45rem;
}

.pdf-report-eyebrow {
  margin: 0 0 0.12rem;
  color: #4c5a56;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.pdf-report-header h1 {
  margin: 0 0 0.2rem;
  font-size: 1.72rem;
  line-height: 1.05;
}

.pdf-report-meta {
  margin: 0;
  color: #4c5a56;
  font-size: 0.7rem;
}

.pdf-month-section {
  break-after: page;
  page-break-after: always;
}

.pdf-month-section:last-child {
  break-after: auto;
  page-break-after: auto;
}

.pdf-month-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.75rem;
  margin-bottom: 0.45rem;
}

.pdf-month-header h2 {
  margin: 0;
  font-size: 1.28rem;
}

.pdf-month-header p {
  margin: 0;
  color: #4c5a56;
  font-size: 0.68rem;
  text-align: right;
}

.pdf-month-stats {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 0.3rem;
  margin-bottom: 0.45rem;
}

.pdf-stat {
  border: 1px solid rgba(29, 38, 35, 0.18);
  border-radius: 6px;
  padding: 0.34rem 0.4rem;
}

.pdf-stat span {
  display: block;
  color: #4c5a56;
  font-size: 0.52rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.pdf-stat strong {
  display: block;
  margin-top: 0.12rem;
  font-size: 0.9rem;
}

.pdf-stat small {
  display: block;
  margin-top: 0.05rem;
  color: #4c5a56;
  font-size: 0.5rem;
}

.pdf-month-table-wrap {
  overflow: hidden;
  border: 1px solid rgba(29, 38, 35, 0.16);
  border-radius: 6px;
}

.pdf-month-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 0.62rem;
}

.pdf-month-table th {
  background: #eaf3ef;
  color: #25332f;
  text-align: left;
  padding: 0.28rem 0.24rem;
  border-bottom: 1px solid rgba(29, 38, 35, 0.16);
}

.pdf-month-table td {
  padding: 0.22rem 0.24rem;
  border-bottom: 1px solid rgba(29, 38, 35, 0.08);
  vertical-align: top;
  overflow-wrap: anywhere;
}

.pdf-month-table th:nth-child(1),
.pdf-month-table td:nth-child(1) {
  width: 8%;
}

.pdf-month-table th:nth-child(2),
.pdf-month-table td:nth-child(2),
.pdf-month-table th:nth-child(3),
.pdf-month-table td:nth-child(3),
.pdf-month-table th:nth-child(4),
.pdf-month-table td:nth-child(4),
.pdf-month-table th:nth-child(5),
.pdf-month-table td:nth-child(5),
.pdf-month-table th:nth-child(6),
.pdf-month-table td:nth-child(6),
.pdf-month-table th:nth-child(7),
.pdf-month-table td:nth-child(7) {
  width: 6%;
}

.pdf-month-table th:nth-child(8),
.pdf-month-table td:nth-child(8) {
  width: 8%;
}

.pdf-month-table th:nth-child(9),
.pdf-month-table td:nth-child(9) {
  width: 9%;
}

.pdf-month-table th:nth-child(10),
.pdf-month-table td:nth-child(10) {
  width: 39%;
  overflow-wrap: normal;
  word-break: normal;
}

.pdf-month-chart-block {
  break-before: page;
  page-break-before: always;
  break-inside: avoid;
  page-break-inside: avoid;
}

.pdf-month-chart-block h3 {
  margin: 0 0 0.35rem;
  font-size: 0.95rem;
}

.pdf-month-chart-box {
  overflow: hidden;
  border: 1px solid rgba(29, 38, 35, 0.16);
  border-radius: 6px;
  background: #ffffff;
}

.pdf-month-chart-box svg {
  display: block;
  width: 100%;
  height: auto;
}

.pdf-month-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.7rem;
  margin-top: 0.35rem;
  color: #4c5a56;
  font-size: 0.65rem;
}

.legend-month-range {
  height: 0;
  border-top: 3px dashed #7a2f6d;
  border-radius: 0;
  margin-bottom: 3px;
}

@media print {
  body {
    background: #ffffff !important;
  }

  .bg-layer {
    display: none !important;
  }

  .card {
    box-shadow: none !important;
    backdrop-filter: none !important;
    background: #ffffff !important;
  }

  .btn,
  .file-controls {
    display: none !important;
  }

  .shell {
    width: auto;
    margin: 0;
  }

  .table-wrap,
  .stack-list,
  .chart-box {
    max-height: none !important;
    overflow: visible !important;
  }

  thead th {
    position: static !important;
  }

  .chart-tooltip {
    display: none !important;
  }
}
