/*
 * Classic Byte — Interactive SVG Word Cloud Styles
 *
 * Follows the "warm dusty library" & "Midnight Library" aesthetic.
 * Relies on CSS variables from theme.css and dark.css:
 *   --bg, --paper, --ink, --ink-light, --terracotta, --old-gold, --dust, --dust-gray
 */

/* --- Container ------------------------------------------------------------- */
.wordcloud-container,
.wordcloud {
  position: relative;
  width: 100%;
  height: 420px;
  overflow: hidden;
  border-radius: 8px;
  box-sizing: border-box;
}

@media (max-width: 640px) {
  .wordcloud-container,
  .wordcloud {
    height: auto;
    aspect-ratio: 16 / 10;
    min-height: 250px;
  }
}

.wordcloud-svg-wrap {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* --- SVG Element ----------------------------------------------------------- */
.wordcloud-svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
  transition: opacity 0.22s ease, transform 0.22s ease;
}

.wordcloud-svg.shuffling {
  opacity: 0.15;
  transform: scale(0.985);
}

/* --- Word Items ------------------------------------------------------------ */
.word-item {
  cursor: pointer;
  font-family: var(--serif, "Cormorant Garamond", Georgia, serif);
  text-anchor: middle;
  dominant-baseline: central;
  font-weight: 500;
  user-select: none;
  transform-box: fill-box;
  transform-origin: center center;
  transition: transform 0.2s cubic-bezier(0.2, 0, 0.2, 1),
              opacity 0.2s ease,
              filter 0.2s ease,
              fill 0.25s ease;
  outline: none;
}

.word-item:hover {
  opacity: 1 !important;
  transform: scale(1.18);
  font-weight: 700;
  filter: drop-shadow(0 2px 6px color-mix(in srgb, var(--ink) 30%, transparent));
}

.word-item:focus-visible {
  opacity: 1 !important;
  transform: scale(1.18);
  font-weight: 700;
  stroke: var(--old-gold);
  stroke-width: 1.5px;
  stroke-linejoin: round;
  paint-order: stroke fill;
  filter: drop-shadow(0 0 8px color-mix(in srgb, var(--old-gold) 70%, transparent));
}

/* Dim sibling words on hover for visual focus */
.wordcloud:hover .word-item:not(:hover),
.wordcloud-container:hover .word-item:not(:hover) {
  opacity: 0.65;
}

/* --- Word Palette Colors (Light / Parchment) ------------------------------- */
.word-item.wc-c-0 { fill: #9a3b2a; } /* Terracotta */
.word-item.wc-c-1 { fill: #b89340; } /* Old Gold */
.word-item.wc-c-2 { fill: #3b2a20; } /* Ink Brown */
.word-item.wc-c-3 { fill: #5a4232; } /* Medium Ink */
.word-item.wc-c-4 { fill: #a3623e; } /* Warm Rust */
.word-item.wc-c-5 { fill: #7a5a3f; } /* Walnut */
.word-item.wc-c-6 { fill: #c49a54; } /* Amber Gold */
.word-item.wc-c-7 { fill: #2e2520; } /* Deep Ink */
.word-item.wc-c-8 { fill: #8c5f32; } /* Sienna */
.word-item.wc-c-9 { fill: #543928; } /* Chestnut */

/* --- Word Palette Colors (Dark Mode / High Contrast) ----------------------- */
:root[data-theme="dark"] .word-item.wc-c-0,
:root:not([data-theme="light"]) .word-item.wc-c-0 { fill: #f07153; }
:root[data-theme="dark"] .word-item.wc-c-1,
:root:not([data-theme="light"]) .word-item.wc-c-1 { fill: #e5b758; }
:root[data-theme="dark"] .word-item.wc-c-2,
:root:not([data-theme="light"]) .word-item.wc-c-2 { fill: #f0e5d3; }
:root[data-theme="dark"] .word-item.wc-c-3,
:root:not([data-theme="light"]) .word-item.wc-c-3 { fill: #d9c7af; }
:root[data-theme="dark"] .word-item.wc-c-4,
:root:not([data-theme="light"]) .word-item.wc-c-4 { fill: #fca779; }
:root[data-theme="dark"] .word-item.wc-c-5,
:root:not([data-theme="light"]) .word-item.wc-c-5 { fill: #d8c29d; }
:root[data-theme="dark"] .word-item.wc-c-6,
:root:not([data-theme="light"]) .word-item.wc-c-6 { fill: #f7c96e; }
:root[data-theme="dark"] .word-item.wc-c-7,
:root:not([data-theme="light"]) .word-item.wc-c-7 { fill: #ffffff; }
:root[data-theme="dark"] .word-item.wc-c-8,
:root:not([data-theme="light"]) .word-item.wc-c-8 { fill: #e6c898; }
:root[data-theme="dark"] .word-item.wc-c-9,
:root:not([data-theme="light"]) .word-item.wc-c-9 { fill: #e89e74; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .word-item.wc-c-0 { fill: #f07153; }
  :root:not([data-theme="light"]) .word-item.wc-c-1 { fill: #e5b758; }
  :root:not([data-theme="light"]) .word-item.wc-c-2 { fill: #f0e5d3; }
  :root:not([data-theme="light"]) .word-item.wc-c-3 { fill: #d9c7af; }
  :root:not([data-theme="light"]) .word-item.wc-c-4 { fill: #fca779; }
  :root:not([data-theme="light"]) .word-item.wc-c-5 { fill: #d8c29d; }
  :root:not([data-theme="light"]) .word-item.wc-c-6 { fill: #f7c96e; }
  :root:not([data-theme="light"]) .word-item.wc-c-7 { fill: #ffffff; }
  :root:not([data-theme="light"]) .word-item.wc-c-8 { fill: #e6c898; }
  :root:not([data-theme="light"]) .word-item.wc-c-9 { fill: #e89e74; }
}

/* --- Styled Tooltip -------------------------------------------------------- */
.wordcloud-tooltip {
  position: absolute;
  z-index: 40;
  width: max-content;
  max-width: 290px;
  padding: 0.65rem 0.9rem;
  background: var(--paper);
  border: 1px solid var(--dust-gray);
  border-radius: 8px;
  box-shadow: 0 8px 24px color-mix(in srgb, var(--ink) 18%, transparent);
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.18s ease, transform 0.18s ease, visibility 0.18s;
  transform: translate(-50%, -100%) translateY(4px);
  box-sizing: border-box;
}

.wordcloud-tooltip[data-placement="bottom"] {
  transform: translate(-50%, 0) translateY(-4px);
}

.wordcloud-tooltip.visible {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, -100%) translateY(0);
}

.wordcloud-tooltip[data-placement="bottom"].visible {
  transform: translate(-50%, 0) translateY(0);
}

.wc-tooltip-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.35rem;
  border-bottom: 1px solid var(--dust);
  padding-bottom: 0.3rem;
}

.wc-tooltip-word {
  font-family: var(--serif, "Cormorant Garamond", Georgia, serif);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--terracotta);
  line-height: 1.2;
}

.wc-tooltip-count {
  font-family: var(--sans, system-ui, sans-serif);
  font-size: 0.72rem;
  color: var(--ink-light);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.wc-tooltip-excerpt {
  font-family: var(--serif, "Cormorant Garamond", Georgia, serif);
  font-style: italic;
  font-size: 0.88rem;
  line-height: 1.35;
  color: var(--ink);
  margin: 0 0 0.4rem;
}

.wc-tooltip-excerpt mark.wc-match {
  background: color-mix(in srgb, var(--old-gold) 35%, transparent);
  color: inherit;
  font-weight: 600;
  padding: 0 2px;
  border-radius: 2px;
}

.wc-tooltip-hint {
  display: block;
  font-family: var(--sans, system-ui, sans-serif);
  font-size: 0.7rem;
  color: var(--dust-gray);
  text-align: right;
  letter-spacing: 0.02em;
}

/* Tooltip dark mode overrides */
:root[data-theme="dark"] .wordcloud-tooltip,
:root:not([data-theme="light"]) .wordcloud-tooltip {
  background: var(--paper);
  border-color: var(--dust);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.65);
}

:root[data-theme="dark"] .wc-tooltip-word,
:root:not([data-theme="light"]) .wc-tooltip-word {
  color: var(--old-gold);
}

:root[data-theme="dark"] .wc-tooltip-hint,
:root:not([data-theme="light"]) .wc-tooltip-hint {
  color: var(--ink-light);
}

/* --- Ambient "↻ Shuffle" Button -------------------------------------------- */
.wordcloud-shuffle-btn {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  z-index: 20;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.32rem 0.8rem;
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  border: 1px solid var(--dust-gray);
  border-radius: 999px;
  color: var(--ink-light);
  font-family: var(--sans, system-ui, sans-serif);
  font-size: 0.78rem;
  font-weight: 500;
  letter-spacing: 0.03em;
  cursor: pointer;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  transition: all 0.2s ease;
  user-select: none;
  box-shadow: 0 1px 4px color-mix(in srgb, var(--ink) 6%, transparent);
}

.wordcloud-shuffle-btn:hover {
  background: var(--paper);
  border-color: var(--old-gold);
  color: var(--ink);
  transform: translateY(-1px);
  box-shadow: 0 3px 8px color-mix(in srgb, var(--ink) 12%, transparent);
}

.wordcloud-shuffle-btn:active {
  transform: translateY(0);
}

.wordcloud-shuffle-btn:focus-visible {
  outline: 2px solid var(--old-gold);
  outline-offset: 2px;
}

.wordcloud-shuffle-btn .shuffle-icon {
  display: inline-block;
  font-size: 0.95rem;
  line-height: 1;
  transition: transform 0.5s cubic-bezier(0.2, 0, 0.2, 1);
}

.wordcloud-shuffle-btn.spinning .shuffle-icon {
  transform: rotate(360deg);
}

:root[data-theme="dark"] .wordcloud-shuffle-btn,
:root:not([data-theme="light"]) .wordcloud-shuffle-btn {
  background: color-mix(in srgb, var(--paper) 80%, black);
  border-color: var(--dust);
  color: var(--ink-light);
}

:root[data-theme="dark"] .wordcloud-shuffle-btn:hover,
:root:not([data-theme="light"]) .wordcloud-shuffle-btn:hover {
  border-color: var(--old-gold);
  color: var(--ink);
}

/* --- Highlight Line Pulse (Jump from Wordcloud) ---------------------------- */
.full-text-line.word-jump-highlight,
p.word-jump-highlight {
  animation: wordJumpPulse 2.4s ease-out forwards;
}

@keyframes wordJumpPulse {
  0% {
    background-color: color-mix(in srgb, var(--old-gold) 55%, transparent);
    box-shadow: 0 0 0 2px var(--old-gold), 0 0 16px color-mix(in srgb, var(--old-gold) 50%, transparent);
    transform: translateX(4px);
  }
  30% {
    background-color: color-mix(in srgb, var(--old-gold) 40%, transparent);
    box-shadow: 0 0 0 2px var(--old-gold), 0 0 12px color-mix(in srgb, var(--old-gold) 35%, transparent);
    transform: translateX(4px);
  }
  100% {
    background-color: transparent;
    box-shadow: none;
    transform: translateX(0);
  }
}
