:root {
  color-scheme: dark;
  --bg: #181818;
  --panel: #222;
  --overlay: #292929;
  --border: #4a4a4a;
  --grid: #3b3b3b;
  --axis: #777;
  --text: #e8e8e8;
  --muted: #aaa;
  --weekly: #9ba9bb;
  --accent: #4d8cbd;
  --green: #62b879;
  --red: #e47770;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f3f4f6;
    --panel: #ffffff;
    --overlay: #ffffff;
    --border: #d0d4da;
    --grid: #e6e8eb;
    --axis: #9aa0a8;
    --text: #1d232b;
    --muted: #5f6873;
    --weekly: #4b5a6e;
    --accent: #286fa8;
    --green: #2f8a4b;
    --red: #c4473f;
  }
}

* { box-sizing: border-box; }

html, body { min-width: 320px; min-height: 100%; }

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

button { font: inherit; }
[hidden] { display: none !important; }

.app-shell { display: flex; flex-direction: column; min-height: 100vh; min-height: 100dvh; }

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  height: 52px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.brand { margin: 0; font-size: 1.05rem; font-weight: 700; white-space: nowrap; }
.icon-button {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  color: var(--text);
  background: transparent;
  font-size: 1.05rem;
  cursor: pointer;
}
.icon-button:hover { border-color: var(--accent); }
.icon-button.loading { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  padding: 8px 8px 0;
}
.stat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
}
.stat small { color: var(--muted); font-size: 0.68rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; }
.stat strong { font-size: 1.35rem; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat span { overflow: hidden; color: var(--muted); font-size: 0.78rem; text-overflow: ellipsis; white-space: nowrap; }
.stat strong.down { color: var(--green); }
.stat strong.up { color: var(--red); }
.stat-meta span:first-of-type { margin-top: 3px; color: var(--text); }

.plot-panel { position: relative; display: flex; flex: 1; flex-direction: column; min-height: 0; padding: 8px; }
.plot-wrap {
  position: relative;
  flex: 1;
  width: 100%;
  min-height: 420px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
}
.weight-plot { position: absolute; inset: 0; }
.empty-state { position: absolute; inset: 0; display: grid; place-content: center; gap: 3px; padding: 16px; color: var(--muted); text-align: center; pointer-events: none; }
.empty-state strong { color: var(--text); font-size: 1rem; }

.legend-key { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: 6px 2px 0; color: var(--muted); font-size: 0.76rem; }
.key-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #4e79a7; }
.key-line { display: inline-block; width: 18px; height: 2px; margin-left: 8px; background: var(--weekly); }

.chart-overlay {
  position: absolute;
  z-index: 20;
  top: 48px;
  left: 70px;
  max-width: min(360px, calc(100% - 32px));
  border: 1px solid var(--axis);
  border-radius: 6px;
  background: var(--overlay);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
}

.next-point-prompt {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 2px 10px;
  padding: 10px 13px;
  color: #f3e7b0;
  background: #3b351e;
  border-color: #9b8434;
  pointer-events: none;
}
.next-point-prompt div { display: flex; flex-wrap: wrap; gap: 7px; }
.next-point-prompt div span { color: #d4c681; }
.next-point-prompt > b { grid-column: 2; font-size: 0.84rem; }
.prompt-dot { grid-row: 1 / 3; width: 9px; height: 9px; border-radius: 50%; background: #b89600; }

.trend-card { width: 340px; padding: 12px 14px 13px; }
.close-button {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--muted);
  background: transparent;
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
}
.close-button:hover { border-color: var(--border); }
.trend-label { display: block; margin-bottom: 1px; color: var(--muted); font-size: 0.68rem; font-weight: 700; letter-spacing: 0.08em; }
.trend-change { display: block; font-size: 1.6rem; line-height: 1.15; }
.trend-change.down { color: var(--green); }
.trend-change.up { color: var(--red); }
.trend-range { display: block; margin-top: 2px; color: var(--muted); font-size: 0.8rem; }
.trend-details { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 10px; padding-top: 9px; border-top: 1px solid var(--border); }
.trend-details span { display: flex; flex-direction: column; min-width: 0; }
.trend-details small { color: var(--muted); font-size: 0.69rem; }
.trend-details b { margin-top: 2px; font-size: 0.8rem; white-space: nowrap; }

.toast {
  position: absolute;
  z-index: 30;
  right: 20px;
  bottom: 20px;
  max-width: min(500px, calc(100% - 40px));
  padding: 9px 12px;
  border: 1px solid;
  border-radius: 6px;
  font-size: 0.84rem;
}
.error-message { color: #ffc4c0; border-color: #a75555; background: #422525; }

@media (max-width: 600px) {
  .toolbar { height: 48px; padding: 0 12px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; padding: 6px 6px 0; }
  .stat { padding: 6px 9px; }
  .stat strong { font-size: 1.15rem; }
  .stat span { white-space: normal; }
  .stat-meta { grid-column: 1 / -1; flex-direction: row; flex-wrap: wrap; gap: 4px 10px; padding: 4px 9px; }
  .stat-meta small { display: none; }
  .stat-meta span:first-of-type { margin-top: 0; }
  .legend-hint { display: none; }
  .plot-panel { padding: 6px; }
  .plot-wrap { min-height: 380px; }
  .chart-overlay { left: 8px; right: 8px; top: 44px; max-width: none; }
  .trend-card { width: auto; }
}
