/* integration.css
   Visual layer for custom enhanced <select> controls (see custom-selects.js).

   Loads AFTER style.css / catalog-archive.css. Mobile gets a bottom sheet;
   widths >= 720px get a centered dialog. Themed for data-scheme="dark"/"light"
   plus prefers-color-scheme, with app CSS tokens preferred when they exist.

   Adjust --dd-z if your topbar / bottom nav / modals use a z-index above it;
   the backdrop must overlay fixed topbar and bottom navigation (QA round 2
   P0: popover extended below viewport and bottom-nav intercepted clicks).
*/

:root {
  --dd-z: 2147483000;
  --dd-accent: var(--accent, #4f46e5);
  --dd-accent-soft: var(--accent-soft, rgba(79, 70, 229, 0.14));
  --dd-danger: var(--danger, #d92d20);
  --dd-bg: var(--surface, #ffffff);
  --dd-field-bg: var(--surface-2, #f7f8fa);
  --dd-fg: var(--text, #1f2430);
  --dd-muted: var(--muted, #5c6470);
  --dd-border: var(--border, rgba(23, 29, 41, 0.16));
  --dd-shadow-sheet: 0 -8px 32px rgba(15, 18, 34, 0.22);
  --dd-shadow-float: 0 12px 40px rgba(15, 18, 34, 0.24);
}

[data-scheme="dark"] {
  --dd-bg: var(--surface, #14161d);
  --dd-field-bg: var(--surface-2, #1d2029);
  --dd-fg: var(--text, #e7e9ee);
  --dd-muted: var(--muted, #98a0ad);
  --dd-border: var(--border, rgba(255, 255, 255, 0.16));
  --dd-accent-soft: var(--accent-soft, rgba(122, 122, 255, 0.22));
  --dd-shadow-sheet: 0 -8px 32px rgba(0, 0, 0, 0.55);
  --dd-shadow-float: 0 12px 40px rgba(0, 0, 0, 0.55);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-scheme="light"]):not([data-scheme="dark"]) {
    --dd-bg: var(--surface, #14161d);
    --dd-field-bg: var(--surface-2, #1d2029);
    --dd-fg: var(--text, #e7e9ee);
    --dd-muted: var(--muted, #98a0ad);
    --dd-border: var(--border, rgba(255, 255, 255, 0.16));
    --dd-accent-soft: var(--accent-soft, rgba(122, 122, 255, 0.22));
    --dd-shadow-sheet: 0 -8px 32px rgba(0, 0, 0, 0.55);
    --dd-shadow-float: 0 12px 40px rgba(0, 0, 0, 0.55);
  }
}

/* Explicit light selection wins even when the OS prefers dark
   (the dark media query excludes [data-scheme="light"] on :root, and this
   block re-asserts light tokens when the attribute sits on body or another wrapper). */
[data-scheme="light"] {
  --dd-bg: var(--surface, #ffffff);
  --dd-field-bg: var(--surface-2, #f7f8fa);
  --dd-fg: var(--text, #1f2430);
  --dd-muted: var(--muted, #5c6470);
  --dd-border: var(--border, rgba(23, 29, 41, 0.16));
  --dd-accent-soft: var(--accent-soft, rgba(79, 70, 229, 0.14));
  --dd-shadow-sheet: 0 -8px 32px rgba(15, 18, 34, 0.22);
  --dd-shadow-float: 0 12px 40px rgba(15, 18, 34, 0.24);
}

/* ── Shell and hidden-but-submitting native select ────────────────────── */

.select-shell {
  position: relative;
  width: 100%;
}

.select-native {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0 0 0 0) !important;
  clip-path: inset(50%) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

/* ── Trigger ──────────────────────────────────────────────────────────── */

.select-shell,
.select-button,
.select-popover,
.select-close-btn,
.select-search,
.select-option,
.select-error {
  box-sizing: border-box;
}

.select-button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
  min-height: 44px; /* touch target */
  padding: 0.55rem 0.9rem;
  font: inherit;
  font-size: 1rem;
  text-align: left;
  color: var(--dd-fg);
  background: var(--dd-field-bg);
  border: 1px solid var(--dd-border);
  border-radius: 10px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.select-button:hover {
  border-color: var(--dd-accent);
}

.select-button:focus-visible {
  outline: 2px solid var(--dd-accent);
  outline-offset: 2px;
}

.select-button[aria-invalid="true"] {
  border-color: var(--dd-danger);
}

/* Disabled native selects produce inert triggers (QA round 3 fixture) */
.select-button:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

.select-button:disabled:hover {
  border-color: var(--dd-border);
}

.select-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Generated caret. Renamed from .select-chevron (QA round 3): the app
   templates keep their own legacy decorative .select-chevron nodes, and the
   two previously collided in selectors and screenshots. */
.select-trigger-chevron {
  display: inline-flex;
  flex: none;
  color: var(--dd-muted);
  transition: transform 0.15s ease;
}

.select-button[aria-expanded="true"] .select-trigger-chevron {
  transform: rotate(180deg);
}

/* The legacy decorative chevron now decorates a hidden native select; the
   enhanced trigger draws its own caret, so hide the orphaned sibling instead
   of rendering a doubled icon. */
.select-shell ~ .select-chevron {
  display: none !important;
}

/* ── Backdrop (covers fixed topbar and bottom navigation) ─────────────── */

.select-backdrop {
  position: fixed;
  inset: 0;
  z-index: var(--dd-z);
  background: rgba(8, 10, 18, 0.55);
}

/* ── Bottom sheet (mobile) ────────────────────────────────────────────── */

.select-popover[hidden] {
  display: none;
}

.select-popover {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: calc(var(--dd-z) + 1);
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: min(85vh, 620px);
  color: var(--dd-fg);
  background: var(--dd-bg);
  border: 1px solid var(--dd-border);
  border-bottom: 0;
  border-radius: 16px 16px 0 0;
  box-shadow: var(--dd-shadow-sheet);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

@supports (height: 1dvh) {
  .select-popover {
    max-height: min(85dvh, 620px);
  }
}

.select-popover::before {
  content: "";
  display: block;
  flex: none;
  width: 3rem;
  height: 4px;
  margin: 0.6rem auto 0;
  border-radius: 999px;
  background: var(--dd-border);
}

.select-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex: none;
  padding: 0.6rem 1rem 0.7rem;
  border-bottom: 1px solid var(--dd-border);
}

.select-title {
  font-weight: 650;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.select-close-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  min-width: 44px;
  min-height: 44px;
  color: var(--dd-muted);
  background: transparent;
  border: 0;
  border-radius: 10px;
  cursor: pointer;
}

.select-close-btn:hover {
  background: var(--dd-accent-soft);
  color: var(--dd-fg);
}

.select-close-btn:focus-visible {
  outline: 2px solid var(--dd-accent);
  outline-offset: 2px;
}

/* ── Search ───────────────────────────────────────────────────────────── */

.select-search {
  flex: none;
  width: calc(100% - 1.5rem);
  margin: 0.8rem 0.75rem 0.4rem;
  min-height: 44px; /* touch target (QA round 3 P2: was 42px, computed ~43.6) */
  padding: 0.55rem 0.8rem;
  font: inherit;
  color: var(--dd-fg);
  background: var(--dd-field-bg);
  border: 1px solid var(--dd-border);
  border-radius: 10px;
  -webkit-appearance: none;
  appearance: none;
}

.select-search:focus-visible {
  outline: 2px solid var(--dd-accent);
  outline-offset: 1px;
}

/* ── Options list ─────────────────────────────────────────────────────── */

.select-list {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0.35rem 0.55rem 0.75rem;
}

.select-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  min-height: 44px;
  padding: 0.6rem 0.85rem;
  font: inherit;
  text-align: left;
  color: var(--dd-fg);
  background: transparent;
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.select-option:hover {
  background: var(--dd-accent-soft);
}

.select-option:focus-visible {
  outline: 2px solid var(--dd-accent);
  outline-offset: -2px;
}

.select-option[aria-selected="true"] {
  background: var(--dd-accent-soft);
  color: var(--dd-accent);
  font-weight: 650;
}

.select-empty {
  padding: 1.2rem 1rem;
  text-align: center;
  color: var(--dd-muted);
}

/* ── Inline validation error (persistent, linked via aria-describedby) ── */

.select-shell .select-error {
  margin: 0.3rem 0 0;
  color: var(--dd-danger);
  font-size: 0.875rem;
  line-height: 1.3;
}

.select-shell .select-error[hidden] {
  display: none;
}

/* ── Desktop: centered dialog instead of bottom sheet ─────────────────── */

@media (min-width: 720px) {
  .select-popover {
    left: 50%;
    right: auto;
    bottom: auto;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 520px;
    max-height: min(70vh, 560px);
    border-bottom: 1px solid var(--dd-border);
    border-radius: 14px;
    box-shadow: var(--dd-shadow-float);
    padding-bottom: 0;
  }

  .select-popover::before {
    content: none;
  }
}

/* ── Reduced motion ───────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  .select-trigger-chevron,
  .select-popover,
  .select-backdrop,
  .select-option {
    /* !important: the app stylesheet sets its own (tiny but non-zero)
       transitions that otherwise win the cascade (QA round 3: 1e-05s) */
    transition: none !important;
    transition-duration: 0s !important;
  }
}
