/* ==========================================================================
   Tribastion CRM — enterprise modules.

   Loaded after app.css, which owns the design tokens. Nothing here defines a
   colour of its own: every value resolves through a token, so both themes and
   the admin typography setting keep working without a second definition.

   Three groups: chart anatomy, interactive tables, and the furniture the new
   modules need (proposals, invoices, resources, exceptions).
   ========================================================================== */

/* ==========================================================================
   CHARTS
   The palette lives in charts.js — it is validated per theme and a chart must
   be REDRAWN, not filtered, when the theme changes. What lives here is the
   anatomy around the marks: recessive axes, tabular figures, and the legend and
   table affordances the palette validation is passed on.
   ========================================================================== */

.chart-card { display: flex; flex-direction: column; gap: .35rem; }
.chart-head { display: flex; align-items: baseline; justify-content: space-between; gap: .75rem; flex-wrap: wrap; }
.chart-head h3 { margin: 0; font-size: .95rem; font-weight: 700; letter-spacing: -.01em; }
.chart-head .sub { font-size: .76rem; color: var(--text-dim); }
.chart-actions { display: flex; align-items: center; gap: .3rem; }

.chart-svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart-plot { min-height: 40px; }
.chart-empty { padding: 1.6rem .5rem; text-align: center; color: var(--text-faint); font-size: .82rem; }

.chart-axis-label { fill: var(--text-faint); font-size: 10.5px; font-family: var(--sans); }
.chart-seg { transition: opacity .12s; }
.chart-seg:hover { opacity: .82; }
.chart-hit { cursor: default; }

/* Donut ------------------------------------------------------------------- */
.chart-donut-wrap { position: relative; max-width: 220px; margin: 0 auto; }
.chart-donut-centre {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; pointer-events: none; text-align: center;
}
.chart-donut-centre strong {
  font-family: var(--display); font-size: 1.22rem; font-weight: 700;
  letter-spacing: -.02em; font-variant-numeric: tabular-nums;
}
.chart-donut-centre span { font-size: .68rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: .06em; }

/* Bars -------------------------------------------------------------------- */
.chart-bars { display: flex; flex-direction: column; gap: .3rem; }
.chart-bar-row {
  display: grid; grid-template-columns: minmax(90px, 30%) 1fr auto;
  align-items: center; gap: .6rem; padding: .18rem .25rem; border-radius: var(--radius-xs);
}
.chart-bar-row.is-clickable { cursor: pointer; }
.chart-bar-row:hover { background: var(--surface-2); }
.chart-bar-label {
  font-size: .78rem; color: var(--text-dim);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chart-bar-track {
  display: flex; height: 12px; border-radius: 4px; background: var(--surface-2);
  overflow: hidden; min-width: 30px;
}
.chart-bar-fill { display: block; height: 100%; border-radius: 4px; }

/* A surface-coloured gap between stacked fills. This is not decoration: it is
   the secondary encoding the colour-vision check is passed on, and without it
   two adjacent hues read as one block to a reader who cannot separate them. */
.chart-stack-seg { display: block; height: 100%; border-right: 2px solid var(--surface); }
.chart-stack-seg:last-child { border-right: 0; }

.chart-bar-value {
  font-size: .78rem; font-weight: 600; font-variant-numeric: tabular-nums;
  white-space: nowrap; color: var(--text);
}

/* Funnel ------------------------------------------------------------------ */
.chart-funnel { display: flex; flex-direction: column; gap: .55rem; }
.chart-funnel-step { display: flex; flex-direction: column; gap: .22rem; padding: .1rem 0; }
.chart-funnel-step.is-clickable { cursor: pointer; }
.chart-funnel-head { display: flex; justify-content: space-between; align-items: baseline; gap: .6rem; }
.chart-funnel-head span { font-size: .8rem; font-weight: 600; }
.chart-funnel-head strong { font-size: .82rem; font-variant-numeric: tabular-nums; }
.chart-funnel-bar { height: 14px; border-radius: 5px; transition: filter .12s; }
.chart-funnel-step:hover .chart-funnel-bar { filter: brightness(1.08); }
.chart-funnel-foot { font-size: .71rem; color: var(--text-faint); }

/* Sparkline --------------------------------------------------------------- */
.chart-spark { width: 118px; height: 28px; display: block; }

/* Legend — always present for two or more series, so identity is never
   carried by colour alone. */
.chart-legend {
  list-style: none; margin: .7rem 0 0; padding: 0;
  display: flex; flex-wrap: wrap; gap: .18rem .9rem;
}
.chart-legend li { display: flex; align-items: center; gap: .38rem; font-size: .755rem; color: var(--text-dim); }
.chart-legend li.is-clickable { cursor: pointer; }
.chart-legend li.is-clickable:hover { color: var(--text); }
.chart-legend i { width: 9px; height: 9px; border-radius: 2.5px; flex: none; }
.chart-legend b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

/* Tooltip ----------------------------------------------------------------- */
.chart-tip {
  position: fixed; z-index: 90; pointer-events: none;
  background: var(--elev); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); box-shadow: var(--shadow-lg);
  padding: .42rem .6rem; font-size: .76rem; line-height: 1.35; max-width: 260px;
  display: flex; flex-direction: column; gap: .1rem;
}
.chart-tip strong { font-weight: 600; }
.chart-tip span { color: var(--text-dim); font-variant-numeric: tabular-nums; }
.chart-tip em { font-style: normal; color: var(--text-faint); font-size: .72rem; }

/* Table view -------------------------------------------------------------- */
.chart-table-wrap { max-height: 260px; overflow: auto; margin-top: .3rem; }
.chart-table { width: 100%; border-collapse: collapse; font-size: .78rem; }
.chart-table th, .chart-table td { padding: .3rem .45rem; border-bottom: 1px solid var(--border); text-align: left; }
.chart-table th { color: var(--text-dim); font-weight: 600; position: sticky; top: 0; background: var(--surface); }
.chart-table .num { text-align: right; font-variant-numeric: tabular-nums; }

@media (prefers-reduced-motion: reduce) {
  .chart-seg, .chart-funnel-bar, .chart-bar-row { transition: none; }
}

/* ==========================================================================
   INTERACTIVE TABLES
   ========================================================================== */

.table-toolbar {
  display: flex; align-items: center; gap: .5rem; flex-wrap: wrap;
  padding: .6rem .75rem; border-bottom: 1px solid var(--border);
}
.table-toolbar .grow { flex: 1 1 180px; }
.table-search {
  width: 100%; padding: .38rem .6rem .38rem 1.9rem; font-size: .82rem;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text);
}
.table-search:focus { outline: none; border-color: var(--brand); box-shadow: var(--ring); }
.table-count { font-size: .74rem; color: var(--text-faint); font-variant-numeric: tabular-nums; white-space: nowrap; }

.chip-filter {
  border: 1px solid var(--border); background: var(--surface); color: var(--text-dim);
  border-radius: 999px; padding: .2rem .62rem; font-size: .74rem; cursor: pointer;
  display: inline-flex; align-items: center; gap: .3rem; font-family: inherit;
}
.chip-filter:hover { border-color: var(--border-strong); color: var(--text); }
.chip-filter.is-on { background: var(--brand-tint); border-color: var(--brand); color: var(--brand-600); font-weight: 600; }
.chip-filter b { font-variant-numeric: tabular-nums; }

th.is-sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.is-sortable:hover { background: var(--brand); }
/* .3 was tuned against a pale grey header. On the magenta band a white glyph
   at .3 disappears, and an affordance nobody can see is not an affordance. */
th.is-sortable::after { content: "\2195"; opacity: .55; margin-left: .3rem; font-size: .78em; }
/* On the magenta band the arrow and the focus ring have to be white; in
   var(--brand) they were pink on magenta and effectively invisible. */
th.is-sortable.is-asc::after { content: "\2191"; opacity: 1; color: var(--th-fg); }
th.is-sortable.is-desc::after { content: "\2193"; opacity: 1; color: var(--th-fg); }
th.is-sortable:focus-visible { outline: 2px solid var(--th-fg); outline-offset: -2px; }

/* ==========================================================================
   MODULE FURNITURE
   ========================================================================== */

/* Severity is a RESERVED status scale. It never borrows a categorical chart
   hue, and it always ships with a word — colour alone is not a label. */
.sev {
  display: inline-flex; align-items: center; gap: .3rem; font-size: .7rem;
  font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  padding: .12rem .45rem; border-radius: 999px; border: 1px solid; white-space: nowrap;
}
/* .sev-* live in app.css. They were redefined here against different tokens
   (--bad/--warn/--info instead of --crit/--high/--med), and since this file
   wins by load order a MEDIUM severity was rendering in the INFO colour — blue
   for something that means amber. Severity colour is semantic; it gets one
   definition. */
.sev-waived { color: var(--text-faint); border-color: var(--border-strong); background: var(--surface-2); }

.exc-row td { vertical-align: top; }
.exc-consequence { font-size: .74rem; color: var(--text-faint); font-style: italic; margin-top: .12rem; }
.exc-domain-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(158px, 1fr)); gap: .6rem; }
.exc-domain {
  display: block; text-decoration: none; color: inherit; padding: .65rem .75rem;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface);
  transition: border-color .12s, transform .12s;
}
.exc-domain:hover { border-color: var(--brand); transform: translateY(-1px); }
.exc-domain.is-clear { opacity: .62; }
.exc-domain .n { font-family: var(--display); font-size: 1.5rem; font-weight: 700; line-height: 1.05; font-variant-numeric: tabular-nums; }
.exc-domain .l { font-size: .74rem; color: var(--text-dim); }
.exc-domain .c { font-size: .7rem; color: var(--bad); font-weight: 600; }

/* The four numbers a commercial page opens with. */
.fact-strip {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 1px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; background: var(--border);
}
/* EVERY direct child, not just div.
   The strip draws its hairlines by painting --border on the container and
   letting each cell paint --surface back over it, leaving the 1px gap showing.
   A cell that never repaints therefore comes out as a slab of border colour
   with no padding — which is what happened on Clients and Resources, where the
   figures are <a> filter links rather than plain divs. Matching the element
   name meant the styling silently depended on a cell not being clickable. */
.fact-strip > * { background: var(--surface); padding: .6rem .75rem; }
/* Clickable figures read as figures, not as links; the hover on .k below is
   what signals they can be followed. Here rather than as an inline style on
   every anchor, so a new one cannot be added without it. */
.fact-strip > a { text-decoration: none; color: inherit; display: block; }
.fact-strip .k { font-size: .69rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-faint); }
.fact-strip .v { font-family: var(--display); font-size: 1.16rem; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.fact-strip .v.is-bad { color: var(--bad); }
/* A gap worth acting on, and the tile you are currently filtered to. Kept beside
   .is-bad so the three states of a strip figure are defined in one place. */
.fact-strip .v.is-warn { color: var(--warn); }
.fact-strip .v.is-brand { color: var(--brand-700); }
.fact-strip a:hover .k { color: var(--brand-700); }
.fact-strip .v.is-ok { color: var(--ok); }
.fact-strip .n { font-size: .71rem; color: var(--text-dim); }

/* ==========================================================================
   THE ONE-SCREEN PROPOSAL BUILDER
   Inputs on the left, the deck's contents on the right, kept in view.
   ========================================================================== */
.pb-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 1.2rem; align-items: start; }
@media (max-width: 1100px) { .pb-layout { grid-template-columns: 1fr; } .pb-preview { position: static; } }
.pb-main { min-width: 0; }
.pb-preview { position: sticky; top: 4.6rem; }

.pb-services { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: .45rem; }
.pb-cat {
  grid-column: 1 / -1; margin-top: .35rem;
  font-size: .68rem; text-transform: uppercase; letter-spacing: .07em; color: var(--text-faint); font-weight: 700;
}
.pb-cat:first-child { margin-top: 0; }
.pb-service {
  display: flex; align-items: center; gap: .6rem; cursor: pointer;
  padding: .55rem .7rem; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); transition: border-color .12s, background .12s;
}
.pb-service:hover { border-color: var(--brand); }
.pb-service:has(input:checked) { border-color: var(--brand); background: var(--brand-tint); }
.pb-service input { flex: none; }
.pb-service-name { flex: 1; min-width: 0; font-size: .86rem; font-weight: 600; }
.pb-service-price { flex: none; font-variant-numeric: tabular-nums; font-size: .84rem; }

#pb-lines input[type=text], #pb-lines input[type=number] { width: 100%; padding: .35rem .5rem; font-size: .84rem; }
#pb-lines input[type=number] { text-align: right; }
.pb-line-total { font-variant-numeric: tabular-nums; font-weight: 600; }
.pb-remove {
  width: 26px; height: 26px; padding: 0; border: 1px solid transparent; border-radius: 6px;
  background: transparent; color: var(--text-faint); cursor: pointer; font-size: 1rem; line-height: 1;
}
.pb-remove:hover { color: var(--bad); border-color: var(--border); background: var(--surface-2); }

/* The deck, as a card. The same band and deep brand as the real slides and the
   email shell, so the preview looks like the thing it previews. */
.pb-deck { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); box-shadow: var(--shadow-sm); }
.pb-deck-band { display: flex; justify-content: space-between; align-items: baseline; padding: .7rem 1rem; background: var(--brand-700); color: #fff; }
.pb-deck-brand { font-family: var(--display); font-weight: 700; letter-spacing: -.01em; }
.pb-deck-kicker { font-size: .68rem; text-transform: uppercase; letter-spacing: .1em; opacity: .85; }
.pb-deck-body { padding: 1rem; }
.pb-deck-title { font-family: var(--display); font-size: 1.05rem; font-weight: 700; line-height: 1.3; letter-spacing: -.01em; }
.pb-deck-client { font-size: .82rem; margin: .15rem 0 .9rem; }
.pb-deck-lines { width: 100%; border-collapse: collapse; font-size: .82rem; }
.pb-deck-lines td { padding: .35rem 0; border-bottom: 1px solid var(--border); vertical-align: top; }
.pb-deck-lines td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; padding-left: .6rem; }
.pb-deck-empty td { border-bottom: none; font-size: .8rem; }
.pb-deck-totals { margin-top: .6rem; font-size: .84rem; }
.pb-deck-totals > div { display: flex; justify-content: space-between; padding: .25rem 0; }
.pb-deck-totals strong { font-variant-numeric: tabular-nums; }
.pb-deck-grand { border-top: 2px solid var(--text); margin-top: .2rem; padding-top: .45rem !important; font-size: .95rem; }
.pb-deck-foot { font-size: .74rem; margin-top: .8rem; padding-top: .6rem; border-top: 1px solid var(--border); }

/* A nudge, not an error page: the form was submitted with nothing in it. */
@keyframes pb-shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
.card.is-shake { animation: pb-shake .3s ease-in-out; border-color: var(--bad); }
@media (prefers-reduced-motion: reduce) { .card.is-shake { animation: none; } }

/* ==========================================================================
   ARRANGE THE SIDEBAR
   Groups and items as draggable rows. The handle is the affordance; the
   arrows are the always-works alternative.
   ========================================================================== */
.sl-groups, .sl-items { list-style: none; margin: 0; }
.sl-groups { padding: 0; display: flex; flex-direction: column; gap: .7rem; }
.sl-group {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
  box-shadow: var(--shadow-sm); overflow: hidden;
}
.sl-group-head {
  display: flex; align-items: center; gap: .6rem; padding: .6rem .8rem;
  background: var(--surface-2); border-bottom: 1px solid var(--border); cursor: grab;
}
.sl-group-name { flex: none; font-family: var(--display); font-size: .92rem; }
.sl-items { padding: .35rem .5rem .5rem 2.2rem; display: flex; flex-direction: column; gap: .2rem; }
.sl-item {
  display: flex; align-items: center; gap: .6rem; padding: .4rem .6rem;
  border: 1px solid transparent; border-radius: var(--radius-sm); cursor: grab; font-size: .86rem;
}
.sl-item:hover { border-color: var(--border); background: var(--surface-2); }
.sl-item-name { flex: 1; min-width: 0; }
.sl-handle { flex: none; color: var(--text-faint); letter-spacing: -.15em; font-size: .9rem; user-select: none; }
.sl-btns { margin-left: auto; flex: none; display: inline-flex; gap: .15rem; }
.sl-btns button {
  width: 26px; height: 26px; padding: 0; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text-dim); cursor: pointer; font-size: .85rem; line-height: 1;
}
.sl-btns button:hover { border-color: var(--brand); color: var(--brand); }
.sl-btns button:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
.is-dragging { opacity: .45; }
.sl-group.is-dragging .sl-group-head, .sl-item.is-dragging { border-color: var(--brand); }

/* The ageing ladder is ORDINAL: the buckets have an order and the reader
   should see it in the colour, so it steps one direction rather than picking
   an unrelated hue per bucket. */
.age-ladder { display: grid; grid-template-columns: repeat(5, 1fr); gap: 1px; border-radius: var(--radius-sm); overflow: hidden; background: var(--border); }
.age-cell { padding: .5rem .55rem; text-decoration: none; color: inherit; display: block; }
.age-cell .k { font-size: .66rem; text-transform: uppercase; letter-spacing: .05em; opacity: .85; }
.age-cell .v { font-weight: 700; font-variant-numeric: tabular-nums; font-size: .92rem; }
.age-cell .n { font-size: .68rem; opacity: .8; }
.age-0 { background: color-mix(in srgb, var(--ok) 12%, var(--surface)); }
.age-1 { background: color-mix(in srgb, var(--med) 14%, var(--surface)); }
.age-2 { background: color-mix(in srgb, var(--warn) 16%, var(--surface)); }
.age-3 { background: color-mix(in srgb, var(--warn) 26%, var(--surface)); }
.age-4 { background: color-mix(in srgb, var(--bad) 24%, var(--surface)); }

/* Utilisation meter. The target is drawn as a line rather than implied by a
   colour change, so the reader can see how far off it is, not just that it is. */
.util-meter { position: relative; height: 16px; border-radius: 4px; background: var(--surface-2); overflow: visible; min-width: 90px; }
.util-meter > i { display: block; height: 100%; background: var(--brand); border-radius: 4px; }
.util-meter > i.under { background: var(--warn); }
.util-meter > i.over { background: var(--bad); }
.util-meter > b { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text-dim); opacity: .8; }
.util-legend { font-size: .68rem; color: var(--text-faint); }

/* Activity timeline. */
.tl { list-style: none; margin: 0; padding: 0; position: relative; }
.tl::before { content: ""; position: absolute; left: 13px; top: 6px; bottom: 6px; width: 1px; background: var(--border); }
.tl li { position: relative; padding: .45rem 0 .45rem 2.1rem; }
.tl .dot {
  position: absolute; left: 5px; top: .55rem; width: 17px; height: 17px; border-radius: 50%;
  background: var(--surface); border: 1px solid var(--border-strong);
  display: grid; place-items: center; color: var(--text-dim);
}
.tl li.is-open .dot { border-color: var(--brand); color: var(--brand); background: var(--brand-tint); }
.tl li.is-late .dot { border-color: var(--bad); color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, transparent); }
.tl .tl-head { display: flex; justify-content: space-between; gap: .6rem; align-items: baseline; }
.tl .tl-title { font-size: .84rem; font-weight: 600; }
.tl .tl-when { font-size: .72rem; color: var(--text-faint); white-space: nowrap; }
.tl .tl-body { font-size: .78rem; color: var(--text-dim); margin-top: .1rem; }

/* Line-item editors for proposals and invoices. */
.line-grid { width: 100%; border-collapse: collapse; }
.line-grid th { font-size: .7rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-faint); text-align: left; padding: .3rem .4rem; background: transparent; }
.line-grid td { padding: .3rem .4rem; border-top: 1px solid var(--border); vertical-align: middle; }
.line-grid input, .line-grid select { width: 100%; padding: .3rem .4rem; font-size: .8rem; }
.line-grid td.num, .line-grid th.num { text-align: right; }
.line-grid td.num input { text-align: right; }
.line-total { font-weight: 700; font-variant-numeric: tabular-nums; }

.margin-good { color: var(--ok); font-weight: 600; }
.margin-thin { color: var(--warn); font-weight: 600; }
.margin-bad { color: var(--bad); font-weight: 700; }

/* The trigger shown beside each approval level in the workflow editor. */
.trigger-note {
  font-size: .72rem; color: var(--text-dim); background: var(--surface-2);
  border: 1px dashed var(--border-strong); border-radius: var(--radius-xs);
  padding: .12rem .4rem; display: inline-block;
}

/* A status pill for the document lifecycles (proposal, invoice, onboarding). */
.doc-status { font-size: .72rem; font-weight: 600; padding: .12rem .5rem; border-radius: 999px; border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text-dim); white-space: nowrap; }
.doc-draft { }
.doc-pending { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); background: color-mix(in srgb, var(--warn) 10%, transparent); }
.doc-live { color: var(--info); border-color: color-mix(in srgb, var(--info) 40%, transparent); background: color-mix(in srgb, var(--info) 10%, transparent); }
.doc-good { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); background: color-mix(in srgb, var(--ok) 10%, transparent); }
.doc-bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); background: color-mix(in srgb, var(--bad) 10%, transparent); }

/* Two-column module layout used by the detail pages. */
.split { display: grid; grid-template-columns: minmax(0, 2fr) minmax(240px, 1fr); gap: 1rem; align-items: start; }
@media (max-width: 900px) { .split { grid-template-columns: 1fr; } }

/* .grid-2 and .grid-3 live in app.css and nowhere else.
   They were redefined here with auto-fit minmax, and because this file loads
   after app.css a later non-media rule beat app.css's earlier media queries —
   so the responsive collapse at 1100px and 720px never applied anywhere on the
   platform. One definition, one set of breakpoints. */

/* Nav group headings. The sidebar carries around 25 entries now, and a flat
 column of 25 is a list nobody reads past the first six. */
.nav-group-label {
  font-size: .64rem; text-transform: uppercase; letter-spacing: .09em; font-weight: 700;
  color: var(--text-faint); padding: .85rem .95rem .25rem; user-select: none;
}
.nav-group-label:first-child { padding-top: .2rem; }
