/* ProcureWatch dashboard — Sanjivani design system.
   Load order: design-tokens.css, sanjivani-components.css (.sj-* components), then this file, which extends them
   for the app (page layout, charts, the Follow-up sheet). Every colour, size and radius is a token. */

* { box-sizing: border-box; margin: 0; padding: 0; }
body { font: var(--body-sm); color: var(--ink); background: var(--surface-sunken); }
a { color: var(--link); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ── App header (extends .sj-header) ─────────────────────────────────────────────────── */
.sj-header { flex-wrap: wrap; row-gap: 0; height: auto; min-height: 56px; position: sticky; top: 0; z-index: 20; }
.sj-header__brand { display: inline-flex; align-items: center; min-height: 28px; color: var(--ink); text-decoration: none; }
a.sj-header__app { text-decoration: none; }
.sj-header__nav { overflow-x: auto; white-space: nowrap; min-width: 0; -webkit-overflow-scrolling: touch; }
.sj-header__link { transition: color var(--motion-fast); }
.sj-header__user { white-space: nowrap; }
.sj-header__tools { display: flex; align-items: center; gap: var(--space-2); }
/* Logo slots: the dark theme shows the reversed logo (sanjivani-logo-reversed.svg). The alt text is the fallback until a file exists. */
.logo-dark { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .logo-light { display: none; }
  :root:not([data-theme="light"]) .logo-dark { display: block; }
}
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="dark"] .logo-dark { display: block; }
.sj-header__logo { width: auto; font: var(--label); color: var(--ink); }
@media (max-width: 1480px) {
  .sj-header { padding: var(--space-2) var(--space-4) 0; }
  .sj-header__spacer { display: none; }
  .sj-header__nav { order: 5; flex: 1 0 100%; margin-left: calc(var(--space-2) * -1); }
  .sj-header__link { padding: var(--space-3) var(--space-3); }
  .sj-header__tools { margin-left: auto; }
}

/* ── Page ─────────────────────────────────────────────────────────────────────────────── */
main { max-width: 1180px; margin: var(--space-5) auto var(--space-8); padding: 0 var(--space-4); }
.loading { color: var(--ink-muted); text-align: center; padding: var(--space-8) 0; }
h1.page-title { font: var(--h1); margin-bottom: var(--space-1); }
.sub { color: var(--ink-muted); font: var(--caption); font-size: 13px; line-height: 18px; margin-bottom: var(--space-3); }
.row { display: flex; gap: var(--space-2); flex-wrap: wrap; align-items: center; }
.row.between { justify-content: space-between; }
.row.end { align-items: flex-end; }
.mt-2 { margin-top: var(--space-2); } .mt-3 { margin-top: var(--space-3); } .mt-4 { margin-top: var(--space-4); } .mt-5 { margin-top: var(--space-5); }
.mb-1 { margin-bottom: var(--space-1); }
.check { display: flex; gap: var(--space-2); align-items: center; font: var(--body-sm); }
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--action); flex: none; }
.overline { font: var(--overline); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-muted); }

/* ── Cards ────────────────────────────────────────────────────────────────────────────── */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: var(--space-4); margin-bottom: var(--space-5); }
.card > h2 { font: var(--h3); color: var(--ink); margin-bottom: var(--space-3); display: flex; justify-content: space-between; align-items: baseline; gap: var(--space-3); flex-wrap: wrap; }
.card > h2 small { font: var(--caption); color: var(--ink-muted); }
.modal-title { font: var(--h3); }
.grid { display: grid; gap: var(--space-5); }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 860px) { .grid.two, .grid.three { grid-template-columns: 1fr; } }

/* ── Fields (extends .sj-field; the inputs are styled by element so the page scripts stay unchanged) ── */
.filters { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: flex-end; margin-bottom: var(--space-4); }
.filters .f { min-width: 120px; flex: 1 1 130px; }
.f, .field { display: flex; flex-direction: column; gap: var(--space-1); }
.f > label, .field > label { font: var(--label); letter-spacing: .01em; color: var(--ink); }
.field .note { font: var(--caption); color: var(--ink-muted); }
input[type=text], input[type=date], input[type=number], input[type=email], input[type=password], select {
  height: 36px; padding: 0 var(--space-3); border: 1px solid var(--border-strong); border-radius: var(--radius-md);
  background: var(--surface); color: var(--ink); font: var(--body-sm); width: 100%;
}
textarea { padding: var(--space-2) var(--space-3); border: 1px solid var(--border-strong); border-radius: var(--radius-md);
  background: var(--surface); color: var(--ink); font: var(--body-sm); width: 100%; resize: vertical; min-height: 64px; }
input::placeholder, textarea::placeholder { color: var(--ink-subtle); }
input:disabled, select:disabled, textarea:disabled { opacity: .45; cursor: not-allowed; }
.sj-btn { transition: background-color var(--motion-fast), border-color var(--motion-fast); }
.sj-btn[disabled] { pointer-events: none; }

/* ── Stat cards (.sj-stat) ────────────────────────────────────────────────────────────── */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: var(--space-4); margin-bottom: var(--space-5); }
.sj-stat--hero .sj-stat__value { font-size: 40px; line-height: 48px; }

/* ── Tables (.sj-table) ───────────────────────────────────────────────────────────────── */
.tbl-wrap { overflow: auto; max-height: 75vh; border: 1px solid var(--border); border-radius: var(--radius-md); }
.sj-table thead th { position: sticky; top: 0; z-index: 1; white-space: nowrap; }
.sj-table tbody tr:last-child td { border-bottom: 0; }
.sj-table td { vertical-align: top; }
.sj-table td.nowrap { white-space: nowrap; }
.sj-table th.num { text-align: right; font: 600 13px/18px var(--font-sans); }
th.sortable { cursor: pointer; } th.sortable:hover { color: var(--ink); text-decoration: underline; }
tr.clickable { cursor: pointer; }
.empty { color: var(--ink-muted); padding: var(--space-5) var(--space-3); text-align: center; }
details.table-view { margin-top: var(--space-3); font: var(--caption); }
details.table-view summary { cursor: pointer; color: var(--ink-muted); }
details.table-view .tbl-wrap { margin-top: var(--space-2); }

/* Small annotations and result text: always a word (or a glyph and a word), never colour alone */
.tag { display: inline-block; font: var(--caption); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 0 var(--space-2); color: var(--ink-muted); margin-left: var(--space-1); white-space: nowrap; }
.good { color: var(--success); font-weight: 600; } .bad { color: var(--danger); font-weight: 600; }
.job-ok::before { content: "✓ "; } .job-failed::before { content: "✕ "; } .job-stale::before { content: "! "; } .job-never::before { content: "– "; }
.job-ok { color: var(--success); font-weight: 600; } .job-failed { color: var(--danger); font-weight: 600; } .job-stale { color: var(--warning); font-weight: 600; } .job-never { color: var(--ink-muted); }
.sj-badge--cancelled { text-decoration: line-through; }

/* ── Charts: neutral marks; the action colour highlights the one series that needs attention ── */
.chart { width: 100%; overflow: visible; }
.chart text { fill: var(--ink-muted); font: var(--caption); }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.chart .axis-line { stroke: var(--border-strong); stroke-width: 1; }
.chart .bar { fill: var(--ink-muted); }
.chart .bar:hover, .chart .bar:focus { opacity: .8; outline: none; stroke: var(--ink); stroke-width: 1; }
.chart .line { fill: none; stroke: var(--ink); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--ink); stroke: var(--surface); stroke-width: 2; }
.chart .val { fill: var(--ink); font: 600 12px/16px var(--font-sans); }
.chart .lbl { fill: var(--ink-muted); }
.chart .hit { fill: transparent; }
.chart .cross { stroke: var(--ink-subtle); stroke-width: 1; }
.hbars .row-bar { display: grid; grid-template-columns: 150px 1fr 56px; align-items: center; gap: var(--space-2); margin: var(--space-2) 0; }
.hbars .row-bar .name { font: var(--body-sm); }
.hbars .track { background: var(--surface-sunken); height: 14px; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }
.hbars .fill { height: 14px; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; min-width: 2px; background: var(--ink-muted); }
.hbars .fill.hi { background: var(--action); }
.hbars .count { text-align: right; font: var(--data); font-variant-numeric: tabular-nums; }
#tip { position: fixed; pointer-events: none; z-index: 50; background: var(--surface-raised); color: var(--ink); border: 1px solid var(--border); border-radius: var(--radius-md); padding: var(--space-2) var(--space-3); font: var(--caption); box-shadow: var(--shadow-md); display: none; max-width: 240px; }
#tip strong { font: var(--h3); display: block; }
#toast { position: fixed; bottom: var(--space-5); right: var(--space-5); padding: var(--space-3) var(--space-4); border-radius: var(--radius-md); font: 600 14px/20px var(--font-sans); z-index: 60; display: none; align-items: center; gap: var(--space-3); box-shadow: var(--shadow-md); color: var(--ink); max-width: min(90vw, 480px); border: 1px solid var(--border); }
#toast::before { font-weight: 700; }
#toast .toast-msg { flex: 1; word-break: break-word; }
#toast .toast-close { flex: none; }
#toast.ok { background: var(--success-tint); border-color: var(--success); } #toast.ok::before { content: "✓"; color: var(--success); }
#toast.err { background: var(--danger-tint); border-color: var(--danger); } #toast.err::before { content: "✕"; color: var(--danger); }

/* ── PO detail ────────────────────────────────────────────────────────────────────────── */
.po-head { display: flex; flex-wrap: wrap; gap: var(--space-3) var(--space-5); align-items: center; justify-content: space-between; }
.po-head h1 { font: 600 22px/30px var(--font-mono); word-break: break-all; }
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: var(--space-3) var(--space-5); margin-top: var(--space-4); }
.kv .k { font: var(--overline); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-muted); }
.kv .v { font: var(--body-sm); word-break: break-word; }
.edit-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: var(--space-4); }
.sj-alert { margin-bottom: var(--space-3); }
.sj-alert__icon { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.sj-alert .row { display: inline-flex; margin-right: var(--space-3); }
.timeline { list-style: none; border-left: 2px solid var(--border); margin-left: var(--space-2); }
.timeline li { padding: 0 0 var(--space-4) var(--space-4); position: relative; }
.timeline li::before { content: ""; position: absolute; left: -7px; top: 4px; width: 10px; height: 10px; border-radius: 50%; background: var(--ink-muted); border: 2px solid var(--surface); }
.timeline li.artwork::before { background: var(--info); } .timeline li.coa::before { background: var(--warning); }
.timeline li.manual::before { background: var(--ink-subtle); border-radius: 2px; }
.timeline .when { font: var(--caption); color: var(--ink-muted); }
.timeline .what { font-weight: 600; }
.timeline .notes { font: var(--body-sm); color: var(--ink-muted); margin-top: var(--space-1); white-space: pre-wrap; word-break: break-word; }
.pager { display: flex; gap: var(--space-3); align-items: center; justify-content: flex-end; margin-top: var(--space-3); font: var(--caption); font-size: 13px; color: var(--ink-muted); }

/* PO tables on tablets and phones: rows become cards */
@media (max-width: 700px) {
  .tbl-cards thead { display: none; }
  .tbl-cards, .tbl-cards tbody, .tbl-cards tr, .tbl-cards td { display: block; width: 100%; }
  .tbl-cards tr { border: 1px solid var(--border); border-radius: var(--radius-md); margin-bottom: var(--space-3); padding: var(--space-2) var(--space-3); background: var(--surface); }
  .tbl-cards td { border: none; padding: var(--space-1) 0; display: flex; justify-content: space-between; gap: var(--space-3); text-align: right; }
  .tbl-cards td::before { content: attr(data-label); color: var(--ink-muted); font: var(--label); text-align: left; flex-shrink: 0; }
  .tbl-cards td.num { text-align: right; }
  .hbars .row-bar { grid-template-columns: 96px 1fr 48px; }
  .sj-stat__value { font-size: 24px; line-height: 32px; } .sj-stat--hero .sj-stat__value { font-size: 32px; line-height: 40px; }
  main { margin-top: var(--space-3); }
}

/* Sales-order tags on the PO page */
.so-list { list-style: none; margin: 0; padding: 0; }
.so-item { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); padding: var(--space-3); border: 1px solid var(--border); border-radius: var(--radius-md); margin-bottom: var(--space-2); background: var(--surface-sunken); }
.so-item .note { font: var(--caption); color: var(--ink-muted); margin-top: var(--space-1); }
.note.warnline { color: var(--warning); font-weight: 600; }

/* ── Follow-up sheet: the table, header and modal follow the system; the cell-state colours are the --fu-* variables ── */
body[data-page=followup] main { max-width: none; }
.fu-wrap { overflow: auto; max-height: 72vh; border: 1px solid var(--border); border-radius: var(--radius-md); }
.fu-table { border-collapse: collapse; width: 100%; font: var(--body-sm); font-size: 13px; background: var(--surface); }
.fu-table th, .fu-table td { border: 1px solid var(--border); padding: var(--space-1) var(--space-2); text-align: center; white-space: nowrap; }
.fu-table thead th { position: sticky; top: 0; z-index: 1; background: var(--surface-sunken); color: var(--ink-muted); font: var(--label); letter-spacing: .01em; }
.fu-table td.num { text-align: right; font: var(--data); font-variant-numeric: tabular-nums; }
.fu-table .fu-party { background: var(--surface-sunken); font-weight: 600; vertical-align: middle; color: var(--ink); white-space: normal; min-width: 76px; }
.fu-table .fu-brand { text-align: left; font-weight: 600; background: var(--surface); white-space: normal; min-width: 150px; }
.fu-table .fu-order { text-align: left; white-space: normal; min-width: 84px; }
.fu-table .fu-order .code { font: var(--data); }
.fu-table td.fu { font-weight: 600; min-width: 84px; max-width: 128px; white-space: normal; line-height: 1.2; }
.fu-table td.fu[role=button] { cursor: pointer; }
.fu-table td.fu[role=button]:hover, .fu-table td.fu[role=button]:focus-visible { outline: 2px solid var(--fu-focus); outline-offset: -2px; }
.fu-table td.fu-flagcell { min-width: 74px; }
.fu-table tr.fu-removed td.fu-brand { background: var(--surface-sunken); color: var(--ink-muted); }
.fu-sub { font: var(--caption); font-weight: 400; color: var(--ink-muted); margin-top: 2px; }
.st-TO_ORDER { background: var(--fu-red); color: var(--fu-red-text); }
.st-ORDERED { background: var(--fu-orange); color: var(--fu-orange-text); }
.st-ARTWORK { background: var(--fu-blue); color: var(--fu-blue-text); }
.st-RECEIVED, .st-AVAILABLE, .st-NA, .fu-ok { background: var(--fu-green); color: var(--fu-green-text); }
.fu-table td.overdue { box-shadow: inset 0 0 0 3px var(--fu-red); }
.fu-table td.overdue::after { content: "Overdue"; display: block; font: 600 11px/14px var(--font-sans); }
.fu-totals { margin-bottom: var(--space-3); }
.fu-chip { padding: 2px var(--space-3); border-radius: var(--radius-pill); font: 600 13px/18px var(--font-sans); }
.fu-chip.fu-warn { background: var(--warning-tint); color: var(--warning); border: 1px solid var(--warning); }
.fu-legend { margin-top: var(--space-3); font: var(--caption); font-size: 13px; color: var(--ink-muted); gap: var(--space-4); }
.fu-dot { display: inline-block; width: 12px; height: 12px; border-radius: 2px; vertical-align: -1px; border: 1px solid var(--border-strong); }
.fu-sync { margin: 0 0 var(--space-2); gap: var(--space-3); font: var(--caption); font-size: 13px; color: var(--ink-muted); }
.fu-modal { position: fixed; inset: 0; background: color-mix(in srgb, var(--brand-ink) 45%, transparent); display: flex; align-items: center; justify-content: center; z-index: 50; padding: var(--space-4); }
.fu-card { background: var(--surface-raised); color: var(--ink); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: var(--space-5); max-width: 640px; width: 100%; max-height: 85vh; overflow: auto; box-shadow: var(--shadow-md); }
.sc-opts { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); margin: var(--space-2) 0; }
.sc-opt { display: flex; gap: var(--space-2); align-items: center; font: var(--body-sm); }

/* Tag picker */
.pk-card { max-width: 720px; }
.pk-card input[type=text] { width: 100%; margin: var(--space-1) 0 var(--space-3); }
.pk-orders { display: flex; flex-direction: column; gap: var(--space-2); max-height: 210px; overflow: auto; margin-bottom: var(--space-3); }
.pk-order { text-align: left; background: var(--surface-sunken); border: 1px solid var(--border); border-radius: var(--radius-md); padding: var(--space-2) var(--space-3); cursor: pointer; font: var(--body-sm); color: var(--ink); transition: background-color var(--motion-fast); }
.pk-order:hover { background: var(--brand-red-tint); }
.pk-lines { max-height: 240px; overflow: auto; border-top: 1px solid var(--border); padding-top: var(--space-2); }
.pk-h { font: var(--label); margin: 0 0 var(--space-2); }
.pk-line { display: flex; gap: var(--space-2); align-items: flex-start; padding: var(--space-2) var(--space-1); font: var(--body-sm); border-bottom: 1px solid var(--border); }
.pk-line.taken { opacity: .55; }

/* Small layout helpers */
.mb-0 { margin-bottom: 0; } .mb-2 { margin-bottom: var(--space-2); } .ml-2 { margin-left: var(--space-2); }
.maxw-240 { max-width: 240px; } .nogrow { flex: 0 0 auto; }

/* ── Sign-in page ─────────────────────────────────────────────────────────────────────── */
body.login { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: var(--space-4); }
.login-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: var(--space-6); width: 100%; max-width: 400px; margin: 0; }
.login-logo { margin-bottom: var(--space-5); }
.login-logo .sj-header__logo { height: 28px; }
.login-box .sj-alert { margin-bottom: var(--space-4); }
.login-form { display: flex; flex-direction: column; gap: var(--space-4); }
.login-form .sj-btn { width: 100%; }
.login-dev { width: 100%; margin-top: var(--space-3); border-style: dashed; }

/* ── Admin settings ───────────────────────────────────────────────────────────────────── */
.group { margin-bottom: var(--space-5); }
.group > h3 { font: var(--overline); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-muted); margin-bottom: var(--space-2); }
.t-row { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3); border: 1px solid var(--border); border-radius: var(--radius-md); margin-bottom: var(--space-2); background: var(--surface-sunken); }
.t-row label { flex: 1; font: var(--body-sm); }
.t-row input[type=number] { width: 90px; flex-shrink: 0; text-align: center; font: var(--data); }
.add-form { display: flex; gap: var(--space-2); flex-wrap: wrap; margin-top: var(--space-3); align-items: flex-end; }
.add-form .f { flex: 1; min-width: 130px; }
.save-bar { margin-top: var(--space-3); display: flex; gap: var(--space-2); }
.admin .tbl-wrap { margin-bottom: var(--space-2); }

/* ── Chat ─────────────────────────────────────────────────────────────────────────────── */
body.chat { height: 100vh; overflow: hidden; }
body.chat #app { display: flex; flex-direction: column; height: 100vh; max-width: none; margin: 0; padding: 0; }
body.chat .sj-header { flex-shrink: 0; position: static; }
#chat-wrap { flex: 1; overflow: hidden; display: flex; flex-direction: column; padding: var(--space-4) 0 0; width: 100%; max-width: 960px; margin: 0 auto; }
#messages { flex: 1; overflow-y: auto; padding: 0 var(--space-4) var(--space-2); display: flex; flex-direction: column; gap: var(--space-3); }
#suggestions { display: flex; flex-wrap: wrap; gap: var(--space-2); padding: var(--space-2) var(--space-4); flex-shrink: 0; }
.chip { height: auto; min-height: 28px; white-space: normal; text-align: left; }
#input-bar { display: flex; gap: var(--space-2); padding: var(--space-3) var(--space-4) var(--space-4); background: var(--surface); border-top: 1px solid var(--border); flex-shrink: 0; align-items: flex-end; }
#msg-input { flex: 1; max-height: 100px; overflow-y: auto; resize: none; min-height: 36px; }
.msg { display: flex; flex-direction: column; max-width: 80%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }
.bubble { padding: var(--space-3) var(--space-4); border-radius: var(--radius-lg); line-height: 1.55; word-break: break-word; }
.msg.user .bubble { background: var(--action); color: var(--on-action); border-bottom-right-radius: var(--radius-sm); }
.msg.assistant .bubble { background: var(--surface); color: var(--ink); border: 1px solid var(--border); border-bottom-left-radius: var(--radius-sm); }
.msg-time { font: var(--caption); color: var(--ink-muted); margin-top: var(--space-1); padding: 0 var(--space-1); }
.bubble ul { margin: var(--space-2) 0 var(--space-2) var(--space-5); }
.bubble li { margin-bottom: var(--space-1); }
.bubble p { margin-bottom: var(--space-2); }
.bubble p:last-child { margin-bottom: 0; }
.bubble.typing { color: var(--ink-muted); }
@media (max-width: 600px) {
  .msg { max-width: 92%; }
  #suggestions { padding: var(--space-2) var(--space-3); }
  #input-bar { padding: var(--space-2) var(--space-3) var(--space-3); }
}
