/* Highlight marks */
mark[data-highlight] {
  border-radius: 3px;
  padding: 0.05em 0;
  cursor: pointer;
  transition: filter var(--transition);
  color: inherit; /* prevent browser default mark color (black) in dark mode */
}

mark[data-highlight]:hover {
  filter: brightness(0.92);
}

mark[data-highlight].hl-focus {
  animation: hlFocusPulse 2s var(--ease-out-quint) forwards;
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@keyframes hlFocusPulse {
  0%   { outline-color: var(--accent); box-shadow: 0 0 0 4px rgba(155,125,245,0.25); }
  60%  { outline-color: var(--accent); box-shadow: 0 0 0 6px rgba(155,125,245,0.1); }
  100% { outline-color: transparent;   box-shadow: none; }
}

mark[data-color="yellow"] { background: var(--hl-yellow); }
mark[data-color="green"]  { background: var(--hl-green); }
mark[data-color="blue"]   { background: var(--hl-blue); }
mark[data-color="pink"]   { background: var(--hl-pink); }
mark[data-color="orange"] { background: var(--hl-orange); }

mark[data-highlight].has-annotation::after {
  content: '💬';
  font-size: 0.65em;
  vertical-align: super;
  margin-left: 2px;
}

/* Definition highlights */
mark[data-color="definition"] {
  background: linear-gradient(120deg, rgba(155, 125, 245, 0.12) 0%, rgba(155, 125, 245, 0.18) 100%);
  border-bottom: 2px dotted rgba(155, 125, 245, 0.5);
  cursor: pointer;
}

[data-theme="dark"] mark[data-color="definition"] {
  background: linear-gradient(120deg, rgba(185, 167, 245, 0.15) 0%, rgba(185, 167, 245, 0.22) 100%);
  border-bottom-color: rgba(185, 167, 245, 0.5);
}

mark[data-highlight].has-definition::after {
  content: '📖';
  font-size: 0.55em;
  vertical-align: super;
  margin-left: 2px;
}

/* Floating toolbar */
.highlight-toolbar {
  position: absolute;
  display: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 6px 8px;
  box-shadow: var(--shadow-md);
  z-index: 100;
  gap: 4px;
  align-items: center;
}

.highlight-toolbar.visible {
  display: flex;
}

.highlight-toolbar .color-btn {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform var(--duration-feedback) var(--ease-out-quint),
              border-color var(--transition-fast),
              box-shadow var(--transition-fast);
}

.highlight-toolbar .color-btn:hover {
  transform: scale(1.2);
  border-color: var(--text-secondary);
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.highlight-toolbar .color-btn:active {
  transform: scale(0.9);
}

.color-yellow { background: var(--hl-yellow-strong); }
.color-green  { background: var(--hl-green-strong); }
.color-blue   { background: var(--hl-blue-strong); }
.color-pink   { background: var(--hl-pink-strong); }
.color-orange { background: var(--hl-orange-strong); }

/* Highlight popover (for editing existing highlights) */
.highlight-popover {
  position: absolute;
  display: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  box-shadow: var(--shadow-lg);
  z-index: 101;
  width: 260px;
}

.highlight-popover.visible {
  display: block;
}

.highlight-popover .color-row {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}

.highlight-popover .annotation-input {
  width: 100%;
  box-sizing: border-box;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 6px 8px;
  font-family: var(--font-ui);
  font-size: 0.875rem;
  background: var(--bg-surface);
  color: var(--text);
  resize: vertical;
  min-height: 64px;
  outline: none;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.highlight-popover .annotation-input:focus {
  border-color: var(--accent);
  box-shadow: var(--focus-ring);
}

.highlight-popover .popover-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
}

.highlight-popover .btn-save {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius-md);
  padding: 6px 14px;
  font-family: var(--font-ui);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--transition-fast), transform var(--duration-feedback) var(--ease-out-quint);
}

.highlight-popover .btn-save:hover {
  background: var(--accent-hover);
}

.highlight-popover .btn-save:active {
  transform: scale(0.96);
}

.highlight-popover .btn-delete {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 0.8rem;
  cursor: pointer;
  font-family: var(--font-ui);
}

.highlight-popover .btn-delete:hover {
  color: var(--danger);
}

.highlight-popover .btn-copy {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 6px 12px;
  font-family: var(--font-ui);
  font-size: 0.8rem;
  cursor: pointer;
  color: var(--text-secondary);
  margin-right: 6px;
  transition: border-color var(--transition-fast), color var(--transition-fast);
}

.highlight-popover .btn-copy:hover {
  border-color: var(--text-secondary);
  color: var(--text);
}

.highlight-popover .popover-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
}

.highlight-popover .popover-actions > div {
  display: flex;
  gap: 6px;
  align-items: center;
}

/* Toast notification */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  background: var(--text);
  color: var(--bg);
  font-family: var(--font-ui);
  font-size: 0.85rem;
  font-weight: 500;
  padding: 0.55em 1.25em;
  border-radius: var(--radius-lg);
  opacity: 0;
  transition: opacity var(--duration-state) var(--ease-out-quart),
              transform var(--duration-state) var(--ease-out-quint);
  z-index: 200;
  pointer-events: none;
  /* Subtle border for depth */
  box-shadow: var(--shadow-lg);
}

#toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Copy button on dashboard cards */
.hl-card-actions {
  display: flex;
  gap: 8px;
  margin-top: 0.4em;
}

.hl-copy-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 8px;
  font-family: var(--font-ui);
  font-size: 0.7rem;
  color: var(--text-secondary);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.hl-copy-btn:hover, .hl-copy-btn:active {
  border-color: var(--text-secondary);
  color: var(--text);
}

/* Highlights dashboard */
.hl-dashboard-group {
  margin-bottom: 2em;
}

.hl-dashboard-group .group-header {
  display: flex;
  align-items: center;
  gap: 0.75em;
  margin-bottom: 0.75em;
  cursor: pointer;
}

.hl-dashboard-group .group-title {
  font-family: var(--font-heading);
  font-weight: 600;
  font-size: 1.05rem;
  color: var(--text);
  letter-spacing: -0.01em;
}

.hl-dashboard-group .group-count {
  font-family: var(--font-ui);
  font-size: 0.75rem;
  color: var(--text-secondary);
  background: var(--bg-surface);
  padding: 0.15em 0.5em;
  border-radius: 999px;
}

.hl-card {
  padding: 0.75em 1em;
  border-left: 4px solid var(--hl-yellow-strong);
  background: var(--bg-surface);
  border-radius: 0 8px 8px 0;
  margin-bottom: 0.5em;
  cursor: pointer;
  transition: all var(--transition);
}

.hl-card:hover {
  box-shadow: var(--shadow-sm);
}

.hl-card[data-color="yellow"] { border-left-color: var(--hl-yellow-strong); }
.hl-card[data-color="green"]  { border-left-color: var(--hl-green-strong); }
.hl-card[data-color="blue"]   { border-left-color: var(--hl-blue-strong); }
.hl-card[data-color="pink"]   { border-left-color: var(--hl-pink-strong); }
.hl-card[data-color="orange"] { border-left-color: var(--hl-orange-strong); }

.hl-card .hl-text {
  font-family: var(--font-body);
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--text);
  margin-bottom: 0.3em;
}

.hl-card .hl-annotation {
  font-family: var(--font-ui);
  font-size: 0.8rem;
  color: var(--text-secondary);
  font-style: italic;
}

.hl-card .hl-meta {
  font-family: var(--font-ui);
  font-size: 0.7rem;
  color: var(--text-secondary);
  margin-top: 0.25em;
}

/* ===== Define Button in Toolbar ===== */
.define-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: var(--grad-hero);
  cursor: pointer;
  font-size: 0.8rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 6px rgba(155, 125, 245, 0.35);
  transition: transform var(--duration-feedback) var(--ease-out-quint),
              box-shadow var(--transition-fast);
  -webkit-tap-highlight-color: transparent;
}

.define-btn:hover {
  transform: scale(1.2);
  box-shadow: 0 3px 12px rgba(155, 125, 245, 0.45);
}

.define-btn:active {
  transform: scale(0.9);
}

/* ===== Definition Popover ===== */
.definition-popover {
  position: absolute;
  display: none;
  width: 300px;
  max-height: 400px;
  overflow-y: auto;
  background: var(--bg-card-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0;
  box-shadow: var(--shadow-lg);
  z-index: 160;
  font-family: var(--font-ui);
  animation: defPopIn 0.25s var(--ease-out-quint) both;
}

.definition-popover.visible {
  display: block;
}

@keyframes defPopIn {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.def-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75em 0.85em 0.5em;
  border-bottom: 1px solid var(--border-light);
}

.def-title-row {
  display: flex;
  align-items: baseline;
  gap: 0.5em;
  min-width: 0;
}

.def-word {
  font-family: var(--font-heading);
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.01em;
}

.def-phonetic {
  font-size: 0.78rem;
  color: var(--text-secondary);
  font-style: italic;
}

.def-actions {
  display: flex;
  align-items: center;
  gap: 0.25em;
  flex-shrink: 0;
}

.def-close,
.def-delete {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.75rem;
  padding: 0.25em;
  line-height: 1;
  border-radius: var(--radius-sm);
  transition: color var(--transition-fast);
  color: var(--text-secondary);
}

.def-close:hover {
  color: var(--text);
}

.def-delete:hover {
  color: var(--danger);
}

.def-body {
  padding: 0.65em 0.85em 0.75em;
}

.def-pos {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  margin-bottom: 0.3em;
  margin-top: 0.5em;
}

.def-pos:first-child {
  margin-top: 0;
}

.def-list {
  margin: 0 0 0.25em;
  padding-left: 1.25em;
  list-style: decimal;
}

.def-item {
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--text);
  margin-bottom: 0.35em;
}

.def-example {
  font-size: 0.75rem;
  color: var(--text-secondary);
  font-style: italic;
  margin-top: 0.15em;
}

.def-error {
  padding: 0.75em 0.85em;
  font-size: 0.82rem;
  color: var(--text-secondary);
  text-align: center;
}

/* Loading dots */
.def-loading {
  display: flex;
  justify-content: center;
  gap: 0.4em;
  padding: 1em 0.85em;
}

.def-loading-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: defDotPulse 1.2s ease-in-out infinite;
}

.def-loading-dot:nth-child(2) { animation-delay: 0.15s; }
.def-loading-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes defDotPulse {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1.1); }
}

/* Mobile touch-friendly highlights */
@media (max-width: 768px) {
  .highlight-toolbar {
    padding: 8px 10px;
    gap: 8px;
    border-radius: 10px;
  }

  .highlight-toolbar .color-btn {
    width: 32px;
    height: 32px;
    -webkit-tap-highlight-color: transparent;
  }

  .define-btn {
    width: 36px;
    height: 36px;
    font-size: 1rem;
  }

  .definition-popover {
    width: calc(100vw - 24px);
    max-width: 340px;
    left: 12px !important;
  }

  .highlight-popover {
    width: calc(100vw - 32px);
    max-width: 300px;
    left: 16px !important;
  }

  .highlight-popover .annotation-input {
    font-size: 16px; /* Prevents iOS zoom */
  }

  .highlight-popover .btn-save,
  .highlight-popover .btn-delete {
    padding: 8px 16px;
    font-size: 0.85rem;
  }

  .highlight-popover .color-row {
    gap: 8px;
  }

  .highlight-popover .color-btn {
    width: 28px;
    height: 28px;
  }
}
