/* Theme: follows the OS by default; the account menu can pin light or dark via <html data-theme>. */
:root {
  color-scheme: light dark;
  --bg: light-dark(#fafafa, #18181b);
  --surface: light-dark(#f1f1f3, #1f1f23);
  --text: light-dark(#18181b, #f4f4f5);
  --muted: light-dark(#71717a, #a1a1aa);
  --border: light-dark(#e4e4e7, #2e2e33);
  --accent: light-dark(#18181b, #f4f4f5);
  --row-hover: light-dark(#efeff1, #232328);
  --scrollbar: light-dark(#d4d4d8, #3f3f46);
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }

/* Slim, borderless scrollbars everywhere (page, table box, tab strip): no track, just a light thumb. */
* { scrollbar-width: thin; scrollbar-color: var(--scrollbar) transparent; }
/* Fallback for browsers without scrollbar-color (older Safari). */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track,
::-webkit-scrollbar-corner { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--scrollbar); background-clip: padding-box; border: 2px solid transparent; border-radius: 999px; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

a { color: inherit; }

h1 { font-size: 20px; font-weight: 600; margin: 0 0 20px; }
h2 { font-size: 15px; font-weight: 600; margin: 32px 0 12px; }

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

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  /* Stays at the top on every page while scrolling. */
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
}

.brand { display: flex; align-items: center; gap: 16px; min-width: 0; }
.topbar-right { display: flex; align-items: center; gap: 20px; }
.primary-nav a { color: var(--muted); text-decoration: none; font-weight: 500; }
/* Desktop: undo the UA popover styles so the nav sits inline; the hamburger is hidden. */
@media (min-width: 768px) {
  .avatar.menu-toggle { display: none; }
  .primary-nav { display: flex; gap: 16px; position: static; inset: auto; margin: 0; padding: 0; border: 0; background: none; color: inherit; overflow: visible; }
}
/* Phones: the nav is a popover card under the hamburger, like the account menu. */
@media (max-width: 767px) {
  .primary-nav:popover-open {
    display: flex;
    flex-direction: column;
    position: fixed;
    inset: 52px 24px auto auto;
    margin: 0;
    min-width: 200px;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    box-shadow: 0 8px 24px rgb(0 0 0 / 12%);
  }
  .primary-nav a { padding: 8px 10px; border-radius: 6px; }
  .primary-nav a:hover { background: var(--row-hover); }
}
.primary-nav a:hover,
.primary-nav a[aria-current] { color: var(--text); }
.logo { display: flex; color: var(--text); }
.logo svg { width: 22px; height: 22px; }

.breadcrumbs {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  margin: 0;
  padding: 0;
}

.breadcrumbs li + li::before {
  content: "/";
  color: var(--border);
  margin: 0 8px;
}

.breadcrumbs a { color: var(--muted); text-decoration: none; }
.breadcrumbs a:hover { color: var(--text); }
.breadcrumbs [aria-current] { font-weight: 500; }

/* Avatar button (Lucide circle-user-round) + account popover card */
.avatar {
  display: flex;
  padding: 4px;
  border: 0;
  border-radius: 999px;
  background: none;
  color: var(--muted);
  cursor: pointer;
}
.avatar:hover,
.avatar:focus-visible { color: var(--text); background: var(--row-hover); }
.avatar svg { width: 24px; height: 24px; }

.account-menu {
  /* Popovers render in the top layer; pin it under the avatar in the top-right corner. */
  position: fixed;
  inset: 52px 24px auto auto;
  margin: 0;
  min-width: 240px;
  max-width: 320px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  box-shadow: 0 8px 24px rgb(0 0 0 / 12%);
}
.account-menu p { margin: 0; }
.account-menu .muted { font-size: 12px; }
.account-email { font-weight: 500; overflow: hidden; text-overflow: ellipsis; }
.account-menu form { margin: 12px 0 0; padding-top: 12px; border-top: 1px solid var(--border); }
.account-menu .btn { width: 100%; justify-content: center; }

.theme-label { margin-top: 12px !important; }
.theme-switch {
  display: grid;
  grid-template-columns: repeat(3, auto);
  gap: 2px;
  margin-top: 6px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}
.theme-switch button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 5px 12px;
  border: 0;
  border-radius: 4px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}
.theme-switch button:hover { color: var(--text); }
.theme-switch button[aria-pressed="true"] { background: var(--bg); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / 10%); }
.theme-switch svg { width: 14px; height: 14px; }


/* About 75% of the window on large screens, never narrower than 1080px (laptops unchanged). */
.container { max-width: max(1080px, 75vw); margin: 0 auto; padding: 32px 24px; }

/* Tables */
/* position: relative keeps an absolute child (e.g. a .sr-only header) inside the scroll box, so a wide table can't
   make the whole page scroll sideways on a phone. */
.table-wrap { position: relative; overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }

/* separate + 0 spacing looks identical to collapse, but keeps header borders on a sticky <th>. */
.table { width: 100%; border-collapse: separate; border-spacing: 0; }
.table th,
.table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table th { font-weight: 500; color: var(--muted); background: var(--surface); font-size: 13px; }
.table tbody tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }

/* Overlay scrollbar (Alpine "overlayScroll" in static/js/app.js): once Alpine runs, the native bar is hidden so the
   table keeps its full width, and a thin thumb floats above the table instead. */
.scroll-area { position: relative; }
.has-overlay-scroll > .table-wrap { scrollbar-width: none; }
.has-overlay-scroll > .table-wrap::-webkit-scrollbar { display: none; }
.scroll-thumb { position: absolute; z-index: 2; border-radius: 999px; background: var(--scrollbar); cursor: pointer; touch-action: none; }
.scroll-thumb-y { right: 3px; width: 6px; }
.scroll-thumb-x { bottom: 3px; height: 6px; }
.scroll-thumb:hover,
.scroll-thumb.dragging { background: var(--muted); }

/* Pages with <main class="fill"> (the list pages) fit the window on desktop: the page doesn't scroll,
   the table scrolls inside its own box under a sticky header, and pagination stays visible below it.
   The table's .scroll-area may sit directly in main or inside the htmx #results wrapper.
   Phones keep normal page scrolling; nested scroll areas are awkward on touch. */
@media (min-width: 768px) {
  body:has(> main.fill) { display: flex; flex-direction: column; height: 100dvh; overflow: hidden; }
  body:has(> main.fill) .topbar { flex: none; }
  main.fill { flex: 1; min-height: 0; width: 100%; display: flex; flex-direction: column; }
  main.fill > *,
  main.fill #results > * { flex: none; }
  main.fill #results { flex: 1; min-height: 0; display: flex; flex-direction: column; }
  main.fill > .scroll-area,
  main.fill #results > .scroll-area { flex: 0 1 auto; min-height: 0; display: flex; flex-direction: column; }
  main.fill .scroll-area > .table-wrap { flex: 0 1 auto; min-height: 0; overflow: auto; }
  main.fill .table thead th { position: sticky; top: 0; z-index: 2; } /* above .row-link .btn (z-index 1) */
}

/* Whole row clickable via the first cell's link */
.row-link { position: relative; cursor: pointer; }
.row-link:hover { background: var(--row-hover); }
.row-link a:not(.btn) { font-weight: 500; text-decoration: none; }
.row-link a:not(.btn)::after { content: ""; position: absolute; inset: 0; }
.row-link .btn { position: relative; z-index: 1; } /* row buttons stay clickable above the row link */
.row-link a:focus-visible { outline: none; }
.row-link:has(a:focus-visible) { outline: 2px solid var(--accent); outline-offset: -2px; }

/* Search + filter bar */
.filters { display: flex; gap: 8px; flex: 1; min-width: 0; }
.filters input,
.filters select {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.filters input { flex: 1; min-width: 0; max-width: 360px; }
.filters input:focus,
.filters select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.pagination { display: flex; align-items: center; justify-content: flex-end; gap: 16px; margin-top: 16px; }

.empty { color: var(--muted); padding: 32px; text-align: center; border: 1px dashed var(--border); border-radius: 8px; }
.empty p { margin: 0; }
.empty .btn { margin-top: 12px; }

/* Status badge */
.badge {
  display: inline-block;
  padding: 1px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  color: var(--muted);
}
.badge[data-status="IN_TRANSIT"] { color: #d97706; border-color: currentColor; }
.badge[data-status="BOOKED"],
.badge[data-status="DELIVERED"] { color: #16a34a; border-color: currentColor; }
.badge[data-status="CANCELLED"] { color: #dc2626; border-color: currentColor; }

/* Detail page */
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.page-head h1 { margin: 0; }

/* The baseline is an inset shadow (not a border) so tab underlines sit on it without overflowing
   the strip, which would otherwise add a vertical scrollbar. */
.tabs { display: flex; gap: 4px; box-shadow: inset 0 -1px 0 var(--border); margin-bottom: 24px; overflow-x: auto; }
.tabs a {
  padding: 8px 12px;
  color: var(--muted);
  text-decoration: none;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.tabs a:hover { color: var(--text); }
.tabs a[aria-current] { color: var(--text); border-bottom-color: var(--accent); font-weight: 500; }

.facts {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 20px 32px;
  margin: 0;
}
.facts dt { color: var(--muted); font-size: 13px; }
.facts dd { margin: 2px 0 0; }

.description { color: var(--muted); margin-top: 20px; max-width: 70ch; }

/* Sign-in: a small centred card. */
.sign-in { max-width: 360px; margin: 12vh auto 0; padding: 32px; border: 1px solid var(--border); border-radius: 12px; text-align: center; }
.sign-in .logo { justify-content: center; margin-bottom: 16px; }
.sign-in .logo svg { width: 32px; height: 32px; }
.sign-in h1 { margin-bottom: 4px; }
.sign-in p { margin: 0 0 24px; }
.sign-in .btn { width: 100%; justify-content: center; padding: 8px 12px; }

@media (max-width: 640px) {
  .topbar, .container { padding-left: 16px; padding-right: 16px; }
  .account-menu { right: 16px; }
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
}
.btn:hover { background: var(--row-hover); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--bg); }
.btn-primary:hover { background: var(--accent); opacity: .9; }
.btn-danger { color: #dc2626; border-color: #dc2626; }
.btn-danger:hover { background: rgb(220 38 38 / 8%); }
.btn-danger.btn-primary { background: #dc2626; border-color: #dc2626; color: #fff; }
.btn-sm { padding: 3px 8px; font-size: 13px; }
.actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.actions form { margin: 0; }
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.toolbar h1,
.toolbar h2 { margin: 0; }
.toolbar > .btn:only-child { margin-left: auto; }
/* A section title row (e.g. "Pallets" + its action): h2's spacing moves to the row so the button lines up. */
.section-head { margin: 32px 0 12px; min-height: 30px; }
.automation-paused { margin: 0 0 12px; }

/* Forms */
.narrow { max-width: 560px; margin: 0 auto; }
.form { display: grid; gap: 16px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; } /* an error under one box doesn't stretch its neighbours */
.form-row-4 { grid-template-columns: repeat(4, 1fr); } /* e.g. length × width × height + unit; phones stack (below) */
.form-actions { display: flex; gap: 8px; margin-top: 8px; }

/* "Request quotes" broker list: a narrow checkbox column; names are the boxes' labels. */
.broker-picker .check-col { width: 1%; padding-right: 0; }
.broker-picker input[type="checkbox"] { display: block; margin: 0; accent-color: var(--accent); }
.broker-picker input[type="checkbox"][hidden] { display: none; }
.broker-picker label { cursor: pointer; }

/* Order form destinations: one bordered box per destination, numbered by a CSS counter so the numbers
   close up when a row is removed (hidden rows don't count). */
.destinations { display: grid; gap: 12px; }
.destinations .alert { margin: 0; }
.destination-rows { display: grid; gap: 12px; counter-reset: destination; }
.destination-row { position: relative; display: grid; gap: 16px; margin: 0; padding: 12px 16px 16px; border: 1px solid var(--border); border-radius: 8px; counter-increment: destination; }
.destination-row legend { padding: 0 4px; font-weight: 600; }
.destination-row legend::after { content: " " counter(destination); }
.destination-fields { display: grid; gap: 16px; min-width: 0; margin: 0; padding: 0; border: 0; }
/* On the top border, opposite the legend (and like it, covering the line), clear of the first field.
   A fieldset places it from below the legend, hence the large negative top. */
.destination-remove { position: absolute; top: -21px; right: 12px; padding: 0 4px; background: var(--bg); font-size: 13px; }
.destination-remove .check { display: flex; align-items: center; gap: 6px; cursor: pointer; }

/* Order page pallet grid: each cell holds a compact .field (input + its error); the footer has the Add area on the
   left, then the totals and Save on the right. */
.pallet-grid .table td { padding: 6px 8px; vertical-align: top; }
.pallet-grid .field input,
.pallet-grid .field select { min-width: 64px; padding: 5px 8px; }
/* Number cells stay narrow so the dropdowns get room for "No class" and destination names instead of clipping them. */
.pallet-grid .field input { width: 10ch; }
.pallet-grid .field select { min-width: 13ch; }
.pallet-grid .field-error { white-space: normal; }
.pallet-actions .check { display: flex; align-items: center; gap: 6px; padding-top: 6px; cursor: pointer; }
.pallet-grid-help { margin: 0 0 12px; }
.pallet-buttons { display: flex; gap: 6px; }
.pallet-grid-footer { display: flex; align-items: center; gap: 16px; margin-top: 12px; }
.pallet-grid-totals { margin: 0 0 0 auto; }

/* Confirmation dialog (native <dialog>, opened with showModal()). */
.dialog {
  width: min(420px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  box-shadow: 0 8px 24px rgb(0 0 0 / 12%);
}
.dialog::backdrop { background: rgb(0 0 0 / 40%); }
.dialog h2 { margin: 0 0 8px; }
.dialog p { margin: 0 0 12px; }
.dialog form { margin: 0; }
.field { display: grid; gap: 4px; }
.field label { font-weight: 500; font-size: 13px; }
.field input,
.field select,
.field textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.field textarea { min-height: 88px; resize: vertical; }
.input-prefixed { display: flex; gap: 6px; } /* e.g. At/Before/After select + time input in one row */
.field .input-prefixed select { width: auto; flex: none; }
.field .check { display: flex; align-items: center; gap: 8px; justify-self: start; cursor: pointer; }
.field .check input { width: auto; margin: 0; padding: 0; accent-color: var(--accent); }
.field input:focus,
.field select:focus,
.field textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field.has-error input,
.field.has-error select,
.field.has-error textarea { border-color: #dc2626; }
.help { margin: 0; color: var(--muted); font-size: 13px; }
.field-error { margin: 0; color: #dc2626; font-size: 13px; }

/* Alerts (Django messages + non-field errors) */
.alert { padding: 10px 14px; border: 1px solid var(--border); border-radius: 6px; margin-bottom: 20px; }
.alert p { margin: 0; }
.alert-success { border-color: #16a34a; color: #16a34a; }
.alert-error { border-color: #dc2626; color: #dc2626; }

@media (max-width: 640px) {
  /* Search + filter get a full row on phones; the action button drops below, right-aligned. */
  .toolbar:has(.filters) { flex-wrap: wrap; }
  .toolbar .filters { flex-basis: 100%; }
  .toolbar .filters input { max-width: none; }
  .toolbar:has(.filters) > .btn { margin-left: auto; }
  .form-row { grid-template-columns: 1fr; }
  .page-head { flex-wrap: wrap; }
}

/* Selects: replace the native arrow so it gets proper spacing from the border (Lucide chevron-down). */
.filters select,
.field select {
  appearance: none;
  padding-right: 30px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2371717a' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 7px center; /* icon has ~3px built-in inset, so the visible gap matches the 10px left padding */
  background-size: 16px;
  cursor: pointer;
}

/* Quote status badges */
.badge[data-status="ACCEPTED"] { color: #16a34a; border-color: currentColor; }
.badge[data-status="NEGOTIATING"] { color: #2563eb; border-color: currentColor; }
.badge[data-status="REJECTED"] { color: var(--muted); opacity: 0.7; }
