/* ==========================================================================
   Report generation modal
   --------------------------------------------------------------------------
   Styles the single contact-capture modal built by report_request_modal() in
   R/cityviz_utils_report_request.R, shown by Building Permits, Economic
   Indicators, Report Studio and Community Profile.

   Loaded globally from golem_add_external_resources() (R/app_ui.R), which
   brochure applies to every route including /embed, so the modal looks the
   same inside an embedded widget iframe.

   Everything is scoped to .report-request-modal / .report-request-dialog and
   prefixed rr-, so it cannot collide with the .format-card / .format-icon /
   .format-name classes report-studio-redesign.css defines for Report Studio's
   on-page picker.

   Two colours arrive as custom properties set inline on .modal-content:
     --rr-accent       the client's brand colour (config$style$brand_primary)
     --rr-accent-soft  the same colour at 8% alpha

   Metrics: 720px wide, 24px of content padding, 20px between sections, 44px
   controls.
   ========================================================================== */

/* --- Stacking ------------------------------------------------------------- */

/* Shiny already appends #shiny-modal to <body>, so the modal is not trapped in
   a lower stacking context — it is simply outranked. cityviz.css:249 gives
   .navbar-expand-xl `position: relative; z-index: 2000` and cityviz.css:1308
   gives .ticker-wrap `z-index: 10000`, both above Bootstrap's 1055/1050, so
   the navigation painted over the top of the dialog.

   The body class is added and removed by the inline script in the modal body,
   which keeps the lift scoped to this modal: every other showModal() in the
   app keeps Bootstrap's ordering. `body` + the id beats Bootstrap's `.modal`
   and `.modal-backdrop` on specificity without !important. */
body.rr-modal-open .modal-backdrop {
  z-index: 10050;
}

body.rr-modal-open #shiny-modal {
  z-index: 10060;
}

/* --- Shell ---------------------------------------------------------------- */

/* Two classes, because cityviz.css:4036 sets .modal-dialog { margin-top: 100px }
   and responsive.css:231 sets body .modal-dialog { margin: 20px 10px }. */
.modal-dialog.report-request-dialog {
  max-width: 720px;
  margin: 1rem auto;
}

.report-request-modal {
  border: none;
  border-top: 3px solid var(--rr-accent, #893448);
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
  font-family: 'Roboto', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: #3E4C59;
  /* Short screens scroll the body rather than pushing the footer off under the
     fixed navigation. dvh first-with-fallback: vh alone jumps as mobile browser
     chrome hides. .modal-content is already a flex column in Bootstrap 5, so
     capping it here is all the body's overflow rule needs. */
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
}

/* --- Header --------------------------------------------------------------- */

/* Title, badge and supporting text share the header. Overrides the global
   .modal-header { padding-top: 0; padding-bottom: 0 }. Right padding clears
   the absolutely positioned close button so a long report name wraps above it
   rather than running underneath. */
.report-request-modal .modal-header {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  flex: 0 0 auto;
  padding: 1.25rem 3.25rem 1.15rem 1.5rem;
  border-bottom: 1px solid #ece9e3;
}

.report-request-modal .modal-title {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 1.25;
  color: #1F2933;
}

.report-request-modal .rr-title-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  background: var(--rr-accent-soft, rgba(0, 0, 0, 0.05));
  color: var(--rr-accent, #893448);
  font-size: 1rem;
}

/* Full-width second row, indented to line up under the title text. */
.report-request-modal .rr-subtitle {
  flex: 0 0 100%;
  margin: 0.3rem 0 0;
  padding-left: 3rem;
  font-size: 0.9rem;
  line-height: 1.45;
  color: #52606D;
}

.report-request-modal .rr-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: #7B8794;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.report-request-modal .rr-close:hover {
  background: #f1efeb;
  color: #1F2933;
}

.report-request-modal .rr-close:focus-visible {
  outline: 2px solid var(--rr-accent, #893448);
  outline-offset: 2px;
}

/* --- Body ----------------------------------------------------------------- */

.report-request-modal .modal-body {
  padding: 1.5rem;
  overflow-y: auto;
}

.report-request-modal .rr-section {
  margin-bottom: 1.25rem;
}

/* Shiny's own stylesheet caps every .shiny-input-container at 300px. The text
   inputs escape it because they are built with width = "100%", but the format
   radiogroup and the consent checkbox would otherwise be boxed into a third of
   the modal — with the format cards' contents spilling out of their borders. */
.report-request-modal .shiny-input-container {
  width: 100%;
  max-width: 100%;
}

/* Section headings and the format picker's own <label> share one look; so does
   the email label, via .rr-label-strong further down — that rule has to come
   after .rr-label, which matches the same element at equal specificity. */
.report-request-modal .rr-section-title,
.report-request-modal .rr-format-section .control-label {
  display: block;
  margin: 0 0 0.5rem;
  font-size: 1rem;
  font-weight: 700;
  color: #1F2933;
}

.report-request-modal .rr-optional {
  font-weight: 400;
  color: #7B8794;
}

/* --- Format selection ----------------------------------------------------- */

/* Shiny renders radioButtons as .radio > label > input + span, so the <span>
   *is* the control: the wrapping label makes the whole thing clickable and the
   native radio keeps keyboard and screen-reader semantics. No JavaScript.

   Compact by design — PDF, Word and PowerPoint need no explaining, so each is
   a radio, an icon and a name on one line. */

.report-request-modal .rr-format-section .shiny-options-group {
  display: flex;
  gap: 0.6rem;
}

.report-request-modal .rr-format-section .radio {
  flex: 1 1 0;
  min-width: 0;
  margin: 0;
}

.report-request-modal .rr-format-section .radio label {
  display: block;
  position: relative;
  height: 100%;
  margin: 0;
  padding: 0;
  font-weight: 400;
  cursor: pointer;
}

.report-request-modal .rr-format-section .radio input[type="radio"] {
  position: absolute;
  top: 50%;
  left: 0.85rem;
  margin: 0;
  transform: translateY(-50%);
  accent-color: var(--rr-accent, #893448);
  cursor: pointer;
}

/* Bootstrap 5 redraws Shiny's radios and checkboxes with `appearance: none`
   and paints the dot as a background image over a #007bc2 fill, so
   `accent-color` above never takes effect and the indicator has to be
   recoloured directly. Its own selectors are .shiny-input-container .radio
   input (0,2,1); these win on specificity. */
.report-request-modal .rr-format-section .radio input[type="radio"]:checked {
  background-color: var(--rr-accent, #893448);
  border-color: var(--rr-accent, #893448);
}

.report-request-modal .rr-format-section .radio input[type="radio"]:focus {
  border-color: var(--rr-accent, #893448);
  box-shadow: 0 0 0 0.2rem var(--rr-accent-soft, rgba(0, 0, 0, 0.08));
}

/* The control itself: radio, file-type icon, name — 44px tall like the inputs
   below it. */
.report-request-modal .rr-format-section .radio label > span {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  height: 100%;
  min-height: 44px;
  padding: 0.4rem 0.75rem 0.4rem 2.5rem;
  border: 1px solid #ded9d1;
  border-radius: 8px;
  background: #fff;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.report-request-modal .rr-format-section .radio label:hover > span {
  border-color: var(--rr-accent, #893448);
}

.report-request-modal .rr-format-section .radio input:checked ~ span {
  border-color: var(--rr-accent, #893448);
  background: var(--rr-accent-soft, rgba(0, 0, 0, 0.04));
  box-shadow: inset 0 0 0 1px var(--rr-accent, #893448);
}

.report-request-modal .rr-format-section .radio input:focus-visible ~ span {
  outline: 2px solid var(--rr-accent, #893448);
  outline-offset: 2px;
}

.report-request-modal .rr-format-icon {
  flex: 0 0 auto;
  font-size: 1.2rem;
  line-height: 1;
}

/* Each file type keeps its own recognisable colour; the client accent is
   reserved for the selection state. */
.report-request-modal .rr-format-icon-pdf { color: #C8102E; }
.report-request-modal .rr-format-icon-word { color: #2B579A; }
.report-request-modal .rr-format-icon-powerpoint { color: #D24726; }
.report-request-modal .rr-format-icon-excel { color: #217346; }

.report-request-modal .rr-format-name {
  min-width: 0;
  overflow: hidden;
  font-size: 0.95rem;
  font-weight: 700;
  color: #1F2933;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* --- Form fields ---------------------------------------------------------- */

.report-request-modal .rr-field {
  min-width: 0;
}

.report-request-modal .rr-label {
  display: block;
  margin-bottom: 0.3rem;
  font-size: 0.9rem;
  font-weight: 400;
  color: #3E4C59;
}

/* The one required field's label carries the weight of a section heading. */
.report-request-modal .rr-label-strong {
  margin-bottom: 0.5rem;
  font-size: 1rem;
  font-weight: 700;
  color: #1F2933;
}

.report-request-modal .rr-required {
  color: #c0392b;
}

/* Shiny's textInput and selectInput emit their own empty .control-label; the
   visible one above is ours, so collapse the placeholder they leave behind. */
.report-request-modal .rr-field .shiny-label-null {
  display: none;
}

.report-request-modal .rr-field .form-group {
  margin-bottom: 0;
}

.report-request-modal .rr-field .form-control,
.report-request-modal .rr-field .form-select,
.report-request-modal .rr-field select {
  height: 44px;
  padding: 0 0.75rem;
  border: 1px solid #ded9d1;
  border-radius: 8px;
  font-size: 0.95rem;
  line-height: 42px;
  color: #1F2933;
}

/* Shiny's selectInput(selectize = FALSE) emits a plain <select class="form-control">,
   and Bootstrap 5's .form-control sets `appearance: none` — which strips the
   native dropdown arrow. Put one back. */
.report-request-modal .rr-field select.form-control {
  padding-right: 2.25rem;
  line-height: normal;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%237B8794' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  background-size: 14px 11px;
}

.report-request-modal .rr-field .form-control:focus,
.report-request-modal .rr-field select:focus {
  border-color: var(--rr-accent, #893448);
  box-shadow: 0 0 0 3px var(--rr-accent-soft, rgba(0, 0, 0, 0.08));
  outline: none;
}

.report-request-modal .rr-field .form-control.rr-invalid {
  border-color: #c0392b;
}

.report-request-modal .rr-field .form-control.rr-invalid:focus {
  box-shadow: 0 0 0 3px rgba(192, 57, 43, 0.15);
}

.report-request-modal .rr-field-error {
  margin-top: 0.3rem;
  font-size: 0.85rem;
  color: #c0392b;
}

.report-request-modal .rr-grid-2,
.report-request-modal .rr-grid-3 {
  display: grid;
  gap: 0.75rem;
}

.report-request-modal .rr-grid-2 {
  grid-template-columns: repeat(2, 1fr);
  margin-bottom: 0.75rem;
}

.report-request-modal .rr-grid-3 {
  grid-template-columns: repeat(3, 1fr);
}

/* --- Marketing consent ---------------------------------------------------- */

/* Kept visually apart from report delivery by a rule, so the optional opt-in
   never reads as part of getting the report. */
.report-request-modal .rr-consent {
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid #ece9e3;
}

.report-request-modal .rr-consent .form-group {
  margin-bottom: 0;
}

.report-request-modal .rr-consent .checkbox label {
  display: flex;
  gap: 0.65rem;
  align-items: flex-start;
  margin: 0;
  font-size: 0.875rem;
  line-height: 1.45;
  color: #52606D;
  cursor: pointer;
}

.report-request-modal .rr-consent .checkbox input[type="checkbox"] {
  position: static;
  flex: 0 0 auto;
  margin: 0.15rem 0 0;
  accent-color: var(--rr-accent, #893448);
  cursor: pointer;
}

/* Same Bootstrap 5 override as the format radios above. */
.report-request-modal .rr-consent .checkbox input[type="checkbox"]:checked {
  background-color: var(--rr-accent, #893448);
  border-color: var(--rr-accent, #893448);
}

.report-request-modal .rr-consent .checkbox input[type="checkbox"]:focus {
  border-color: var(--rr-accent, #893448);
  box-shadow: 0 0 0 0.2rem var(--rr-accent-soft, rgba(0, 0, 0, 0.08));
}

/* Optionality and the policy link both run inline with the sentence — neither
   gets a line of its own. */
.report-request-modal .rr-optional-tag {
  margin-left: 0.25rem;
  color: #7B8794;
}

.report-request-modal .rr-privacy-link {
  color: var(--rr-accent, #893448);
  text-decoration: underline;
  white-space: nowrap;
}

/* --- Footer --------------------------------------------------------------- */

/* Overrides the global .modal-footer { justify-content: flex-start;
   padding-top: 0 }. Slim, but the buttons keep the 44px target. */
.report-request-modal .modal-footer {
  justify-content: flex-end;
  flex: 0 0 auto;
  gap: 0.65rem;
  padding: 0.85rem 1.5rem;
  border-top: 1px solid #ece9e3;
}

.report-request-modal .modal-footer > * {
  margin: 0;
}

.report-request-modal .rr-btn-cancel,
.report-request-modal .rr-btn-generate {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  height: 44px;
  padding: 0 1.5rem;
  border-radius: 8px;
  font-size: 0.95rem;
}

/* Outlined in the client colour: clearly the lesser of the two actions, but
   still part of the same pair. */
.report-request-modal .rr-btn-cancel {
  border: 1px solid var(--rr-accent, #893448);
  background: #fff;
  font-weight: 500;
  color: var(--rr-accent, #893448);
}

.report-request-modal .rr-btn-cancel:hover {
  background: var(--rr-accent-soft, rgba(0, 0, 0, 0.04));
  color: var(--rr-accent, #893448);
}

.report-request-modal .rr-btn-generate {
  border: 1px solid var(--rr-accent, #893448);
  background: var(--rr-accent, #893448);
  font-weight: 700;
  color: #fff;
}

.report-request-modal .rr-btn-generate:hover,
.report-request-modal .rr-btn-generate:focus {
  filter: brightness(1.12);
  background: var(--rr-accent, #893448);
  border-color: var(--rr-accent, #893448);
  color: #fff;
}

.report-request-modal .rr-btn-cancel:focus-visible,
.report-request-modal .rr-btn-generate:focus-visible {
  outline: 2px solid var(--rr-accent, #893448);
  outline-offset: 2px;
}

/* --- Narrower screens ----------------------------------------------------- */

/* Two columns first: at this width the contact row's three fields are narrower
   than the text they hold, but a single column is a needlessly tall form. The
   Country select takes the full second row. */
@media (max-width: 767.98px) {
  .report-request-modal .rr-grid-3 {
    grid-template-columns: repeat(2, 1fr);
  }

  .report-request-modal .rr-grid-3 > :last-child {
    grid-column: 1 / -1;
  }
}

/* One column: format choices and every field. */
@media (max-width: 575.98px) {
  .modal-dialog.report-request-dialog {
    margin: 0.75rem;
  }

  .report-request-modal .modal-body,
  .report-request-modal .modal-footer {
    padding-left: 1.15rem;
    padding-right: 1.15rem;
  }

  .report-request-modal .modal-header {
    padding-left: 1.15rem;
    padding-right: 3rem;
  }

  .report-request-modal .rr-subtitle {
    padding-left: 0;
  }

  /* `flex: 1 1 0` sizes the cards evenly across a row, but in a column it
     collapses them to a zero basis and the contents spill out — so each sizes
     to its own content here. */
  .report-request-modal .rr-format-section .shiny-options-group {
    flex-direction: column;
  }

  .report-request-modal .rr-format-section .radio {
    flex: 0 0 auto;
  }

  .report-request-modal .rr-grid-2,
  .report-request-modal .rr-grid-3 {
    grid-template-columns: 1fr;
  }

  .report-request-modal .rr-grid-3 > :last-child {
    grid-column: auto;
  }

  .report-request-modal .modal-footer {
    flex-direction: column-reverse;
    align-items: stretch;
  }
}
