:root {
  --bg: #0d0f12;
  --bg-elevated: #16191d;
  --bg-card: #1c2126;
  --border: #2a2f36;
  --text: #f2f3f5;
  --text-dim: #9aa1ab;
  --text-faint: #666d77;
  --accent: #ff5722;
  --accent-dim: #ff572233;
  --success: #4caf50;
  --danger: #e5484d;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

body {
  padding-bottom: calc(64px + var(--safe-bottom));
}

h1, h2, h3, p {
  margin: 0;
}

button {
  font-family: inherit;
  touch-action: manipulation;
}

input, select {
  font-family: inherit;
}

@keyframes view-fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.view-fade {
  animation: view-fade-in 0.22s ease;
}

@media (prefers-reduced-motion: reduce) {
  .view-fade { animation: none; }
}

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100vh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 14px;
  background: var(--bg);
}

.topbar h1 {
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.topbar .back {
  background: none;
  border: none;
  color: var(--text);
  font-size: 16px;
  padding: 8px 0;
  display: flex;
  align-items: center;
  gap: 4px;
}

.view {
  padding: 0 20px 24px;
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.06s ease, opacity 0.15s ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  width: 100%;
}

.btn-secondary {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
  width: 100%;
}

.btn-ghost {
  background: none;
  color: var(--accent);
  padding: 10px 12px;
}

.btn-danger {
  background: none;
  color: var(--danger);
  padding: 10px 12px;
}

.btn-small {
  padding: 8px 14px;
  font-size: 14px;
  border-radius: 10px;
}

.btn-icon {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  width: 40px;
  height: 40px;
  border-radius: 10px;
  padding: 0;
  font-size: 18px;
}

/* ---- Cards ---- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.card-tap {
  cursor: pointer;
  transition: transform 0.06s ease, background 0.12s ease;
}

.card-tap:active {
  background: var(--bg-elevated);
  transform: scale(0.985);
}

.stat-row {
  display: flex;
  gap: 10px;
  margin-bottom: 18px;
}

.stat {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 12px;
  text-align: center;
}

.stat .num {
  font-size: 24px;
  font-weight: 800;
  color: var(--accent);
  display: block;
}

.stat .label {
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.section-title {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  margin: 22px 0 10px;
}

.routine-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.routine-card .info h3 {
  font-size: 17px;
  font-weight: 700;
  margin-bottom: 4px;
}

.routine-card .info p {
  font-size: 13px;
  color: var(--text-dim);
}

.chip-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  margin-bottom: 4px;
}

.chip {
  flex-shrink: 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 20px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  transition: transform 0.08s ease;
}

.chip:active {
  transform: scale(0.94);
}

.chip-active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-dim);
}

.empty-state .big-icon {
  font-size: 40px;
  margin-bottom: 10px;
  display: block;
}

/* ---- Forms ---- */
.field {
  margin-bottom: 16px;
}

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.field input[type="text"],
.field input[type="number"],
.field select {
  width: 100%;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  color: var(--text);
  font-size: 16px;
}

.field input:focus,
.field select:focus {
  outline: none;
  border-color: var(--accent);
}

/* ---- Exercise picker list ---- */
.picker-list {
  max-height: 50vh;
  overflow-y: auto;
  margin: 12px 0;
}

.picker-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 4px;
  border-bottom: 1px solid var(--border);
}

.picker-item .name {
  font-weight: 600;
}

.picker-item .muscle {
  font-size: 12px;
  color: var(--text-dim);
}

.picker-item input[type="checkbox"] {
  width: 22px;
  height: 22px;
  accent-color: var(--accent);
}

.routine-exercise-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg-elevated);
  border-radius: 10px;
  margin-bottom: 8px;
}

.routine-exercise-row > span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.reorder-controls {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.reorder-btn {
  width: 28px;
  height: 28px;
  font-size: 11px;
}

.reorder-btn:disabled {
  opacity: 0.25;
}

.reorder-btn:active:not(:disabled) {
  transform: scale(0.88);
}

/* ---- Supersets ---- */
.superset-block {
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 12px 12px 4px;
  margin-bottom: 12px;
  background: rgba(255, 87, 34, 0.05);
}

.superset-label {
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent);
  margin-bottom: 8px;
}

.superset-block .exercise-block {
  margin-bottom: 14px;
}

.superset-block .routine-exercise-row {
  margin-bottom: 6px;
}

.block-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 10px;
}

.link-next-btn {
  display: block;
  width: 100%;
  background: none;
  border: 1px dashed var(--border);
  color: var(--text-dim);
  border-radius: 10px;
  padding: 8px;
  font-size: 12px;
  font-weight: 700;
  margin-bottom: 12px;
}

.link-next-btn:active {
  transform: scale(0.98);
}

/* ---- Active workout ---- */
.workout-header {
  padding: 16px 20px;
  background: var(--bg-elevated);
  position: sticky;
  top: 0;
  z-index: 10;
}

.workout-header .title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.workout-header h2 {
  font-size: 19px;
  font-weight: 800;
}

.workout-timer {
  font-size: 13px;
  color: var(--text-dim);
  margin-top: 2px;
}

.exercise-block {
  margin-bottom: 20px;
}

.exercise-block h3 {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 4px;
}

.exercise-block .last-time {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 10px;
}

.set-table-head,
.set-row {
  display: grid;
  grid-template-columns: 30px 1fr 1fr 28px;
  gap: 6px;
  align-items: center;
  padding: 6px 0;
}

.set-table-head {
  font-size: 11px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.set-row {
  border-radius: 10px;
  transition: background 0.15s ease;
}

.set-row.set-row-done {
  background: rgba(76, 175, 80, 0.08);
}

.set-row.set-row-pr {
  background: rgba(255, 87, 34, 0.1);
}

.set-row .set-num {
  color: var(--text-dim);
  font-weight: 700;
  text-align: center;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 28px;
  height: 28px;
  padding: 0;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s ease, background 0.15s ease, color 0.15s ease;
}

.set-row.set-row-done .set-num {
  background: var(--success);
  border-color: var(--success);
  color: #fff;
}

.set-row .set-num:active {
  transform: scale(0.88);
}

.stepper-group {
  display: flex;
  align-items: center;
  gap: 3px;
}

.stepper-btn {
  flex-shrink: 0;
  width: 24px;
  height: 34px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 16px;
  line-height: 1;
  padding: 0;
}

.stepper-btn:active {
  background: var(--border);
  transform: scale(0.92);
}

.set-row input {
  width: 100%;
  min-width: 0;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 4px;
  color: var(--text);
  font-size: 15px;
  text-align: center;
}

.set-row .set-remove {
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 16px;
  padding: 6px;
}

.set-row .set-remove:active {
  transform: scale(0.88);
}

.pr-tag {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  padding: 0 0 6px 36px;
  margin-top: -4px;
}

.pr-badge {
  color: var(--accent);
  font-weight: 700;
}

/* ---- Cardio logging ---- */
.cardio-set-row {
  background: var(--bg-elevated);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 8px;
  transition: background 0.15s ease;
}

.cardio-set-row.cardio-set-row-done {
  background: rgba(76, 175, 80, 0.08);
}

.cardio-set-row.cardio-set-row-pr {
  background: rgba(255, 87, 34, 0.1);
}

.cardio-set-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.cardio-fields {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.cardio-field label {
  display: block;
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-faint);
  margin-bottom: 4px;
}

.cardio-field input {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 4px;
  color: var(--text);
  font-size: 15px;
  text-align: center;
}

.add-set-btn {
  width: 100%;
  margin-top: 4px;
  background: var(--bg-elevated);
  border: 1px dashed var(--border);
  color: var(--accent);
  border-radius: 10px;
  padding: 10px;
  font-weight: 700;
  font-size: 14px;
}

.finish-bar {
  position: sticky;
  bottom: calc(64px + var(--safe-bottom));
  padding: 12px 20px;
  background: linear-gradient(to top, var(--bg) 60%, transparent);
}

/* ---- Bottom nav ---- */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(64px + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 20;
}

.bottom-nav.hidden {
  display: none;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 11px;
  font-weight: 600;
  transition: transform 0.08s ease, color 0.15s ease;
}

.nav-item:active {
  transform: scale(0.92);
}

.nav-item .icon {
  font-size: 20px;
  transition: transform 0.15s ease;
}

.nav-item.active {
  color: var(--accent);
}

.nav-item.active .icon {
  transform: translateY(-1px);
}

/* ---- History ---- */
.history-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.history-item .date {
  font-size: 12px;
  color: var(--text-dim);
}

.history-item h3 {
  font-size: 16px;
  font-weight: 700;
  margin: 2px 0 6px;
}

.history-item .summary {
  font-size: 13px;
  color: var(--text-dim);
}

.detail-set-list {
  font-size: 14px;
  color: var(--text-dim);
  margin-top: 4px;
}

.detail-set-list div {
  padding: 2px 0;
}

/* ---- Exercise progress ---- */
.pr-row {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}

.chart-wrap {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}

.volume-bar-row {
  display: grid;
  grid-template-columns: 96px 1fr 46px;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.volume-bar-label {
  font-size: 13px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.volume-bar-track {
  height: 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.volume-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 6px;
}

.volume-bar-value {
  font-size: 12px;
  color: var(--text-dim);
  text-align: right;
}

/* ---- Badges ---- */
.badge-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}

.badge-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 4px 10px;
  transition: transform 0.08s ease;
}

.badge-tile:active {
  transform: scale(0.94);
}

.badge-tile.badge-earned {
  border-color: var(--accent);
  background: rgba(255, 87, 34, 0.08);
}

.badge-tile.badge-locked {
  opacity: 0.5;
}

.badge-icon {
  font-size: 24px;
  line-height: 1;
}

.badge-name {
  font-size: 9px;
  font-weight: 700;
  text-align: center;
  color: var(--text-dim);
  line-height: 1.25;
}

.badge-tile.badge-earned .badge-name {
  color: var(--text);
}

/* ---- Plate calculator ---- */
.plate-viz {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 18px 0;
  overflow-x: auto;
  padding: 4px;
}

.plate-stack {
  display: flex;
  align-items: center;
  gap: 2px;
}

.plate-stack-left {
  flex-direction: row-reverse;
}

.bar-segment {
  width: 46px;
  height: 8px;
  background: var(--text-faint);
  border-radius: 2px;
  margin: 0 2px;
  flex-shrink: 0;
}

.plate {
  width: 16px;
  flex-shrink: 0;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  writing-mode: vertical-rl;
  font-size: 9px;
  font-weight: 700;
}

/* ---- Modal / sheet ---- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 30;
  display: flex;
  align-items: flex-end;
  opacity: 0;
  transition: opacity 0.22s ease;
}

.sheet-backdrop.hidden {
  display: none;
}

.sheet-backdrop.open {
  opacity: 1;
}

.sheet {
  background: var(--bg-elevated);
  border-radius: 20px 20px 0 0;
  padding: 20px 20px calc(24px + var(--safe-bottom));
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  transform: translateY(24px);
  transition: transform 0.22s cubic-bezier(0.32, 0.72, 0, 1);
}

.sheet-backdrop.open .sheet {
  transform: translateY(0);
}

.sheet h2 {
  font-size: 18px;
  font-weight: 800;
  margin-bottom: 4px;
}

.toast {
  position: fixed;
  bottom: calc(84px + var(--safe-bottom));
  left: 50%;
  transform: translate(-50%, 8px);
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 30px;
  font-size: 14px;
  font-weight: 600;
  z-index: 50;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---- Rest timer bar ---- */
.rest-bar {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(84px + var(--safe-bottom));
  background: var(--bg-elevated);
  border: 1px solid var(--accent);
  border-radius: 16px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 25;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  animation: view-fade-in 0.2s ease;
}

.rest-label {
  font-weight: 700;
  font-size: 15px;
}

.rest-controls {
  display: flex;
  gap: 6px;
}

.rest-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 700;
}

.rest-btn:active {
  transform: scale(0.92);
}

.rest-btn.rest-skip {
  color: var(--accent);
}

.row-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.text-dim {
  color: var(--text-dim);
  font-size: 13px;
}

.divider {
  height: 1px;
  background: var(--border);
  margin: 18px 0;
}

.file-input {
  display: none;
}
