:root {
  color-scheme: light;
  --bg: #f5f7f4;
  --surface: #ffffff;
  --ink: #17211c;
  --muted: #627167;
  --line: #d9e1d8;
  --teal: #187f73;
  --green: #1d8c55;
  --amber: #b36b10;
  --rose: #bd3b48;
  --blue: #3568a8;
  --shadow: 0 18px 46px rgba(23, 33, 28, 0.08);
}

* {
  box-sizing: border-box;
  letter-spacing: 0;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(24, 127, 115, 0.08), rgba(245, 247, 244, 0) 360px),
    var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "PingFang SC", "Microsoft YaHei", sans-serif;
}

button,
canvas,
table {
  font: inherit;
}

.app-shell {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 28px 0 40px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}

.topbar h1,
.panel-heading h2 {
  margin: 0;
  line-height: 1.1;
}

.topbar h1 {
  font-size: 2rem;
  font-weight: 800;
}

.panel-heading h2 {
  font-size: 1.1rem;
  font-weight: 760;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.connection,
.status-badge,
.target-range,
.refresh-button {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
  white-space: nowrap;
}

.chart-tools {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 10px;
}

.range-selector {
  min-height: 36px;
  display: inline-grid;
  grid-template-columns: repeat(3, minmax(56px, 1fr));
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
}

.range-selector button {
  min-width: 0;
  min-height: 28px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 760;
  line-height: 1;
  white-space: nowrap;
}

.range-selector button:hover {
  color: var(--teal);
}

.range-selector button[aria-pressed="true"] {
  background: rgba(24, 127, 115, 0.1);
  color: var(--teal);
}

.connection,
.status-badge,
.target-range {
  padding: 0 12px;
}

.dashboard {
  display: grid;
  grid-template-columns: minmax(300px, 0.9fr) minmax(420px, 1.35fr);
  gap: 16px;
  align-items: stretch;
}

.current-panel,
.chart-panel,
.history-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 20px;
}

.current-panel {
  display: flex;
  flex-direction: column;
  min-height: 430px;
}

.chart-panel {
  min-height: 430px;
}

.history-panel {
  grid-column: 1 / -1;
  min-height: 248px;
}

.panel-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

.status-badge {
  color: var(--teal);
  border-color: rgba(24, 127, 115, 0.24);
  background: rgba(24, 127, 115, 0.09);
}

body[data-status="low"] .status-badge {
  color: var(--amber);
  border-color: rgba(179, 107, 16, 0.28);
  background: rgba(179, 107, 16, 0.1);
}

body[data-status="high"] .status-badge {
  color: var(--rose);
  border-color: rgba(189, 59, 72, 0.28);
  background: rgba(189, 59, 72, 0.1);
}

.reading-lockup {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 18px 0 24px;
}

.reading-value {
  min-width: 0;
  color: var(--ink);
  font-size: 5rem;
  font-weight: 850;
  line-height: 1;
}

.reading-unit {
  color: var(--muted);
  font-size: 1rem;
  font-weight: 740;
}

.meta-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: auto;
}

.meta-grid div {
  min-height: 74px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #fbfcfb;
}

.meta-grid span {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.meta-grid strong {
  display: block;
  overflow-wrap: anywhere;
  font-size: 0.95rem;
  line-height: 1.35;
}

.chart-wrap {
  position: relative;
  width: 100%;
  min-height: 330px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
  overflow: hidden;
  touch-action: none;
}

#glucoseChart {
  display: block;
  width: 100%;
  height: 330px;
}

.chart-tooltip {
  position: absolute;
  z-index: 2;
  min-width: 148px;
  max-width: min(220px, calc(100% - 24px));
  padding: 10px 12px;
  border: 1px solid rgba(24, 127, 115, 0.24);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 12px 28px rgba(23, 33, 28, 0.14);
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 720;
  line-height: 1.35;
  pointer-events: none;
  transform: translate(-50%, calc(-100% - 14px));
}

.chart-tooltip::after {
  position: absolute;
  left: 50%;
  bottom: -6px;
  width: 10px;
  height: 10px;
  border-right: 1px solid rgba(24, 127, 115, 0.24);
  border-bottom: 1px solid rgba(24, 127, 115, 0.24);
  background: rgba(255, 255, 255, 0.96);
  content: "";
  transform: translateX(-50%) rotate(45deg);
}

.chart-tooltip.is-below {
  transform: translate(-50%, 14px);
}

.chart-tooltip.is-below::after {
  top: -6px;
  bottom: auto;
  transform: translateX(-50%) rotate(225deg);
}

.chart-tooltip span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
}

.chart-tooltip strong {
  display: block;
  margin-top: 3px;
  color: var(--teal);
  font-size: 1rem;
  font-weight: 840;
}

.refresh-button {
  cursor: pointer;
  padding: 0 14px;
}

.refresh-button:hover {
  border-color: rgba(24, 127, 115, 0.34);
  color: var(--teal);
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 13px 10px;
  text-align: left;
  vertical-align: middle;
}

th {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 760;
}

td {
  font-size: 0.95rem;
  font-weight: 650;
}

td:nth-child(2) {
  color: var(--blue);
}

tr:last-child td {
  border-bottom: 0;
}

.tone-low {
  color: var(--amber);
}

.tone-in_range {
  color: var(--green);
}

.tone-high {
  color: var(--rose);
}

@media (max-width: 860px) {
  .app-shell {
    width: min(100% - 24px, 680px);
    padding-top: 20px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .dashboard {
    grid-template-columns: 1fr;
  }

  .current-panel,
  .chart-panel {
    min-height: 0;
  }

  .reading-value {
    font-size: 4rem;
  }
}

@media (max-width: 520px) {
  .panel-heading,
  .reading-lockup {
    align-items: flex-start;
    flex-direction: column;
  }

  .chart-tools {
    width: 100%;
    align-items: flex-start;
    justify-content: flex-start;
  }

  .range-selector {
    width: 100%;
    max-width: 260px;
  }

  .meta-grid {
    grid-template-columns: 1fr;
  }

  .reading-value {
    font-size: 3.2rem;
  }

  th,
  td {
    padding-left: 6px;
    padding-right: 6px;
  }
}
