* { box-sizing: border-box; }

:root {
  --bg: #fafafa;
  --fg: #1a1a1a;
  --muted: #666;
  --accent: #2f6feb;
  --accent-fg: #fff;
  --border: #ddd;
  --danger: #b4231e;
  --saved: #1f8a3a;
  font-size: 17px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --fg: #eee;
    --muted: #888;
    --accent: #4a8cff;
    --border: #2a2a2a;
    --danger: #e07070;
    --saved: #60d07a;
  }
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  min-height: 100vh;
}

body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

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

header h1 {
  font-size: 1.2rem;
  margin: 0;
}

main, section {
  padding: 16px;
  max-width: 640px;
  margin: 0 auto;
}

h2 { margin-top: 0; font-size: 1.05rem; color: var(--muted); font-weight: 600; }

/* Login */

#login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 360px;
  margin: 10vh auto;
}

#login-form label { font-size: 0.9rem; color: var(--muted); }

#login-err { color: var(--danger); min-height: 1.2em; margin: 0; }

/* Inputs */

input, button {
  font-size: 1rem;
  font-family: inherit;
}

input[type='text'], input[type='number'], input[type='password'] {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  width: 100%;
  min-height: 44px;
}

button {
  padding: 12px 16px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
  min-height: 44px;
  cursor: pointer;
}

button:disabled { opacity: 0.5; }

#logout {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 6px 12px;
  min-height: 32px;
  font-size: 0.85rem;
}

/* Template list */

#template-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.template-btn {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
  text-align: left;
  padding: 16px;
}

/* Session form */

.session-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 12px;
}

.session-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.session-row label {
  flex: 0 0 64px;
  color: var(--muted);
  font-size: 0.9rem;
}

.session-field {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.prev-hint {
  font-size: 0.75rem;
  color: var(--muted);
  padding-left: 4px;
}

.prev-header {
  margin: 0 0 12px 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.checkbox-row { padding: 12px 0; }
.checkbox-field {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 1.15rem;
  color: var(--fg);
  cursor: pointer;
}
.checkbox-field input[type="checkbox"] {
  width: 28px;
  height: 28px;
}
.checkbox-description {
  margin: 8px 0 4px 0;
  white-space: pre-wrap;
}
.detail-description {
  margin: 0 0 4px 0;
  white-space: pre-wrap;
}
#nt-description { width: 100%; min-height: 60px; resize: vertical; }
.detail-checkbox {
  font-size: 1.1rem;
  margin: 0 0 8px 0;
}
.nt-kind { display: flex; flex-direction: column; gap: 4px; }
.nt-kind label { font-weight: normal; }

.session-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.status {
  font-size: 0.85rem;
  color: var(--muted);
}
.status[data-state='saved'] { color: var(--saved); }
.status[data-state='saving'] { color: var(--accent); }
.status[data-state='dirty'] { color: var(--muted); }

.banner {
  background: var(--border);
  padding: 8px 12px;
  border-radius: 6px;
  margin-bottom: 12px;
  font-size: 0.9rem;
  color: var(--muted);
}

.done {
  color: var(--saved);
  font-weight: 600;
}

.muted {
  color: var(--muted);
  font-size: 0.9rem;
}

button.secondary {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
  font-weight: 500;
}

.back-btn {
  margin-bottom: 12px;
  padding: 6px 14px;
  min-height: 36px;
  font-size: 0.9rem;
}

.home-admin-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.admin-btn {
  flex: 1;
  min-height: 36px;
  padding: 8px 12px;
  font-size: 0.9rem;
  font-weight: 500;
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.home-disclosure {
  margin-top: 20px;
}

.home-disclosure-summary {
  list-style: none;
  cursor: pointer;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0;
  padding-right: 4px;
  user-select: none;
}

.home-disclosure-summary::-webkit-details-marker { display: none; }

.home-disclosure-summary::after {
  content: '';
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform 160ms ease;
  margin-right: 6px;
}

.home-disclosure[open] > .home-disclosure-summary::after {
  transform: rotate(45deg);
  margin-bottom: 4px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  background: transparent;
  color: var(--fg);
  border: 0;
  border-radius: 8px;
  width: 40px;
  height: 40px;
  min-height: 40px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* New-template form */

#new-tpl-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field > span {
  font-size: 0.85rem;
  color: var(--muted);
}

.err {
  color: var(--danger);
  min-height: 1.2em;
  margin: 0;
  font-size: 0.9rem;
}

.form-actions button { width: 100%; }

/* Manage templates */

#manage-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.manage-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.manage-row.is-archived {
  opacity: 0.6;
}

.manage-name {
  font-weight: 600;
}

.manage-meta {
  color: var(--muted);
  font-size: 0.85rem;
}

.manage-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

button.small {
  padding: 6px 12px;
  min-height: 32px;
  font-size: 0.85rem;
  flex: 1 1 auto;
}

.session-form.no-row-labels .session-row label { display: none; }

/* Column builder (rows-mode new template) */

.col-builder-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 4px;
}

.col-builder-header span {
  font-size: 0.85rem;
  color: var(--muted);
}

#nt-col-builder {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.col-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.col-row .col-name {
  flex: 1 1 160px;
  min-width: 0;
}

.col-row select {
  flex: 0 0 auto;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  font-size: 0.95rem;
  min-height: 44px;
}

.col-row .col-unit {
  flex: 0 0 88px;
}

.col-row .col-del {
  flex: 0 0 44px;
  padding: 0;
  min-width: 44px;
  font-size: 1.1rem;
}

/* Home section headings and routine cards */

.home-heading {
  margin: 18px 0 8px 0;
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}

#routine-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.routine-btn {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}

.routine-name { font-weight: 600; }
.routine-count { color: var(--muted); font-size: 0.85rem; }

/* Routine builder (Selected + Available) */

.rt-section { margin-top: 16px; }

.rt-heading {
  margin: 0 0 8px 0;
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}

#nr-selected, #nr-available {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rt-row.selected {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.rt-pos { color: var(--muted); font-size: 0.85rem; min-width: 20px; }
.rt-name { font-weight: 500; }

.rt-controls {
  display: inline-flex;
  gap: 4px;
}

.rt-controls button {
  flex: 0 0 36px;
  min-width: 36px;
  padding: 0;
  min-height: 36px;
  font-size: 1rem;
}

/* Runner */

.runner-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.runner-header .back-btn { margin-bottom: 0; }

.runner-progress {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
}

#runner-routine-name {
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}

#runner-step {
  font-size: 0.95rem;
  font-weight: 500;
}

.runner-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.runner-actions button:not(.small) {
  min-width: 96px;
}

/* History list */

#history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-row {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
  text-align: left;
  padding: 12px 14px;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    'name meta'
    'summary summary';
  gap: 4px 12px;
  min-height: unset;
  font-weight: normal;
}

.history-primary { grid-area: name; font-weight: 600; }
.history-meta { grid-area: meta; color: var(--muted); font-size: 0.85rem; }
.history-summary { grid-area: summary; color: var(--muted); font-size: 0.9rem; }

.history-row.is-workout {
  border-left: 3px solid var(--accent);
}

.history-row.is-workout .history-primary::before {
  content: '◈ ';
  color: var(--accent);
}

.workout-exercise-name {
  margin: 24px 0 4px 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--fg);
}

.workout-exercise-name:first-of-type {
  margin-top: 16px;
}

/* Read-only detail table */

.detail-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
}

.detail-table th, .detail-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-weight: normal;
}

.detail-table thead th {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}

.detail-table tbody th {
  color: var(--muted);
  font-size: 0.9rem;
  width: 64px;
}

/* Swipe-to-reveal row action (history list, workout detail children) */

.row-wrap {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  touch-action: pan-y;
}

.row-foreground {
  position: relative;
  z-index: 1;
  transition: transform 180ms ease-out;
  will-change: transform;
}

.history-row.row-foreground {
  background: var(--bg);
  width: 100%;
  display: grid;
}

.workout-child.row-foreground {
  background: var(--bg);
  padding-right: 4px;
}

.row-action {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 88px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: var(--danger);
  color: #fff;
  font-size: 1.6rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  min-height: 44px;
  font-weight: normal;
}

.row-action:focus-visible {
  outline: 2px solid var(--fg);
  outline-offset: -3px;
}
