/* Ecofami shared modal system. Loaded last so every module follows one responsive contract. */
:root {
  --eco-modal-surface: #fff;
  --eco-modal-border: #dce9e1;
  --eco-modal-text: #17382a;
  --eco-modal-radius: 22px;
  --eco-modal-gutter: clamp(12px, 2.2vw, 28px);
  --eco-modal-shadow: 0 28px 80px rgba(9, 48, 31, .28);
}

body:has(> .modal-bg) { overscroll-behavior: none; }

body > .modal-bg,
#ecoapp > .modal-bg,
#ecoapp .modal-bg {
  position: fixed;
  inset: 0;
  z-index: 2147482000;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding: var(--eco-modal-gutter);
  overflow: hidden;
  background: rgba(13, 31, 23, .58);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  isolation: isolate;
}

.modal-bg > .modal,
.modal-bg .modal {
  position: relative;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  container: eco-modal / inline-size;
  width: min(920px, calc(100vw - (2 * var(--eco-modal-gutter))));
  max-width: 100%;
  max-height: min(92dvh, 900px);
  margin: 0;
  overflow: hidden;
  color: var(--eco-modal-text);
  background: var(--eco-modal-surface);
  border: 1px solid var(--eco-modal-border);
  border-radius: var(--eco-modal-radius);
  box-shadow: var(--eco-modal-shadow);
}

.modal-bg .modal,
.modal-bg .modal *,
.modal-bg .modal *::before,
.modal-bg .modal *::after { box-sizing: border-box; }

.modal-bg .modal-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-width: 0;
  min-height: 68px;
  padding: 17px 22px;
  border-bottom: 1px solid var(--eco-modal-border);
  background: var(--eco-modal-surface);
}

.modal-bg .modal-head h2,
.modal-bg .modal-head h3,
.modal-bg .modal-head .modal-title {
  min-width: 0;
  margin: 0;
  color: var(--eco-modal-text);
  font-size: clamp(18px, 2.2vw, 22px);
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.modal-bg .modal-head .x {
  flex: 0 0 38px;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  min-width: 38px;
  min-height: 38px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: #354a3f;
  background: #f1f6f3;
  cursor: pointer;
}

.modal-bg .modal-body {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  max-width: 100%;
  max-height: none;
  padding: 20px 22px;
  overflow-x: hidden;
  overflow-y: auto;
  overflow-wrap: anywhere;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  -webkit-overflow-scrolling: touch;
}

.modal-bg .modal-body > *,
.modal-bg .modal-body :is(section, article, div, form, fieldset, label) {
  min-width: 0;
  max-width: 100%;
}

.modal-bg .modal-body :is(img, svg, video, canvas, iframe) {
  max-width: 100%;
  height: auto;
}

.modal-bg .modal-body :is(p, b, strong, small, span, a, td, th) { overflow-wrap: anywhere; }

.modal-bg .modal-body :is(.tablewrap, .ch-table-wrap) {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  -webkit-overflow-scrolling: touch;
}

.modal-bg .modal-body :is(input:not([type="checkbox"]):not([type="radio"]), select, textarea) { max-width: 100%; }

.modal-bg .modal-foot {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 10px;
  min-width: 0;
  padding: 13px 22px max(13px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--eco-modal-border);
  background: #fbfdfb;
}

.modal-bg .modal-foot .btn {
  min-height: 42px;
  margin: 0;
  white-space: normal;
}

/* Source rows respond to the dialog width, not to the browser viewport width. */
.modal-bg .ch-source-list {
  display: grid;
  gap: 12px;
  width: 100%;
  max-height: none;
  overflow: visible;
}

.modal-bg .ch-source-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr) minmax(160px, auto);
  align-items: center;
  gap: 14px 18px;
  width: 100%;
  min-width: 0;
}

.modal-bg .ch-source-card > *,
.modal-bg .ch-source-formula,
.modal-bg .ch-source-formula > * {
  min-width: 0;
  overflow-wrap: anywhere;
}

.modal-bg .ch-source-card > :last-child { justify-content: flex-end; }

.modal-bg .ch-source-card .ch-status {
  max-width: 100%;
  text-align: center;
  white-space: normal;
}

@container eco-modal (max-width: 800px) {
  .modal-bg .ch-source-card { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .modal-bg .ch-source-card > :last-child {
    grid-column: 1 / -1;
    justify-content: flex-start;
  }
}

@container eco-modal (max-width: 560px) {
  .modal-bg .ch-source-card { grid-template-columns: minmax(0, 1fr); }
  .modal-bg .ch-source-card > :last-child {
    grid-column: auto;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-items: stretch;
  }
  .modal-bg .ch-source-card > :last-child .btn { width: 100%; }
}

/* Chat is intentionally a large application-style dialog. */
.modal-bg.ch-chat-modal-bg > .modal.ch-chat-modal {
  width: min(920px, calc(100vw - (2 * var(--eco-modal-gutter))));
  height: min(780px, 92dvh);
}

@media (max-width: 640px) {
  :root { --eco-modal-gutter: 0px; }

  body > .modal-bg,
  #ecoapp > .modal-bg,
  #ecoapp .modal-bg {
    align-items: flex-end;
    padding: max(8px, env(safe-area-inset-top)) 0 0;
  }

  .modal-bg > .modal,
  .modal-bg .modal {
    width: 100vw;
    max-width: 100vw;
    max-height: calc(100dvh - max(8px, env(safe-area-inset-top)));
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 20px 20px 0 0;
  }

  .modal-bg .modal-head {
    min-height: 60px;
    padding: 13px 15px;
  }

  .modal-bg .modal-head h2,
  .modal-bg .modal-head h3,
  .modal-bg .modal-head .modal-title { font-size: 17px; }

  .modal-bg .modal-body {
    padding: 15px;
    scrollbar-gutter: auto;
  }

  .modal-bg .modal-foot {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    padding: 11px 12px max(11px, env(safe-area-inset-bottom));
  }

  .modal-bg .modal-foot .btn {
    width: 100%;
    min-width: 0;
  }

  .modal-bg.ch-chat-modal-bg { padding: 0; }

  .modal-bg.ch-chat-modal-bg > .modal.ch-chat-modal {
    width: 100vw !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    border: 0;
    border-radius: 0;
  }
}

@media (max-height: 560px) and (orientation: landscape) {
  body > .modal-bg,
  #ecoapp > .modal-bg,
  #ecoapp .modal-bg {
    align-items: flex-end;
    padding-top: 4px;
  }
  .modal-bg > .modal,
  .modal-bg .modal { max-height: calc(100dvh - 4px); }
  .modal-bg .modal-head {
    min-height: 54px;
    padding-top: 10px;
    padding-bottom: 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .modal-bg,
  .modal-bg .modal {
    scroll-behavior: auto;
    animation: none !important;
    transition: none !important;
  }
}
