/* ===== Tokens ===== */
:root {
  --bg: #0f1115;
  --bg-elev: #181b22;
  --bg-elev2: #1f232c;
  --line: #262a33;
  --text: #e8eaed;
  --text-soft: #a4a8b3;
  --text-mute: #6b7280;
  --primary: #34d399; /* streak green */
  --primary-dim: rgba(52, 211, 153, 0.16);
  --warn: #fbbf24;
  --danger: #f87171;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --radius: 14px;
  --radius-lg: 18px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  font-size: 16px;
  line-height: 1.5;
  overscroll-behavior-y: contain;
}
body { min-height: 100vh; min-height: 100dvh; padding-bottom: calc(100px + var(--safe-bottom)); }
button { font-family: inherit; cursor: pointer; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
.hidden { display: none !important; }

/* ===== App bar ===== */
.app-bar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 16px 12px;
  background: rgba(15, 17, 21, 0.85);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 17px; }
.logo-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--primary);
  box-shadow: 0 0 12px var(--primary);
}
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  background: transparent; border: 1px solid var(--line);
  color: var(--text); display: inline-flex; align-items: center; justify-content: center;
  transition: background 0.15s, transform 0.1s;
}
.icon-btn:hover { background: var(--bg-elev); }
.icon-btn:active { transform: scale(0.96); }
.icon-btn.small { width: 32px; height: 32px; font-size: 18px; }

/* ===== Stats ===== */
.stats {
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 10px; padding: 14px 16px 0;
}
.stat {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 12px; text-align: center;
}
.stat.streak {
  background: linear-gradient(160deg, var(--primary-dim), transparent 70%), var(--bg-elev);
  border-color: rgba(52, 211, 153, 0.35);
}
.stat-value { font-size: 24px; font-weight: 700; line-height: 1.1; letter-spacing: -0.02em; }
.stat.streak .stat-value { color: var(--primary); }
.stat-label { font-size: 12px; color: var(--text-mute); margin-top: 4px; }

/* ===== Calendar ===== */
.calendar-wrap { padding: 16px; }
.calendar-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.cal-title { font-size: 15px; font-weight: 600; color: var(--text-soft); }
.cal-weekdays {
  display: grid; grid-template-columns: repeat(7, 1fr);
  text-align: center; font-size: 11px; color: var(--text-mute);
  margin-bottom: 6px;
}
.calendar { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-day {
  aspect-ratio: 1 / 1; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; color: var(--text-soft);
  background: transparent; border: 1px solid transparent;
  position: relative;
  transition: background 0.12s, transform 0.08s;
}
.cal-day.outside { color: var(--text-mute); opacity: 0.4; }
.cal-day.today { border-color: var(--text-soft); font-weight: 700; color: var(--text); }
.cal-day.lg-empty  { background: rgba(255,255,255,0.02); }
.cal-day.lg-partial{ background: rgba(251, 191, 36, 0.22); color: #fde68a; }
.cal-day.lg-done   { background: var(--primary); color: #062b1e; font-weight: 700; }
.cal-day.lg-future { opacity: 0.3; pointer-events: none; }
.cal-day:active:not(.outside):not(.lg-future) { transform: scale(0.94); }
.cal-legend {
  margin-top: 12px; display: flex; gap: 10px; align-items: center;
  font-size: 11px; color: var(--text-mute);
}
.lg { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-right: 4px; vertical-align: middle; }
.lg-empty { background: rgba(255,255,255,0.05); border: 1px solid var(--line); }
.lg-partial { background: rgba(251, 191, 36, 0.4); }
.lg-done { background: var(--primary); }

/* ===== Today ===== */
.today { padding: 4px 16px 0; }
.today-head { display: flex; align-items: center; justify-content: space-between; margin: 8px 0 12px; }
.today-head h2 { font-size: 17px; }
.today-meta { display: flex; gap: 6px; }
.tag {
  font-size: 11px; padding: 3px 8px; border-radius: 999px;
  background: var(--bg-elev2); color: var(--text-soft); border: 1px solid var(--line);
}
.tag-warn { background: rgba(251, 191, 36, 0.18); border-color: rgba(251, 191, 36, 0.4); color: #fde68a; }

/* ===== Exercise list ===== */
.exercise-list { display: flex; flex-direction: column; gap: 10px; }
.exercise-list.small .ex-name { font-size: 14px; }
.exercise-list.small .ex-check { width: 28px; height: 28px; }
.exercise-list.small .ex-card { padding: 8px; gap: 10px; }

.ex-card {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: var(--radius-lg); padding: 10px;
  transition: border-color 0.15s, transform 0.08s;
  position: relative;
}
.ex-card.done { border-color: rgba(52, 211, 153, 0.45); background: linear-gradient(180deg, rgba(52, 211, 153, 0.06), var(--bg-elev)); }
.ex-card:active { transform: scale(0.99); }
.ex-thumb {
  width: 64px; height: 64px; border-radius: 12px;
  background: var(--bg-elev2); flex: 0 0 64px;
  background-size: cover; background-position: center;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-mute); font-size: 22px;
  overflow: hidden;
}
.ex-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ex-info { flex: 1; min-width: 0; }
.ex-name { font-size: 15px; font-weight: 600; line-height: 1.3; }
.ex-card.done .ex-name { color: var(--primary); }
.ex-note { font-size: 12px; color: var(--text-mute); margin-top: 2px; }
.ex-check {
  width: 36px; height: 36px; flex: 0 0 36px;
  border-radius: 50%; border: 2px solid var(--text-mute);
  background: transparent; color: transparent;
  display: inline-flex; align-items: center; justify-content: center;
  transition: all 0.15s;
}
.ex-check svg { width: 18px; height: 18px; }
.ex-card.done .ex-check {
  background: var(--primary); border-color: var(--primary); color: #062b1e;
  box-shadow: 0 0 0 4px rgba(52, 211, 153, 0.18);
}
.ex-edit {
  position: absolute; top: 6px; right: 56px;
  width: 28px; height: 28px; border-radius: 8px;
  background: transparent; border: 1px solid transparent;
  color: var(--text-mute); display: none; align-items: center; justify-content: center;
}
.ex-card:hover .ex-edit, .ex-card:focus-within .ex-edit { display: inline-flex; }
.ex-edit:hover { background: var(--bg-elev2); color: var(--text); border-color: var(--line); }

/* ===== Empty ===== */
.empty {
  text-align: center; padding: 40px 20px;
  background: var(--bg-elev); border: 1px dashed var(--line);
  border-radius: var(--radius-lg);
}
.empty-emoji { font-size: 40px; }
.empty-title { font-weight: 600; margin-top: 8px; }
.empty-desc { font-size: 13px; color: var(--text-mute); margin-top: 4px; }

/* ===== FAB ===== */
.fab {
  position: fixed; bottom: calc(20px + var(--safe-bottom)); right: 20px;
  width: 56px; height: 56px; border-radius: 18px;
  background: var(--primary); color: #062b1e; border: none;
  box-shadow: var(--shadow);
  display: inline-flex; align-items: center; justify-content: center;
  z-index: 20;
  transition: transform 0.12s;
}
.fab:active { transform: scale(0.93); }

/* ===== Modal ===== */
.modal {
  position: fixed; inset: 0; z-index: 30;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 16px;
  animation: fade 0.18s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal-card {
  width: 100%; max-width: 480px;
  background: var(--bg-elev);
  border-radius: var(--radius-lg);
  border: 1px solid var(--line);
  overflow: hidden;
  max-height: 86vh;
  display: flex; flex-direction: column;
  animation: slide-up 0.22s ease;
}
@keyframes slide-up { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid var(--line); }
.modal-head h3 { font-size: 16px; }
.modal-body { padding: 16px; overflow-y: auto; flex: 1; }
.modal-foot { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); background: var(--bg-elev); }
.modal-foot .spacer { flex: 1; }

/* Fields */
.field { display: block; margin-bottom: 14px; }
.field-label { display: block; font-size: 12px; color: var(--text-mute); margin-bottom: 6px; }
.field input[type="text"] {
  width: 100%; padding: 12px 14px;
  background: var(--bg-elev2); border: 1px solid var(--line); border-radius: 12px;
  color: var(--text); font-size: 15px; font-family: inherit;
  outline: none;
}
.field input[type="text"]:focus { border-color: var(--primary); }

/* Session picker (in exercise modal) */
.session-picker {
  display: flex; gap: 10px;
}
.session-chip {
  flex: 1;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 14px;
  background: var(--bg-elev2);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text-soft);
  font-size: 14px;
  transition: all 0.15s;
}
.session-chip .session-emoji { font-size: 20px; }
.session-chip:hover { border-color: var(--text-mute); color: var(--text); }
.session-chip.active {
  background: var(--primary-dim);
  border-color: var(--primary);
  color: var(--text);
}
.session-chip.active[data-session="afternoon"] { background: rgba(251, 191, 36, 0.18); border-color: rgba(251, 191, 36, 0.5); }
.session-chip.active[data-session="evening"] { background: rgba(129, 140, 248, 0.18); border-color: rgba(129, 140, 248, 0.5); }

/* Session block (on today view + day modal) */
.session-block {
  margin-bottom: 16px;
}
.session-head {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 4px 8px;
  font-size: 14px; color: var(--text-soft);
}
.session-head.compact { padding: 4px 0 6px; }
.session-head .session-emoji { font-size: 18px; }
.session-head .session-title { font-weight: 600; }
.session-head .session-progress {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-mute);
  font-variant-numeric: tabular-nums;
}
.session-head .session-progress.all-done { color: var(--primary); font-weight: 600; }
.session-block.all-done .session-head .session-title { color: var(--primary); }

/* Empty session message */
.session-empty {
  background: var(--bg-elev);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
  font-size: 12px;
  color: var(--text-mute);
}
.img-picker { display: flex; gap: 10px; align-items: center; }
.img-btn {
  flex: 1; padding: 14px; min-height: 84px;
  background: var(--bg-elev2); border: 1px dashed var(--line); border-radius: 12px;
  color: var(--text-soft); font-size: 13px;
  display: flex; align-items: center; justify-content: center;
  background-size: cover; background-position: center;
  transition: border-color 0.15s;
}
.img-btn:hover { border-color: var(--text-soft); }
#ex-image-placeholder { color: var(--text-mute); }
.img-btn.has-image { border-style: solid; border-color: var(--line); }
.img-btn.has-image #ex-image-placeholder { display: none; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  height: 40px; padding: 0 16px; border-radius: 12px;
  background: var(--bg-elev2); border: 1px solid var(--line); color: var(--text);
  font-size: 14px; font-weight: 500;
  transition: background 0.12s, transform 0.08s;
}
.btn:hover { background: var(--bg); }
.btn:active { transform: scale(0.96); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #062b1e; font-weight: 600; }
.btn-primary:hover { background: #4ade80; }
.btn-ghost { background: transparent; }
.btn-danger { background: transparent; color: var(--danger); border-color: rgba(248, 113, 113, 0.4); }
.btn-danger:hover { background: rgba(248, 113, 113, 0.1); }

/* Existing exercises in modal */
.ex-existing {
  margin-top: 12px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
}
.ex-existing-chip {
  position: relative; aspect-ratio: 1; border-radius: 10px;
  background: var(--bg-elev2); background-size: cover; background-position: center;
  overflow: hidden; border: 1px solid var(--line);
  display: flex; align-items: flex-end; padding: 4px;
}
.ex-existing-chip span {
  font-size: 10px; padding: 2px 6px; border-radius: 6px;
  background: rgba(0,0,0,0.55); color: #fff;
  width: 100%; text-align: center; line-height: 1.2;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ex-existing-chip button {
  position: absolute; top: 4px; right: 4px;
  width: 22px; height: 22px; border-radius: 50%;
  background: rgba(0,0,0,0.7); color: var(--danger); border: none;
  font-size: 12px; display: flex; align-items: center; justify-content: center;
}

/* Day modal */
.day-summary { font-size: 13px; color: var(--text-soft); margin-bottom: 10px; }

/* Settings */
.settings-row { display: flex; align-items: center; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid var(--line); gap: 12px; }
.settings-row:last-child { border-bottom: none; }
.settings-name { font-size: 14px; font-weight: 500; }
.settings-desc { font-size: 12px; color: var(--text-mute); margin-top: 2px; max-width: 340px; }
.settings-desc.danger { color: var(--danger); }
.settings-row.info .settings-desc { color: var(--text-soft); }

/* Toast */
.toast {
  position: fixed; bottom: calc(90px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.85); color: #fff;
  padding: 10px 16px; border-radius: 999px;
  font-size: 13px; z-index: 50;
  box-shadow: var(--shadow);
  animation: toast 0.2s ease;
}
@keyframes toast { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* Small phones */
@media (max-width: 360px) {
  .stat-value { font-size: 20px; }
  .ex-thumb { width: 56px; height: 56px; flex-basis: 56px; }
}

/* Reduce motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ===== Settings section title ===== */
.settings-section-title {
  font-size: 11px; color: var(--text-mute);
  text-transform: uppercase; letter-spacing: 0.08em;
  padding: 14px 0 4px;
}
.settings-section-title:first-child { padding-top: 0; }

/* ===== QR sync ===== */
.sync-help {
  font-size: 13px; color: var(--text-soft); line-height: 1.6; margin: 0 0 14px;
}
.qr-stage {
  background: #fff;
  border-radius: 14px;
  padding: 12px;
  display: flex; align-items: center; justify-content: center;
  min-height: 260px;
  width: 100%;
  max-width: 320px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
}
.qr-canvas { display: block; max-width: 100%; height: auto; image-rendering: pixelated; }
.qr-progress {
  display: flex; justify-content: space-between;
  width: 100%; max-width: 320px;
  font-size: 12px; color: var(--text-mute);
}
.qr-scan-stage {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-width: 360px;
  margin: 0 auto;
  background: #000;
  border-radius: 14px;
  overflow: hidden;
}
.qr-scan-stage video {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}
.qr-overlay {
  position: absolute; inset: 0; pointer-events: none;
}
.qr-scan-frame {
  position: absolute;
  top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 70%; aspect-ratio: 1 / 1;
  border: 2px solid rgba(52, 211, 153, 0.7);
  border-radius: 12px;
  box-shadow: 0 0 0 2000px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}
.qr-scan-frame::before, .qr-scan-frame::after {
  content: ''; position: absolute; width: 18px; height: 18px;
  border: 3px solid var(--primary);
}
.qr-scan-frame::before { top: -3px; left: -3px; border-right: none; border-bottom: none; }
.qr-scan-frame::after { bottom: -3px; right: -3px; border-left: none; border-top: none; }

.merge-summary {
  background: var(--bg-elev2);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 13px;
  line-height: 1.7;
  color: var(--text-soft);
}
.merge-summary strong { color: var(--text); }

/* ===== Day note textarea ===== */
.day-note {
  width: 100%; padding: 10px 12px;
  background: var(--bg-elev2); border: 1px solid var(--line); border-radius: 10px;
  color: var(--text); font-family: inherit; font-size: 14px; line-height: 1.5;
  resize: vertical; min-height: 60px; outline: none;
}
.day-note:focus { border-color: var(--primary); }

/* Calendar note indicator */
.cal-day.has-note::after {
  content: ''; position: absolute; bottom: 4px; left: 50%;
  transform: translateX(-50%);
  width: 4px; height: 4px; border-radius: 50%;
  background: var(--text-soft);
}
.cal-day.lg-done.has-note::after { background: rgba(6, 43, 30, 0.6); }
.cal-day.lg-partial.has-note::after { background: #fde68a; }
