*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #ffffff;
  --bg2: #f1f5f9;
  --text: #1e293b;
  --text2: #64748b;
  --border: #e2e8f0;
  --pt: #3b82f6;
  --pt-bg: #dbeafe;
  --self: #22c55e;
  --self-bg: #dcfce7;
  --rest: #6b7280;
  --rest-bg: #f3f4f6;
  --danger: #ef4444;
  --accent: #3b82f6;
  --card-shadow: 0 2px 8px rgba(0,0,0,0.06);
  --modal-overlay: rgba(0,0,0,0.4);
  --radius: 12px;
  --radius-sm: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --bg2: #1e293b;
    --text: #f1f5f9;
    --text2: #94a3b8;
    --border: #334155;
    --pt: #60a5fa;
    --pt-bg: #1e3a5f;
    --self: #4ade80;
    --self-bg: #14532d;
    --rest: #9ca3af;
    --rest-bg: #374151;
    --card-shadow: 0 2px 8px rgba(0,0,0,0.2);
    --modal-overlay: rgba(0,0,0,0.6);
  }
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

.hero-sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  top: -100%;
  left: 8px;
  background: var(--accent);
  color: white;
  padding: 8px 16px;
  border-radius: 0 0 8px 8px;
  z-index: 200;
  font-size: 0.9rem;
  font-weight: 600;
  text-decoration: none;
}

.skip-link:focus {
  top: 0;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  padding: 0;
}

header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;
}

header h2 {
  flex: 1;
  text-align: center;
  font-size: 1.1rem;
  font-weight: 600;
}

.icon-btn {
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  transition: background 0.15s;
}

.icon-btn:active {
  background: var(--bg2);
}

.add-btn {
  background: var(--accent);
  color: white;
  font-size: 1.5rem;
  font-weight: 600;
}

.add-btn:active {
  opacity: 0.8;
}

.day-names {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text2);
  padding: 8px 4px 4px;
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 2px;
  padding: 0 4px;
  flex: 1;
}

.day-cell {
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  position: relative;
  transition: background 0.15s;
  gap: 2px;
  min-height: 48px;
}

.day-cell:active {
  transform: scale(0.93);
}

.day-cell.other-month {
  opacity: 0.25;
}

.day-cell.today {
  font-weight: 700;
  box-shadow: inset 0 0 0 2px var(--accent);
}

.day-cell .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  display: block;
}

.day-cell .dot.pt { background: var(--pt); }
.day-cell .dot.self { background: var(--self); }
.day-cell .dot.rest { background: var(--rest); }

.day-cell.has-workout {
  background: var(--bg2);
}

#calendar {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#statsBar {
  display: flex;
  justify-content: space-around;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg);
  font-size: 0.8rem;
  color: var(--text2);
  font-weight: 500;
  gap: 8px;
}

#statsBar span {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: var(--modal-overlay);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  animation: fadeIn 0.15s ease;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--bg);
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  border-radius: var(--radius) var(--radius) 0 0;
  max-height: 85dvh;
  overflow-y: auto;
  animation: slideUp 0.2s ease;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
}

.modal-header {
  display: flex;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
}

.modal-header h3 {
  flex: 1;
  text-align: center;
  font-size: 1rem;
}

.close-btn, .edit-btn, .delete-btn {
  font-size: 1.1rem;
  color: var(--text2);
}

.edit-btn { color: var(--accent); }
.delete-btn { color: var(--danger); }

.day-detail {
  padding: 16px;
}

.day-detail .type-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 12px;
}

.type-badge.pt { background: var(--pt-bg); color: var(--pt); }
.type-badge.self { background: var(--self-bg); color: var(--self); }
.type-badge.rest { background: var(--rest-bg); color: var(--rest); }

.exercise-card {
  background: var(--bg2);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin: 8px 0;
}

.exercise-card h4 {
  font-size: 0.9rem;
  margin-bottom: 6px;
}

.exercise-card .set {
  display: flex;
  gap: 12px;
  font-size: 0.85rem;
  color: var(--text2);
  padding: 2px 0;
}

.day-detail .notes-section {
  margin-top: 12px;
  padding: 8px 12px;
  background: var(--bg2);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  color: var(--text2);
}

.edit-form {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-bottom: 32px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.field select, .field textarea {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  width: 100%;
}

.field textarea {
  resize: vertical;
  min-height: 48px;
}

.field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.hidden { display: none !important; }

.exercise-entry {
  background: var(--bg2);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 8px;
}

.exercise-entry .exercise-header {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}

.exercise-entry .exercise-header input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  background: var(--bg);
  color: var(--text);
}

.exercise-entry .exercise-header input:focus {
  outline: none;
  border-color: var(--accent);
}

.exercise-entry .remove-exercise {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 4px;
}

.set-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 4px;
}

.set-row input {
  width: 70px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.8rem;
  background: var(--bg);
  color: var(--text);
  text-align: center;
}

.set-row input:focus {
  outline: none;
  border-color: var(--accent);
}

.set-row span {
  font-size: 0.8rem;
  color: var(--text2);
}

.set-row .remove-set {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 1rem;
  padding: 2px;
}

.add-set-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.8rem;
  cursor: pointer;
  padding: 4px 0;
  font-weight: 500;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
  font-family: inherit;
}

.btn:active { opacity: 0.7; }

.btn.primary {
  background: var(--accent);
  color: white;
}

.btn.secondary {
  background: var(--bg2);
  color: var(--text);
}

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

.form-actions .btn { flex: 1; }

#dayDetail .empty-state {
  text-align: center;
  color: var(--text2);
  padding: 32px 16px;
  font-size: 0.9rem;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translateY(30px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@media (min-width: 768px) {
  .modal {
    align-items: center;
  }
  .modal-content {
    border-radius: var(--radius);
    max-height: 80dvh;
  }
}
