/* ══════════════════════════════════════════════════════════════════
   CODEX ORDERS VIEW (redesign step 9)
   See design_handoff_codex_redesign/README.md §Per-view specs · Orders.

   CSS-only restyle of #ordersDashboard. Preserves the existing markup
   (stats strip + toolbar + active-filter chip + orders list) and adds
   Codex chrome on top.

   Deferred (DEFERRED.md):
     - "+ New order" + "⊞ Batch from cards" toolbar buttons
     - 30-day spend-by-source stacked-bar strip
     - 2-per-row grid (vs current vertical list) — needs ordersView.js
       render change
     - Cancelled status stat chip (only 4 statuses present currently)
   ══════════════════════════════════════════════════════════════════ */

/* ── Dashboard wrapper ────────────────────────────────────────────── */
#ordersDashboard.orders-dashboard {
  background: transparent;
  padding: 0;
  /* Full-bleed — the rest of the app shell uses the entire viewport at
     2K+, so the dashboard does too. Override the legacy 1400px cap from
     orders.css. */
  max-width: none;
  margin: 0;
  width: 100%;
}

/* ── Stats strip (All / Pending / Shipped / Delivered / Cancelled) ──
   Responsive grid of centred tiles: each tile shows the icon + label on
   one line and the big count centred underneath. auto-fit keeps the row
   tight on desktop and wraps it into a clean 2-3 column grid on narrow
   screens — no more counts floating off in a cell's far corner. */
#ordersDashboard .orders-stats-strip {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  /* The 1px gap over a border-coloured backdrop draws clean hairlines
     between every tile in both axes, so the layout still reads right
     when it wraps to fewer columns on small screens. */
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0;
  margin-bottom: 12px;
  background-image: none;
  box-shadow: none;
  overflow: hidden;
}
@media (max-width: 680px) {
  #ordersDashboard .orders-stats-strip { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 400px) {
  #ordersDashboard .orders-stats-strip { grid-template-columns: repeat(2, 1fr); }
}

#ordersDashboard .orders-stat {
  /* Grid keeps the layout deterministic: icon + label share the top row
     (centred as a pair), the big count sits on its own row below. */
  display: grid;
  grid-template-columns: auto auto;
  grid-template-areas:
    "icon label"
    "value value";
  justify-content: center;
  align-content: center;
  column-gap: 6px;
  row-gap: 3px;
  padding: 12px 10px;
  cursor: pointer;
  position: relative;
  text-align: center;
  background: var(--surface);
  transition: background-color 0.15s ease;
}
#ordersDashboard .orders-stat:hover {
  background: var(--surface2);
}
#ordersDashboard .orders-stat--active {
  background: var(--surface2);
}

#ordersDashboard .orders-stat-icon {
  grid-area: icon;
  align-self: center;
  width: 15px;
  height: 15px;
  color: inherit;
  opacity: 0.85;
}

#ordersDashboard .orders-stat-label {
  grid-area: label;
  align-self: center;
  font-family: var(--font-sans);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  /* Sits right next to the icon and inherits its status color so the
     two read as a single labelled glyph. */
  color: inherit;
}

#ordersDashboard .orders-stat-value {
  grid-area: value;
  justify-self: center;
  font-family: var(--font-mono);
  font-size: 22px;
  font-weight: 600;
  line-height: 1.1;
  color: var(--text);
  letter-spacing: -0.01em;
}

/* Per-status tinting on the icon + label group. Value stays in --text
   so the number reads as the primary metric regardless of state. */
#ordersDashboard .orders-stat--pending  { color: var(--warm); }
#ordersDashboard .orders-stat--shipped  { color: var(--cool); }
#ordersDashboard .orders-stat--delivered{ color: var(--pos);  }
#ordersDashboard .orders-stat--cancelled{ color: var(--neg);  }
#ordersDashboard .orders-stat--pending .orders-stat-value { color: var(--warm); }
#ordersDashboard .orders-stat--shipped .orders-stat-value { color: var(--cool); }
#ordersDashboard .orders-stat--delivered .orders-stat-value { color: var(--pos); }
#ordersDashboard .orders-stat--cancelled .orders-stat-value { color: var(--neg); }

/* ── Toolbar (search + sort) ──────────────────────────────────────── */
#ordersDashboard .orders-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  margin-bottom: 12px;
  background-image: none;
  box-shadow: none;
}

#ordersDashboard .orders-search-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0 10px;
  height: 30px;
  flex: 1;
  min-width: 200px;
  max-width: 460px;
  color: var(--text-dim);
}
#ordersDashboard .orders-search-wrap:focus-within {
  border-color: var(--accent);
  box-shadow: var(--focus-ring);
}
#ordersDashboard .orders-search-icon {
  /* Legacy orders.css pinned this icon at position:absolute left:12px;
     we explicitly reset that so the flex order:2 wins and the icon
     lands at the right edge of the input. */
  position: static;
  left: auto;
  top: auto;
  transform: none;
  display: block;
  width: 14px;
  height: 14px;
  min-width: 14px;
  flex-shrink: 0;
  align-self: center;
  color: var(--text-dim);
  order: 2;
  margin-left: 6px;
}
#ordersDashboard .orders-search-input {
  flex: 1;
  background: transparent;
  border: 0;
  outline: none;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 12.5px;
  padding: 0;
  height: 28px;
  min-height: 0;
}
#ordersDashboard .orders-search-input::placeholder {
  color: var(--text-mute);
}

#ordersDashboard .orders-toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  flex-shrink: 0;
}

/* "+ New order" button (Codex step 9). */
#ordersDashboard .orders-new-order-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 30px;
  padding: 0 12px;
  border-radius: var(--radius-md);
  font-family: var(--font-sans);
  font-size: 11.5px;
  font-weight: 500;
  cursor: pointer;
  background: var(--accent);
  color: var(--bg);
  border: 1px solid var(--accent);
  background-image: none;
  box-shadow: none;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
#ordersDashboard .orders-new-order-btn:hover {
  background: color-mix(in srgb, var(--accent) 88%, white);
}

#ordersDashboard .orders-toolbar-divider {
  width: 1px;
  height: 22px;
  background: var(--border);
}

/* Below ~760px the toolbar can't hold everything on one line. Give the
   search its own full-width row and let the chips + right-hand controls
   wrap underneath instead of cramming into a tall ragged stack. */
@media (max-width: 760px) {
  #ordersDashboard .orders-toolbar { flex-wrap: wrap; }
  #ordersDashboard .orders-search-wrap { flex: 1 1 100%; max-width: none; }
  #ordersDashboard .orders-toolbar-right { margin-left: 0; flex-wrap: wrap; }
}

#ordersDashboard .orders-result-info {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-dim);
}

/* Sort control (reuses the .sort-control pattern from the countbar) */
#ordersDashboard .order-sort-control {
  display: inline-flex;
  align-items: stretch;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--surface2);
}
#ordersDashboard .order-sort-control .sort-select {
  background: transparent;
  border: 0;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 11.5px;
  padding: 0 26px 0 10px;
  height: 100%;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238a929e' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
  background-repeat: no-repeat;
  background-position: right 6px center;
  background-size: 12px 12px;
}
#ordersDashboard .order-sort-control .sort-order-toggle-inline {
  width: 28px;
  background: transparent;
  border: 0;
  border-left: 1px solid var(--border);
  color: var(--text-dim);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-mono);
  font-size: 13px;
}
#ordersDashboard .order-sort-control .sort-order-toggle-inline:hover {
  background: var(--surface3);
  color: var(--text);
}

/* ── Edit-order modal: sticky footer with Cancel←/Save→ ──────────── */
.edit-order-modal-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-modal);
  overflow: hidden;
}
.edit-order-modal-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex: 0 0 auto;
}

/* ── Quick source-filter chips — inline in the toolbar row ────────── */
#ordersDashboard .orders-quick-sources {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
}
#ordersDashboard .orders-quick-sources:empty {
  display: none;
}
#ordersDashboard .orders-quick-source-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  color: var(--text-dim);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
#ordersDashboard .orders-quick-source-chip:hover {
  background: var(--surface2);
  color: var(--text);
  border-color: var(--border-hi);
}
#ordersDashboard .orders-quick-source-chip.active {
  background: var(--accent-dim);
  color: var(--accent);
  border-color: var(--accent);
}
#ordersDashboard .orders-quick-source-name {
  color: inherit;
}
#ordersDashboard .orders-quick-source-count {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-mute);
  background: var(--surface2);
  padding: 1px 6px;
  border-radius: var(--radius-full);
}
#ordersDashboard .orders-quick-source-chip.active .orders-quick-source-count {
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 18%, transparent);
}

/* ── Orders list — auto-fill so wide screens pack more (appropriately
   sized) cards instead of stretching 2-3 cards across the whole width
   with tiny thumbnails and dead space in the middle. ──────────────── */
#ordersDashboard .orders-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(440px, 1fr));
  gap: 10px;
}
@media (max-width: 900px) {
  #ordersDashboard .orders-list {
    grid-template-columns: 1fr;
  }
}

/* Each order card (rendered by ordersView.js). */
#ordersDashboard .orders-list .order-card,
#ordersDashboard .orders-list .order-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  background-image: none;
  box-shadow: none;
  transition: border-color 0.15s ease, box-shadow 0.2s ease;
}
#ordersDashboard .orders-list .order-card:hover,
#ordersDashboard .orders-list .order-item:hover {
  border-color: var(--border-hi);
  box-shadow: var(--shadow-tile-hover);
}

/* Status stripe on order cards (left border). */
#ordersDashboard .orders-list .order-card.status-pending,
#ordersDashboard .orders-list .order-item.status-pending {
  border-left: 3px solid var(--warm);
}
#ordersDashboard .orders-list .order-card.status-shipped,
#ordersDashboard .orders-list .order-item.status-shipped {
  border-left: 3px solid var(--cool);
}
#ordersDashboard .orders-list .order-card.status-delivered,
#ordersDashboard .orders-list .order-item.status-delivered {
  border-left: 3px solid var(--pos);
}
#ordersDashboard .orders-list .order-card.status-cancelled,
#ordersDashboard .orders-list .order-item.status-cancelled {
  border-left: 3px solid var(--neg);
  opacity: 0.6;
}

/* ── Order detail modal (Codex step 9 — already a modal; chrome) ── */
#orderDetailModal.modal-overlay {
  background: var(--backdrop);
  backdrop-filter: blur(8px);
}
#orderDetailModal .order-detail-modal {
  background: var(--surface);
  border: 1px solid var(--border-hi);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-modal);
  max-width: 920px;
  width: 90vw;
  max-height: 88vh;
  background-image: none;
  color: var(--text);
}
#orderDetailModal .modal-header {
  background: transparent;
  border-bottom: 1px solid var(--border);
  padding: 16px 22px;
}
#orderDetailModal .modal-header h2 {
  font-family: var(--font-sans);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text);
  margin: 0;
}
#orderDetailModal .modal-close {
  width: 30px;
  height: 30px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  border-radius: var(--radius-md);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}
#orderDetailModal .modal-close:hover {
  background: var(--surface2);
  color: var(--text);
  border-color: var(--border-hi);
}
#orderDetailModal .modal-body {
  padding: 20px 22px;
  overflow-y: auto;
}

#orderDetailModal .order-detail-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 12px;
}
#orderDetailModal .order-detail-grid-3 {
  grid-template-columns: repeat(3, 1fr);
}

#orderDetailModal .order-detail-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

#orderDetailModal .order-detail-label {
  font-family: var(--font-sans);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-dim);
}

#orderDetailModal .order-detail-value {
  font-family: var(--font-sans);
  font-size: 13px;
  color: var(--text);
}
#orderDetailModal .order-detail-total {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--accent);
}
#orderDetailModal .order-detail-tracking {
  font-family: var(--font-mono);
}

#orderDetailModal .order-detail-section {
  margin-bottom: 12px;
}

#orderDetailModal .order-detail-notes {
  font-family: var(--font-sans);
  font-size: 12.5px;
  color: var(--text);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 8px 10px;
  white-space: pre-wrap;
}

#orderDetailModal .order-detail-cards-list {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 6px;
  /* Use the room the modal actually has (max-height 88vh) instead of a
     fixed 340px box that felt cramped on desktop while leaving the modal
     half-empty. Still scrolls for very large orders. */
  max-height: min(52vh, 560px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#orderDetailModal .order-detail-card {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 6px 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 5px;
  text-decoration: none;
  color: inherit;
}
#orderDetailModal .order-detail-card:hover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 6%, var(--surface));
}

#orderDetailModal .order-detail-card-image {
  width: 64px;
  height: 89px;
  object-fit: cover;
  border-radius: 5px;
  flex-shrink: 0;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}

#orderDetailModal .order-detail-card-name {
  font-family: var(--font-sans);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text);
}
#orderDetailModal .order-detail-card-set {
  font-family: var(--font-sans);
  font-size: 11px;
  color: var(--text-dim);
}
#orderDetailModal .order-detail-card-meta {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 2px;
}
#orderDetailModal .order-detail-card-notes {
  font-family: var(--font-sans);
  font-size: 11px;
  color: var(--text-mute);
  font-style: italic;
}

#orderDetailModal #orderStatusSelect {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 5px;
  font-family: var(--font-sans);
  font-size: 12.5px;
  padding: 4px 8px;
  height: 28px;
}

/* ── Order detail status timeline (Codex step 9) ──────────────────── */
#orderDetailModal .order-status-timeline {
  display: flex;
  gap: 3px;
  margin-bottom: 16px;
}
#orderDetailModal .order-timeline-step {
  flex: 1;
}
#orderDetailModal .order-timeline-bar {
  height: 3px;
  border-radius: 2px;
  background: var(--surface3);
}
#orderDetailModal .order-timeline-bar--reached {
  background: var(--accent);
}
#orderDetailModal .order-timeline-bar--cancelled {
  background: var(--neg);
}
#orderDetailModal .order-timeline-label {
  margin-top: 8px;
  font-family: var(--font-sans);
  font-size: 12px;
  color: var(--text-dim);
}
#orderDetailModal .order-timeline-label--reached {
  color: var(--text);
  font-weight: 500;
}

/* ── Order detail footer buttons — Codex tone ─────────────────────── */
#orderDetailModal .order-detail-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}
#orderDetailModal .order-detail-actions-left,
#orderDetailModal .order-detail-actions-right {
  display: flex;
  gap: 8px;
  align-items: center;
}
#orderDetailModal .order-detail-actions .btn {
  background-image: none;
  box-shadow: none;
  border-radius: var(--radius-md);
  font-family: var(--font-sans);
  font-size: 12.5px;
  font-weight: 500;
}
#orderDetailModal .order-detail-actions .btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}
#orderDetailModal .order-detail-actions .btn-secondary:hover {
  background: var(--surface3);
  border-color: var(--border-hi);
}
#orderDetailModal .order-detail-actions .btn-success {
  background: var(--accent);
  color: var(--bg);
  border: 1px solid var(--accent);
}
#orderDetailModal .order-detail-actions .btn-success:hover {
  background: color-mix(in srgb, var(--accent) 88%, white);
}
#orderDetailModal .order-detail-actions .btn-danger {
  background: var(--surface2);
  color: var(--neg);
  border: 1px solid color-mix(in srgb, var(--neg) 35%, transparent);
}
#orderDetailModal .order-detail-actions .btn-danger:hover {
  background: color-mix(in srgb, var(--neg) 10%, var(--surface2));
  border-color: var(--neg);
}

/* ── Dark-mode safety ─────────────────────────────────────────────── */
body.dark-mode #ordersDashboard .orders-stats-strip,
body.dark-mode #ordersDashboard .orders-toolbar,
body.dark-mode #ordersDashboard .orders-list .order-card,
body.dark-mode #ordersDashboard .orders-list .order-item {
  background: var(--surface);
  border-color: var(--border);
}
