/* === Shared Base Layout === */
*, *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: #0b1220;
  color: #e5e7eb;
  font-family: Arial, sans-serif;
}

body {
  display: flex;
  justify-content: center;
}

.app {
  width: 100%;
  max-width: 620px;
  height: 100dvh;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #0b1220;
  position: relative;
  overflow: hidden;
}

#page-container {
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
}
#page-container > .app {
  width: 100%;
  max-width: none;
}

/* === Topbar === */
.topbar {
  flex: 0 0 auto;
  padding: 10px;
  border-bottom: 1px solid #1f2a3d;
  background: #0f172a;
  position: sticky;
  top: 0;
  z-index: 20;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.app-title-row {
  min-width: 0;
}

.title {
  font-size: 18px;
  font-weight: 800;
  letter-spacing: -0.5px;
  min-width: 0;
  white-space: nowrap;
}

.title-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.section-name {
  margin-left: 8px;
  color: #94a3b8;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0;
}

.app-title-actions {
  justify-content: flex-end;
}

.title-action-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}

.title-action-btn.ai-btn {
  min-width: 28px;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.title-action-btn.health-btn {
  min-width: 42px;
  padding: 0 8px;
  color: #86efac;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.02em;
}

.title-action-btn.health-btn.health-ok {
  border-color: rgba(34, 197, 94, 0.55);
  background: rgba(20, 83, 45, 0.38);
  color: #bbf7d0;
}

.title-action-btn.health-btn.health-warn {
  border-color: rgba(245, 158, 11, 0.62);
  background: rgba(120, 53, 15, 0.42);
  color: #fde68a;
}

.title-action-btn.health-btn.health-bad {
  border-color: rgba(239, 68, 68, 0.72);
  background: rgba(127, 29, 29, 0.5);
  color: #fecaca;
}

.title-timer {
  min-width: 32px;
  text-align: right;
}

.logo-vb { color: #3b82f6; }
.logo-2 { color: #f59e0b; }
.logo-map { color: #e5e7eb; }

.data-age {
  font-size: 11px;
  color: #64748b;
  white-space: nowrap;
}

.data-age.stale { color: #f59e0b; }

.reload-btn {
  height: 24px;
  min-width: 24px;
  border: 1px solid #334155;
  border-radius: 8px;
  background: #111827;
  color: #e5e7eb;
  font-size: 14px;
  cursor: pointer;
}

.reload-btn:hover { background: #172033; }

@media (max-width: 520px) {
  .title-row {
    gap: 6px;
  }

  .title {
    font-size: 16px;
  }

  .section-name {
    margin-left: 5px;
    font-size: 11px;
  }

  .title-right {
    gap: 4px;
  }

  .title-action-btn {
    width: 24px;
    min-width: 24px;
    height: 24px;
    padding: 0;
    font-size: 13px;
  }

  .title-action-btn.ai-btn {
    font-size: 10px;
  }

  .title-timer {
    min-width: 24px;
    font-size: 10px;
  }
}

/* === System health popover === */
.system-health-popover {
  position: fixed;
  inset: 0;
  z-index: 260;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 54px 10px 10px;
  background: rgba(2, 6, 23, 0.28);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.16s ease, visibility 0.16s ease;
}

.system-health-popover.open {
  opacity: 1;
  visibility: visible;
}

.health-card {
  width: min(460px, 100%);
  max-height: min(78vh, 620px);
  overflow: auto;
  border: 1px solid #1f3b5c;
  border-radius: 16px;
  background:
    radial-gradient(circle at 12% 0%, rgba(56, 189, 248, 0.18), transparent 34%),
    linear-gradient(180deg, #101a2d 0%, #0b1220 100%);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.5);
  padding: 12px;
}

.health-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.health-kicker {
  color: #38bdf8;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.health-title {
  color: #f8fafc;
  font-size: 18px;
  font-weight: 900;
  line-height: 1.15;
}

.health-close {
  width: 28px;
  height: 28px;
  border: 1px solid #334155;
  border-radius: 9px;
  background: #0f172a;
  color: #cbd5e1;
  font-size: 18px;
  cursor: pointer;
}

.health-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  margin-bottom: 10px;
}

.health-grid > div {
  min-width: 0;
  border: 1px solid rgba(51, 65, 85, 0.9);
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.72);
  padding: 7px 8px;
}

.health-grid span,
.health-row-sub,
.health-foot {
  color: #94a3b8;
  font-size: 10px;
}

.health-grid strong {
  display: block;
  overflow: hidden;
  color: #e5e7eb;
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.health-section-title {
  margin: 12px 2px 6px;
  color: #bfdbfe;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.health-list {
  display: grid;
  gap: 6px;
}

.health-row {
  border: 1px solid rgba(51, 65, 85, 0.86);
  border-radius: 11px;
  background: rgba(15, 23, 42, 0.68);
  padding: 7px 8px;
}

.health-row-main {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  color: #cbd5e1;
  font-size: 11px;
}

.health-row-main strong {
  color: #f8fafc;
  white-space: nowrap;
}

.health-row-main span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.health-dot {
  width: 8px;
  height: 8px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #64748b;
  box-shadow: 0 0 0 3px rgba(100, 116, 139, 0.12);
}

.health-ok .health-dot { background: #22c55e; box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.14); }
.health-warn .health-dot { background: #f59e0b; box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.14); }
.health-bad .health-dot { background: #ef4444; box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.14); }
.health-muted .health-dot { background: #64748b; }

.health-row-error {
  margin-top: 4px;
  color: #fca5a5;
  font-size: 10px;
  line-height: 1.35;
}

.health-empty {
  border: 1px dashed #334155;
  border-radius: 10px;
  color: #94a3b8;
  font-size: 11px;
  padding: 8px;
}

.health-foot {
  margin-top: 10px;
  line-height: 1.4;
}

.health-foot code {
  color: #bfdbfe;
}

/* === Navigation tabs === */
.map-toggle {
  display: flex;
  background: #111827;
  border: 1px solid #334155;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 6px;
}

.map-toggle .toggle-btn {
  flex: 1;
  height: 26px;
  border: none;
  background: transparent;
  color: #94a3b8;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  padding: 0 12px;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.map-toggle .toggle-btn:not(:last-child) {
  border-right: 1px solid #334155;
}

.map-toggle .toggle-btn.active {
  background: #3b82f6;
  color: #fff;
}

.map-toggle .toggle-btn:hover:not(.active) {
  background: #1e293b;
  color: #cbd5e1;
}

/* === Controls === */
.controls-row {
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
  align-items: center;
  flex-wrap: nowrap;
}

.toggle-group {
  display: flex;
  flex: 1;
  min-width: 0;
  background: #111827;
  border: 1px solid #334155;
  border-radius: 8px;
  overflow: hidden;
}

.toggle-group .toggle-btn {
  flex: 1;
  min-width: 0;
  height: 24px;
  border: none;
  background: transparent;
  color: #94a3b8;
  font-size: 10px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  padding: 0 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
}

.toggle-group .toggle-btn:not(:last-child) {
  border-right: 1px solid #334155;
}

.toggle-group .toggle-btn:hover { background: #1e293b; color: #cbd5e1; }
.toggle-group .toggle-btn.active { background: #3b82f6; color: #fff; }

/* === Heatmap === */
.heatmap-wrap {
  flex: 1 1 auto;
  min-height: 0;
  padding: 6px;
  position: relative;
}

.heatmap-card {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #0f172a;
  border: 1px solid #1f2a3d;
  touch-action: manipulation;
}

svg {
  width: 100%;
  height: 100%;
  display: block;
  shape-rendering: crispEdges;
}

/* === Treemap tiles === */
.tile { cursor: pointer; }

.tile-rect {
  stroke: rgba(255,255,255,0.22);
  stroke-width: 1;
  rx: 0;
  ry: 0;
  transition: all 0.1s ease;
}

.tile:hover .tile-rect,
.tile.active .tile-rect {
  filter: brightness(1.08);
  stroke: rgba(255,255,255,0.55);
}

.tile-text { pointer-events: none; stroke: none; }
.symbol-text { font-weight: 700; pointer-events: none; stroke: none; }

/* === Tooltip === */
.tooltip {
  position: fixed;
  z-index: 100;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.15s, visibility 0.15s;
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 11px;
  color: #e5e7eb;
  line-height: 1.4;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  white-space: nowrap;
}

.tooltip.show { opacity: 1; visibility: visible; }

/* === Semantic colors === */
.positive { color: #4ade80; }
.negative { color: #f87171; }
.neutral { color: #cbd5e1; }
.warning { color: #fbbf24; }

/* === Help overlay === */
.help-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s, visibility 0.2s;
}

.help-overlay.open { opacity: 1; visibility: visible; }

.help-content {
  width: 94%;
  max-width: 480px;
  max-height: 85vh;
  background: #0f172a;
  border: 1px solid #22314a;
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 40px rgba(0,0,0,0.4);
}

.help-section-title {
  font-size: 13px;
  font-weight: 800;
  color: #3b82f6;
  margin-bottom: 8px;
  letter-spacing: 0.7px;
  text-transform: uppercase;
}

/* === Notification overlay === */
.notif-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s, visibility 0.2s;
}

.notif-overlay.open { opacity: 1; visibility: visible; }

.notif-content {
  width: 94%;
  max-width: 460px;
  max-height: 80vh;
  background: #0f172a;
  border: 1px solid #22314a;
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 40px rgba(0,0,0,0.4);
}

.settings-panel-root {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.settings-section {
  border: 1px solid #1e293b;
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.72);
  padding: 12px;
}

.settings-section-header {
  margin-bottom: 10px;
}

.settings-section-title {
  font-size: 12px;
  font-weight: 800;
  color: #e2e8f0;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.settings-section-subtitle {
  margin-top: 4px;
  font-size: 11px;
  line-height: 1.4;
  color: #64748b;
}

.settings-section-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.settings-row-stack {
  align-items: flex-start;
  flex-direction: column;
}

.settings-control-label {
  font-size: 11px;
  color: #94a3b8;
}

.settings-chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.settings-chip {
  min-width: 44px;
}

.settings-inline-btn {
  height: 32px;
  padding: 0 10px;
}

.settings-permission-status {
  font-size: 12px;
  color: #cbd5e1;
  line-height: 1.4;
}

.settings-builtins,
.settings-source-rules {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.settings-placeholder {
  font-size: 11px;
  color: #64748b;
  border: 1px dashed #334155;
  border-radius: 8px;
  padding: 10px 12px;
  background: rgba(15, 23, 42, 0.35);
}

/* === AI Analysis Panel === */
.ai-overlay {
  position: fixed;
  inset: 0;
  z-index: 240;
  background: rgba(2, 6, 23, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.18s ease, visibility 0.18s ease;
}

.ai-overlay.open {
  opacity: 1;
  visibility: visible;
}

.ai-card {
  width: min(1280px, 100%);
  height: min(92vh, 920px);
  background: linear-gradient(180deg, #0f172a 0%, #111827 100%);
  border: 1px solid #22314a;
  border-radius: 18px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.45);
}

.ai-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px 12px;
  border-bottom: 1px solid #1f2a3d;
}

.ai-title {
  font-size: 18px;
  font-weight: 900;
  color: #f8fafc;
}

.ai-subtitle,
.ai-meta {
  margin-top: 4px;
  font-size: 12px;
  line-height: 1.4;
  color: #94a3b8;
}

.ai-signal-row {
  padding: 8px 18px 0;
  min-height: 28px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.ai-signal-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 11px;
  font-weight: 700;
  border: 1px solid transparent;
  background: rgba(15, 23, 42, 0.8);
  color: #e2e8f0;
}

.ai-signal-chip strong {
  color: inherit;
}

.ai-signal-chip.positive {
  background: rgba(34, 197, 94, 0.12);
  border-color: rgba(34, 197, 94, 0.24);
  color: #86efac;
}

.ai-signal-chip.negative {
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.24);
  color: #fca5a5;
}

.ai-signal-chip.neutral {
  background: rgba(59, 130, 246, 0.12);
  border-color: rgba(59, 130, 246, 0.24);
  color: #93c5fd;
}

.ai-header-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.ai-btn,
.ai-secondary-btn {
  height: 30px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid #334155;
  background: #111827;
  color: #e5e7eb;
  cursor: pointer;
}

.ai-btn:hover,
.ai-secondary-btn:hover {
  background: #1e293b;
}

.ai-btn {
  min-width: 30px;
  padding: 0;
  font-weight: 800;
}

.ai-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 12px 18px;
  border-bottom: 1px solid #1f2a3d;
  align-items: end;
}

.ai-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 160px;
  color: #cbd5e1;
  font-size: 11px;
}

.ai-field.ai-small {
  min-width: 110px;
  width: 110px;
}

.ai-field input,
.ai-field select {
  height: 32px;
  border-radius: 8px;
  border: 1px solid #334155;
  background: #0b1220;
  color: #e5e7eb;
  padding: 0 10px;
  font-size: 12px;
  outline: none;
}

.ai-field input:focus,
.ai-field select:focus {
  border-color: #3b82f6;
}

.ai-switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  color: #cbd5e1;
  font-size: 12px;
  user-select: none;
}

.ai-grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  padding: 12px 18px 18px;
}

.ai-panel {
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid #1e293b;
  border-radius: 14px;
  background: rgba(15, 23, 42, 0.72);
  overflow: hidden;
}

.ai-panel-head {
  flex: 0 0 auto;
  padding: 10px 12px;
  border-bottom: 1px solid #1f2a3d;
  font-size: 12px;
  font-weight: 800;
  color: #e2e8f0;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.ai-panel-head-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.ai-inline-btn {
  flex: 0 0 auto;
  height: 24px;
  border: 1px solid rgba(59, 130, 246, 0.45);
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.92);
  color: #bfdbfe;
  padding: 0 10px;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  cursor: pointer;
}

.ai-inline-btn:hover {
  background: rgba(30, 64, 175, 0.42);
  color: #eff6ff;
}

.ai-pre {
  flex: 1;
  min-height: 0;
  margin: 0;
  padding: 12px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  color: #cbd5e1;
  font-size: 12px;
  line-height: 1.55;
}

.ai-context-summary {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 12px;
}

.ai-summary {
  display: grid;
  gap: 12px;
}

.ai-summary-cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.ai-summary-card,
.ai-summary-block,
.ai-summary-note {
  border: 1px solid rgba(51, 65, 85, 0.86);
  border-radius: 12px;
  background:
    radial-gradient(circle at 0% 0%, rgba(59, 130, 246, 0.1), transparent 35%),
    rgba(15, 23, 42, 0.76);
}

.ai-summary-card {
  min-width: 0;
  padding: 9px 10px;
}

.ai-summary-card span,
.ai-summary-line span {
  display: block;
  color: #94a3b8;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.ai-summary-card strong {
  display: block;
  overflow: hidden;
  margin-top: 4px;
  color: #f8fafc;
  font-size: 14px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ai-summary-card small {
  display: block;
  overflow: hidden;
  margin-top: 3px;
  color: #93c5fd;
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ai-summary-card.is-ok {
  border-color: rgba(34, 197, 94, 0.35);
  background: rgba(20, 83, 45, 0.18);
}

.ai-summary-card.is-warn {
  border-color: rgba(245, 158, 11, 0.35);
  background: rgba(120, 53, 15, 0.18);
}

.ai-summary-block {
  padding: 10px;
}

.ai-summary-title {
  margin-bottom: 7px;
  color: #bfdbfe;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.ai-summary-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 0;
  border-top: 1px solid rgba(51, 65, 85, 0.58);
}

.ai-summary-line:first-of-type {
  border-top: 0;
}

.ai-summary-line strong {
  color: #e5e7eb;
  font-size: 12px;
  white-space: nowrap;
}

.ai-summary-note,
.ai-summary-empty {
  color: #94a3b8;
  font-size: 11px;
  line-height: 1.5;
  padding: 10px;
}

.ai-summary-note code {
  color: #bfdbfe;
  word-break: break-all;
}

@media (max-width: 900px) {
  .ai-grid {
    grid-template-columns: 1fr;
  }

  .ai-summary-cards {
    grid-template-columns: 1fr;
  }
}

/* === Media Queries === */
@media (min-width: 768px) {
  .app { max-width: 960px; }
  #page-container { max-width: 960px; }
}
@media (max-width: 520px) {
  .topbar { padding: 8px; }
  .title { font-size: 16px; }
  .toggle-group .toggle-btn { font-size: 9px; height: 22px; }
  .map-toggle .toggle-btn { font-size: 10px; }
}

@media (hover: none) and (pointer: coarse) {
  .tooltip { display: none; }
  .tile { cursor: default; }
}
