* { box-sizing: border-box; }
#login { position: fixed; inset: 0; z-index: 100; background: #0f172a;
  display: flex; align-items: center; justify-content: center; }
#login.hidden { display: none; }
#login-card { background: #fff; padding: 28px 30px; border-radius: 12px; width: 310px;
  display: flex; flex-direction: column; gap: 10px; box-shadow: 0 10px 44px rgba(0,0,0,.45); }
#login-card h2 { margin: 0; font-size: 17px; }
.lg-sub { color: #6b7280; font-size: 12px; margin-bottom: 6px; }
#login-card input { padding: 9px 11px; border: 1px solid #d1d5db; border-radius: 7px; font-size: 14px; }
#login-card button { padding: 10px; border: 0; border-radius: 7px; background: #2563eb; color: #fff; font-weight: 600; font-size: 14px; cursor: pointer; }
#lg-err { color: #dc2626; font-size: 12px; min-height: 14px; }
#userbox { position: relative; flex: 0 0 auto; }
#userbtn, #clog-btn { background: rgba(255,255,255,.12); border: 0; color: #e5e7eb; font-size: 15px;
  width: 32px; height: 27px; border-radius: 6px; cursor: pointer; flex: 0 0 auto; }
#clog-btn.on { background: #2563eb; }
#usermenu { position: absolute; right: 0; top: 33px; background: #fff; color: #111827;
  border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.28); padding: 10px 13px; min-width: 160px; z-index: 30; }
#usermenu.hidden { display: none; }
.um-info { font-size: 12px; color: #6b7280; margin-bottom: 9px; white-space: nowrap; }
.um-info b { color: #111827; }
#usermenu a#logout { color: #dc2626; font-weight: 600; font-size: 13px; text-decoration: none; }
html, body { margin: 0; height: 100%; font-family: system-ui, "Segoe UI", Roboto, sans-serif; }
#map { position: absolute; inset: 0; }

#topbar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 6px 12px; background: rgba(17,24,39,.92); color: #e5e7eb;
  font-size: 13px; backdrop-filter: blur(4px);
}
#title { font-weight: 700; font-size: 14px; color: #fff; flex: 0 0 auto; }
.tabs { display: flex; gap: 6px; flex: 1 1 auto; }
.tab { flex: 0 0 150px; text-align: center; background: rgba(255,255,255,.08); color: #cbd5e1;
  border: 0; padding: 6px 8px; border-radius: 6px; font-size: 13px; cursor: pointer; white-space: nowrap; }
.tab.active { background: #2563eb; color: #fff; font-weight: 600; }
#rf-wrap { flex: 0 0 auto; color: #cbd5e1; font-size: 12px; }
#regionfilter { padding: 3px 6px; border-radius: 5px; border: 1px solid #475569; background:#1e293b; color:#e5e7eb; font-size:12px; }
#meta, #diff { flex: 0 1 250px; min-width: 90px; text-align: center; padding: 5px 8px; border-radius: 6px;
  background: rgba(255,255,255,.06); font-size: 12px; line-height: 1.35;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#meta { color: #cbd5e1; }
#diff { color: #fcd34d; }

#scorecard, #optimizer, #rezone, #chatlog { position: absolute; top: 40px; left: 0; right: 0; bottom: 0; z-index: 4;
  background: #f8fafc; overflow: auto; padding: 18px 24px; }
#scorecard.hidden, #optimizer.hidden, #rezone.hidden, #chatlog.hidden { display: none; }
.sc-head h2 { margin: 0 0 4px; font-size: 18px; }
.sc-note { color: #6b7280; font-size: 12px; margin-bottom: 14px; }
table.sc { border-collapse: collapse; width: 100%; font-size: 13px; background:#fff; box-shadow:0 1px 4px rgba(0,0,0,.08); }
table.sc th, table.sc td { padding: 7px 10px; border-bottom: 1px solid #eef2f7; text-align: right; white-space: nowrap; }
table.sc th { background:#f1f5f9; cursor: pointer; user-select:none; position: sticky; top:0; }
table.sc th:first-child, table.sc td:first-child { text-align: left; font-weight: 600; }
table.sc tr.region-row:hover { background:#eff6ff; cursor:pointer; }
table.sc tr.total td { font-weight: 700; background:#f8fafc; border-top: 2px solid #cbd5e1; }
.hot { background:#fee2e2; } .cold { background:#dbeafe; }
.sc-sub { margin-top: 22px; }
.sc-sub h3 { margin: 0 0 8px; font-size: 15px; }
.sc-sub h3 small { color:#6b7280; font-weight: 400; font-size: 12px; }
table.sc td.l, table.sc th.l { text-align: left; }
.chip { display:inline-block; background:#2563eb; color:#fff; padding:3px 10px; border-radius:14px; font-size:12px; cursor:pointer; margin-left:8px; }

#ctrl-toggle {
  position: absolute; top: 48px; left: 10px; z-index: 6; width: 34px; height: 34px;
  border: 0; border-radius: 8px; background: rgba(255,255,255,.97);
  box-shadow: 0 4px 18px rgba(0,0,0,.18); font-size: 18px; line-height: 34px; padding: 0; cursor: pointer;
}
#ctrl-toggle:hover { background: #f1f5f9; }
#controls.collapsed { display: none; }
#controls {
  position: absolute; top: 88px; left: 10px; z-index: 5; width: 268px;
  background: rgba(255,255,255,.97); border-radius: 10px; padding: 12px;
  box-shadow: 0 4px 18px rgba(0,0,0,.18); font-size: 13px; max-height: calc(100% - 100px); overflow:auto;
}
.grp { margin-bottom: 12px; }
.grp > label:first-child { display:block; font-weight: 600; margin-bottom: 6px; color:#374151; font-size:12px; }
.ck { display: flex; align-items: center; gap: 6px; font-weight: 400 !important; margin: 3px 0; cursor: pointer; }
#controls select, #controls input[type=text], #search {
  width: 100%; padding: 6px 8px; border: 1px solid #d1d5db; border-radius: 6px; font-size: 13px;
}
#planmode { width: 100%; padding: 8px; border: 0; border-radius: 6px; background: #2563eb; color:#fff; font-weight:600; cursor:pointer; }
#planmode.on { background: #dc2626; }
.note { font-size: 11px; color: #6b7280; margin-top: 6px; }
.dq { font-size: 11px; color: #b45309; margin-top: 8px; border-top: 1px dashed #e5e7eb; padding-top: 8px; }

#legend { border-top: 1px solid #eee; padding-top: 8px; margin-top: 4px; }
.lg { display: flex; align-items: center; gap: 7px; margin: 3px 0; font-size: 12px; }
.sw { width: 13px; height: 13px; border-radius: 3px; flex: none; }
.sw.circle { border-radius: 50%; }

#search-results { max-height: 160px; overflow: auto; }
.sr { padding: 5px 6px; border-bottom: 1px solid #f3f4f6; cursor: pointer; font-size: 12px; }
.sr:hover { background: #eff6ff; }

#detail {
  position: absolute; top: 48px; right: 10px; z-index: 6; width: 330px;
  background: #fff; border-radius: 10px; padding: 14px 16px; box-shadow: 0 4px 18px rgba(0,0,0,.22);
  font-size: 13px; max-height: calc(100% - 60px); overflow: auto;
}
#detail.hidden { display: none; }
#detail-close { position: absolute; top: 8px; right: 10px; border: 0; background: none; font-size: 16px; cursor: pointer; color:#9ca3af; }
#detail h3 { margin: 0 8px 4px 0; font-size: 15px; }
.tag { display:inline-block; padding:1px 7px; border-radius: 10px; font-size:11px; font-weight:600; color:#fff; margin-right:4px; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 2px 8px; margin: 8px 0; }
.kv div:nth-child(odd) { color: #6b7280; }
.kv div:nth-child(even) { font-weight: 600; text-align: right; }
.warn { color: #b45309; font-weight: 600; }
table.bal { width:100%; border-collapse: collapse; margin-top: 8px; font-size: 12px; }
table.bal th, table.bal td { border-bottom: 1px solid #eee; padding: 4px 5px; text-align: right; }
table.bal th:first-child, table.bal td:first-child { text-align: left; }
.delta-neg { color: #dc2626; } .delta-pos { color: #16a34a; }
.maplibregl-popup-content { font-size: 12px; padding: 6px 9px; }

#chat-fab { position: absolute; right: 16px; bottom: 16px; z-index: 40; background: #2563eb; color: #fff;
  border: 0; border-radius: 22px; padding: 10px 16px; font-size: 14px; font-weight: 600; cursor: pointer;
  box-shadow: 0 4px 14px rgba(37,99,235,.45); }
#chat { position: absolute; right: 16px; bottom: 16px; z-index: 41; width: 400px; height: 560px; max-height: calc(100% - 70px);
  background: #fff; border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.3); display: flex; flex-direction: column; overflow: hidden; }
#chat.hidden, #chat-fab.hidden { display: none; }
#chat-head { background: #1e293b; color: #fff; padding: 10px 14px; font-weight: 600; font-size: 14px; display: flex; align-items: center; }
#chat-scope { color: #93c5fd; font-weight: 400; font-size: 12px; margin-left: 6px; }
#chat-close { margin-left: auto; background: none; border: 0; color: #cbd5e1; font-size: 16px; cursor: pointer; }
#chat-msgs { flex: 1; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; background: #f8fafc; }
.msg { font-size: 13px; line-height: 1.45; padding: 8px 11px; border-radius: 10px; max-width: 90%; }
.msg.u { align-self: flex-end; background: #2563eb; color: #fff; white-space: pre-wrap; }
.msg.a { align-self: flex-start; background: #fff; border: 1px solid #e5e7eb; color: #111827; }
.msg.a table { border-collapse: collapse; font-size: 12px; margin: 6px 0; }
.msg.a th, .msg.a td { border: 1px solid #e5e7eb; padding: 3px 6px; }
.msg.a p { margin: 4px 0; } .msg.a ul { margin: 4px 0; padding-left: 18px; }
#chat-input { display: flex; gap: 6px; padding: 9px; border-top: 1px solid #eef2f7; }
#chat-q { flex: 1; resize: none; border: 1px solid #d1d5db; border-radius: 8px; padding: 8px 10px; font-size: 13px; font-family: inherit; }
#chat-send { border: 0; background: #2563eb; color: #fff; border-radius: 8px; padding: 0 16px; font-weight: 600; cursor: pointer; }

/* ---- Thống kê lịch sử chat ---- */
.stat-cards { display: flex; flex-wrap: wrap; gap: 10px; margin: 12px 0 16px; }
.stat-card { flex: 1 1 120px; background: #f8fafc; border: 1px solid #e5e7eb; border-radius: 10px; padding: 10px 14px; }
.stat-card .sv { font-size: 22px; font-weight: 700; color: #111827; }
.stat-card .st { font-size: 11px; color: #6b7280; margin-top: 2px; }
.stat-grid { display: grid; grid-template-columns: 1.3fr 1fr 1fr; gap: 18px; align-items: start; }
.stat-col h4 { margin: 0 0 6px; font-size: 12px; color: #374151; }
.bar-row { display: flex; align-items: center; gap: 6px; font-size: 11px; margin: 2px 0; }
.bar-row .bd { width: 38px; color: #6b7280; font-variant-numeric: tabular-nums; }
.bar-row .bb { flex: 1; background: #eef2f7; border-radius: 4px; height: 12px; overflow: hidden; }
.bar-row .bb i { display: block; height: 100%; background: #2563eb; border-radius: 4px; }
.bar-row .bn { width: 30px; text-align: right; color: #374151; font-variant-numeric: tabular-nums; }
ol.topq { margin: 4px 0; padding-left: 22px; font-size: 12px; }
ol.topq li { margin: 3px 0; display: flex; justify-content: space-between; gap: 10px; }
ol.topq li span { color: #374151; }
ol.topq li b { color: #2563eb; white-space: nowrap; }
@media (max-width: 1100px) { .stat-grid { grid-template-columns: 1fr; } }
#cs-ai { font-size: 11px; border: 1px solid #c7d2fe; background: #eef2ff; color: #3730a3; border-radius: 6px; padding: 2px 8px; cursor: pointer; }
#cs-ai:disabled { opacity: .6; cursor: default; }
.ai-summary { margin-top: 10px; background: #f8fafc; border: 1px solid #e5e7eb; border-left: 3px solid #6366f1; border-radius: 8px; padding: 10px 14px; font-size: 12.5px; }
.ai-summary h1, .ai-summary h2, .ai-summary h3 { font-size: 13px; margin: 8px 0 4px; }
.ai-summary ul, .ai-summary ol { margin: 4px 0; padding-left: 20px; }
.ai-summary li { margin: 3px 0; }
.ai-summary p { margin: 5px 0; }
