/* ============================================================================
   MODAL / SHEET / FLOATING PANEL: the chrome of shared/_modal_card and
   shared/_sheet_card, in the #modal and #panel frames.
   ============================================================================ */

/* --- Modal (shared/_modal_card.html.erb) ------------------------------- */

.modal-card {
  background: var(--sf);
  border: var(--bw) solid var(--line);
  border-radius: var(--r-modal);
  box-shadow: var(--sh);
  width: 100%;
  max-width: 560px;
  min-width: min(440px, 100%);
  display: flex;
  flex-direction: column;
  max-height: calc(100dvh - 32px);
}

/* Desktop: centered on the whole window, horizontally and vertically,
   sidebar or not. max-height above keeps it inside a short window; the body
   scrolls between the pinned header and footer. */
@media (min-width: 46.25rem) {
  dialog.modal-dialog:has(.modal-card) {
    align-items: center;
    justify-content: center;
    padding: var(--sp-4);
  }
}

.modal-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--sp-4);
  padding: var(--sp-6) var(--sp-6) var(--sp-4);
  flex-shrink: 0;
}

.modal-card-title {
  margin: 0;
  font: var(--fw-bold) var(--fz-title)/28px var(--font);
  letter-spacing: var(--tracking-tight);
  color: var(--tx);
}

.modal-card-subline {
  margin: 2px 0 0;
  overflow-wrap: anywhere;
  font: var(--fw-regular) 14px/20px var(--font);
  color: var(--tx2);
}

.modal-card-body {
  padding: 0 var(--sp-6) var(--sp-6);
  overflow-y: auto;
  min-height: 0;
}

.modal-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-6);
  border-top: var(--bw) solid var(--line);
  flex-shrink: 0;
}

.modal-card-footer-end {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-left: auto;
}

/* Phone: a modal is a bottom sheet, top radius 26 with a grabber, and its
   actions sit full width at the bottom */
@media (max-width: 46.1875rem) {
  dialog.modal-dialog:has(.modal-card) {
    align-items: flex-end;
    padding: 0;
  }

  /* The #modal frame is the dialog's flex item and would shrink to its
     content; a phone sheet is full width */
  dialog.modal-dialog:has(.modal-card) > turbo-frame {
    width: 100%;
  }

  .modal-card {
    min-width: 0;
    max-height: 90dvh;
    border-radius: var(--r-sheet) var(--r-sheet) 0 0;
    border-bottom: none;
  }

  .modal-card::before {
    content: "";
    align-self: center;
    flex-shrink: 0;
    width: 36px;
    height: 5px;
    margin: 6px 0 2px;
    border-radius: var(--r-pill);
    background: color-mix(in oklch, var(--tx3) 45%, transparent);
  }

  .modal-card-header {
    padding: var(--sp-2) var(--sp-5) var(--sp-4);
  }

  .modal-card-body {
    padding: 0 var(--sp-5) var(--sp-5);
  }

  .modal-card-footer {
    padding: var(--sp-4) var(--sp-5) calc(30px + var(--safe-inset-bottom));
  }
}

/* A confirm (Delete this session) is the narrow end of the range */
.modal-card--small {
  max-width: 440px;
}

.modal-card-text {
  margin: 0;
  font: var(--fw-regular) var(--t-body) var(--font);
  color: var(--tx2);
}

@media (max-width: 46.1875rem) {
  .modal-card {
    max-width: 100%;
  }
}

/* Words that differ between the desktop panel and the phone sheet
   ("Recorded as…" is desktop only; "To Alex and 1 CC" is the phone line) */
.sheet-phone-only {
  display: none;
}

@media (max-width: 46.1875rem) {
  .sheet-desktop-only {
    display: none;
  }

  .sheet-phone-only {
    display: inline;
  }
}

/* --- Sheet / floating panel (shared/_sheet_card.html.erb) --------------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: var(--scrim);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--spring-dur) var(--spring);
}

.sheet-backdrop.is-visible {
  opacity: 1;
  pointer-events: auto;
}

/* A panel URL loaded as a page of its own (a reload, no JS, a shared link)
   renders its sheet inside main, not the layout's #panel. .app-content is a
   stacking context (animations.css), which caps the sheet's z-index inside
   it, so the phone bottom nav (100) and the sidebar (200) would paint over
   the sheet and its footer. Lift the whole content area above them while it
   holds a sheet. */
.app-content:has(.sheet-backdrop) {
  z-index: 1000;
}

/* Phone: a sheet takes the height its content needs, up to 90% of the
   screen; past that the body scrolls between the pinned header and footer.
   Long forms reach the cap. */
.sheet-card {
  background: var(--sf);
  width: 100%;
  height: auto;
  max-height: 90dvh;
  border-radius: var(--r-sheet) var(--r-sheet) 0 0;
  box-shadow: var(--sh);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translateY(100%);
  transition: transform var(--spring-dur) var(--spring);
}

.sheet-card.is-open {
  transform: translateY(0);
}

.sheet-grabber {
  width: 36px;
  height: 5px;
  border-radius: var(--r-pill);
  background: color-mix(in oklch, var(--tx3) 45%, transparent);
  margin: 6px auto 2px;
  flex-shrink: 0;
}

/* Phone: the grabber and header drag the sheet (sheet_drag_controller.js),
   so the browser mustn't claim that vertical drag as a scroll */
@media (max-width: 46.1875rem) {
  .sheet-backdrop {
    background: color-mix(in oklch, var(--scrim) calc(100% - var(--drag-progress, 0) * 100%), transparent);
  }

  .sheet-grabber,
  .sheet-header,
  .modal-card::before,
  .modal-card-header {
    touch-action: none;
  }
}

.sheet-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-5) var(--sp-4);
  flex-shrink: 0;
}

.sheet-header-text {
  flex: 1;
  min-width: 0;
}

.sheet-header-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

.sheet-header-nav {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

.sheet-body {
  padding: 0 var(--sp-5) var(--sp-5);
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.sheet-footer {
  padding: var(--sp-4) var(--sp-5);
  padding-bottom: calc(var(--sp-4) + var(--safe-inset-bottom));
  border-top: var(--bw) solid var(--line);
  flex-shrink: 0;
}

.sheet-footer .button {
  width: 100%;
}

@media (max-width: 46.1875rem) {
  .sheet-footer {
    padding-bottom: calc(30px + var(--safe-inset-bottom));
  }
}

/* Desktop: the floating panel, inset from the content area */
@media (min-width: 46.25rem) {
  .sheet-backdrop {
    background: transparent;
    align-items: flex-start;
    justify-content: flex-end;
    padding: 16px 16px 16px 0;
  }

  .sheet-card {
    width: 448px;
    height: auto;
    max-height: calc(100dvh - 32px);
    border-radius: var(--r-modal);
    /* In from the right edge and back out the same way, settling into place
       rather than just fading */
    transform: translateX(32px) scale(0.98);
    transform-origin: right center;
    transition: transform var(--spring-dur) var(--spring), opacity 200ms ease-out;
    opacity: 0;
  }

  .sheet-card.is-open {
    transform: translateX(0);
    opacity: 1;
  }

  .sheet-grabber {
    display: none;
  }
}

body:has(.sheet-backdrop:not(:empty)) {
  overflow: hidden;
}

/* --- Form sheet: Log session, Add and Edit player ------------ */

/* --- The panel: 480 wide and full height on desktop ----------------- */

@media (min-width: 46.25rem) {
  .sheet-card--form {
    width: 480px;
    height: calc(100dvh - 32px);
  }

  /* Unlike the floating session card, the form dims the page behind it */
  .sheet-backdrop:has(.sheet-card--form) {
    background: var(--scrim);
  }
}

.sheet-card--form .sheet-header {
  padding: 18px var(--sp-5) var(--sp-4) var(--sp-6);
  border-bottom: var(--bw) solid var(--line);
}

.sheet-card--form .modal-card-title {
  font: var(--fw-bold) 18px/24px var(--font);
  letter-spacing: 0;
}

.sheet-card--form .modal-card-subline {
  font: var(--fw-regular) var(--t-label) var(--font);
}

.sheet-card--form .sheet-body {
  padding: var(--sp-5) var(--sp-6);
}

.sheet-card--form .sheet-footer {
  padding: var(--sp-4) var(--sp-6);
}

@media (max-width: 46.1875rem) {
  .sheet-card--form .sheet-header {
    align-items: center;
    padding: 6px var(--sp-2) 10px var(--sp-4);
  }

  .sheet-card--form .modal-card-title {
    font-size: 17px;
  }

  .sheet-card--form .sheet-body {
    padding: var(--sp-4);
  }

  .sheet-card--form .sheet-footer {
    padding: var(--sp-4) var(--sp-4) calc(30px + var(--safe-inset-bottom));
  }
}

/* The form wraps the whole sheet so the footer's button submits it */
.sheet-form,
.session-form,
.session-picker {
  display: contents;
}

/* Footer: Cancel, then the primary taking the rest. Phone keeps
   only the primary, full width. A destructive action ("Delete
   Alex", Edit player) sits on the far left. */
.sheet-form-footer {
  display: flex;
  gap: 10px;
}

.sheet-footer .sheet-form-footer .sheet-form-cancel,
.sheet-footer .sheet-form-footer .sheet-form-destructive {
  width: auto;
}

.sheet-footer .sheet-form-footer .sheet-form-destructive {
  margin-right: auto;
  padding: 0 var(--sp-2);
}

.sheet-footer .sheet-form-footer:has(.sheet-form-destructive) .sheet-form-submit {
  flex: 0 1 auto;
}

.sheet-footer .sheet-form-footer .sheet-form-submit {
  flex: 1;
}

@media (max-width: 46.1875rem) {
  .sheet-footer .sheet-form-footer .sheet-form-cancel {
    display: none;
  }

  .sheet-footer .sheet-form-footer .sheet-form-submit {
    height: 50px;
  }
}

/* --- Training Days modal ----------------------------------------
   The panel's bottom sheet on phone. On desktop a modal centered
   on the whole viewport, horizontally and vertically, sidebar or not:
   480 wide (440 for Edit), radius 22, the modal header, a
   dimmed page, and a max height with the body scrolling between a pinned
   header and footer. It stays in the #panel frame, so a save closes it like
   any panel form. Only New and Edit Training Day use it. */

@media (min-width: 46.25rem) {
  .sheet-backdrop:has(.sheet-card--dialog) {
    align-items: center;
    justify-content: center;
    padding: var(--sp-4);
    background: var(--scrim);
  }

  .sheet-card--dialog {
    width: 480px;
    max-width: 100%;
    height: auto;
    max-height: calc(100dvh - 32px);
    border: var(--bw) solid var(--line);
    transform: scale(0.96);
    transform-origin: center;
  }

  .sheet-card--dialog.is-open {
    transform: translateY(0);
  }

  .sheet-card--dialog-small {
    width: 440px;
  }

  .sheet-card--dialog .sheet-header {
    padding: 22px var(--sp-6) var(--sp-4);
  }

  .sheet-card--dialog .sheet-body {
    padding: var(--sp-1) var(--sp-6) var(--sp-6);
  }

  .sheet-card--dialog .sheet-footer {
    padding: var(--sp-4) var(--sp-6);
  }

  /* Cancel then the primary, at the right; a destructive action far left */
  .sheet-card--dialog .sheet-form-footer {
    justify-content: flex-end;
  }

  .sheet-card--dialog .sheet-footer .sheet-form-footer .sheet-form-submit {
    flex: 0 1 auto;
    width: auto;
  }
}

/* --- Training Days footer ------------------------------------- */

/* The destructive action is its own form (button_to); let its button sit in
   the footer's row as if the form weren't there */
.sheet-form-destructive-form {
  display: contents;
}

/* Phone: the destructive action is a real button, sharing the row
   with the primary half and half */
@media (max-width: 46.1875rem) {
  .sheet-footer .sheet-form-footer.sheet-form-footer--split .sheet-form-destructive {
    flex: 1;
    margin-right: 0;
    height: 50px;
    padding: 0 var(--sp-4);
    background: var(--sf2);
    border-color: var(--line);
    box-shadow: var(--sh-1);
  }

  .sheet-footer .sheet-form-footer.sheet-form-footer--split .sheet-form-submit {
    flex: 1;
  }
}

/* --- Sheet content: action rows, a note, a list ---------------------------
   Used by Today's Skip or move sheet and the still-to-log list. */

.sheet-actions {
  display: flex;
  flex-direction: column;
  border: var(--bw) solid var(--line);
  border-radius: var(--r-card);
  overflow: hidden;
}

.sheet-action-form {
  display: contents;
}

.sheet-action {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  width: 100%;
  min-height: 64px;
  padding: var(--sp-3) var(--sp-4);
  border: 0;
  background: var(--sf);
  color: var(--tx);
  font: inherit;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
  list-style: none;
}

/* A line between rows; a button_to row's form is display: contents, so its
   button carries the line instead */
.sheet-actions > * + *,
.sheet-actions > * + .sheet-action-form > .sheet-action {
  border-top: var(--bw) solid var(--line);
}

.sheet-action::-webkit-details-marker {
  display: none;
}

.sheet-action:hover {
  background: var(--sf2);
}

.sheet-action:focus-visible {
  outline: none;
  box-shadow: inset var(--focus-ring);
}

.sheet-action-disc {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: var(--r-pill);
  background: var(--sf2);
  color: var(--tx2);
}

.sheet-action-disc--peach {
  background: var(--peach);
  color: var(--peach-tx);
}

.sheet-action-disc .icon {
  width: 20px;
  height: 20px;
}

.sheet-action-body {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.sheet-action-title {
  font: var(--fw-semibold) var(--t-body) var(--font);
  color: var(--tx);
}

.sheet-action-sub {
  font: var(--fw-regular) var(--t-label) var(--font);
  color: var(--tx2);
}

.sheet-action-chevron {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  color: var(--tx3);
}

.sheet-action-disclosure[open] .sheet-action-chevron {
  transform: rotate(180deg);
}

.sheet-move-form {
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
  padding: 0 var(--sp-4) var(--sp-4);
}

.sheet-move-fields {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--sp-4);
}

.sheet-move-fields .field + .field {
  margin-top: 0;
}

.sheet-move-submit {
  width: 100%;
}

.sheet-note {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
  padding: var(--sp-3) var(--sp-4);
  border: var(--bw) solid var(--lilac-edge);
  border-radius: var(--r-tile);
  background: var(--lilac);
  color: var(--lilac-tx);
}

/* A quiet note, no status: "This changes every Tuesday." */
.sheet-note--quiet {
  align-items: flex-start;
  margin: var(--sp-5) 0 0;
  border-color: var(--sf2);
  background: var(--sf2);
  color: var(--tx2);
}

.sheet-note--quiet .sheet-note-text {
  font-weight: var(--fw-regular);
}

.sheet-note > .icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.sheet-note-text {
  flex: 1;
  margin: 0;
  font: var(--fw-semibold) var(--t-body-s) var(--font);
}

.sheet-note form {
  flex-shrink: 0;
}

.sheet-error {
  margin-bottom: var(--sp-4);
  padding: var(--sp-3) var(--sp-4);
  border: var(--bw) solid var(--dan);
  border-radius: var(--r-tile);
  background: var(--dan-bg);
  color: var(--dan);
  font: var(--fw-semibold) var(--t-body-s) var(--font);
}

.sheet-error p {
  margin: 0;
}

.sheet-list {
  display: flex;
  flex-direction: column;
}

.sheet-list-row {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-height: 60px;
  padding: 10px 0;
  color: inherit;
  text-decoration: none;
}

.sheet-list-row + .sheet-list-row {
  /* The divider starts where the text does, after the avatar, as in iOS lists */
  border-top: 0;
  background: linear-gradient(var(--line), var(--line)) right top / calc(100% - 52px) var(--bw) no-repeat;
}

.sheet-list-row:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--r-field);
}

.sheet-list-row-body {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-width: 0;
}

.sheet-list-row-title {
  font: var(--fw-semibold) var(--t-body) var(--font);
  color: var(--tx);
}

.sheet-list-row-meta {
  font: var(--fw-regular) var(--t-label) var(--font);
  font-variant-numeric: tabular-nums;
  color: var(--tx2);
}

.sheet-list-row-action {
  font: var(--fw-semibold) var(--t-label) var(--font);
  color: var(--ac-text);
}

.sheet-list-row-chevron {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  color: var(--tx3);
}

@media (hover: hover) {
  .sheet-list-row:hover .sheet-list-row-action {
    text-decoration: underline;
  }
}

.sheet-quiet {
  margin: 0;
  font: var(--fw-regular) var(--t-body-s) var(--font);
  color: var(--tx2);
}

/* --- Action menu: a <details> ⋯ menu (Edit session, Player) ------------- */

.action-menu {
  position: relative;
}

.action-menu > summary {
  list-style: none;
  cursor: pointer;
}

.action-menu > summary::-webkit-details-marker {
  display: none;
}

.action-menu-list {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  min-width: 232px;
  padding: 6px;
  border: var(--bw) solid var(--line);
  border-radius: var(--r-tile);
  background: var(--sf);
  box-shadow: var(--sh-2);
  /* Grows out of the ⋯ button that opened it */
  transform-origin: top right;
  transition: transform var(--spring-dur) var(--spring), opacity 150ms ease-out;
}

@starting-style {
  .action-menu[open] .action-menu-list {
    opacity: 0;
    transform: scale(0.9);
  }
}

.action-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 0 10px;
  border-radius: 10px;
  font: var(--fw-regular) var(--t-body-s) var(--font);
  color: var(--tx);
  text-decoration: none;
}

.action-menu-item .icon {
  width: 18px;
  height: 18px;
  color: var(--tx3);
}

.action-menu-item:hover,
.action-menu-item:focus-visible {
  outline: none;
  background: var(--sf2);
}

.action-menu-item--destructive,
.action-menu-item--destructive .icon {
  color: var(--dan);
}

.action-menu-item--destructive:hover,
.action-menu-item--destructive:focus-visible {
  background: var(--dan-bg);
}

.action-menu-divider {
  margin: 6px 4px;
  border: none;
  border-top: var(--bw) solid var(--line);
}

/* Items can be links, buttons (button_to) or labels (a file input) */
.action-menu-list .button_to {
  display: contents;
}

button.action-menu-item,
label.action-menu-item {
  width: 100%;
  border: none;
  background: none;
  text-align: left;
  cursor: pointer;
}

/* A sheet or modal with no field takes focus itself when it opens (so the
   close button doesn't open ringed); the card shows no ring of its own */
.sheet-card:focus,
.modal-card:focus {
  outline: none;
}

/* --- Modal open: scales up into place on desktop, rises from the bottom
   edge on phone, the same way the sheets arrive ------------------------- */

dialog.modal-dialog .modal-card {
  transition: transform var(--spring-dur) var(--spring), opacity 200ms ease-out;
}

@starting-style {
  dialog.modal-dialog[open] .modal-card {
    opacity: 0;
    transform: scale(0.96);
  }
}

@media (max-width: 46.1875rem) {
  @starting-style {
    dialog.modal-dialog[open] .modal-card {
      opacity: 1;
      transform: translateY(100%);
    }
  }
}

/* Reduced motion: overlays cross-fade in place instead of sliding or
   scaling, and the page behind isn't pushed back */
@media (prefers-reduced-motion: reduce) {
  .sheet-card,
  .sheet-card.is-open,
  dialog.modal-dialog .modal-card,
  .action-menu-list {
    transform: none !important;
  }

  .sheet-card {
    opacity: 0;
    transition: opacity 200ms ease-out !important;
  }

  .sheet-card.is-open {
    opacity: 1;
  }

  .app-content,
  .bottom-nav {
    transform: none !important;
  }
}

/* --- Layers: a modal opened over a sheet (Preview recap, a confirm) pushes
   that sheet back and down a step, the way iOS stacks sheets ------------- */

body:has(dialog.modal-dialog[open]) #panel .sheet-card.is-open {
  transform: scale(0.96);
  transform-origin: top center;
}

@media (max-width: 46.1875rem) {
  body:has(dialog.modal-dialog[open]) #panel .sheet-card.is-open {
    transform: translateY(-12px) scale(0.92);
    border-radius: var(--r-sheet);
  }
}

/* --- Disclosures (<details>): the content grows open and closed on the
   spring instead of appearing at full height. Browsers without
   ::details-content or interpolate-size simply open instantly. ---------- */

:root {
  interpolate-size: allow-keywords;
}

details:not(.action-menu)::details-content {
  block-size: 0;
  overflow-y: clip;
  opacity: 0;
  transition: block-size var(--spring-dur) var(--spring), opacity 200ms ease-out, content-visibility var(--spring-dur) allow-discrete;
}

details:not(.action-menu)[open]::details-content {
  block-size: auto;
  opacity: 1;
}

.sheet-action-chevron {
  transition: transform var(--spring-dur) var(--spring);
}

@media (prefers-reduced-motion: reduce) {
  details::details-content {
    transition: none;
  }
}
