/*
 * Shared components used by every page: tables, toolbars, results, empty
 * states, forms, alerts, toasts, and the shared narrow/touch and reduced-motion
 * floors. Tokens live in tokens.css. Shell layout lives in app.css.
 */

/* ---------- Tables ---------- */

.table-card {
  overflow: hidden;
}

/* A wide table scrolls inside its card; the page itself never scrolls
   sideways. */
.table-scroll {
  overflow-x: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border-strong) transparent;
}

/* Full lists share the available desktop height with their controls and pager.
   Short windows and touch layouts keep natural document scrolling.
   :has(.table-card) sizes the shell around the table; it stays with this file. */
@media (min-width: 1024px) and (min-height: 700px) {
  .app-layout:has(.table-card) {
    height: 100dvh;
  }

  .app-main:has(.table-card),
  .collection-layout:has(.table-card) {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .app-main:has(.table-card) {
    padding-bottom: var(--space-6);
  }

  .app-main:has(.table-card) > *,
  .collection-layout:has(.table-card) > * {
    flex-shrink: 0;
  }

  .collection-layout:has(.table-card) {
    flex: 1 1 auto;
  }

  .app-main .table-card {
    display: flex;
    flex-direction: column;
    flex: 0 1 auto;
    min-height: 12rem;
  }

  .table-card > .table-scroll {
    min-height: 0;
  }

  .table-card + nav.table-toolbar {
    margin-top: var(--space-4);
    margin-bottom: 0;
  }
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-base);
}

.data-table th {
  padding: var(--space-3) var(--space-4);
  border-bottom: var(--border-width) solid var(--color-border);
  background: var(--color-surface-subtle);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-align: left;
  white-space: nowrap;
}

.data-table--sticky thead th {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
}

.data-table td {
  padding: var(--space-3) var(--space-4);
  border-bottom: var(--border-width) solid var(--color-border);
  vertical-align: top;
}

.data-table tbody tr:last-child td {
  border-bottom: 0;
}

.data-table tbody tr:hover td {
  background: var(--color-surface-subtle);
}

.data-table tbody tr:focus-within td {
  background: var(--color-action-subtle);
}

/* Failed rows get a semantic edge bar rather than a tinted row:
   the row stays readable, the signal stays unmissable. */
.data-table .row-danger td:first-child {
  box-shadow: inset 2px 0 0 var(--color-danger-text);
}

.table-link {
  color: var(--color-text);
  font-weight: 600;
  text-decoration: none;
}

.table-link:hover {
  color: var(--color-action-text);
  text-decoration: underline;
}

/* A second line under the primary cell value: the context that would
   otherwise need its own column. */
.cell-sub {
  display: block;
  margin-top: var(--space-1);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.cell-muted {
  color: var(--color-text-muted);
}

/* A link on the sub-line is a side route, not the cell's subject; it takes
   the action colour without the primary link's weight. */
.cell-sub .table-link {
  color: var(--color-action-text);
  font-weight: 500;
}

.cell-actions {
  text-align: right;
  white-space: nowrap;
}

/* Operational identifiers stay in the one family:
   tabular figures and letter-spacing do the work a mono face would. */
.code {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

/* ---------- Table toolbar ---------- */

.table-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--space-3);
  padding: var(--space-4);
  margin-bottom: var(--space-4);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface);
}

.toolbar-field {
  min-width: 11rem;
}

.toolbar-actions {
  display: flex;
  gap: var(--space-2);
  margin-left: auto;
}

/* Small collection filters use available space for search, not empty padding. */
.collection-filters {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem) auto;
  gap: var(--space-4);
  padding: var(--space-5);
}
.collection-filters .toolbar-field { min-width: 0; }
.collection-filters .toolbar-actions { margin: 0; align-items: center; }
.collection-filters .htmx-indicator { display: none; }
.collection-filters .htmx-indicator.htmx-request { display: inline; }
.collection-filters--search-only { grid-template-columns: minmax(0, 1fr) auto; }
.collection-filters--two-selects {
  grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem) minmax(12rem, 18rem) auto;
}
@media (max-width: 960px) and (min-width: 721px) {
  .collection-filters--two-selects {
    grid-template-columns: minmax(12rem, 1fr) minmax(12rem, 1fr) auto;
  }
  .collection-filters--two-selects .collection-search { grid-column: 1 / -1; }
}
@media (max-width: 720px) {
  .collection-filters,
  .collection-filters--two-selects { grid-template-columns: minmax(0, 1fr) auto; }
  .collection-filters .collection-search { grid-column: 1 / -1; }
  .collection-filters--search-only .collection-search { grid-column: auto; }
}
@media (max-width: 440px) {
  .collection-filters,
  .collection-filters--search-only,
  .collection-filters--two-selects { grid-template-columns: minmax(0, 1fr); }
}

.collection-note { font-size: var(--text-label); color: var(--color-text-secondary); line-height: 1.6; margin: 0 0 var(--space-4); }
.collection-count { font-size: var(--text-label); color: var(--color-text-secondary); font-weight: 600; }
.collection-table { min-width: 76rem; }
.collection-table .collection-lane { min-width: 13rem; }
.collection-table time { white-space: nowrap; }

/* ---------- Results regions ---------- */

/* The results dim while the next set is on the way, and a bar runs across the
   top so the wait reads as progress rather than as a page gone faint. The
   controls above stay live so a second change can be made straight away. */
.results-region {
  position: relative;
  transition: opacity var(--duration-base) var(--ease-out);
}

.results-region.htmx-request {
  opacity: 0.5;
}

.results-region::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 30%;
  height: 2px;
  border-radius: var(--radius-sm);
  background: var(--color-action);
  opacity: 0;
}

.results-region.htmx-request::before {
  opacity: 1;
  animation: results-progress 1.2s linear infinite;
}

@keyframes results-progress {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(433%);
  }
}

.sort-button {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  font-weight: inherit;
  letter-spacing: inherit;
  text-align: left;
  text-transform: inherit;
  text-decoration: none;
  cursor: pointer;
}

.sort-button:hover,
.sort-button.is-active {
  color: var(--color-text);
}

.sort-button--numeric {
  justify-content: flex-end;
}

.request-id {
  min-width: 0;
  overflow-wrap: anywhere;
  white-space: normal;
}

.request-copy[hidden] {
  display: none;
}

.lookup-time-details {
  margin-top: var(--space-2);
  font-size: var(--text-label);
  text-align: left;
  white-space: normal;
}

.lookup-time-details summary {
  min-height: var(--touch-target-min);
  padding: var(--space-2) 0;
  color: var(--color-action-text);
  cursor: pointer;
}

.lookup-time-list {
  margin: 0;
  padding: var(--space-3);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-md);
}

.lookup-time-list dt {
  color: var(--color-text-secondary);
  font-weight: 600;
}

.lookup-time-list dd {
  margin: var(--space-1) 0 var(--space-2);
  color: var(--color-text);
}

/* ---------- Empty & placeholder states ---------- */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-10) var(--space-6);
  margin-top: var(--space-4);
  text-align: center;
}

.empty-state--inline {
  padding: var(--space-6) var(--space-5);
  margin-top: 0;
}

.empty-state-icon {
  width: 2.5rem;
  height: 2.5rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  color: var(--color-border-strong);
}

.empty-state-title {
  margin: var(--space-2) 0 0;
  font-size: var(--text-md);
  font-weight: 600;
  color: var(--color-text-secondary);
}

.empty-state-body {
  max-width: 30rem;
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--text-label);
  line-height: 1.6;
}

.empty-state .btn {
  margin-top: var(--space-4);
}

/* ---------- Forms & buttons ---------- */

label {
  display: block;
  margin-bottom: var(--space-1);
  font-size: var(--text-label);
  font-weight: 500;
  color: var(--color-text-secondary);
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="search"],
input[type="date"],
select,
textarea {
  width: 100%;
  min-height: var(--control-height-md);
  padding: 0 var(--space-3);
  border: var(--border-width) solid var(--color-border-strong);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: var(--text-base);
}

input[type="date"] {
  color-scheme: inherit;
}

input::placeholder,
textarea::placeholder {
  color: var(--color-text-muted);
  opacity: 1;
}

input:hover,
select:hover,
textarea:hover {
  border-color: var(--color-text-muted);
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  border-color: var(--color-action);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--control-height-md);
  padding: 0 var(--space-4);
  border: 0;
  border-radius: var(--radius-md);
  font-size: var(--text-label);
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition:
    background var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out);
}

.btn[aria-busy="true"] {
  cursor: progress;
  opacity: 0.65;
}

.btn-primary {
  background: var(--color-action-active);
  color: var(--color-on-action);
}

.btn-primary:hover {
  background: var(--color-action-hover);
}

.btn-ghost {
  border: var(--border-width) solid var(--color-border-strong);
  background: none;
  color: var(--color-text-secondary);
  text-decoration: none;
}

.btn-ghost:hover {
  border-color: var(--color-action);
  color: var(--color-action-text);
}

.btn-block {
  width: 100%;
}

.form-actions {
  display: flex;
  gap: var(--space-3);
  align-items: center;
}

@media (max-width: 40rem) {
  .form-actions {
    flex-direction: column-reverse;
    align-items: stretch;
  }
}

/* ---------- Alerts ---------- */

/* One message component. Tone carries the meaning, the icon repeats it for
   anyone who cannot see the colour, and the same markup serves both an inline
   alert and a toast. */
.alert {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  margin-bottom: var(--space-4);
  border: var(--border-width) solid;
  border-radius: var(--radius-md);
  font-size: var(--text-label);
}

.alert .icon {
  width: var(--icon-sm);
  height: var(--icon-sm);
  margin-top: 0.15em;
}

.alert-body {
  min-width: 0;
  flex: 1;
}

.alert-detail {
  opacity: 0.85;
}

.alert--error {
  border-color: var(--color-danger-border);
  background: var(--color-danger-bg);
  color: var(--color-danger-text);
}

.alert--warning {
  border-color: var(--color-warning-border);
  background: var(--color-warning-bg);
  color: var(--color-warning-text);
}

.alert--success {
  border-color: var(--color-success-border);
  background: var(--color-success-bg);
  color: var(--color-success-text);
}

.alert--info {
  border-color: var(--color-action-subtle-hover);
  background: var(--color-action-subtle);
  color: var(--color-action-text);
}

/* ---------- Toasts ---------- */

/* Outcomes with nothing on screen to attach to. The region is always in the
   DOM so its live announcements are heard; it only takes space when full. */
.toast-region {
  position: fixed;
  right: var(--space-4);
  bottom: var(--space-4);
  z-index: var(--z-overlay);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  width: min(24rem, calc(100vw - var(--space-8)));
  pointer-events: none;
}

.toast-region .alert {
  margin: 0;
  pointer-events: auto;
  background: var(--color-surface);
  box-shadow: var(--shadow-lg);
  animation: toast-in var(--duration-slow) var(--ease-out);
}

/* The tinted fill reads as a wash at this size; the tone lives in a solid edge
   instead, so a toast stays legible over whatever it covers. */
.toast-region .alert--error {
  border-left: 3px solid var(--color-danger-text);
}

.toast-region .alert--warning {
  border-left: 3px solid var(--color-warning-text);
}

.toast-region .alert--success {
  border-left: 3px solid var(--color-success-text);
}

.toast-region .alert--info {
  border-left: 3px solid var(--color-action);
}

.toast-dismiss {
  width: var(--icon-lg);
  height: var(--icon-lg);
  margin: -0.125rem -0.25rem 0 0;
  color: var(--color-text-muted);
}

.toast-dismiss:hover {
  background: var(--color-surface-sunken);
  color: var(--color-text);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(0.5rem);
  }
}

/* ---------- Narrow screens & touch ----------
 * One query on purpose. It floors shell chrome (topbar, main, page header)
 * as well as toolbars and buttons; both stylesheets load on every page. */

@media (max-width: 767px) {
  .app-topbar {
    gap: var(--space-2);
    padding-inline: var(--space-3);
  }

  .app-main {
    padding: var(--space-4) var(--space-3) var(--space-8);
  }

  .page-header {
    gap: var(--space-2);
    margin-bottom: var(--space-4);
  }

  .page-title {
    font-size: var(--text-mobile-title);
  }

  .page-lede {
    margin-bottom: var(--space-4);
  }

  .card-pad {
    padding: var(--space-4);
  }

  .stat-card {
    padding: var(--space-3) var(--space-4);
  }

  .stat-value {
    font-size: var(--text-mobile-title);
  }

  .table-toolbar {
    gap: var(--space-3);
    padding: var(--space-3);
  }

  .toolbar-field {
    flex: 1 1 100%;
    min-width: 0;
  }

  .toolbar-actions {
    width: 100%;
    margin-left: 0;
    justify-content: flex-end;
  }

  .session-dropdown {
    width: min(20rem, calc(100vw - 2 * var(--space-3)));
  }
}

@media (pointer: coarse) {
  .icon-button,
  .session-menu summary,
  .nav-item,
  .nav-group-label,
  .session-dropdown > button,
  .session-dropdown form button,
  .session-dropdown a,
  .sort-button,
  .btn,
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="search"],
  input[type="date"],
  select,
  textarea {
    min-height: var(--touch-target-min);
  }
}

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  * {
    transition-duration: 60ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
  }
  /* transforms intentionally untouched: drawer/dropdown positioning uses them */

  /* A one-shot sweep would leave the bar parked off-screen: hold it still and
     full-width instead, so "working" is still visible without motion. */
  .results-region.htmx-request::before {
    animation: none;
    width: 100%;
  }
}
