:root {
  --bg: #f1f5f9;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #1e3a8a;
  --accent: #2563eb;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --radius: 8px;
  color-scheme: light;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  display: flex;
  flex-direction: column;
}
h2, h3, h4 { margin: 0 0 .5rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
h4 { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-top: 1rem; }
a { color: var(--accent); }
code { background: var(--bg); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.grow { flex: 1; }

/* topo */
.topbar { background: var(--brand); color: #fff; display: flex; align-items: center; gap: 1.5rem; padding: 0 1rem; height: 48px; flex: none; }
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.1rem; letter-spacing: .02em; }
.topbar nav { display: flex; gap: .25rem; overflow-x: auto; }
.topbar nav a { color: #cbd5e1; text-decoration: none; padding: .5rem .8rem; border-radius: 6px; white-space: nowrap; }
.topbar nav a:hover { background: rgba(255, 255, 255, .1); color: #fff; }
.topbar nav a.active { background: rgba(255, 255, 255, .18); color: #fff; }

main { flex: 1; min-height: 0; display: flex; }
.view { flex: 1; min-width: 0; min-height: 0; }
.view:not([hidden]) { display: flex; flex-direction: column; }

/* controles */
input, select, textarea, button { font: inherit; }
input:not([type=checkbox]):not([type=color]), select, textarea {
  border: 1px solid #cbd5e1; border-radius: 6px; padding: .4rem .6rem; background: #fff; color: var(--ink); min-width: 0; width: 100%;
}
input[type=color] { width: 36px; height: 32px; padding: 2px; border: 1px solid #cbd5e1; border-radius: 6px; background: #fff; flex: none; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: .85rem; color: #334155; margin-bottom: .6rem; }
label > input:not([type=checkbox]), label > select, label > textarea { margin-top: .2rem; }
label.check { display: inline-flex; align-items: center; gap: .4rem; margin: 0; }
label.check input { width: auto; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  border: 1px solid #cbd5e1; background: #fff; color: var(--ink); border-radius: 6px; padding: .4rem .8rem; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: #f8fafc; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.sm { padding: .25rem .55rem; font-size: .85rem; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #1d4ed8; }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; }
.btn.active { background: #dbeafe; border-color: var(--accent); color: var(--brand); }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0 .3rem; text-decoration: underline; }

/* páginas */
.page { padding: 1rem; overflow: auto; flex: 1; width: 100%; max-width: 1200px; margin: 0 auto; }
.page.narrow { max-width: 820px; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; margin-bottom: 1rem; }
.page-head h2 { margin: 0; }
.page-head input[type=search] { width: 260px; }
.page-head select { width: auto; }
.table-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: .55rem .8rem; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table th { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: #f8fafc; white-space: nowrap; }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .actions { text-align: right; white-space: nowrap; }
.table .due { color: var(--danger); font-weight: 600; }
.table .empty { text-align: center; color: var(--muted); padding: 2rem; }
.wa { margin-left: .4rem; font-size: .78rem; color: var(--ok); }
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; color: #fff; background: var(--c); }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; }
.pill.paid { background: #dcfce7; color: #166534; }
.pill.part { background: #fef3c7; color: #92400e; }
.pill.open { background: #fee2e2; color: #991b1b; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .75rem 1rem; display: flex; flex-direction: column; }
.card span { color: var(--muted); font-size: .8rem; }
.card b { font-size: 1.3rem; }
.card b small { font-size: .8rem; font-weight: 400; color: var(--muted); }
.card.ok b { color: var(--ok); }
.card.warn b { color: var(--warn); }

/* configurações */
.box { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.rows { display: flex; flex-direction: column; gap: .4rem; margin-bottom: .75rem; }
.row, .row-form { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.row > input:not([type=checkbox]):not([type=color]), .row-form > input:not([type=checkbox]):not([type=color]) { flex: 1 1 120px; width: auto; }
.row > input[type=number], .row-form > input[type=number] { flex: 0 1 100px; }

/* mapa */
.map-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 260px minmax(0, 1fr) 280px; }
.side, .panel { background: var(--panel); overflow-y: auto; padding: .75rem; display: flex; flex-direction: column; gap: .5rem; min-height: 0; }
.side { border-right: 1px solid var(--line); }
.panel { border-left: 1px solid var(--line); display: block; }
.side-head { display: flex; flex-direction: column; gap: .4rem; }
.counts { color: var(--muted); font-size: .8rem; }
#glist { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; min-height: 120px; }
#glist li.g { display: flex; align-items: center; gap: .55rem; padding: .35rem .4rem; border-radius: 6px; cursor: pointer; user-select: none; }
#glist li.g:hover { background: #f1f5f9; }
#glist li.g.armed { background: #dbeafe; outline: 2px solid var(--accent); }
#glist li.g.placed .gname small::before { content: "● "; color: var(--ok); }
#glist li.empty { color: var(--muted); padding: 1rem; text-align: center; }
.dot { flex: none; width: 30px; height: 30px; border-radius: 50%; color: #fff; font-size: .72rem; font-weight: 700; display: grid; place-items: center; }
.gname { display: flex; flex-direction: column; min-width: 0; }
.gname b, .gname small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gname small { color: var(--muted); font-size: .75rem; }

.canvas { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.toolbar { display: flex; align-items: center; flex-wrap: wrap; gap: .4rem; padding: .5rem .75rem; background: var(--panel); border-bottom: 1px solid var(--line); }
.toolbar select { width: auto; max-width: 200px; }
.toolbar .sep { width: 1px; align-self: stretch; background: var(--line); margin: 0 .25rem; }
.viewport { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #cbd5e1; touch-action: none; user-select: none; cursor: grab; }
.viewport:active { cursor: grabbing; }
.viewport[data-tool=zone], .viewport[data-tool=post] { cursor: crosshair; }
.stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; background: #fff; box-shadow: 0 2px 16px rgba(15, 23, 42, .25); }
.stage img, .stage svg { position: absolute; left: 0; top: 0; display: block; max-width: none; }
.stage img { pointer-events: none; }
.stage svg { overflow: visible; }
.stage polygon { cursor: pointer; }
.stage .post, .stage .handle { cursor: move; }
.stage text { font-family: inherit; }
.zlabel, .zcount, .plabel { paint-order: stroke; stroke: #fff; stroke-linejoin: round; fill: #0f172a; font-weight: 700; }
.zcount { fill: #475569; }
.zcount.ok { fill: #15803d; }
.zcount.low { fill: #b91c1c; }
.plabel { font-weight: 600; }
.hint { position: absolute; left: 50%; top: .75rem; transform: translateX(-50%); background: rgba(15, 23, 42, .88); color: #fff; padding: .4rem .9rem; border-radius: 999px; font-size: .85rem; max-width: 90%; text-align: center; pointer-events: none; }
.empty-map { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 2rem; gap: .25rem; background: var(--bg); cursor: default; }
.empty-map p { max-width: 380px; color: var(--muted); }

.panel label { margin-bottom: .6rem; }
.panel .btns { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .75rem; }
.zones { list-style: none; margin: 0; padding: 0; }
.zones li { display: flex; align-items: center; gap: .5rem; padding: .4rem .3rem; border-radius: 6px; cursor: pointer; }
.zones li:hover { background: #f1f5f9; }
.zones li.muted { cursor: default; }
.zones .sw { width: 12px; height: 12px; border-radius: 3px; flex: none; }
.zones .zn { flex: 1; min-width: 0; }
.zones .cnt { font-variant-numeric: tabular-nums; font-weight: 600; }
.zones .cnt.low { color: var(--danger); }
.bar { height: 5px; background: #e2e8f0; border-radius: 3px; overflow: hidden; margin-top: 3px; }
.bar i { display: block; height: 100%; background: var(--warn); }
.bar i.ok { background: var(--ok); }
.cover { padding: .4rem .6rem; border-radius: 6px; background: #f1f5f9; }
.cover.ok { background: #dcfce7; color: #166534; }
.cover.low { background: #fee2e2; color: #991b1b; }
.mini { margin: .25rem 0 .5rem; padding: 0; list-style: none; }
.mini li { padding: .2rem .3rem; cursor: pointer; border-radius: 4px; }
.mini li:hover { background: #f1f5f9; }

/* diálogos */
dialog.modal { border: 0; border-radius: 12px; padding: 0; width: min(460px, calc(100vw - 2rem)); box-shadow: 0 20px 50px rgba(15, 23, 42, .35); color: var(--ink); }
dialog.modal.wide { width: min(680px, calc(100vw - 2rem)); }
dialog.modal::backdrop { background: rgba(15, 23, 42, .5); }
dialog.modal form { padding: 1.25rem; }
.modal-body { margin-bottom: .5rem; }
.modal-actions { display: flex; align-items: center; gap: .5rem; margin-top: 1rem; }
.form-error { color: var(--danger); margin: .5rem 0 0; min-height: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 .75rem; }
.total-line { margin: 0 0 .75rem; font-size: 1rem; }
canvas.preview { max-width: 100%; max-height: 260px; border: 1px solid var(--line); border-radius: 6px; display: block; margin: .5rem auto 0; }

#toast { position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%) translateY(20px); background: #0f172a; color: #fff; padding: .55rem 1rem; border-radius: 8px; opacity: 0; pointer-events: none; transition: .2s; z-index: 1000; max-width: 90vw; }
#toast.show { opacity: 1; transform: translateX(-50%); }
#toast.error { background: var(--danger); }

/* telas menores: painéis empilham abaixo do mapa */
@media (max-width: 900px) {
  .map-layout { grid-template-columns: 1fr; grid-template-rows: minmax(380px, 62vh) auto auto; overflow-y: auto; }
  .canvas { order: 1; }
  .panel { order: 2; border-left: 0; border-top: 1px solid var(--line); }
  .side { order: 3; border-right: 0; border-top: 1px solid var(--line); max-height: 60vh; }
  main { overflow-y: auto; }
  .page-head input[type=search] { width: 100%; }
  .grid2 { grid-template-columns: 1fr; }
}
