/* ─────────────────────────────────────────────────────────────────
   Crowd — fondo bianco, accento Cosmo
   ───────────────────────────────────────────────────────────────── */

:root {
  --bg: #FFFFFF;
  --surface: #FFFFFF;
  --surface-2: #F4F6F8;
  --border: #E6E8EB;
  --border-strong: #D0D4D9;
  --text: #1A1D21;
  --muted: #5C6570;
  --subtle: #8B939C;
  --accent: #0066ff;
  --accent-soft: #E8F1FF;
  --accent-text: #0047B3;
  --success: #1F9D55;
  --success-soft: #E7F6EE;
  --warn: #E07A00;
  --warn-soft: #FFF4E5;
  --warn-text: #9A5300;
  --danger: #E11D48;
  --danger-soft: #FDE8ED;
  --gate: #6D4AFF;
  --gate-soft: #F0ECFF;
  --gate-text: #4C2FD6;
  --blue: var(--gate);
  --blue-soft: var(--gate-soft);

  --font-sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-serif: var(--font-sans);
  --font-mono: ui-monospace, "SF Mono", Menlo, monospace;

  --shadow-sm: 0 1px 0 rgba(16, 24, 40, 0.04);
  --shadow-md: 0 1px 2px rgba(16, 24, 40, 0.05), 0 4px 14px rgba(16, 24, 40, 0.06);
  --shadow-lg: 0 8px 32px rgba(16, 24, 40, 0.10), 0 2px 6px rgba(16, 24, 40, 0.04);

  --radius-sm: 5px;
  --radius: 8px;
  --radius-lg: 12px;

  --sidebar-w: 240px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font-sans);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  letter-spacing: -0.005em;
}

button { font-family: inherit; }
input, select, textarea { font-family: inherit; color: inherit; }

/* Layout shell ─────────────────────────────────────────────────── */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  overflow: hidden;
}

.main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

.topbar {
  height: 56px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.content {
  flex: 1;
  overflow: auto;
  padding: 28px 32px 48px;
}

/* Sidebar ──────────────────────────────────────────────────────── */
.sidebar {
  background: #FFFFFF;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  overflow: hidden;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px 18px;
}
.sidebar-brand-mark {
  width: 28px;
  height: 28px;
  background: var(--accent);
  color: #fff;
  border-radius: 7px;
  display: grid;
  place-items: center;
  font-family: var(--font-sans);
  font-size: 16px;
  font-weight: 650;
  line-height: 1;
  padding-bottom: 0;
}
.sidebar-brand-name {
  font-family: var(--font-sans);
  font-size: 18px;
  font-weight: 650;
  letter-spacing: -0.02em;
  line-height: 1;
}
.sidebar-section {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--subtle);
  padding: 14px 10px 6px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  border-radius: 6px;
  color: var(--text);
  font-size: 13.5px;
  cursor: pointer;
  user-select: none;
  border: 0;
  background: transparent;
  width: 100%;
  text-align: left;
}
.nav-item:hover { background: rgba(0,0,0,0.04); }
.nav-item.active { background: var(--accent-soft); color: var(--accent-text); font-weight: 600; }
.nav-item .ico { width: 16px; height: 16px; flex-shrink: 0; color: var(--muted); }
.nav-item.active .ico { color: var(--accent); }
.nav-item-suffix {
  margin-left: auto;
  color: var(--subtle);
  font-size: 11px;
  font-family: var(--font-mono);
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.user-pill {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 7px;
  cursor: pointer;
}
.user-pill:hover { background: rgba(0,0,0,0.04); }
.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 500;
}
.user-pill-info { display: flex; flex-direction: column; line-height: 1.2; }
.user-pill-name { font-size: 13px; font-weight: 500; }
.user-pill-role { font-size: 11px; color: var(--muted); }

/* Headings ────────────────────────────────────────────────────── */
.page-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 24px;
  gap: 24px;
}
.page-title {
  font-family: var(--font-sans);
  font-size: 32px;
  line-height: 1.1;
  letter-spacing: -0.03em;
  margin: 0 0 6px;
  font-weight: 650;
}
.page-sub {
  color: var(--muted);
  font-size: 14px;
  margin: 0;
}
.page-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* Breadcrumb */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 12.5px;
  margin-bottom: 10px;
}
.breadcrumb a, .breadcrumb button {
  color: var(--muted);
  cursor: pointer;
  border: 0;
  background: transparent;
  padding: 0;
  font-size: inherit;
}
.breadcrumb a:hover, .breadcrumb button:hover { color: var(--text); }
.breadcrumb .sep { color: var(--subtle); }

/* Buttons ─────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 32px;
  padding: 0 12px;
  border-radius: 7px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: var(--shadow-sm);
}
.btn:hover { background: var(--surface-2); }
.btn .ico { width: 14px; height: 14px; }
.btn.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.btn.primary:hover { background: #0052CC; border-color: #0052CC; }
.btn.ghost {
  border-color: transparent;
  background: transparent;
  box-shadow: none;
}
.btn.ghost:hover { background: rgba(0,0,0,0.04); }
.btn.danger {
  color: var(--danger);
  border-color: var(--danger-soft);
}
.btn.sm { height: 26px; padding: 0 9px; font-size: 12px; }

.icon-btn {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 7px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn .ico { width: 16px; height: 16px; }

/* Cards ───────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card-head {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.card-title {
  font-size: 14px;
  font-weight: 500;
  margin: 0;
}
.card-sub {
  font-size: 12.5px;
  color: var(--muted);
  margin: 2px 0 0;
}
.card-body { padding: 18px; }
.card-foot {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12.5px;
  color: var(--muted);
}

/* Stats */
.stat {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.stat-label {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-weight: 500;
}
.stat-value {
  font-family: var(--font-sans);
  font-size: 40px;
  font-weight: 650;
  line-height: 1;
  letter-spacing: -0.03em;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.stat-value.mono {
  font-family: var(--font-mono);
  font-size: 32px;
  font-weight: 500;
  letter-spacing: -0.02em;
}
.stat-delta {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.stat-delta.up { color: var(--success); }
.stat-delta.down { color: var(--danger); }

/* Tables ──────────────────────────────────────────────────────── */
.tbl { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td {
  padding: 11px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  vertical-align: middle;
}
.tbl th {
  font-size: 11.5px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  background: var(--surface-2);
}
.tbl tr:last-child td { border-bottom: 0; }
.tbl tr:hover td { background: rgba(0,0,0,0.015); }
.tbl .row-action {
  visibility: hidden;
  display: flex;
  gap: 4px;
  justify-content: flex-end;
}
.tbl tr:hover .row-action { visibility: visible; }

/* Badges ──────────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 22px;
  padding: 0 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 500;
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
  letter-spacing: 0.005em;
}
.badge .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}
.badge.live { background: var(--success-soft); color: var(--success); border-color: transparent; }
.badge.offline { background: var(--surface-2); color: var(--muted); }
.badge.warn { background: var(--warn-soft); color: var(--warn-text); border-color: transparent; }
.badge.area { background: var(--accent-soft); color: var(--accent-text); border-color: transparent; }
.badge.gate { background: var(--gate-soft); color: var(--gate-text); border-color: transparent; }
.badge.admin { background: var(--accent); color: #fff; border-color: transparent; }

/* Pulsing dot for live */
.live-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 0 currentColor;
  animation: pulse 1.6s ease-out infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(31, 157, 85, 0.45); }
  70% { box-shadow: 0 0 0 8px rgba(31, 157, 85, 0); }
  100% { box-shadow: 0 0 0 0 rgba(31, 157, 85, 0); }
}

/* Forms ───────────────────────────────────────────────────────── */
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text);
}
.field-hint {
  font-size: 11.5px;
  color: var(--muted);
}
.input, .select, .textarea {
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: 7px;
  padding: 8px 10px;
  font-size: 13px;
  outline: none;
  width: 100%;
}
.input:focus, .select:focus, .textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0, 102, 255, 0.16);
}
.select { padding-right: 28px; appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path fill='%235C6570' d='M0 0h10L5 6z'/></svg>");
  background-repeat: no-repeat; background-position: right 10px center;
}

/* Segmented control */
.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border-radius: 7px;
  padding: 3px;
  gap: 2px;
}
.segmented button {
  appearance: none;
  border: 0;
  background: transparent;
  padding: 5px 12px;
  font-size: 12.5px;
  border-radius: 5px;
  color: var(--muted);
  cursor: pointer;
  font-weight: 500;
}
.segmented button.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.segmented a {
  appearance: none;
  border: 0;
  background: transparent;
  padding: 5px 12px;
  font-size: 12.5px;
  border-radius: 5px;
  color: var(--muted);
  cursor: pointer;
  font-weight: 500;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}
.segmented a.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
[hidden] { display: none !important; }

/* Modal ──────────────────────────────────────────────────────── */
.modal-bg {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 40, 0.4);
  display: grid;
  place-items: center;
  z-index: 100;
  padding: 24px;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
.modal {
  width: 520px;
  max-width: 100%;
  max-height: 90vh;
  background: var(--surface);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-head {
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-title {
  font-family: var(--font-sans);
  font-size: 18px;
  margin: 0;
  font-weight: 650;
}
.modal-body { padding: 20px 22px; display: flex; flex-direction: column; gap: 14px; overflow: auto;}
.modal-foot {
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Empty/placeholder */
.empty {
  padding: 40px 24px;
  text-align: center;
  color: var(--muted);
}
.empty-title { color: var(--text); font-size: 15px; font-weight: 500; margin: 0 0 4px; }

/* Mono numbers */
.mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* Utility ─────────────────────────────────────────────────────── */
.row { display: flex; align-items: center; gap: 12px; }
.gap-2 { gap: 8px; } .gap-3 { gap: 12px; } .gap-4 { gap: 16px; } .gap-6 { gap: 24px; }
.col { display: flex; flex-direction: column; }
.flex1 { flex: 1; }
.muted { color: var(--muted); }
.subtle { color: var(--subtle); font-size: 12px; }
.divider { height: 1px; background: var(--border); width: 100%; }
.spacer { flex: 1; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }

/* Sensor map ─────────────────────────────────────────────────── */
.mapwrap {
  position: relative;
  background:
    linear-gradient(var(--border) 1px, transparent 1px) 0 0/24px 24px,
    linear-gradient(90deg, var(--border) 1px, transparent 1px) 0 0/24px 24px,
    var(--surface-2);
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.mapwrap svg { display: block; width: 100%; height: 100%; }

.sensor-poly { fill: rgba(0, 102, 255, 0.14); stroke: var(--accent); stroke-width: 1.5; cursor: pointer; }
.sensor-poly:hover, .sensor-poly.selected { fill: rgba(0, 102, 255, 0.28); stroke-width: 2; }
.sensor-line { stroke: var(--gate); stroke-width: 3; cursor: pointer; }
.sensor-line:hover, .sensor-line.selected { stroke-width: 4.5; }
.sensor-line-arrow { fill: var(--gate); }

#map-svg { pointer-events: all; }
.map-bg { position: absolute; inset: 0; pointer-events: none; }
.map-bg svg { pointer-events: none; }
.map-hit { pointer-events: all; }
.sensor-label {
  font-size: 11px;
  font-family: var(--font-sans);
  font-weight: 500;
  fill: var(--text);
  paint-order: stroke;
  stroke: rgba(255,255,255,0.85);
  stroke-width: 3;
  pointer-events: none;
}
.zone-readout { pointer-events: none; }

.shape-pick {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  text-align: left;
  cursor: pointer;
  color: inherit;
  font: inherit;
}
.shape-pick:hover, .shape-pick.active { background: var(--accent-soft); }
#shape-editor {
  padding: 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.help-pill { display: flex; align-items: center; gap: 8px; }
.help-pill .btn { height: 24px; background: #fff; color: var(--accent-text); border-color: transparent; }

.sensor-handle {
  fill: white;
  stroke: var(--accent);
  stroke-width: 1.5;
  cursor: grab;
  pointer-events: all;
}

/* Event ticker */
.ticker {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}
.ticker-list {
  flex: 1;
  overflow: auto;
  padding: 4px 0;
}
.ticker-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 18px;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
  animation: slideIn 0.4s ease-out;
}
.ticker-item:last-child { border-bottom: 0; }
.ticker-item .time {
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--muted);
  width: 56px;
  flex-shrink: 0;
}
.ticker-item .type {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.ticker-item .type.in { background: var(--success-soft); color: var(--success); }
.ticker-item .type.out { background: var(--danger-soft); color: var(--danger); }
.ticker-item .type.warn { background: var(--warn-soft); color: var(--warn-text); }
.ticker-item .type.info { background: var(--accent-soft); color: var(--accent-text); }
.ticker-item .body { flex: 1; color: var(--text); }
.ticker-item .where { color: var(--muted); font-size: 12px; }

@keyframes slideIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Number that ticks */
.tick {
  display: inline-block;
  transition: transform 0.18s ease-out;
}
.tick.bump { animation: bump 0.45s ease-out; }
@keyframes bump {
  0% { transform: translateY(0); color: var(--text); }
  35% { transform: translateY(-3px); color: var(--accent); }
  100% { transform: translateY(0); color: var(--text); }
}

/* Heatmap cells */
.heat-cell {
  border-radius: 3px;
  border: 1px solid rgba(0,0,0,0.03);
}

/* Login ──────────────────────────────────────────────────────── */
.login-bg {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background: var(--bg);
}
.login-card {
  width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  padding: 36px;
}
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 28px; }

/* Open/closed dot */
.dot-open { width: 7px; height: 7px; border-radius: 50%; background: var(--success); display: inline-block; }
.dot-closed { width: 7px; height: 7px; border-radius: 50%; background: var(--subtle); display: inline-block; }

/* Drawer toolbar (map editor) */
.tool-bar {
  position: absolute; top: 12px; left: 12px;
  display: flex; gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  box-shadow: var(--shadow-md);
}
.tool-btn {
  width: 34px; height: 34px;
  border: 0; background: transparent;
  border-radius: 7px;
  display: grid; place-items: center;
  cursor: pointer;
  color: var(--muted);
}
.tool-btn:hover { background: rgba(0,0,0,0.04); color: var(--text); }
.tool-btn.active { background: var(--accent); color: #fff; }
.tool-btn .ico { width: 16px; height: 16px; }

.map-legend {
  position: absolute; bottom: 12px; left: 12px;
  display: flex; gap: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  box-shadow: var(--shadow-sm);
  font-size: 12px;
}
.legend-swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; margin-right: 6px; vertical-align: middle; }

.help-pill {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  background: var(--accent); color: #fff;
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 12.5px;
  box-shadow: var(--shadow-md);
  z-index: 2;
  max-width: 80%;
  text-align: center;
}

a.nav-item { text-decoration: none; }
a.quiet { color: inherit; text-decoration: none; font-weight: 500; }
a.quiet:hover { color: var(--accent-text); }

.banner {
  margin-bottom: 16px;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
}
.banner.ok { background: var(--success-soft); color: #157A40; border-color: transparent; }
.banner.err { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.banner.warn { background: var(--warn-soft); color: var(--warn-text); border-color: transparent; }

.donut {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: conic-gradient(var(--accent) var(--p, 0%), var(--surface-2) 0);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.donut::after {
  content: "";
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--surface);
}
.donut.warn { background: conic-gradient(var(--warn) var(--p, 0%), var(--surface-2) 0); }

.map-wall { fill: none; stroke: var(--text); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; cursor: pointer; }
.map-wall.selected { stroke-width: 3.5; }
.map-door { fill: var(--surface); stroke: var(--muted); stroke-width: 1.5; cursor: pointer; }
.map-door.selected { stroke: var(--text); stroke-width: 2.5; }

.install-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 32px 16px;
  background: var(--bg);
}
.install-card { width: 640px; max-width: 100%; }

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
}

.chart-box { width: 100%; overflow: hidden; }
.chart-box svg { display: block; width: 100%; height: auto; }
