:root {
  --bg: #f3f5f7;
  --panel: #ffffff;
  --ink: #18212f;
  --muted: #64748b;
  --border: #dbe2ea;
  --primary: #9f1d20;
  --primary-dark: #7f1417;
  --shadow: 0 12px 30px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
}
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: wait; opacity: .65; }

.app-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 18px 24px;
  color: #fff;
  background: linear-gradient(120deg, #111827, #3b1719);
}
.app-header h1 { margin: 4px 0; font-size: clamp(1.6rem, 3vw, 2.25rem); }
.app-header p { margin: 0; color: #d8dee8; }
.eyebrow { font-size: .72rem; letter-spacing: .16em; font-weight: 800; color: #eab9ba; }
.header-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.status-chip { padding: 8px 12px; border: 1px solid rgba(255,255,255,.2); border-radius: 999px; font-size: .8rem; }
.status-chip.ok { background: rgba(22, 163, 74, .22); }
.status-chip.error { background: rgba(220, 38, 38, .25); }

.layout {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(290px, 350px) minmax(0, 1fr);
}
.sidebar { min-height: 0; overflow: auto; padding: 16px; border-right: 1px solid var(--border); }
.panel { padding: 15px; margin-bottom: 13px; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); }
.panel h2 { margin: 0 0 14px; font-size: 1rem; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
label { display: grid; gap: 6px; margin-bottom: 12px; font-size: .82rem; font-weight: 700; color: #334155; }
input, select, textarea {
  width: 100%;
  padding: 10px 11px;
  border: 1px solid #cbd5e1;
  border-radius: 9px;
  background: #fff;
  color: var(--ink);
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 3px solid rgba(159, 29, 32, .13); border-color: var(--primary); }

.primary-button, .secondary-button, .text-button, .icon-button, .inline-action {
  border: 0;
  border-radius: 9px;
  font-weight: 750;
}
.primary-button { padding: 10px 15px; color: #fff; background: var(--primary); }
.primary-button:hover { background: var(--primary-dark); }
.secondary-button { padding: 10px 13px; color: #334155; background: #e9eef4; }
.secondary-button:hover { background: #dfe6ee; }
.text-button { padding: 5px; color: var(--primary); background: transparent; }
.icon-button { width: 38px; height: 38px; font-size: 1.6rem; color: #475569; background: #edf1f5; }
.inline-action { margin-top: 8px; padding: 7px 10px; color: var(--primary); background: #f8e9ea; }
.filter-summary { min-height: 18px; margin-top: 9px; color: var(--muted); font-size: .72rem; }

.metrics-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.metrics-grid article { text-align: center; padding: 9px 3px; background: #f8fafc; border-radius: 10px; }
.metrics-grid strong { display: block; font-size: 1.35rem; }
.metrics-grid span { font-size: .68rem; color: var(--muted); }

.incident-list { display: grid; gap: 9px; }
.incident-card { padding: 11px; border: 1px solid var(--border); border-left: 4px solid var(--incident-color, #64748b); border-radius: 10px; background: #fff; }
.incident-card button { width: 100%; padding: 0; text-align: left; color: inherit; background: transparent; border: 0; }
.incident-card h3 { margin: 0 0 5px; font-size: .9rem; }
.incident-card p { margin: 0 0 6px; font-size: .75rem; color: var(--muted); }
.incident-meta { display: flex; justify-content: space-between; gap: 8px; font-size: .68rem; color: #475569; }
.empty-state { display: grid; gap: 5px; padding: 18px 5px; text-align: center; color: var(--muted); font-size: .85rem; }
.empty-state strong { color: #334155; }

.map-section { position: relative; min-width: 0; min-height: 0; overflow: hidden; }
#map { position: absolute; inset: 0; width: 100%; height: 100%; background: #dce3e8; }
.map-toolbar {
  position: absolute;
  z-index: 700;
  top: 14px;
  left: 58px;
  display: flex;
  gap: 8px;
  padding: 9px 11px;
  border-radius: 10px;
  background: rgba(255,255,255,.96);
  box-shadow: var(--shadow);
}
.switch-label { display: flex; align-items: center; gap: 5px; margin: 0; font-size: .76rem; }
.switch-label input { width: auto; }
.map-note {
  position: absolute;
  z-index: 700;
  right: 14px;
  bottom: 14px;
  max-width: 420px;
  padding: 9px 12px;
  border-radius: 9px;
  background: rgba(17,24,39,.88);
  color: #fff;
  font-size: .7rem;
}
.map-status {
  position: absolute;
  z-index: 750;
  top: 62px;
  left: 14px;
  max-width: min(430px, calc(100% - 28px));
  padding: 7px 10px;
  border: 1px solid #dbe2ea;
  border-radius: 8px;
  background: rgba(255,255,255,.95);
  color: #475569;
  font-size: .72rem;
  box-shadow: var(--shadow);
  transition: opacity .2s ease;
}
.map-status.ok { color: #15803d; }
.map-status.error { color: #b91c1c; border-color: #fecaca; }
.map-status.hidden { opacity: 0; pointer-events: none; }

.leaflet-container { font-family: inherit; }
.leaflet-popup-content h3 { margin: 0 0 6px; font-size: 1rem; }
.popup-meta { margin: 3px 0; color: #475569; font-size: .76rem; }
.popup-source { display: inline-block; margin-top: 8px; color: var(--primary); font-weight: 750; }

#incidentDialog {
  position: fixed;
  inset: 8px 8px 8px auto;
  width: min(610px, calc(100vw - 24px));
  height: calc(100dvh - 16px);
  max-height: none;
  margin: 0;
  padding: 0;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 24px 70px rgba(0,0,0,.25);
  z-index: 1100;
}
#incidentDialog::backdrop { background: rgba(15,23,42,.08); }
#incidentForm { padding: 20px; }
.dialog-header { position: sticky; z-index: 5; top: -20px; display: flex; justify-content: space-between; align-items: start; margin: -20px -20px 16px; padding: 20px; background: rgba(255,255,255,.97); border-bottom: 1px solid #eef2f6; backdrop-filter: blur(8px); }
.dialog-header h2 { margin: 4px 0; }
.source-section { margin-bottom: 12px; padding: 12px; border: 1px solid #e2e8f0; border-radius: 12px; background: #f8fafc; }
.source-section label { margin-bottom: 0; }
.source-input-action { grid-template-columns: minmax(0,1fr) auto; }
.source-preview { margin-top: 8px; }
.source-preview-message { padding: 8px 10px; border-radius: 8px; background: #fff; color: var(--muted); font-size: .76rem; }
.source-preview-message.error { color: #b91c1c; background: #fff1f2; }
.source-preview-card { display: grid; grid-template-columns: 72px minmax(0,1fr); gap: 10px; align-items: start; padding: 9px; border-radius: 10px; background: #fff; border: 1px solid #e2e8f0; }
.source-preview-card:not(:has(img)) { grid-template-columns: 1fr; }
.source-preview-card img { width: 72px; height: 72px; object-fit: cover; border-radius: 8px; background: #e2e8f0; }
.source-preview-card strong { display: block; font-size: .8rem; color: #334155; }
.source-preview-card span { display: block; margin-top: 2px; color: var(--muted); font-size: .7rem; }
.source-preview-card p { margin: 5px 0 0; color: #334155; font-size: .76rem; line-height: 1.35; }
.source-preview-card small { display: block; margin-top: 5px; color: #92400e; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 0 12px; }
.full-width { grid-column: 1 / -1; }
.input-action { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 8px; }
.geocode-results { display: grid; gap: 7px; margin: -4px 0 12px; }
.geocode-result { padding: 9px 10px; text-align: left; border: 1px solid var(--border); border-radius: 9px; background: #f8fafc; }
.form-help { margin: 4px 0 10px; color: var(--muted); font-size: .75rem; line-height: 1.45; }
.form-message { min-height: 20px; margin-bottom: 8px; font-size: .8rem; font-weight: 700; }
.form-message.error { color: #b91c1c; }
.form-message.success { color: #15803d; }
.dialog-actions { position: sticky; bottom: -20px; display: flex; justify-content: flex-end; gap: 9px; margin: 0 -20px -20px; padding: 12px 20px 20px; background: linear-gradient(to top, #fff 76%, rgba(255,255,255,.75)); }

@media (max-width: 900px) {
  body { height: auto; min-height: 100dvh; overflow: auto; }
  .app-header { align-items: flex-start; flex-direction: column; }
  .layout { grid-template-columns: 1fr; min-height: auto; }
  .sidebar { overflow: visible; border-right: 0; }
  .map-section { min-height: 68vh; height: 68vh; }
}
@media (max-width: 620px) {
  .app-header { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .full-width { grid-column: auto; }
  .metrics-grid { grid-template-columns: repeat(3, minmax(0,1fr)); }
  .map-toolbar { left: 54px; flex-direction: column; }
  .map-note { left: 12px; right: 12px; max-width: none; }
  #incidentDialog { inset: 0; width: 100vw; height: 100dvh; border-radius: 0; }
  .source-input-action, .input-action { grid-template-columns: 1fr; }
}
