/* Custom dropdowns, date/time pickers (see static/js/ui-controls.js). Plain CSS so it works without a Tailwind rebuild. */
:root {
  --ui-primary: #3b82f6;
  --ui-primary-soft: rgba(59, 130, 246, 0.12);
  --ui-bg: #ffffff;
  --ui-bg-hover: #f3f4f6;
  --ui-text: #1f2937;
  --ui-subtext: #6b7280;
  --ui-border: #e5e7eb;
  --ui-shadow: 0 12px 32px rgba(15, 23, 42, 0.16), 0 2px 6px rgba(15, 23, 42, 0.08);
}
.dark {
  --ui-bg: #1f2937;
  --ui-bg-hover: #374151;
  --ui-text: #f9fafb;
  --ui-subtext: #9ca3af;
  --ui-border: #374151;
  --ui-primary-soft: rgba(59, 130, 246, 0.25);
  --ui-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
}

/* The original control stays in the page (forms, validation, page scripts) but is invisible. */
.ui-ctl { position: relative; display: block; min-width: 0; }
.ui-ctl.ui-inline { display: inline-block; }
.ui-native {
  position: absolute !important; inset: 0; width: 100% !important; height: 100% !important;
  opacity: 0 !important; pointer-events: none !important; margin: 0 !important;
}

/* The visible button: keeps the page's own classes, adds a chevron and left-aligned text. */
.ui-trigger {
  display: flex !important; align-items: center; justify-content: space-between; gap: 0.5rem;
  text-align: left; cursor: pointer; min-height: 2.5rem; overflow: hidden; white-space: nowrap;
}
.ui-trigger:disabled { cursor: not-allowed; opacity: 0.6; }
.ui-trigger .ui-label { overflow: hidden; text-overflow: ellipsis; flex: 1 1 auto; }
.ui-trigger .ui-placeholder { color: var(--ui-subtext); }
.ui-trigger .ui-icon { flex: none; font-family: 'Material Icons Outlined'; font-size: 1.15rem; line-height: 1; color: var(--ui-subtext);
  font-feature-settings: 'liga'; -webkit-font-feature-settings: 'liga'; }
.ui-trigger[aria-expanded="true"] { border-color: var(--ui-primary) !important; box-shadow: 0 0 0 3px var(--ui-primary-soft); }

/* Popover */
.ui-pop {
  position: absolute; z-index: 1000; min-width: 12rem; max-width: min(24rem, calc(100vw - 16px));
  background: var(--ui-bg); color: var(--ui-text); border: 1px solid var(--ui-border); border-radius: 0.75rem;
  box-shadow: var(--ui-shadow); padding: 0.375rem; font-size: 0.875rem; font-family: Inter, system-ui, sans-serif;
  animation: ui-pop-in 0.12s ease-out;
}
@keyframes ui-pop-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.ui-backdrop { position: fixed; inset: 0; z-index: 999; background: rgba(15, 23, 42, 0.35); }

/* Phones: pickers open as a bottom sheet with big touch targets. */
@media (max-width: 639px) {
  .ui-pop.ui-sheet {
    position: fixed; left: 0 !important; right: 0; bottom: 0; top: auto !important; width: 100% !important;
    max-width: none; max-height: 75vh; overflow: auto; border-radius: 1rem 1rem 0 0; padding: 0.75rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
    animation: ui-sheet-in 0.18s ease-out;
  }
  .ui-sheet .ui-option { min-height: 2.75rem; font-size: 1rem; }
  .ui-sheet .ui-day { height: 2.75rem; font-size: 1rem; }
  .ui-sheet .ui-sheet-grip { display: block; }
  /* iOS zooms into inputs smaller than 16px; keep everything at 16px on phones. */
  input, select, textarea, .ui-trigger { font-size: 16px !important; }
}
@keyframes ui-sheet-in { from { transform: translateY(100%); } to { transform: none; } }
.ui-sheet-grip { display: none; width: 2.5rem; height: 0.3rem; border-radius: 9999px; background: var(--ui-border); margin: 0 auto 0.6rem; }
.ui-sheet-title { display: none; font-weight: 600; padding: 0 0.25rem 0.5rem; }
@media (max-width: 639px) { .ui-sheet .ui-sheet-title { display: block; } }

/* Lists */
.ui-search {
  width: 100%; margin-bottom: 0.375rem; padding: 0.5rem 0.65rem; border: 1px solid var(--ui-border); border-radius: 0.5rem;
  background: var(--ui-bg); color: var(--ui-text); font-size: 0.875rem; outline: none;
}
.ui-search:focus { border-color: var(--ui-primary); box-shadow: 0 0 0 3px var(--ui-primary-soft); }
.ui-list { max-height: 18rem; overflow-y: auto; overscroll-behavior: contain; }
.ui-sheet .ui-list { max-height: 55vh; }
.ui-group { padding: 0.5rem 0.6rem 0.25rem; font-size: 0.7rem; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--ui-subtext); }
.ui-option {
  display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; width: 100%; padding: 0.45rem 0.6rem;
  border-radius: 0.5rem; cursor: pointer; text-align: left; background: none; border: 0; color: inherit; font: inherit;
}
.ui-option:hover, .ui-option.ui-active { background: var(--ui-bg-hover); }
.ui-option[aria-selected="true"] { color: var(--ui-primary); font-weight: 600; }
.ui-option[aria-selected="true"]::after { content: 'check'; font-family: 'Material Icons Outlined'; font-size: 1.1rem; font-feature-settings: 'liga'; }
.ui-option[aria-disabled="true"] { opacity: 0.4; cursor: not-allowed; }
.ui-option .ui-muted { color: var(--ui-subtext); }
.ui-empty { padding: 0.75rem; color: var(--ui-subtext); text-align: center; }

/* Calendar */
.ui-cal { width: 18.5rem; max-width: 100%; padding: 0.25rem; }
.ui-sheet .ui-cal { width: 100%; }
.ui-cal-head { display: flex; align-items: center; justify-content: space-between; padding: 0.25rem 0.25rem 0.5rem; }
.ui-cal-title { font-weight: 600; }
.ui-cal-nav { width: 2rem; height: 2rem; border-radius: 9999px; border: 0; background: none; color: var(--ui-text); cursor: pointer;
  font-family: 'Material Icons Outlined'; font-size: 1.25rem; font-feature-settings: 'liga'; }
.ui-cal-nav:hover { background: var(--ui-bg-hover); }
.ui-cal-nav:disabled { opacity: 0.3; cursor: default; }
.ui-cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.ui-dow { text-align: center; font-size: 0.7rem; font-weight: 600; color: var(--ui-subtext); padding: 0.25rem 0; }
.ui-day { height: 2.25rem; border-radius: 0.5rem; border: 0; background: none; color: var(--ui-text); cursor: pointer; font: inherit; }
.ui-day:hover:not(:disabled), .ui-day.ui-active { background: var(--ui-bg-hover); }
.ui-day.ui-outside { color: var(--ui-subtext); opacity: 0.55; }
.ui-day.ui-today { box-shadow: inset 0 0 0 1.5px var(--ui-primary); }
.ui-day[aria-selected="true"] { background: var(--ui-primary) !important; color: #fff; font-weight: 600; opacity: 1; }
.ui-day:disabled { opacity: 0.25; cursor: not-allowed; text-decoration: line-through; }
.ui-cal-foot { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; padding: 0.5rem 0.25rem 0.1rem; }
.ui-link { border: 0; background: none; color: var(--ui-primary); font-weight: 600; cursor: pointer; font: inherit; font-weight: 600; padding: 0.25rem 0.4rem; border-radius: 0.4rem; }
.ui-link:hover { background: var(--ui-primary-soft); }
.ui-link:disabled { opacity: 0.4; cursor: default; background: none; }
.ui-hint { font-size: 0.72rem; color: var(--ui-subtext); padding: 0.25rem 0.25rem 0; }

/* Date + time together */
.ui-dt { display: flex; gap: 0.5rem; }
.ui-dt .ui-list { max-height: 17rem; min-width: 7.5rem; border-left: 1px solid var(--ui-border); padding-left: 0.375rem; }
@media (max-width: 639px) { .ui-dt { flex-direction: column; } .ui-dt .ui-list { border-left: 0; padding-left: 0; max-height: 30vh; } }

/* Phones: tables marked .stack-sm turn each row into a labelled card (labels come from data-label). */
@media (max-width: 639px) {
  table.stack-sm, table.stack-sm tbody, table.stack-sm tr, table.stack-sm td { display: block; width: 100%; }
  table.stack-sm { min-width: 0 !important; }
  table.stack-sm thead { display: none; }
  table.stack-sm tbody { border: 0 !important; }
  table.stack-sm tr {
    border: 1px solid var(--ui-border) !important; border-radius: 0.75rem; padding: 0.5rem 0.75rem; margin-bottom: 0.6rem;
    background: var(--ui-bg);
  }
  table.stack-sm tr.even\:bg-gray-50:nth-child(even) { background: var(--ui-bg); }
  table.stack-sm td {
    display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; padding: 0.3rem 0 !important;
    text-align: right !important; white-space: normal !important; border: 0 !important; max-width: none !important;
  }
  table.stack-sm td[data-label]::before {
    content: attr(data-label); flex: none; text-align: left; font-size: 0.7rem; font-weight: 600; letter-spacing: 0.04em;
    text-transform: uppercase; color: var(--ui-subtext); padding-top: 0.15rem;
  }
  table.stack-sm td.stack-title { justify-content: flex-start; align-items: center; gap: 0; text-align: left !important; font-size: 1rem; font-weight: 600; }
  table.stack-sm td.hidden { display: none !important; }
  /* Unlabelled cells (row actions) sit together on the right. */
  table.stack-sm td:not([data-label]):not(.stack-title):not(.stack-full) { justify-content: flex-end; gap: 0.25rem; padding-top: 0.5rem !important; }
  table.stack-sm td.stack-full { display: block; text-align: left !important; }
  table.stack-sm td.stack-full::before { display: block; margin-bottom: 0.15rem; }
  table.stack-sm td:empty { display: none; }
  table.stack-sm td .line-clamp-3 { text-align: left; }
  table.stack-sm .ui-ctl, table.stack-sm select, table.stack-sm input:not([type=checkbox]) { flex: 1 1 auto; max-width: 70%; }
}
