/* ==========================================================================
   Nova Link dialogs: the single dialog system.
   --------------------------------------------------------------------------
   Every <dialog> in the app gets the same shell (surface, radius, shadow,
   backdrop, entrance motion, bottom sheet on phones). Variants:

   Canonical markup for new dialogs:
     <dialog class="dialog" aria-labelledby="dlgTitle" aria-describedby="dlgText">
       <header class="dialog-header">
         <div><h2 class="dialog-title" id="dlgTitle">…</h2><p class="dialog-description" id="dlgText">…</p></div>
         <button class="dialog-close icon-btn" type="button" aria-label="Закрыть">×</button>
       </header>
       <div class="dialog-body">…</div>
       <footer class="dialog-footer">
         <button class="danger">Удалить</button>          <- danger always far left
         <button class="secondary">Отмена</button><button class="primary">Сохранить</button>
       </footer>
     </dialog>
   Sizes: .sm 400px · default 520px · .lg 680px · .xl 920px (or data-size="sm|lg|xl").
   .center keeps it centred on phones instead of becoming a bottom sheet.

   Existing generators covered here:
     #actionDialog       app.js modal(): .close, .mark (hidden), h2, .modal-body, .modal-actions, .modal-note
     #authDialog         index.html + account-services.js (.auth-switch, .auth-error) + activation-flow.js
     .nova-question      ui-dialogs.js NovaDialogs.confirm/prompt (form > h2, p, label, menu)
     .wallet-checkout-dialog (class on #actionDialog): wide base; details in billing.css
   Surface files style the insides of: .alliance-dialog (alliances.css),
   .badge-commerce / .gift-redeem (badges.css), .admin-detail /
   .admin-badge-dialog (admin.css), .email-binding-dialog / .sessions-dialog
   (settings.css).
   ========================================================================== */

@layer components {
  /* r181: a solid surface-1 panel (20px radius, hairline, soft neutral shadow) over a plain scrim:
     no orange wash inside, no blur behind. */
  dialog {
    --dialog-w: 520px;
    --dialog-pad: var(--nv-space-6);
    width: min(var(--dialog-w), calc(100vw - 32px));
    max-width: none;
    max-height: calc(100dvh - 48px);
    margin: auto;
    padding: var(--dialog-pad);
    overflow: auto;
    overscroll-behavior: contain;
    border: 1px solid var(--nv-line-strong);
    border-radius: var(--nv-r-xl);
    background: var(--nv-surface-1);
    color: var(--nv-text);
    box-shadow: var(--nv-shadow-3);
    scrollbar-gutter: stable;
  }

  dialog[open] {
    animation: nv-dialog-in var(--nv-dur-3) var(--nv-ease);
  }

  /* showModal() focuses the dialog itself while it has nothing focusable yet (loading state):
     no ring around the whole sheet, its own shadow stays. Controls inside keep their rings. */
  dialog:focus-visible {
    outline: none;
    box-shadow: var(--nv-shadow-3);
  }

  dialog::backdrop {
    background: var(--nv-scrim);
    animation: nv-fade-in var(--nv-dur-3) var(--nv-ease);
  }

  /* Must out-rank the base `dialog` rule above (same layer), so no :where() here. */
  dialog.sm, dialog[data-size="sm"] { --dialog-w: 400px; }
  dialog.lg, dialog[data-size="lg"] { --dialog-w: 680px; }
  dialog.xl, dialog[data-size="xl"] { --dialog-w: 920px; }

  /* ---- Header / body / footer --------------------------------------------- */
  :where(.dialog-header) {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--nv-space-4);
    margin-bottom: var(--nv-space-5);
  }

  :where(.dialog-title, dialog h2) {
    color: var(--nv-text);
    font-size: var(--nv-fs-22);
    font-weight: var(--nv-fw-medium);
    line-height: var(--nv-lh-snug);
    letter-spacing: var(--nv-tracking-tight);
  }

  :where(.dialog-description) {
    margin-top: var(--nv-space-2);
    color: var(--nv-text-2);
    font-size: var(--nv-fs-14);
    line-height: var(--nv-lh-body);
  }

  :where(.dialog-body) {
    display: grid;
    gap: var(--nv-space-4);
  }

  :where(.dialog-footer) {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: var(--nv-space-2);
    margin-top: var(--nv-space-6);
  }

  /* Destructive action sits apart, on the far left. */
  :where(.dialog-footer, .modal-actions) > :where(.danger) {
    order: -1;
    margin-right: auto;
  }

  /* ---- Close button (×). .close is the legacy name used by app.js. ----------- */
  :where(dialog) :where(.close, .dialog-close, .admin-detail-close) {
    display: grid;
    flex: none;
    place-items: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 1px solid transparent;
    border-radius: var(--nv-r-control);
    background: transparent;
    color: var(--nv-text-3);
    font-size: 22px;
    font-weight: var(--nv-fw-regular);
    line-height: 1;
    cursor: pointer;
    transition: background-color var(--nv-dur-1) var(--nv-ease), color var(--nv-dur-1) var(--nv-ease);
  }

  :where(dialog) :where(.close, .dialog-close, .admin-detail-close):hover {
    border-color: var(--nv-line);
    background: var(--nv-surface-2);
    color: var(--nv-text);
  }

  /* r180: a full 44px ✕ on touch, landscape included (the fixed 36px above ignores --btn-h). */
  @media (pointer: coarse) {
    :where(dialog) :where(.close, .dialog-close, .admin-detail-close) {
      width: var(--nv-hit);
      height: var(--nv-hit);
    }
  }

  /* Legacy dialogs put .close as a direct child: pin it to the corner. */
  :where(dialog) > :where(.close) {
    position: absolute;
    top: 16px;
    right: 16px;
    z-index: 2;
  }

  /* ---- #actionDialog: app.js modal() ---------------------------------------- */
  #actionDialog {
    position: fixed;
  }

  /* modal() still prints a decorative letter mark; the new dialogs carry no logo. */
  #actionDialog > .mark {
    display: none;
  }

  #actionDialog > h2 {
    margin: 0 44px var(--nv-space-4) 0;
  }

  :where(.modal-body) {
    display: grid;
    gap: var(--nv-space-4);
    color: var(--nv-text-2);
    font-size: var(--nv-fs-14);
    line-height: var(--nv-lh-body);
  }

  :where(.modal-body) :where(h3) {
    color: var(--nv-text);
    font-size: var(--nv-fs-16);
  }

  :where(.modal-note) {
    color: var(--nv-text-2);
    font-size: var(--nv-fs-14);
    line-height: var(--nv-lh-body);
  }

  :where(.modal-actions) {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: var(--nv-space-2);
    margin-top: var(--nv-space-6);
  }

  :where(.modal-actions):empty {
    display: none;
  }

  /* Checkout / wallet (payment-icons.js toggles the class). Inner layout: billing.css. */
  #actionDialog.wallet-checkout-dialog {
    --dialog-w: 640px;
  }

  /* ---- Auth dialog (#authDialog) --------------------------------------------- */
  #authDialog {
    --dialog-w: 440px;
    --dialog-pad: 32px;
    position: fixed;
    text-align: center;
  }

  #authDialog > .mark.big {
    margin: 0 auto var(--nv-space-4);
  }

  #authDialog > h2 {
    font-size: var(--nv-fs-22);
    font-weight: var(--nv-fw-medium);
    letter-spacing: var(--nv-tracking-tight);
    text-wrap: balance;
  }

  #authDialog > p {
    margin: var(--nv-space-2) auto var(--nv-space-6);
    max-width: 34ch;
    color: var(--nv-text-2);
    font-size: var(--nv-fs-14);
    line-height: var(--nv-lh-body);
  }

  #authForm {
    display: grid;
    gap: var(--nv-space-4);
    text-align: left;
  }

  #authForm > button[type="submit"] {
    --btn-h: var(--nv-h-lg);
    width: 100%;
    margin-top: var(--nv-space-1);
    font-size: var(--nv-fs-16);
  }

  /* "Forgot password" only makes sense when logging in. */
  #authDialog:not([data-mode="login"]) #forgotPassword {
    display: none;
  }

  #authDialog #forgotPassword {
    margin: var(--nv-space-2) auto 0;
  }

  /* "or continue with" separator */
  #authDialog .or {
    display: flex;
    align-items: center;
    gap: var(--nv-space-3);
    margin: var(--nv-space-6) 0 var(--nv-space-4);
    color: var(--nv-text-3);
    font-size: var(--nv-fs-12);
    font-weight: var(--nv-fw-medium);
  }

  #authDialog .or::before,
  #authDialog .or::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--nv-line);
  }

  .auth-providers {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: var(--nv-space-2);
  }

  .auth-provider {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--nv-space-2);
    min-width: 0;
    min-height: var(--nv-h-lg);
    padding: 0 12px;
    border: 1px solid var(--nv-line-strong);
    border-radius: var(--nv-r-control);
    background: var(--nv-surface-2);
    color: var(--nv-text);
    font-size: var(--nv-fs-13);
    font-weight: var(--nv-fw-medium);
    overflow: hidden;
    cursor: pointer;
    transition: background-color var(--nv-dur-1) var(--nv-ease), border-color var(--nv-dur-1) var(--nv-ease);
  }

  .auth-provider:hover {
    border-color: var(--nv-line-strong);
    background: var(--nv-surface-3);
  }

  .auth-provider > svg {
    flex: none;
    width: 20px;
    height: 20px;
  }

  #discordAuth > svg { fill: #8b95ff; }
  .telegram-provider > svg { fill: #3fb3ec; }

  /* Telegram: the real widget iframe covers the tile invisibly (it must be
     clicked directly); if loading fails, auth-widget.js puts a visible retry
     button in the same holder. */
  .telegram-login {
    position: absolute;
    inset: 0;
    z-index: 2;
    overflow: hidden;
  }

  .telegram-login:empty {
    display: none;
  }

  .telegram-login > iframe {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    min-height: var(--nv-h-lg);
    border: 0;
    opacity: .001;
  }

  .telegram-provider:focus-within {
    outline: 2px solid var(--nv-focus);
    outline-offset: 2px;
  }

  /* Visible error / retry state */
  .telegram-login > button {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 4px 8px;
    border-radius: inherit;
    background: var(--nv-surface-2);
    color: var(--nv-accent-text);
    font-size: var(--nv-fs-12);
    font-weight: var(--nv-fw-medium);
    line-height: 1.2;
    text-align: center;
    text-wrap: balance;
  }

  .telegram-login > button:hover {
    background: var(--nv-surface-3);
    color: var(--nv-accent-text);
  }

  /* Loading state (progressive: :has support only): only the script is present. */
  .telegram-provider:has(.telegram-login > script:only-child) > span {
    opacity: .5;
  }

  .telegram-provider:has(.telegram-login > script:only-child)::after {
    content: "";
    position: absolute;
    top: 8px;
    right: 8px;
    width: 10px;
    height: 10px;
    border: 2px solid var(--nv-line-strong);
    border-top-color: var(--nv-accent);
    border-radius: 50%;
    animation: nv-spin .75s linear infinite;
  }

  #authDialog > small {
    display: block;
    margin-top: var(--nv-space-5);
    color: var(--nv-text-3);
    font-size: var(--nv-fs-12);
    line-height: var(--nv-lh-body);
  }

  #authDialog > small a {
    color: var(--nv-text-2);
  }

  /* Login / register switch (account-services.js) */
  .auth-switch {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 4px 8px;
    margin-top: var(--nv-space-4);
    padding-top: var(--nv-space-4);
    border-top: 1px solid var(--nv-line-soft);
    color: var(--nv-text-2);
    font-size: var(--nv-fs-13);
  }

  .auth-switch > button {
    min-height: 32px;
    padding: 4px 8px;
    border-radius: var(--nv-r-xs);
    color: var(--nv-accent-text);
    font-weight: var(--nv-fw-medium);
  }

  .auth-switch > button:hover {
    color: var(--nv-accent-text);
    text-decoration: underline;
    text-underline-offset: 3px;
  }

  /* Activation flow (activation-flow.js) reuses #authDialog. */
  #authDialog.activation-dialog .auth-switch {
    display: none;
  }

  /* ---- NovaDialogs (ui-dialogs.js): .nova-question ----------------------------- */
  .nova-question {
    --dialog-w: 440px;
    --dialog-pad: 0;
  }

  .nova-question > form {
    display: grid;
    gap: var(--nv-space-4);
    padding: var(--nv-space-6);
  }

  .nova-question h2 {
    font-size: var(--nv-fs-18);
    font-weight: var(--nv-fw-medium);
  }

  .nova-question p {
    margin-top: calc(var(--nv-space-2) * -1);
    color: var(--nv-text-2);
    font-size: var(--nv-fs-14);
    line-height: var(--nv-lh-body);
  }

  .nova-question label {
    display: grid;
    gap: 8px;
    color: var(--nv-text-2);
    font-size: var(--nv-fs-13);
    font-weight: var(--nv-fw-medium);
  }

  .nova-question menu {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: var(--nv-space-2);
    margin-top: var(--nv-space-2);
  }

  /* The cancel button has no class in the generated markup: give it the
     secondary look here. */
  .nova-question menu > button:not(.primary, .danger) {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--nv-h-md);
    padding: 4px 16px;
    border: 1px solid var(--nv-line-strong);
    border-radius: var(--nv-r-control);
    background: var(--nv-surface-2);
    color: var(--nv-text);
    font-size: var(--nv-fs-14);
    font-weight: var(--nv-fw-medium);
    transition: background-color var(--nv-dur-1) var(--nv-ease), transform var(--nv-dur-1) var(--nv-ease);
  }

  .nova-question menu > button:not(.primary, .danger):active {
    transform: scale(var(--nv-press-scale));
  }

  .nova-question menu > button:not(.primary, .danger):hover {
    background: var(--nv-surface-3);
  }

  /* ---- Generic shells used by surface dialogs (refined in surface files) ---- */
  :where(.alliance-dialog, .badge-commerce, .admin-detail, .admin-badge-dialog) {
    --dialog-pad: 0;
  }

  :where(.alliance-dialog-shell, .badge-commerce-shell, .gift-redeem-shell, .admin-detail-body) {
    display: grid;
    gap: var(--nv-space-4);
    padding: var(--nv-space-6);
  }

  :where(.alliance-dialog-shell, .badge-commerce-shell, .gift-redeem-shell, .admin-detail-body) > :where(header, .admin-detail-head) {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--nv-space-4);
  }

  /* The small label above a sheet title: sentence case, muted (r181: no orange tracked caps). */
  :where(.alliance-dialog-shell, .badge-commerce-shell, .gift-redeem-shell) > header span {
    color: var(--nv-text-3);
    font-size: var(--nv-fs-13);
    font-weight: var(--nv-fw-medium);
    letter-spacing: var(--nv-tracking-eyebrow);
    text-transform: none;
  }

  :where(.alliance-dialog-shell, .badge-commerce-shell, .gift-redeem-shell, .admin-detail-body) > header p {
    margin-top: var(--nv-space-2);
    color: var(--nv-text-2);
    font-size: var(--nv-fs-14);
    line-height: var(--nv-lh-body);
  }

  /* r180: the page under a modal sheet stays put on touch: a swipe on the backdrop no longer scrolls
     it away and the list position survives closing (same idea as .ed-sheet-open in editor.css). */
  @media (max-width: 640px), (pointer: coarse) {
    html:has(dialog:modal) {
      overflow: hidden;
    }
  }

  /* ---- Phones: dialogs become bottom sheets ----------------------------------- */
  @media (max-width: 640px) {
    dialog:not(.center) {
      --dialog-pad: var(--nv-space-5);
      width: 100%;
      max-height: calc(100dvh - 24px);
      margin: auto 0 0;
      padding-bottom: max(var(--dialog-pad), env(safe-area-inset-bottom));
      border-bottom: 0;
      border-radius: var(--nv-r-xl) var(--nv-r-xl) 0 0;
    }

    dialog:not(.center)[open] {
      animation: nv-sheet-in var(--nv-dur-3) var(--nv-ease);
    }

    /* r180: no grab handle: sheets close with ✕, the backdrop or Back, not by dragging, so a handle promised
       a gesture that does nothing. */
    dialog:not(.center)::before {
      content: none;
    }

    .nova-question:not(.center),
    :where(.alliance-dialog, .badge-commerce, .admin-detail, .admin-badge-dialog):not(.center) {
      --dialog-pad: 0;
    }

    #authDialog {
      --dialog-pad: var(--nv-space-6);
    }

    /* r160: compact sheets — a little less padding. Sheets that pad their own sections
       (--dialog-pad: 0 above) are left alone. */
    dialog:not(.center, .nova-question, .alliance-dialog, .badge-commerce, .admin-detail, .admin-badge-dialog) {
      --dialog-pad: var(--nv-space-4);
    }

    #authDialog {
      --dialog-pad: var(--nv-space-5);
    }

    :where(.dialog-header) {
      gap: var(--nv-space-3);
      margin-bottom: var(--nv-space-4);
    }

    :where(.dialog-description) {
      font-size: var(--nv-fs-13);
    }

    :where(.dialog-body, .modal-body) {
      gap: var(--nv-space-3);
    }

    :where(.dialog-footer, .modal-actions) {
      margin-top: var(--nv-space-5);
    }

    #actionDialog > h2 {
      margin-bottom: var(--nv-space-3);
    }

    :where(.dialog-footer, .modal-actions, .nova-question menu) {
      display: grid;
      grid-template-columns: 1fr 1fr;
    }

    /* r166 (G8): NovaDialogs confirms use the same one-row footer as modal(): Отмена · action. */
    .nova-question menu {
      display: grid;
      grid-template-columns: 1fr 1fr;
    }

    /* r166 (G8, K3): a tinted «Удалить» next to «Отмена · Сохранить» is a compact red text button in the
       same row, not a full-width red bar that outweighs Save. A solid danger (the action of a confirm)
       stays a normal half of the row. */
    :where(.dialog-footer, .modal-actions):has(> .danger:not(.solid)) {
      display: flex;
    }

    :where(.dialog-footer, .modal-actions):has(> .danger:not(.solid)) > :where(button, a) {
      flex: 1 1 0;
    }

    :where(.dialog-footer, .modal-actions):has(> .danger:not(.solid)) > .danger:not(.solid) {
      --btn-px: 10px;
      flex: 0 0 auto;
      width: auto;
      margin-right: 0;
      border-color: transparent;
      background: transparent;
    }

    :where(.dialog-footer, .modal-actions, .nova-question menu) > :only-child {
      grid-column: 1 / -1;
    }

    :where(.dialog-footer, .modal-actions, .nova-question menu) > :where(button, a) {
      width: 100%;
    }
  }

  @media (max-width: 359px) {
    .auth-providers {
      grid-template-columns: 1fr;
    }
  }
}

@keyframes nv-dialog-in {
  from { opacity: 0; transform: translateY(8px); }
}

@keyframes nv-sheet-in {
  from { transform: translateY(32px); opacity: .4; }
}

@keyframes nv-fade-in {
  from { opacity: 0; }
}
