:root {
  --bg: #f5f6f8; --panel: #ffffff; --text: #18202b; --muted: #667085; --line: #e4e7ec; --soft: #f2f4f7;
  --brand: #0f62fe; --brand-ink: #ffffff; --brand-soft: #e8f0ff;
  --ok: #067647; --ok-soft: #e7f6ee; --warn: #b54708; --warn-soft: #fef4e6; --bad: #b42318; --bad-soft: #fdecea;
  --info: #175cd3; --info-soft: #eaf2fd; --side: #101828; --side-text: #cfd4dc; --side-active: #1d2939;
  --radius: 8px; --shadow: 0 1px 2px rgba(16, 24, 40, .06);
  --bar: #0f62fe; --bar2: #98a2b3;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c111d; --panel: #161b26; --text: #e6e9ee; --muted: #94a0b4; --line: #283041; --soft: #1d2433;
    --brand: #4d8bff; --brand-ink: #fff; --brand-soft: #1a2a4a;
    --ok: #47cd89; --ok-soft: #0f2a1d; --warn: #f7b155; --warn-soft: #33240f; --bad: #f97066; --bad-soft: #3a1512;
    --info: #84adff; --info-soft: #172544; --side: #0a0e17; --side-text: #aab3c2; --side-active: #1b2232;
    --bar: #4d8bff; --bar2: #5b667a;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Helvetica, Arial, sans-serif;
       background: var(--bg); color: var(--text); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }
code { background: var(--soft); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }

/* layout */
.shell { display: grid; grid-template-columns: 228px 1fr; min-height: 100vh; }
.side { background: var(--side); color: var(--side-text); padding: 18px 12px; position: sticky; top: 0; height: 100vh;
        overflow-y: auto; display: flex; flex-direction: column; }
.brand { color: #fff; font-weight: 700; font-size: 16px; padding: 4px 10px 2px; display: flex; gap: 8px; align-items: center; }
.brand-sub { font-size: 12px; color: var(--side-text); padding: 0 10px 18px; opacity: .8; }
.side nav a { display: flex; align-items: center; gap: 10px; color: var(--side-text); padding: 7px 10px; border-radius: 6px;
              font-size: 13.5px; }
.side nav a:hover { background: var(--side-active); text-decoration: none; color: #fff; }
.side nav a.active { background: var(--side-active); color: #fff; font-weight: 600; }
.side nav .sect { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; opacity: .55; padding: 14px 10px 4px; }
.side nav svg { width: 16px; height: 16px; flex: none; opacity: .85; }
.side .me { margin-top: auto; border-top: 1px solid #2a3344; padding: 12px 10px 0; font-size: 12.5px; }
.side .me b { color: #fff; display: block; }
.side .me form { display: inline; }
.side .me button { background: none; border: 0; color: var(--side-text); padding: 0; cursor: pointer; font: inherit; text-decoration: underline; }
.badge-count { margin-left: auto; background: var(--warn); color: #fff; border-radius: 10px; font-size: 11px; padding: 0 7px; font-weight: 600; }
.main { padding: 24px 28px 60px; min-width: 0; }
.head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.head .sub { color: var(--muted); margin-top: 4px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.main .menu-btn { display: none; }

/* panels */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
         padding: 18px; margin-bottom: 18px; }
.panel.flush { padding: 0; }
.panel.flush > h2 { padding: 16px 18px 0; }
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.panel-head h2 { margin: 0; }
.grid { display: grid; gap: 18px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g-side { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: 12.5px; }
.kpi .value { font-size: 22px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.kpi .hint { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.warn .value { color: var(--warn); }
.kpi.bad .value { color: var(--bad); }

/* tables */
.table-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; }
table.t th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 9px 12px;
             border-bottom: 1px solid var(--line); background: var(--soft); white-space: nowrap; }
table.t td { padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.t tr:last-child td { border-bottom: 0; }
table.t tbody tr:hover td { background: color-mix(in srgb, var(--soft) 60%, transparent); }
table.t tfoot td { font-weight: 650; border-top: 2px solid var(--line); background: var(--soft); }
table.t th.num, table.t td.num { text-align: right; }
.panel:not(.flush) table.t th:first-child, .panel:not(.flush) table.t td:first-child { padding-left: 0; }
.panel:not(.flush) table.t th { background: none; }
dl.facts { display: grid; grid-template-columns: 150px 1fr; gap: 6px 12px; margin: 0; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; overflow-wrap: anywhere; }

/* badges */
.badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; font-weight: 600; white-space: nowrap;
         background: var(--soft); color: var(--muted); }
.b-active { background: var(--ok-soft); color: var(--ok); }
.b-expiring { background: var(--warn-soft); color: var(--warn); }
.b-expired, .b-terminated, .b-failed { background: var(--bad-soft); color: var(--bad); }
.b-renewed, .b-sent { background: var(--info-soft); color: var(--info); }
.b-closed { background: var(--ok-soft); color: var(--ok); }
.days-bad { color: var(--bad); font-weight: 600; }
.days-warn { color: var(--warn); font-weight: 600; }

/* forms */
label { display: block; font-weight: 550; font-size: 13px; margin-bottom: 4px; }
.field { margin-bottom: 14px; }
.field .help { color: var(--muted); font-size: 12px; margin-top: 3px; font-weight: 400; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=month],
input[type=search], input[type=url], select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); color: var(--text);
  font: inherit; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--brand) 35%, transparent); border-color: var(--brand); }
textarea { min-height: 80px; resize: vertical; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 500; }
.check input { width: 16px; height: 16px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 18px; }
.form-grid .full { grid-column: 1 / -1; }
.form-grid.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.filters input, .filters select { width: auto; min-width: 140px; padding: 6px 9px; }
.filters input[type=search] { min-width: 220px; }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px; border-radius: 6px; border: 1px solid var(--line);
       background: var(--panel); color: var(--text); font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap; }
.btn:hover { text-decoration: none; background: var(--soft); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--line)); }
.btn.sm { padding: 3px 9px; font-size: 12.5px; }
.btn.link { border: 0; background: none; color: var(--brand); padding: 0; }
form.inline { display: inline; }

/* flash */
.flash { padding: 10px 14px; border-radius: 6px; margin-bottom: 14px; border: 1px solid transparent; }
.flash.ok { background: var(--ok-soft); color: var(--ok); }
.flash.error { background: var(--bad-soft); color: var(--bad); }
.flash.info { background: var(--info-soft); color: var(--info); }

/* charts */
.bars { display: flex; align-items: flex-end; gap: 6px; height: 180px; padding-top: 10px; }
.bars .col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; min-width: 0; }
.bars .stack { width: 100%; max-width: 38px; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; position: relative; }
.bars .bar { width: 100%; background: var(--bar); border-radius: 3px 3px 0 0; min-height: 1px; }
.bars .bar.cost { background: var(--bar2); position: absolute; bottom: 0; left: 25%; width: 50%; border-radius: 2px 2px 0 0; opacity: .9; }
.bars .lbl { font-size: 11px; color: var(--muted); white-space: nowrap; }
.bars .col.open .bar:not(.cost) { background: repeating-linear-gradient(45deg, var(--bar), var(--bar) 4px, color-mix(in srgb, var(--bar) 70%, transparent) 4px, color-mix(in srgb, var(--bar) 70%, transparent) 8px); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.meter { height: 6px; background: var(--soft); border-radius: 3px; overflow: hidden; min-width: 60px; }
.meter > span { display: block; height: 100%; background: var(--bar); }

/* contract line editor */
.lines-table input, .lines-table select { padding: 6px 7px; font-size: 13px; }
.lines-table td { padding: 6px 4px !important; vertical-align: middle !important; }
.lines-table .w-desc { min-width: 220px; }
.lines-table .w-num { width: 92px; }
.lines-table .w-sel { min-width: 120px; }
.lines-table .more td { border-bottom: 1px solid var(--line); background: var(--soft); }
.lines-table tr.line-main td { border-bottom: 0; }
.x { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 18px; line-height: 1; padding: 4px; }
.x:hover { color: var(--bad); }
.totals-bar { display: flex; gap: 22px; justify-content: flex-end; padding: 12px 4px 0; flex-wrap: wrap; }
.totals-bar b { font-variant-numeric: tabular-nums; }

/* auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 28px; box-shadow: var(--shadow); }
.auth h1 { margin-bottom: 4px; }
.auth .btn { width: 100%; justify-content: center; padding: 10px; margin-top: 6px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 18px; flex-wrap: wrap; }
.tabs a { padding: 8px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; font-weight: 550; }
.tabs a.on, .tabs a:hover { color: var(--text); border-bottom-color: var(--brand); text-decoration: none; }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; }
.pill-row { display: flex; gap: 6px; flex-wrap: wrap; }
.secret { font-family: ui-monospace, Menlo, monospace; font-size: 15px; background: var(--soft); padding: 6px 10px; border-radius: 6px; display: inline-block; user-select: all; }
.perm-matrix td, .perm-matrix th { text-align: center; }
.perm-matrix td:first-child, .perm-matrix th:first-child { text-align: left; }

@media (max-width: 960px) {
  .g2, .g3, .g-side { grid-template-columns: 1fr; }
  .form-grid.g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 780px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 auto 0 0; width: 240px; z-index: 20; transform: translateX(-100%); transition: transform .2s; }
  body.nav-open .side { transform: none; box-shadow: 0 0 0 100vmax rgba(0,0,0,.35); }
  .main .menu-btn { display: inline-flex; }
  .main { padding: 16px 16px 48px; }
  .form-grid, .form-grid.g4 { grid-template-columns: 1fr; }
  dl.facts { grid-template-columns: 1fr; gap: 0 0; }
  dl.facts dt { margin-top: 8px; }
}
@media print {
  .side, .actions, .filters, .menu-btn { display: none !important; }
  .shell { display: block; }
  .panel { box-shadow: none; break-inside: avoid; }
}

/* ---- TrackDesk additions ---- */
.b-won { background: var(--soft); color: var(--muted); }
.b-ordered { background: var(--info-soft); color: var(--info); }
.b-delivered { background: var(--warn-soft); color: var(--warn); }
.b-billed { background: var(--brand-soft); color: var(--brand); }
.b-paid, .b-renewed-ok { background: var(--ok-soft); color: var(--ok); }
.b-on_hold { background: var(--warn-soft); color: var(--warn); }
.b-cancelled { background: var(--bad-soft); color: var(--bad); }
.b-closed { background: var(--soft); color: var(--muted); }
.b-queued, .b-logged, .b-skipped { background: var(--soft); color: var(--muted); }
.b-recurring { background: var(--brand-soft); color: var(--brand); }
.b-non_recurring { background: var(--soft); color: var(--muted); }
.steps { display: flex; gap: 0; margin: 4px 0 18px; flex-wrap: wrap; }
.steps .st { flex: 1; min-width: 110px; padding: 9px 12px; background: var(--soft); color: var(--muted); font-size: 12.5px;
             font-weight: 600; position: relative; border-right: 2px solid var(--bg); }
.steps .st:first-child { border-radius: 6px 0 0 6px; }
.steps .st:last-child { border-radius: 0 6px 6px 0; border-right: 0; }
.steps .st.done { background: var(--ok-soft); color: var(--ok); }
.steps .st.now { background: var(--brand); color: var(--brand-ink); }
.steps .st small { display: block; font-weight: 400; opacity: .85; }
.board { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; align-items: start; }
.board .lane { background: var(--soft); border-radius: var(--radius); padding: 10px; min-height: 120px; }
.board .lane h3 { display: flex; justify-content: space-between; font-size: 13px; margin: 2px 4px 10px; }
.card-item { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 9px 11px; margin-bottom: 8px;
             box-shadow: var(--shadow); font-size: 13px; }
.card-item.late { border-left: 3px solid var(--bad); }
.card-item .t1 { font-weight: 600; overflow-wrap: anywhere; }
.card-item .t2 { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.card-item form { margin-top: 6px; display: flex; gap: 4px; }
.card-item form input { padding: 3px 6px; font-size: 12px; }
.section-title { display: flex; justify-content: space-between; align-items: center; margin: 0 0 12px; }
.section-title h2 { margin: 0; }
.locked-note { font-size: 12px; color: var(--muted); }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
fieldset[disabled] input, fieldset[disabled] select, fieldset[disabled] textarea { background: var(--soft); color: var(--muted); }
.grid-rr td, .grid-rr th { padding: 6px 8px !important; font-size: 12.5px; }
.grid-rr td.client { min-width: 220px; max-width: 320px; }
.grid-rr .lk { color: var(--muted); }
.ent input { width: 120px; text-align: right; padding: 5px 7px; }
.comment { border-bottom: 1px solid var(--line); padding: 10px 0; }
.comment:last-child { border-bottom: 0; }
.comment .who { font-size: 12.5px; color: var(--muted); }
.comment p { margin: 4px 0 0; white-space: pre-wrap; }
.trunc { max-width: 380px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.pre { white-space: pre-wrap; }
@media (max-width: 1100px) { .board { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 640px) { .board { grid-template-columns: 1fr; } }
.actions select, .actions input { width: auto; }
