:root {
  color-scheme: light;
  --bg: #f1f0ed;
  --surface: #fcfcfb;
  --surface-2: #f4f3f0;
  --border: #e2e1dc;
  --grid: #e8e7e3;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --accent-wash: rgba(42, 120, 214, 0.10);
  --accent-ink: #ffffff;
  --trend: #eb6834;
  --danger: #c93a39;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 2px 8px rgba(0, 0, 0, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #111110;
    --surface: #1a1a19;
    --surface-2: #232322;
    --border: #2e2e2c;
    --grid: #2a2a28;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #8f8e86;
    --accent: #3987e5;
    --accent-wash: rgba(57, 135, 229, 0.12);
    --trend: #d95926;
    --danger: #e66767;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  padding: env(safe-area-inset-top, 0px) 0 env(safe-area-inset-bottom, 0px);
}

.app {
  max-width: 680px;
  margin: 0 auto;
  padding: 12px 16px 32px;
}

h1, h2 { margin: 0; letter-spacing: -0.01em; }
h1 { font-size: 26px; font-weight: 700; }
h2 { font-size: 16px; font-weight: 600; }

.muted { color: var(--muted); font-size: 13px; }

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0 16px;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

/* Stats */
.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}
@media (max-width: 560px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  box-shadow: var(--shadow);
}
.stat-label { font-size: 12px; color: var(--text-2); font-weight: 500; }
.stat-value {
  font-size: 22px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stat-value small { font-size: 13px; font-weight: 500; color: var(--text-2); margin-left: 2px; }
.stat-sub { font-size: 12px; color: var(--muted); min-height: 1.2em; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

/* Form */
.entry-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; border: 0; padding: 0; margin: 0; }
.field > span, .field > legend { font-size: 13px; color: var(--text-2); font-weight: 500; padding: 0; }
.field em { font-style: normal; color: var(--muted); }
.field-wide { grid-column: 1 / -1; }

input[type="date"], input[type="number"], input[type="text"] {
  width: 100%;
  min-width: 0;
  height: 44px;
  padding: 0 12px;
  font: inherit;
  font-size: 16px; /* prevents iOS zoom */
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
input:focus-visible { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-wash); }
#weight { font-variant-numeric: tabular-nums; font-weight: 600; }

.form-actions {
  grid-column: 1 / -1;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.btn {
  height: 40px;
  padding: 0 16px;
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
}
.btn:hover { filter: brightness(0.97); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); min-width: 120px; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-danger { color: var(--danger); }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }

/* Segmented control */
.seg {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 2px;
  gap: 2px;
}
.seg button, .seg label {
  border: 0;
  background: transparent;
  color: var(--text-2);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 8px;
  cursor: pointer;
  text-align: center;
}
.seg button[aria-pressed="true"], .seg label:has(input:checked) {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg-full { display: flex; }
.seg-full label { flex: 1; padding: 8px; }

/* Chart */
.chart-wrap { position: relative; min-height: 240px; }
#chart { display: block; width: 100%; height: 240px; touch-action: pan-y; overflow: visible; }
#chart text { font: 11px system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; fill: var(--muted); font-variant-numeric: tabular-nums; }
#chart .grid { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
#chart .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
#chart .area { fill: var(--accent-wash); }
#chart .dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
#chart .goal { stroke: var(--text-2); stroke-width: 1; shape-rendering: crispEdges; opacity: 0.6; }
#chart text.goal-label { fill: var(--text-2); font-weight: 600; }
#chart text.end-label { fill: var(--text); font-weight: 650; font-size: 12px; }
#chart .cross { stroke: var(--text-2); stroke-width: 1; opacity: 0.5; }
#chart .trend { fill: none; stroke: var(--trend); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
#chart .hover-trend { fill: var(--trend); stroke: var(--surface); stroke-width: 3; }
#chart .hover-dot { fill: var(--accent); stroke: var(--surface); stroke-width: 3; }

.legend { display: flex; gap: 16px; margin: -4px 0 8px; font-size: 12px; color: var(--text-2); }
.legend[hidden] { display: none; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { display: inline-block; width: 14px; height: 3px; border-radius: 2px; }
.sw-weight { background: var(--accent); }
.sw-trend { background: var(--trend); }
.tooltip .sw { margin-right: 6px; vertical-align: middle; }
.tt-trend { color: var(--text-2); font-variant-numeric: tabular-nums; }

.height-row { display: flex; align-items: center; gap: 8px; }
.height-row[hidden] { display: none; }
.height-row input { flex: 1; }
.height-row .suffix { color: var(--muted); font-size: 14px; }

.tooltip {
  position: absolute;
  top: 0;
  pointer-events: none;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  white-space: nowrap;
  max-width: 220px;
  z-index: 2;
}
.tooltip strong { display: block; font-size: 15px; font-variant-numeric: tabular-nums; }
.tooltip .tt-date { color: var(--text-2); }
.tooltip .tt-note { color: var(--muted); white-space: normal; margin-top: 2px; }

.empty { color: var(--muted); text-align: center; margin: 24px 0; }
.chart-wrap .empty { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; }
.chart-wrap .empty[hidden] { display: none; }

/* History */
.history { list-style: none; margin: 0; padding: 0; }
.history li {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-top: 1px solid var(--grid);
}
.history li:first-child { border-top: 0; }
.h-main { min-width: 0; }
.h-date { font-weight: 500; }
.h-note { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.h-weight { text-align: right; font-variant-numeric: tabular-nums; }
.h-weight b { font-size: 16px; }
.h-delta { display: block; font-size: 12px; color: var(--muted); }
.h-actions { display: flex; gap: 2px; }
.h-actions .icon-btn { width: 34px; height: 34px; color: var(--muted); }
.h-actions .icon-btn:hover { color: var(--text); }

.footnote { color: var(--muted); font-size: 12px; text-align: center; margin: 8px 0 0; }

/* Settings dialog */
.sheet {
  width: min(460px, calc(100vw - 32px));
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.35); }
.sheet-body { display: flex; flex-direction: column; gap: 18px; padding: 20px; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
  z-index: 10;
  max-width: calc(100vw - 32px);
}
