/* SDOS v2 components — includes redefinitions of v1 legacy classes
   (.chip, .badge-*, table.orders, .order-tile, .capacity-bar, dl.detail …)
   so every existing screen reskins without view changes. */
@layer components {

  /* ---- buttons ---- */
  .btn, .actions button, input[type="submit"], button.linklike-btn {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 7px 14px; font-size: 13px; font-weight: 600;
    border-radius: var(--r-sm); border: 1px solid var(--line-strong);
    background: var(--surface); color: var(--text);
    text-decoration: none;
  }
  .btn:hover, .actions button:hover { background: var(--surface-2); text-decoration: none; }
  .btn-primary, input[type="submit"] {
    background: var(--primary); border-color: var(--primary); color: #fff;
  }
  .btn-primary:hover, input[type="submit"]:hover { filter: brightness(1.07); background: var(--primary); }
  .btn-sm { padding: 4px 10px; font-size: 12.5px; }
  .btn-danger, .actions .danger button, .actions button.danger {
    color: var(--danger-fg); border-color: var(--danger-fg); background: var(--danger-bg);
  }
  .linklike { background: none; border: none; color: var(--primary); text-decoration: underline; cursor: pointer; font-size: 13px; padding: 0; }
  .actions { display: flex; gap: 6px; flex-wrap: wrap; }
  .actions form { display: inline; }

  /* ---- status pills ---- */
  .pill, .badge {
    display: inline-block; padding: 2px 10px;
    border-radius: var(--r-pill); font-size: 12px; font-weight: 600;
    white-space: nowrap; background: var(--surface-3); color: var(--text-2);
  }
  .pill-new, .badge-pending { background: var(--st-new-bg); color: var(--st-new-fg); }
  .pill-verified, .badge-confirmed { background: var(--st-verified-bg); color: var(--st-verified-fg); }
  .pill-planned { background: var(--st-planned-bg); color: var(--st-planned-fg); }
  .pill-delivered, .badge-delivered { background: var(--st-delivered-bg); color: var(--st-delivered-fg); }
  .pill-cancelled, .badge-cancelled { background: var(--st-cancelled-bg); color: var(--st-cancelled-fg); }
  .pill-ok { background: var(--ok-bg); color: var(--ok-fg); }
  .pill-warn { background: var(--warn-bg); color: var(--warn-fg); }
  .pill-danger, .pill-urgent { background: var(--danger-bg); color: var(--danger-fg); }

  /* ---- cards ---- */
  .card {
    background: var(--surface); border: 1px solid var(--line);
    border-radius: var(--r-lg); padding: 14px 16px;
    box-shadow: var(--shadow-card);
  }
  .card-title { font-size: 13px; font-weight: 700; margin: 0 0 10px; }
  .kpi-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 12px 14px; box-shadow: var(--shadow-card); }
  .kpi-label { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
  .kpi-value { font-size: 22px; font-weight: 700; font-family: var(--font-mono); margin-top: 2px; }
  .kpi-sub { font-size: 11.5px; color: var(--muted); }
  .grid-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }

  /* ---- tab bar (server-filtered link tabs with counts) ---- */
  .tabbar { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
  .tabbar a {
    padding: 8px 13px; font-size: 13px; font-weight: 600;
    color: var(--text-2); border-bottom: 2px solid transparent;
    white-space: nowrap; text-decoration: none;
  }
  .tabbar a:hover { color: var(--text); }
  .tabbar a.active { color: var(--primary); border-bottom-color: var(--primary); }
  .tabbar .count.count-warn { background: var(--warn-bg); color: var(--warn-fg); }
  .tabbar .count {
    background: var(--surface-3); border-radius: var(--r-pill);
    font-size: 11px; padding: 1px 7px; margin-left: 5px; color: var(--text-2);
  }

  /* ---- filters / chips ---- */
  .filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
  .chip {
    padding: 5px 12px; border-radius: var(--r-pill);
    background: var(--surface); border: 1px solid var(--line-strong);
    text-decoration: none; color: var(--text); font-size: 13px;
  }
  .chip:hover { background: var(--surface-2); text-decoration: none; }
  .chip-active { background: var(--primary); color: #fff; border-color: var(--primary); }
  .search { margin-left: auto; }
  .chip-toggle { display: inline-flex; align-items: center; gap: 5px; padding: 4px 11px; border-radius: var(--r-pill); border: 1px solid var(--line-strong); font-size: 12.5px; background: var(--surface); cursor: pointer; }
  .chip-toggle.on { background: var(--primary-soft); border-color: var(--primary-border); color: var(--primary); }

  /* ---- tables ---- */
  table.orders, table.data {
    width: 100%; border-collapse: collapse;
    background: var(--surface); border-radius: var(--r-md); overflow: hidden;
    border: 1px solid var(--line);
  }
  table.orders th, table.orders td, table.data th, table.data td {
    padding: 9px 10px; text-align: left;
    border-bottom: 1px solid var(--line); font-size: 13px; vertical-align: top;
  }
  table.orders th, table.data th {
    background: var(--surface-2); color: var(--text-2);
    font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  }
  table.orders th a, table.data th a { color: var(--text-2); text-decoration: none; }
  table.orders tbody tr:hover, table.data tbody tr:hover { background: var(--surface-2); }
  tr.status-cancelled td { opacity: 0.55; }

  /* ---- detail definition lists ---- */
  dl.detail {
    background: var(--surface); border: 1px solid var(--line);
    border-radius: var(--r-md); padding: 14px;
    display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px;
  }
  dl.detail dt { color: var(--muted); font-size: 12.5px; }
  dl.detail dd { margin: 0; }

  /* ---- flash ---- */
  .flash { background: var(--ok-bg); color: var(--ok-fg); border: 1px solid transparent; padding: 9px 13px; border-radius: var(--r-md); margin: 0 0 12px; }
  .flash-alert { background: var(--danger-bg); color: var(--danger-fg); }

  /* ---- forms ---- */
  form div { margin-bottom: 10px; }
  label { font-size: 12px; font-weight: 600; color: var(--text-2); }
  .topics-form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
  .topic-check { font-size: 13px; white-space: nowrap; font-weight: 400; }
  .topics-form input[type="submit"] {
    padding: 3px 9px; font-size: 12.5px; width: auto;
    background: var(--surface); color: var(--text); border: 1px solid var(--line-strong);
  }

  /* ---- form + media primitives (the shared design-system vocabulary) ---- */
  /* a labeled, stacked control: <label class="field">Name <input/select></label> */
  .field { display: flex; flex-direction: column; gap: 3px; min-width: 0; font-size: 12px; color: var(--muted); }
  .field > input, .field > select, .field > textarea { width: 100%; min-width: 140px; color: var(--text); }
  /* a horizontal row of fields + a trailing action, baseline-aligned */
  .inline-form { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
  .inline-form > input[type="submit"], .inline-form > button { align-self: flex-end; }
  /* a collapsible card (details/summary) with a clean caret marker */
  .disclosure {
    background: var(--surface); border: 1px solid var(--line);
    border-radius: var(--r-lg); padding: 12px 14px; box-shadow: var(--shadow-card);
  }
  .disclosure > summary { cursor: pointer; font-weight: 600; list-style: none; display: flex; align-items: center; gap: 8px; }
  .disclosure > summary::-webkit-details-marker { display: none; }
  .disclosure > summary::before { content: "▸"; color: var(--muted); font-size: 11px; transition: transform 0.15s ease; }
  .disclosure[open] > summary::before { transform: rotate(90deg); }
  .disclosure[open] > summary { margin-bottom: 10px; }
  /* an image gallery (delivery proofs, etc.) */
  .thumbs { display: flex; gap: 8px; flex-wrap: wrap; }
  .thumb { width: 74px; height: 96px; object-fit: cover; border-radius: var(--r-sm); background: var(--surface-3); display: block; }
  .thumb--lg { width: 92px; height: 120px; border-radius: 7px; }
  /* a page top-bar: a back link on the left, actions pushed to the right */
  .page-bar { display: flex; align-items: center; gap: 10px; margin: 0 0 6px; }
  .page-bar .spacer { flex: 1; }
  /* a compact inline select (e.g. in a table row) */
  .select-sm { max-width: 170px; font-size: 12px; }
  /* generic flex spacer — pushes following items to the far edge of a flex row */
  .spacer { flex: 1; }

  /* ---- planning board (kanban) ---- */
  .plan-board { display: flex; gap: 12px; align-items: flex-start; overflow-x: auto; padding-bottom: 12px; }
  .plan-column {
    background: var(--surface-2); border: 1px solid var(--line);
    border-radius: var(--r-md); padding: 10px; min-width: 240px; max-width: 280px; flex: 1;
  }
  .plan-column h3 { margin: 2px 4px 8px; font-size: 14px; }
  .stop-list { min-height: 60px; display: flex; flex-direction: column; gap: 6px; }
  .order-tile, .tile {
    background: var(--surface); border: 1px solid var(--line-strong);
    border-radius: var(--r-sm); padding: 6px 9px; cursor: grab;
    display: flex; flex-direction: column; gap: 1px; font-size: 13px;
    box-shadow: var(--shadow-card);
  }
  .order-tile:active, .tile:active { cursor: grabbing; }
  .order-tile strong, .tile strong { font-size: 13px; }
  .tile-id { font-family: var(--font-mono); font-size: 11.5px; color: var(--muted); }
  .tile-meta { color: var(--muted); font-size: 12px; }
  .tile.urgent, .order-tile.urgent { border-color: var(--urgent); cursor: default; }
  .sortable-ghost { opacity: 0.4; }

  /* ---- capacity bars ---- */
  .capacity { margin-bottom: 8px; }
  .capacity-bar { background: var(--surface-3); border: 1px solid var(--line); border-radius: var(--r-pill); height: 10px; overflow: hidden; }
  .capacity-fill { background: var(--warn); height: 100%; transition: width 0.2s; }
  .capacity-fill.good { background: var(--ok); }
  .capacity-fill.over { background: var(--danger); }
  .cap-text { font-size: 12.5px; color: var(--text-2); }

  /* ---- pure-CSS bar chart ---- */
  .bar-chart { display: flex; align-items: flex-end; gap: 4px; height: 90px; }
  .bar-chart .bar { flex: 1; background: var(--primary-soft); border: 1px solid var(--primary-border); border-radius: 3px 3px 0 0; min-width: 8px; position: relative; }
  .bar-chart .bar.hot { background: var(--primary); border-color: var(--primary); }
  .bar-chart .bar-label { position: absolute; top: 100%; left: 50%; transform: translateX(-50%); font-size: 9.5px; color: var(--muted); margin-top: 3px; white-space: nowrap; }

  /* ---- page header row ---- */
  .page-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
  .page-head .spacer { margin-left: auto; }

  /* ---- live indicator dot (dashboard / driver / map) ---- */
  @keyframes sdosPulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
  .live-dot {
    display: inline-block; width: 8px; height: 8px; border-radius: 50%;
    background: var(--ok); animation: sdosPulse 2s ease-in-out infinite;
  }

  /* ---- maps ---- */
  .map-box { border-radius: var(--r-lg); overflow: hidden; border: 1px solid var(--line); }
  .map-pin {
    width: 28px; height: 28px; border-radius: 50%;
    background: var(--primary); color: #fff;
    display: grid; place-items: center;
    font-size: 12.5px; font-weight: 700; font-family: var(--font-mono);
    border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  }
  .map-pin-origin { background: var(--rail); font-size: 14px; }

  @media (max-width: 900px) {
    .order-detail-grid { grid-template-columns: 1fr !important; }
  }

  /* ---- production flow strip ---- */
  .flow-strip { display: flex; align-items: stretch; gap: 8px; margin: 10px 0 16px; flex-wrap: wrap; }
  .flow-node {
    background: var(--surface); border: 1px solid var(--line);
    border-radius: var(--r-lg); padding: 10px 14px; min-width: 130px;
    box-shadow: var(--shadow-card);
  }
  .flow-arrow { align-self: center; color: var(--muted); font-size: 18px; }
}
