/* D07: Biz-Brain design tokens
   Palette: ink bg / off-white surfaces / amber accent / sage done / clay error
   Type: humanist grotesque display + Inter data face
   Signature: state pill — rounded-square tag, per-state color, consistent across all rows */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');

:root {
  /* Color tokens */
  --color-bg:           #1C2128;
  --color-surface:      #242B35;
  --color-surface-2:    #2D3644;
  --color-border:       #3A4452;
  --color-text:         #F7F5F0;
  --color-text-muted:   #8B96A8;
  --color-accent:       #D98E3B;
  --color-accent-dim:   #8C5A1E;
  --color-state-done:   #5B8266;
  --color-state-done-bg:#1E3027;
  --color-state-err:    #B85C4A;
  --color-state-err-bg: #321815;
  --color-state-warn:   #C49A3C;
  --color-state-warn-bg:#312510;
  --color-state-ongoing:#C4853C;
  --color-state-ongoing-bg:#17311F;
  --color-state-neutral:#5A7098;
  --color-state-neutral-bg:#1A2332;

  /* Type */
  --font-body:   'Inter', system-ui, -apple-system, sans-serif;
  --font-display: 'Inter', system-ui, sans-serif;

  /* Scale */
  --text-xs:   0.75rem;
  --text-sm:   0.875rem;
  --text-base: 1rem;
  --text-lg:   1.125rem;
  --text-xl:   1.375rem;
  --text-2xl:  1.75rem;

  /* Space */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --space-10: 2.5rem;
  --space-12: 3rem;

  /* Radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-pill: 999px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; }

body {
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-body);
  font-size: var(--text-sm);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  /* Issue 4: never let body be wider than the viewport */
  overflow-x: hidden;
}

/* Typography */
h1, h2, h3 { font-family: var(--font-display); font-weight: 600; line-height: 1.2; }
h1 { font-size: var(--text-2xl); letter-spacing: -0.02em; }
h2 { font-size: var(--text-xl); }
h3 { font-size: var(--text-lg); }

a { color: var(--color-accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Layout */
.app-layout {
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 100vh;
}

/* ── Topbar — desktop (single row) ──────────────────────────────────────── */

.topbar {
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  padding: var(--space-3) var(--space-8);
  display: flex;
  align-items: center;
  gap: var(--space-6);
  /* Issue 4: never overflow viewport */
  width: 100%;
  min-width: 0;
}

.topbar__wordmark {
  font-weight: 700;
  font-size: var(--text-base);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-text);
  white-space: nowrap;
  flex-shrink: 0;
}

.topbar__wordmark span {
  color: var(--color-accent);
}

.topbar__nav {
  display: flex;
  gap: var(--space-1);
  list-style: none;
}

/* Issue 4 again: when the tabs do not fit (below ~1230px since SS-11 §2 added
   Shop Stock) they wrap onto a second line instead of widening the page.
   Not a hidden sideways strip: a mouse could not reach the tabs past the
   edge. Phones keep their own sliding strip (MOBILE, below). */
@media (min-width: 769px) {
  .topbar > nav { min-width: 0; }
  .topbar__nav { flex-wrap: wrap; row-gap: var(--space-1); }
}

.topbar__nav a {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-weight: 500;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  transition: color 0.15s, background 0.15s;
  white-space: nowrap;
}

.topbar__nav a:hover,
.topbar__nav a.active {
  color: var(--color-text);
  background: var(--color-surface-2);
  text-decoration: none;
}

.topbar__actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-shrink: 0;
}

.filter-bar {
  display: flex;
  gap: var(--space-3);
  align-items: flex-end;
  flex-wrap: wrap;
  margin-bottom: var(--space-4);
}

.page {
  padding: var(--space-8);
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
}

.page-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: var(--space-6);
}

.page-header h1 { font-size: var(--text-xl); }

/* Cards / surfaces */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

/* Tables */
.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table thead th {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  border-bottom: 1px solid var(--color-border);
}

.data-table tbody tr {
  border-bottom: 1px solid var(--color-border);
  transition: background 0.1s;
}

.data-table tbody tr:last-child { border-bottom: none; }
.data-table tbody tr:hover { background: var(--color-surface-2); }

.data-table tbody td {
  padding: var(--space-3) var(--space-4);
  vertical-align: middle;
}

.data-table__empty {
  padding: var(--space-10);
  text-align: center;
  color: var(--color-text-muted);
}

/* State pill — the signature element */
.state-pill {
  display: inline-flex;
  align-items: center;
  padding: 2px var(--space-2);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  white-space: nowrap;
}

.state-pill--done,
.state-pill--supplier_confirmed { background: var(--color-state-done-bg); color: var(--color-state-done); }

.state-pill--cancelled,
.state-pill--unavailable,
.state-pill--called_off { background: var(--color-state-err-bg); color: var(--color-state-err); }

.state-pill--assigned,
.state-pill--invoice_received,
.state-pill--owner_approved_invoice { background: var(--color-state-warn-bg); color: var(--color-state-warn); }

.state-pill--accepted { background: var(--color-state-ongoing-bg); color: var(--color-state-ongoing); }

.state-pill--unassigned,
.state-pill--draft,
.state-pill--sent,
.state-pill--pending_clarification,
.state-pill--ready_for_pickup { background: var(--color-state-neutral-bg); color: var(--color-state-neutral); }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  font-weight: 500;
  font-family: var(--font-body);
  cursor: pointer;
  border: none;
  transition: background 0.15s, opacity 0.15s;
  text-decoration: none;
  white-space: nowrap;
}

.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn:hover:not(:disabled) { text-decoration: none; }

.btn--primary {
  background: var(--color-accent);
  color: #fff;
}
.btn--primary:hover:not(:disabled) { background: #c07e30; }

.btn--ghost {
  background: transparent;
  color: var(--color-text-muted);
  border: 1px solid var(--color-border);
}
.btn--ghost:hover:not(:disabled) {
  background: var(--color-surface-2);
  color: var(--color-text);
}

.btn--danger {
  background: var(--color-state-err);
  color: #fff;
}
.btn--danger:hover:not(:disabled) { background: #a04030; }

.btn--sm {
  padding: var(--space-1) var(--space-3);
  font-size: var(--text-xs);
}

/* Forms */
.form-group { display: flex; flex-direction: column; gap: var(--space-2); }
.form-label { font-size: var(--text-sm); font-weight: 500; color: var(--color-text-muted); }

.form-input,
.form-select,
.form-textarea {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text);
  font-family: var(--font-body);
  font-size: var(--text-sm);
  padding: var(--space-2) var(--space-3);
  width: 100%;
  transition: border-color 0.15s;
  /* Issue 4: inputs must never overflow their container */
  min-width: 0;
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  outline: none;
  border-color: var(--color-accent);
}

.form-textarea { resize: vertical; min-height: 80px; }

/* Modal / drawer for create forms */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  z-index: 100;
  padding: var(--space-4);
}

.modal {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  /* Issue 4: never wider than the viewport */
  width: 420px;
  max-width: calc(100vw - var(--space-8));
  max-height: calc(100vh - var(--space-8));
  overflow-y: auto;
  padding: var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal__title { font-size: var(--text-lg); font-weight: 600; }

.modal__body { display: flex; flex-direction: column; gap: var(--space-4); }

.modal__footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  padding-top: var(--space-2);
  border-top: 1px solid var(--color-border);
}

/* Confirm dialog */
.confirm-dialog {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-6);
  /* Issue 4: never wider than viewport */
  width: 380px;
  max-width: calc(100vw - var(--space-8));
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.confirm-dialog p { color: var(--color-text-muted); font-size: var(--text-sm); }

.confirm-dialog__actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
}

/* Toast */
.toast-container {
  position: fixed;
  bottom: var(--space-6);
  right: var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  z-index: 200;
  /* Issue 4: toasts don't push page width */
  max-width: calc(100vw - var(--space-8));
}

.toast {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-sm);
  min-width: 260px;
  max-width: 100%;
  animation: toast-in 0.2s ease;
  word-break: break-word;
}

.toast--error { border-color: var(--color-state-err); }
.toast--success { border-color: var(--color-state-done); }

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

/* Live indicator */
.live-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  font-weight: 500;
}

.live-badge__dot {
  width: 6px;
  height: 6px;
  border-radius: var(--radius-pill);
  background: var(--color-state-done);
}

.live-badge--connected .live-badge__dot { animation: pulse 2s infinite; }

.live-badge--disconnected .live-badge__dot {
  background: var(--color-state-err);
  animation: none;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.3; }
}

/* Login page */
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: var(--color-bg);
  padding: var(--space-4);
}

.login-card {
  /* Issue 4: login card must fit any phone */
  width: 360px;
  max-width: 100%;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-8);
  display: flex;
  flex-direction: column;
  gap: var(--space-6);
}

.login-card__wordmark {
  font-weight: 700;
  font-size: var(--text-lg);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.login-card__wordmark span { color: var(--color-accent); }

.login-form { display: flex; flex-direction: column; gap: var(--space-4); }

.error-banner {
  background: var(--color-state-err-bg);
  border: 1px solid var(--color-state-err);
  border-radius: var(--radius-sm);
  color: var(--color-state-err);
  font-size: var(--text-sm);
  padding: var(--space-3);
}

/* Chips for order links in supply request rows */
.chip-list { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.chip {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
  padding: 1px var(--space-2);
  color: var(--color-text-muted);
  font-family: monospace;
}

/* Utility */
.text-muted { color: var(--color-text-muted); }
.text-sm    { font-size: var(--text-sm); }
.text-xs    { font-size: var(--text-xs); }
.font-mono  { font-family: monospace; }
.hidden     { display: none !important; }

/* D08-5: channel badge — used on actors page */
.channel-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px var(--space-2);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  white-space: nowrap;
}

/* One line per chat link: the Channel and Sender ID cells of a row hold one
   line each per link, and the lines have to stay level (CA spec §13.5). */
.link-line {
  display: flex;
  align-items: center;
  min-height: 1.75rem;
}

/* CA spec §14.3: the Chat language. The small EN / TH tag beside a name —
   only on a person who differs from the shop — and the choice in a row. */
.lang-tag {
  display: inline-flex;
  align-items: center;
  margin-left: var(--space-1);
  padding: 0 var(--space-1);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.05em;
  vertical-align: middle;
}

.data-table .chat-language-select {
  width: auto;
  min-width: 9rem;
}

.chat-language-line {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-2);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  white-space: nowrap;
}

/* LINE: green-ish */
.channel-badge--line {
  background: #0d2e1a;
  color: #4caf7d;
}

/* Worker message bubble */
.worker-message-row td {
  padding: 0 var(--space-4) var(--space-3);
  background: var(--color-surface-2);
}

.worker-message-bubble {
  border-left: 3px solid var(--color-accent);
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.worker-message-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.worker-message-text {
  font-size: var(--text-sm);
  color: var(--color-text);
}

.worker-reply-box {
  display: flex;
  gap: var(--space-2);
  margin-top: var(--space-1);
}

.worker-reply-input {
  flex: 1;
}

/* ── F01: Order short-name tag ─────────────────────────────────────────── */

.order-tag {
  display: inline-flex;
  align-items: center;
  padding: 1px var(--space-2);
  border-radius: var(--radius-sm);
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  color: var(--color-accent);
  font-family: monospace;
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.04em;
  white-space: nowrap;
  vertical-align: middle;
  position: relative;
  top: -1px;
}

/* ══════════════════════════════════════════════════════════════════════════
   Breakpoint tokens + responsive shell
   ══════════════════════════════════════════════════════════════════════════ */

:root {
  --bp-mobile: 768px;
  --bp-tablet: 1024px;
}

/* Utility: show/hide by viewport */
.desktop-only { display: revert; }
.mobile-only  { display: none; }

/* ══════════════════════════════════════════════════════════════════════════
   MOBILE  ≤ 768px
   ══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {

  /* ── Utilities ──────────────────────────────────────────────────────── */
  .mobile-only  { display: block; }
  .desktop-only { display: none; }

  /* ── Page layout ────────────────────────────────────────────────────── */
  .page {
    padding: var(--space-4);
    max-width: 100%;
  }
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
  }

  /* ── Desktop table hidden — card layout replaces it ─────────────────── */
  .data-table { display: none; }

  /* ── 16px on inputs prevents iOS auto-zoom ──────────────────────────── */
  .form-input,
  .form-select,
  .form-textarea { font-size: 16px; }

  /* ────────────────────────────────────────────────────────────────────
     TOPBAR — 2-row mobile layout
     Row 1: wordmark  |  branch switcher  |  actions (account + sign-out)
     Row 2: nav tabs (scrollable, full width)
     ────────────────────────────────────────────────────────────────────
     We use a CSS grid on the topbar so the two rows are cleanly separated
     regardless of how many nav items the locale adds.
  ──────────────────────────────────────────────────────────────────── */
  .topbar {
    display: grid;
    /* Row 1: wordmark | branch | actions (all auto-width, space-between)
       Row 2: nav tabs spanning full width */
    grid-template-columns: auto 1fr auto;
    grid-template-rows: auto auto;
    grid-template-areas:
      "wordmark branch actions"
      "nav      nav     nav";
    align-items: center;
    gap: 0;
    padding: var(--space-3) var(--space-4) 0;
    row-gap: 0;
  }

  /* Row 1 — wordmark */
  .topbar__wordmark {
    grid-area: wordmark;
    padding-right: var(--space-3);
    padding-bottom: var(--space-2);
  }

  /* Row 1 — branch switcher (middle, takes remaining space) */
  .topbar > .branch-switcher__btn {
    grid-area: branch;
    padding-bottom: var(--space-2);
    /* reset positioning from old CSS */
    order: unset;
    flex: unset;
    border-top: none;
    margin-top: 0;
    padding-top: 0;
    max-width: none;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .topbar .branch-switcher__btn {
    font-size: var(--text-xs);
    padding: 2px var(--space-2);
    max-width: 100%;
  }

  /* Row 1 — actions (sign-out / account, right-aligned) */
  .topbar__actions {
    grid-area: actions;
    margin-left: 0;
    padding-bottom: var(--space-2);
    gap: var(--space-2);
  }
  .topbar__actions .btn--sm {
    padding: var(--space-1) var(--space-2);
    font-size: var(--text-xs);
  }
  /* Hide the locale switcher on mobile — accessed via account settings */
  .topbar__actions .locale-switcher,
  .locale-switcher {
    display: none;
  }

  /* Row 2 — nav (full-width scrollable strip) */
  .topbar > nav {
    grid-area: nav;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    border-top: 1px solid var(--color-border);
    padding: var(--space-1) 0;
    -webkit-mask-image: linear-gradient(to right, black 80%, transparent 100%);
    mask-image: linear-gradient(to right, black 80%, transparent 100%);
    /* stretch to full grid width */
    margin: 0 calc(-1 * var(--space-4));
    padding-left: var(--space-4);
    padding-right: var(--space-8);
  }
  .topbar > nav::-webkit-scrollbar { display: none; }

  .topbar__nav {
    display: flex;
    flex-wrap: nowrap;
    gap: 0;
    list-style: none;
    width: max-content;
  }
  .topbar__nav a {
    white-space: nowrap;
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-xs);
    display: block;
  }
  .topbar__nav a.active {
    color: var(--color-text);
    background: var(--color-surface-2);
    border-radius: var(--radius-sm);
  }

  /* ── Filter bar: stacked ────────────────────────────────────────────── */
  .filter-bar {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-3);
  }
  .filter-bar .form-group {
    min-width: unset !important;
    width: 100%;
    margin-bottom: 0;
  }
  .filter-bar .form-group select,
  .filter-bar .form-group input { width: 100%; }
  .filter-bar .btn {
    width: 100%;
    justify-content: center;
  }

  /* ── Modal backdrop: full-screen ────────────────────────────────────── */
  .modal-backdrop:not(.hidden) {
    display: flex !important;
    padding: 0;
    align-items: stretch;
    justify-content: stretch;
  }

  /* ── Modal panel: full-screen ───────────────────────────────────────── */
  .modal {
    width: 100%;
    max-width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    border: none;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    gap: 0;
  }

  .modal__header {
    position: sticky;
    top: 0;
    background: var(--color-surface);
    z-index: 1;
    border-bottom: 1px solid var(--color-border);
    padding: var(--space-4);
    min-height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    flex-shrink: 0;
  }

  .modal__body {
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: var(--space-4);
    min-height: 0;
  }

  .modal__footer {
    position: sticky;
    bottom: 0;
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    padding: var(--space-4);
    flex-shrink: 0;
    flex-direction: column-reverse;
    gap: var(--space-3);
  }

  .modal__footer .btn {
    width: 100%;
    justify-content: center;
    min-height: 44px;
  }

  .modal__header .btn--ghost.btn--sm:last-child {
    min-width: 44px;
    min-height: 44px;
    justify-content: center;
  }

  /* ── Confirm dialog: full-screen on mobile ──────────────────────────── */
  .modal-backdrop .confirm-dialog {
    width: 100%;
    max-width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    border: none;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: var(--space-8) var(--space-6);
    gap: var(--space-6);
  }

  .confirm-dialog__actions {
    flex-direction: column-reverse;
    gap: var(--space-3);
  }

  .confirm-dialog__actions .btn {
    width: 100%;
    justify-content: center;
    min-height: 44px;
  }

} /* end @media (max-width: 768px) */