/*
 * One frame for every dialog: the showModal() family (div.modal > .modal-scrim + .dialog-card)
 * and the native <dialog class="mail-logen-dialog"> family (mail, shipping, ledger, calendar).
 * Only the frame is set here: backdrop, panel, title, close button, footer, buttons and the
 * plain form controls. What sits inside a dialog stays with the screen that opens it.
 *
 * Linked last, so these rules win over the family rules at the same specificity while a
 * screen's own, more specific rule still wins.
 *
 * Scope: not while 진행주문 (frame .order-project-workspace) or the old 운영 shell (.app-shell)
 * is on screen. Every dialog opened there keeps today's look in both themes. The :where()
 * keeps that condition from adding specificity.
 *
 * Colours are tokens. A dialog follows the dark theme only when it is in the theme layer
 * (body > .ds-layer, see design-system.css); any other dialog reads the light tokens there,
 * so a dialog whose content still has fixed light colours never turns half dark.
 */

/* Backdrop */
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal { padding: var(--ds-space-6); transition: opacity var(--ds-duration-slow) var(--ds-ease); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal-scrim { background: var(--ds-scrim); backdrop-filter: none; }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) dialog.mail-logen-dialog::backdrop { background: var(--ds-scrim); }

/* Panel: fits the viewport, scrolls inside itself */
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card {
  max-width: 100%;
  max-height: calc(100dvh - 2 * var(--ds-space-6));
  padding: var(--ds-space-6);
  border: 1px solid var(--ds-line);
  border-radius: var(--ds-radius-lg);
  color: var(--ds-ink);
  background: var(--ds-surface);
  box-shadow: var(--ds-shadow-2);
  transition: transform var(--ds-duration-slow) var(--ds-ease);
}
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) dialog.mail-logen-dialog {
  max-width: calc(100vw - 2 * var(--ds-space-4));
  max-height: calc(100dvh - 2 * var(--ds-space-4));
  border: 1px solid var(--ds-line);
  border-radius: var(--ds-radius-lg);
  color: var(--ds-ink);
  background: var(--ds-surface);
  box-shadow: var(--ds-shadow-2);
}
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal.ds-layer, dialog.mail-logen-dialog.ds-layer) { font-family: var(--ds-font-sans); }

/* Title bar: title, one line under it, close button on the right */
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--ds-space-3); margin-bottom: var(--ds-space-5); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-head > div { min-width: 0; }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-head .overline { display: none; }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal .dialog-head h2, .mail-logen-dialog h2) { margin: 0; color: var(--ds-ink); font-size: var(--ds-font-xl); font-weight: var(--ds-weight-bold); line-height: var(--ds-leading-tight); letter-spacing: var(--ds-tracking-title); overflow-wrap: anywhere; }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-head p { margin: var(--ds-space-2) 0 0; color: var(--ds-ink-2); font-size: var(--ds-font-sm); line-height: var(--ds-leading); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-head > .icon-button { flex: none; width: var(--ds-control-md); height: var(--ds-control-md); margin: calc(var(--ds-space-2) * -1) calc(var(--ds-space-2) * -1) 0 0; border-radius: var(--ds-radius-md); color: var(--ds-ink-2); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-head > .icon-button:hover { color: var(--ds-ink); background: var(--ds-sunken); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-head > .icon-button svg { width: var(--ds-icon-md); height: var(--ds-icon-md); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .mail-logen-dialog > header { padding: var(--ds-space-6); border-bottom-color: var(--ds-line); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .mail-logen-dialog > header small { color: var(--ds-ink-2); font-size: var(--ds-font-xs); }

/* Footer: buttons on the right, the main one last */
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: var(--ds-space-2); margin-top: var(--ds-space-6); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .mail-logen-dialog > footer { padding: var(--ds-space-4) var(--ds-space-6); border-top-color: var(--ds-line); background: var(--ds-sunken); }

/* Buttons: the outlined default, the one filled main button, the quiet danger */
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card .button,
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .mail-logen-dialog :is(button.secondary, button[data-close]) {
  min-height: var(--ds-control-md); padding: 0 var(--ds-space-4); border: 1px solid var(--ds-line-strong); border-radius: var(--ds-radius-md);
  color: var(--ds-ink); background: var(--ds-surface); box-shadow: none; font-size: var(--ds-font-sm); font-weight: var(--ds-weight-strong);
  transition: background-color var(--ds-duration) var(--ds-ease), border-color var(--ds-duration) var(--ds-ease); transform: none;
}
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card .button:hover:not(:disabled) { background: var(--ds-sunken); transform: none; }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .mail-logen-dialog button { border-color: var(--ds-accent); border-radius: var(--ds-radius-md); color: var(--ds-on-accent); background: var(--ds-accent); font-size: var(--ds-font-sm); font-weight: var(--ds-weight-strong); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card .button.primary { border-color: var(--ds-accent); color: var(--ds-on-accent); background: var(--ds-accent); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal .dialog-card .button.primary, .mail-logen-dialog button:not(.secondary):not([data-close])):hover:not(:disabled) { border-color: var(--ds-accent-strong); background: var(--ds-accent-strong); filter: none; }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .mail-logen-dialog :is(button.secondary, button[data-close]):hover:not(:disabled) { background: var(--ds-sunken); filter: none; }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card .button.danger { border-color: var(--ds-line-strong); color: var(--ds-danger); background: var(--ds-surface); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card .button.danger:hover:not(:disabled) { border-color: var(--ds-danger); background: var(--ds-danger-soft); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card .button:disabled { opacity: .45; }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .mail-logen-dialog button:disabled { border-color: var(--ds-line-strong); color: var(--ds-ink-3); background: var(--ds-sunken); opacity: 1; }

/* Plain form controls inside a dialog */
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal .dialog-card, .mail-logen-dialog) label { color: var(--ds-ink-2); font-size: var(--ds-font-sm); font-weight: var(--ds-weight-medium); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal .dialog-card, .mail-logen-dialog) label:has(> input:is([type="checkbox"], [type="radio"])) { color: var(--ds-ink); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal .dialog-card, .mail-logen-dialog) :is(input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="range"]), select, textarea) {
  border-color: var(--ds-line-strong); border-radius: var(--ds-radius-md); color: var(--ds-ink); background: var(--ds-surface);
}
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal .dialog-card, .mail-logen-dialog) :is(input, select, textarea)::placeholder { color: var(--ds-placeholder); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal .dialog-card, .mail-logen-dialog) :is(input[type="checkbox"], input[type="radio"]) { accent-color: var(--ds-accent); }
/* A stacked label (the plain grid label) never grows past its column; a screen's own label layout still wins (zero specificity). */
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :where(.modal .dialog-card) label { min-width: 0; grid-template-columns: minmax(0, 1fr); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :where(.modal .dialog-card > label + label) { margin-top: var(--ds-space-3); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card label small { color: var(--ds-ink-2); font-size: var(--ds-font-xs); font-weight: var(--ds-weight-regular); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card fieldset { border-color: var(--ds-line); border-radius: var(--ds-radius-md); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card legend { color: var(--ds-ink); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card .form-error { color: var(--ds-danger); font-size: var(--ds-font-sm); }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .mail-logen-dialog :is(small, .mail-logen-note) { color: var(--ds-ink-2); }

/* Keyboard position */
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal .dialog-card, .mail-logen-dialog) :is(button, a[href], summary):focus-visible { outline: 2px solid var(--ds-focus); outline-offset: 2px; box-shadow: none; }
:where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal .dialog-card, .mail-logen-dialog) :is(input, select, textarea):focus-visible { outline: none; border-color: var(--ds-accent) !important; box-shadow: 0 0 0 3px var(--ds-accent-soft); }

/* Touch and narrow screens: 44px controls, a smaller gutter so the panel fits a folded phone (360px) */
@media (max-width: 900px), (pointer: coarse) {
  :where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) :is(.modal .dialog-card .button, .modal .dialog-head > .icon-button, .mail-logen-dialog button) { min-height: var(--ds-touch); }
  :where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-head > .icon-button { width: var(--ds-touch); height: var(--ds-touch); }
}
@media (max-width: 640px) {
  :where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal { padding: var(--ds-space-3); }
  :where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-card { width: 100%; max-height: calc(100dvh - 2 * var(--ds-space-3)); padding: var(--ds-space-4); }
  :where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) dialog.mail-logen-dialog { width: calc(100vw - 2 * var(--ds-space-3)); max-width: calc(100vw - 2 * var(--ds-space-3)); max-height: calc(100dvh - 2 * var(--ds-space-3)); }
  :where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .mail-logen-dialog > header { padding: var(--ds-space-4); }
  :where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .mail-logen-dialog > footer { padding: var(--ds-space-3) var(--ds-space-4); }
  :where(:root:not(:has(> body > #app > :is(.order-project-workspace, .app-shell)))) .modal .dialog-actions > * { flex: 1 1 auto; }
}
