:root {
  color-scheme: dark;
  --black: #111111;
  --white: #ffffff;
  --yellow: #ffd84a;
  --red: #c91e31;
  --red-hover: #a81728;
  --shell-muted: #b9b9b9;
  --shell-border: #373737;
  --bg: #131313;
  --surface: #1b1b1b;
  --card: #202020;
  --input: #171717;
  --secondary: #292929;
  --border: #383838;
  --control-border: #757575;
  --text: #f8f8f8;
  --muted: #bcbcbc;
  --danger: #ff8993;
  --accent: var(--yellow);
  --chart-red: #ff6372;
  --chart-yellow: var(--yellow);
  --chart-neutral: #dddddd;
  --chart-fill: rgba(255, 99, 114, 0.16);
  --chart-grid: rgba(255, 255, 255, 0.13);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f1f1f1;
  --surface: var(--white);
  --card: var(--white);
  --input: #f8f8f8;
  --secondary: #ebebeb;
  --border: #d7d7d7;
  --control-border: #767676;
  --text: #171717;
  --muted: #555555;
  --danger: #ad1729;
  --accent: #ad1729;
  --chart-red: #c91e31;
  --chart-yellow: #896200;
  --chart-neutral: #333333;
  --chart-fill: rgba(201, 30, 49, 0.11);
  --chart-grid: rgba(0, 0, 0, 0.13);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.07);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.45;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; letter-spacing: -0.055em; font-weight: 850; }
h2 { font-size: clamp(1.9rem, 4vw, 2.8rem); letter-spacing: -0.055em; font-weight: 850; }
h3 { font-size: 1.1rem; letter-spacing: -0.025em; }
.muted, .hint, .empty, .stat-line { color: var(--muted); }
.muted { margin: 0.25rem 0 0; }
.hint, .empty, .stat-line { font-size: 0.9rem; margin: 0.65rem 0 0; }
.hidden { display: none !important; }
.sr-only, .skip-link:not(:focus) {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%);
}
.skip-link:focus {
  position: absolute; inset: 0 auto auto 0; z-index: 20; padding: 0.75rem;
  color: var(--text); background: var(--surface);
}

.view { min-height: 100vh; }
#login-view {
  display: grid; place-items: center;
  padding: max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right))
    max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
}
.card.login-card {
  width: min(100%, 420px);
  padding: clamp(1.2rem, 5vw, 2rem);
  border-top: 4px solid var(--red);
}
.login-card .brand { margin-bottom: 1rem; }
.login-card h1 { font-size: 2.2rem; }
#login-form { display: grid; gap: 0.65rem; margin-top: 1.25rem; }
#login-form label { font-weight: 650; }

.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 1rem;
  padding: 0.8rem clamp(0.75rem, 3vw, 2rem);
  padding-top: calc(0.8rem + env(safe-area-inset-top));
  padding-inline: max(clamp(0.75rem, 3vw, 2rem), env(safe-area-inset-left))
    max(clamp(0.75rem, 3vw, 2rem), env(safe-area-inset-right));
  border-bottom: 1px solid var(--shell-border); background: var(--black); color: var(--white);
}
.brand { display: flex; align-items: center; gap: 0.7rem; min-width: 0; }
.brand h1 span { color: var(--yellow); }
.brand-mark {
  flex: none; width: 40px; height: 40px;
  background: var(--red); color: var(--white); fill: currentColor; border-radius: 9px;
}
.brand-copy { min-width: 0; }
#welcome { margin-top: 0.1rem; color: var(--shell-muted); overflow-wrap: anywhere; font-size: 0.8rem; }
#logout-btn {
  flex: none; background: transparent; color: var(--white);
  border-color: #777777; font-size: 0.875rem;
}
#logout-btn:hover { background: var(--shell-border); }

.app-nav {
  position: fixed; z-index: 10; inset: auto 0 0;
  display: grid; grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 2px; align-items: stretch;
  padding: 0.35rem max(0.25rem, env(safe-area-inset-right))
    calc(0.35rem + env(safe-area-inset-bottom)) max(0.25rem, env(safe-area-inset-left));
  border-top: 1px solid var(--shell-border); background: var(--black);
}
.app-nav button {
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  gap: 0.25rem; padding: 0.35rem 0; min-height: 58px;
  background: transparent; color: var(--shell-muted);
  font-size: clamp(0.55rem, 2.65vw, 0.75rem); line-height: 1.15;
}
.app-nav button:hover { background: var(--shell-border); color: var(--white); }
.app-nav button[aria-current="page"] { background: var(--yellow); color: var(--black); }
.app-nav button:focus-visible, #logout-btn:focus-visible { outline-color: var(--yellow); }
.app-nav button:focus-visible { outline-offset: -2px; }
.app-nav button[aria-current="page"]:focus-visible { outline-color: var(--black); }
.app-nav svg {
  width: 20px; height: 20px; fill: none; stroke: currentColor;
  stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}

#main-content {
  min-width: 0; width: min(100%, 1100px); margin: 0 auto;
  padding: 1rem clamp(0.75rem, 3vw, 2rem) calc(6rem + env(safe-area-inset-bottom));
  padding-inline: max(clamp(0.75rem, 3vw, 2rem), env(safe-area-inset-left))
    max(clamp(0.75rem, 3vw, 2rem), env(safe-area-inset-right));
}
.date-bar {
  display: flex; justify-content: space-between; align-items: center; gap: 0.5rem;
  margin-bottom: 1rem; padding-bottom: 0.75rem; border-bottom: 1px solid var(--border);
  color: var(--muted); font-size: 0.8rem; font-weight: 700;
}
.date-controls { display: flex; gap: 0.25rem; }
.date-controls button { min-width: 44px; padding: 0; font-size: 1.4rem; line-height: 1; }
.date-controls input { width: clamp(128px, 35vw, 166px); padding: 0.4rem; }

.screen { display: grid; gap: 1rem; }
.screen-heading { padding-bottom: 0.15rem; }
.card {
  min-width: 0; padding: clamp(0.8rem, 2.5vw, 1.25rem);
  background: var(--card); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow);
}
.card > h3 { margin-bottom: 0.75rem; }
.quick-card { border-inline-start: 4px solid var(--red); }
.stack { display: grid; gap: 0.75rem; }
.fields, .row.four { display: grid; gap: 0.65rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 120px), 1fr)); }
.fields { grid-template-columns: repeat(auto-fit, minmax(min(100%, 132px), 1fr)); }
.fields label { display: grid; gap: 0.35rem; align-content: start; color: var(--muted); font-size: 0.9rem; }
.fields input, .fields select, .row input { width: 100%; min-width: 0; }
.row.inline { display: grid; gap: 0.65rem; grid-template-columns: minmax(0, 1fr) auto; }
.stack > .hint { margin: 0; }
#targets-display { margin: 0 0 1rem; }
.backup-actions { display: flex; flex-wrap: wrap; gap: 0.65rem; margin-top: 0.85rem; }

input, select, button {
  min-height: 44px; border: 1px solid var(--control-border); border-radius: 8px;
  padding: 0.55rem 0.65rem; background: var(--input); color: var(--text);
  font: inherit; font-size: 1rem;
}
input::placeholder { color: var(--muted); opacity: 1; }
input:focus, select:focus, button:focus-visible, h2:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
button { cursor: pointer; border-color: transparent; font-weight: 700; }
button.primary { background: var(--red); color: var(--white); }
button.primary:hover { background: var(--red-hover); }
button.secondary { background: var(--secondary); border-color: var(--control-border); }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger); font-size: 0.85rem; }
button:disabled { cursor: default; opacity: 0.5; }

.quick-actions, .summary-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 126px), 1fr)); gap: 0.65rem;
}
.quick-actions { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.quick-actions button { min-height: 48px; padding-inline: 0.25rem; }
.stat, .item, .chart-wrap {
  background: var(--input); border: 1px solid var(--border); border-radius: 10px;
}
.stat { padding: clamp(0.65rem, 2vw, 0.95rem); }
.stat .label { color: var(--muted); font-size: 0.82rem; }
.stat .value { font-size: clamp(1.12rem, 2.5vw, 1.5rem); font-weight: 800; margin-top: 0.2rem; }
.stat .target { color: var(--muted); font-size: 0.82rem; }
.summary-grid .stat:first-child {
  background: var(--yellow); border-color: var(--yellow); color: var(--black);
}
.summary-grid .stat:first-child :is(.label, .target) { color: #393016; }

.list { margin-top: 0.75rem; display: grid; gap: 0.5rem; }
.item { display: flex; justify-content: space-between; gap: 0.65rem; align-items: center; padding: 0.6rem; }
.item-main { min-width: 0; overflow-wrap: anywhere; }
.item-title { font-weight: 650; }
.item-meta { color: var(--muted); font-size: 0.85rem; margin-top: 0.15rem; }
.plan-list { list-style: none; padding: 0; margin-bottom: 0; }
.plan-list:empty { display: none; }
.plan-list button { flex-shrink: 0; }
#gym-plan-day { text-overflow: ellipsis; }

.chart-header { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; margin-bottom: 0.75rem; }
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 290px), 1fr)); gap: 0.75rem; }
.chart-wrap { position: relative; height: 260px; padding: 0.5rem; }
#weight-chart-wrap { margin-top: 0.65rem; }

.toast {
  position: fixed; z-index: 15; inset: auto 0.75rem calc(5rem + env(safe-area-inset-bottom)) auto;
  max-width: min(24rem, calc(100% - 1.5rem)); pointer-events: none;
}
.toast:not(:empty) {
  padding: 0.7rem 0.95rem; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-inline-start: 4px solid var(--accent);
  border-radius: 8px; box-shadow: var(--shadow);
}
.toast[data-error]:not(:empty) { border-color: var(--danger); }

@media (min-width: 760px) {
  #app-view { display: grid; grid-template-columns: 12rem minmax(0, 1fr); grid-template-rows: auto 1fr; }
  .topbar { grid-column: 1 / -1; }
  .app-nav {
    position: sticky; top: 0; align-self: start; display: flex; flex-direction: column;
    gap: 0.35rem; min-height: calc(100vh - 75px); padding: 1rem 0.75rem;
    border-top: 0; border-right: 1px solid var(--shell-border);
  }
  .app-nav button {
    flex-direction: row; align-items: center; gap: 0.6rem; min-height: 48px;
    padding: 0.55rem 0.65rem; text-align: left; font-size: 0.95rem;
  }
  #main-content { padding-bottom: 2rem; }
  .summary-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .stack > button { justify-self: start; }
  .toast { bottom: 1rem; right: 1rem; }
}

@media (max-width: 360px) {
  .row.inline { grid-template-columns: 1fr; }
  .app-nav { gap: 0; }
}
