:root {
  --bg: #09090b;
  --panel: #141417;
  --panel-2: #1b1b1f;
  --line: #27272a;
  --red: #e11d2a;
  --red-soft: #2a0e10;
  --red-bright: #ff3b46;
  --slate: #475569;
  --slate-light: #94a3b8;
  --txt: #f4f4f5;
  --muted: #9ca3af;
  --good: #22c55e;
  --good-soft: #0f2417;
  --warn: #f59e0b;
  --miss: #ef4444;
  --radius: 16px;
  --tabbar-h: 64px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--txt);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  overscroll-behavior-y: contain;
}
button { font-family: inherit; cursor: pointer; }
a { color: var(--red-bright); }

/* ---------- Header / countdown ---------- */
header.topbar {
  position: sticky; top: 0; z-index: 30;
  background: linear-gradient(180deg, #0c0c0f 0%, rgba(9,9,11,0.96) 100%);
  border-bottom: 1px solid var(--line);
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
  backdrop-filter: blur(8px);
}
.topbar .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--red); box-shadow: 0 0 10px var(--red); }
.brand h1 { font-size: 15px; margin: 0; font-weight: 800; letter-spacing: 0.3px; text-transform: uppercase; }
.brand small { display: block; color: var(--muted); font-weight: 600; font-size: 11px; letter-spacing: 0.5px; }
.countpill { text-align: right; }
.countpill .big { font-size: 22px; font-weight: 900; color: var(--red-bright); line-height: 1; }
.countpill .lbl { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }
.progress-track { margin-top: 12px; height: 8px; background: var(--panel-2); border-radius: 99px; overflow: hidden; border: 1px solid var(--line); }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--red), var(--red-bright)); border-radius: 99px; transition: width .4s ease; }

/* ---------- Layout ---------- */
main { padding: 16px 16px 24px; max-width: 680px; margin: 0 auto; }
.view { display: none; animation: fade .18s ease; }
.view.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.section-title { font-size: 12px; letter-spacing: 1.2px; text-transform: uppercase; color: var(--muted); margin: 22px 2px 10px; font-weight: 700; }
.section-title:first-child { margin-top: 4px; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card + .card { margin-top: 12px; }

/* ---------- Stat row (macros / burn) ---------- */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.stat .n { font-size: 24px; font-weight: 900; }
.stat .n .of { font-size: 13px; color: var(--muted); font-weight: 600; }
.stat .k { font-size: 11px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted); margin-top: 2px; }
.bar { margin-top: 10px; height: 6px; border-radius: 99px; background: var(--panel-2); overflow: hidden; }
.bar > span { display: block; height: 100%; border-radius: 99px; }
.bar.cal > span { background: linear-gradient(90deg, var(--red), var(--red-bright)); }
.bar.burn > span { background: linear-gradient(90deg, #1d4ed8, #38bdf8); }
.macro-mini { display: grid; grid-template-columns: repeat(3,1fr); gap: 8px; margin-top: 12px; }
.macro-mini .m { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.macro-mini .m b { display: block; font-size: 16px; }
.macro-mini .m span { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }

/* ---------- Checklist ---------- */
.task {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 10px; position: relative; overflow: hidden;
}
.task::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--slate); }
.task.done::before { background: var(--good); }
.task.missed::before { background: var(--miss); }
.task.overdue::before { background: var(--warn); }
.task.done { opacity: 0.72; }
.task .head { display: flex; align-items: center; gap: 10px; }
.task .time { font-size: 12px; font-weight: 800; color: var(--red-bright); min-width: 64px; }
.task.done .time { color: var(--good); }
.task .title { font-weight: 700; flex: 1; }
.task .badge { font-size: 9px; text-transform: uppercase; letter-spacing: 0.5px; padding: 3px 7px; border-radius: 99px; background: var(--panel-2); color: var(--slate-light); border: 1px solid var(--line); }
.task .detail { color: var(--muted); font-size: 13px; margin: 8px 0 0 74px; }
.task .actions { display: flex; gap: 8px; margin: 12px 0 0 74px; flex-wrap: wrap; }
.task .overdue-flag { margin: 10px 0 0 74px; font-size: 12px; color: var(--warn); font-weight: 700; display: none; }
.task.overdue .overdue-flag { display: block; }

.btn { border: 1px solid var(--line); background: var(--panel-2); color: var(--txt); border-radius: 10px; padding: 8px 12px; font-size: 12px; font-weight: 700; letter-spacing: 0.3px; }
.btn:active { transform: scale(0.97); }
.btn.done { background: var(--good-soft); border-color: #1f6f43; color: #86efac; }
.btn.miss { background: var(--red-soft); border-color: #7f1d1d; color: #fca5a5; }
.btn.ghost { background: transparent; }
.btn.full { width: 100%; text-align: center; padding: 13px; font-size: 14px; }
.btn.red { background: var(--red); border-color: var(--red); color: #fff; }
.btn.red:active { background: var(--red-bright); }
.btn[aria-pressed="true"].done { box-shadow: inset 0 0 0 1px #22c55e; }
.btn[aria-pressed="true"].miss { box-shadow: inset 0 0 0 1px #ef4444; }
.btn.tiny { padding: 6px 9px; font-size: 11px; }

.phase-label { display: flex; align-items: center; gap: 8px; margin: 20px 2px 10px; }
.phase-label .ico { font-size: 11px; font-weight: 900; color: var(--bg); background: var(--red); border-radius: 6px; padding: 3px 6px; letter-spacing: 0.5px; }
.phase-label span { font-size: 12px; letter-spacing: 1px; text-transform: uppercase; color: var(--slate-light); font-weight: 700; }

/* ---------- Food ---------- */
.food-row { display: flex; align-items: center; gap: 10px; padding: 11px 0; border-bottom: 1px solid var(--line); }
.food-row:last-child { border-bottom: none; }
.food-row .fname { flex: 1; font-weight: 600; }
.food-row .fmacro { font-size: 11px; color: var(--muted); }
.food-row .fcal { font-weight: 800; color: var(--red-bright); white-space: nowrap; }
.food-row .x { background: none; border: none; color: var(--muted); font-size: 18px; padding: 0 4px; }
.empty { color: var(--muted); text-align: center; padding: 18px 0; font-size: 13px; }

label.fld { display: block; margin-bottom: 12px; }
label.fld .lab { font-size: 11px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted); margin-bottom: 5px; display: block; font-weight: 700; }
input, textarea, select {
  width: 100%; background: var(--panel-2); border: 1px solid var(--line); color: var(--txt);
  border-radius: 10px; padding: 12px; font-size: 15px; font-family: inherit;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--red); }
.grid4 { display: grid; grid-template-columns: repeat(4,1fr); gap: 8px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.lib-item { display: flex; align-items: center; gap: 10px; padding: 11px 0; border-bottom: 1px solid var(--line); }
.lib-item:last-child { border-bottom: none; }
.lib-item .li-main { flex: 1; }
.lib-item .li-main b { display: block; }
.lib-item .li-main small { color: var(--muted); font-size: 11px; }

/* ---------- Coach chat ---------- */
.chat-wrap { display: flex; flex-direction: column; min-height: 50vh; }
.chat-feed { flex: 1; display: flex; flex-direction: column; gap: 10px; padding: 4px 0 12px; }
.msg { max-width: 86%; padding: 11px 13px; border-radius: 14px; font-size: 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg.coach { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); border-top-left-radius: 4px; }
.msg.me { align-self: flex-end; background: var(--red); color: #fff; border-top-right-radius: 4px; }
.msg.sys { align-self: center; background: var(--red-soft); border: 1px solid #7f1d1d; color: #fca5a5; font-size: 12px; text-align: center; }
.chat-input { display: flex; gap: 8px; align-items: flex-end; position: sticky; bottom: 0; background: var(--bg); padding-top: 8px; }
.chat-input textarea { resize: none; max-height: 120px; }
.typing { color: var(--muted); font-size: 12px; font-style: italic; align-self: flex-start; }

/* ---------- Rules / info ---------- */
.rule { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.rule:last-child { border-bottom: none; }
.rule .num { color: var(--red-bright); font-weight: 900; min-width: 18px; }
.rule b { display: block; }
.rule p { margin: 3px 0 0; color: var(--muted); font-size: 13px; }
.safety { background: #1a1206; border: 1px solid #7c5e12; border-radius: var(--radius); padding: 14px; }
.safety h3 { margin: 0 0 8px; color: var(--warn); font-size: 14px; }
.safety p, .safety li { font-size: 13px; color: #e7d8b0; }
.safety ul { margin: 8px 0 0; padding-left: 18px; }
.note { font-size: 12px; color: var(--muted); margin-top: 8px; }

/* ---------- Tabbar ---------- */
nav.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 40;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: rgba(12,12,15,0.96); border-top: 1px solid var(--line);
  display: flex; backdrop-filter: blur(10px);
}
nav.tabbar button {
  flex: 1; background: none; border: none; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 10px; font-weight: 700; letter-spacing: 0.3px;
}
nav.tabbar button .ic { font-size: 19px; line-height: 1; }
nav.tabbar button.active { color: var(--red-bright); }
nav.tabbar button.active .ic { filter: drop-shadow(0 0 6px var(--red)); }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + 20px);
  background: #fff; color: #111; padding: 11px 16px; border-radius: 12px; font-weight: 700; font-size: 13px;
  z-index: 60; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: 90%;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
#toast.bad { background: var(--red); color: #fff; }

.hidden { display: none !important; }
.spin { display: inline-block; width: 14px; height: 14px; border: 2px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: r .7s linear infinite; vertical-align: -2px; }
@keyframes r { to { transform: rotate(360deg); } }
.muted { color: var(--muted); }
.center { text-align: center; }
.flex { display: flex; gap: 8px; align-items: center; }
.flex.between { justify-content: space-between; }
.pill-toggle { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- Mode toggle ---------- */
.mode-toggle { display: flex; gap: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 4px; }
.mode-toggle .mpill { flex: 1; background: transparent; border: none; color: var(--muted); font-weight: 800; font-size: 12px; letter-spacing: 0.5px; padding: 9px; border-radius: 9px; text-transform: uppercase; }
.mode-toggle .mpill.on { background: var(--red); color: #fff; box-shadow: 0 2px 10px rgba(225,29,42,0.4); }

/* ---------- Alerts ---------- */
.alert { background: #1a1206; border: 1px solid #7c5e12; border-radius: var(--radius); padding: 14px; animation: pop .25s ease; }
.alert b { color: var(--warn); }
.alert p { margin: 6px 0 0; font-size: 13px; color: #e7d8b0; }
.alert .mango { color: var(--red-bright); font-weight: 800; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.alert.camp { background: var(--red-soft); border-color: #7f1d1d; }
.alert.camp b { color: var(--red-bright); }
.alert.camp p { color: #fca5a5; }

/* ---------- Logged workout summary ---------- */
.logged { margin: 8px 0 0 74px; font-size: 12px; color: var(--good); background: var(--good-soft); border: 1px solid #1f6f43; border-radius: 8px; padding: 7px 10px; }

/* ---------- Modal / pop card ---------- */
.modal-backdrop { position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,0.6); display: flex; align-items: flex-end; justify-content: center; opacity: 0; transition: opacity .2s ease; backdrop-filter: blur(2px); padding: 0 0 env(safe-area-inset-bottom); }
.modal-backdrop.in { opacity: 1; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-top-left-radius: 22px; border-top-right-radius: 22px; width: 100%; max-width: 680px; max-height: 88vh; overflow-y: auto; padding: 18px 18px calc(18px + env(safe-area-inset-bottom)); transform: translateY(40px) scale(.98); transition: transform .26s cubic-bezier(.2,.9,.3,1.2); }
.modal-backdrop.in .modal-card { transform: none; }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.modal-head b { font-size: 17px; font-weight: 800; }
.modal-head .x { background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); font-size: 20px; line-height: 1; border-radius: 8px; width: 32px; height: 32px; }
.modal-body { font-size: 14px; }
.modal-body p { color: var(--muted); }
.modal-actions { display: flex; gap: 8px; margin-top: 14px; }
.modal-actions .btn { flex: 1; text-align: center; padding: 13px; }
.camp-sec { margin-bottom: 14px; }
.camp-sec b { color: var(--red-bright); display: block; margin-bottom: 4px; }
.camp-sec ul { margin: 4px 0 0; padding-left: 18px; }
.camp-sec li { font-size: 13px; color: var(--txt); margin-bottom: 3px; }
@keyframes pop { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- Walk screen ---------- */
.walk-card { text-align: center; }
.walk-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 14px; }
.walk-stat { background: var(--panel-2); border: 1px solid var(--line); border-radius: 14px; padding: 18px 10px; }
.walk-stat b { display: block; font-size: 34px; font-weight: 900; color: var(--red-bright); line-height: 1; }
.walk-stat span { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
