/* --- BASE & VARIABLES --- */
:root {
  --bg-main: #121212;
  --bg-card: #1e1e1e;
  --text-main: #ffffff;
  --text-muted: #aaaaaa;
  --border-color: #333333;
}

body.light-mode {
  --bg-main: #f4f6f9;
  --bg-card: #ffffff;
  --text-main: #212529;
  --text-muted: #6c757d;
  --border-color: #dee2e6;
}

body {
  margin: 0;
  padding: 0;
  background-color: var(--bg-main);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
  user-select: none;
}

/* --- HEADER --- */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  height: 60px;
  background-color: var(--bg-card);
  border-bottom: 1px solid var(--border-color);
  box-sizing: border-box;
}

.nav-btn {
  font-size: 24px;
  cursor: pointer;
  background: #007bff;
  color: white;
  border: none;
  padding: 5px 15px;
  border-radius: 5px;
}

.clock-display {
  font-size: 1.5em;
  font-weight: bold;
}

/* --- HEADER WORKOUT & MESSAGE DISPLAY --- */
.header-workout-title {
  flex: 1;
  text-align: center;
  font-size: 1.25em;
  font-weight: bold;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 15px;
  transition: color 0.2s ease, transform 0.2s ease;
}

.header-workout-title.has-message {
  color: #ffc107;
  font-size: 1.3em;
}

/* --- MAIN LAYOUT WRAPPERS --- */
.main-container {
  display: flex;
  width: 100vw;
  height: calc(100vh - 60px);
  box-sizing: border-box;
  overflow: hidden;
  padding: 10px;
  gap: 15px;
}

.main-container.controls-left {
  flex-direction: row;
}

.main-container:not(.controls-left) {
  flex-direction: row-reverse;
}

/* SIDE CONTROLS COLUMN */
.controls-col {
  width: 380px;
  min-width: 380px;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  box-sizing: border-box;
}

/* METRICS COLUMN */
.metrics-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
}

/* --- 4x4 GRID --- */
.grid-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 10px;
  width: 100%;
  flex: 1;
  min-height: 0;
  box-sizing: border-box;
  /* Let the browser handle vertical panning natively; horizontal movement is
     ours (page-swipe), so we don't want the browser fighting the drag. */
  touch-action: pan-y;
}

/* --- PAGE BAR (multi-page grid, Garmin-style) --- */
.page-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  flex: 0 0 auto;
  height: 30px;
  margin-top: 6px;
}

.page-dots {
  display: flex;
  align-items: center;
  gap: 9px;
}

.page-dot {
  width: 9px;
  height: 9px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--border-color);
  cursor: pointer;
  transition: transform 0.15s ease, background-color 0.15s ease;
}

.page-dot.active {
  background: #007bff;
  transform: scale(1.35);
}

/* Add/Delete Page controls: hidden unless Edit Mode is on. */
.page-edit-btn {
  display: none;
  width: 26px;
  height: 26px;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: #007bff;
  color: #fff;
  font-size: 1.1em;
  font-weight: bold;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}

.page-edit-btn:active {
  opacity: 0.8;
}

.page-edit-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.page-bar.edit-mode .page-edit-btn {
  display: inline-flex;
}

.grid-cell {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.metric-box {
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  position: relative;
  cursor: pointer;
  overflow: hidden;
  border: 2px solid transparent;
  transition: transform 0.1s;
  container-type: size;
}

.metric-box.pressing {
  transform: scale(0.95);
  border-color: #007bff;
}

.metric-box.full,
.metric-box.half {
  flex: 1;
}

/* --- TYPOGRAPHY (Full Card) --- */
/* Text scales with the TILE (container units), not the screen: the number
   fills the box, and title / subtitle are fractions of it. The only caps are
   generous ones. JS (fitValueText) shrinks a number that is too wide. */
.metric-title {
  font-size: clamp(0.85rem, min(13cqh, 8cqw), 3.2rem);
  font-weight: bold;
  color: var(--text-muted);
  position: absolute;
  top: 3cqh;
  max-width: 94%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.metric-val {
  font-size: clamp(2rem, min(46cqh, 26cqw), 12rem);
  font-weight: bold;
  line-height: 1;
  max-width: 100%;
  white-space: nowrap;
}

.metric-sub {
  font-size: clamp(0.8rem, min(10cqh, 6cqw), 2.2rem);
  color: var(--text-main);
  position: absolute;
  bottom: 4cqh;
  max-width: 94%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* --- HALF CARD DYNAMIC SCALING --- */
.metric-box.half .metric-title,
.metric-box.half .metric-sub {
  position: relative;
  top: auto;
  bottom: auto;
}

.metric-box.half .metric-title {
  font-size: clamp(0.7rem, min(19cqh, 8cqw), 2rem);
  line-height: 1.1;
  margin-bottom: 1cqh;
}

.metric-box.half .metric-val {
  font-size: clamp(1.4rem, min(52cqh, 24cqw), 7rem);
}

/* The subtitle only appears on a half tile that is tall enough to hold it. */
.metric-box.half .metric-sub {
  display: none;
  font-size: clamp(0.7rem, min(15cqh, 6cqw), 1.4rem);
  line-height: 1.1;
  margin-top: 1cqh;
}

@container (min-height: 110px) {
  .metric-box.half .metric-sub:not(:empty) { display: block; }
}

.power-slider-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* --- ZONE COLORS --- */
/* Zone 1 remains the default dark/light card background */
.zone-1 {
  background-color: var(--bg-card);
  color: var(--text-main);
}

/* In Dark Mode: dynamic zones now use full-brightness colors with dark text */
.zone-2 {
  background-color: #4da3ff;
  color: #001f3f;
}

/* Z2 Blue */
.zone-3 {
  background-color: #28a745;
  color: #002b0c;
}

/* Z3 Green */
.zone-4 {
  background-color: #ffc107;
  color: #332200;
}

/* Z4 Yellow (Sweet Spot / Threshold) */
.zone-5 {
  background-color: #dc3545;
  color: #ffffff;
}

/* Z5 Red (VO2 Max) */
.zone-6 {
  background-color: #a855f7;
  color: #ffffff;
}

/* Z6 Purple (Anaerobic) */
.zone-7 {
  background-color: #e83e8c;
  color: #ffffff;
}

/* Z7 Magenta (Neuromuscular) */

/* Light Mode: maintains standard contrast */
body.light-mode .zone-1 {
  background-color: var(--bg-card);
  color: var(--text-main);
}

body.light-mode .zone-2 {
  background-color: #cce5ff;
  color: #001f3f;
}

body.light-mode .zone-3 {
  background-color: #d4edda;
  color: #003311;
}

body.light-mode .zone-4 {
  background-color: #fff3cd;
  color: #332200;
}

body.light-mode .zone-5 {
  background-color: #f8d7da;
  color: #330000;
}

body.light-mode .zone-6 {
  background-color: #e2d9f3;
  color: #1a0033;
}

body.light-mode .zone-7 {
  background-color: #f8d7eb;
  color: #330022;
}

/* Keep title and subtitle readable when dynamic colored backgrounds are active */
.metric-box:not(.zone-1) .metric-title {
  color: inherit;
  opacity: 0.8;
}

.metric-box:not(.zone-1) .metric-sub {
  color: inherit;
  opacity: 0.9;
}

/* --- CONTROLS (Timer, Cards, Buttons) --- */
.timer-controls {
  display: flex;
  gap: 10px;
}

.timer-btn {
  flex: 1;
  padding: 15px 4px;
  font-size: 1.1em;
  font-weight: bold;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  color: white;
}

.primary-btn {
  background-color: #28a745;
}

.secondary-btn {
  background-color: #17a2b8;
}

.export-btn {
  background-color: #6c757d;
}

.reset-btn {
  background-color: #dc3545;
}

.card {
  background-color: var(--bg-card);
  padding: 15px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
}

.card h3 {
  margin-top: 0;
  margin-bottom: 15px;
  font-size: 1.1em;
  color: var(--text-main);
}

/* --- TRAINER CONTROL ACTIVE HIGHLIGHT --- */
.card.control-active {
  border-color: #17a2b8;
  box-shadow: 0 0 0 2px rgba(23, 162, 184, 0.4);
  background-color: rgba(23, 162, 184, 0.08);
}

body.light-mode .card.control-active {
  border-color: #007bff;
  box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
  background-color: rgba(0, 123, 255, 0.06);
}

.controls-row {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.controls-row:last-child {
  margin-bottom: 0;
}

/* Remove default browser arrows on number inputs to save space */
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type=number] {
  -moz-appearance: textfield;
  appearance: textfield;
}

.controls-row input {
  flex: 1;
  min-width: 65px;
  padding: 0 4px;
  text-align: center;
  font-size: 1.2em;
  font-weight: bold;
  border-radius: 4px;
  border: 1px solid var(--border-color);
  background: var(--bg-main);
  color: var(--text-main);
  box-sizing: border-box;
}

/* Unified Adjustment Buttons */
.adj-btn,
.res-btn,
.erg-adj-btn,
.erg-btn {
  background: #6c757d;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 10px 5px;
  font-size: 1.1em;
  cursor: pointer;
  flex: 1;
}

.adj-btn:active,
.res-btn:active,
.erg-adj-btn:active,
.erg-btn:active {
  opacity: 0.8;
}

.status-text {
  font-size: 1.1em;
  color: var(--text-muted);
  margin-top: 5px;
}

/* --- WORKOUT HEADER & CLEAR BUTTON --- */
.workout-header-wrapper {
  text-align: center;
  margin-bottom: 15px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
}

.clear-workout-btn {
  background: #dc3545;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 4px 15px;
  font-size: 0.85em;
  cursor: pointer;
  font-weight: bold;
  display: none;
}

.clear-workout-btn:active {
  opacity: 0.8;
}

.workout-btn-row {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.skip-interval-btn {
  background: #17a2b8;
  color: white;
  border: none;
  border-radius: 12px;
  padding: 4px 15px;
  font-size: 0.85em;
  cursor: pointer;
  font-weight: bold;
  display: none;
}

.skip-interval-btn:active {
  opacity: 0.8;
}

/* --- SIDENAV --- */
.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
  background-color: var(--bg-card);
  overflow-x: hidden;
  overflow-y: auto;
  box-sizing: border-box;
  transition: 0.3s;
  padding-top: 60px;
  padding-bottom: 20px;
  color: var(--text-main);
}

.sidenav .closebtn {
  position: absolute;
  top: 10px;
  right: 20px;
  font-size: 36px;
  text-decoration: none;
  color: var(--text-main);
}

.menu-section {
  padding: 20px;
  border-bottom: 1px solid var(--border-color);
}

.menu-section h3 {
  margin-top: 0;
  color: #17a2b8;
  font-size: 1.2em;
}

.menu-section label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  font-size: 1.1em;
}

.menu-hint {
  margin: -5px 0 15px 0;
  font-size: 0.9em;
  color: var(--text-muted);
  line-height: 1.35;
}

.menu-section select {
  padding: 5px;
  background: var(--bg-main);
  color: var(--text-main);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 1.1em;
  min-width: 100px;
}

.menu-section input[type="number"] {
  width: 75px;
  padding: 5px;
  background: var(--bg-main);
  color: var(--text-main);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  text-align: center;
  font-size: 1.1em;
}

.toggle-label {
  cursor: pointer;
  display: flex;
  justify-content: flex-start !important;
  gap: 15px;
}

.menu-btn {
  width: 100%;
  padding: 12px;
  background-color: #28a745;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 1.2em;
  cursor: pointer;
}

/* --- MODAL --- */
.modal-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  z-index: 20;
}

.modal-content {
  display: none;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--bg-card);
  padding: 20px;
  border-radius: 10px;
  z-index: 21;
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  overflow-y: auto;
  border: 1px solid var(--border-color);
}

.modal-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.modal-btn {
  padding: 15px;
  font-size: 1em;
  font-weight: bold;
  background: var(--bg-main);
  color: var(--text-main);
  border: 1px solid var(--border-color);
  border-radius: 5px;
  cursor: pointer;
}

.modal-btn:hover {
  background: #007bff;
  color: white;
}

/* --- FIELD PICKER ACCORDION --- */
/* One collapsible section per category, closed by default. Sections keep
   whatever open/closed state the rider left them in between field picks. */
.modal-accordion {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.accordion-section {
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
}

.accordion-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 15px;
  background: var(--bg-main);
  color: var(--text-main);
  border: none;
  font-size: 1.05em;
  font-weight: bold;
  text-align: left;
  cursor: pointer;
}

.accordion-chevron {
  display: inline-block;
  color: var(--text-muted);
  transition: transform 0.2s ease;
}

.accordion-section.open .accordion-chevron {
  transform: rotate(180deg);
}

/* max-height drives the collapse animation; the section's own grid columns
   come from .modal-grid, unaffected by open/closed state. */
.accordion-body {
  max-height: 0;
  padding: 0 12px;
  overflow: hidden;
  transition: max-height 0.25s ease, padding 0.25s ease;
}

.accordion-section.open .accordion-body {
  max-height: 2000px;
  padding: 12px;
}

/* --- WORKOUT PULSE WARNING --- */
@keyframes pulseAlert {
  0% {
    box-shadow: inset 0 0 0px rgba(220, 53, 69, 0);
  }

  50% {
    box-shadow: inset 0 0 50px rgba(220, 53, 69, 0.8);
    background-color: rgba(220, 53, 69, 0.1);
  }

  100% {
    box-shadow: inset 0 0 0px rgba(220, 53, 69, 0);
  }
}

body.pulse-warning {
  animation: pulseAlert 1s infinite;
}

/* --- BLUETOOTH TOGGLE BUTTONS --- */
.bt-btn-off {
  background-color: #444 !important;
  border: 1px solid #555 !important;
  color: white !important;
}

.bt-btn-on {
  background-color: rgba(23, 162, 184, 0.15) !important;
  border: 1px solid #17a2b8 !important;
  color: #17a2b8 !important;
}

body.light-mode .bt-btn-on {
  background-color: rgba(0, 123, 255, 0.1) !important;
  border: 1px solid #007bff !important;
  color: #007bff !important;
}
.edit-toggle {
  padding: 6px 14px;
  border-radius: 6px;
  border: 2px solid #007bff;
  background: transparent;
  color: #007bff;
  cursor: pointer;
}
.edit-toggle.active { background: #007bff; color: #fff; }

/* Edit mode visuals */
.grid-container.edit-mode .metric-box {
  cursor: grab;
  border-style: dashed;
  border-color: rgba(0, 123, 255, 0.6);
  user-select: none;
  -webkit-user-select: none;
}
.grid-container.edit-mode .metric-box:active { cursor: grabbing; }

/* Children never become the event target, so the box itself is always the
   drag source / drop target (also stops sliders etc. from swallowing the drag). */
.grid-container.edit-mode .metric-box * { pointer-events: none; }

.metric-box.dragging { opacity: 0.35; }
.metric-box.drag-over {
  border-style: solid;
  border-color: #007bff;
  outline: 3px solid rgba(0, 123, 255, 0.35);
  transform: scale(0.97);
}
/* File pickers follow the theme (were hardcoded white, invisible in light mode) */
.sidenav input[type="file"] {
  color: var(--text-main);
}