:root {
  color-scheme: light dark;
  --color-bg: #f8fafc;
  --color-surface: #ffffff;
  --color-surface-muted: #f1f5f9;
  --color-border: #e2e8f0;
  --color-text: #0f172a;
  --color-text-muted: #64748b;
  --color-primary: #059669;
  --color-primary-hover: #047857;
  --color-primary-soft: #ecfdf5;
  --color-success: #16a34a;
  --color-warning: #d97706;
  --color-danger: #dc2626;
  --color-neutral: #94a3b8;
  --color-success-soft: #f0fdf4;
  --color-warning-soft: #fffbeb;
  --color-danger-soft: #fef2f2;
  --color-neutral-soft: #f1f5f9;
  --chart-weight-actual: #059669;
  --chart-weight-average: #3b82f6;
  --chart-goal-track: #f97316;
  --chart-trend: #8b5cf6;
  --chart-grid: #f1f5f9;
  --chart-axis: #94a3b8;
  --bg: var(--color-bg);
  --surface: var(--color-surface);
  --surface-soft: var(--color-surface-muted);
  --ink: var(--color-text);
  --muted: var(--color-text-muted);
  --line: var(--color-border);
  --accent: var(--color-primary);
  --accent-dark: var(--color-primary-hover);
  --blue: var(--chart-weight-average);
  --amber: var(--color-warning);
  --red: var(--color-danger);
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 8px 24px rgba(15, 23, 42, 0.07);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-bg: #0a0f1e;
    --color-surface: #111827;
    --color-surface-muted: #1a2233;
    --color-border: #1f2937;
    --color-text: #f1f5f9;
    --color-text-muted: #94a3b8;
    --color-primary: #34d399;
    --color-primary-hover: #6ee7b7;
    --color-primary-soft: #0a2416;
    --color-success: #34d399;
    --color-warning: #fbbf24;
    --color-danger: #f87171;
    --color-success-soft: #052e16;
    --color-warning-soft: #1c1507;
    --color-danger-soft: #1c0a0a;
    --color-neutral-soft: #1e293b;
    --chart-grid: #1a2233;
    --chart-axis: #64748b;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
    --bg: #0a0f1e;
    --surface: #111827;
    --surface-soft: #1a2233;
    --ink: #f1f5f9;
    --muted: #94a3b8;
    --line: #1f2937;
    --accent: #34d399;
    --accent-dark: #6ee7b7;
    --amber: #fbbf24;
    --red: #f87171;
  }
}

[data-theme="dark"] {
  --color-bg: #0a0f1e;
  --color-surface: #111827;
  --color-surface-muted: #1a2233;
  --color-border: #1f2937;
  --color-text: #f1f5f9;
  --color-text-muted: #94a3b8;
  --color-primary: #34d399;
  --color-primary-hover: #6ee7b7;
  --color-primary-soft: #0a2416;
  --color-success: #34d399;
  --color-warning: #fbbf24;
  --color-danger: #f87171;
  --color-success-soft: #052e16;
  --color-warning-soft: #1c1507;
  --color-danger-soft: #1c0a0a;
  --color-neutral-soft: #1e293b;
  --chart-grid: #1a2233;
  --chart-axis: #64748b;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
  --bg: #0a0f1e;
  --surface: #111827;
  --surface-soft: #1a2233;
  --ink: #f1f5f9;
  --muted: #94a3b8;
  --line: #1f2937;
  --accent: #34d399;
  --accent-dark: #6ee7b7;
  --amber: #fbbf24;
  --red: #f87171;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--color-bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  font-weight: 600;
}

button:disabled {
  cursor: wait;
  opacity: 0.62;
}

a {
  color: var(--accent-dark);
}

h1,
h2,
h3,
p {
  margin: 0;
}

h2 {
  font-size: clamp(1.1rem, 2vw, 1.5rem);
  font-weight: 600;
  letter-spacing: -0.01em;
}

/* ── Header ──────────────────────────────── */

.app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--color-bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  width: min(1180px, calc(100% - 32px));
  margin-inline: auto;
  height: 60px;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  min-width: 0;
}

.brand-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
  background: var(--color-primary-soft);
  display: grid;
  place-items: center;
  font-size: 18px;
  flex-shrink: 0;
}

.brand-title {
  display: block;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.2;
}

#headerSyncStatus {
  display: block;
  font-size: 0.7rem;
  font-weight: 500;
  color: var(--muted);
  line-height: 1.5;
}

#headerSyncStatus.sync-ok { color: var(--color-success); }
#headerSyncStatus.sync-warning { color: var(--amber); }
#headerSyncStatus.sync-error { color: var(--red); }

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
  background: var(--surface-soft);
  color: var(--ink);
}

.admin-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0 12px;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.admin-btn:hover {
  border-color: var(--accent);
  background: var(--color-primary-soft);
  color: var(--accent-dark);
}

/* ── Shell ───────────────────────────────── */

.app-shell {
  width: min(1180px, calc(100% - 32px));
  margin-inline: auto;
  display: grid;
  gap: 12px;
  padding: 20px 0 56px;
}

.panel,
.metric-card,
.goal-card,
.trend-stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.panel {
  padding: clamp(16px, 2vw, 22px);
}

/* ── Section headings ────────────────────── */

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

.section-heading.compact {
  align-items: center;
  margin-bottom: 12px;
}

.eyebrow {
  color: var(--accent-dark);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* ── Date pill (overview panel) ──────────── */

.date-pill {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--color-surface);
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 7px 12px;
  white-space: nowrap;
}

/* ── Overview panel ──────────────────────── */

.overview-panel {
  padding-bottom: 18px;
}

.overview-panel,
.goals-panel {
  border: 0;
  background: transparent;
  box-shadow: none;
  padding-inline: 0;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.metric-card {
  display: grid;
  align-content: start;
  gap: 8px;
  min-height: 148px;
  padding: 14px;
  box-shadow: none;
}

.metric-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: fit-content;
  min-height: 34px;
  margin-top: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--ink);
  padding: 0 10px;
  font-size: 0.78rem;
}

.metric-action:hover {
  border-color: var(--accent);
  background: var(--color-primary-soft);
  color: var(--accent-dark);
}

.metric-card.good {
  border-color: color-mix(in srgb, var(--accent) 30%, transparent);
  background: linear-gradient(180deg, var(--color-surface) 0%, var(--color-surface-muted) 100%);
}

.metric-card.warning {
  border-color: color-mix(in srgb, var(--amber) 34%, transparent);
}

.metric-card.low {
  border-color: color-mix(in srgb, var(--color-neutral) 36%, transparent);
}

.metric-title,
.metric-card small,
.trend-stat span,
.goal-card span {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
}

.metric-card strong {
  color: var(--ink);
  font-size: clamp(1.4rem, 2.8vw, 1.9rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.metric-meta {
  color: var(--accent-dark);
  font-size: 0.8rem;
  font-weight: 600;
}

.progress-track {
  width: 100%;
  height: 6px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--color-surface-muted);
}

.progress-fill {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--muted);
}

.progress-fill.good { background: var(--accent); }
.progress-fill.warning { background: var(--amber); }
.progress-fill.low { background: var(--color-neutral); }

/* ── Coach panel ─────────────────────────── */

.coach-panel {
  background: var(--surface);
}

.insight-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.insight-list li {
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  background: var(--surface-soft);
  color: var(--ink);
  padding: 12px 14px;
  font-size: 0.9rem;
  font-weight: 500;
  line-height: 1.45;
}

/* ── Forms ───────────────────────────────── */

.entry-form,
.goals-form,
.quick-weight-form {
  display: grid;
  gap: 16px;
}

.field-grid,
.goals-form {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.checkin-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.goals-form {
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  align-items: end;
}

.quick-weight-form {
  grid-template-columns: minmax(150px, 1fr) minmax(150px, 1fr) auto;
  align-items: end;
  margin-top: 14px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.quick-weight-form .status-text {
  grid-column: 1 / -1;
  margin-top: -8px;
}

.quick-weight-form.is-hidden {
  display: none;
}

label {
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 600;
}

input,
select {
  width: 100%;
  min-height: 42px;
  margin-top: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  outline: none;
  padding: 0 12px;
  font-size: 0.9rem;
}

input::placeholder {
  color: var(--color-neutral);
}

input:focus,
select:focus {
  border-color: var(--accent);
}

input:focus,
select:focus,
button:focus-visible,
a:focus-visible,
summary:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 25%, transparent);
  outline-offset: 2px;
}

.input-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
}

.input-row span {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
}

/* ── Buttons ─────────────────────────────── */

.primary-button,
.secondary-button,
.ghost-button {
  min-height: 42px;
  border-radius: 8px;
  padding: 0 14px;
}

.primary-button {
  background: var(--accent);
  color: white;
  font-weight: 600;
}

.primary-button:hover {
  background: var(--accent-dark);
}

.secondary-button {
  background: var(--color-text);
  color: white;
  font-weight: 600;
}

.secondary-button:hover,
.icon-button:hover {
  background: var(--color-primary-hover);
}

.ghost-button {
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  text-decoration: none;
  font-weight: 500;
}

.ghost-button:hover {
  border-color: var(--accent);
  background: var(--color-primary-soft);
  color: var(--accent-dark);
}

.icon-button {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 999px;
  background: var(--color-text);
  color: white;
  font-size: 1.2rem;
}

/* ── Admin login panel ───────────────────── */

.admin-login-panel {
  border-color: color-mix(in srgb, var(--accent) 24%, transparent);
  background: var(--surface);
}

.admin-login-panel.is-hidden {
  display: none;
}

.admin-login-form {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  align-items: end;
}

[hidden] {
  display: none !important;
}

body:not(.is-admin) .admin-only {
  display: none !important;
}

body.is-admin .public-note {
  display: none;
}

/* ── Details / advanced ──────────────────── */

.advanced-checkin,
.checkin-panel,
.sync-panel {
  color: var(--muted);
}

details summary {
  cursor: pointer;
  font-weight: 600;
}

.advanced-checkin {
  border: 1px dashed var(--line);
  border-radius: 10px;
  padding: 12px;
  background: var(--surface-soft);
}

.checkin-panel summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.checkin-panel summary span {
  display: grid;
  gap: 3px;
}

.checkin-panel .entry-form {
  margin-top: 16px;
}

.advanced-checkin label {
  display: block;
  margin-top: 12px;
}

.status-text,
.goal-summary {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 500;
}

.status-text {
  min-height: 20px;
}

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

.remove-button {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 999px;
  background: var(--color-danger-soft);
  color: var(--red);
  font-weight: 600;
}

.remove-button:hover {
  background: color-mix(in srgb, var(--red) 15%, transparent);
}

/* ── Trend / chart ───────────────────────── */

.trend-toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}

.segmented-control {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.segmented-control button {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  padding: 0 12px;
  font-size: 0.82rem;
  font-weight: 500;
}

.segmented-control button:hover {
  border-color: var(--accent);
  background: var(--color-primary-soft);
  color: var(--accent-dark);
}

.segmented-control button.is-active {
  border-color: var(--accent);
  background: var(--color-primary-soft);
  color: var(--accent-dark);
  font-weight: 600;
}

.trend-stats,
.goal-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.trend-stat,
.goal-card {
  display: grid;
  gap: 5px;
  padding: 12px;
  box-shadow: none;
}

.trend-stat strong,
.goal-card strong {
  font-size: 1rem;
  font-weight: 600;
}

.trend-stat small {
  color: var(--muted);
  font-weight: 500;
}

.trend-analysis {
  border: 1px solid color-mix(in srgb, var(--accent) 22%, transparent);
  border-radius: 8px;
  background: var(--color-primary-soft);
  color: var(--ink);
  padding: 14px 16px;
  margin-bottom: 12px;
}

.trend-analysis strong {
  display: block;
  margin-bottom: 4px;
  font-weight: 600;
}

.trend-analysis p {
  margin: 0;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.9rem;
  line-height: 1.5;
}

.trend-toggle {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-weight: 600;
  font-size: 0.82rem;
  padding: 0 13px;
  margin-bottom: 10px;
}

.trend-toggle input {
  width: 14px;
  min-height: 14px;
  height: 14px;
  margin: 0;
  padding: 0;
  accent-color: var(--accent);
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.chart-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 6px 10px;
}

.chart-legend-item i {
  display: block;
  width: 16px;
  height: 3px;
  border-radius: 999px;
}

.chart-canvas-wrap {
  position: relative;
  max-width: 820px;
  margin-inline: auto;
}

.chart-panel canvas {
  display: block;
  width: 100%;
  height: 240px;
}

.chart-panel.is-empty .trend-stats {
  margin-bottom: 0;
}

.chart-panel.is-empty .chart-canvas-wrap {
  max-width: 560px;
}

.chart-panel.is-empty canvas {
  height: 140px;
}

.chart-tooltip {
  position: absolute;
  z-index: 3;
  display: none;
  min-width: 150px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--ink);
  padding: 10px 12px;
  pointer-events: none;
}

.chart-tooltip.visible {
  display: grid;
  gap: 4px;
}

.chart-tooltip strong {
  color: var(--accent-dark);
  font-size: 0.82rem;
  font-weight: 600;
}

.chart-tooltip span {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 500;
}

.goal-summary {
  display: grid;
  gap: 4px;
  min-height: 20px;
  margin-top: 8px;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.84rem;
  line-height: 1.4;
}

/* ── History ─────────────────────────────── */

.history-panel,
.sync-panel {
  box-shadow: none;
}

.history-panel summary,
.sync-panel summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.history-panel summary span,
.sync-panel summary span {
  display: grid;
  gap: 3px;
}

.history-list {
  display: grid;
  gap: 8px;
  max-height: 480px;
  overflow-y: auto;
  padding: 14px 6px 0 0;
  overscroll-behavior: contain;
}

.history-list::-webkit-scrollbar {
  width: 6px;
}

.history-list::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: var(--color-neutral);
}

.history-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 14px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-soft);
  padding: 10px 12px;
}

.history-stats {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: end;
  gap: 6px;
  margin: 0;
}

.history-stats div {
  min-width: 72px;
  border-radius: 8px;
  background: var(--color-surface-muted);
  padding: 7px 8px;
  text-align: center;
}

.history-stats dt {
  color: var(--muted);
  font-size: 0.67rem;
  font-weight: 700;
  text-transform: uppercase;
}

.history-stats dd {
  margin: 2px 0 0;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ── Sync grid ───────────────────────────── */

.sync-grid {
  margin-top: 16px;
  grid-template-columns: minmax(0, 1fr) auto;
}

.sync-grid code {
  grid-column: 1 / -1;
  overflow-wrap: anywhere;
  color: var(--ink);
  font-size: 0.83rem;
}

/* ── Misc ────────────────────────────────── */

.empty-state {
  color: var(--muted);
  font-weight: 500;
  padding: 10px 0;
}

.empty-state.compact {
  padding: 6px 0;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ── Responsive ──────────────────────────── */

@media (max-width: 1040px) {
  .metric-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 780px) {
  .metric-grid,
  .insight-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .field-grid,
  .admin-login-form,
  .quick-weight-form,
  .sync-grid {
    grid-template-columns: 1fr;
  }

  .quick-weight-form .status-text {
    grid-column: 1;
  }
}

@media (max-width: 560px) {
  .app-shell {
    width: calc(100% - 22px);
  }

  .panel {
    padding: 14px;
  }

  .section-heading {
    display: grid;
  }

  .metric-grid,
  .insight-list {
    grid-template-columns: 1fr;
  }

  .metric-card {
    min-height: 0;
  }

  .chart-panel:not(.is-empty) canvas {
    height: 220px;
  }

  .history-item {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .history-stats {
    grid-column: 1 / -1;
    justify-content: start;
  }
}
