* { box-sizing: border-box; margin: 0; }
html, body { height: 100%; font-family: 'Segoe UI', system-ui, sans-serif; }
body { display: flex; flex-direction: column; }
#topbar { display: flex; align-items: center; gap: 16px; padding: 8px 16px;
  background: #0f766e; color: #fff; }
.brand h1 { font-size: 20px; }
.brand .sub { font-size: 11px; opacity: .85; }
.controls { margin-left: auto; display: flex; gap: 8px; }
.controls select { padding: 5px 8px; border-radius: 4px; border: 0; font-size: 13px; }
#demo-banner { background: #f59e0b; color: #1f2937; font-weight: 600;
  font-size: 12px; padding: 5px 10px; border-radius: 4px; }
#wrap { flex: 1; display: flex; position: relative; }
#panel { width: 360px; background: #fff; border-right: 1px solid #ddd;
  overflow-y: auto; transition: margin .2s; }
#panel:not(.open) { margin-left: -360px; }
#toggle-panel { position: absolute; left: 0; top: 50%; z-index: 500;
  transform: translateY(-50%); background: #0f766e; color: #fff; border: 0;
  padding: 10px 6px; cursor: pointer; border-radius: 0 4px 4px 0; }
#map { flex: 1; }
#tabs { display: flex; border-bottom: 1px solid #ddd; position: sticky; top: 0; background: #fff; }
#tabs button { flex: 1; padding: 10px 4px; border: 0; background: none;
  cursor: pointer; font-size: 12px; color: #555; border-bottom: 2px solid transparent; }
#tabs button.active { color: #0f766e; border-bottom-color: #0f766e; font-weight: 600; }
.tab { display: none; padding: 14px; font-size: 13px; }
.tab.active { display: block; }
.tab label { display: block; margin: 6px 0; }
.tab select { width: 100%; margin: 6px 0; padding: 6px; }
h3 { margin: 10px 0 6px; color: #0f766e; font-size: 14px; }
h4 { margin: 14px 0 6px; color: #0f766e; font-size: 12.5px; }
.hint { font-size: 12px; color: #666; margin-bottom: 8px; }
#kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.kpi { background: #f0fdfa; border: 1px solid #ccfbf1; border-radius: 6px; padding: 8px; }
.kpi .v { font-size: 18px; font-weight: 700; color: #0f766e; }
.kpi .l { font-size: 10.5px; color: #555; }
.table-wrap { overflow-x: auto; max-width: 100%; }
.tbl { width: 100%; border-collapse: collapse; font-size: 10px; }
.tbl th { background: #0f766e; color: #fff; padding: 3px 4px; text-align: left;
  white-space: nowrap; }
.tbl td { padding: 2px 4px; border-bottom: 1px solid #e5e7eb; white-space: nowrap; }
.tbl tr:nth-child(even) td { background: #f8fafc; }
#chat { min-height: 200px; }
.msg { margin: 10px 0; padding: 8px 10px; border-radius: 6px; font-size: 12.5px; }
.msg.user { background: #e0f2f1; margin-left: 30px; }
.msg.bot { background: #f1f5f9; margin-right: 10px; }
.msg .interp { font-style: italic; color: #555; font-size: 11px; margin-bottom: 4px; }
.chat-input { display: flex; gap: 6px; position: sticky; bottom: 0; background: #fff; padding-top: 6px; }
.chat-input input { flex: 1; padding: 8px; border: 1px solid #ccc; border-radius: 4px; }
.chat-input button { background: #0f766e; color: #fff; border: 0; padding: 8px 12px;
  border-radius: 4px; cursor: pointer; }
#chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip { background: #f0fdfa; border: 1px solid #99f6e4; border-radius: 12px;
  padding: 4px 10px; font-size: 11px; cursor: pointer; color: #0f766e; }
#legenda { font-size: 11px; margin: 8px 0; }
.leg-item { display: flex; align-items: center; gap: 6px; margin: 2px 0; }
.leg-cor { width: 14px; height: 14px; border-radius: 2px; display: inline-block; }
#file { margin: 10px 0; width: 100%; }
button#btn-upload, button#btn-pdf { background: #0f766e; color: #fff; border: 0;
  padding: 9px 14px; border-radius: 4px; cursor: pointer; font-size: 13px; }
#resumo-geo { margin: 10px 0; font-size: 12px; }
#ws-link { margin-top: 10px; font-size: 11px; background: #fffbeb;
  border: 1px solid #fde68a; padding: 8px; border-radius: 4px; word-break: break-all; }
#ws-atual { font-size: 12px; margin: 8px 0; color: #555; }
footer { padding: 4px 16px; font-size: 11px; color: #777; background: #f8fafc;
  border-top: 1px solid #e5e7eb; }
.hidden { display: none; }
.chart-wrap { position: relative; width: 100%; height: 150px; }
.chart-wrap.tall { height: 190px; }
.chat-chart-wrap { position: relative; width: 100%; height: 130px; }
.msg .row-hi { cursor: pointer; }
.msg .row-hi:hover td { background: #e0f2f1; }
.popup h4 { margin: 0 0 4px; font-size: 13px; color: #0f766e; }
.popup table { font-size: 11px; }
.popup td { padding: 1px 6px 1px 0; }

#sheet-handle { display: none; }
/* cada renderer canvas escuta cliques no próprio elemento; o hit-test é
   delegado em app.js, então os canvases não devem capturar eventos */
.leaflet-pane canvas { pointer-events: none; }

/* ---- visão Brasil ---- */
#br-card { position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
  z-index: 950; background: #fff; border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .25); padding: 10px 16px;
  max-width: 360px; text-align: center; }
#br-card .br-num { font-size: 20px; font-weight: 800; color: #7f0000; line-height: 1.25; }
#br-card .br-top { font-size: 12px; margin-top: 4px; }
#br-card .br-top a { color: #0f766e; font-weight: 600; cursor: pointer; }
#br-card .br-hint { font-size: 11px; color: #666; margin-top: 2px; }
#br-card .br-x { position: absolute; top: 4px; right: 9px; border: 0;
  background: none; font-size: 16px; color: #666; cursor: pointer; }
.rank-badge { width: 26px; height: 26px; border-radius: 50%; background: #7f0000;
  color: #fff; font-weight: 700; font-size: 12px; display: flex; align-items: center;
  justify-content: center; border: 2px solid #fff; box-shadow: 0 1px 3px rgba(0,0,0,.4); }
.uf-label { color: #334155; font-size: 11px; font-weight: 600; white-space: nowrap;
  text-shadow: 0 0 3px #fff, 0 0 3px #fff, 0 0 3px #fff; }
.btn-voltar { background: #fff; border: 0; padding: 8px 10px; font-size: 13px;
  cursor: pointer; color: #0f766e; font-weight: 600; }
.btn-analisar { margin-top: 6px; width: 100%; background: #0f766e; color: #fff;
  border: 0; border-radius: 4px; padding: 8px; font-size: 13px; cursor: pointer; }
.leg-title { font-weight: 700; color: #0f766e; margin-bottom: 3px; }
#tbl-brasil th { cursor: pointer; }
#tbl-brasil tr.row-uf { cursor: pointer; }
#tbl-brasil tr.row-uf:hover td { background: #e0f2f1; }

/* ===================== MOBILE ===================== */
@media (max-width: 768px) {
  html, body { height: 100vh; height: 100dvh; overflow: hidden; }
  body { overflow: hidden; }

  /* header em uma linha ~52px */
  #topbar { height: 52px; padding: 6px 10px; gap: 8px; }
  .brand h1 { font-size: 17px; white-space: nowrap; }
  .brand .sub { display: none; }
  .controls { gap: 6px; min-width: 0; }
  .controls select { height: 40px; font-size: 15px; padding: 4px 6px; }
  #sel-uf { min-width: 84px; width: auto; flex: 0 0 auto; }
  #sel-linha { flex: 1; min-width: 0; text-overflow: ellipsis; }
  #demo-banner { font-size: 0; padding: 6px 8px; white-space: nowrap; }
  #demo-banner::before { content: "⚠ DEMO"; font-size: 12px; font-weight: 700; }

  footer { display: none; }

  /* mapa em tela cheia; painel vira gaveta inferior */
  #map { position: absolute; inset: 0; }
  #panel { position: absolute; left: 0; right: 0; bottom: 0; width: 100%;
    border-right: 0; border-radius: 14px 14px 0 0;
    box-shadow: 0 -4px 18px rgba(0, 0, 0, .28); z-index: 1100;
    margin-left: 0 !important; display: flex; flex-direction: column;
    overflow: hidden; transition: height .25s ease; }
  #panel[data-sheet="peek"] { height: 64px; }
  #panel[data-sheet="half"] { height: 50vh; height: 50dvh; }
  #panel[data-sheet="full"] { height: calc(100vh - 60px); height: calc(100dvh - 60px); }
  #toggle-panel { display: none; }
  .leaflet-bottom { bottom: var(--sheet-h, 0px) !important;
    transition: bottom .25s ease; }

  /* alça de arraste: barra 40x5 dentro de área de 24px */
  #sheet-handle { display: flex; align-items: center; justify-content: center;
    height: 24px; flex: 0 0 24px; cursor: grab; touch-action: none; }
  #sheet-handle span { width: 40px; height: 5px; border-radius: 3px;
    background: #cbd5e1; }
  #tabs { flex: 0 0 auto; }
  .tab { overflow-y: auto; }
  .tab.active { display: block; flex: 1; }

  /* alvos de toque >= 44px */
  #tabs button { min-height: 48px; font-size: 13px; padding: 8px 2px; }
  .tab label { min-height: 44px; display: flex; align-items: center; margin: 0; gap: 8px; }
  .tab input[type="checkbox"] { width: 22px; height: 22px; flex: 0 0 22px;
    accent-color: #0f766e; }
  .tab select, .tab button, #btn-upload, #btn-pdf { min-height: 44px; font-size: 14px; }
  #chips { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; }
  .chip { padding: 10px 14px; font-size: 13.5px; flex: 0 0 auto;
    max-width: 260px; overflow: hidden; text-overflow: ellipsis;
    white-space: nowrap; }
  .chat-input input { font-size: 16px; height: 44px; }
  .chat-input button { min-height: 44px; min-width: 44px; }
  #file { min-height: 44px; }

  /* controles do Leaflet */
  .leaflet-control-zoom a { width: 44px !important; height: 44px !important;
    line-height: 44px !important; font-size: 20px; }
  .leaflet-control-layers-toggle { width: 44px !important; height: 44px !important;
    background-size: 22px 22px !important; }
  .leaflet-popup-content-wrapper { max-width: 260px; }
  .leaflet-popup-content { max-width: 240px; }

  /* tabelas: fonte menor + primeira coluna fixa */
  .tbl { font-size: 12px; }
  .tbl th { padding: 6px 8px; }
  .tbl td { padding: 6px 8px; }
  .tbl th:first-child, .tbl td:first-child { position: sticky; left: 0; z-index: 1; }
  .tbl td:first-child { background: #fff; }
  .tbl tr:nth-child(even) td:first-child { background: #f8fafc; }
  .tbl th:first-child { background: #0f766e; }

  .chart-wrap { height: 200px; }
  .chart-wrap.tall { height: 240px; }
  .chat-chart-wrap { height: 170px; }

  .btn-analisar { min-height: 44px; font-size: 14px; }
  .btn-voltar { min-height: 44px; }
  #br-card { width: calc(100vw - 100px); max-width: none; padding: 6px 10px;
    top: 6px; }
  #br-card .br-num { font-size: 17px; }
  #br-card .br-top { font-size: 13px; margin-top: 2px; }
  #br-card .br-hint { font-size: 12px; margin-top: 1px; }
  .rank-badge { width: 28px; height: 28px; font-size: 13px; }
}
@media (max-width: 768px) {
  #tabs { z-index: 5; }
  .chat-input { z-index: 5; }
  .leaflet-bottom { bottom: min(var(--sheet-h, 0px), 50dvh) !important; }
}
