:root {
  --bg: #363a22;
  --bg-elev: #171a21;
  --bg-elev-2: #1f232c;
  --border: #2a2f3a;
  --text: #eef1f6;
  --text-dim: #9aa3b2;
  --accent: #5ee6a8;
  --accent-dim: #2f6b52;
  --danger: #ff6b6b;
  --warn: #ffb454;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Light theme -- follows the system by default (prefers-color-scheme),
   or the manual override set via Settings (data-theme on <html>). */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #eef0e2;
    --bg-elev: #ffffff;
    --bg-elev-2: #f3f4ec;
    --border: #d7d9c8;
    --text: #1b1d14;
    --text-dim: #5b5e4d;
    --accent: #1f9d63;
    --accent-dim: #cdeee0;
    --danger: #c8362c;
    --warn: #a1650b;
  }
}
:root[data-theme="light"] {
  --bg: #eef0e2;
  --bg-elev: #ffffff;
  --bg-elev-2: #f3f4ec;
  --border: #d7d9c8;
  --text: #1b1d14;
  --text-dim: #5b5e4d;
  --accent: #1f9d63;
  --accent-dim: #cdeee0;
  --danger: #c8362c;
  --warn: #a1650b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  position: relative;
  padding-bottom: calc(64px + var(--safe-bottom));
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  background-image: url("../icons/flag-bg.svg");
  background-repeat: no-repeat;
  background-position: center top;
  background-size: auto 100vh;
  opacity: 0.22;
  pointer-events: none;
  z-index: 0;
}

.app {
  position: relative;
  z-index: 1;
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px 32px;
  min-height: 100vh;
}

h1, h2, h3 { margin: 0 0 4px; line-height: 1.2; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }
p { color: var(--text-dim); margin: 4px 0 0; font-size: 14px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}
.topbar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-crop {
  display: block;
  width: 100px;
  height: 41px;
  overflow: hidden;
  position: relative;
  border-radius: 6px;
}
.logo-crop img {
  position: absolute;
  top: -24px;
  left: 0;
  width: 100px;
  height: 100px;
  max-width: none;
}

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.card.tappable, .row.tappable {
  cursor: pointer;
  transition: background .15s ease, transform .1s ease;
}
.card.tappable:active { transform: scale(0.98); background: var(--bg-elev-2); }
.row.tappable:active { opacity: .7; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.row + .row { margin-top: 8px; }

.pill {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .02em;
  text-transform: uppercase;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 1px solid var(--accent-dim);
  padding: 3px 8px;
  border-radius: 999px;
}

.reorder-btns {
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.reorder-btns button {
  width: 28px;
  height: 22px;
  padding: 0;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 5px;
  font-size: 9px;
  line-height: 1;
  cursor: pointer;
}
.reorder-btns button:disabled {
  opacity: .3;
  cursor: default;
}
.reorder-btns button:not(:disabled):active {
  background: var(--bg-elev);
  color: var(--text);
}

.btn {
  appearance: none;
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border);
  width: 100%;
  text-decoration: none;
  text-align: center;
  display: inline-block;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--accent); color: #06120c; border-color: var(--accent); }
.btn.danger { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn.ghost { background: transparent; }
.btn.small { width: auto; padding: 8px 12px; font-size: 13px; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }

label { display: block; font-size: 13px; color: var(--text-dim); margin: 12px 0 6px; }
label:first-child { margin-top: 0; }

input[type="text"], input[type="url"], input[type="number"], textarea, select {
  width: 100%;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
}
textarea { min-height: 140px; resize: vertical; font-family: ui-monospace, monospace; font-size: 13px; }
select { appearance: none; }

.hint { font-size: 12px; color: var(--text-dim); margin-top: 6px; }
.error { font-size: 13px; color: var(--danger); margin-top: 8px; }
.divider { text-align: center; color: var(--text-dim); font-size: 12px; margin: 18px 0; position: relative; }
.divider::before, .divider::after {
  content: ""; position: absolute; top: 50%; width: 42%; height: 1px; background: var(--border);
}
.divider::before { left: 0; }
.divider::after { right: 0; }

.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  display: flex;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  z-index: 10;
}
.tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 10px 0 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  cursor: pointer;
}
.tab svg { width: 20px; height: 20px; fill: currentColor; }
.tab.active { color: var(--accent); }

.empty {
  text-align: center;
  padding: 48px 16px;
  color: var(--text-dim);
}
.empty svg { width: 48px; height: 48px; opacity: .5; margin-bottom: 12px; }

.week-group { margin: 20px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-dim); }

.card.current-week { border-color: var(--accent-dim); background: linear-gradient(180deg, color-mix(in srgb, var(--accent) 6%, transparent), transparent 40%), var(--bg-elev); }

.rest-timer-inline {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  padding: 3px 4px 3px 10px;
  border-radius: 999px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
}
.rest-timer-inline .rest-timer-display { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--text-dim); }
.rest-timer-btn {
  appearance: none;
  cursor: pointer;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #06120c;
  font-size: 11px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  flex-shrink: 0;
}
.rest-timer-card.rest-timer-done { border-color: var(--accent); animation: rest-timer-flash 0.6s ease-in-out 2; }
@keyframes rest-timer-flash {
  0%, 100% { background: var(--bg-elev-2); }
  50% { background: color-mix(in srgb, var(--accent) 18%, transparent); }
}

.progress-track { height: 5px; border-radius: 3px; background: var(--bg-elev-2); margin-top: 10px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--accent-dim); border-radius: 3px; transition: width .3s ease; }
.progress-fill.complete { background: var(--accent); }
.done-check { color: var(--accent); font-weight: 700; }

.sync-status {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  right: 12px;
  z-index: 20;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  color: var(--text-dim);
  opacity: 0;
  transform: translateY(-6px);
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
.sync-status.show { opacity: 1; transform: translateY(0); }
.sync-status.pending { color: var(--text-dim); }
.sync-status.synced { color: var(--accent); border-color: var(--accent-dim); }
.sync-status.error { color: var(--warn); border-color: var(--warn); }
.week-fields { display: flex; flex-wrap: nowrap; gap: 6px; margin: 10px 0; overflow-x: auto; }
.week-fields .field { flex: 1 1 0; min-width: 52px; }
.week-fields .field label { margin: 0 0 4px; font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.week-fields .field input { width: 100%; padding: 8px 2px; text-align: center; }
.week-fields .field input + input { margin-top: 4px; }
.readonly-value {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 2px;
  text-align: center;
  font-size: 15px;
  color: var(--text);
  min-height: 18px;
}
.readonly-value + .readonly-value { margin-top: 4px; }

.source-chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-dim);
  background: var(--bg-elev-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 10px;
}

.video-embed {
  position: relative;
  width: 100%;
  padding-top: 56.25%; /* 16:9 */
  margin-bottom: 12px;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-elev);
}
.video-embed iframe {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  border: 0;
}

.toast {
  position: fixed;
  bottom: calc(76px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 20;
  box-shadow: 0 8px 24px rgba(0,0,0,.4);
}

.log-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-dim); }
.log-ex { font-size: 13px; margin: 4px 0; }

.cardio-month h3 { margin-bottom: 4px; }
.cardio-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cardio-grid.cardio-weekdays { margin: 8px 0 4px; font-size: 11px; color: var(--text-dim); text-align: center; }
.cardio-cell {
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 12px;
  padding: 0;
  cursor: pointer;
  appearance: none;
  font-family: inherit;
}
.cardio-cell:active { background: var(--bg-elev); }
.cardio-cell.empty { background: transparent; border-color: transparent; cursor: default; }
.cardio-cell.today { border-color: var(--accent); }
.cardio-cell.logged { background: color-mix(in srgb, var(--accent) 10%, transparent); border-color: var(--accent-dim); }
.cardio-dot { width: 5px; height: 5px; border-radius: 50%; background: var(--accent); }

@media (min-width: 520px) {
  .tabbar { max-width: 640px; left: 50%; transform: translateX(-50%); border-left: 1px solid var(--border); border-right: 1px solid var(--border); border-radius: 16px 16px 0 0; }
}

.body-map { max-width: 220px; margin: 0 auto; }
.body-map svg { width: 100%; height: auto; display: block; }
.body-map .body-base { fill: var(--bg-elev-2); stroke: var(--border); stroke-width: 1.5; }
.body-map .muscle-hotspot { fill: var(--accent-dim); stroke: var(--accent); stroke-width: 1.5; cursor: pointer; }
.body-map .muscle-hotspot:active, .body-map .muscle-hotspot:hover { fill: var(--accent); }
.body-map .muscle-hotspot text {
  fill: var(--text);
  font-size: 10px;
  font-weight: 600;
  text-anchor: middle;
  pointer-events: none;
}

/* ---- Nutrition ---- */

.macro-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
}
.macro-stat { min-width: 0; }
.macro-stat-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .02em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.macro-stat h3 {
  margin: 2px 0 0;
  font-size: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.macro-stat-goal {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 1px;
}

.food-entry-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 0;
  border-top: 1px solid var(--border);
}
.food-entry-row:first-of-type { border-top: none; padding-top: 0; }

.scan-video-wrap {
  position: relative;
  width: 100%;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  margin-bottom: 14px;
  aspect-ratio: 3 / 4;
}
#scan-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.scan-video-wrap::after {
  content: "";
  position: absolute;
  inset: 18% 12%;
  border: 2px solid color-mix(in srgb, #fff 70%, transparent);
  border-radius: 12px;
  pointer-events: none;
}
