/* =====================================================================
   KTM Receivables — data-dense finance UI.
   Light and dark, no external fonts, no CDN.
   ===================================================================== */

:root {
  --bg:        #f6f7f9;
  --surface:   #ffffff;
  --surface-2: #fbfcfd;
  --border:    #e2e6ec;
  --border-2:  #cdd4de;
  --text:      #16202c;
  --text-dim:  #5b6878;
  --text-mute: #8894a4;

  --accent:    #1f5fd6;
  --accent-dim:#e8f0fe;

  --good:      #1d8a55;
  --good-bg:   #e6f5ec;
  --warn:      #b5730d;
  --warn-bg:   #fdf3e2;
  --bad:       #c33a34;
  --bad-bg:    #fdeceb;

  --b1: #2f9e6d;   /* 0-15   */
  --b2: #66b355;   /* 16-25  */
  --b3: #b8ac3c;   /* 26-31  */
  --b4: #d78f36;   /* 32-41  */
  --b5: #cf6636;   /* 42-46  */
  --b6: #bb3b39;   /* >47    */

  --radius: 10px;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 1px 3px rgba(16,24,40,.06);
  --mono: ui-monospace, SFMono-Regular, "Cascadia Mono", Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #0f141b;
    --surface:   #161c25;
    --surface-2: #1b222c;
    --border:    #263040;
    --border-2:  #35415a;
    --text:      #e6ecf3;
    --text-dim:  #9aa8ba;
    --text-mute: #6f7d90;

    --accent:    #6ea8fe;
    --accent-dim:#1a2942;

    --good:      #4ac48a;
    --good-bg:   #14301f;
    --warn:      #e0a94c;
    --warn-bg:   #322612;
    --bad:       #f0736c;
    --bad-bg:    #351a1a;

    --shadow: 0 1px 2px rgba(0,0,0,.3);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- chrome ---------- */

.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  padding: 0 1.25rem; height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}

.brand { display: flex; align-items: center; gap: .6rem; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand-mark {
  background: var(--accent); color: #fff;
  font-weight: 700; font-size: 11px; letter-spacing: .06em;
  padding: .3rem .45rem; border-radius: 5px;
}
.brand-name { font-weight: 600; white-space: nowrap; }

.mainnav { display: flex; gap: .15rem; flex: 1; overflow-x: auto; }
.mainnav a {
  padding: .45rem .7rem; border-radius: 7px;
  color: var(--text-dim); font-weight: 500; white-space: nowrap;
}
.mainnav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.mainnav a.active { background: var(--accent-dim); color: var(--accent); }

.userbox { display: flex; align-items: center; gap: .75rem; }
.who { font-size: 12px; color: var(--text-dim); text-align: right; line-height: 1.25; }
.who em { display: block; font-style: normal; font-size: 10.5px; color: var(--text-mute); text-transform: uppercase; letter-spacing: .04em; }

.wrap { max-width: 1400px; margin: 0 auto; padding: 1.5rem 1.25rem 4rem; }

.foot {
  border-top: 1px solid var(--border);
  padding: 1rem 1.25rem; color: var(--text-mute); font-size: 12px;
  text-align: center;
}

/* ---------- headings ---------- */

.page-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem;
}
.page-head h1 { margin: 0; font-size: 21px; font-weight: 650; letter-spacing: -.01em; }
.page-head .sub { color: var(--text-dim); font-size: 13px; margin-top: .2rem; }

h2.section { font-size: 15px; font-weight: 620; margin: 2rem 0 .75rem; letter-spacing: -.005em; }
h2.section:first-child { margin-top: 0; }
.section-note { color: var(--text-dim); font-size: 12.5px; margin: -.5rem 0 .75rem; max-width: 68ch; }

/* ---------- cards ---------- */

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden;
}
.card-pad { padding: 1rem 1.1rem; }
.card-head {
  padding: .8rem 1.1rem; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center; gap: 1rem;
  font-weight: 600; font-size: 13.5px;
}
.card-head .muted { font-weight: 400; color: var(--text-mute); font-size: 12px; }

.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }

/* ---------- KPI tiles ---------- */

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(175px, 1fr)); gap: .75rem; margin-bottom: 1.5rem; }
.kpi {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .85rem .95rem; box-shadow: var(--shadow);
}
.kpi .label { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-mute); font-weight: 600; }
.kpi .value { font-size: 22px; font-weight: 660; margin-top: .3rem; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.kpi .value small { font-size: 12px; font-weight: 500; color: var(--text-mute); margin-left: .15rem; }
.kpi .foot { font-size: 11.5px; color: var(--text-dim); margin-top: .3rem; }
.kpi.is-bad .value { color: var(--bad); }
.kpi.is-warn .value { color: var(--warn); }
.kpi.is-good .value { color: var(--good); }

/* ---------- tables ---------- */

.table-scroll { overflow-x: auto; }

table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th {
  text-align: left; font-weight: 600; font-size: 11px;
  text-transform: uppercase; letter-spacing: .04em; color: var(--text-mute);
  padding: .55rem .7rem; border-bottom: 1px solid var(--border);
  background: var(--surface-2); position: sticky; top: 0; white-space: nowrap;
}
table.data td { padding: .5rem .7rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data tbody tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); font-size: 12.5px; }
table.data td.mono { font-family: var(--mono); font-size: 12px; }
table.data tfoot td { padding: .55rem .7rem; font-weight: 650; border-top: 2px solid var(--border-2); font-variant-numeric: tabular-nums; }
table.data tr.is-total td { font-weight: 650; background: var(--surface-2); }

.name-cell { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- bucket bar ---------- */

.bucketbar { display: flex; height: 22px; border-radius: 5px; overflow: hidden; background: var(--surface-2); }
.bucketbar span { display: block; height: 100%; }
.bucketbar span:hover { filter: brightness(1.12); }
.b1 { background: var(--b1); } .b2 { background: var(--b2); } .b3 { background: var(--b3); }
.b4 { background: var(--b4); } .b5 { background: var(--b5); } .b6 { background: var(--b6); }

.bucket-legend { display: flex; flex-wrap: wrap; gap: .35rem 1.1rem; margin-top: .7rem; font-size: 12px; }
.bucket-legend div { display: flex; align-items: center; gap: .4rem; }
.bucket-legend i { width: 9px; height: 9px; border-radius: 2px; display: inline-block; }
.bucket-legend b { font-variant-numeric: tabular-nums; font-weight: 600; }
.bucket-legend span { color: var(--text-mute); }

/* thin inline bar used inside table rows */
.minibar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; min-width: 60px; display: flex; }
.minibar span { display: block; height: 100%; }

/* ---------- badges / pills ---------- */

.pill {
  display: inline-block; padding: .1rem .45rem; border-radius: 20px;
  font-size: 11px; font-weight: 600; letter-spacing: .01em;
  border: 1px solid var(--border-2); color: var(--text-dim); white-space: nowrap;
}
.pill.good { background: var(--good-bg); color: var(--good); border-color: transparent; }
.pill.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.pill.bad  { background: var(--bad-bg);  color: var(--bad);  border-color: transparent; }
.pill.info { background: var(--accent-dim); color: var(--accent); border-color: transparent; }

.tag-scope { font-family: var(--mono); font-size: 11px; color: var(--text-dim); }

/* ---------- forms / buttons ---------- */

.btn, .btn-ghost, .btn-danger {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .45rem .85rem; border-radius: 7px; font-size: 13px; font-weight: 550;
  border: 1px solid transparent; cursor: pointer; font-family: inherit;
  transition: background .12s, border-color .12s;
}
.btn { background: var(--accent); color: #fff; }
.btn:hover { filter: brightness(1.08); text-decoration: none; }
.btn-ghost { background: var(--surface); border-color: var(--border-2); color: var(--text); }
.btn-ghost:hover { background: var(--surface-2); text-decoration: none; }
.btn-danger { background: var(--bad); color: #fff; }
.btn-sm { padding: .3rem .6rem; font-size: 12px; }
.btn:disabled, .btn-ghost:disabled { opacity: .5; cursor: not-allowed; }

input[type=text], input[type=password], input[type=number], input[type=search], select, textarea {
  font: inherit; padding: .45rem .6rem; border-radius: 7px;
  border: 1px solid var(--border-2); background: var(--surface); color: var(--text);
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; min-height: 80px; }
label.field { display: block; margin-bottom: .8rem; }
label.field > span { display: block; font-size: 12px; font-weight: 600; color: var(--text-dim); margin-bottom: .25rem; }

.toolbar { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; }
.toolbar select, .toolbar input { width: auto; min-width: 160px; }
.toolbar .spacer { flex: 1; }

/* ---------- notices ---------- */

.notice { border-radius: var(--radius); padding: .8rem 1rem; font-size: 13px; margin-bottom: 1rem; border: 1px solid; }
.notice.info { background: var(--accent-dim); border-color: transparent; color: var(--text); }
.notice.good { background: var(--good-bg); border-color: transparent; color: var(--text); }
.notice.warn { background: var(--warn-bg); border-color: transparent; color: var(--text); }
.notice.bad  { background: var(--bad-bg);  border-color: transparent; color: var(--text); }
.notice strong { display: block; margin-bottom: .2rem; }
.notice ul { margin: .4rem 0 0; padding-left: 1.1rem; }
.notice li { margin: .15rem 0; }

/* ---------- charts (inline SVG) ---------- */

.chart { width: 100%; height: auto; display: block; }
.chart .axis { stroke: var(--border); stroke-width: 1; }
.chart .gridline { stroke: var(--border); stroke-width: 1; stroke-dasharray: 2 3; opacity: .7; }
.chart .lbl { fill: var(--text-mute); font-size: 10px; font-family: var(--sans); }
.chart .val { fill: var(--text); font-size: 10.5px; font-family: var(--mono); }
.chart .ln { fill: none; stroke: var(--accent); stroke-width: 2; }
.chart .dot { fill: var(--accent); }
.chart .area { fill: var(--accent); opacity: .12; }

/* ---------- markdown (AI output) ---------- */

.md { font-size: 14px; line-height: 1.65; }
.md h3 { font-size: 15px; font-weight: 650; margin: 1.5rem 0 .5rem; padding-bottom: .3rem; border-bottom: 1px solid var(--border); }
.md h3:first-child { margin-top: 0; }
.md h4 { font-size: 13.5px; font-weight: 620; margin: 1.1rem 0 .4rem; }
.md p { margin: .6rem 0; }
.md ul, .md ol { margin: .5rem 0; padding-left: 1.3rem; }
.md li { margin: .3rem 0; }
.md code { background: var(--surface-2); padding: .1rem .3rem; border-radius: 4px; font-family: var(--mono); font-size: 12.5px; }
.md pre.md-code { background: var(--surface-2); padding: .8rem; border-radius: 7px; overflow-x: auto; border: 1px solid var(--border); }
.md pre.md-code code { background: none; padding: 0; }
.md blockquote { margin: .7rem 0; padding-left: .9rem; border-left: 3px solid var(--border-2); color: var(--text-dim); }
.md table.md-table { width: 100%; border-collapse: collapse; font-size: 13px; margin: .8rem 0; }
.md table.md-table th, .md table.md-table td { padding: .4rem .6rem; border: 1px solid var(--border); text-align: left; }
.md table.md-table th { background: var(--surface-2); font-weight: 600; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.2rem 0; }

/* ---------- SQL block ---------- */

pre.sql {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 7px; padding: .8rem 1rem; overflow-x: auto;
  font-family: var(--mono); font-size: 12.5px; line-height: 1.55; margin: 0;
  color: var(--text);
}

/* ---------- upload dropzone ---------- */

.dropzone {
  border: 2px dashed var(--border-2); border-radius: var(--radius);
  padding: 2.5rem 1.5rem; text-align: center; background: var(--surface-2);
  transition: border-color .15s, background .15s; cursor: pointer;
}
.dropzone.dragging { border-color: var(--accent); background: var(--accent-dim); }
.dropzone .big { font-size: 15px; font-weight: 600; margin-bottom: .3rem; }
.dropzone .small { font-size: 12.5px; color: var(--text-dim); }
.dropzone input[type=file] { display: none; }
.file-chosen { margin-top: .9rem; font-size: 13px; font-family: var(--mono); color: var(--accent); }

/* ---------- login ---------- */

body.bare { display: grid; place-items: center; min-height: 100vh; background: var(--bg); }
.login-card { width: min(370px, 92vw); background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 1.75rem; box-shadow: 0 8px 30px rgba(16,24,40,.10); }
.login-card h1 { font-size: 18px; margin: 0 0 .25rem; }
.login-card .sub { color: var(--text-dim); font-size: 13px; margin-bottom: 1.4rem; }
.login-card .btn { width: 100%; justify-content: center; margin-top: .4rem; }

/* ---------- misc ---------- */

.muted { color: var(--text-mute); }
.dim   { color: var(--text-dim); }
.num   { font-variant-numeric: tabular-nums; font-family: var(--mono); }
.right { text-align: right; }
.nowrap{ white-space: nowrap; }
.stack > * + * { margin-top: 1rem; }
.empty { text-align: center; padding: 3rem 1rem; color: var(--text-dim); }
.empty h2 { color: var(--text); font-size: 17px; margin-bottom: .4rem; }

.pager { display: flex; gap: .4rem; align-items: center; justify-content: center; margin-top: 1.2rem; font-size: 13px; }
.pager a, .pager span { padding: .35rem .6rem; border-radius: 6px; border: 1px solid var(--border); }
.pager .current { background: var(--accent); color: #fff; border-color: transparent; }

.def-list { display: grid; grid-template-columns: max-content 1fr; gap: .4rem 1.2rem; font-size: 13px; }
.def-list dt { color: var(--text-mute); }
.def-list dd { margin: 0; font-variant-numeric: tabular-nums; }

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; height: auto; padding: .6rem 1rem; }
  .mainnav { order: 3; width: 100%; }
  .wrap { padding: 1rem .75rem 3rem; }
}
