/* ==========================================================================
   Smith Lambert — Collection Engine · Dashboard v2
   Low-saturation professional palette, generous whitespace, no gradients.
   ========================================================================== */
:root {
  --navy: #1B3A6B;
  --navy-dark: #12294d;
  --accent: #2f5fa3;
  --bg: #f4f6f9;
  --card: #ffffff;
  --border: #dfe4ec;
  --text: #232a35;
  --muted: #6b7484;
  --green: #1e7e46;
  --green-bg: #d6f0dd;
  --red: #b3261e;
  --red-bg: #fbdcdc;
  --amber: #9a6a00;
  --amber-bg: #fff3d6;
  --slate: #47556b;
  --slate-bg: #e2e8f2;
  --sidebar-w: 232px;
  --pdf-scale: 0.75;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
}
h1, h2, h3 { color: var(--navy); }
button {
  font: inherit; cursor: pointer; border-radius: 6px; border: 1px solid var(--border);
  background: #fff; color: var(--text); padding: 7px 14px;
}
button.primary { background: var(--navy); border-color: var(--navy); color: #fff; }
button.primary:hover { background: var(--navy-dark); }
button.ghost { background: transparent; color: #fff; border-color: rgba(255,255,255,.4); }
button.ghost-dark { background: transparent; color: var(--text); border-color: var(--border); }
button.danger { color: var(--red); border-color: var(--red); }
button.wide { width: 100%; }
button.small { padding: 3px 9px; font-size: 12px; border-radius: 5px; }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea {
  font: inherit; padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px;
  background: #fff; color: var(--text); max-width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { resize: vertical; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.spacer { flex: 1; }
.money { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

.fld { display: block; text-align: left; margin-bottom: 12px; }
.fld span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.fld input, .fld select, .fld textarea { width: 100%; }

/* ------------------------------- Login ------------------------------- */
#loginView { min-height: 100vh; display: flex; align-items: flex-start; justify-content: center; padding-top: 9vh; }
.login-card {
  width: min(400px, 94vw); background: var(--card); border: 1px solid var(--border);
  border-radius: 12px; padding: 28px 32px; text-align: center;
  box-shadow: 0 8px 30px rgba(18, 41, 77, .08);
}
.login-card img { height: 56px; margin-bottom: 12px; }
.login-card h2 { margin: 0 0 4px; }
.login-card > p { margin: 0 0 20px; }
.login-langrow { text-align: right; margin-bottom: 4px; }
.keyfallback { margin-top: 16px; text-align: left; border-top: 1px dashed var(--border); padding-top: 12px; }
.keyfallback summary { cursor: pointer; color: var(--muted); font-size: 13px; margin-bottom: 10px; }
.keyfallback input { width: 100%; margin-bottom: 8px; }
#loginError { color: var(--red); margin-top: 12px; min-height: 18px; }

/* ------------------------------- Shell ------------------------------- */
#appShell { display: flex; min-height: 100vh; }
#appShell[hidden] { display: none; }
#sidebar {
  width: var(--sidebar-w); flex: 0 0 var(--sidebar-w); background: var(--navy); color: #dbe4f2;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
#sidebar .brand { display: flex; gap: 10px; align-items: center; padding: 16px 16px 14px; border-bottom: 1px solid rgba(255,255,255,.12); }
#sidebar .brand img { height: 34px; background: #fff; border-radius: 5px; padding: 2px 5px; }
#sidebar .brand strong { display: block; color: #fff; font-size: 14px; }
#sidebar .brand span { font-size: 10.5px; opacity: .75; display: block; line-height: 1.25; }
#mainNav { padding: 10px 8px; display: flex; flex-direction: column; gap: 2px; flex: 1; }
#mainNav .nav-group { font-size: 10px; text-transform: uppercase; letter-spacing: .08em; opacity: .55; padding: 12px 10px 4px; }
.navbtn {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  background: transparent; border: 0; color: #dbe4f2; padding: 8px 10px; border-radius: 7px; font-weight: 500;
}
.navbtn:hover { background: rgba(255,255,255,.08); }
.navbtn.active { background: rgba(255,255,255,.16); color: #fff; font-weight: 600; }
.navbtn .dot { width: 7px; height: 7px; border-radius: 50%; background: rgba(255,255,255,.35); flex: 0 0 auto; }
.navbtn.active .dot { background: #fff; }
.sidebar-foot { padding: 12px 16px; border-top: 1px solid rgba(255,255,255,.12); color: #9fb0cc; }

#mainCol { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#topbar {
  background: var(--card); border-bottom: 1px solid var(--border); padding: 10px 24px;
  display: flex; align-items: center; gap: 12px; position: sticky; top: 0; z-index: 20;
}
#topbar h1 { font-size: 17px; margin: 0; font-weight: 600; }
.userchip { display: flex; align-items: center; gap: 8px; padding: 4px 10px 4px 4px; border: 1px solid var(--border); border-radius: 999px; background: #fff; }
.userchip .avatar {
  width: 28px; height: 28px; border-radius: 50%; background: var(--navy); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700;
}
.userchip-text { display: flex; flex-direction: column; line-height: 1.1; }
.userchip-text strong { font-size: 12px; }
.userchip-text em { font-style: normal; font-size: 10px; color: var(--muted); letter-spacing: .04em; }
#viewRoot { padding: 22px 24px 60px; }

/* ------------------------------ Cards / KPI ------------------------------ */
.card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.card .card-head { padding: 12px 16px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; }
.card .card-head h3 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; }
.card .card-body { padding: 16px; }
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
.kpi .kpi-label { font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 6px; }
.kpi .kpi-value { font-size: 24px; font-weight: 700; color: var(--navy); font-variant-numeric: tabular-nums; }
.kpi .kpi-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.kpi.warn .kpi-value { color: var(--amber); }
.kpi.bad .kpi-value { color: var(--red); }
.section-gap { margin-bottom: 18px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 1100px) { .grid-2 { grid-template-columns: 1fr; } }

/* Stage distribution bar (CSS only) */
.stagebar { display: flex; height: 26px; border-radius: 6px; overflow: hidden; border: 1px solid var(--border); }
.stagebar > div { height: 100%; }
.stagebar-legend { display: flex; flex-wrap: wrap; gap: 10px 18px; margin-top: 10px; font-size: 12px; }
.stagebar-legend .swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; }

/* Automation flags readout */
.flaglist { display: flex; flex-wrap: wrap; gap: 8px; }
.flagpill {
  display: inline-flex; align-items: center; gap: 7px; padding: 5px 12px; border-radius: 999px;
  border: 1px solid var(--border); background: #fff; font-size: 12.5px; font-weight: 600;
}
.flagpill .lamp { width: 9px; height: 9px; border-radius: 50%; }
.flagpill.on .lamp { background: var(--green); }
.flagpill.off { border-color: var(--red); background: var(--red-bg); color: var(--red); }
.flagpill.off .lamp { background: var(--red); }

/* ------------------------------- Tables ------------------------------- */
table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data th { background: #f0f3f8; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
table.data tbody tr.clickable { cursor: pointer; }
table.data tbody tr.clickable:hover { background: #f6f9ff; }
table.data td.wrap { white-space: normal; }
.table-scroll { overflow-x: auto; }
.pager { display: flex; align-items: center; gap: 12px; padding: 10px 12px; justify-content: flex-end; }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; }
.toolbar input[type=search], .toolbar input[type=text] { min-width: 220px; }

/* ------------------------------- Badges ------------------------------- */
.badge {
  display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: #e8ecf3; color: var(--text); white-space: nowrap;
}
.badge.s-new { background:#e3ecf9; color:#1B3A6B; }
.badge.s-progress { background:#e0e8f5; color:#2f5fa3; }
.badge.s-col1 { background:#fff3d6; color:#9a6a00; }
.badge.s-col2 { background:#ffe3c2; color:#9a5200; }
.badge.s-col3 { background:#ffd4c2; color:#a03a00; }
.badge.s-agree { background:#d6f0dd; color:#1e7e46; }
.badge.s-broken { background:#fbdcdc; color:#b3261e; }
.badge.s-prelegal, .badge.s-legal { background:#e5d6f7; color:#5b21a0; }
.badge.s-closed { background:#e2e5ea; color:#5c6470; }
.badge.av-MATCH { background:#d6f0dd; color:#1e7e46; }
.badge.av-MOVED, .badge.av-MISMATCH { background:#ffe3c2; color:#9a5200; }
.badge.av-POSSIBLY_FALSE { background:#fbdcdc; color:#b3261e; }
.badge.av-UNVERIFIED { background:#e8ecf3; color:#6b7484; }
.badge.ai-on { background: var(--green-bg); color: var(--green); }
.badge.ai-off { background: var(--red-bg); color: var(--red); }
.badge.ok { background: var(--green-bg); color: var(--green); }
.badge.bad { background: var(--red-bg); color: var(--red); }
.badge.warn { background: var(--amber-bg); color: var(--amber); }
.badge.info { background: var(--slate-bg); color: var(--slate); }
.badge.conf-EXACT_MATCH { background: var(--green-bg); color: var(--green); }
.badge.conf-HIGH_CONFIDENCE { background: var(--slate-bg); color: var(--slate); }
.badge.conf-REVIEW_REQUIRED { background: var(--amber-bg); color: var(--amber); }
.badge.conf-REJECT { background: var(--red-bg); color: var(--red); }
.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11px; background: #eef2f8; border: 1px solid var(--border); margin: 1px 2px 1px 0; }
.chip.hit { background: var(--green-bg); border-color: transparent; color: var(--green); }
.chip.miss { background: #f1f3f7; color: var(--muted); text-decoration: line-through; }

/* ------------------------------- Drawer ------------------------------- */
#drawerOverlay { position: fixed; inset: 0; background: rgba(18,28,45,.45); display: none; z-index: 40; }
#drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(760px, 96vw);
  background: var(--bg); box-shadow: -8px 0 30px rgba(0,0,0,.2);
  overflow-y: auto; display: none; z-index: 41; padding: 24px;
}
#drawer.open, #drawerOverlay.open { display: block; }
#drawer h2 { margin: 0; }
.drawer-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 16px; }
.section {
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  padding: 16px; margin-bottom: 14px;
}
.section h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; }
.kv { display: grid; grid-template-columns: 190px 1fr; row-gap: 6px; column-gap: 12px; margin: 0; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; align-items: center; }
.list { margin: 0; padding: 0; list-style: none; }
.list li { padding: 6px 0; border-bottom: 1px dashed var(--border); display: flex; justify-content: space-between; gap: 8px; }
.list li:last-child { border-bottom: 0; }
.timeline { margin: 0; padding: 0; list-style: none; }
.timeline li { padding: 8px 0 8px 18px; border-left: 3px solid var(--border); position: relative; margin-left: 6px; }
.timeline li::before { content: ""; position: absolute; left: -7px; top: 14px; width: 9px; height: 9px; border-radius: 50%; background: var(--accent); }
.timeline .when { font-size: 12px; color: var(--muted); }
.stat-ok { color: var(--green); font-weight: 600; }
.stat-fail { color: var(--red); font-weight: 600; }
.stat-dry { color: var(--amber); font-weight: 600; }

/* ------------------------------- Modal ------------------------------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(18,28,45,.45); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-box { background: var(--card); border-radius: 12px; padding: 24px; width: min(640px, 94vw); max-height: 85vh; overflow-y: auto; }
.modal-box h3 { margin-top: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
#csvResults { margin-top: 14px; font-size: 13px; max-height: 300px; overflow-y: auto; }
#csvResults .ok { color: var(--green); } #csvResults .err { color: var(--red); }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--navy); color: #fff; padding: 10px 20px; border-radius: 8px;
  display: none; z-index: 60; box-shadow: 0 4px 16px rgba(0,0,0,.3); max-width: 80vw;
}

/* ------------------------------ Conversations ------------------------------ */
.chat { display: flex; flex-direction: column; gap: 10px; }
.bubble { max-width: 82%; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--border); background: #fff; white-space: pre-wrap; }
.bubble.in { align-self: flex-start; background: #f0f3f8; }
.bubble.out { align-self: flex-end; background: #e3ecf9; border-color: #c9d8ef; }
.bubble .meta { font-size: 11px; color: var(--muted); margin-top: 6px; display: flex; gap: 8px; align-items: center; }
.authorbadge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 10px; font-weight: 700; letter-spacing: .03em; }
.authorbadge.DEBTOR { background: #e8ecf3; color: #232a35; }
.authorbadge.AI { background: var(--green-bg); color: var(--green); }
.authorbadge.ADMIN { background: #e3ecf9; color: #1B3A6B; }
.authorbadge.SYSTEM { background: var(--amber-bg); color: var(--amber); }
details.toolcalls { margin-top: 6px; font-size: 12px; }
details.toolcalls summary { cursor: pointer; color: var(--accent); }
details.toolcalls pre, pre.jsonview { background: #f6f9ff; border: 1px solid var(--border); border-radius: 6px; padding: 8px; overflow-x: auto; font-size: 12px; }
.replybox { display: flex; gap: 8px; margin-top: 12px; }
.replybox textarea { flex: 1; min-height: 44px; }

/* ------------------------------ Toggle switch ------------------------------ */
.switch { position: relative; display: inline-block; width: 40px; height: 22px; flex: 0 0 auto; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track { position: absolute; inset: 0; background: #c6cdd9; border-radius: 999px; transition: background .15s; }
.switch .track::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; transition: left .15s; box-shadow: 0 1px 3px rgba(0,0,0,.25);
}
.switch input:checked + .track { background: var(--green); }
.switch input:checked + .track::after { left: 21px; }
.switch input:disabled + .track { opacity: .5; }
.flagrow { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px dashed var(--border); }
.flagrow:last-child { border-bottom: 0; }
.flagrow .flagname { flex: 1; }
.flagrow .flagname small { display: block; color: var(--muted); }

/* ------------------------------ Documents designer ------------------------------ */
.designer { display: grid; grid-template-columns: 240px minmax(0, 1fr) 300px; gap: 14px; align-items: start; }
@media (max-width: 1250px) { .designer { grid-template-columns: 210px minmax(0, 1fr) 270px; } }
.designer .pane { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.designer .pane h3 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.tpl-item { padding: 8px 9px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px; cursor: pointer; }
.tpl-item:hover { background: #f6f9ff; }
.tpl-item.selected { border-color: var(--accent); background: #eef3fb; }
.tpl-item .tpl-name { font-weight: 600; display: block; margin-bottom: 4px; word-break: break-word; }
.canvas-wrap { overflow: auto; background: #e9edf3; border: 1px solid var(--border); border-radius: 8px; padding: 18px; display: flex; justify-content: center; }
.pdf-scaler { width: calc(612px * var(--pdf-scale)); height: calc(792px * var(--pdf-scale)); flex: 0 0 auto; }
.pdf-page {
  width: 612px; height: 792px; background: #fff; position: relative;
  box-shadow: 0 3px 14px rgba(18,41,77,.18); transform: scale(var(--pdf-scale)); transform-origin: top left;
  outline: 1px solid #c9d2e0;
}
.overlay {
  position: absolute; border: 1px dashed transparent; display: flex; align-items: center; justify-content: center;
  overflow: hidden; cursor: grab; user-select: none; text-align: center; padding: 2px;
}
.overlay:hover { border-color: #9db4d6; }
.overlay.selected { border: 1.5px solid var(--accent); cursor: grabbing; z-index: 999 !important; }
.overlay .ovl-label { font-size: 11px; color: var(--muted); pointer-events: none; line-height: 1.25; }
.overlay .ovl-text { pointer-events: none; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.overlay.kind-stamp { background: rgba(27,58,107,.06); }
.overlay.kind-signature { background: rgba(30,126,70,.05); }
.overlay.kind-image { background: rgba(107,116,132,.06); }
.overlay .resize-handle {
  position: absolute; right: -6px; bottom: -6px; width: 12px; height: 12px; border-radius: 3px;
  background: var(--accent); border: 2px solid #fff; cursor: nwse-resize; display: none;
}
.overlay.selected .resize-handle { display: block; }
.designer-toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.designer-toolbar .sep { width: 1px; height: 22px; background: var(--border); }
.propgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.propgrid .fld { margin-bottom: 0; }
.propgrid .fld.full { grid-column: 1 / -1; }
.palette { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.asset-item { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 7px; margin-bottom: 6px; cursor: pointer; }
.asset-item:hover { background: #f6f9ff; }
.asset-item .asset-key { font-size: 11px; color: var(--muted); word-break: break-all; }
.render-frame { width: 100%; height: 480px; border: 1px solid var(--border); border-radius: 8px; background: #fff; margin-top: 12px; }

.empty-note { padding: 26px; text-align: center; color: var(--muted); }
.error-panel { padding: 18px; border: 1px solid var(--red); background: var(--red-bg); color: var(--red); border-radius: 10px; }
.error-panel strong { display: block; margin-bottom: 4px; }
