/* Loading states: spinners, skeletons, fades, copy fields */

/* ---------- spinner (in buttons and in the top bar) */
.spin {
  display: inline-block; flex: none; box-sizing: border-box;
  width: 0; height: 14px; margin-right: -6px; /* collapsed: takes no space (buttons use gap: 6px) */
  border: 0 solid currentColor; border-right-color: transparent; border-radius: 50%;
  opacity: 0; vertical-align: -2px;
  animation: spin .75s linear infinite;
  transition: width .22s ease, margin .22s ease, opacity .22s ease, border-width .22s ease;
}
.busy > .spin { width: 14px; margin-right: 0; border-width: 2px; opacity: .9; }
.btn.busy, .btn[aria-busy="true"] { cursor: progress; }
.btn.busy:disabled { opacity: 1; }
.icon-btn > .spin { height: 12px; }
.icon-btn.busy > .spin { width: 12px; }
@keyframes spin { to { transform: rotate(360deg); } }

.activity {
  display: inline-flex; align-items: center; justify-content: center; width: 20px; height: 20px;
  opacity: 0; transform: scale(.5); transition: opacity .3s ease, transform .3s ease; pointer-events: none;
  color: var(--primary);
}
.activity .spin { width: 16px; height: 16px; margin: 0; border-width: 2px; opacity: 1; transition: none; }
.activity.on { opacity: 1; transform: scale(1); }

/* switch while its change is being applied */
.switch.busy .slider { opacity: .55; }
.switch.busy .slider::after {
  border: 2px solid var(--edge-ok); border-right-color: transparent; box-sizing: border-box;
  animation: spin .75s linear infinite;
}
.switch .slider::after { transition: transform .15s, background .15s; }

/* ---------- skeletons */
.skel {
  display: block; flex: none; border-radius: 6px; max-width: 100%;
  background: linear-gradient(90deg,
    color-mix(in srgb, var(--text) 8%, transparent) 0%,
    color-mix(in srgb, var(--text) 16%, transparent) 45%,
    color-mix(in srgb, var(--text) 8%, transparent) 90%);
  background-size: 220% 100%;
  animation: shimmer 1.35s ease-in-out infinite;
}
@keyframes shimmer { from { background-position: 110% 0; } to { background-position: -110% 0; } }
.skel-text { min-width: 110px; color: transparent !important; position: relative; }
.skel-text::after { content: ""; position: absolute; inset: 7px 10px; border-radius: 6px; background: inherit; }
#userBtn.skel-text::after {
  background: linear-gradient(90deg, var(--surface-2), var(--border), var(--surface-2)); background-size: 220% 100%;
  animation: shimmer 1.35s ease-in-out infinite;
}

.skel-tile {
  width: 310px; height: 196px; box-sizing: border-box; margin-bottom: 64px;
  display: flex; flex-direction: column; gap: 11px; padding: 14px 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
.skel-row { display: flex; gap: 8px; margin-top: auto; }
.board-skel {
  position: absolute; inset: 0; display: flex; justify-content: center; gap: 260px; padding-top: 56px;
  pointer-events: none; transition: opacity .35s ease, visibility .35s;
}
.board-skel.gone { opacity: 0; visibility: hidden; }
.world { transition: opacity .35s ease; }
.world.loading { opacity: 0; }
.world.animate { transition: transform .4s cubic-bezier(.2, .7, .2, 1), opacity .35s ease; }
.skel-tray { display: flex; flex-direction: column; gap: 7px; border-style: solid; }
.agent-head .skel { margin-right: 6px; }

/* ---------- fades */
.fade-in { animation: fadeIn .3s ease both; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } }
main.view-in { animation: fadeOnly .25s ease backwards; }
main.stale { opacity: .55; }
main { transition: opacity .4s ease; }
/* tiles are positioned with transform – fade them with opacity only */
.node { animation: fadeOnly .3s ease backwards; }
@keyframes fadeOnly { from { opacity: 0; } }
.toast { animation: fadeIn .2s ease both; }

/* ---------- dialog: wide variant & copy fields */
dialog.wide { width: min(760px, calc(100vw - 32px)); }
.copy-field { display: grid; grid-template-columns: 1fr auto; gap: 6px 8px; align-items: start; }
.copy-label { grid-column: 1 / -1; font-weight: 500; font-size: 13px; }
.copy-field textarea, .copy-field input {
  width: 100%; box-sizing: border-box; resize: none; margin: 0;
  font-family: var(--mono); font-size: 12.5px; line-height: 1.5;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text);
  white-space: pre-wrap; word-break: break-all; overflow: hidden; cursor: text;
}
.copy-field textarea:focus, .copy-field input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); outline: none; }
.copy-field .btn { align-self: stretch; min-width: 96px; }
.token-field { padding: 10px 12px; border-radius: 10px; background: var(--primary-soft); }
.token-field .copy-label { color: var(--primary); }
.token-field input { background: var(--surface); font-size: 13.5px; font-weight: 600; }
.btn.copied { color: var(--ok); border-color: var(--ok); }
.os-pane { display: flex; flex-direction: column; gap: 12px; }

/* ---------- inline spinner for automatic waits (retry strips, badges, connection) */
.spin.inline { width: 12px; height: 12px; margin: 0 2px 0 0; border-width: 2px; opacity: .95; transition: none; vertical-align: -2px; }
.badge .spin.inline { width: 10px; height: 10px; }
.conn .spin.inline { color: var(--warn); }
.conn:not(.on) i { display: none; }
.node-strip.retry { background: var(--warn-soft); color: var(--warn); border-color: color-mix(in srgb, var(--warn) 35%, transparent); }
.node-strip .spin.inline { margin-right: 4px; }

/* ---------- agent port dialog */
.select-row { display: flex; gap: 8px; align-items: stretch; }
.select-row select { flex: 1; min-width: 0; }
.select-row .btn { flex: none; }
.title-badge { font-size: 11px; vertical-align: 3px; margin: 0 8px 0 0; padding: 1px 8px; }

/* ---------- optimistic drag & drop */
.node.leaving { opacity: 0; transform-origin: center; transition: opacity .2s ease; }
.node.pending { border-style: dashed; }
.node.pending .node-name { color: var(--muted); }
.node-info .skel { display: inline-block; vertical-align: -1px; }
.node-stats { min-height: 18px; align-items: center; }
.tray-item { will-change: transform; }
.tray-item.pending { cursor: progress; }
.tray-item .skel { margin-top: 5px; }
.tray-item .t-spin { position: absolute; top: 8px; right: 8px; }
.tray-list { overflow-y: hidden; }

/* ---------- agents: ownership, latency, updates */
.agent-group { margin-bottom: 26px; }
.group-head { display: flex; align-items: baseline; gap: 12px; margin: 4px 0 10px; flex-wrap: wrap; }
.group-head h3 { margin: 0; font-size: 14px; letter-spacing: .01em; }
.group-head .count { display: inline-block; min-width: 20px; padding: 0 6px; margin-left: 4px; border-radius: 99px; background: var(--surface-2); color: var(--muted); font-size: 12px; text-align: center; }
.agent-card.mine { border-left: 3px solid var(--primary); }
.owner-badge { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.badge.lat { font-variant-numeric: tabular-nums; }
.update-badge { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.small-empty { padding: 16px; font-size: 13px; }
.node.pending > .node-core { height: var(--tile-h, 222px); overflow: hidden; }

/* ---------- node body + expandable panel (manual send / live log) */
.node-core { position: relative; }
.node-toggle { transition: transform .2s ease; }
.node-toggle.open { transform: rotate(180deg); color: var(--primary); }
.node-panel {
  border-top: 1px solid var(--border); padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 8px;
  cursor: default; animation: fadeOnly .2s ease backwards;
}
.panel-label { font-size: 12px; color: var(--muted); font-weight: 500; }
.panel-row { display: flex; gap: 8px; align-items: center; }
.panel-row select { flex: 1; width: auto; padding: 5px 8px; font-size: 12.5px; }
.panel-input, .node-log {
  width: 100%; box-sizing: border-box; font-family: var(--mono); font-size: 12px; line-height: 1.45;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); color: var(--text);
  -webkit-user-select: text; user-select: text; cursor: text;
}
.panel-input { resize: vertical; min-height: 44px; padding: 6px 8px; touch-action: auto; }
.panel-input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.node-log { margin: 0; height: 150px; overflow: auto; padding: 6px 8px; white-space: pre-wrap; word-break: break-all; touch-action: pan-y; }
.node-log:empty::before { content: "Noch keine Daten empfangen …"; color: var(--muted); }
.node.expanded { z-index: 3; }

/* ---------- legend behind a button (touch) */
.toolbar { position: relative; }
.legend-pop {
  position: absolute; z-index: 30; left: 12px; right: 12px; margin-top: 4px;
  display: flex; flex-wrap: wrap; gap: 8px 16px; padding: 10px 12px; font-size: 12.5px; color: var(--muted);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
}
.legend-pop i { display: inline-block; width: 22px; height: 0; border-top: 2.5px solid; vertical-align: middle; margin-right: 5px; }
.board { position: relative; }

/* ---------- touch devices: whole app fits the screen, only the board pans/zooms */
#app:not([hidden]) { height: 100vh; height: 100dvh; }
html.touch, html.touch body { overscroll-behavior: none; touch-action: manipulation; }
.canvas, .tray, .node, .tray-item, .toolbar, .handle, .topbar {
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
.tray-item.draggable { touch-action: pan-x; }
.canvas.drop-target { box-shadow: inset 0 0 0 2px var(--primary); }
html.touch .handle { width: 18px; height: 18px; margin-top: -9px; }
html.touch .handle-out { right: -10px; }
html.touch .handle-in { left: -10px; }
html.touch .handle::before { content: ""; position: absolute; inset: -16px; border-radius: 50%; }
html.touch .edge-hit { stroke-width: 40; }
html.touch .tray-head .hint { display: none; }
