*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #1a1a1c;
  --surface: #232326;
  --surface-2: #2b2b2f;
  --border: rgba(255,255,255,0.08);
  --text: #e8eaed;
  --text-muted: rgba(232,234,237,0.45);
  --blue: #8ab4f8;
  --purple: #c0a4f5;
  --wire: #8ab4f8;
  --widget-bg: #202124;
  --popup-bg: #232326;
  --pill-bg: rgba(35,35,38,0.85);
  --pill-border: rgba(255,255,255,0.13);
  --hover: rgba(255,255,255,0.07);
  --grip: rgba(255,255,255,0.18);
  --media-bg: #161618;
  --port-border: rgba(255,255,255,0.16);
  --scroll-thumb: rgba(255,255,255,0.12);
  --font: 'Google Sans', 'Segoe UI', system-ui, sans-serif;
  --font-scale: 1; /* escala global dos textos dos widgets (A− / A+) */
}

/* ── Tema claro ── */
body.light {
  --bg: #f3f4f7;
  --surface: #ffffff;
  --surface-2: #e9eaee;
  --border: rgba(20,25,35,0.12);
  --text: #1f2227;
  --text-muted: rgba(31,34,39,0.5);
  --blue: #1a73e8;
  --purple: #7c4dff;
  --wire: #1a73e8;
  --widget-bg: #ffffff;
  --popup-bg: #ffffff;
  --pill-bg: rgba(255,255,255,0.85);
  --pill-border: rgba(20,25,35,0.12);
  --hover: rgba(20,25,35,0.06);
  --grip: rgba(20,25,35,0.22);
  --media-bg: #e9ecf0;
  --port-border: rgba(20,25,35,0.25);
  --scroll-thumb: rgba(20,25,35,0.18);
  background: var(--bg);
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  touch-action: none;
}

body.panning { cursor: grabbing; }

/* durante arraste/resize/conexão, mídias não capturam o mouse
   (senão "engolem" o mousemove/mouseup e a ação trava) */
body.interacting iframe,
body.interacting video,
body.interacting .media { pointer-events: none; }
/* enquanto puxa um fio, as linhas existentes não roubam o drop */
body.interacting #wires path { pointer-events: none; }
/* impede o arraste nativo da imagem (que sequestra o mouse no resize) */
.media { -webkit-user-drag: none; user-select: none; }
img.media { -webkit-user-drag: none; }

#dots {
  position: fixed;
  inset: 0;
  display: block;
}

/* ── Camada de fios (blueprint) ── */
#tag-wires {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 4;
}
#tag-wires path {
  fill: none;
  stroke-width: 1.2;
  stroke-dasharray: 4 4;
  stroke-linecap: round;
}
#tag-wires path.tag-wire-at {
  stroke: rgba(138, 180, 248, 0.45);
  filter: drop-shadow(0 0 3px rgba(138, 180, 248, 0.25));
}
#tag-wires path.tag-wire-hash {
  stroke: rgba(129, 201, 149, 0.5);
  filter: drop-shadow(0 0 3px rgba(129, 201, 149, 0.3));
}

#wires {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 5;
}

#wires path {
  fill: none;
  stroke: var(--wire);
  stroke-width: 1.2;
  stroke-linecap: round;
  opacity: 0.6;
  filter: drop-shadow(0 0 3px rgba(138,180,248,0.25));
  pointer-events: stroke; /* clickable only on the path stroke */
  cursor: pointer;
  transition: stroke 0.15s, stroke-width 0.15s, filter 0.15s;
}

#wires path:hover {
  stroke: var(--accent-danger, #ff4b4b);
  stroke-width: 2.2;
  filter: drop-shadow(0 0 5px rgba(255, 75, 75, 0.6));
  opacity: 0.95;
}

#wires path.temp {
  stroke-dasharray: 5 6;
  opacity: 0.45;
  pointer-events: none;
}

/* ── Toolbar (canto superior direito) ── */
#toolbar {
  position: fixed;
  top: 14px;
  right: 16px;
  z-index: 40;
  display: flex;
  gap: 8px;
}

/* ── Toolbar (canto superior esquerdo: mapas) ── */
#toolbar-left {
  position: fixed;
  top: 14px;
  left: 16px;
  z-index: 40;
  display: flex;
  gap: 8px;
  align-items: flex-start;
}
#btn-board { max-width: 260px; }
#btn-board .chevron { color: var(--text-muted); flex-shrink: 0; }
#board-name {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#board-menu {
  position: absolute;
  top: 46px;
  left: 0;
  min-width: 230px;
  max-height: 60vh;
  overflow-y: auto;
}
.board-item.active { background: rgba(138,180,248,0.12); color: var(--blue); }
.board-item > span:first-child {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.board-icon {
  display: flex;
  flex-shrink: 0;
  opacity: 0;
  color: var(--text-muted);
  border-radius: 6px;
  padding: 2px;
  transition: opacity 0.12s, background 0.12s, color 0.12s;
}
.board-item .board-rename { margin-left: auto; }
.board-item:hover .board-icon { opacity: 0.6; }
.board-rename:hover { background: rgba(138,180,248,0.18); color: var(--blue); opacity: 1 !important; }
.board-del:hover { background: rgba(255,75,75,0.15); color: #ff7b7b; opacity: 1 !important; }

/* nome do mapa em edição inline */
#board-name { cursor: default; }
#board-name.editing {
  outline: 1.5px solid var(--blue);
  background: rgba(0,0,0,0.25);
  border-radius: 5px;
  padding: 0 5px;
  cursor: text;
  min-width: 40px;
}

.pill {
  display: flex;
  align-items: center;
  gap: 7px;
  background: var(--pill-bg);
  backdrop-filter: blur(10px);
  border: 1px solid var(--pill-border);
  color: var(--text);
  font-family: var(--font);
  font-size: 13.5px;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 22px;
  cursor: pointer;
  transition: background 0.13s, border-color 0.13s;
}
.pill:hover { background: var(--surface-2); border-color: var(--pill-border); }
.pill-icon { padding: 8px; }
.pill svg { color: var(--text-muted); }
.pill.saved { border-color: rgba(129,201,149,0.5); color: #81c995; }
.pill.saved svg { color: #81c995; }
.pill.error { border-color: rgba(255,75,75,0.6); color: #ff7b7b; }
.pill.error svg { color: #ff7b7b; }

/* ── Menu de contexto ── */
.popup {
  position: fixed;
  z-index: 50;
  min-width: 190px;
  background: var(--popup-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px;
  box-shadow: 0 10px 34px rgba(0,0,0,0.6);
  animation: popup-in 0.13s ease-out;
}
.popup.hidden { display: none; }

@keyframes popup-in {
  from { opacity: 0; transform: scale(0.95) translateY(3px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}

.popup-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 13.5px;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
  text-align: left;
  transition: background 0.12s;
}
.popup-item:hover { background: var(--hover); }
.popup-item.disabled { color: var(--text-muted); opacity: 0.55; pointer-events: none; }
.popup-item .kbd {
  margin-left: auto;
  color: var(--text-muted);
  font-size: 12px;
}
.popup-item.disabled .kbd { color: rgba(232,234,237,0.18); }

.popup-sep {
  height: 1px;
  background: var(--border);
  margin: 5px 8px;
}

/* submenu */
.has-sub .sub-arrow { margin-left: auto; color: var(--text-muted); }

.submenu {
  display: none;
  position: absolute;
  left: calc(100% - 4px);
  top: -6px;
  min-width: 160px;
  background: var(--popup-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px;
  box-shadow: 0 10px 34px rgba(0,0,0,0.6);
}
.has-sub:hover .submenu { display: block; }
.submenu.flip { left: auto; right: calc(100% - 4px); }

/* ── Widgets ── */
#widgets { position: fixed; inset: 0; pointer-events: none; z-index: 10; transform-origin: 0 0; }

.widget {
  position: absolute;
  width: 440px;
  max-width: calc(100vw - 40px);
  pointer-events: auto;
  background: var(--widget-bg);
  border: 1px solid var(--w-border, var(--border));
  border-radius: 20px;
  box-shadow: 0 10px 36px rgba(0,0,0,0.45);
  animation: widget-in 0.18s ease-out;
  transition: border-color 0.2s, border-radius 0.25s;
  display: flex;
  flex-direction: column;
}

/* corpo cresce com o widget; rodapé fica sempre no fim (não-circular) */
.widget-grip, .widget-title, .widget-footer { flex: 0 0 auto; }
.widget:not(.circular) .widget-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.widget:not(.circular) .widget-input {
  flex: 1 1 auto;
  max-height: none;
}

.widget.dragging { cursor: grabbing; box-shadow: 0 16px 48px rgba(0,0,0,0.6); }

/* forma circular */
.widget.circular {
  width: 300px;
  height: 300px;
  border-radius: 50%;
}
.widget.circular .widget-grip { padding-top: 22px; }
.widget.circular .widget-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 44px;
  overflow: hidden;
}
.widget.circular .widget-input {
  text-align: center;
  max-height: 100%;
  overflow-y: auto;
}
/* rodapé sempre na parte de baixo do círculo */
.widget.circular .widget-footer {
  margin-top: auto;
  justify-content: center;
  padding: 8px 40px 24px;
}
.widget.circular .widget-config { top: 30px; right: 44px; }

@keyframes widget-in {
  from { opacity: 0; transform: scale(0.96) translateY(6px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}

.widget-grip {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px 0 6px;
  min-height: 24px;
  cursor: grab;
  color: var(--grip);
}
.widget-grip:active { cursor: grabbing; }

/* botões do canto superior direito */
.widget-config {
  position: absolute;
  top: 3px;
  right: 4px;
  z-index: 12;
  width: 26px;
  height: 26px;
}
.widget-minimize {
  position: absolute;
  top: 3px;
  right: 32px;
  z-index: 12;
  width: 26px;
  height: 26px;
}
.widget-minimize .ic-restore { display: none; }
.widget.minimized .widget-minimize .ic-min { display: none; }
.widget.minimized .widget-minimize .ic-restore { display: block; }

/* estado minimizado: só o título */
.widget.minimized {
  height: auto !important;
  border-radius: 16px !important;
}
.widget.minimized .widget-body,
.widget.minimized .widget-footer,
.widget.minimized .widget-resize {
  display: none;
}
.widget.minimized .widget-title {
  padding-bottom: 14px;
}
.widget.minimized .widget-title:empty::before { content: 'Sem título'; }
/* círculo minimizado vira um pill compacto */
.widget.circular.minimized {
  width: 280px;
  display: block;
}
.widget.circular.minimized .widget-title { text-align: left; padding: 4px 64px 14px 18px; }

/* título do widget */
.widget-title {
  padding: 4px 64px 0 18px;
  font-size: calc(16px * var(--font-scale));
  font-weight: 600;
  line-height: 1.3;
  color: var(--text);
  outline: none;
  word-break: break-word;
}
.widget-title:empty::before {
  content: attr(data-placeholder);
  color: var(--text-muted);
  opacity: 0.5;
  pointer-events: none;
}
.widget.circular .widget-title { text-align: center; padding: 18px 48px 0; }

.widget-body { padding: 8px 18px 0; }

.widget-input {
  min-height: 48px;
  max-height: 200px;
  overflow-y: auto;
  outline: none;
  font-size: calc(14.5px * var(--font-scale));
  line-height: 1.6;
  color: var(--text);
  word-break: break-word;
}

.widget-input:empty::before {
  content: attr(data-placeholder);
  color: var(--text-muted);
  pointer-events: none;
}

/* marcadores / listas */
.widget-input ul {
  margin: 4px 0;
  padding-left: 20px;
  text-align: left;
  list-style-position: outside;
}
.widget-input ul li { margin: 2px 0; }
.widget-input ul li::marker { color: var(--blue); }

/* mídia (imagem, vídeo, pdf, site) */
.media-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.media-row .media-url { flex: 1; }

.media-upload {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  background: var(--surface);
}
.media-upload:hover { border-color: rgba(138,180,248,0.5); }

.media-url {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-family: var(--font);
  font-size: 13px;
  padding: 9px 12px;
  outline: none;
}
.media-url:focus { border-color: rgba(138,180,248,0.5); }
.media-url::placeholder { color: var(--text-muted); }

.media {
  display: block;
  width: 100%;
  border-radius: 12px;
  border: none;
}
img.media, video.media { max-height: 320px; object-fit: contain; background: var(--media-bg); }
.media-frame { height: 300px; background: var(--media-bg); }

.widget-footer {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 14px 14px;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.widget-close {
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 16px;
  transition: background 0.12s, color 0.12s;
}
.widget-close:hover { background: var(--hover); color: var(--text); }

/* ── Menu de configuração do widget ── */
.config-menu {
  position: absolute;
  top: 40px;
  right: 10px;
  z-index: 20;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  box-shadow: 0 8px 26px rgba(0,0,0,0.5);
  animation: popup-in 0.12s ease-out;
}
.config-menu.hidden { display: none; }

.widget.circular .config-menu { top: 62px; right: 44px; }

.cfg-shape {
  display: block;
  width: 100%;
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  font-family: var(--font);
  font-size: 12.5px;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 10px;
  transition: background 0.12s;
}
.cfg-shape:hover { background: var(--surface-2); }

.cfg-colors {
  display: flex;
  gap: 7px;
}

.cfg-color {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.12s, border-color 0.12s;
}
.cfg-color:hover { transform: scale(1.2); }
.cfg-color.selected { border-color: var(--text); }

/* ── Portas de conexão ── */
.port {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: transparent;
  border: 1.5px solid var(--port-border);
  opacity: 0.6;
  cursor: crosshair;
  transition: transform 0.12s, background 0.12s, border-color 0.12s, box-shadow 0.12s, opacity 0.12s;
  z-index: 11;
}
/* área de acerto invisível maior (facilita mirar, principalmente com zoom out) */
.port::after {
  content: '';
  position: absolute;
  inset: -9px;
  border-radius: 50%;
}
.port:hover, .port.active {
  opacity: 1;
  background: var(--blue);
  border-color: var(--blue);
  transform: scale(1.4);
  box-shadow: 0 0 10px rgba(138,180,248,0.7);
}
.port.connected {
  opacity: 1;
  background: var(--blue);
  border-color: var(--blue);
  box-shadow: 0 0 6px rgba(138,180,248,0.45);
}

.port-top    { top: -6px;    left: 50%; margin-left: -5px; }
.port-bottom { bottom: -6px; left: 50%; margin-left: -5px; }
.port-left   { left: -6px;   top: 50%;  margin-top: -5px; }
.port-right  { right: -6px;  top: 50%;  margin-top: -5px; }

/* ── Tags ── */
.tag {
  display: inline-block;
  font-size: calc(13px * var(--font-scale));
  font-weight: 500;
  padding: 1px 9px;
  margin: 0 1px;
  border-radius: 20px;
  white-space: nowrap;
}
.tag-at {
  color: #8ab4f8;
  background: rgba(138,180,248,0.13);
  border: 1px solid rgba(138,180,248,0.3);
}
.tag-hash {
  color: #81c995;
  background: rgba(129,201,149,0.13);
  border: 1px solid rgba(129,201,149,0.3);
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--scroll-thumb); border-radius: 3px; }

/* ── Popup Header Title ── */
.popup-title {
  font-size: 10px;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 6px 12px 3px;
  font-weight: 600;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}

/* ── Widget Resize Handle ── */
.widget-resize {
  position: absolute;
  bottom: 6px;
  right: 6px;
  width: 12px;
  height: 12px;
  cursor: se-resize;
  color: var(--grip);
  transition: color 0.12s;
  z-index: 15;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
}
.widget-resize:hover {
  color: var(--blue);
}
.widget.resizing {
  border-color: rgba(138, 180, 248, 0.5);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
}

/* Custom circular resize handle positioning */
.widget.circular .widget-resize {
  display: flex;
  bottom: 13.5%;
  right: 13.5%;
  transform: translate(50%, 50%);
}

.port .scissors-icon {
  display: none;
  pointer-events: none;
  color: #fff;
}
.port.connected:hover {
  background: var(--accent-danger, #ff4b4b) !important;
  border-color: var(--accent-danger, #ff4b4b) !important;
  box-shadow: 0 0 10px rgba(255, 75, 75, 0.7) !important;
  transform: scale(1.4);
  display: flex;
  align-items: center;
  justify-content: center;
}
.port.connected:hover .scissors-icon {
  display: block;
}

.widget.selected {
  border-color: transparent !important;
  box-shadow: 0 0 25px rgba(192, 164, 245, 0.22), 0 10px 36px rgba(0,0,0,0.5);
}
.widget.selected::before {
  content: '';
  position: absolute;
  inset: -1.5px;
  border-radius: 20px;
  background: var(--w-border, linear-gradient(135deg, #c0a4f5 0%, #8ab4f8 50%, #f48fb1 100%));
  background-size: 200% 200%;
  z-index: -1;
  animation: border-glow-flow 4s linear infinite;
  pointer-events: none;
}
.widget.circular.selected::before {
  border-radius: 50%;
}
@keyframes border-glow-flow {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* ── Retângulo de seleção (marquee) ── */
#marquee {
  position: fixed;
  z-index: 30;
  border: 1px solid var(--blue);
  background: rgba(138,180,248,0.10);
  border-radius: 4px;
  pointer-events: none;
}
body.light #marquee { background: rgba(26,115,232,0.08); }

/* ── Botão de tema (sol/lua) ── */
#btn-theme .ic-sun { display: none; }
body.light #btn-theme .ic-sun { display: block; }
body.light #btn-theme .ic-moon { display: none; }

/* ── Ajustes de contraste no tema claro ── */
body.light .tag-at {
  color: #1a5dc8;
  background: rgba(26,115,232,0.10);
  border-color: rgba(26,115,232,0.35);
}
body.light .tag-hash {
  color: #188038;
  background: rgba(24,128,56,0.10);
  border-color: rgba(24,128,56,0.35);
}
body.light #board-name.editing { background: var(--hover); }
body.light .widget { box-shadow: 0 10px 32px rgba(30,40,60,0.18); }
body.light .popup,
body.light .submenu { box-shadow: 0 10px 34px rgba(30,40,60,0.22); }
body.light #tag-wires path.tag-wire-at { stroke: rgba(26,115,232,0.45); }
body.light #tag-wires path.tag-wire-hash { stroke: rgba(24,128,56,0.5); }
