:root {
  --bg: #f6f7f5;
  --panel: #ffffff;
  --border: #e2e5e0;
  --text: #1d2420;
  --muted: #667167;
  --accent: #3a8a4a;
  --accent-contrast: #ffffff;
  --danger: #b3261e;
  --radius: 8px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* ---- Shell ---- */

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* Hidden on desktop, where the sidebar is always visible -- see the media query at the bottom. */
.menu-toggle {
  display: none;
  width: 36px;
  height: 36px;
  margin-right: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.sidebar-backdrop {
  display: none;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-ball {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}

.brand h1 {
  font-size: 16px;
  margin: 0;
}

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

.nav button {
  text-align: left;
  padding: 8px 10px;
  border: none;
  background: none;
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text);
  font-size: 14px;
}

.nav button:hover { background: var(--bg); }
.nav button.active { background: var(--accent); color: var(--accent-contrast); }

.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.topbar h2 { margin: 0; font-size: 18px; }

.page {
  padding: 24px;
  flex: 1;
}

.switch-user {
  margin-top: auto;
  padding: 6px 8px;
  font-size: 11px;
  color: var(--muted);
  background: none;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  cursor: pointer;
}

.switch-user:hover {
  color: var(--text);
  border-color: var(--muted);
}

/* ---- Shared controls ---- */

/* The one prominent action button: save, add, unlock. */
.btn-primary {
  padding: 10px 18px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: default;
}

/* A text field or two next to a primary button (onboarding, admin add-rows, admin lock). */
.input-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 12px 0 24px;
}

.input-row input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 14px;
  min-width: 0;
}

.error-text {
  color: var(--danger);
}

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

/* ---- Tables ---- */

table {
  border-collapse: collapse;
  width: 100%;
  background: var(--panel);
}

th, td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  font-size: 14px;
  text-align: left;
}

th { background: var(--bg); font-weight: 600; }

table input {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: inherit;
  font-size: 13px;
}

table button {
  padding: 5px 10px;
  margin-right: 4px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  cursor: pointer;
  font-size: 12px;
}

.table-scroll {
  overflow-x: auto;
  margin-bottom: 16px;
}

/* The name column stays put while the exercise columns scroll sideways. */
.col-sticky {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--panel);
  box-shadow: 2px 0 4px rgba(0, 0, 0, 0.08);
}

thead .col-sticky {
  z-index: 2;
  background: var(--bg);
}

.grid-input {
  width: 64px;
}

.clickable-row {
  cursor: pointer;
}

.clickable-row:hover {
  background: var(--bg);
}

/* ---- Onboarding ---- */

.onboarding {
  max-width: 560px;
}

.onboarding-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin: 20px 0 28px;
}

.onboarding-card {
  padding: 16px 22px;
  min-width: 96px;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}

.onboarding-card:hover {
  border-color: var(--accent);
  background: #eef6ef;
  transform: translateY(-1px);
}

.onboarding-add {
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.onboarding-add input {
  flex: 1;
  max-width: 260px;
}

/* ---- Date selector ---- */

.date-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 10px;
  margin-bottom: 20px;
}

.date-row input[type="date"] {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 14px;
}

.date-row button {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  cursor: pointer;
  font-size: 13px;
}

.day-name {
  font-size: 13px;
  color: var(--muted);
  text-transform: capitalize;
}

/* ---- Daily entry form ---- */

.entry-form {
  max-width: 640px;
}

.entry-who {
  font-weight: 600;
  color: var(--muted);
}

.entry-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}

.entry-points {
  font-weight: 600;
}

/* One row per exercise: label on the left, the reset / < / input / > group on the right. */
.entry-rows {
  display: flex;
  flex-direction: column;
  margin-bottom: 20px;
  border-top: 1px solid var(--border);
}

.entry-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 12px;
  padding: 12px 2px;
  border-bottom: 1px solid var(--border);
}

.field-label {
  font-size: 14px;
  color: var(--text);
  flex: 1 1 120px;
  min-width: 0;
}

.entry-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

.entry-input {
  width: 56px;
  height: 40px;
  padding: 0 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 16px;
  font-family: inherit;
  text-align: center;
}

.reset-btn,
.step-btn {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: var(--radius);
  cursor: pointer;
  font-family: inherit;
}

.step-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  font-size: 20px;
  font-weight: 600;
  color: var(--text);
}

.step-btn:active {
  background: var(--bg);
}

.reset-btn {
  border: 1px dashed var(--border);
  background: none;
  font-size: 15px;
  color: var(--muted);
}

.reset-btn:active {
  color: var(--text);
}

/* ---- Badges (rating tier / streak) ---- */

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  color: white;
}

.badge-lemra { background: #9aa39b; }
.badge-borec { background: #3a8a4a; }
.badge-superborec { background: #2f6fb3; }
.badge-arciborec { background: #7a4fc4; }
.badge-other { background: #c9862f; }

.badge-streak {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  background: #fff1d6;
  color: #8a5a00;
}

/* ---- History stats ---- */

.stats-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin: 16px 0 24px;
}

.stat-card {
  padding: 14px 20px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  min-width: 120px;
}

.stat-value {
  font-size: 20px;
  font-weight: 700;
}

.stat-label {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

/* ---- Tab rows (leaderboard periods, admin sections) ---- */

.tab-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 20px;
}

.tab-row button {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel);
  cursor: pointer;
  font-size: 13px;
}

.tab-row button.active {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

/* Sits beside a section heading rather than above a whole screen. */
.tab-row-inline {
  margin-bottom: 0;
  gap: 6px;
}

.tab-row-inline button {
  padding: 5px 12px;
  font-size: 12px;
}

/* ---- Leaderboard sections (all three boards stacked) ---- */

.board + .board {
  margin-top: 32px;
}

.board-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 14px;
}

.board-head h3 {
  margin: 0;
  font-size: 16px;
}

.board-range {
  margin: 4px 0 10px;
  font-size: 12px;
  color: var(--muted);
}

/* ---- Statistics chart ---- */

.chart {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 16px 0 24px;
}

.chart-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.chart-label {
  flex: 0 0 140px;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chart-bar-track {
  flex: 1;
  height: 22px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
}

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

/* Tier-coloured bars reuse the badge-* classes for colour; that background rule needs to win over
   the plain accent green above regardless of stylesheet order, hence the extra specificity. */
.chart-bar-fill.badge-lemra { background: #9aa39b; }
.chart-bar-fill.badge-borec { background: #3a8a4a; }
.chart-bar-fill.badge-superborec { background: #2f6fb3; }
.chart-bar-fill.badge-arciborec { background: #7a4fc4; }
.chart-bar-fill.badge-other { background: #c9862f; }

.chart-value {
  flex: 0 0 110px;
  font-size: 12px;
  color: var(--muted);
  text-align: right;
  white-space: nowrap;
}

/* Trend-over-time: a scrollable row of vertical bars (one per day or month). */
.trend-scroll {
  overflow-x: auto;
  margin: 16px 0 24px;
}

.trend-chart {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  width: min-content;
}

.trend-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex-shrink: 0;
}

.trend-bar-track {
  width: 18px;
  height: 120px;
  display: flex;
  align-items: flex-end;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}

.trend-bar-fill {
  width: 100%;
  min-height: 2px;
  background: var(--accent);
}

.trend-label {
  margin-top: 4px;
  font-size: 10px;
  color: var(--muted);
  white-space: nowrap;
}

/* Legend for the colour-coded exercise composition chart. */
.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin-bottom: 12px;
  font-size: 12px;
  color: var(--muted);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
}

.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* One participant's exercise mix as a single bar split proportionally into coloured segments. */
.stacked-bar-track {
  flex: 1;
  height: 22px;
  display: flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.stacked-bar-segment {
  height: 100%;
}

.admin-lock {
  max-width: 420px;
}

.admin-lock .input-row input {
  flex: 1;
  font-size: 16px; /* iOS zooms in on anything smaller when focused. */
}

/* ---- Phone: the app is used from one, so the menu folds behind the hamburger ---- */
@media (max-width: 760px) {
  .menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.15);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-backdrop.open {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 40;
  }

  .page {
    padding: 16px;
  }

  .topbar {
    padding: 12px 16px;
  }

  /* The controls take the full width below the label, keeping them within thumb reach. */
  .entry-row {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }

  /* `flex: 1 1 120px` sizes the label's width in the row layout, but flex-basis follows the main
     axis -- which flips to vertical here -- so it would become a 120px min-height and open a huge
     gap above the buttons. Reset it to the label's natural height. */
  .field-label {
    flex: 0 0 auto;
  }

  .entry-controls {
    justify-content: flex-end;
  }

  .entry-input {
    flex: 1 1 auto;
    width: auto;
    min-width: 0;
  }

  .chart-label {
    flex-basis: 84px;
  }

  .chart-value {
    flex-basis: 84px;
  }
}
