:root {
  --safe-top: env(safe-area-inset-top);
  --bg: #0f1115;
  --surface: #1a1d24;
  --surface-2: #242832;
  --border: #2e333f;
  --text: #ecedef;
  --text-dim: #9aa0ad;
  --primary: #4f8cff;
  --primary-text: #ffffff;
  --danger: #ef5350;
  --success: #66bb6a;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.45);
  --radius: 10px;
  --tap: 48px;
}

* { 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 Neue", Arial, sans-serif;
  font-size: 17px;
  line-height: 1.4;
  min-height: 100vh;
  -webkit-text-size-adjust: 100%;
}
body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

h1, h2, h3 { margin: 0 0 12px; line-height: 1.2; }
h1 { font-size: 22px; }
h2 { font-size: 20px; }
h3 { font-size: 17px; color: var(--text-dim); margin-top: 18px; }
p { margin: 0 0 8px; }
button { font: inherit; }

.hidden { display: none !important; }

#top-bar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px 16px 0;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
}
#top-bar .top-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.user-area {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}
.user-area .user-name {
  color: var(--text-dim);
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.btn-logout {
  padding: 6px 10px;
  min-height: 36px;
  font-size: 13px;
  font-weight: 600;
}

#tabs {
  display: flex;
  gap: 6px;
  border-bottom: 1px solid var(--border);
}
.tab {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 12px 0;
  font-weight: 600;
  font-size: 16px;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  min-height: var(--tap);
}
.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

#main { padding: 16px; }
.view { display: block; }

.btn-primary, .btn-secondary, .btn-danger {
  border: none;
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  min-height: var(--tap);
  color: var(--primary-text);
}
.btn-primary { background: var(--primary); }
.btn-primary:hover { filter: brightness(1.1); }
.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
}
.btn-danger { background: var(--danger); }
.btn-primary:disabled, .btn-secondary:disabled, .btn-danger:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn-primary.block, .btn-secondary.block { display: block; width: 100%; margin: 0 0 14px; }
.big { font-size: 22px; padding: 18px; }

.card-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.card-list.compact { gap: 6px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.card-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.card-title {
  font-weight: 700;
  font-size: 17px;
  margin: 0;
  flex: 1;
  word-break: break-word;
}
.card-meta {
  color: var(--text-dim);
  font-size: 13px;
}
.card-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.card-actions button {
  flex: 1;
  min-width: 90px;
  padding: 10px;
  font-size: 14px;
}

.exercise-card-row {
  display: flex;
  gap: 10px;
  align-items: center;
}
.exercise-thumb {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 8px;
  background: var(--surface-2);
  flex-shrink: 0;
}
.exercise-thumb.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 12px;
}

.empty {
  color: var(--text-dim);
  text-align: center;
  margin: 24px 0;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  padding: 0;
}
.modal-content {
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  width: 100%;
  max-width: 560px;
  max-height: 92vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
@media (min-width: 600px) {
  .modal { align-items: center; padding: 20px; }
  .modal-content { border-radius: var(--radius); max-height: 88vh; }
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 8px 0;
  border: none;
  padding: 0;
}
.field legend, .field > span {
  font-weight: 600;
  font-size: 14px;
  color: var(--text-dim);
}
.field input[type="text"],
.field input[type="password"],
.field input[type="email"],
.field input[type="number"],
.field input:not([type]),
.field textarea,
.field select {
  width: 100%;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font: inherit;
  min-height: var(--tap);
}
.field textarea { min-height: 60px; resize: vertical; }
.field input[type="file"] {
  background: var(--surface-2);
  color: var(--text);
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.field.radio, .radio { flex-direction: row; align-items: center; gap: 8px; }
.radio { font-size: 16px; padding: 4px 0; min-height: var(--tap); }

fieldset.field { display: flex; flex-direction: column; gap: 2px; }
fieldset.field legend { padding: 0 0 4px; }

.img-preview {
  max-width: 100%;
  max-height: 180px;
  border-radius: 8px;
  margin: 8px 0;
  object-fit: contain;
  background: var(--surface-2);
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}
.modal-actions button { flex: 1; }
.modal-error {
  background: rgba(239, 83, 80, 0.15);
  color: #ffbcb9;
  border: 1px solid rgba(239, 83, 80, 0.4);
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 14px;
  margin: 8px 0 0;
}

/* Workout-edit items list */
.workout-items {
  list-style: none;
  margin: 0 0 8px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.workout-item {
  background: var(--surface-2);
  border-radius: 8px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.workout-item-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.workout-item-name {
  font-weight: 600;
  flex: 1;
  word-break: break-word;
}
.workout-item-controls {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}
.workout-item-controls button {
  min-width: 40px;
  min-height: 40px;
  padding: 6px 10px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
}
.workout-item-controls button.danger { color: var(--danger); }
.workout-item-sets {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-dim);
}
.workout-item-sets input {
  width: 70px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px;
  font: inherit;
  text-align: center;
}
.workout-item-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-dim);
}
.workout-item-override,
.workout-item-rest {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  align-items: center;
  font-size: 14px;
  color: var(--text-dim);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
}
.workout-item-override label,
.workout-item-rest label {
  display: flex;
  align-items: center;
  gap: 6px;
}
.workout-item-override input[type="number"],
.workout-item-rest input[type="number"] {
  width: 80px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
  font: inherit;
  text-align: center;
}
.workout-item-mode {
  display: flex;
  gap: 12px;
  border: none;
  padding: 0;
  margin: 0;
  width: 100%;
}
.workout-item-mode legend { display: none; }
.workout-item-ov-fields {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
.share-user-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 16px;
}
.share-user-row input[type="checkbox"] {
  width: 22px;
  height: 22px;
}

/* Run mode */
.run {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 50;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
}
.btn-quit {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 8px);
  right: 12px;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 32px;
  width: var(--tap);
  height: var(--tap);
  cursor: pointer;
  z-index: 1;
}
.run-inner {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  text-align: center;
  padding-top: clamp(40px, 6vh, 64px);
  gap: clamp(6px, 1.2vh, 14px);
  max-width: min(900px, 95vw);
  width: 100%;
  margin: 0 auto;
}
.run-progress { color: var(--text-dim); font-size: clamp(13px, 1.6vh, 18px); }
.run-name { font-size: clamp(22px, 4vh, 44px); margin: 4px 0 0; }
.run-set { color: var(--text-dim); font-size: clamp(14px, 2vh, 22px); margin: 0; }
.run-image {
  flex: 1 1 0;
  min-height: 0;
  width: 100%;
  object-fit: contain;
  margin: 4px 0;
  cursor: zoom-in;
}
.image-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
  cursor: zoom-out;
}
.image-overlay img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
.run-desc { color: var(--text-dim); font-size: clamp(13px, 1.8vh, 20px); max-width: 640px; }
.run-counter {
  font-size: clamp(72px, 16vh, 180px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  margin: clamp(6px, 1.5vh, 16px) 0;
  letter-spacing: -2px;
}
.run-counter.small { font-size: clamp(48px, 11vh, 130px); }
.run-status {
  font-size: clamp(15px, 2vh, 22px);
  color: var(--text-dim);
  min-height: 1.4em;
  margin: 0;
}
.run-counter.prep { color: #ffd54f; }
.run-counter.active { color: var(--primary); }
.run-counter.done { color: var(--success); }
.run-counter.rest { color: #8ab4ff; }
#run-action-btn {
  margin-top: auto;
  margin-bottom: clamp(12px, 2vh, 24px);
  max-width: 560px;
  font-size: clamp(16px, 2.2vh, 22px);
}

/* History (heatmap) */
.history-stats {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.history-stats .stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  flex: 1;
  min-width: 110px;
}
.history-stats .stat-value {
  font-size: 22px;
  font-weight: 700;
  line-height: 1.1;
}
.history-stats .stat-label {
  font-size: 12px;
  color: var(--text-dim);
}
.history-scroll {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 12px;
  -webkit-overflow-scrolling: touch;
}
.history-grid {
  display: grid;
  grid-template-rows: 14px repeat(7, 12px);
  grid-auto-flow: column;
  grid-auto-columns: 12px;
  gap: 3px;
  width: max-content;
}
.history-grid .month-label {
  grid-row: 1;
  font-size: 10px;
  color: var(--text-dim);
  white-space: nowrap;
  align-self: center;
}
.history-grid .cell {
  width: 12px;
  height: 12px;
  border-radius: 2px;
  background: var(--surface-2);
  cursor: pointer;
  border: 1px solid transparent;
}
.history-grid .cell.future {
  background: transparent;
  cursor: default;
  pointer-events: none;
}
.history-grid .cell.lvl1 { background: rgba(63, 185, 80, 0.45); }
.history-grid .cell.lvl2 { background: rgb(46, 160, 67); }
.history-grid .cell.today { outline: 1px solid var(--text-dim); }
.history-grid .cell.selected {
  border-color: var(--text);
}
.history-legend {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 10px 0 0;
  font-size: 12px;
  color: var(--text-dim);
  justify-content: flex-end;
  flex-wrap: wrap;
}
.history-legend i {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
}
.history-legend i + span { margin-right: 8px; }
.history-legend i.lvl0 { background: var(--surface-2); }
.history-legend i.lvl1 { background: rgba(63, 185, 80, 0.45); }
.history-legend i.lvl2 { background: rgb(46, 160, 67); }
.history-detail {
  margin-top: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}
.history-detail.empty {
  color: var(--text-dim);
  text-align: center;
}
.history-detail h4 {
  margin: 0 0 8px;
  font-size: 15px;
}
.history-detail ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.history-detail li {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
}
.history-detail li .time {
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

/* Ranking */
.ranking-title {
  margin: 4px 0 12px;
  font-size: 18px;
}
.ranking-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.ranking-item {
  display: grid;
  grid-template-columns: 36px 1fr auto;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
}
.ranking-item.ranking-self {
  border-color: var(--primary);
  background: rgba(79, 140, 255, 0.08);
}
.ranking-rank {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-dim);
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.ranking-item:nth-child(1) .ranking-rank { color: #ffd54f; }
.ranking-item:nth-child(2) .ranking-rank { color: #c0c4cc; }
.ranking-item:nth-child(3) .ranking-rank { color: #d2966b; }
.ranking-name {
  font-weight: 700;
  font-size: 16px;
}
.ranking-meta {
  color: var(--text-dim);
  font-size: 13px;
}
.ranking-count {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* Login */
.login-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 20px calc(24px + env(safe-area-inset-bottom));
  width: 100%;
  max-width: 360px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}
.login-card h1 {
  margin: 0 0 4px;
  text-align: center;
  font-size: 24px;
}
.login-sub {
  text-align: center;
  color: var(--text-dim);
  margin: 0 0 14px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  z-index: 200;
  box-shadow: var(--shadow);
  max-width: 90vw;
}
.toast.error { border-color: var(--danger); }

/* Pull-to-refresh */
html, body { overscroll-behavior-y: contain; }
#pull-refresh {
  position: fixed;
  top: env(safe-area-inset-top);
  left: 0;
  right: 0;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  color: var(--text-dim);
  font-size: 14px;
  font-weight: 600;
  z-index: 300;
  transform: translateY(calc(-100% - env(safe-area-inset-top)));
  pointer-events: none;
  border-bottom: 1px solid var(--border);
}
#pull-refresh.ready { color: var(--primary); }
#pull-refresh.snap { transition: transform 0.25s ease-out; }
