/* Rowing Calculator — same look as the Watt Calculator (cycling). */

:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --text: #000000;
  --muted: #8e8e93;
  --accent: #007aff;
  --border: #e5e5ea;
  --focus: #0a84ff;

  /* Rowing zones (same colors as the Peloton level table) */
  --r-z1: #B8BCC6; /* Recovery */
  --r-z2: #22D3EE; /* Easy */
  --r-z3: #6EE07A; /* Moderate */
  --r-z4: #FBBF24; /* Challenging */
  --r-z5: #F87171; /* Max */

  /* Heart rate zones */
  --hr-z1: #8e8e93;
  --hr-z2: #34c759;
  --hr-z3: #007aff;
  --hr-z4: #ff9500;
  --hr-z5: #ff3b30;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1e;
    --card: #2c2c2e;
    --text: #ffffff;
    --muted: #8e8e93;
    --accent: #0a84ff;
    --border: #3a3a3c;
    --focus: #64a8ff;
    --r-z1: #8E939E;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

/* Never let the page scroll sideways (tables scroll inside their own boxes). */
html, body { max-width: 100%; }
/* overflow: clip doesn't create a scroll box, so the sticky tab bar keeps
   working; older browsers fall back to hiding overflow on the page only. */
html { overflow-x: hidden; }
@supports (overflow: clip) { html, body { overflow-x: clip; } }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg);
  margin: 0;
  padding:
    calc(15px + env(safe-area-inset-top))
    calc(15px + env(safe-area-inset-right))
    calc(40px + env(safe-area-inset-bottom))
    calc(15px + env(safe-area-inset-left));
  color: var(--text);
}

.container { max-width: 680px; margin: 0 auto; min-width: 0; }

h1 { font-size: 22px; text-align: center; margin-bottom: 15px; }

[hidden] { display: none !important; }

/* ---------- Tabs ---------- */

.tabs, .sub-tabs {
  display: flex;
  margin-bottom: 15px;
  background: var(--border);
  padding: 2px;
  border-radius: 8px;
  gap: 2px;
}

.sub-tabs { margin-bottom: 12px; border-radius: 6px; }

/* Main tab bar stays at the top while the page scrolls. It sits just below
   the iPhone status bar / notch, and a strip of page colour covers the gap
   above it so content doesn't show through. */
.tabs {
  position: sticky;
  top: env(safe-area-inset-top);
  z-index: 50;
  box-shadow: 0 0 0 6px var(--bg), 0 6px 10px -6px rgba(0, 0, 0, 0.25);
}
.tabs::before {
  content: "";
  position: absolute;
  left: -16px;
  right: -16px;
  bottom: 100%;
  height: calc(env(safe-area-inset-top) + 8px);
  background: var(--bg);
}

.tab, .sub-tab {
  flex: 1;
  text-align: center;
  font-weight: 600;
  cursor: pointer;
  border: none;
  background: transparent;
  text-transform: uppercase;
  -webkit-tap-highlight-color: transparent;
  font-family: inherit;
}

.tab {
  min-width: 0;
  padding: 8px 2px;
  font-size: clamp(9.5px, 3.1vw, 12px);
  letter-spacing: -0.1px;
  min-height: 44px;
  border-radius: 6px;
  color: var(--text);
  overflow-wrap: anywhere;
  hyphens: none;
}
.sub-tab { min-width: 0; padding: 6px 4px; font-size: clamp(10px, 3.1vw, 12px); min-height: 40px; border-radius: 4px; color: var(--muted); }

.tab.active, .sub-tab.active {
  background: var(--card);
  box-shadow: 0 1px 3px rgba(0,0,0,0.15);
  color: var(--text);
}

.tab:focus-visible, .sub-tab:focus-visible, .system-toggle-btn:focus-visible,
.secondary-btn:focus-visible, input:focus-visible, select:focus-visible,
.levels-table tbody tr:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* ---------- Cards and rows ---------- */

.card {
  background: var(--card);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 14px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.card-pad-8 { padding: 8px; }

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  padding: 10px 0;
  gap: 10px;
}

.card > .row:last-child { border-bottom: none; }

.row-stack { align-items: flex-start; flex-direction: column; gap: 4px; }

.row-split { display: flex; justify-content: space-between; width: 100%; align-items: center; gap: 8px; }

label {
  font-size: 15px;
  font-weight: 500;
  width: 55%;
  overflow-wrap: break-word;
  line-height: 1.3;
}

.row-split label { width: auto; flex: 1; }

.muted-label { color: var(--muted); width: 55%; font-size: 15px; font-weight: 500; }

.value-display { text-align: right; font-weight: bold; font-size: 16px; width: 40%; color: var(--muted); }

.subnote-right { font-size: 11px; color: var(--muted); text-align: right; width: 100%; line-height: 1.2; }

/* ---------- Inputs ---------- */

input {
  border: none;
  text-align: right;
  font-size: 16px;
  width: 40%;
  min-height: 44px;
  outline: none;
  background: transparent;
  font-weight: bold;
  color: var(--text);
  font-family: inherit;
}

.input-boxed { border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px; }

.input-boxed:focus, .input-small:focus {
  border-color: var(--focus);
  box-shadow: 0 0 0 2px rgba(10, 132, 255, 0.18);
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type="number"] { -moz-appearance: textfield; appearance: textfield; }

.input-small {
  width: 64px;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 4px;
}

.inline-fields { display: flex; justify-content: flex-end; align-items: center; gap: 3px; flex-wrap: nowrap; }
.inline-fields span { font-size: 12px; line-height: 1; color: var(--muted); }

.select-wrapper {
  position: relative;
  width: 40%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  min-height: 44px;
}

.select-display {
  font-size: 16px;
  font-weight: bold;
  color: var(--accent);
  padding-right: 18px;
  text-align: right;
  pointer-events: none;
}

.select-wrapper::after {
  content: " ▲▼";
  font-size: 8px;
  color: var(--accent);
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
  letter-spacing: -1px;
}

select {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  font-size: 16px;
}

.error-text { color: #ff3b30; font-size: 12px; margin-top: 8px; }
.error-text:empty { display: none; }

/* ---------- Results ---------- */

.result-box { text-align: center; padding: 15px 14px; }

.result-val { font-size: 30px; font-weight: bold; margin-bottom: 4px; font-variant-numeric: tabular-nums; }

.result-lbl { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }

.result-sub { font-size: 14px; margin-top: 6px; }
.result-sub:empty { display: none; }

.system-toggle-btn, .secondary-btn {
  background: var(--border);
  color: var(--text);
  border: none;
  border-radius: 6px;
  padding: 8px 12px;
  min-height: 44px;
  font-size: 12px;
  font-weight: bold;
  text-transform: uppercase;
  cursor: pointer;
  font-family: inherit;
  -webkit-tap-highlight-color: transparent;
}

.system-toggle-btn { margin-top: 10px; margin-bottom: 4px; }

.system-toggle-btn:active, .secondary-btn:active { background: var(--accent); color: #fff; }
.secondary-btn:disabled { opacity: 0.5; }

.badge-container {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 14px;
  border-top: 1px solid var(--border);
  padding-top: 14px;
}

.calc-badge {
  text-align: center;
  font-size: 13px;
  padding: 10px;
  border-radius: 8px;
  color: #111;
  font-weight: bold;
  background: var(--border);
  transition: background 0.2s ease;
}

.calc-badge.badge-empty { color: var(--muted); }

.calc-badge .badge-subline { display: block; font-size: 11px; margin-top: 2px; font-weight: 600; opacity: 0.85; }

.tss-note { font-size: 12px; color: var(--muted); line-height: 1.4; margin: 0 4px 14px; }
.tss-note summary { cursor: pointer; font-weight: 600; color: var(--accent); padding: 4px 0; }
.tss-note p { margin: 6px 0 0; }

.help-text { font-size: 12px; color: var(--muted); line-height: 1.4; margin-top: 8px; padding: 0 4px; }

.controls-row { display: flex; justify-content: flex-end; gap: 8px; margin: 0 0 10px; flex-wrap: wrap; }

.export-title { display: none; font-size: 18px; font-weight: 800; text-align: center; margin: 4px 0 10px; }

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

.table-scroll-wrapper { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

.power-matrix-scroll { overscroll-behavior-x: contain; scrollbar-width: thin; }

table { border-collapse: separate; border-spacing: 0; font-variant-numeric: tabular-nums; }

/* Power zones table */
.row-zones { width: 100%; font-size: 14px; }

.row-zones th {
  text-align: left;
  padding: 8px 6px;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  font-weight: 600;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}

.row-zones td { padding: 10px 6px; border-bottom: 1px solid var(--border); font-weight: 700; white-space: nowrap; }

.row-zones tr:last-child td { border-bottom: none; }

.zone-chip {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 6px;
  color: #111;
  font-weight: 800;
  font-size: 13px;
}

.zone-chip small { display: block; font-size: 10px; font-weight: 600; opacity: 0.75; }

/* Which Peloton level is in use, and why */
.level-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.35;
}
.card-pad-8 > .level-status { padding: 2px 4px 6px; }
.level-note { text-align: left; margin-top: 2px; }
.level-buttons { display: flex; gap: 6px; flex-shrink: 0; }
.level-status .secondary-btn { min-height: 36px; padding: 6px 10px; }

/* Heart rate zones (same colors as the Watt Calculator) */
.hr-group-title {
  font-size: 12px;
  font-weight: bold;
  color: var(--muted);
  margin: 14px 4px 4px;
  text-transform: uppercase;
}

.controls-row + .export-title + .hr-group-title,
.controls-row + .export-title + .error-text + .hr-group-title { margin-top: 4px; }

.hr-zones td:nth-child(2), .hr-zones th:nth-child(2) { width: 40%; }

.hr-range-edit { display: inline-flex; align-items: center; gap: 3px; white-space: nowrap; }
.hr-range-edit span { font-size: 12px; color: var(--muted); font-weight: 600; }
.hr-range-edit input { width: 46px; min-height: 40px; padding: 4px 2px; }
#hr-custom-export .row-zones td { padding-left: 4px; padding-right: 4px; }
#hr-custom-export .zone-chip { font-size: 12px; padding: 4px 6px; }

/* Peloton levels table */
.levels-table { width: max-content; min-width: 100%; font-size: 13px; }

.levels-table th, .levels-table td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  text-align: center;
  padding: 6px 4px;
  min-width: 58px;
  white-space: nowrap;
}

.levels-table thead .zone-head th { font-size: 14px; font-weight: 800; font-style: italic; color: #111; padding: 8px 4px; }

.levels-table thead .sub-head th { font-size: 11px; font-weight: 700; color: var(--text); text-transform: uppercase; }
.levels-table thead .sub-head th.mid { color: #111; }

.levels-table .lvl {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--card);
  text-align: left;
  font-weight: 800;
  min-width: 70px;
  padding-left: 8px;
}

.levels-table thead .lvl { z-index: 3; }

.levels-table tbody tr { cursor: pointer; }

.levels-table .cell-pace { display: block; font-size: 12px; color: var(--muted); font-weight: 600; }
.levels-table .cell-watts { display: block; font-size: 15px; font-weight: 800; }

.levels-table .mid .cell-watts { font-size: 17px; }
.levels-table .mid .cell-pace { color: inherit; opacity: 0.8; }
.levels-table .mid { font-style: italic; color: #111; }

.levels-table tbody tr.selected td { box-shadow: inset 0 2px 0 var(--accent), inset 0 -2px 0 var(--accent); }
.levels-table tbody tr.selected .lvl { color: var(--accent); }
.levels-table .lvl small { display: block; font-size: 11px; font-weight: 600; color: var(--muted); }
.levels-table tbody tr.my-ftp { cursor: default; }
.levels-table tbody tr.my-ftp td { border-top: 2px dashed var(--accent); border-bottom: 2px dashed var(--accent); }
.levels-table tbody tr.my-ftp.selected td { box-shadow: none; }

/* ---------- Update notice (offline/updates) ---------- */

.update-notice {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px 10px 16px;
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

.update-notice button {
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  cursor: pointer;
}

.update-notice button:disabled { opacity: 0.6; }

/* ---------- Export (image) mode: light colors, no controls ---------- */

.exporting { background: #fff !important; color: #000 !important; box-shadow: none !important; width: max-content; min-width: 560px; padding: 16px !important; }
.exporting .no-export, .exporting .export-hide { display: none !important; }
.exporting, .exporting * { font-variant-numeric: normal !important; }
.exporting .export-title { display: block; color: #000; }
.exporting .table-scroll-wrapper { overflow: visible !important; }
.exporting .levels-table, .exporting .row-zones { width: 100%; }
.exporting .levels-table .lvl { background: #fff; color: #000; position: static; }
.exporting .levels-table tbody tr.selected .lvl { color: #000; }
.exporting th, .exporting td { border-color: #cfcfcf !important; color: #000; }
.exporting .cell-pace { color: #444 !important; }
.exporting .help-text { color: #444; width: 0; min-width: 100%; }
.exporting tr.selected td { box-shadow: none !important; }

/* ---------- Print: only the table being printed ---------- */

@media print {
  :root { --bg: #fff; --card: #fff; --text: #000; --muted: #444; --border: #cfcfcf; --r-z1: #B8BCC6; }
  @page { margin: 0.4in; size: landscape; }
  html, body { background: #fff !important; padding: 0 !important; }
  * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
  body > .container > *:not(.print-target):not(.print-target-parent) { display: none !important; }
  .print-target-parent > *:not(.print-target) { display: none !important; }
  .print-target { display: block !important; box-shadow: none !important; }
  .no-export, .export-hide, .update-notice { display: none !important; }
  .export-title { display: block; }
  .table-scroll-wrapper { overflow: visible !important; }
  .levels-table, .row-zones { width: 100% !important; }
  .levels-table .lvl { position: static; }
  .levels-table tbody tr.selected .lvl { color: #000; }
  tr.selected td { box-shadow: none !important; }
  .container { max-width: none; }
}

.exporting .export-value { font-weight: 800; }
.exporting .hr-range-edit span { color: #000; font-weight: 800; font-size: 14px; }

/* Small phones (and iPhone Display Zoom): tighten the zone tables to fit. */
@media (max-width: 480px) {
  .row-zones { font-size: 12.5px; }
  .row-zones th, .row-zones td { padding-left: 3px; padding-right: 3px; }
  .zone-chip { font-size: 11px; padding: 3px 5px; }
  .zone-chip small { font-size: 9.5px; }
  .row-zones th { font-size: 10px; }
}

@media (max-width: 359px) {
  #hr-custom-export .zone-chip small { display: none; }
  .hr-range-edit { gap: 2px; }
  .hr-range-edit input { width: 40px; }
}

/* ---------------------------------------------------------------------------
   Landscape phones: use the full width (like the Watt Calculator) so the
   Peloton Levels table fits without swiping, and tighten vertical spacing.
   --------------------------------------------------------------------------- */
@media (orientation: landscape) and (max-height: 500px) {
  body {
    padding:
      calc(10px + env(safe-area-inset-top))
      calc(12px + env(safe-area-inset-right))
      calc(24px + env(safe-area-inset-bottom))
      calc(12px + env(safe-area-inset-left));
  }
  .container { max-width: 1000px; }
  h1 { font-size: 20px; margin: 4px 0 10px; }
  .card { padding: 12px; margin-bottom: 12px; }

  /* Peloton Levels: all 12 columns on screen at once */
  .levels-table { width: 100%; table-layout: fixed; }
  .levels-table th, .levels-table td { min-width: 0; padding: 4px 2px; }
  .levels-table .lvl { width: 72px; padding-left: 6px; }
  .levels-table .cell-watts { font-size: 14px; }
  .levels-table .mid .cell-watts { font-size: 15px; }
  .levels-table .cell-pace { font-size: 11px; }
  .levels-table thead .zone-head th { font-size: 13px; }
  .levels-table thead .sub-head th { font-size: 10px; }

  .swipe-hint { display: none; }

  /* Heart rate: Max HR and LTHR tables side by side */
  .hr-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
}

/* Large screens (tablets, computers) */
@media (min-width: 900px) {
  .container { max-width: 1000px; }
  .levels-table { width: 100%; table-layout: fixed; }
  .levels-table th, .levels-table td { min-width: 0; }
  .levels-table .lvl { width: 90px; }
  .swipe-hint { display: none; }
  .hr-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 0 20px; }
}
.hr-pair .hr-group-title { margin-top: 10px; }
.hr-pair > div > .table-scroll-wrapper { margin-bottom: 4px; }

.sub-tabs-label { margin: 0 4px 6px; }

/* Sub-tab rows (e.g. the Peloton Levels rounding options) stay pinned just
   below the main tab bar. --tabs-h is set from the bar's real height in app.js. */
.view > .sub-tabs {
  position: sticky;
  top: calc(env(safe-area-inset-top) + var(--tabs-h, 48px) + 8px);
  z-index: 40;
  box-shadow: 0 0 0 8px var(--bg);
}
/* The rounding row and its label share one pinned block; its background
   runs from the tab bar down to just below the buttons. */
.sticky-sub {
  position: sticky;
  top: calc(env(safe-area-inset-top) + var(--tabs-h, 48px));
  z-index: 40;
  background: var(--bg);
  margin: -15px -4px 8px;
  padding: 12px 4px 8px;
}
.sticky-sub .sub-tabs { margin-bottom: 0; }
.sticky-sub .sub-tabs-label { margin-top: 0; }
@media print { .view > .sub-tabs, .sticky-sub { position: static; } }
.row-zones th { white-space: normal; line-height: 1.2; vertical-align: bottom; }
