/*
 * application.css — primitives on top of tokens.css
 *
 * Semantic classes only (decision log D2/D5): buttons, cards, forms,
 * flash, header/nav, empty states. Colors/sizes here always come from a
 * token — raw hex belongs in tokens.css and nowhere else.
 */

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

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

body {
  margin: 0;
  font-family: var(--font-sans);
  line-height: var(--leading);
  background: var(--color-bg);
  color: var(--color-text);
}

h1,
h2,
h3,
p {
  margin: 0 0 var(--space-4);
}

h1 {
  font-size: var(--text-2xl);
}

img,
svg {
  display: block;
  max-width: 100%;
}

/* Icons (Slice A) — inline SVG, sized with a utility class, coloured via currentColor */
.icon {
  display: inline-block;
  vertical-align: middle;
  flex-shrink: 0;
}

.icon-sm {
  width: 1rem;
  height: 1rem;
}

.icon-md {
  width: 1.25rem;
  height: 1.25rem;
}

.icon-lg {
  width: 1.5rem;
  height: 1.5rem;
}

input,
button,
select,
textarea {
  font: inherit;
}

main {
  max-width: 46rem;
  margin-inline: auto;
  padding: var(--space-4);
}

/* Header / nav */
header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

header strong {
  color: var(--color-accent);
}

header span {
  margin-left: auto;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

/* Coach top nav (Slice B, D4): horizontal tabs from 768px up, hamburger
   dropdown below. The dropdown positions against .coach-nav, not .nav. */
.coach-nav {
  position: relative;
  display: flex;
  align-items: center;
}

.nav ul {
  display: none;
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav.nav-open ul {
  display: block;
  position: absolute;
  top: calc(100% + var(--space-2));
  left: 0;
  z-index: 30;
  min-width: 11rem;
  padding: var(--space-2);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
}

.nav-link {
  display: block;
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text-muted);
  text-decoration: none;
  border-radius: var(--radius-sm);
}

.nav-link:hover {
  color: var(--color-accent);
}

.nav-link.active {
  color: var(--color-accent);
}

/* Flash */
.flash {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-sm);
}

.flash-notice {
  background: var(--color-success-soft);
  color: var(--color-success);
}

.flash-alert {
  background: var(--color-danger-soft);
  color: var(--color-danger);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  padding: var(--space-2) var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  font-weight: 600;
  line-height: var(--leading);
  min-height: var(--tap-target);
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.15s ease;
}

.btn:hover {
  transform: translateY(-1px);
}

/* Icon-only square button (reorder arrows, compact controls). Height comes
   from .btn's min-height; this adds the matching width for a square target. */
.btn-icon {
  padding: var(--space-2);
  min-width: var(--tap-target);
}

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

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

.btn-danger {
  background: var(--color-danger-soft);
  color: var(--color-danger);
}

.btn-danger:hover {
  background: var(--color-danger);
  color: var(--color-surface);
}

.btn-ghost {
  background: var(--color-surface);
  border-color: var(--color-border);
  color: var(--color-text);
}

.btn-ghost:hover {
  border-color: var(--color-accent);
  color: var(--color-accent);
}

/* Cards */
@keyframes card-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.card-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: var(--space-4);
}

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  box-shadow: var(--shadow-sm);
  animation: card-in 0.18s ease-out both;
}

.card h2 {
  margin: var(--space-1) 0 var(--space-2);
  font-size: var(--text-lg);
}

.card time,
.card .state {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.card.upcoming {
  border-left: 4px solid var(--color-accent);
}

.card.inactive {
  border-left: 4px solid var(--color-danger);
  color: var(--color-text-muted);
}

.actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-3);
}

/* Forms */
form {
  max-width: 28rem;
}

form > div {
  margin-bottom: var(--space-4);
}

label {
  display: block;
  margin-bottom: var(--space-1);
  font-weight: 600;
}

input[type="email"],
input[type="password"],
input[type="text"],
input[type="date"],
select,
textarea {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text);
}

input[type="email"]:focus,
input[type="password"]:focus,
input[type="text"]:focus,
input[type="date"]:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-accent-soft);
}

textarea {
  resize: vertical;
}

.hint {
  margin: var(--space-1) 0 0;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.errors {
  background: var(--color-danger-soft);
  color: var(--color-danger);
  border-radius: var(--radius-sm);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-4);
}

.errors h2 {
  margin-bottom: var(--space-2);
  font-size: var(--text-base);
}

.errors ul {
  margin: 0;
  padding-left: var(--space-5);
}

/* Empty states */
.empty-state {
  padding: var(--space-8) var(--space-4);
  background: var(--color-surface);
  border: 1px dashed var(--color-border);
  border-radius: var(--radius-md);
  text-align: center;
  color: var(--color-text-muted);
}

.empty-state .icon {
  display: block;
  margin: 0 auto var(--space-2);
  color: var(--color-text-muted);
}

/* Day navigation (390px-first): prev / date picker / next on one centered row */
.day-nav {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.day-nav form {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  max-width: none;
}

.day-nav input[type="date"] {
  width: auto;
}

/* Session blocks */
.block-section {
  margin-top: var(--space-3);
}

.block-header {
  margin: var(--space-3) 0 var(--space-2);
  font-size: var(--text-lg);
}

.block-subheader {
  margin: var(--space-2) 0 var(--space-1);
  font-size: var(--text-base);
  color: var(--color-text-muted);
}

.exercise-row {
  margin-bottom: var(--space-3);
}

.exercise-row strong {
  display: block;
  margin-bottom: var(--space-1);
}

.exercise-row p {
  margin-bottom: var(--space-1);
}

/* Exercise comments (Slice 16) — the athlete's private thread on /day and the
   coach inbox at /coach/comments. The disclosure reuses the notes idiom. */
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.25rem;
  height: 1.25rem;
  padding: 0 var(--space-1);
  font-size: var(--text-sm);
  font-weight: 700;
  line-height: 1;
  color: var(--color-surface);
  background: var(--color-accent);
  border-radius: 999px;
}

.comment-thread {
  margin-top: var(--space-2);
}

.comment-thread > summary {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  cursor: pointer;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.comment-thread > summary:hover {
  color: var(--color-accent);
}

.comment-count {
  min-width: 1.25rem;
  padding: 0 var(--space-1);
  font-size: var(--text-sm);
  font-weight: 700;
  text-align: center;
  color: var(--color-accent);
  background: var(--color-accent-soft);
  border-radius: 999px;
}

.comment-panel {
  margin-top: var(--space-2);
  padding: var(--space-3);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

.comment-list {
  list-style: none;
  margin: 0 0 var(--space-3);
  padding: 0;
}

.comment + .comment {
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-border);
}

.comment-meta {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  margin: 0 0 var(--space-1);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.comment-author {
  font-weight: 600;
  color: var(--color-text);
}

.comment-body {
  margin: 0;
  white-space: pre-wrap;
}

.comment-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: none;
}

.comment-form .btn {
  align-self: flex-start;
}

.comments-filter {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.comment-context,
.comment-excerpt {
  margin-bottom: var(--space-1);
}

.comment-excerpt {
  white-space: pre-wrap;
}

/* Session form nested blocks and exercises */
.exercises-fieldset {
  /* Fieldsets default to `min-width: min-content`, which lets a wide child (the
     exercise picker) burst the dialog's width at 390px (D7). */
  min-width: 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-4);
  margin-bottom: var(--space-4);
}

.exercises-fieldset legend {
  font-weight: 600;
  padding: 0 var(--space-2);
}

.session-block {
  margin-bottom: var(--space-3);
}

/* Only style the wrapper once it actually holds visible content: the element
   always exists in the form (the Stimulus controller appends re-parented blocks
   into it) and soft-deleted children stay in the DOM with `hidden`, so an
   empty or fully-removed wrapper must stay invisible. `> *` covers both the
   form's `.session-block` children and the day view's sub-header/exercise rows. */
.block-children:has(> *:not([hidden])) {
  margin-top: var(--space-3);
  padding: var(--space-3);
  background: var(--color-accent-soft);
  border-left: 3px solid var(--color-accent);
  border-radius: var(--radius-sm);
}

/* Nested blocks read lighter than their top-level parent (audit U2) */
.block-children .session-block {
  background: var(--color-surface);
  box-shadow: none;
}

.block-children .session-block label {
  font-size: var(--text-sm);
}

.block-exercise-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-2);
}

.inline-exercise {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
}

.inline-exercise .btn {
  white-space: nowrap;
}

.inline-exercise input[type="text"] {
  width: auto;
}

.session-exercise-row {
  margin-bottom: var(--space-3);
  padding: var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
}

.search-form {
  display: flex;
  gap: var(--space-2);
  max-width: none;
  margin-bottom: var(--space-4);
}

.search-form input[type="text"] {
  flex: 1;
}

/* Week calendar (Slice B, D5): stacked day sections on mobile, seven columns
   from 768px up (see the responsive block). Today's column is accent-marked
   so the "upcoming" highlight of the old list survives (AC1.2). */
.week-nav {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.week-range {
  margin-left: auto;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.week-grid {
  display: grid;
  gap: var(--space-3);
}

.week-day {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3);
}

.week-day.today {
  border-top: 4px solid var(--color-accent);
}

.week-day-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-2);
  /* Override the global `header` banner padding: the day column is already
     padded, and the extra 32px squeezed the (+)-button onto a second line on
     the wider columns. */
  padding: 0 0 var(--space-2);
}

.week-day-header h2 {
  margin: 0;
  min-width: 0;
  font-size: var(--text-base);
}

.week-day.today .week-day-header h2 {
  color: var(--color-accent);
}

.day-session-list {
  list-style: none;
  margin: var(--space-3) 0 0;
  padding: 0;
  display: grid;
  gap: var(--space-2);
}

.day-session {
  padding: var(--space-2) var(--space-3);
  box-shadow: none;
}

.day-session h3 {
  margin: 0 0 var(--space-1);
  font-size: var(--text-base);
}

.day-session .actions {
  flex-wrap: wrap;
  margin-top: var(--space-2);
}

.day-empty {
  margin: var(--space-3) 0 0;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

/* Modal (Slice B, D6): native <dialog> overlay hosting the shared session
   form. After a failed create the dialog is re-rendered with the plain `open`
   attribute (no showModal call), so it needs the same fixed centering. */
.modal {
  border: none;
  border-radius: var(--radius-md);
  padding: var(--space-4);
  margin: auto;
  width: min(34rem, calc(100vw - 2 * var(--space-4)));
  max-height: calc(100vh - 2 * var(--space-8));
  overflow-y: auto;
  background: var(--color-bg);
  box-shadow: var(--shadow-md);
}

.modal[open] {
  position: fixed;
  inset: 0;
  z-index: 40;
}

.modal::backdrop {
  background: var(--overlay);
}

/* D8/D10: the shared form fills the dialog instead of the global 28rem cap,
   so the wider dialog actually buys room for one-line exercise rows. */
.modal form {
  max-width: none;
}

/* D8: notes live behind a disclosure. Closed by default (the common case is an
   empty note), open when the field already has content. */
.notes-disclosure {
  margin: 0;
}

.notes-disclosure > summary,
.inline-create > summary {
  cursor: pointer;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.notes-disclosure > summary:hover,
.inline-create > summary:hover {
  color: var(--color-accent);
}

.notes-disclosure[open] > summary,
.inline-create[open] > summary {
  margin-bottom: var(--space-1);
}

/* D9: keep the dialog title/close and the submit actions always reachable while
   the dialog itself remains the single scrollport. The negative margins cancel
   the dialog's padding so the chrome reaches its edges. */
.modal-header {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin: calc(-1 * var(--space-4)) calc(-1 * var(--space-4)) var(--space-4);
  padding: var(--space-4);
  background: var(--color-bg);
  border-bottom: 1px solid var(--color-border);
}

.modal-header h2 {
  margin: 0;
  font-size: var(--text-xl);
}

.modal form > .actions {
  position: sticky;
  bottom: 0;
  z-index: 2;
  margin: var(--space-4) calc(-1 * var(--space-4)) calc(-1 * var(--space-4));
  padding: var(--space-3) var(--space-4);
  background: var(--color-bg);
  border-top: 1px solid var(--color-border);
}

/* U5: brief highlight on the exercise row inserted by inline creation */
@keyframes row-flash {
  from {
    background-color: var(--color-accent-soft);
  }

  to {
    background-color: var(--color-surface);
  }
}

.exercise-row-flash {
  animation: row-flash 1.5s ease-out;
}

/* Responsive — mobile-first (D10): base targets 390px, then tablet and desktop */
@media (min-width: 768px) {
  main {
    max-width: 52rem;
    padding: var(--space-6) var(--space-5);
  }

  .card-list {
    grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr));
  }

  /* Coach nav: tabs inline, hamburger hidden */
  .nav-toggle {
    display: none;
  }

  .nav ul,
  .nav.nav-open ul {
    display: flex;
    position: static;
    gap: var(--space-1);
    min-width: 0;
    padding: 0;
    background: none;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }

  /* Week calendar: seven Mon-Sun columns (D5) */
  .week-grid {
    grid-template-columns: repeat(7, minmax(0, 1fr));
  }

  /* D10: the dialog widens with the viewport (544px → 640px). */
  .modal {
    width: 40rem;
  }

  /* D8: exercise picker and its icon actions share one row now that the dialog
     is wide enough; notes stay on their own line below. Explicit placement is
     required because the notes span both columns. */
  .session-exercise-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: var(--space-2);
  }

  .session-exercise-row .exercise-field {
    grid-column: 1;
    grid-row: 1;
  }

  .session-exercise-row > .actions {
    grid-column: 2;
    grid-row: 1;
    margin-top: 0;
  }

  .session-exercise-row .exercise-notes {
    grid-column: 1 / -1;
    grid-row: 2;
  }
}

@media (min-width: 1024px) {
  main {
    max-width: 64rem;
    padding: var(--space-8) var(--space-6);
  }

  .modal {
    width: 45rem;
  }
}

/* D1: on large/4K displays the week calendar's seven columns need room to breathe.
   Scoped to pages that actually render the calendar — widening every page just
   strands narrow forms (e.g. the sign-in form) at the left edge. */
@media (min-width: 1440px) {
  main:has(.week-grid) {
    max-width: 90rem;
  }
}

/* Respect the user's motion preference (Slice A animations are decorative) */
@media (prefers-reduced-motion: reduce) {
  .card {
    animation: none;
  }

  .btn {
    transition: none;
  }

  .btn:hover {
    transform: none;
  }

  .exercise-row-flash {
    animation: none;
  }
}
