/* =========
   Global Styles
   ========= */
:root {
  color-scheme: dark;
  --bg: #0f172a;
  --bg-elevated: rgba(15, 23, 42, 0.75);
  --bg-panel: rgba(30, 41, 59, 0.72);
  --bg-panel-hover: rgba(30, 41, 59, 0.85);
  --border: rgba(148, 163, 184, 0.12);
  --accent: #38bdf8;
  --accent-soft: rgba(56, 189, 248, 0.15);
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --danger: #f87171;
  --success: #4ade80;
  --font-body: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
  font-family: var(--font-body);
  font-size: 15px;
}

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

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at top right, rgba(56, 189, 248, 0.1), transparent 45%), radial-gradient(circle at bottom left, rgba(74, 222, 128, 0.12), transparent 40%), var(--bg);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  font-family: var(--font-body);
  line-height: 1.6;
  letter-spacing: 0.01em;
}

a {
  color: inherit;
  text-decoration: none;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

/* =========
   App Shell
   ========= */
.app-header {
  backdrop-filter: blur(14px);
  background: rgba(15, 23, 42, 0.7);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.app-header__inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0.85rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.2rem;
}

.brand-title {
  font-weight: 600;
  letter-spacing: 0.015em;
  display: flex;
  align-items: center;
  gap: 0.7rem;
  font-size: 1rem;
}

.brand-title svg {
  width: 28px;
  height: 28px;
}

.role-badge {
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.85rem;
  font-weight: 500;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.toolbar button,
.toolbar select {
  min-width: 120px;
}

.hidden {
  display: none !important;
}

.app-main {
  flex: 1;
  width: 100%;
  max-width: 1120px;
  margin: 2.5rem auto;
  padding: 0 1.5rem 3rem;
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 2rem;
}


.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 1.45rem;
  backdrop-filter: blur(20px);
  box-shadow: 0 18px 40px rgba(15, 23, 42, 0.35);
  display: flex;
  flex-direction: column;
  gap: 1.35rem;
  transition: transform 0.18s ease, background 0.18s ease, border-color 0.18s ease;
}

.panel:hover {
  transform: translateY(-2px);
  background: var(--bg-panel-hover);
  border-color: rgba(148, 163, 184, 0.2);
}

.panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.panel__title {
  font-size: 1.2rem;
  font-weight: 600;
  margin: 0;
  letter-spacing: 0.01em;
}

.panel__subtitle {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.9rem;
  line-height: 1.5;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.9rem;
}

.stat-card {
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: 14px;
  padding: 0.9rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  transition: transform 0.2s ease, border-color 0.2s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  border-color: rgba(56, 189, 248, 0.4);
}

.stat-label {
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

.stat-value {
  font-size: 1.35rem;
  font-weight: 600;
}

.section-divider {
  margin: 0;
  border: none;
  height: 1px;
  background: var(--border);
}

/* =========
   Inputs & Buttons
   ========= */
.button {
  border: 1px solid rgba(148, 163, 184, 0.22);
  background: rgba(148, 163, 184, 0.1);
  color: var(--text-primary);
  padding: 0.5rem 0.85rem;
  border-radius: 12px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease, transform 0.2s ease;
}

.button:hover {
  background: rgba(148, 163, 184, 0.18);
  border-color: rgba(148, 163, 184, 0.35);
  transform: translateY(-1px);
}

.button--accent {
  background: linear-gradient(135deg, #38bdf8, #6366f1);
  border-color: transparent;
  color: #0b1120;
  font-weight: 600;
}

.button--accent:hover {
  background: linear-gradient(135deg, #0ea5e9, #4f46e5);
}

.button--danger {
  color: var(--danger);
  border-color: rgba(248, 113, 113, 0.35);
  background: rgba(248, 113, 113, 0.08);
}

.button--ghost {
  background: rgba(15, 23, 42, 0.55);
  border-color: rgba(148, 163, 184, 0.22);
}

.button--ghost:hover {
  border-color: rgba(56, 189, 248, 0.5);
  background: rgba(56, 189, 248, 0.12);
}

.button--danger:hover {
  background: rgba(248, 113, 113, 0.15);
  border-color: rgba(248, 113, 113, 0.45);
}

.button:disabled,
.button[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.input,
.select {
  width: 100%;
  padding: 0.65rem 0.9rem;
  border-radius: 12px;
  border: 1px solid rgba(148, 163, 184, 0.18);
  background: rgba(15, 23, 42, 0.65);
  color: var(--text-primary);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.input:focus,
.select:focus {
  outline: none;
  border-color: rgba(56, 189, 248, 0.7);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.15);
}

.muted {
  color: var(--text-secondary);
  font-size: 0.88rem;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.field-group--row {
  flex-direction: row;
  align-items: flex-end;
}

.field-group--row .input {
  flex: 1;
}

.tags-footer {
  margin-top: 1rem;
  display: none;
  justify-content: flex-end;
}

.icon-button {
  all: unset;
  cursor: pointer;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid rgba(148, 163, 184, 0.18);
  background: rgba(15, 23, 42, 0.6);
  display: grid;
  place-items: center;
  transition: border-color 0.2s ease, background 0.2s ease, transform 0.2s ease;
  color: var(--text-secondary);
}

.icon-button:hover {
  border-color: rgba(56, 189, 248, 0.5);
  background: rgba(56, 189, 248, 0.15);
  transform: translateY(-1px);
}

.icon-button.is-active {
  border-color: rgba(56, 189, 248, 0.6);
  background: rgba(56, 189, 248, 0.18);
  color: var(--accent);
}

.icon-button svg {
  width: 18px;
  height: 18px;
  stroke: var(--text-secondary);
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button.is-active svg {
  stroke: var(--accent);
}

.icon-button:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.25);
}

.icon-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  border-color: rgba(148, 163, 184, 0.18);
  background: rgba(15, 23, 42, 0.5);
  color: var(--text-secondary);
}

/* =========
   Tag Sphere
   ========= */
#tagsArea {
  position: relative;
  min-height: min(440px, 55vh);
}

.tags-stage {
  position: relative;
  height: min(440px, 55vh);
  display: flex;
  align-items: center;
  justify-content: center;
  perspective: 1200px;
  perspective-origin: center;
  touch-action: none;
  --rotateX: -14deg;
  --rotateY: 18deg;
}

.sphere-wrapper {
  position: relative;
  width: clamp(280px, 38vw, 360px);
  height: clamp(280px, 38vw, 360px);
  transform-style: preserve-3d;
}

.sphere {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transform: rotateX(var(--rotateX)) rotateY(var(--rotateY));
  animation: sphere-spin 12s linear infinite;
  transition: transform 0.25s ease;
}

.sphere--hover {
  animation-play-state: paused;
}

.sphere__halo {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(56, 189, 248, 0.28), rgba(15, 23, 42, 0.05));
  filter: blur(0);
  opacity: 0.65;
  transform: translateZ(-40px);
  pointer-events: none;
}

.sphere__core {
  position: absolute;
  inset: 18%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(99, 102, 241, 0.35), rgba(15, 23, 42, 0.05));
  filter: blur(0);
  opacity: 0.55;
  transform: translateZ(-60px);
  pointer-events: none;
}

.tag-node {
  --depth: 0.5;
  --translateX: 0px;
  --translateY: 0px;
  --translateZ: 0px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform-style: preserve-3d;
  padding: 0.55rem 0.9rem;
  min-width: 96px;
  border-radius: 999px;
  border: 1px solid rgba(148, 163, 184, 0.18);
  background: rgba(15, 23, 42, calc(0.7 + var(--depth) * 0.25));
  box-shadow: 0 12px 24px rgba(15, 23, 42, 0.35);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 0.45rem;
  filter: brightness(calc(0.75 + var(--depth) * 0.35));
  backdrop-filter: blur(6px);
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease, filter 0.2s ease;
  cursor: grab;
  transform: translate3d(-50%, -50%, 0) translate3d(var(--translateX), var(--translateY), var(--translateZ));
}

.tag-node:hover {
  border-color: rgba(56, 189, 248, 0.5);
  box-shadow: 0 16px 38px rgba(15, 23, 42, 0.45);
  transform: translate3d(-50%, -50%, 0)
    translate3d(var(--translateX), var(--translateY), var(--translateZ))
    scale(1.05);
}

.tag-node:active {
  cursor: grabbing;
}

.tag-node__label {
  font-size: 0.9rem;
  white-space: nowrap;
}

.grid-item__action {
  all: unset;
  cursor: pointer;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.22);
  background: rgba(15, 23, 42, 0.8);
  transition: border-color 0.2s ease, background 0.2s ease, transform 0.2s ease, opacity 0.2s ease;
}

.grid-item__action svg {
  width: 14px;
  height: 14px;
  stroke: var(--text-secondary);
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: stroke 0.2s ease;
}

.grid-item__action:hover {
  border-color: rgba(56, 189, 248, 0.5);
  background: rgba(56, 189, 248, 0.12);
  transform: translateY(-1px);
}

.grid-item__action:hover svg {
  stroke: var(--accent);
}

.grid-item__action:disabled {
  cursor: not-allowed;
  opacity: 0.45;
  transform: none;
}

.grid-item__action:disabled svg {
  stroke: var(--text-secondary);
}

.grid-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.7rem;
  margin-top: 1rem;
}

.grid-item {
  border-radius: 14px;
  border: 1px solid rgba(148, 163, 184, 0.18);
  background: rgba(15, 23, 42, 0.68);
  padding: 0.72rem 1rem;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  column-gap: 0.45rem;
  cursor: pointer;
  transition: border-color 0.2s ease, transform 0.2s ease, background 0.2s ease;
}

.grid-item:hover {
  border-color: rgba(56, 189, 248, 0.5);
  transform: translateY(-2px);
  background: rgba(15, 23, 42, 0.85);
}

.grid-item__name {
  font-size: 0.92rem;
  letter-spacing: 0.01em;
  white-space: nowrap;
  min-width: 0;
}

.grid-item__actions {
  display: inline-flex;
  gap: 0.35rem;
  justify-content: flex-end;
  opacity: 0;
  transform: translateX(-6px);
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.grid-item:hover .grid-item__actions,
.grid-item:focus-within .grid-item__actions,
.grid-item:active .grid-item__actions {
  opacity: 1;
  transform: translateX(0);
  pointer-events: auto;
}

.empty-state {
  border: 1px dashed rgba(148, 163, 184, 0.25);
  border-radius: 14px;
  padding: 1.4rem;
  text-align: center;
  color: var(--text-secondary);
  background: rgba(15, 23, 42, 0.4);
}

@keyframes sphere-spin {
  0% {
    transform: rotateX(calc(var(--rotateX) - 6deg)) rotateY(calc(var(--rotateY) - 8deg));
  }
  50% {
    transform: rotateX(calc(var(--rotateX) + 10deg)) rotateY(calc(var(--rotateY) + 16deg));
  }
  100% {
    transform: rotateX(calc(var(--rotateX) - 6deg)) rotateY(calc(var(--rotateY) - 8deg));
  }
}

@media (prefers-reduced-motion: reduce) {
  .sphere {
    animation: none;
  }
}

/* =========
   Dialogs
   ========= */
dialog {
  border: none;
  border-radius: 18px;
  background: rgba(15, 23, 42, 0.95);
  color: var(--text-primary);
  min-width: 360px;
  padding: 1.8rem;
  box-shadow: 0 25px 60px rgba(15, 23, 42, 0.45);
}

dialog::backdrop {
  background: rgba(2, 6, 23, 0.55);
  backdrop-filter: blur(6px);
}

dialog header {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 1.2rem;
}

dialog header + p {
  margin-top: -0.6rem;
  margin-bottom: 0.8rem;
  font-size: 0.95rem;
  color: var(--text-secondary);
}

dialog form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-error {
  color: var(--danger);
  font-size: 0.88rem;
  margin-top: -0.4rem;
  min-height: 1.2rem;
}

dialog footer {
  margin-top: 1.4rem;
  display: flex;
  justify-content: flex-end;
  gap: 0.8rem;
}

/* =========
   Toasts
   ========= */

.toast-host {
  position: fixed;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  z-index: 90;
  bottom: 1.4rem;
  right: 1.4rem;
}

.toast {
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid rgba(148, 163, 184, 0.22);
  border-left: 3px solid var(--accent);
  border-radius: 14px;
  padding: 0.75rem 1rem;
  min-width: 220px;
  box-shadow: 0 18px 36px rgba(15, 23, 42, 0.4);
  animation: toast-in 0.3s ease;
  z-index: 60;
}

.toast--error {
  border-left-color: var(--danger);
}

.toast__message {
  margin: 0;
  font-size: 0.95rem;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* =========
   Responsive
   ========= */
@media (max-width: 960px) {
  .app-main {
    grid-template-columns: 1fr;
  }

  .panel {
    padding: 1.3rem;
  }
}

@media (max-width: 640px) {
  .app-header__inner {
    flex-direction: column;
    align-items: flex-start;
  }

  .toolbar {
    width: 100%;
  }

  .toolbar button,
  .toolbar select {
    flex: 1;
    min-width: auto;
  }

  dialog {
    width: 90vw;
    min-width: 0;
  }
}
