/* ============================================================
   Workouts — calm & light design system
   Warm off-white ground, sage accent, native SF faces.
   Light + warm-dark themes via tokens; iPad-first layout.
   ============================================================ */

/* ---------- Tokens ---------- */
:root {
  --paper:      #F1EEE7;   /* warm off-white ground */
  --paper-2:    #E8E3D9;   /* wells / hover */
  --card:       #FBFAF6;   /* card & control surface */
  --ink:        #2E2B27;   /* primary text (warm charcoal) */
  --ink-soft:   #6F6A60;   /* secondary text */
  --ink-faint:  #A39C8E;   /* tertiary / meta */
  --sage:       #5E7D68;   /* accent */
  --sage-btn:   #4E6B58;   /* filled button (AA on white) */
  --sage-soft:  #E3EAE1;   /* selected tint */
  --line:       #E4DFD4;   /* hairline */
  --backdrop:   #141310;   /* player letterbox */
  --shadow-sm:  0 1px 2px rgba(60,52,38,0.05), 0 4px 14px rgba(60,52,38,0.05);
  --shadow-lg:  0 8px 30px rgba(50,44,32,0.14);

  --r-card: 16px;
  --r-pill: 999px;

  --ui:    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --round: ui-rounded, "SF Pro Rounded", "Hiragino Maru Gothic ProN", var(--ui);
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper:     #1A1815;
    --paper-2:   #262219;
    --card:      #24211B;
    --ink:       #ECE7DD;
    --ink-soft:  #ABA495;
    --ink-faint: #726B5C;
    --sage:      #8FAB95;
    --sage-btn:  #6E9078;
    --sage-soft: #2B3129;
    --line:      #322D25;
    --backdrop:  #0D0C0A;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3), 0 4px 14px rgba(0,0,0,0.25);
    --shadow-lg: 0 10px 34px rgba(0,0,0,0.5);
  }
}
:root[data-theme="light"] {
  --paper:#F1EEE7; --paper-2:#E8E3D9; --card:#FBFAF6; --ink:#2E2B27; --ink-soft:#6F6A60;
  --ink-faint:#A39C8E; --sage:#5E7D68; --sage-btn:#4E6B58; --sage-soft:#E3EAE1; --line:#E4DFD4;
  --backdrop:#141310; --shadow-sm:0 1px 2px rgba(60,52,38,0.05),0 4px 14px rgba(60,52,38,0.05);
  --shadow-lg:0 8px 30px rgba(50,44,32,0.14);
}
:root[data-theme="dark"] {
  --paper:#1A1815; --paper-2:#262219; --card:#24211B; --ink:#ECE7DD; --ink-soft:#ABA495;
  --ink-faint:#726B5C; --sage:#8FAB95; --sage-btn:#6E9078; --sage-soft:#2B3129; --line:#322D25;
  --backdrop:#0D0C0A; --shadow-sm:0 1px 2px rgba(0,0,0,0.3),0 4px 14px rgba(0,0,0,0.25);
  --shadow-lg:0 10px 34px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--ui);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior-y: contain;
}
button { font-family: inherit; border: none; background: none; color: inherit; cursor: pointer; }
input { font-family: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--sage); outline-offset: 3px; border-radius: 8px; }
.hidden { display: none !important; }

#app { height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.screen { height: 100%; display: flex; flex-direction: column; min-height: 0; }

/* ---------- Login ---------- */
.login {
  align-items: center; justify-content: center; padding: 32px;
  background:
    radial-gradient(120% 80% at 50% -15%, color-mix(in srgb, var(--sage) 15%, transparent), transparent 60%),
    var(--paper);
}
.login-card {
  width: 100%; max-width: 380px; text-align: center;
  display: flex; flex-direction: column; align-items: center;
}
.mark {
  width: 60px; height: 60px; border-radius: 20px; margin-bottom: 16px;
  display: grid; place-items: center; color: #fff;
  background: linear-gradient(150deg, var(--sage), var(--sage-btn));
  box-shadow: var(--shadow-lg);
}
.mark svg { width: 30px; height: 30px; }
.brand { font-family: var(--round); font-weight: 700; font-size: 34px; letter-spacing: -0.02em; margin: 0; }
.sub { color: var(--ink-soft); font-size: 15.5px; line-height: 1.5; margin: 8px 0 26px; max-width: 300px; }
.btn-primary {
  width: 100%; padding: 15px 20px; border-radius: 14px;
  background: var(--sage-btn); color: #fff; font-weight: 600; font-size: 16px;
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  box-shadow: var(--shadow-sm);
  transition: transform .15s ease, background .15s ease;
}
.btn-primary:hover { background: var(--sage); }
.btn-primary:active { transform: translateY(1px) scale(0.995); }
.error-text { color: #b4432f; font-size: 13.5px; margin-top: 18px; line-height: 1.5; }
.config-warning { color: var(--ink-soft); font-size: 12.5px; margin-top: 18px; line-height: 1.55; }
.config-warning code {
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 11.5px;
  background: var(--paper-2); padding: 1px 5px; border-radius: 5px; color: var(--ink);
}
.login-foot { color: var(--ink-faint); font-size: 12.5px; margin-top: 22px; }

/* ---------- Topbar ---------- */
.topbar {
  flex-shrink: 0; z-index: 20;
  background: color-mix(in srgb, var(--paper) 85%, transparent);
  backdrop-filter: saturate(1.2) blur(14px);
  -webkit-backdrop-filter: saturate(1.2) blur(14px);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.topbar-inner { max-width: 1180px; width: 100%; margin: 0 auto; padding: 16px 24px 12px; }
.topbar-row { display: flex; align-items: center; gap: 14px; }
.head-title { min-width: 0; }
.greeting { font-size: 13px; color: var(--ink-soft); letter-spacing: 0.02em; margin: 0 0 1px; }
.title-lg { font-family: var(--round); font-weight: 700; font-size: 26px; letter-spacing: -0.02em; margin: 0; white-space: nowrap; }
.spacer { flex: 1; }

.search { position: relative; flex: 1; max-width: 340px; min-width: 0; display: block; }
.search svg { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); width: 17px; height: 17px; color: var(--ink-faint); }
.search input {
  width: 100%; padding: 11px 14px 11px 38px; font-size: 15px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--r-pill);
  box-shadow: var(--shadow-sm); transition: border-color .15s ease;
}
.search input::placeholder { color: var(--ink-faint); }
.search input:focus { outline: none; border-color: var(--sage); }
.search input::-webkit-search-cancel-button { -webkit-appearance: none; }

.icon-btn {
  width: 44px; height: 44px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; color: var(--ink-soft);
  background: var(--card); border: 1px solid var(--line); box-shadow: var(--shadow-sm);
  transition: color .15s ease, transform .15s ease;
}
.icon-btn:hover { color: var(--ink); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { width: 19px; height: 19px; }

/* ---------- Breadcrumb ---------- */
.breadcrumb {
  display: flex; align-items: center; gap: 4px; margin-top: 12px;
  font-size: 14px; overflow-x: auto; white-space: nowrap; scrollbar-width: none;
}
.breadcrumb::-webkit-scrollbar { display: none; }
.breadcrumb button {
  color: var(--ink-soft); padding: 5px 11px; border-radius: var(--r-pill);
  background: var(--card); border: 1px solid var(--line); font-size: 13.5px; font-weight: 500;
  transition: color .15s ease;
}
.breadcrumb button:hover { color: var(--ink); }
.breadcrumb .crumb-current { color: var(--ink); font-weight: 600; padding: 5px 4px; }
.breadcrumb .crumb-sep { color: var(--ink-faint); font-size: 13px; }

/* ---------- Library ---------- */
#browser-screen { position: relative; } /* anchor for the pull-to-refresh indicator */
.scroll-area { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; min-height: 0; overscroll-behavior: contain; }

/* Pull-to-refresh indicator — a calm sage spinner that follows the drag */
.pull-indicator {
  position: absolute; left: 50%; z-index: 15;
  width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--card); border: 1px solid var(--line); box-shadow: var(--shadow-sm);
  transform: translate(-50%, -46px); opacity: 0;
  pointer-events: none;
}
.pull-indicator.ready { border-color: var(--sage); }
.pull-spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2.5px solid var(--line); border-top-color: var(--sage);
}
.pull-indicator.refreshing .pull-spinner { animation: spin 0.8s linear infinite; }

/* spin the manual refresh button while it reloads */
.icon-btn.spinning svg { animation: spin 0.8s linear infinite; }
.library { max-width: 1180px; margin: 0 auto; padding: 22px 24px calc(60px + env(safe-area-inset-bottom)); }
.section-label {
  font-size: 12.5px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--ink-faint); margin: 6px 2px 14px;
}
#collections-wrap { margin-bottom: 34px; }

.grid { display: grid; gap: 22px; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }

.card {
  text-align: left; padding: 0; border-radius: var(--r-card); width: 100%;
  display: flex; flex-direction: column; gap: 10px;
  transition: transform .18s ease;
}
.card:hover { transform: translateY(-3px); }
.card:hover .thumb { box-shadow: var(--shadow-lg); }
.card:active { transform: translateY(-1px); }

.thumb {
  position: relative; aspect-ratio: 16 / 10; border-radius: var(--r-card); overflow: hidden;
  box-shadow: var(--shadow-sm); background-color: var(--paper-2);
  background-size: cover; background-position: center;
  transition: box-shadow .18s ease;
}
.thumb::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(180deg, transparent 55%, rgba(20,16,10,0.28));
}
.thumb .dur {
  position: absolute; right: 8px; bottom: 8px; z-index: 2;
  font-family: var(--round); font-variant-numeric: tabular-nums; font-size: 12px; font-weight: 600;
  color: #fff; background: rgba(20,18,14,0.55); backdrop-filter: blur(4px);
  padding: 3px 8px; border-radius: var(--r-pill);
}
.thumb .play { position: absolute; inset: 0; z-index: 2; display: grid; place-items: center; opacity: 0; transition: opacity .18s ease; }
.card:hover .thumb .play { opacity: 1; }
.play-dot {
  width: 52px; height: 52px; border-radius: 50%;
  background: rgba(255,255,255,0.92); color: var(--sage-btn);
  display: grid; place-items: center; box-shadow: var(--shadow-lg);
}
.play-dot svg { width: 22px; height: 22px; margin-left: 3px; }

.card-body { display: flex; flex-direction: column; gap: 3px; padding: 0 2px; min-width: 0; }
.card-title { font-size: 15.5px; font-weight: 600; line-height: 1.3; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.card-meta { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.card-meta:empty { display: none; }
.cat-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--sage); flex-shrink: 0; }

/* folder / collection cards */
.folder .thumb {
  display: grid; place-items: center;
  background-image: linear-gradient(160deg, var(--sage-soft), color-mix(in srgb, var(--sage-soft) 55%, var(--card)));
}
.folder .thumb::after { display: none; }
.folder .thumb svg { width: 46px; height: 46px; color: var(--sage); }

.status-bar {
  max-width: 1180px; margin: 12px auto -6px; padding: 10px 16px;
  font-size: 13.5px; color: var(--ink-soft);
  background: var(--sage-soft); border-radius: 12px; width: calc(100% - 48px);
}
.status-bar.error { color: #b4432f; background: color-mix(in srgb, #b4432f 10%, var(--card)); }

.empty { text-align: center; color: var(--ink-soft); padding: 70px 20px; font-size: 15px; }
.loading { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 80px 20px; color: var(--ink-soft); font-size: 14px; }
.spinner {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2.5px solid var(--line); border-top-color: var(--sage);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Player ---------- */
.player-screen { position: fixed; inset: 0; z-index: 60; background: var(--backdrop); justify-content: center; align-items: center; animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.stage { width: 100%; height: 100%; display: grid; place-items: center; }
.player-video { width: 100%; height: 100%; max-height: 100dvh; object-fit: contain; background: #000; }

.player-bar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 3;
  display: flex; align-items: center; gap: 12px;
  padding: max(16px, env(safe-area-inset-top)) 18px 34px;
  background: linear-gradient(180deg, rgba(10,9,7,0.62), transparent);
  color: #fff;
}
.p-title { flex: 1; min-width: 0; }
.p-title .t { font-family: var(--round); font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.p-title .m { font-size: 12.5px; opacity: 0.72; }
.p-btn { width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0; display: grid; place-items: center; color: #fff; background: rgba(255,255,255,0.14); transition: background .15s ease, opacity .15s ease; }
.p-btn:hover { background: rgba(255,255,255,0.24); }
.p-btn:disabled { opacity: 0.3; pointer-events: none; }
.p-btn svg { width: 20px; height: 20px; }
.speed { height: 42px; padding: 0 15px; border-radius: var(--r-pill); background: rgba(255,255,255,0.14); color: #fff; font-family: var(--round); font-weight: 600; font-size: 14px; font-variant-numeric: tabular-nums; transition: background .15s ease; }
.speed:hover { background: rgba(255,255,255,0.24); }
.player-nav { display: flex; gap: 8px; }
.player-error {
  position: absolute; bottom: calc(28px + env(safe-area-inset-bottom)); left: 18px; right: 18px; z-index: 4;
  background: rgba(180,67,47,0.16); border: 1px solid #b4432f; color: #fff;
  font-size: 13.5px; padding: 12px 14px; border-radius: 12px; text-align: center;
}

/* ---------- Download control (card badge + player button) ---------- */
.dl-badge {
  position: absolute; top: 8px; left: 8px; z-index: 3;
  width: 30px; height: 30px; border-radius: 50%; padding: 0;
  display: grid; place-items: center; color: #fff;
  background: rgba(20,18,14,0.5); backdrop-filter: blur(4px);
  transition: opacity .15s ease, background .15s ease;
}
.dl-badge .dl-ico { grid-area: 1 / 1; display: none; line-height: 0; }
.dl-badge svg, .dl-btn svg { display: block; }
.dl-badge svg { width: 15px; height: 15px; }
.dl-badge[data-state="idle"] { opacity: 0; }
.card:hover .dl-badge[data-state="idle"],
.card:focus-visible .dl-badge[data-state="idle"] { opacity: 1; }
.dl-badge[data-state="idle"] .dl-arrow { display: block; }
.dl-badge[data-state="saved"] { background: var(--sage-btn); opacity: 1; }
.dl-badge[data-state="saved"] .dl-check { display: block; }
.dl-badge[data-state="downloading"] {
  opacity: 1; color: transparent;
  background: conic-gradient(#fff var(--p, 0%), rgba(255,255,255,0.28) 0);
}
.dl-badge[data-state="downloading"]::after {
  content: ""; position: absolute; inset: 4px; border-radius: 50%;
  background: rgba(20,18,14,0.82);
}

/* player download button reuses .p-btn; swap icons + progress ring by state */
.dl-btn .dl-ico { grid-area: 1 / 1; display: none; line-height: 0; }
.dl-btn { display: grid; place-items: center; }
.dl-btn svg { width: 20px; height: 20px; }
.dl-btn[data-state="idle"] .dl-arrow { display: block; }
.dl-btn[data-state="saved"] { background: var(--sage-btn); }
.dl-btn[data-state="saved"] .dl-check { display: block; }
.dl-btn[data-state="downloading"] {
  background: conic-gradient(rgba(255,255,255,0.92) var(--p, 0%), rgba(255,255,255,0.2) 0);
}
.dl-btn[data-state="downloading"] .dl-arrow { display: block; opacity: 0.45; }

/* ---------- Workout details ---------- */
.info-btn { position: relative; }
.info-btn.has-note::after {
  content: ""; position: absolute; top: 7px; right: 7px;
  width: 7px; height: 7px; border-radius: 50%; background: var(--sage);
  box-shadow: 0 0 0 2px rgba(10,9,7,0.5);
}
/* ⓘ badge on cards that have a details note (opens the info sheet without playing) */
.info-badge {
  position: absolute; top: 8px; right: 8px; z-index: 3; padding: 0;
  width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center; color: #fff;
  background: rgba(20,18,14,0.5); backdrop-filter: blur(4px);
}
.info-badge svg { width: 16px; height: 16px; display: block; }

/* Touch devices have no hover — keep the tap-to-download badge visible there */
@media (hover: none) {
  .dl-badge[data-state="idle"] { opacity: 1; }
}

.details-play {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  margin-top: 18px; background: var(--sage-btn); color: #fff;
}
.details-play svg { width: 16px; height: 16px; }
.details-play:active { filter: brightness(0.95); }

.detail-chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 0; }
.detail-chips:empty { display: none; }
.detail-chip {
  padding: 5px 12px; border-radius: var(--r-pill); font-size: 13px; font-weight: 600;
  background: var(--sage-soft); color: var(--sage-btn);
}
.detail-chip.diff { background: transparent; border: 1px solid var(--line); color: var(--ink-soft); text-transform: capitalize; }
.detail-chip.len { background: var(--paper-2); color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.detail-body { color: var(--ink); font-size: 14.5px; line-height: 1.55; margin-top: 16px; max-height: 46vh; overflow-y: auto; }
.detail-body:empty { margin-top: 0; }
.detail-body h3, .detail-body h4, .detail-body h5 { font-family: var(--round); margin: 15px 0 6px; line-height: 1.25; }
.detail-body h3 { font-size: 17px; } .detail-body h4 { font-size: 15px; } .detail-body h5 { font-size: 14px; }
.detail-body p { margin: 8px 0; }
.detail-body ul { margin: 8px 0; padding-left: 20px; }
.detail-body li { margin: 3px 0; }
.detail-body strong { font-weight: 700; }
.detail-body .hint { color: var(--ink-soft); font-size: 13.5px; line-height: 1.5; }

/* ---------- Downloads sheet ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 70; display: flex;
  align-items: flex-end; justify-content: center;
  background: rgba(20,18,14,0.42); animation: fade .2s ease;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet-panel {
  background: var(--card); width: 100%; max-width: 440px;
  border-radius: 22px 22px 0 0; padding: 20px 20px 26px;
  box-shadow: var(--shadow-lg);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h3 { font-family: var(--round); font-size: 20px; letter-spacing: -0.01em; margin: 0; }
.sheet-close { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; color: var(--ink-soft); }
.sheet-close:active { background: var(--paper-2); }
.sheet-close svg { width: 18px; height: 18px; }
.sheet-sub { color: var(--ink-soft); font-size: 13.5px; line-height: 1.5; margin: 6px 0 16px; }
.storage-line { font-size: 14px; color: var(--ink); font-variant-numeric: tabular-nums; margin: 0 0 8px; }
.storage-meter { height: 8px; border-radius: 999px; background: var(--paper-2); overflow: hidden; margin-bottom: 20px; }
.storage-meter span { display: block; height: 100%; width: 0; background: var(--sage); border-radius: 999px; transition: width .3s ease; }
.sheet-actions { display: flex; flex-direction: column; gap: 10px; }
.sheet-btn { padding: 13px; border-radius: 12px; background: var(--paper-2); color: var(--ink); font-weight: 600; font-size: 15px; transition: filter .15s ease; }
.sheet-btn:active { filter: brightness(0.96); }
.sheet-btn.danger { color: #b4432f; }
.sheet-btn:disabled { opacity: 0.45; pointer-events: none; }
@media (min-width: 640px) {
  .sheet { align-items: center; }
  .sheet-panel { border-radius: 20px; margin: 20px; }
}

/* ---------- Responsive ---------- */
@media (max-width: 620px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(148px, 1fr)); gap: 16px; }
  .search { max-width: none; }
  .title-lg { font-size: 22px; }
  .topbar-inner { padding: 14px 16px 10px; }
  .library { padding: 18px 16px 60px; }
}

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