/* --- 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: #ffffff;
  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%;
  gap: 8px;
}

/* --- 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;
  touch-action: pan-y;  /* let the swipe handler own horizontal touch gestures */
}

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

/* --- PAGE BAR (Garmin-style paging: dots + swipe, edit controls when
   Edit Mode is on) --- */
.page-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  min-height: 26px;
}

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

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

.page-dot.active {
  background: #17a2b8;
  transform: scale(1.25);
}

.page-edit-controls {
  display: none;   /* toggled to flex by toggleEditMode() */
  align-items: center;
  gap: 8px;
}

.page-edit-btn {
  padding: 5px 12px;
  font-size: 0.85em;
  font-weight: bold;
  border: 1px solid #007bff;
  color: #007bff;
  background: transparent;
  border-radius: 12px;
  cursor: pointer;
}

.page-edit-btn.danger {
  border-color: #dc3545;
  color: #dc3545;
}

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

.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) --- */
.metric-title {
  font-size: clamp(1.1rem, 16cqh, 1.8rem);
  font-weight: bold;
  color: var(--text-muted);
  position: absolute;
  top: 10px;
}

.metric-val {
  /* 16 cells are smaller than 12 were, so size the number to the cell. */
  font-size: clamp(1.6rem, min(34cqh, 17cqw), 3.5rem);
  font-weight: bold;
  line-height: 1;
}

.metric-sub {
  font-size: 1.1em;
  color: var(--text-main);
  position: absolute;
  bottom: 10px;
}

/* --- 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.75rem, 15cqh, 1.1rem);
  margin-bottom: 2cqh;
}

.metric-box.half .metric-val {
  font-size: clamp(1.5rem, 40cqh, 2.6rem);
}

.metric-box.half .metric-sub {
  font-size: clamp(0.75rem, 12cqh, 1rem);
  margin-top: 2cqh;
}

.metric-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);
}

/* 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;
}

/* Auto-Lap row: label + unit input side by side, matching the rest of the menu */
.auto-lap-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  font-size: 1.1em;
  gap: 10px;
}

.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;
}

/* --- LAP LOG (fills the empty space below the controls) --- */
.lap-log-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.lap-log-wrap h3 {
  flex-shrink: 0;
}

.lap-log {
  flex: 1;
  min-height: 80px;
  overflow-y: auto;
}

.lap-row {
  display: grid;
  grid-template-columns: 0.7fr 1fr 1fr 1.2fr;
  gap: 4px;
  padding: 6px 4px;
  border-bottom: 1px solid var(--border-color);
  font-size: 0.95em;
}

.lap-log.has-power .lap-row {
  grid-template-columns: 0.6fr 1fr 0.8fr 0.8fr 1.2fr;
}

.lap-row span {
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lap-row span:first-child {
  font-weight: bold;
  text-align: left;
}

.lap-row-head {
  font-weight: bold;
  color: var(--text-muted);
  font-size: 0.8em;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border-bottom: 1px solid var(--border-color);
}

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

/* Not a .lap-row: the empty-state message has its own single-column
   layout, and giving it "lap-row" too would let the higher-specificity
   ".lap-log.has-power .lap-row" grid override it back to 5 columns. */
.lap-row-empty {
  padding: 6px 4px;
  color: var(--text-muted);
  font-style: italic;
}

/* --- LAP POPUP (momentary, center-screen) --- */
.lap-popup {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.92);
  background: var(--bg-card);
  border: 2px solid #17a2b8;
  border-radius: 14px;
  padding: 22px 34px;
  z-index: 30;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.lap-popup.visible {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.lap-popup-title {
  font-size: 1.4em;
  font-weight: bold;
  color: var(--text-main);
}

.lap-popup-stats {
  display: flex;
  gap: 24px;
}

.lap-popup-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.lap-popup-val {
  font-size: 1.8em;
  font-weight: bold;
  color: var(--text-main);
}

.lap-popup-label {
  font-size: 0.8em;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

/* --- 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);
}

/* Outer accordion list: category sections stacked full-width. Each
   section's own field list still uses .modal-grid below for its 2-col
   button layout. */
.modal-categories {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

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

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

.modal-cat-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-muted);
  font-weight: normal;
  font-size: 0.85em;
}

.modal-cat-count {
  background: var(--border-color);
  color: var(--text-main);
  border-radius: 10px;
  padding: 1px 8px;
  font-size: 0.9em;
}

.modal-cat-chevron {
  display: inline-block;
  transition: transform 0.2s ease;
}

.modal-category.open .modal-cat-chevron {
  transform: rotate(-180deg);
}

/* Closed by default: the field grid is display:none until .open is toggled. */
.modal-category .modal-grid {
  display: none;
  padding: 12px;
  padding-top: 4px;
}

.modal-category.open .modal-grid {
  display: grid;
}

.modal-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;
}

/* --- 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);
}