:root {
  --bg: #f6f8fc;
  --panel: #ffffff;
  --panel-solid: #ffffff;
  --panel-border: #e9edf5;
  --accent: #2f73ff;
  --accent-strong: #0f63ff;
  --accent-ink: #ffffff;
  --accent2: #7a8ca8;
  --good: #27c46b;
  --warn: #f59e0b;
  --bad: #ef4444;
  --text: #111827;
  --muted: #7a8496;
  --soft: #f3f6fb;
  --shadow: 0 18px 50px rgba(30, 45, 75, 0.08);
  --stage-bg: #eef2f7;
  --muscle-off: #d9dee7;
  --font-display: "Inter", system-ui, sans-serif;
  --font-body: "Inter", system-ui, sans-serif;
}

body.light {
  --bg: #f6f8fc;
  --panel: #ffffff;
  --panel-solid: #ffffff;
  --panel-border: #e9edf5;
  --accent: #2f73ff;
  --accent-strong: #0f63ff;
  --accent-ink: #ffffff;
  --accent2: #7a8ca8;
  --good: #27c46b;
  --warn: #f59e0b;
  --bad: #ef4444;
  --text: #111827;
  --muted: #7a8496;
  --soft: #f3f6fb;
  --shadow: 0 18px 50px rgba(30, 45, 75, 0.08);
  --stage-bg: #eef2f7;
  --muscle-off: #d9dee7;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
button, input, select { font: inherit; }
button { touch-action: manipulation; }

body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px max(18px, env(safe-area-inset-left)) 14px;
  background: rgba(246, 248, 252, 0.88);
  backdrop-filter: blur(16px);
}

.logo {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-size: 17px;
  font-weight: 800;
  letter-spacing: 0;
  color: #0f172a;
  white-space: nowrap;
}
.logo span { color: var(--accent); }
.logo em { display: none; }

.profile { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.chip {
  display: none;
  padding: 6px 9px;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}
.chip b { color: var(--accent); }

.shell-icon,
.theme-btn {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 18px;
  background: transparent;
  color: #111827;
  font-size: 18px;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.app {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 10px 18px 104px;
  display: grid;
  grid-template-columns: minmax(320px, 1fr) minmax(320px, 1fr);
  gap: 22px;
}

.panel {
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 22px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.panel-title {
  margin-bottom: 20px;
  color: var(--text);
  font-size: 18px;
  font-weight: 800;
  text-align: center;
  letter-spacing: 0;
}
.panel-note {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}

.coach-strip {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px;
  margin-bottom: 16px;
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 12px 34px rgba(30, 45, 75, 0.06);
}
.coach-dot {
  width: 28px;
  height: 28px;
  border-radius: 14px;
  background: #eaf2ff;
  position: relative;
  flex: none;
}
.coach-dot::after {
  content: "";
  position: absolute;
  inset: 9px;
  border-radius: 50%;
  background: var(--accent);
}
.coach-strip b {
  display: block;
  color: var(--accent);
  font-size: 13px;
  font-weight: 800;
  text-transform: uppercase;
}
.coach-strip p {
  margin-top: 5px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}
.coach-wave {
  margin-left: auto;
  color: var(--accent);
  font-size: 26px;
  font-weight: 800;
}

.workout-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 10px 0 14px;
}
.workout-head small {
  color: #5f6979;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}
.workout-head h1 {
  margin-top: 8px;
  color: var(--text);
  font-size: 24px;
  line-height: 1;
  letter-spacing: 0;
}
.workout-head p {
  margin-top: 7px;
  color: var(--muted);
  font-size: 13px;
}
.live-pill {
  display: inline-block;
  margin-top: 8px;
  color: var(--good);
  font-size: 12px;
  font-weight: 700;
}

.video-wrap {
  position: relative;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  border-radius: 16px;
  border: 1px solid var(--panel-border);
  background:
    linear-gradient(180deg, rgba(255,255,255,.2), rgba(255,255,255,.7)),
    var(--stage-bg);
}
#cam, #overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
#cam { object-fit: cover; }
#cam.mirrored { transform: scaleX(-1); }
#cam.hidden { display: none; }

.feedback-banner {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 3;
  max-width: min(280px, 78%);
  padding: 9px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--text);
  font-size: 11px;
  font-weight: 800;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-shadow: 0 10px 24px rgba(30, 45, 75, 0.08);
}
.feedback-banner.good { color: var(--good); }
.feedback-banner.warn { color: var(--warn); }
.feedback-banner.bad { color: var(--bad); }

.status-overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(248, 250, 252, 0.78);
  text-align: center;
}
.status-overlay.hidden { display: none; }
.status-card {
  max-width: 380px;
  padding: 18px 20px;
  border-radius: 16px;
  background: #fff;
  color: #4b5563;
  border: 1px solid var(--panel-border);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.55;
}
.status-card b { color: var(--accent); }

.rep-flash {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 5;
  transform: translate(-50%, -50%) scale(.75);
  color: var(--accent);
  font-size: 62px;
  font-weight: 800;
  opacity: 0;
  pointer-events: none;
}
.rep-flash.show { animation: pop .7s ease-out; }
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(.6); }
  25% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1.25); }
}

.set-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 16px 0 0;
  padding: 15px;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: #fff;
}
.set-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text);
  font-size: 13px;
  font-weight: 800;
}
.set-chip b { color: var(--text); }
.stepper { display: flex; gap: 4px; margin-left: 4px; }
.stepper button {
  width: 24px;
  height: 24px;
  border: 0;
  border-radius: 12px;
  background: var(--soft);
  color: var(--accent);
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
}

.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 16px;
}
#btnCamera { grid-column: 1 / -1; }
.btn {
  min-height: 48px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: #fff;
  color: var(--text);
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  transition: transform .15s ease, box-shadow .15s ease, background .15s ease;
}
.btn:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 12px 22px rgba(30,45,75,.08); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary {
  color: #fff;
  background: linear-gradient(180deg, #347dff, #1767f2);
  border: 0;
  box-shadow: 0 12px 24px rgba(47, 115, 255, .25);
}
.btn.accent {
  color: #ef4444;
  background: #fff;
}
.btn.icon, .btn.mini {
  min-height: 40px;
  padding: 0 14px;
}
.btn.hidden, .cam-select.hidden { display: none; }
.cam-select {
  grid-column: 1 / -1;
  min-height: 44px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: #fff;
  color: var(--text);
  padding: 0 12px;
}

.stat-hero {
  margin: 16px 0 0;
  padding: 16px;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: #fff;
}
.rep-count {
  display: flex;
  align-items: baseline;
  justify-content: center;
  color: var(--text);
  font-weight: 800;
  line-height: 1;
}
.rep-count > span:first-child { font-size: 42px; }
.rep-goal {
  margin-left: 5px;
  color: var(--muted);
  font-size: 20px;
  font-weight: 700;
}
.rep-goal b { color: var(--muted); }
.rep-label {
  margin-top: 9px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  text-align: center;
  text-transform: uppercase;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 10px;
}
.stat {
  min-height: 84px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 8px;
  padding: 15px;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: #fff;
}
.stat small {
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
}
.stat b {
  color: var(--text);
  font-size: 24px;
  font-weight: 800;
}
.ring-stat {
  position: relative;
  align-items: center;
  grid-row: span 2;
}
.ring { width: 96px; height: 96px; }
.ring circle { fill: none; stroke-width: 8; }
.ring-bg { stroke: #e8edf5; }
.ring-fg {
  stroke: var(--accent);
  stroke-linecap: round;
  stroke-dasharray: 264;
  stroke-dashoffset: 0;
  transform: rotate(-90deg);
  transform-origin: center;
}
.ring-text {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-weight: 800;
}
.ring-text b { font-size: 24px; }
.ring-text small { color: var(--good); font-size: 11px; text-transform: none; }

.ex-search {
  width: 100%;
  height: 54px;
  margin-bottom: 16px;
  padding: 0 18px;
  border: 0;
  border-radius: 16px;
  outline: none;
  background: var(--soft);
  color: var(--text);
  font-size: 14px;
}
.ex-search::placeholder { color: var(--muted); }
.ex-search:focus { box-shadow: inset 0 0 0 2px rgba(47,115,255,.22); }

.group-chips {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 18px;
}
.chip-btn {
  min-height: 38px;
  border: 1px solid var(--panel-border);
  border-radius: 19px;
  background: #f7f9fc;
  color: #546173;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}
.chip-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.ex-list {
  max-height: 430px;
  overflow-y: auto;
  margin-bottom: 14px;
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  background: #fff;
  -webkit-overflow-scrolling: touch;
}
.ex-item {
  width: 100%;
  display: grid;
  grid-template-columns: 46px 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 0;
  border-bottom: 1px solid var(--panel-border);
  background: #fff;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}
.ex-item:last-child { border-bottom: 0; }
.ex-ico {
  width: 42px;
  height: 42px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(145deg, #edf3ff, #f7f9fc);
  font-size: 20px;
}
.ex-name { display: block; font-size: 14px; font-weight: 800; }
.ex-sub { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; font-weight: 600; }
.ex-tag {
  justify-self: end;
  min-width: 28px;
  height: 28px;
  border-radius: 14px;
  background: var(--soft);
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0;
}
.ex-tag::after { content: "..."; font-size: 13px; font-weight: 800; line-height: 1; }
.ex-item.active .ex-tag { background: var(--accent); color: #fff; }
.ex-item.active .ex-tag::after { content: "✓"; font-size: 13px; }
.ex-empty { padding: 20px; color: var(--muted); text-align: center; }

.ex-meta,
.dash-block,
.coach-card {
  padding: 16px;
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  background: #fff;
}
.meta-name {
  color: var(--text);
  font-size: 15px;
  font-weight: 800;
}
.meta-sub {
  margin-top: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.meta-cues {
  list-style: none;
  margin-top: 12px;
  color: #374151;
  font-size: 13px;
  line-height: 1.5;
}
.meta-cues li {
  position: relative;
  padding: 6px 0 6px 26px;
  border-top: 1px solid var(--panel-border);
}
.meta-cues li::before {
  content: "✓";
  position: absolute;
  left: 0;
  top: 6px;
  color: var(--good);
  font-weight: 800;
}

.dash-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
.dash-wide { grid-column: 1 / -1; }
.dash-block small,
.coach-card small {
  display: block;
  margin-bottom: 12px;
  color: var(--text);
  font-size: 13px;
  font-weight: 800;
}
.dash-sub { margin-top: 9px; color: var(--muted); font-size: 12px; }
.xp-bar {
  height: 10px;
  overflow: hidden;
  border-radius: 999px;
  background: #e8edf5;
}
.xp-fill { height: 100%; width: 0; background: var(--accent); transition: width .4s ease; }
.week-cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.wk {
  min-height: 86px;
  padding: 14px;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: #fff;
}
.wk b { display: block; color: var(--text); font-size: 24px; font-weight: 800; }
.wk span { display: block; margin-top: 6px; color: var(--muted); font-size: 12px; }
#weekChart { width: 100%; height: 140px; }
#weekChart polyline { fill: none; stroke: var(--accent); stroke-width: 3; }
#weekChart circle { fill: var(--accent); }
#weekChart text { fill: var(--muted); font-size: 11px; font-family: var(--font-body); }
#weekChart .bubble { fill: var(--accent); }
#weekChart .bubble-text { fill: #fff; font-size: 10px; font-weight: 800; }

.muscle-wrap { display: flex; align-items: center; justify-content: center; gap: 16px; flex-wrap: wrap; }
.muscle { width: 110px; height: auto; }
.muscle .sil rect, .muscle .sil circle { fill: #e3e7ee; stroke: #aeb7c4; stroke-width: .5; }
.muscle [data-m] { fill: var(--muscle-off); transition: fill .25s ease; }
.muscle .m-low { fill: #f5b51b; }
.muscle .m-med { fill: #ff8126; }
.muscle .m-high { fill: #ff4438; }
.muscle-legend { display: flex; flex-direction: column; gap: 8px; color: var(--muted); font-size: 12px; font-weight: 700; }
.muscle-legend i { display: inline-block; width: 12px; height: 12px; border-radius: 2px; margin-right: 8px; background: var(--muscle-off); vertical-align: -2px; }
.muscle-legend i.m-low { background: #f5b51b; }
.muscle-legend i.m-med { background: #ff8126; }
.muscle-legend i.m-high { background: #ff4438; }

.pr-list, .history, .coach-list { list-style: none; color: var(--text); font-size: 13px; }
.pr-list li, .history li, .coach-list li {
  padding: 11px 0;
  border-bottom: 1px solid var(--panel-border);
}
.pr-list li:last-child, .history li:last-child, .coach-list li:last-child { border-bottom: 0; }
.pr-list b, .history b { float: right; color: var(--accent); font-size: 12px; }
.muted { color: var(--muted); }

.coach-hero {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 18px;
  margin-bottom: 14px;
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  background: #fff;
}
.coach-avatar {
  width: 58px;
  height: 58px;
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: linear-gradient(145deg, #172554, #7c3aed);
  box-shadow: 0 12px 24px rgba(47, 115, 255, .18);
  font-size: 17px;
  font-weight: 800;
}
.coach-hero b { display: block; font-size: 18px; font-weight: 800; }
.coach-hero p { margin-top: 6px; color: var(--muted); font-size: 13px; line-height: 1.45; }
.coach-card { margin-bottom: 14px; }
.coach-card p { color: #1f2937; font-size: 14px; line-height: 1.55; }
.coach-list li { position: relative; padding-left: 34px; }
.coach-list li::before {
  content: "!";
  position: absolute;
  left: 0;
  top: 10px;
  width: 22px;
  height: 22px;
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff2df;
  color: #f59e0b;
  font-size: 12px;
  font-weight: 800;
}
.coach-list li.good-item::before {
  content: "✓";
  background: #e9fbf1;
  color: var(--good);
}
.coach-chips { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }

.bottom-nav {
  position: fixed;
  left: 50%;
  bottom: 0;
  z-index: 30;
  width: min(100%, 520px);
  transform: translateX(-50%);
  display: flex;
  gap: 2px;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--panel-border);
  border-radius: 22px 22px 0 0;
  background: rgba(255,255,255,.94);
  backdrop-filter: blur(18px);
  box-shadow: 0 -14px 34px rgba(30, 45, 75, .08);
}
.bottom-nav button {
  flex: 1;
  min-width: 0;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 7px 2px;
  font-size: 10px;
  font-weight: 700;
  cursor: pointer;
}
.bottom-nav button span { font-size: 19px; line-height: 1; }
.bottom-nav button.active { color: var(--accent); }

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(15, 23, 42, .35);
  backdrop-filter: blur(10px);
}
.modal.hidden { display: none; }
.modal-card {
  width: min(430px, 100%);
  max-height: 88vh;
  overflow-y: auto;
  padding: 26px;
  border: 1px solid var(--panel-border);
  border-radius: 24px;
  background: #fff;
  box-shadow: 0 24px 80px rgba(15,23,42,.18);
  text-align: center;
}
.modal-card h2 { font-size: 18px; font-weight: 800; }
.pr-badge {
  display: inline-flex;
  margin-left: 6px;
  padding: 4px 8px;
  border-radius: 999px;
  background: #fff7ed;
  color: #ea580c;
  font-size: 11px;
}
.pr-badge.hidden { display: none; }
.score-hero { margin: 18px 0 20px; color: var(--accent); font-weight: 800; }
.score-hero b { font-size: 62px; }
.score-hero span { color: var(--muted); font-size: 20px; }
.sum-rows { display: flex; flex-direction: column; gap: 10px; text-align: left; }
.sum-row {
  display: grid;
  grid-template-columns: 118px 1fr 56px;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
}
.sum-row b { color: var(--text); text-align: right; }
.sum-row .bar {
  height: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: #e8edf5;
}
.sum-row .bar div { height: 100%; background: var(--accent); }
.xp-earned { color: var(--good) !important; }

#screen-workout { grid-column: 1; grid-row: 1; }
#screen-exercises { grid-column: 2; grid-row: 1; }
#screen-progress { grid-column: 1; grid-row: 2; }
#screen-coach { grid-column: 2; grid-row: 2; }

@media (max-width: 820px) {
  body { background: #f7f9fc; }
  .chip { display: none; }
  .topbar { padding: 14px 18px 8px; }
  .app {
    display: block;
    padding: 8px 14px calc(96px + env(safe-area-inset-bottom));
  }
  .panel {
    display: none;
    border: 0;
    border-radius: 0;
    padding: 0;
    box-shadow: none;
    background: transparent;
  }
  .screen.active { display: block; }
  .panel-title { margin: 10px 0 22px; }
  .controls { grid-template-columns: 1fr; }
  #btnCamera { order: 0; }
  #btnFlipCamera { order: 1; }
  #btnDemo { order: 2; }
  #btnVoice { order: 3; }
  #btnEnd { order: 4; }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .group-chips { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .dash-grid { grid-template-columns: 1fr; }
  .week-cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .coach-chips { grid-template-columns: 1fr 1fr 1fr; }
  .bottom-nav { width: 100%; }
}

@media (min-width: 821px) {
  .bottom-nav { display: none; }
  .chip { display: inline-flex; }
  .theme-btn { border: 1px solid var(--panel-border); background: #fff; }
}

@media (max-width: 390px) {
  .group-chips { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .coach-chips { grid-template-columns: 1fr; }
  .set-bar { flex-direction: column; align-items: stretch; }
}
