/* The dialog every page shares (reusableModal.ts, markup in partials/_reusableModal.ejs): its overlay,
   sheet, close button and buttons, and the bottom sheet a phone shows it as. */

/* Every overlay a dialog opens in (dialogVisibility.ts): it covers the page and shows only while it
   is open (dialog-open), on screen; a printout never carries a dialog. Each overlay sets its own
   backdrop and layer, and may lay its dialog out (--dialog-overlay-display). */
.dialog-overlay {
  display: none;
  position: fixed;
  inset: 0;
}

@media screen {
  .dialog-overlay.dialog-open {
    display: var(--dialog-overlay-display, block);
  }
}

.reusable-modal {
  z-index: var(--z-dialog);
  background-color: var(--shadow-heavy);
}
/* The surface every dialog and sheet is drawn on. */
.dialog-surface {
  background-color: var(--surface);
}

/* Every dialog, on any screen: placed over the page, its text centred. Its laptop frame and its phone
   sheet are below. */
.dialog-frame {
  position: absolute;
  text-align: center;
}

/* The whole dialog scrolls, unless it has a foot that stays (below). */
.reusable-modal-content:not([data-with-foot]) {
  overflow-y: auto;
}

/* On a laptop every dialog is framed and centred the same way (.dialog-frame); each sets its own
   size. A phone shows it as a sheet (.phone-sheet, below). */
@media (min-width: 769px) and (min-height: 501px) {
  .dialog-frame {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    --sheet-inline-padding: 20px;
    padding: var(--sheet-inline-padding);
    border: 1px solid var(--ink-3);
    max-width: calc(100vw - 32px);
  }

  .reusable-modal-content {
    border-radius: 10px;
    width: 408px;
    max-height: 88dvh;
  }
}

/* A short laptop gives a dialog nearly all its height. */
@media (min-width: 769px) and (min-height: 501px) and (max-height: 600px) {
  .reusable-modal-content {
    max-height: calc(100dvh - 16px);
  }
}

/* A dialog with a foot that stays: the dialog, its message and its content are one column, and the
   content's body scrolls between the title and the foot. */
.reusable-modal-content[data-with-foot] {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.reusable-modal-content[data-with-foot] .reusable-close-btn {
  flex: none;
}

[data-with-foot] .sheet-title {
  flex: none;
}

/* The message and the content inside it fill the dialog's column, so the body can scroll above the foot. */
[data-with-foot] .reusable-modal-message,
.dialog-with-foot {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 0;
}
/* Plain text in a dialog. :where() takes these to no weight at all, so a list or paragraph with
   spacing of its own (the paint list) keeps it whichever stylesheet loads last. */
:where(.reusable-modal-content p) {
  margin: 0.75em 0;
}
:where(.reusable-modal-content ul) {
  text-align: left;
  padding-left: 1.5em;
  margin: 0.5em 0;
}
/* Styles for the modal buttons */
.reusable-modal-button {
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  font-size: 15px;
  cursor: pointer;
  margin: 10px;
  transition: background-color 0.3s ease;
}

/* Affirmative button (e.g., "Yes", "Confirm") */
a.reusable-modal-button {
  display: inline-flex;
  align-items: center;
  color: white;
  text-decoration: none;
}

.reusable-modal-button-affirmative {
  background-color: var(--brand); /* Green */
  color: white;
}

@media (hover: hover) {
  .reusable-modal-button-affirmative:hover {
    background-color: var(--brand-strong);
  }
}

/* Secondary button — the ALTERNATIVE, not the dangerous one. Distinct from -negative:
   red carries a "you're declining / this destroys something" reading, which is wrong on a
   dialog whose job is reassurance (e.g. the load-failure "Start something new", which
   discards nothing). Green marks the recommended way forward; this marks the other door. */
.reusable-modal-button-secondary {
  background-color: var(--surface-2);
  color: var(--ink-5, #666);
  border: 1px solid var(--line-4);
}

@media (hover: hover) {
  .reusable-modal-button-secondary:hover {
    background-color: var(--surface-3);
  }
}

/* ---------------------------------------------------------------------------
   Every dialog: one close button (ink ×, finger-sized tap target, top-right)
   --------------------------------------------------------------------------- */
/* A dialog takes focus itself on opening (dialogFocus.ts); its first control shows the ring once Tab
   reaches it. */
[data-holds-focus]:focus {
  outline: none;
}

/* How far every dialog's × keeps inside the top-right corner of its dialog, or of the panel it sits
   in, so its hover circle never touches an edge. Each dialog places it with this. */
:root {
  --dialog-close-size: 44px;
  --dialog-close-inset: 4px;
}

.dialog-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--dialog-close-size);
  height: var(--dialog-close-size);
  /* The font draws × below the middle of its line; this lifts it to the circle's centre. */
  padding: 0 0 0.14em;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--dialog-close-color, var(--ink));
  font: inherit;
  font-size: 28px;
  font-weight: 400;
  line-height: 1;
  cursor: pointer;
  /* The ring is drawn inside the circle, so the inset that keeps the circle off the dialog's edge
     keeps the ring off it too. */
  --focus-ring-offset: -2px;
}

.dialog-close:focus-visible {
  background: var(--dialog-close-tint, var(--line-2));
}
@media (hover: hover) {
  .dialog-close:hover {
    background: var(--dialog-close-tint, var(--line-2));
  }
}

/* On a laptop the × is pinned to the dialog's top-right corner, with room for it on both sides of the
   title so the title stays centred and clear of it. A phone's sheet places both itself (.sheet-close,
   .sheet-title). */
@media (min-width: 769px) and (min-height: 501px) {
  .reusable-close-btn {
    position: absolute;
    top: var(--dialog-close-inset);
    right: var(--dialog-close-inset);
  }

  .reusable-modal-title {
    padding: 0 calc(var(--dialog-close-size) - var(--dialog-close-inset));
  }
}

/* ---------------------------------------------------------------------------
   Phone: dialogs open as bottom sheets with the title + close pinned
   --------------------------------------------------------------------------- */
@media (max-width: 768px), (max-height: 500px) {
  /* The look every phone sheet shares. */
  .phone-sheet {
    box-sizing: border-box;
    width: 100%;
    max-width: none;
    border: none;
    border-radius: var(--sheet-radius);
    box-shadow: var(--sheet-shadow);
  }

  /* A sheet that rises from the bottom edge. */
  .phone-sheet--bottom {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    transform: none;
    height: auto;
    max-height: var(--sheet-height);
    overscroll-behavior: contain;
    margin: 0;
    --sheet-inline-padding: 16px;
    --sheet-padding-top: 0px;
    padding: var(--sheet-padding-top) var(--sheet-inline-padding) calc(16px + env(safe-area-inset-bottom, 0px));
  }

  /* The editor's own sheets take their whole height, starting just under the header; a dialog's
     sheet takes what it needs. */
  .phone-sheet--full {
    height: var(--sheet-height);
  }

  /* The close button sticks to the top of the scrolling sheet; its negative bottom margin lets the
     (also sticky) title share its row instead of starting below it. */
  .sheet-close {
    position: sticky;
    top: var(--dialog-close-inset);
    right: auto;
    z-index: 3;
    float: none;
    display: flex;
    margin: var(--dialog-close-inset) calc(var(--dialog-close-inset) - var(--sheet-inline-padding)) calc(-1 * (var(--dialog-close-size) + var(--dialog-close-inset))) auto;
  }

  .sheet-title {
    position: sticky;
    top: 0;
    z-index: 2;
    margin: var(--sheet-title-margin-top, 0) calc(-1 * var(--sheet-inline-padding)) var(--sheet-title-gap, 12px);
    padding: var(--sheet-title-padding-top, 14px) calc(var(--dialog-close-size) + 2 * var(--dialog-close-inset)) 8px;
    background: var(--surface);
    font-size: 1.2rem;
    line-height: var(--sheet-title-line-height, 1.3);
    text-align: center;
  }

  /* Content scrolled up under the title is set off by a shadow rather than a permanent line
     (data-scrolled, scrollFade.ts). */
  [data-scrolled] .sheet-title {
    box-shadow: var(--sheet-title-shadow);
  }
}
