/* SLC Sale Partner Dashboard */
:root {
  --brand: #2b9ec0;
  --brand-dark: #1b7d9a;
  --surface: #f4f6f7;
  --card: #ffffff;
  --ink: #16232a;
  --ink-2: #52616b;
  --ink-3: #8a979f;
  --line: #e2e8ec;
  --good: #0ca30c;
  --warn: #fab219;
  --serious: #ec835a;
  --crit: #d03b3b;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(22,35,42,.08), 0 4px 14px rgba(22,35,42,.05);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", -apple-system, "Sukhumvit Set", "Noto Sans Thai",
    "Leelawadee UI", sans-serif;
  background: var(--surface); color: var(--ink); font-size: 14.5px;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; }
.hidden { display: none !important; }

/* ---------- Login ---------- */
#login-view {
  min-height: 100vh; min-height: 100dvh; display: flex;
  align-items: center; justify-content: center; padding: 20px;
  background: linear-gradient(160deg, #eaf6fa 0%, #f4f6f7 55%, #fdfdfc 100%);
}
.login-card {
  background: var(--card); border-radius: 18px; box-shadow: var(--shadow);
  padding: 34px 30px 28px; width: 100%; max-width: 380px; text-align: center;
}
.login-card img.logo { height: 54px; margin-bottom: 6px; }
.login-card h1 { font-size: 19px; margin: 4px 0 2px; }
.login-card .sub { color: var(--ink-2); font-size: 13px; margin-bottom: 20px; }
.login-card label {
  display: block; text-align: left; font-size: 12.5px; color: var(--ink-2);
  margin: 14px 2px 6px; font-weight: 600;
}
.login-card select, .login-card input {
  width: 100%; padding: 11px 12px; border: 1.5px solid var(--line);
  border-radius: 10px; font-size: 15px; background: #fff; color: var(--ink);
  font-family: inherit;
}
.login-card input#pin { letter-spacing: .55em; text-align: center; font-size: 20px; }
.login-card select:focus, .login-card input:focus {
  outline: none; border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(43,158,192,.15);
}
#login-btn {
  width: 100%; margin-top: 20px; padding: 12px; border: 0; border-radius: 10px;
  background: var(--brand); color: #fff; font-size: 15.5px; font-weight: 700;
  cursor: pointer;
}
#login-btn:active { background: var(--brand-dark); }
#login-err { color: var(--crit); font-size: 13px; min-height: 18px; margin-top: 10px; }
.login-hint { color: var(--ink-3); font-size: 11.5px; margin-top: 14px; }

/* ---------- App shell ---------- */
#app-header {
  position: sticky; top: 0; z-index: 30; background: var(--card);
  border-bottom: 1px solid var(--line); padding: 8px 14px;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
#app-header img.logo { height: 30px; }
#hdr-title { font-weight: 700; font-size: 15px; }
#hdr-title small { display: block; font-weight: 400; color: var(--ink-2); font-size: 11px; }
#hdr-spacer { flex: 1; }
.hdr-ctl { display: flex; align-items: center; gap: 6px; }
.hdr-ctl select {
  padding: 7px 9px; border: 1.5px solid var(--line); border-radius: 9px;
  background: #fff; font-size: 13px; color: var(--ink); font-family: inherit;
}
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px;
  color: var(--ink-2); cursor: pointer; user-select: none; }
.toggle input { accent-color: var(--brand); width: 16px; height: 16px; }
#logout-btn {
  border: 1.5px solid var(--line); background: #fff; color: var(--ink-2);
  border-radius: 9px; padding: 7px 12px; font-size: 12.5px; cursor: pointer;
}
#custom-range { display: flex; gap: 6px; align-items: center; }
#custom-range input { padding: 6px 8px; border: 1.5px solid var(--line);
  border-radius: 8px; font-size: 12.5px; font-family: inherit; }

#tabs {
  display: flex; gap: 4px; padding: 8px 14px 0; background: var(--card);
  border-bottom: 1px solid var(--line); overflow-x: auto;
  position: sticky; top: 55px; z-index: 29;
}
.tab-btn {
  border: 0; background: none; padding: 9px 14px 11px; font-size: 13.5px;
  color: var(--ink-2); cursor: pointer; border-bottom: 2.5px solid transparent;
  white-space: nowrap; font-weight: 600;
}
.tab-btn.active { color: var(--brand-dark); border-bottom-color: var(--brand); }
.tab-btn .lock { font-size: 11px; }

#main { padding: 14px; max-width: 1280px; margin: 0 auto; }
.range-note { color: var(--ink-2); font-size: 12.5px; margin: 2px 2px 12px; }
.range-note b { color: var(--ink); }

/* ---------- Cards ---------- */
.kpi-grid {
  display: grid; gap: 10px;
  grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
  margin-bottom: 14px;
}
.kpi {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 14px 10px; min-height: 86px; position: relative;
}
.kpi .k-label { font-size: 11.5px; color: var(--ink-2); font-weight: 600; }
.kpi .k-value { font-size: 21px; font-weight: 800; margin: 3px 0 1px; }
.kpi .k-sub { font-size: 11px; color: var(--ink-3); }
.kpi .k-cmp { font-size: 11.5px; margin-top: 2px; font-weight: 600; }
.kpi .k-cmp.up { color: var(--good); }
.kpi .k-cmp.down { color: var(--crit); }
.kpi .k-cmp.flat { color: var(--ink-3); }
.kpi svg.spark { position: absolute; right: 10px; top: 12px; opacity: .9; }

.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 16px; margin-bottom: 14px;
}
.card h3 { margin: 0 0 2px; font-size: 14.5px; }
.card .sub { color: var(--ink-2); font-size: 12px; margin-bottom: 10px; }
.card-row { display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 900px) { .card-row.two { grid-template-columns: 1fr 1fr; } }

.chart-scroll { overflow-x: auto; }
svg text { font-family: inherit; }

/* ---------- Tables ---------- */
.tbl-wrap { overflow-x: auto; }
table.tbl { border-collapse: collapse; width: 100%; font-size: 13px; }
.tbl th {
  text-align: right; color: var(--ink-2); font-weight: 600; font-size: 11.5px;
  border-bottom: 1.5px solid var(--line); padding: 7px 8px; white-space: nowrap;
  cursor: pointer; user-select: none;
}
.tbl th:first-child, .tbl td:first-child { text-align: left; }
.tbl th.sorted { color: var(--brand-dark); }
.tbl td { padding: 7px 8px; border-bottom: 1px solid var(--line);
  text-align: right; white-space: nowrap; }
.tbl td:first-child { font-weight: 600; }
.tbl tr.clickable { cursor: pointer; }
.tbl tr.clickable:hover td { background: #f0f7fa; }
.tbl .pos { color: var(--good); } .tbl .neg { color: var(--crit); }
.tbl .muted { color: var(--ink-3); font-weight: 400; }
.rank-badge {
  display: inline-block; min-width: 22px; text-align: center; border-radius: 7px;
  background: var(--surface); color: var(--ink-2); font-size: 11.5px;
  padding: 2px 4px; margin-right: 7px; font-weight: 700;
}
tr:nth-child(1) .rank-badge { background: #ffe9ad; color: #7a5b00; }
tr:nth-child(2) .rank-badge { background: #e8ecef; color: #5a6870; }
tr:nth-child(3) .rank-badge { background: #f6dcc8; color: #8a4d1e; }
.bar-cell { position: relative; }
.bar-cell .bar-bg {
  position: absolute; left: 0; top: 15%; bottom: 15%; border-radius: 4px;
  background: rgba(42,120,214,.14); z-index: 0;
}
.bar-cell span { position: relative; z-index: 1; }

/* ---------- Heatmap ---------- */
.hm-wrap { overflow-x: auto; }
table.hm { border-collapse: separate; border-spacing: 2px; font-size: 11.5px; }
.hm th { font-size: 11px; color: var(--ink-2); padding: 4px 6px; font-weight: 600;
  white-space: nowrap; text-align: center; }
.hm th.rowh { text-align: left; position: sticky; left: 0; background: var(--card);
  z-index: 2; max-width: 210px; overflow: hidden; text-overflow: ellipsis; }
.hm td {
  min-width: 52px; text-align: center; padding: 6px 4px; border-radius: 6px;
  font-weight: 700; color: var(--ink);
}
.hm td.c0 { background: #f0f1f2; color: var(--ink-3); font-weight: 400; }
.hm td.c1 { background: #f5c8c8; }         /* < 70% */
.hm td.c2 { background: #f9ddc9; }         /* 70–85 */
.hm td.c3 { background: #fdedc2; }         /* 85–95 */
.hm td.c4 { background: #eef0d8; }         /* 95–100 */
.hm td.c5 { background: #cdeccd; }         /* 100–110 */
.hm td.c6 { background: #a8dfa8; }         /* >= 110 */
.hm-legend { display: flex; gap: 10px; flex-wrap: wrap; font-size: 11.5px;
  color: var(--ink-2); margin-top: 10px; align-items: center; }
.hm-legend .sw { display: inline-block; width: 14px; height: 14px;
  border-radius: 4px; vertical-align: -2px; margin-right: 4px; }

/* ---------- Tooltip ---------- */
#tooltip {
  position: fixed; z-index: 99; pointer-events: none; background: #16232a;
  color: #fff; border-radius: 8px; padding: 8px 10px; font-size: 12px;
  box-shadow: 0 4px 14px rgba(0,0,0,.25); max-width: 260px; line-height: 1.45;
  opacity: 0; transition: opacity .08s;
}
#tooltip b { color: #9fdcee; }

/* ---------- Misc ---------- */
.pill {
  display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 11px;
  font-weight: 700;
}
.pill.ok { background: #dff3df; color: #0a6e0a; }
.pill.warn { background: #fdeec6; color: #8a6400; }
.pill.bad { background: #f8dcdc; color: #a12626; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px;
  color: var(--ink-2); margin-top: 8px; }
.legend .sw { display: inline-block; width: 11px; height: 11px; border-radius: 3px;
  margin-right: 5px; vertical-align: -1px; }
#loading {
  position: fixed; inset: 0; display: flex; align-items: center;
  justify-content: center; background: rgba(244,246,247,.85); z-index: 60;
  font-size: 14px; color: var(--ink-2);
}
.back-btn { border: 1.5px solid var(--line); background: #fff; border-radius: 9px;
  padding: 6px 12px; font-size: 12.5px; color: var(--ink-2); cursor: pointer;
  margin-bottom: 10px; }
.day-filter-note {
  background: #eaf6fa; border: 1px solid #c8e8f2; color: var(--brand-dark);
  border-radius: 9px; padding: 7px 12px; font-size: 12.5px; margin-bottom: 12px;
  display: flex; align-items: center; gap: 10px;
}
.day-filter-note button { border: 0; background: var(--brand); color: #fff;
  border-radius: 7px; padding: 3px 10px; font-size: 11.5px; cursor: pointer; }
@media (max-width: 640px) {
  #hdr-title small { display: none; }
  .kpi .k-value { font-size: 18px; }
  #tabs { top: 51px; }
}
