:root {
  --bg: #111214;
  --card: #1a1c1f;
  --card2: #22252a;
  --line: #2e3237;
  --text: #f2f3f5;
  --dim: #9ba1a8;
  --dimmer: #6f767d;
  --accent: #f5c445;
  --accent-dim: #6b5518;
  --good: #4ec98a;
  --warn: #f0a742;
  --bad: #ec6a5e;
  --tap: 48px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: contain;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- app bar ---------- */
#appbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 16px 10px;
  background: linear-gradient(var(--bg) 70%, rgba(17,18,20,0.85));
  border-bottom: 1px solid var(--line);
}
#bar-title { font-size: 20px; font-weight: 700; letter-spacing: -0.01em; }
#bar-sub { font-size: 12.5px; color: var(--dim); margin-top: 2px; }

main {
  padding: 14px 14px calc(120px + var(--safe-b));
  max-width: 720px;
  margin: 0 auto;
}

/* ---------- tab bar ---------- */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: #16181b; border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
#tabs button {
  background: none; border: 0; padding: 13px 4px 12px;
  color: var(--dimmer); font-size: 12.5px; font-weight: 600;
  min-height: var(--tap);
}
#tabs button[data-route="chat"] { color: #b9932f; }
#tabs button[data-route="chat"]::before { content: '✦ '; }
#tabs button.on { color: var(--accent); }
#tabs button.on .ti { border-bottom: 2px solid var(--accent); padding-bottom: 3px; }

/* ---------- primitives ---------- */
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 14px; margin-bottom: 12px;
}
.card.tight { padding: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.dim { color: var(--dim); }
.small { font-size: 13px; }
.tiny { font-size: 11.5px; letter-spacing: 0.04em; text-transform: uppercase; color: var(--dimmer); font-weight: 700; }
.mono { font-variant-numeric: tabular-nums; }
h2 { font-size: 15px; margin: 22px 4px 10px; letter-spacing: 0.02em; text-transform: uppercase; color: var(--dim); }
h3 { font-size: 16.5px; margin: 0; font-weight: 650; letter-spacing: -0.01em; }
p { margin: 6px 0; }
.spacer { height: 6px; }

.btn {
  border: 1px solid var(--line); background: var(--card2); color: var(--text);
  border-radius: 12px; padding: 13px 16px; min-height: var(--tap);
  font-weight: 650; width: 100%;
}
.btn.primary { background: var(--accent); color: #17130a; border-color: var(--accent); }
.btn.danger { color: var(--bad); }
.btn.slim { min-height: 38px; padding: 8px 12px; font-size: 14px; width: auto; }
.ghost { background: none; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; color: var(--dim); font-size: 13px; font-weight: 600; }
.ghost.small { padding: 6px 10px; font-size: 12px; }

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--card2); border: 1px solid var(--line);
  border-radius: 999px; padding: 4px 10px; font-size: 12px; color: var(--dim);
  white-space: nowrap;
}
.pill.accent { background: var(--accent-dim); border-color: #8a6d1f; color: #ffd873; }
.pill.good { color: var(--good); border-color: #26543c; background: #16281f; }
.pill.warn { color: var(--warn); border-color: #57411d; background: #241d10; }
.pill.bad { color: var(--bad); border-color: #5a2b26; background: #241413; }
.pill.priority { color: #ffd873; border-color: #6b5518; background: #221c0d; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: var(--card2); border-radius: 10px;
  padding: 10px 12px; min-height: 42px; font-size: 14px; font-weight: 600;
}
.chip.on { background: var(--accent); color: #17130a; border-color: var(--accent); }

input, textarea, select {
  font: inherit; color: var(--text); background: var(--card2);
  border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; width: 100%;
}
textarea { min-height: 76px; resize: vertical; }
input[type=number] { -moz-appearance: textfield; }
input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
label.field { display: block; margin-bottom: 10px; }
label.field .tiny { display: block; margin-bottom: 5px; }

/* ---------- day switcher ---------- */
.dayscroll { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 8px; scrollbar-width: none; }
.dayscroll::-webkit-scrollbar { display: none; }
.dayscroll .chip { flex: 0 0 auto; }
.dayscroll .chip.done::after { content: ' ✓'; color: var(--good); }
.dayscroll .chip.on.done::after { color: #17130a; }

/* ---------- exercise card ---------- */
.exhead { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.exmeta { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.coachline {
  margin-top: 10px; padding: 10px 12px; background: #191b18;
  border: 1px solid #2b3126; border-left: 3px solid var(--accent);
  border-radius: 8px; font-size: 13.5px;
}
.coachline .hl { font-weight: 700; color: #ffd873; }
.coachline .why { color: var(--dim); margin-top: 3px; }
.setrow { display: flex; gap: 7px; flex-wrap: wrap; margin-top: 12px; }
.setbtn {
  flex: 1 1 72px; min-width: 72px; min-height: 54px;
  border-radius: 11px; border: 1px dashed var(--line); background: #15171a;
  color: var(--dimmer); display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1px; font-size: 12px; padding: 4px;
}
.setbtn .n { font-size: 10.5px; letter-spacing: 0.06em; text-transform: uppercase; }
.setbtn .v { font-size: 14.5px; font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }
.setbtn.filled { border-style: solid; border-color: #2f5f45; background: #16241d; color: var(--good); }
.setbtn.filled.miss { border-color: #5a2b26; background: #231414; color: var(--bad); }
.setbtn.next { border-color: var(--accent); border-style: solid; background: #1e1b12; color: var(--accent); }

/* ---------- rest timer ---------- */
#timer {
  position: fixed; left: 14px; right: 14px; bottom: calc(72px + var(--safe-b)); z-index: 45;
  display: flex; align-items: center; gap: 12px;
  background: #201d12; border: 1px solid #6b5518; border-radius: 12px;
  padding: 10px 12px; max-width: 692px; margin: 0 auto;
}
#timer-label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: #ffd873; font-weight: 700; }
#timer-time { flex: 1; text-align: right; font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; color: #ffd873; }
#timer.done { background: #16281f; border-color: #2f5f45; }
#timer.done #timer-label, #timer.done #timer-time { color: var(--good); }

/* ---------- bottom sheet ---------- */
#sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 60; }
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 61;
  background: var(--card); border-top: 1px solid var(--line);
  border-radius: 18px 18px 0 0; padding: 16px 16px calc(20px + var(--safe-b));
  max-height: 88vh; overflow-y: auto; max-width: 720px; margin: 0 auto;
  box-shadow: 0 -12px 40px rgba(0,0,0,0.5);
}
.sheet-title { font-size: 17px; font-weight: 700; margin-bottom: 2px; }
.sheet-sub { font-size: 13px; color: var(--dim); margin-bottom: 14px; }

/* ---------- number stepper ---------- */
.stepper { display: flex; align-items: stretch; gap: 8px; margin-bottom: 4px; }
.stepper button {
  flex: 0 0 62px; min-height: 60px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card2); font-size: 18px; font-weight: 700;
}
.stepper .val { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.stepper .val input {
  text-align: center; font-size: 30px; font-weight: 700; padding: 6px 4px;
  border: 0; background: none; font-variant-numeric: tabular-nums;
}
.stepper .val .tiny { margin-top: -4px; }

/* ---------- tables ---------- */
.tablewrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -14px; padding: 0 14px; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
th, td { text-align: right; padding: 9px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--dimmer); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; font-weight: 700; }
th:first-child, td:first-child { text-align: left; position: sticky; left: 0; background: var(--card); }
td.v { font-variant-numeric: tabular-nums; font-weight: 650; }
td.empty { color: #464b51; }
tr.priority td:first-child::after { content: ' ★'; color: var(--accent); font-size: 11px; }

/* ---------- notes / coach ---------- */
.note { border-left: 3px solid var(--line); padding-left: 12px; margin-bottom: 16px; }
.note.high { border-color: var(--bad); }
.note.med { border-color: var(--warn); }
.note.low { border-color: var(--good); }
.note .nt { font-weight: 650; margin-bottom: 3px; }
.note .nb { color: var(--dim); font-size: 14px; }

.board { display: grid; gap: 8px; }
.boardrow { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.boardrow:last-child { border-bottom: 0; }
.boardrow .bl { font-weight: 650; font-size: 14.5px; }
.boardrow .bd { color: var(--dim); font-size: 12.5px; margin-top: 2px; }

/* ---------- photos ---------- */
.pgrid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.pcard { background: var(--card); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; position: relative; }
.pcard img { width: 100%; aspect-ratio: 3/4; object-fit: cover; display: block; background: #000; }
.pcard .pmeta { padding: 8px 10px; font-size: 12px; }
.pcard.sel { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-dim); }
.pcard .selmark { position: absolute; top: 8px; right: 8px; background: var(--accent); color: #17130a; border-radius: 999px; width: 24px; height: 24px; display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 800; }
.compare { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.compare img { width: 100%; border-radius: 10px; display: block; }
.spark { width: 100%; height: 54px; display: block; }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(130px + var(--safe-b)); z-index: 80;
  background: #2b2f34; border: 1px solid var(--line); color: var(--text);
  padding: 10px 16px; border-radius: 999px; font-size: 13.5px; font-weight: 600;
  box-shadow: 0 6px 24px rgba(0,0,0,0.5);
}


#offline {
  position: fixed; left: 0; right: 0; bottom: calc(64px + var(--safe-b)); z-index: 44;
  background: #3a2a12; color: #ffd873; text-align: center;
  font-size: 12.5px; font-weight: 600; padding: 7px 12px;
  border-top: 1px solid #6b5518;
}

/* ---------- chat ---------- */
body.chatting main { padding-bottom: calc(190px + var(--safe-b)); }
#chatlog { display: flex; flex-direction: column; gap: 10px; }
.bub {
  max-width: 88%; padding: 11px 14px; border-radius: 16px;
  font-size: 15px; line-height: 1.45; overflow-wrap: anywhere;
}
.bub.you { align-self: flex-end; background: var(--accent); color: #17130a; border-bottom-right-radius: 5px; font-weight: 550; }
.bub.coach { align-self: flex-start; background: var(--card); border: 1px solid var(--line); border-bottom-left-radius: 5px; }
.bub.warnbub { border-color: #5a2b26; color: #f2b3ad; background: #241413; }
.bub code { background: #2b2f34; padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.bub .li { display: block; margin: 3px 0 3px 2px; }
.bub.thinking { color: var(--dimmer); letter-spacing: 3px; }
.dots { animation: pulse 1.2s ease-in-out infinite; display: inline-block; }
@keyframes pulse { 0%, 100% { opacity: 0.35 } 50% { opacity: 1 } }
.toolnote {
  align-self: flex-start; font-size: 12.5px; color: var(--good);
  background: #16241d; border: 1px solid #26543c; border-radius: 999px; padding: 4px 12px;
}

#composer {
  position: fixed; left: 0; right: 0; bottom: calc(64px + var(--safe-b)); z-index: 47;
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 12px; background: #16181b; border-top: 1px solid var(--line);
  max-width: 720px; margin: 0 auto;
}
#composer textarea {
  min-height: 46px; height: 46px; max-height: 130px; resize: none;
  border-radius: 14px; padding: 12px 14px; font-size: 16px;
}
.sendbtn {
  flex: 0 0 auto; min-height: 46px; padding: 0 18px; border: 0; border-radius: 14px;
  background: var(--accent); color: #17130a; font-weight: 750;
}

/* ---------- backfill ---------- */
.bfrow { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.bfrow:last-child { border-bottom: 0; }
.bfinputs { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }
.bfnum { width: 66px; text-align: center; padding: 10px 4px; font-size: 16px; }
.bfx { color: var(--dimmer); font-size: 13px; }

/* ---------- mobile ---------- */
button, .chip, .setbtn, a { touch-action: manipulation; }
input, textarea, select { font-size: 16px; } /* stops iOS zooming on focus */
@media (max-width: 380px) {
  #tabs button { font-size: 11.5px; padding: 12px 2px; }
  .setbtn { min-width: 64px; }
}

/* ---------- endurance ---------- */
.card.cardio { border-color: #2c4a63; background: linear-gradient(180deg, #16202a, var(--card)); }
.card.cardio .coachline { border-left-color: #4f9ad8; background: #141c24; border-color: #24384a; }
.card.cardio .coachline .hl { color: #8fc4ee; }
.card.cardio .pill.accent { background: #16344d; border-color: #2c5f8a; color: #8fc4ee; }
.card.donecard { opacity: 0.92; }
.weekbar { display: flex; align-items: flex-end; gap: 5px; height: 60px; margin: 12px 0 6px; }
.weekbar .wb { flex: 1; background: #23303c; border-radius: 4px 4px 0 0; min-height: 3px; position: relative; }
.weekbar .wb.now { background: #4f9ad8; }
.weeklabels { display: flex; gap: 5px; }
.weeklabels div { flex: 1; text-align: center; font-size: 10px; color: var(--dimmer); }
