:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1b1f24;
  --text-muted: #6b7280;
  --accent: #3a86ff;
  --accent-contrast: #ffffff;
  --danger: #e5484d;
  --border: #e3e5e8;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  --radius: 12px;
}

* {
  box-sizing: border-box;
}

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

#app {
  max-width: 480px;
  margin: 0 auto;
  padding: 16px 16px 24px;
  width: 100%;
}

h1 {
  font-size: 1.3rem;
  margin: 4px 0 16px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.field {
  display: block;
  margin-bottom: 14px;
}

.field span {
  display: block;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}

input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  background: var(--surface);
  color: var(--text);
}

textarea {
  resize: vertical;
}

button {
  font-size: 0.95rem;
  cursor: pointer;
  border-radius: 8px;
  border: none;
  font-family: inherit;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  padding: 12px 18px;
  font-weight: 600;
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 10px 14px;
  margin-bottom: 12px;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
  padding: 10px 14px;
}

.btn-link {
  background: none;
  color: var(--accent);
  padding: 6px 0;
  margin-bottom: 8px;
}

.btn-icon {
  background: var(--surface);
  border: 1px solid var(--border);
  width: 40px;
  height: 40px;
  border-radius: 8px;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.btn-icon:disabled {
  opacity: 0.35;
  cursor: default;
}

.form-footer {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

.form-footer .btn-primary {
  flex: 1;
}

.round-card {
  --tile-color: var(--accent);
  background: var(--surface);
  border-radius: var(--radius);
  border-left: 4px solid var(--tile-color);
  box-shadow: var(--shadow);
  padding: 12px;
  margin-bottom: 14px;
}

.round-card[data-tile-color="0"] { --tile-color: #3a86ff; }
.round-card[data-tile-color="1"] { --tile-color: #ff9f1c; }
.round-card[data-tile-color="2"] { --tile-color: #2ec4b6; }
.round-card[data-tile-color="3"] { --tile-color: #e5484d; }

.round-card.warmup {
  --tile-color: #f7931e;
  background: #fff8ef;
}

.round-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  margin-bottom: 10px;
}

.round-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.round-badge {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--tile-color);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 700;
  flex-shrink: 0;
}

.round-actions {
  display: flex;
  gap: 8px;
}

.round-card.readonly {
  border-left-width: 4px;
}

.exercise-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px dashed var(--border);
}

.exercise-row:last-of-type {
  border-bottom: none;
  padding-bottom: 0;
}

.exercise-row select.exercise-select {
  flex: 1 0 100%;
  margin-bottom: 2px;
}

.exercise-fields {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  flex: 1 0 100%;
}

.field-mini {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.field-mini input {
  width: 100%;
  padding: 8px 4px;
  text-align: center;
}

.field-mini-label {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
}

.exercise-row-actions {
  display: flex;
  gap: 8px;
  flex: 1 0 100%;
  justify-content: flex-end;
}

.btn-add-exercise {
  width: 100%;
  margin-bottom: 0;
}

#btn-add-exercise-admin {
  width: 100%;
  margin-bottom: 14px;
}

.admin-exercise-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  margin-bottom: 8px;
}

.admin-exercise-name {
  font-weight: 500;
  flex: 1;
  min-width: 0;
  overflow-wrap: break-word;
}

.admin-exercise-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  margin-bottom: 10px;
}

.history-split {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.history-list-panel {
  flex: 0 0 90px;
  max-height: calc(100vh - 140px);
  overflow-y: auto;
}

.history-month-label {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin: 14px 0 4px;
  padding: 0 4px;
}

.history-month-label:first-child {
  margin-top: 0;
}

.history-day {
  display: block;
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 0;
  margin-bottom: 6px;
  text-align: center;
  font-weight: 600;
  color: var(--text);
}

.history-day.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}

.history-detail-panel {
  flex: 1;
  min-width: 0;
}

.history-detail-panel h2 {
  font-size: 1.1rem;
  margin: 0 0 12px;
}

.exercise-table {
  display: flex;
  flex-direction: column;
}

.exercise-table-row {
  display: grid;
  grid-template-columns: 1fr repeat(4, 30px);
  gap: 4px;
  align-items: center;
  padding: 5px 0;
  border-bottom: 1px dashed var(--border);
}

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

.exercise-table-header {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
  margin-bottom: 2px;
}

.exercise-table .col-name {
  overflow-wrap: break-word;
}

.exercise-table .col-num {
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}

.detail-actions button {
  flex: 1 1 100px;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.stat-card {
  text-align: center;
  margin-bottom: 0;
  padding: 10px 4px;
}

.stat-value {
  font-size: 1.3rem;
  font-weight: 700;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.metric-selector {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.metric-btn {
  flex: 1 1 45%;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 8px;
  font-size: 0.85rem;
}

.metric-btn.active {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

.chart-wrap {
  position: relative;
  height: 260px;
}

.empty-state {
  color: var(--text-muted);
  text-align: center;
  margin-top: 40px;
}

.duplicate-picker {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  margin-bottom: 14px;
}

.duplicate-picker-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  padding: 10px 4px;
  font-size: 0.85rem;
}

.duplicate-picker-cancel {
  display: block;
  width: 100%;
  text-align: center;
  background: none;
  color: var(--text-muted);
  padding: 8px 4px 2px;
}

.hidden {
  display: none !important;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  gap: 10px;
  height: 52px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-logo {
  width: 26px;
  height: 26px;
}

.app-title {
  font-weight: 700;
  font-size: 1.05rem;
}

.app-shell {
  display: block;
}

.sidebar {
  position: fixed;
  top: 52px;
  left: 0;
  bottom: 0;
  width: 260px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  box-shadow: var(--shadow);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  z-index: 55;
  overflow-y: auto;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
}

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

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 52px 0 0 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 50;
}

.sidebar-backdrop.open {
  display: block;
}

.nav-btn {
  background: none;
  border: none;
  border-radius: 8px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.95rem;
  color: var(--text);
  text-align: left;
  width: 100%;
}

.nav-btn.active {
  background: var(--bg);
  color: var(--accent);
  font-weight: 600;
}

.nav-icon {
  font-size: 1.2rem;
}

@media (min-width: 768px) {
  #btn-menu-toggle,
  .sidebar-backdrop {
    display: none !important;
  }

  .app-shell {
    display: flex;
  }

  .sidebar {
    position: static;
    top: auto;
    width: 220px;
    height: auto;
    transform: none;
    box-shadow: none;
    flex-shrink: 0;
  }

  #app {
    margin: 0;
  }
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-content {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px;
  width: 100%;
  max-width: 360px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.modal-actions button {
  flex: 1;
}

@media (max-width: 360px) {
  #app {
    padding-left: 12px;
    padding-right: 12px;
  }

  .exercise-fields {
    grid-template-columns: repeat(2, 1fr);
  }

  .metric-btn {
    font-size: 0.78rem;
    padding: 7px 4px;
  }
}
