/* ==========================================================================
   MODAL CHROME — single owner of the shared modal component.

   Everything in this file is the shared component. Page CSS may tune layout
   of a modal's *contents*, but must not restyle the chrome itself.

   SoT: variables.css holds --modal-* tokens and the 4-step width scale.
   Structure: .modal > .modal-content > (.modal-header + .modal-body + .modal-footer)

   Rules:
   - no literal shadows / radii here; all come from --modal-* tokens
   - widths come only from --modal-w-sm|md|lg|xl (or --modal-w-fluid)
   - z-index comes only from --z-modal / --z-modal-confirm / --z-modal-top
   ========================================================================== */

/* ===== Overlay ===== */

.modal {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  background: var(--modal-overlay-bg);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: var(--z-modal);
  padding: var(--modal-gutter);
  box-sizing: border-box;
}

/* ===== Surface ===== */

.modal-content {
  background: var(--color-white);
  border-radius: var(--modal-radius);
  box-shadow: var(--modal-shadow);
  max-width: 90%;
  width: 100%;
  display: flex;
  flex-direction: column;
  max-height: var(--modal-max-h);
  overflow: hidden;
  animation: var(--modal-anim);
}

@keyframes modal-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* ===== Width scale (the only sanctioned widths) ===== */

.modal-content--sm { max-width: var(--modal-w-sm); }
.modal-content--md { max-width: var(--modal-w-md); }
.modal-content--lg { max-width: var(--modal-w-lg); }
.modal-content--xl { max-width: var(--modal-w-xl); }
.modal-content--fluid { max-width: var(--modal-w-fluid); }
.modal-content--fluid { max-width: var(--modal-w-fluid); }

/* ===== Header ===== */

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--color-border-medium);
  flex-shrink: 0;
}

.modal-header h3 {
  margin: 0;
  font-size: var(--modal-title-size);
  font-weight: 600;
  color: var(--color-text-dark);
  line-height: 1.3;
}

/* Tinted header variant (modals with a coloured band, e.g. cost entry). */
.modal-header--tinted {
  background: var(--color-bg-light);
  border-radius: var(--modal-radius) var(--modal-radius) 0 0;
}

/* Title + optional subtitle stack (replaces per-page .modal-subtitle hacks). */
.modal-header-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.modal-subtitle {
  margin: 0;
  font-size: 12px;
  font-weight: 500;
  color: var(--color-text-lighter);
  line-height: 1.4;
}

/* ===== Body ===== */

.modal-body {
  padding: 16px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.modal-body-scroll {
  max-height: 70vh;
  overflow-y: auto;
}

.cg-capocommessa-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-sm);
  padding: var(--spacing-xs) var(--spacing-sm);
  border: var(--border-width) solid var(--color-border-medium);
  border-radius: var(--radius-md);
  margin-bottom: var(--spacing-xs);
}

.modal-hint {
  margin: 0 0 14px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--color-text-lighter);
}

/* ===== Footer ===== */

.modal-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  padding: 14px 16px;
  border-top: 1px solid var(--color-border-medium);
  background: var(--color-bg-light);
  flex-shrink: 0;
  flex-wrap: wrap;
}

/* Split footer: hint/totals left, actions right. */
.modal-footer-split {
  justify-content: space-between;
  gap: 12px;
}

.modal-footer-hint {
  font-size: 11px;
  color: var(--color-text-lighter);
}

.modal-footer-stack {
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
}

.modal-footer-actions {
  align-self: flex-end;
  margin-left: 0;
}

/* ===== Close button ===== */

.close-modal {
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  color: var(--color-text-lighter);
  cursor: pointer;
  padding: 0;
  line-height: 1;
  font-size: 20px;
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition-fast), color var(--transition-fast);
}

.close-modal:hover {
  background: var(--color-bg-gray);
  color: var(--color-text);
}

.close-modal:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* ===== Z-order: confirm sits above dialogs, commessa above both ===== */

#commessa-modal {
  z-index: var(--z-modal-top);
}

#confirm-modal {
  z-index: var(--z-modal-confirm);
}

/* Commessa modal (3-column wizard). The page strip is the shared stepper in
   its clickable variant; only the wrap spacing stays page-specific. */
#commessa-modal .commessa-modal-grid {
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.commessa-modal-pager {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.commessa-modal-page-label {
  font-size: 12px;
  color: var(--color-text-lighter);
  min-width: 4.5rem;
  text-align: center;
}

.commessa-modal-footer-actions {
  display: inline-flex;
  gap: 8px;
  margin-left: auto;
}

/* ===== Confirm dialog ===== */

#confirm-modal .modal-content {
  max-width: var(--modal-w-sm);
}

.confirm-modal-footer {
  justify-content: space-between;
  align-items: center;
}

.confirm-modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

#confirm-modal .confirm-btn-main {
  background: var(--color-primary);
}

#confirm-modal .confirm-btn-main:hover {
  background: var(--color-primary-dark);
}

#confirm-modal .confirm-btn-main.is-danger {
  background: var(--color-danger);
}

#confirm-modal .confirm-btn-main.is-danger:hover {
  background: var(--color-danger-dark);
}

/* ===== Stepper (multi-step dialogs) =====

   One component, two skins. `cg-stepper--dots` is the compact inline variant
   (import presenze); `cg-stepper--circles` is the larger modal variant
   (conferma mese). State classes are always is-active / is-done.

   Owner: components-modals.css. Pages must not restyle it. */

.cg-stepper {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  color: var(--color-text-lighter);
}

.cg-step {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--color-text-lighter);
}

.cg-step-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--color-text-light);
}

.cg-step.is-active .cg-step-label {
  color: var(--color-primary);
}

.cg-step.is-done .cg-step-label {
  color: var(--color-success);
}

.cg-step-connector {
  flex: 1;
  min-width: 32px;
  height: 2px;
  flex-shrink: 0;
  background: var(--color-border);
  border-radius: var(--radius-full);
}

.cg-step.is-done + .cg-step-connector,
.cg-step-connector.is-done {
  background: var(--color-success);
}

/* --- dots variant (compact) --- */

.cg-stepper--dots .cg-step-dot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  flex-shrink: 0;
  border-radius: var(--radius-full);
  background: var(--color-bg-gray);
  border: 1px solid var(--color-border-medium);
  color: var(--color-text-lighter);
  font-size: 12px;
  font-weight: 700;
}

.cg-stepper--dots .cg-step.is-active .cg-step-dot {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: var(--color-white);
}

.cg-stepper--dots .cg-step.is-done .cg-step-dot {
  background: var(--color-success);
  border-color: var(--color-success);
  color: var(--color-white);
}

/* --- circles variant (modal: Nuovo dipendente, Conferma mese, Scelte vestiario) --- */

.cg-stepper--circles .cg-step-dot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  flex-shrink: 0;
  border-radius: var(--radius-full);
  border: 2px solid var(--color-border-medium);
  background: var(--color-white);
  color: var(--color-text-light);
  font-size: 13px;
  font-weight: 700;
  transition: border-color var(--transition-fast), background var(--transition-fast),
    color var(--transition-fast), box-shadow var(--transition-fast);
}

/* Filled primary + white numeral: same contrast as dots active / Nuovo dipendente. */
.cg-stepper--circles .cg-step.is-active .cg-step-dot {
  border-color: var(--color-primary);
  background: var(--color-primary);
  color: var(--color-white);
  box-shadow: 0 0 0 3px var(--color-primary-shadow);
}

.cg-stepper--circles .cg-step.is-done .cg-step-dot {
  border-color: var(--color-success);
  background: var(--color-success);
  color: var(--color-white);
}

.cg-stepper--circles .cg-step-label {
  font-size: 13px;
  color: var(--color-text-light);
}

.cg-stepper--circles .cg-step.is-active .cg-step-label {
  color: var(--color-primary);
}

/* Clickable variant (commessa page strip — pill chips, not modal circles). */
.cg-step--clickable {
  cursor: pointer;
  border: 1px solid var(--color-border-medium);
  background: var(--color-bg-gray);
  border-radius: var(--radius-md);
  padding: 6px 10px;
  font-family: var(--font-family);
  line-height: 1.2;
}

.cg-step--clickable:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
}

.cg-step--clickable.is-active {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: var(--color-white);
}

/* Dirty marker: this step has unsaved edits. */
.cg-step.is-dirty:not(.is-active)::after {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-left: 6px;
  border-radius: var(--radius-full);
  background: var(--color-warning-strong);
  vertical-align: middle;
}

.cg-stepper--inline {
  margin-bottom: 8px;
}

/* Stepper mount between header and body (import, conferma, nuovo dipendente). */
.modal-stepper {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: var(--spacing-md) var(--spacing-lg);
  border-bottom: 1px solid var(--color-border-light);
}

.modal-step {
  display: none;
}

.modal-step.is-active {
  display: block;
}

/* ===== Responsive ===== */

/* Every dialog, not just commessa, must fit a phone. */
@media (max-width: 600px) {
  .modal {
    padding: 0.75rem;
  }

  .modal-content,
  .modal-content--sm,
  .modal-content--md,
  .modal-content--lg,
  .modal-content--xl,
  .modal-content--fluid {
    width: 100%;
    max-width: 100%;
    max-height: 94vh;
  }

  #commessa-modal .commessa-modal-grid {
    grid-template-columns: 1fr;
  }

  .modal-footer,
  .modal-footer-split {
    flex-direction: column-reverse;
    align-items: stretch;
  }

  .modal-footer-actions {
    align-self: stretch;
  }

  .confirm-modal-actions {
    margin-left: 0;
    width: 100%;
  }

  .confirm-modal-actions > * {
    flex: 1;
  }
}

@media (max-width: 1024px) {
  #commessa-modal .commessa-modal-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
