:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-ink: #ffffff;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 1px 2px rgba(15, 23, 42, .04);
  --radius: 12px;

  --green-bg:#dcfce7; --green-tx:#166534;
  --amber-bg:#fef3c7; --amber-tx:#92400e;
  --blue-bg:#dbeafe;  --blue-tx:#1e40af;
  --red-bg:#fee2e2;   --red-tx:#991b1b;
  --gray-bg:#e2e8f0;  --gray-tx:#475569;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg:#0b1120; --surface:#111827; --surface-2:#0f172a; --border:#1f2937;
    --text:#e5e7eb; --muted:#94a3b8; --primary:#6366f1; --shadow:none;
    --green-bg:#052e16; --green-tx:#4ade80;
    --amber-bg:#3b2f0b; --amber-tx:#fbbf24;
    --blue-bg:#0c2a4d;  --blue-tx:#60a5fa;
    --red-bg:#3f1414;   --red-tx:#f87171;
    --gray-bg:#1f2937;  --gray-tx:#94a3b8;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}
a { color: var(--primary); text-decoration: none; }

/* ---------- App shell ---------- */
.layout { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
  width: 244px; flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column;
  padding: 18px 14px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; padding: 6px 10px 18px; }
.brand small { display: block; font-weight: 500; font-size: 11px; color: var(--muted); letter-spacing: .04em; text-transform: uppercase; }
.logo { color: var(--primary); font-size: 22px; }

.side-nav { display: flex; flex-direction: column; gap: 4px; }
.side-nav a {
  display: flex; align-items: center; gap: 11px;
  padding: 10px 12px; border-radius: 10px;
  color: var(--muted); font-weight: 500; font-size: 14px;
}
.side-nav a svg { flex-shrink: 0; opacity: .85; }
.side-nav a:hover { background: var(--surface-2); color: var(--text); }
.side-nav a.active { background: color-mix(in srgb, var(--primary) 12%, transparent); color: var(--primary); font-weight: 600; }

.nav-group { display: flex; flex-direction: column; }
.nav-group-label {
  display: flex; align-items: center; gap: 11px; width: 100%;
  padding: 10px 12px; border: none; background: none; cursor: pointer;
  color: var(--muted); font: inherit; font-weight: 500; font-size: 14px;
  border-radius: 10px; text-align: left;
}
.nav-group-label:hover { background: var(--surface-2); color: var(--text); }
.nav-group-label > svg:first-child { opacity: .85; flex-shrink: 0; }
.nav-group-label.on { color: var(--primary); }
.nav-group-label.on > svg:first-child { opacity: 1; }
.nav-caret { margin-left: auto; transition: transform .2s; opacity: .7; }
.nav-group.open .nav-caret { transform: rotate(180deg); }
.nav-sub-list { display: none; flex-direction: column; gap: 2px; margin-top: 2px; }
.nav-group.open .nav-sub-list { display: flex; }
.nav-sub { display: block; padding: 8px 12px 8px 41px; border-radius: 10px; color: var(--muted); font-weight: 500; font-size: 13.5px; }
.nav-sub:hover { background: var(--surface-2); color: var(--text); }
.nav-sub.active { background: color-mix(in srgb, var(--primary) 12%, transparent); color: var(--primary); font-weight: 600; }

.sidebar-foot { margin-top: auto; padding: 12px 10px 4px; font-size: 11.5px; color: var(--muted); display: flex; align-items: center; gap: 7px; }
.sidebar-foot .dot { width: 8px; height: 8px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 0 3px color-mix(in srgb, #22c55e 22%, transparent); }

/* Main column */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 60px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar-title { font-weight: 600; font-size: 15px; color: var(--muted); }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  position: relative; display: grid; place-items: center;
  width: 38px; height: 38px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.dot-badge { position: absolute; top: 8px; right: 9px; width: 8px; height: 8px; border-radius: 50%; background: #ef4444; border: 2px solid var(--surface); }
.profile { display: flex; align-items: center; gap: 10px; padding: 5px 8px 5px 5px; border-radius: 10px; cursor: pointer; }
.profile:hover, .profile:focus { background: var(--surface-2); outline: none; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; font-size: 13px; font-weight: 700;
  color: #fff; background: linear-gradient(135deg, var(--primary), #8b5cf6);
}
.profile-meta { line-height: 1.15; }
.profile-meta strong { font-size: 13.5px; display: block; }
.profile-meta small { font-size: 11.5px; color: var(--muted); }
.profile .caret { color: var(--muted); }
@media (max-width: 640px) { .profile-meta { display: none; } }

/* ---------- Layout ---------- */
.container { max-width: 1160px; margin: 0 auto; width: 100%; padding: 28px 28px 48px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 24px; }
.page-head p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.footer { text-align: center; color: var(--muted); font-size: 12px; padding: 24px; }

/* ---------- Cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 20px; }
.grid { display: grid; gap: 16px; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 0 0 12px; font-weight: 600; }

/* ---------- Stat cards ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.stat .num { font-size: 30px; font-weight: 700; line-height: 1.1; }
.stat .lbl { font-size: 13px; color: var(--muted); margin-top: 4px; }
.stat.accent-green .num { color: var(--green-tx); }
.stat.accent-amber .num { color: var(--amber-tx); }
.stat.accent-blue .num { color: var(--blue-tx); }
.stat.accent-red .num { color: var(--red-tx); }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); padding: 12px 16px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 13px 16px; border-bottom: 1px solid var(--border); }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
.mono { font-variant-numeric: tabular-nums; }
.t-strong { font-weight: 600; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge-standard { background: var(--green-bg); color: var(--green-tx); }
.badge-substandard { background: var(--amber-bg); color: var(--amber-tx); }
.badge-refer { background: var(--blue-bg); color: var(--blue-tx); }
.badge-decline { background: var(--red-bg); color: var(--red-tx); }
.badge-approved, .badge-aligned { background: var(--green-bg); color: var(--green-tx); }
.badge-need_confirmation, .badge-gray { background: var(--amber-bg); color: var(--amber-tx); }
.badge-rejected, .badge-conflict { background: var(--red-bg); color: var(--red-tx); }
.badge-processing, .badge-evaluated, .badge-mock, .badge-extracted, .badge-pending { background: var(--gray-bg); color: var(--gray-tx); }

/* ---------- Buttons ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 16px; border-radius: 9px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font-weight: 600; font-size: 14px; cursor: pointer; }
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn-primary:hover { filter: brightness(1.07); background: var(--primary); }
.btn-sm { padding: 6px 12px; font-size: 13px; }

/* ---------- Filters ---------- */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.chip { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--muted); font-size: 13px; font-weight: 500; }
.chip:hover { color: var(--text); }
.chip.active { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.filters .sep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }

/* ---------- Forms ---------- */
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.full { grid-column: 1 / -1; }
label { font-size: 13px; font-weight: 600; color: var(--muted); }
input, select, textarea {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface-2); color: var(--text); font-size: 14px; font-family: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--primary) 40%, transparent); border-color: var(--primary); }
.hint { font-size: 12px; color: var(--muted); }
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 26px 20px; text-align: center; background: var(--surface-2); cursor: pointer;
  transition: border-color .15s, background .15s;
}
.dropzone:hover { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 5%, var(--surface-2)); }
.dropzone .dz-icon { font-size: 26px; color: var(--muted); line-height: 1; }
.dropzone strong { color: var(--text); font-size: 15px; }
.dropzone .hint { max-width: 460px; }
.dropzone input[type="file"] { margin-top: 8px; max-width: 100%; }

/* ---------- Detail ---------- */
.detail-grid { display: grid; grid-template-columns: 1.4fr 1fr; gap: 20px; align-items: start; }
.kv { display: grid; grid-template-columns: 190px 1fr; gap: 8px 14px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 500; }
.decision-hero { text-align: center; padding: 26px 20px; }
.decision-hero .big-badge { font-size: 22px; padding: 8px 22px; border-radius: 999px; font-weight: 700; display: inline-block; }
.decision-hero .loading { margin-top: 12px; font-size: 14px; color: var(--muted); }
.reasons { list-style: none; margin: 0; padding: 0; }
.reasons li { padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px; margin-bottom: 8px; font-size: 14px; background: var(--surface-2); display: flex; gap: 8px; }
.reasons li::before { content: "•"; color: var(--primary); font-weight: 700; }
.doc-thumb { width: 100%; border: 1px solid var(--border); border-radius: 9px; }
details.raw { margin-top: 12px; }
details.raw pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px; padding: 12px; overflow-x: auto; font-size: 12px; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 12px 0; border-bottom: 1px solid var(--border); font-size: 14px; display: flex; gap: 12px; align-items: center; }
.timeline li:last-child { border-bottom: none; }
.timeline .when { color: var(--muted); font-size: 12px; min-width: 130px; }

/* ---------- T&C ---------- */
.rule-row { display: grid; grid-template-columns: 1.6fr repeat(4, 1fr); gap: 10px; align-items: end; padding: 12px 0; border-bottom: 1px solid var(--border); }
.rule-row:last-of-type { border-bottom: none; }
.rule-row .rname { font-weight: 600; font-size: 14px; }
.rule-row .rname small { display: block; color: var(--muted); font-weight: 400; font-size: 12px; }
.rule-row input, .rule-row select { padding: 7px 9px; font-size: 13px; }
.rule-row .muted-cell { color: var(--muted); font-size: 12px; align-self: center; }
.version-pills { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }

/* ---------- Section blocks (dashboard) ---------- */
.section-block { margin-bottom: 34px; }
.block-h { font-size: 17px; margin: 0 0 4px; }
.section-block .row-between { margin-bottom: 14px; }

/* ---------- Checkbox groups ---------- */
.check-row { display: flex; flex-wrap: wrap; gap: 8px 18px; padding: 4px 0; }
.check { display: flex; align-items: center; gap: 7px; font-weight: 500; color: var(--text); font-size: 14px; cursor: pointer; }
.check input { width: auto; margin: 0; }

/* ---------- Alignment standard editor ---------- */
.std-rule { padding: 14px 0; border-bottom: 1px solid var(--border); }
.std-rule:last-of-type { border-bottom: none; }
.std-rule-name { font-weight: 600; font-size: 14px; margin-bottom: 8px; }
.std-rule-name small { color: var(--muted); font-weight: 400; font-size: 12px; }
.std-rule-fields { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end; }
.std-rule-fields .field { min-width: 150px; }
.std-rule-fields .field.full { min-width: 100%; }

/* ---------- Findings ---------- */
.findings { list-style: none; margin: 0; padding: 0; }
.findings li { display: flex; gap: 10px; align-items: flex-start; padding: 11px 0; border-bottom: 1px solid var(--border); }
.findings li:last-child { border-bottom: none; }
.findings .badge { flex-shrink: 0; text-transform: capitalize; }

.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.mt-16 { margin-top: 16px; } .mt-24 { margin-top: 24px; }
.stack > * + * { margin-top: 20px; }
.empty { text-align: center; color: var(--muted); padding: 40px; }

@media (max-width: 820px) {
  .detail-grid { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .rule-row { grid-template-columns: 1fr 1fr; }
  .layout { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; position: static;
    flex-direction: row; align-items: center; gap: 6px;
    padding: 8px 10px; overflow-x: auto;
    border-right: none; border-bottom: 1px solid var(--border);
  }
  .brand { padding: 4px 10px 4px 4px; }
  .brand-txt small { display: none; }
  .side-nav { flex-direction: row; align-items: center; }
  .side-nav > a span { display: none; }
  .side-nav > a { padding: 10px; }
  .nav-group { flex-direction: row; align-items: center; }
  .nav-group-label { display: none; }
  .nav-sub-list { display: flex !important; flex-direction: row; margin-top: 0; }
  .nav-sub { padding: 10px 12px; }
  .sidebar-foot { display: none; }
}

/* ---- Loading overlay (async POST: OCR / underwriting / alignment) ---- */
@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: rgba(15, 23, 42, .45);
  backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
}
.loading-overlay[hidden] { display: none; }
.loading-box {
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  max-width: 320px; padding: 28px 36px; text-align: center;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 12px 44px rgba(15, 23, 42, .28);
}
.spinner {
  width: 40px; height: 40px; border-radius: 50%;
  border: 4px solid var(--border); border-top-color: var(--primary);
  animation: spin .7s linear infinite;
}
.loading-msg { margin: 0; font-weight: 600; font-size: 14.5px; }
.loading-sub { margin: 0; color: var(--muted); font-size: 12px; }

/* inline spinner inside the clicked button */
.btn.is-loading { position: relative; color: transparent !important; pointer-events: none; }
.btn.is-loading::after {
  content: ""; position: absolute; top: 50%; left: 50%;
  width: 15px; height: 15px; margin: -8px 0 0 -8px;
  border: 2px solid rgba(127, 127, 127, .4); border-top-color: var(--primary);
  border-radius: 50%; animation: spin .7s linear infinite;
}
@media (prefers-reduced-motion: reduce) {
  .spinner, .btn.is-loading::after { animation-duration: 1.6s; }
}

/* ---- AI underwriter note (case detail) ---- */
.ai-note { border-left: 3px solid var(--primary); }
.ai-note .ai-summary { margin: 6px 0 0; font-weight: 600; line-height: 1.55; }
.ai-note .ai-rationale { margin: 10px 0 0; color: var(--muted); line-height: 1.6; }
.ai-note .ai-by {
  font-size: 11px; font-weight: 600; letter-spacing: .02em;
  padding: 2px 8px; border: 1px solid var(--border); border-radius: 999px;
  white-space: nowrap;
}
.ai-note .ai-disclaimer { margin: 12px 0 0; font-size: 11.5px; font-style: italic; }
