:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f7f8fc;
  --border: #e3e7f0;
  --text: #1c2333;
  --muted: #6b7488;
  --accent: #2f6fed;
  --accent-soft: #eaf1ff;
  --success: #17a34a;
  --success-soft: #e9f9ef;
  --danger: #dc3545;
  --danger-soft: #fdecec;
  --warn: #d97706;
  --warn-soft: #fff3e0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

.auth-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}
.auth-shell[hidden], #app-shell[hidden], #app-header[hidden], #tabs[hidden] { display: none; }

/* Site-wide legal notice -- sits below whichever shell (auth or app) is visible. */
.legal-footer {
  max-width: 900px;
  margin: 8px auto 0;
  padding: 18px 24px 28px;
  border-top: 1px solid var(--border);
  text-align: center;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.6;
}
.legal-footer p { margin: 0; }
.legal-footer-mark {
  margin-top: 6px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--text);
}
.auth-card {
  width: min(100%, 420px);
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 16px 40px rgba(28, 35, 51, 0.08);
}
.auth-card img { display: block; width: 190px; max-width: 100%; margin-bottom: 20px; }
.auth-card h1 { margin: 0 0 8px; font-size: 24px; }
.auth-card p { color: var(--muted); margin: 0 0 20px; }
.auth-card label { display: grid; gap: 6px; margin-top: 14px; font-size: 13px; font-weight: 600; }
.auth-card input { width: 100%; }
.auth-card form button { margin-top: 18px; width: 100%; }
.auth-success button { margin-top: 18px; width: 100%; }
.auth-form-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.social-login { display: grid; gap: 8px; margin-top: 10px; }
.social-login button { width: 100%; }
.auth-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 12px; }
.social-login[hidden], .auth-actions[hidden] { display: none; }
.icon-button { display: inline-flex; align-items: center; justify-content: center; min-width: 38px; min-height: 36px; padding: 8px; }
.icon-button[hidden] { display: none; }
.icon-button .icon { width: 16px; height: 16px; }
#admin-users-table select { min-width: 92px; }
#admin-users-table td:last-child { white-space: nowrap; }
#admin-users-table td:last-child button { margin-left: 4px; }

.icon {
  width: 14px;
  height: 14px;
  fill: currentColor;
  display: inline-block;
  vertical-align: -2px;
  flex-shrink: 0;
}

.review-flag {
  position: relative;
  display: inline-block;
  margin-left: 6px;
}
.review-flag .icon { width: 12px; height: 12px; color: var(--warn); vertical-align: -1px; }
.review-flag::after {
  content: attr(data-tooltip);
  position: absolute;
  left: 50%;
  bottom: calc(100% + 6px);
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  font-size: 12px;
  line-height: 1.4;
  padding: 6px 10px;
  border-radius: 6px;
  white-space: normal;
  width: max-content;
  max-width: 260px;
  text-align: left;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.1s ease;
  z-index: 20;
}
.review-flag:hover::after {
  opacity: 1;
  visibility: visible;
}

/* --------------------------------- Header --------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
  gap: 10px;
}

.brand-heading {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0;
  color: var(--text);
  font-size: 18px;
  font-weight: 700;
  white-space: nowrap;
}
.brand-heading img {
  display: block;
  width: 190px;
  height: auto;
}
.profile-menu {
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
}
.profile-menu-icon { color: var(--accent); display: inline-flex; }
.profile-menu-icon .icon { width: 18px; height: 18px; }
.profile-menu select { min-width: 150px; }

.account-menu-trigger {
  display: flex;
  align-items: center;
  gap: 6px;
}
.account-menu-trigger .icon { width: 11px; height: 11px; transition: transform 0.15s ease; }
.account-menu-trigger[aria-expanded="true"] .icon { transform: rotate(180deg); }

.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 200px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(20, 30, 60, 0.12);
  padding: 6px;
  z-index: 20;
}
.dropdown-menu[hidden] { display: none; }
.dropdown-menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  border: none;
  background: transparent;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}
.dropdown-menu-item:hover { background: var(--surface-2); color: var(--accent); }
.dropdown-menu-item.danger:hover { color: var(--danger); }
.dropdown-menu-item .icon { width: 14px; height: 14px; }

/* Compact ok/bad indicator next to a card's <h2> -- icon only, full status text
   lives in the title attribute (native tooltip) rather than as visible label. */
.status-dot {
  display: inline-flex;
  align-items: center;
  margin-left: 6px;
  color: var(--muted);
}
.status-dot-ok { color: var(--success); }
.status-dot-bad { color: var(--danger); }

@keyframes spin { to { transform: rotate(360deg); } }

@keyframes garmin-spin {
  to {
    transform: rotate(360deg);
  }
}

.tabs {
  display: flex;
  gap: 8px;
  padding: 10px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.tab {
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
}
.tab .icon { width: 14px; height: 14px; }
.tab:hover { color: var(--text); border-color: var(--muted); }
.tab.active { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.tab[hidden] { display: none; }

.tab-panel { display: none; }
.tab-panel.active { display: flex; flex-direction: column; gap: 16px; }

/* --------------------------------- Layout ---------------------------------- */

.layout { padding: 20px 24px; max-width: 1200px; margin: 0 auto; }

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
  align-items: start;
}

/* Settings tab: routing table stays on its own column so a long "platform (not
   connected)" option can't force the whole row layout to grow past the card. */
.settings-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}
.settings-column {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
@media (max-width: 720px) {
  .settings-grid { grid-template-columns: 1fr; }
}

/* Admin tab: the user management table needs most of the width; the auth
   policy card is just a handful of narrow fields, so give it a fixed minimum
   instead of splitting the row 50/50 like .settings-grid does. */
.admin-grid {
  display: grid;
  grid-template-columns: minmax(0, 2.4fr) minmax(260px, 1fr);
  gap: 16px;
  align-items: start;
}
@media (max-width: 900px) {
  .admin-grid { grid-template-columns: 1fr; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  box-shadow: 0 1px 2px rgba(20, 30, 60, 0.04);
}
.card h2 {
  font-size: 14px;
  margin: 0 0 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
}
.card h2 .icon { color: var(--accent); width: 15px; height: 15px; }
/* Higher specificity than the accent color above, so the ok/bad status dot
   keeps its own color instead of inheriting the heading icon's accent blue. */
.card h2 .status-dot-ok .icon { color: var(--success); }
.card h2 .status-dot-bad .icon { color: var(--danger); }
.hint { font-size: 12.5px; color: var(--muted); margin: 0 0 12px; line-height: 1.5; }

/* -------------------------------- Uploads ---------------------------------- */

.file-drop {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  border: 1.5px dashed var(--border);
  border-radius: 10px;
  padding: 26px 10px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  margin-bottom: 8px;
  background: var(--surface-2);
}
.file-drop .icon { width: 22px; height: 22px; color: var(--accent); }
.file-drop:hover { border-color: var(--accent); color: var(--text); }
.file-drop.dragover { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }

/* --------------------------------- Fields ----------------------------------- */

.field-row { display: flex; gap: 8px; margin-bottom: 8px; align-items: center; }
.field-row[hidden] { display: none; }
.field-row.inline { margin-bottom: 0; }
.field-row label { font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; }
.field-row input[type=text],
.field-row input[type=password],
.field-row input[type=email],
.field-row input[type=number] {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  color: var(--text);
  font-size: 13px;
}
.field-row select { flex: 1; min-width: 0; }
.field-row input:focus, select:focus, input[type=date]:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

select, input[type=date], input[type=number] {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 9px;
  color: var(--text);
  font-size: 13px;
}

.checkbox-label { display: flex; align-items: center; gap: 6px; }

button {
  font: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
button:hover { border-color: var(--accent); color: var(--accent); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { color: #fff; filter: brightness(1.08); }
button.primary:disabled { background: var(--surface-2); border-color: var(--border); color: var(--muted); }
button.ghost { background: transparent; }
button.active { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
button.danger { color: var(--danger); }
button.danger:hover { border-color: var(--danger); color: var(--danger); background: var(--danger-soft); }

.current-file { font-size: 11.5px; color: var(--muted); word-break: break-all; margin: 0; }
.field-msg { font-size: 12.5px; margin: 6px 0 0; color: var(--muted); }
.field-msg.ok { color: var(--success); }
.field-msg.err { color: var(--danger); }
.field-msg.busy {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.field-msg.busy::before {
  content: "";
  width: 12px;
  height: 12px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: garmin-spin 0.7s linear infinite;
}

.file-list { list-style: none; margin: 4px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.file-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  padding: 7px 10px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.file-list .file-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.file-list .file-active { color: var(--accent); font-weight: 700; }
.file-list .file-badge {
  font-size: 10px;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 2px 7px;
  border-radius: 999px;
  margin-left: 6px;
  flex-shrink: 0;
}
.file-list button {
  padding: 4px 9px;
  font-size: 11px;
  flex-shrink: 0;
}
.file-list .empty { color: var(--muted); font-style: italic; background: none; border: none; padding: 4px 0; }

/* Threshold table form -- laid out and coloured to mirror the coach's PDF:
   two read-only anaerobic rows, a division band, then the editable aerobic zones. */
.threshold-grid { display: flex; flex-direction: column; gap: 16px; }
#zones-save .icon, #zones-clear .icon, #zones-cancel .icon { margin-right: 6px; }
.threshold-card .table-wrap { overflow-x: auto; }
.threshold-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.threshold-table th, .threshold-table td { border: 1px solid var(--border); padding: 4px 6px; vertical-align: middle; }
.threshold-table thead th { color: var(--muted); text-align: center; white-space: nowrap; font-size: 11px; }
.threshold-table input {
  width: 100%;
  min-width: 58px;
  box-sizing: border-box;
  padding: 5px 6px;
  text-align: center;
}
.threshold-table .zone-band {
  width: 84px;
  color: #fff;
  font-weight: 700;
  font-size: 11px;
  text-align: center;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35);
}
.threshold-table .zone-terminology { min-width: 190px; }
.threshold-table .zone-name { display: block; font-weight: 700; color: var(--text); }
.threshold-table .zone-duration { display: block; color: var(--muted); font-size: 11px; }
.threshold-table .zone-effort { white-space: nowrap; text-align: center; color: var(--muted); }
.threshold-table .zone-readonly, .threshold-table .zone-na { color: var(--muted); font-style: italic; text-align: center; }
.threshold-table .zone-open { color: var(--muted); font-style: italic; text-align: center; white-space: nowrap; }
.threshold-table input.zone-derived { background: var(--bg); color: var(--muted); cursor: default; }
.threshold-table .zone-divider td {
  background: #ff0000;
  color: #fff;
  font-weight: 700;
  font-size: 11px;
  text-align: center;
  letter-spacing: 0.02em;
}

.zone-max { background: #c00000; }
.zone-sprint { background: #ff0000; }
.zone-vo2max { background: #ffff00; color: #1c1c1c !important; text-shadow: none !important; }
.zone-lt2 { background: #ffc000; color: #1c1c1c !important; text-shadow: none !important; }
.zone-int-end { background: #ffe599; color: #1c1c1c !important; text-shadow: none !important; }
.zone-pure-end { background: #92d050; color: #1c1c1c !important; text-shadow: none !important; }
.zone-easy { background: #00b050; }

/* -------------------------------- Dashboard --------------------------------- */

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}
.toolbar select#program-select { min-width: 260px; }
.filters { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.filters label { font-size: 12px; color: var(--muted); display: flex; gap: 6px; align-items: center; font-weight: 500; }

.summary-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.summary { font-size: 13px; color: var(--muted); }
.summary b { color: var(--text); }

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

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow-x: auto;
  overflow-y: hidden;
}
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left;
  padding: 10px 14px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
th.col-check, td.col-check { padding-left: 16px; padding-right: 6px; width: 20px; }
td.col-date { white-space: nowrap; }
td.col-date input[type="date"] { font-size: 12.5px; padding: 2px 4px; }
.icon-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  padding: 2px 4px;
  font-size: 13px;
  line-height: 1;
  color: var(--muted);
}
.icon-btn:hover { color: var(--text); }
tbody td { padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: top; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.row-selected { background: var(--accent-soft); }
tbody tr[data-row-key] { cursor: pointer; }
tbody tr.row-weekend { background: var(--surface-2); }
tbody tr.row-weekend.row-selected { background: var(--accent-soft); }
td.col-day { color: var(--muted); font-size: 12px; }
.raw-text {
  margin: 0;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
  font-size: 12.5px;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
}
.raw-text-highlight {
  padding: 10px 12px;
  margin-bottom: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
  font-size: 12.5px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
}
.warn-count { color: var(--warn); }
.empty-table-hint { padding: 20px; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-overlay[hidden] { display: none; }
.modal {
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  width: min(560px, 92vw);
  max-height: 82vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}
.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 15px; }
.modal-close {
  border: none;
  background: transparent;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
  padding: 0;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 16px 18px; overflow-y: auto; }

#routing-table { table-layout: fixed; }
#workout-rows td select { width: auto; font-size: 12.5px; padding: 4px 6px; }

.chip-field {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}
/* Reuses .badge's color/icon/font formatting (see the dashboard platform column)
   -- .chip only adds the extra right-hand padding for the remove button. */
.chip { padding-right: 4px; }
.chip-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  border: none;
  border-radius: 50%;
  padding: 0;
  font-size: 13px;
  line-height: 1;
  color: inherit;
  background: rgba(0, 0, 0, 0.12);
  cursor: pointer;
}
.chip-remove:hover { background: rgba(0, 0, 0, 0.25); }
.chip-add {
  font-size: 12.5px;
  padding: 4px 6px;
  border: 1px dashed var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
}

.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.badge-garmin { color: #0a6b3d; background: #d8f5e4; }
.badge-trainerday { color: #1746a2; background: #dce9ff; }
.badge-none { color: var(--muted); background: var(--surface-2); }
.badge-unavailable { color: var(--muted); background: var(--surface-2); text-decoration: line-through; }

.platform-badges { display: flex; flex-wrap: wrap; gap: 5px; }

.badge-new { color: #0a6b3d; background: var(--success-soft); }
.badge-changed { color: #92600a; background: var(--warn-soft); }
.badge-unchanged { color: var(--muted); background: var(--surface-2); }

/* Fixed to the viewport (not the scrolling page) so it stays visible the moment
   an import/delete starts, no matter how far down the workout table the user
   has scrolled or which tab is showing underneath. Collapsed by default to
   just the round "fab" icon; .expanded (set on hover or while a stream is
   actually running -- see app.js) reveals the full panel above it. */
.audit-log-widget {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}
/* Our own `display: flex` above would otherwise beat the browser's default
   `[hidden] { display: none }` (author styles always outrank the UA
   stylesheet) -- state explicitly so the [hidden] attribute still works. */
.audit-log-widget[hidden] { display: none; }

.audit-log-fab {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  border: none;
  box-shadow: 0 8px 20px rgba(20, 30, 60, 0.28);
  cursor: pointer;
  flex-shrink: 0;
}
.audit-log-fab .icon { width: 20px; height: 20px; color: #fff; }
.audit-log-fab.is-running .icon { animation: spin 1s linear infinite; }
.audit-log-fab.is-ok { background: var(--success); }
.audit-log-fab.is-err { background: var(--danger); }

.audit-log-panel {
  width: min(440px, calc(100vw - 48px));
  max-height: min(50vh, 420px);
  display: none;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(20, 30, 60, 0.22);
  overflow: hidden;
}
.audit-log-widget.expanded .audit-log-panel { display: flex; }

.audit-log-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 10px 10px 18px;
  flex-shrink: 0;
}
.audit-log-header h2 { margin: 0; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.audit-log-close {
  border: none;
  background: none;
  font-size: 20px;
  line-height: 1;
  padding: 4px 8px;
  color: var(--muted);
}
.audit-log-close:hover { color: var(--danger); border-color: transparent; }
.audit-log-body {
  overflow-y: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  line-height: 1.7;
  background: #12172a;
  color: #d6dceb;
  border-radius: 8px;
  margin: 0 18px 18px;
  padding: 12px 14px;
  white-space: pre-wrap;
}
.audit-log-body .fail { color: #ff8a8a; }
.audit-log-body .ok { color: #7ee8a8; }

@media (max-width: 600px) {
  .audit-log-panel { width: calc(100vw - 48px); }
}

