/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  min-height: var(--target-min);
  min-width: var(--target-min);
  padding: 0 var(--space-lg);
  border: 2px solid transparent;
  border-radius: var(--radius-md);
  background: var(--color-primary);
  color: var(--color-on-primary);
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  user-select: none;
}

.btn:disabled,
.btn[aria-disabled='true'] { opacity: 0.5; cursor: not-allowed; }

.btn--primary { background: var(--color-primary); color: var(--color-on-primary); }
.btn--secondary { background: transparent; color: var(--color-foreground); border-color: var(--color-primary); }
.btn--danger { background: var(--color-destructive); color: var(--color-on-destructive); }
.btn--ghost { background: transparent; color: var(--color-foreground); }
.btn--lg {
  min-height: 64px;
  padding: 0 var(--space-xl);
  font-family: var(--font-display);
  font-weight: 400;
  font-size: 1.75rem;
  letter-spacing: 0.05em;
}
.btn--block { width: 100%; }

.btn:not(:disabled):hover { opacity: 0.88; }
.btn--secondary:not(:disabled):hover { background: var(--color-muted); opacity: 1; }
.btn--ghost:not(:disabled):hover { background: var(--color-muted); opacity: 1; }

@media (prefers-reduced-motion: no-preference) {
  .btn { transition: opacity var(--dur-fast) ease, background-color var(--dur-fast) ease; }
}

/* ---------- Card ---------- */
.card {
  background: var(--color-card);
  color: var(--color-card-foreground);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-lg);
  box-shadow: var(--shadow-sm);
}

.card__title { margin-bottom: var(--space-md); }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: 2px var(--space-sm);
  border-radius: var(--radius-sm);
  background: var(--color-muted);
  color: var(--color-muted-foreground);
  font-size: 0.8125rem;
  font-weight: 600;
  line-height: 1.4;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.badge--priority { background: var(--color-priority); color: var(--color-primary); }
.badge--called { background: var(--color-success); color: var(--color-on-primary); }
.badge--skipped { background: var(--color-muted); color: var(--color-muted-foreground); }

/* ---------- Form fields ---------- */
.field { display: flex; flex-direction: column; gap: var(--space-xs); }

.field__label { font-weight: 600; }

.field__input,
.field select,
.field textarea {
  min-height: var(--target-min);
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--color-muted-foreground);
  border-radius: var(--radius-md);
  background: var(--color-card);
  font-size: 1rem;
  width: 100%;
}

.field__input[aria-invalid='true'] { border-color: var(--color-destructive); }

.field__hint { color: var(--color-muted-foreground); font-size: 0.875rem; }

.field__error {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  color: var(--color-destructive);
  font-size: 0.9375rem;
  font-weight: 600;
}

.field__error:empty { display: none; }

@media (prefers-reduced-motion: no-preference) {
  .field__input { transition: border-color var(--dur-fast) ease; }
}

/* ---------- Switch (checkbox with role=switch) ---------- */
.switch {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  min-height: var(--target-min);
  cursor: pointer;
}

.switch input {
  appearance: none;
  position: relative;
  flex: none;
  width: 52px;
  height: 30px;
  margin: 0;
  border-radius: 15px;
  background: var(--color-muted-foreground);
  cursor: pointer;
}

.switch input::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--color-card);
}

.switch input:checked { background: var(--color-success); }
.switch input:checked::after { transform: translateX(22px); }
.switch input:disabled { opacity: 0.5; cursor: not-allowed; }

@media (prefers-reduced-motion: no-preference) {
  .switch input,
  .switch input::after { transition: transform var(--dur-fast) ease, background-color var(--dur-fast) ease; }
}

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }

.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-card);
}

.table th,
.table td {
  padding: var(--space-sm) var(--space-md);
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}

.table th {
  font-size: 0.8125rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-muted-foreground);
}

.table tbody tr:hover { background: var(--color-muted); }

/* ---------- Toast ---------- */
.toast-region {
  position: fixed;
  left: var(--space-md);
  right: var(--space-md);
  bottom: calc(var(--nav-height) + var(--space-md));
  z-index: 200;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-sm);
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  min-height: var(--target-min);
  max-width: 480px;
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-md);
  background: var(--color-primary);
  color: var(--color-on-primary);
  box-shadow: var(--shadow-lg);
  font-weight: 600;
  pointer-events: auto;
}

.toast__text { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }
.toast__action { flex: none; min-height: var(--target-min); padding: 0 var(--space-sm); border: 0; background: transparent; color: inherit; font: inherit; font-weight: 700; text-decoration: underline; text-underline-offset: 3px; cursor: pointer; border-radius: var(--radius-md); }
.toast__action:hover { text-decoration-thickness: 2px; }
.toast__action:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
.toast__close { flex: none; display: inline-flex; align-items: center; justify-content: center; width: var(--target-min); height: var(--target-min); margin-right: calc(var(--space-sm) * -1); padding: 0; border: 0; border-radius: var(--radius-md); background: transparent; color: inherit; cursor: pointer; }
.toast__close .icon { width: 1.25rem; height: 1.25rem; }
.toast__close:hover { background: rgb(0 0 0 / 0.15); }
.toast__close:focus-visible { outline: 2px solid currentColor; outline-offset: -2px; }

.toast--success { background: var(--color-success); }
.toast--error { background: var(--color-destructive); color: var(--color-on-destructive); }

@media (prefers-reduced-motion: no-preference) {
  .toast { animation: toast-in var(--dur) ease; }
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

@media (min-width: 1024px) {
  .toast-region { bottom: var(--space-lg); }
}

/* ---------- Modal ---------- */
dialog.modal {
  width: min(500px, 90vw);
  padding: var(--space-xl);
  border: 0;
  border-radius: var(--radius-lg);
  background: var(--color-card);
  color: var(--color-card-foreground);
  box-shadow: var(--shadow-xl);
}

dialog.modal::backdrop { background: var(--color-overlay); }

.modal__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--space-sm);
  margin-top: var(--space-lg);
}

/* ---------- Ticket ---------- */
.ticket {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  min-height: var(--target-min);
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-card);
}

.ticket__number { font-family: var(--font-display); font-size: 2rem; line-height: 1; min-width: 3ch; }
.ticket__main { display: flex; flex-direction: column; min-width: 0; }
.ticket__line { display: flex; align-items: center; gap: var(--space-sm); font-weight: 600; }
.ticket__club { color: var(--color-muted-foreground); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ticket__flag { width: 28px; height: auto; aspect-ratio: 4 / 3; border-radius: 3px; object-fit: cover; box-shadow: 0 0 0 1px var(--color-border); }
.ticket--priority { border-left: 6px solid var(--color-priority); }

/* ---------- KPI ---------- */
.kpi { display: flex; flex-direction: column; gap: var(--space-xs); padding: var(--space-md); border: 1px solid var(--color-border); border-radius: var(--radius-lg); background: var(--color-card); }
.kpi__value { font-family: var(--font-display); font-size: 3rem; line-height: 1; }
.kpi__label { color: var(--color-muted-foreground); font-size: 0.875rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; }
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--space-md); }

/* ---------- Skeleton ---------- */
.skeleton {
  display: block;
  min-height: 1em;
  border-radius: var(--radius-sm);
  background: var(--color-muted);
}

@media (prefers-reduced-motion: no-preference) {
  .skeleton { animation: skeleton-pulse 1.4s ease-in-out infinite; }
}

@keyframes skeleton-pulse {
  50% { opacity: 0.5; }
}

/* ---------- Connection status ---------- */
.status-pill { display: inline-flex; align-items: center; gap: var(--space-xs); font-size: 0.875rem; font-weight: 600; }
.status-pill::before { content: ''; width: 10px; height: 10px; border-radius: 50%; background: var(--color-success); }
.status-pill[data-state='reconnecting']::before { background: var(--color-priority); }

/* ---------- App shell ---------- */
.app-shell { min-height: 100vh; padding-bottom: calc(var(--nav-height) + var(--space-md)); }

.app-shell__header {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  min-height: var(--nav-height);
  padding: var(--space-sm) var(--space-md);
  background: var(--color-primary);
  color: var(--color-on-primary);
}

.app-shell__brand { display: inline-flex; align-items: center; gap: var(--space-sm); min-height: var(--target-min); color: inherit; text-decoration: none; font-family: var(--font-display); font-size: 1.75rem; letter-spacing: 0.05em; margin-right: auto; min-width: 0; overflow: hidden; }
.app-shell__logo { height: 28px; width: auto; }
.app-shell__header select { min-width: 0; flex: 0 1 auto; text-overflow: ellipsis; }
.app-shell__brand span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 899px) {
  .app-shell__brand span { display: none; }
}
@media (max-width: 599px) {
  .app-shell__logout-text { display: none; }
}

.app-shell__header select {
  min-height: var(--target-min);
  max-width: 50vw;
  padding: 0 var(--space-sm);
  border-radius: var(--radius-md);
  border: 1px solid var(--color-muted-foreground);
  background: var(--color-secondary);
  color: var(--color-on-secondary);
}

.app-shell__header :focus-visible,
.app-shell__nav--side :focus-visible { outline-color: var(--color-on-primary); }

.app-shell__header .btn--ghost { color: var(--color-on-primary); }
.app-shell__header .btn--ghost:not(:disabled):hover { background: var(--color-secondary); }

.app-shell__nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  display: flex;
  height: var(--nav-height);
  background: var(--color-card);
  border-top: 1px solid var(--color-border);
}

.app-shell__link {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: var(--target-min);
  min-height: var(--target-min);
  color: var(--color-muted-foreground);
  font-size: 0.75rem;
  font-weight: 600;
  text-decoration: none;
}

.app-shell__link[aria-current='page'] { color: var(--color-foreground); box-shadow: inset 0 3px 0 var(--color-primary); }

.app-shell__main { padding: var(--space-md); max-width: min(2400px, 100%); margin: 0 auto; }

@media (min-width: 1024px) {
  /* Header and side nav are both fixed, so they always move together (no gap or overlap while scrolling or rubber-banding). */
  html { scroll-padding-top: var(--nav-height); } /* focused or linked elements never land under the fixed header */
  .app-shell { padding-bottom: 0; padding-top: var(--nav-height); padding-left: var(--sidebar-width); }
  .app-shell__header { position: fixed; top: 0; left: 0; right: 0; height: var(--nav-height); padding-left: calc(var(--space-md) + var(--space-sm)); }
  .app-shell__nav {
    top: var(--nav-height);
    right: auto;
    bottom: 0;
    width: var(--sidebar-width);
    height: auto;
    overflow-y: auto;
    flex-direction: column;
    padding: var(--space-md) var(--space-sm);
    border-top: 0;
    border-right: 1px solid var(--color-border);
  }
  .app-shell__link {
    flex: none;
    flex-direction: row;
    justify-content: flex-start;
    gap: var(--space-sm);
    padding: 0 var(--space-md);
    border-radius: var(--radius-md);
    font-size: 1rem;
  }
  .app-shell__link[aria-current='page'] { color: var(--color-foreground); background: var(--color-muted); box-shadow: inset 3px 0 0 var(--color-primary); } /* same marker as the station sub-items */
  .app-shell__main { padding: var(--space-xl); }
}

@media (min-width: 1600px) {
  .app-shell__main { padding: var(--space-xl) var(--space-3xl); }
}

/* ---------- Card grid ---------- */
.card-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-lg); align-items: stretch; } /* all cards in a row share the tallest height */
.card-grid > * { min-width: 0; }
@media (min-width: 768px) {
  .card-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: var(--space-md); }
.login__card { width: min(420px, 100%); }
.login__logo { display: flex; flex-direction: column; align-items: center; gap: var(--space-sm); margin-bottom: var(--space-lg); text-align: center; }
.login__logo img { height: 48px; width: auto; }
.login__brand { font-family: var(--font-display); font-size: clamp(1.5rem, 6vw, 2rem); letter-spacing: 0.05em; text-wrap: balance; }

/* Station sub-items under "Stations" (side nav only) */
.app-shell__sub { display: none; list-style: none; margin: 0; padding: 0; }
@media (min-width: 1024px) {
  .app-shell__sub { display: block; }
  .app-shell__sublink {
    display: flex;
    align-items: center;
    min-height: var(--target-min);
    padding: 0 var(--space-md) 0 calc(var(--space-md) + 1.5rem);
    border-radius: var(--radius-md);
    color: var(--color-muted-foreground);
    font-size: 0.9375rem;
    font-weight: 600;
    text-decoration: none;
    overflow-wrap: anywhere;
  }
  .app-shell__sublink:hover { color: var(--color-foreground); background: var(--color-muted); }
  .app-shell__sublink[aria-current='true'] { color: var(--color-foreground); background: var(--color-muted); box-shadow: inset 3px 0 0 var(--color-primary); }
}

/* More panel for bottom nav with > 5 entries */
.app-shell__more { background: transparent; border: 0; font-family: inherit; }
.app-shell__link--extra { display: none; }

@media (max-width: 1023.98px) {
  .app-shell__nav[data-open] .app-shell__link--extra {
    display: flex;
    position: absolute;
    right: var(--space-sm);
    min-width: 160px;
    flex: none;
    flex-direction: row;
    justify-content: flex-start;
    gap: var(--space-sm);
    padding: 0 var(--space-md);
    background: var(--color-card);
    border: 1px solid var(--color-border);
    box-shadow: var(--shadow-md);
    border-radius: var(--radius-md);
    font-size: 1rem;
  }
  .app-shell__nav[data-open] .app-shell__link--extra:nth-last-child(2) { bottom: calc(var(--nav-height) + var(--space-sm)); }
  .app-shell__nav[data-open] .app-shell__link--extra:nth-last-child(3) { bottom: calc(var(--nav-height) + var(--space-sm) + 44px); }
  .app-shell__nav[data-open] .app-shell__link--extra:nth-last-child(4) { bottom: calc(var(--nav-height) + var(--space-sm) + 88px); }
}

@media (min-width: 1024px) {
  .app-shell__link--extra { display: flex; }
  .app-shell__more { display: none; }
}

/* ---------- Drag & drop (public/js/dnd.js) ---------- */
.dnd-handle { touch-action: none; cursor: grab; }
.dnd-handle--press { touch-action: pan-y; cursor: grab; -webkit-touch-callout: none; }
.dnd-item img, .dnd-item a { -webkit-user-drag: none; user-select: none; -webkit-user-select: none; }
.dnd-active, .dnd-active * { cursor: grabbing !important; user-select: none; -webkit-user-select: none; }
.dnd-source { opacity: 0.4; }
.dnd-ghost { position: fixed; top: 0; left: 0; z-index: 300; margin: 0; box-shadow: var(--shadow-xl); opacity: 0.95; pointer-events: none; will-change: transform; }
.dnd-indicator { height: 0; border-top: 4px solid var(--color-ring); border-radius: 2px; margin: 2px 0; pointer-events: none; }
@media (prefers-reduced-motion: no-preference) {
  .dnd-ghost { transition: box-shadow var(--dur-fast); }
}
