/**
 * Component Library
 * Reusable UI components following the design system
 */

/* ========== Base Button Styles ========== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  line-height: var(--line-height-tight);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-base);
  white-space: nowrap;
  user-select: none;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn:focus-visible {
  outline: none;
  box-shadow: var(--shadow-focus);
}

/* Primary button */
.btn-primary {
  background: var(--color-primary);
  color: var(--color-text-inverse);
}

.btn-primary:hover:not(:disabled) {
  background: var(--color-primary-hover);
}

.btn-primary:active:not(:disabled) {
  background: var(--color-primary-active);
}

/* Secondary button */
.btn-secondary {
  background: var(--color-bg-panel);
  color: var(--color-text-primary);
  border-color: var(--color-border-strong);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--color-bg-hover);
}

/* Ghost button */
.btn-ghost {
  background: transparent;
  color: var(--color-text-secondary);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--color-bg-hover);
  color: var(--color-text-primary);
}

/* Upgrade button */
.btn-upgrade {
  background: linear-gradient(135deg, var(--color-premium) 0%, #F97316 100%);
  color: var(--color-text-inverse);
  font-weight: var(--font-weight-semibold);
}

.btn-upgrade:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

/* Icon button (square) */
.btn-icon {
  padding: var(--space-sm);
  width: 36px;
  height: 36px;
}

/* Small button */
.btn-sm {
  padding: var(--space-xs) var(--space-sm);
  font-size: var(--font-size-xs);
}

/* ========== Tool Button (Left Toolbar) ========== */
.tool-btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  padding: 0;
  background: transparent;
  border: 2px solid transparent;
  border-radius: var(--radius-lg);
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: all var(--transition-base);
}

.tool-btn:hover {
  background: var(--color-bg-hover);
  color: var(--color-text-primary);
}

.tool-btn.active {
  background: var(--color-primary-lighter);
  color: var(--color-primary);
  border-color: var(--color-primary);
}

.tool-btn:focus-visible {
  outline: none;
  box-shadow: var(--shadow-focus);
}

.tool-btn svg {
  width: 24px;
  height: 24px;
}

/* Premium badge on tool */
.tool-btn .badge {
  position: absolute;
  top: -4px;
  right: -4px;
  padding: 2px 4px;
  background: var(--color-premium);
  color: var(--color-text-inverse);
  font-size: 9px;
  font-weight: var(--font-weight-bold);
  border-radius: var(--radius-sm);
  line-height: 1;
}

/* ========== Input Fields ========== */
.input {
  padding: var(--space-sm) var(--space-md);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  color: var(--color-text-primary);
  background: var(--color-bg-panel);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  transition: all var(--transition-base);
}

.input:hover {
  border-color: var(--color-border-strong);
}

.input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: var(--shadow-focus);
}

.input:disabled {
  background: var(--color-bg-sidebar);
  color: var(--color-text-disabled);
  cursor: not-allowed;
}

/* ========== Select Dropdown ========== */
.select {
  padding: var(--space-sm) var(--space-md);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  color: var(--color-text-primary);
  background: var(--color-bg-panel);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-base);
}

.select:hover {
  border-color: var(--color-border-strong);
}

.select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: var(--shadow-focus);
}

/* ========== Card ========== */
.card {
  background: var(--color-bg-panel);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

/* ========== Modal ========== */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--color-bg-overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--z-modal);
  animation: fadeIn var(--transition-slow);
}

.modal {
  background: var(--color-bg-panel);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-2xl);
  max-width: 90vw;
  max-height: 90vh;
  overflow: auto;
  animation: scaleIn var(--transition-slow);
}

.modal-header {
  padding: var(--space-lg);
  border-bottom: 1px solid var(--color-border);
}

.modal-title {
  margin: 0;
  font-size: var(--font-size-xl);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
}

.modal-body {
  padding: var(--space-lg);
}

.modal-footer {
  padding: var(--space-lg);
  border-top: 1px solid var(--color-border);
  display: flex;
  gap: var(--space-md);
  justify-content: flex-end;
}

/* ========== Badge ========== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px var(--space-sm);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-medium);
  border-radius: var(--radius-full);
  line-height: 1;
}

.badge-primary {
  background: var(--color-primary-lighter);
  color: var(--color-primary);
}

.badge-success {
  background: var(--color-success-light);
  color: var(--color-success);
}

.badge-error {
  background: var(--color-error-light);
  color: var(--color-error);
}

.badge-warning {
  background: var(--color-warning-light);
  color: var(--color-warning);
}

.badge-premium {
  background: var(--color-premium-bg);
  color: var(--color-premium);
}

/* ========== Tooltip ========== */
.tooltip {
  position: absolute;
  padding: var(--space-xs) var(--space-sm);
  background: var(--color-text-primary);
  color: var(--color-text-inverse);
  font-size: var(--font-size-xs);
  border-radius: var(--radius-md);
  white-space: nowrap;
  z-index: var(--z-tooltip);
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--transition-base);
}

.tooltip.show {
  opacity: 1;
}

/* ========== Separator ========== */
.separator {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: var(--space-md) 0;
}

.separator-vertical {
  border: none;
  border-left: 1px solid var(--color-border);
  margin: 0 var(--space-md);
  height: 100%;
}

/* ========== Panel ========== */
.panel {
  background: var(--color-bg-panel);
  border-left: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-header {
  padding: var(--space-md);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.panel-title {
  margin: 0;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
}

.panel-body {
  flex: 1;
  padding: var(--space-md);
  overflow-y: auto;
}

/* ========== Tabs ========== */
.tabs {
  display: flex;
  border-bottom: 1px solid var(--color-border);
  width: 100%;
  overflow-x: auto;
  flex-wrap: nowrap;
}

.tab {
  padding: var(--space-sm) var(--space-xs);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-secondary);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  transition: all var(--transition-base);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 1;
  min-width: 0;
}

.tab:hover {
  color: var(--color-text-primary);
  background: var(--color-bg-hover);
}

.tab.active {
  color: var(--color-primary);
  border-bottom-color: var(--color-primary);
}

/* ========== Form Group ========== */
.form-group {
  margin-bottom: var(--space-md);
}

.form-label {
  display: block;
  margin-bottom: var(--space-xs);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-primary);
}

.form-help {
  margin-top: var(--space-xs);
  font-size: var(--font-size-xs);
  color: var(--color-text-secondary);
}

/* ========== Animations ========== */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ========== Utility Classes ========== */
.hidden {
  display: none !important;
}

.flex {
  display: flex;
}

.flex-col {
  flex-direction: column;
}

.items-center {
  align-items: center;
}

.justify-between {
  justify-content: space-between;
}

.gap-sm {
  gap: var(--space-sm);
}

.gap-md {
  gap: var(--space-md);
}

.text-xs {
  font-size: var(--font-size-xs);
}

.text-sm {
  font-size: var(--font-size-sm);
}

.text-base {
  font-size: var(--font-size-base);
}

.font-medium {
  font-weight: var(--font-weight-medium);
}

.font-semibold {
  font-weight: var(--font-weight-semibold);
}

.text-secondary {
  color: var(--color-text-secondary);
}

.text-error {
  color: var(--color-error);
}

.text-success {
  color: var(--color-success);
}

/* ========== Symbol Palette Buttons ========== */
.symbol-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-bold);
  background: var(--color-bg-panel);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text-primary);
  cursor: pointer;
  transition: all var(--transition-base);
}

.symbol-btn:hover {
  background: var(--color-bg-hover);
  border-color: var(--color-border-strong);
}

.symbol-btn.active {
  background: var(--color-primary-lighter);
  border-color: var(--color-primary);
  color: var(--color-primary);
}

/* ========== Modal (generic) ========== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-overlay.hidden {
  display: none;
}

.modal {
  background: var(--color-surface, #ffffff);
  border-radius: 8px;
  padding: 16px;
  max-width: 480px;
  width: 90%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
}

/* ========== DMC search results ========== */
.dmc-results {
  overflow-y: auto;
  max-height: 50vh;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.dmc-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  cursor: pointer;
  border-radius: 4px;
}

.dmc-row:hover {
  background: rgba(0, 0, 0, 0.06);
}

.dmc-swatch {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  flex-shrink: 0;
}

/* ========== Palette list (Colors tab, right panel) ========== */
.palette-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 8px;
}

.palette-actions {
  display: flex;
  gap: 4px;
}

.palette-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px;
  border-radius: 6px;
}

.palette-row.active {
  background: rgba(108, 92, 231, 0.08);
}

.palette-row .palette-swatch {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  border: 2px solid transparent;
  cursor: pointer;
  flex-shrink: 0;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15);
}

.palette-row.active .palette-swatch {
  border-color: var(--color-primary, #6c5ce7);
}

.palette-row .palette-name {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  border: 1px solid transparent;
  background: transparent;
  padding: 2px 4px;
  border-radius: 4px;
  color: inherit;
}

.palette-row .palette-name:focus {
  border-color: var(--color-primary, #6c5ce7);
  background: var(--color-surface, #fff);
  outline: none;
}

.palette-row .palette-remove {
  border: none;
  background: transparent;
  cursor: pointer;
  color: inherit;
  opacity: 0.5;
  font-size: 14px;
  padding: 0 4px;
}

.palette-row .palette-remove:hover {
  opacity: 1;
}

/* ========== Follow mode controls ========== */
.follow-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

#followModeBtn.active {
  background: var(--color-primary, #6c5ce7);
  color: #fff;
}

.palette-row .palette-edit {
  border: none;
  background: transparent;
  cursor: pointer;
  color: inherit;
  opacity: 0.5;
  font-size: 13px;
  padding: 0 4px;
}

.palette-row .palette-edit:hover {
  opacity: 1;
}

/* Current brush color button (top toolbar) */
#brushColorBtn {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-md);
  border: 2px solid var(--color-border-strong);
  cursor: pointer;
  padding: 0;
  background: #000;
}

/* Quick palette popover under the brush color button.
   position: fixed so the top toolbar's scroll container can't clip it;
   coordinates are set from the button rect when it opens. */
.palette-popover {
  position: fixed;
  z-index: 980;
  background: var(--color-bg-panel);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: var(--space-sm);
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  width: 176px;
}

.palette-popover .pop-swatch {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-md);
  border: 2px solid transparent;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15);
  padding: 0;
}

.palette-popover .pop-swatch.active {
  border-color: var(--color-primary);
}

.palette-popover .pop-more {
  width: 100%;
  margin-top: 2px;
}

/* Color picker dialog */
.color-picker-dialog {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  width: 244px;
}

.color-picker-dialog canvas {
  border-radius: var(--radius-md);
  cursor: crosshair;
  display: block;
}

.cp-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.cp-row .btn {
  flex: 1;
}

.cp-preview {
  width: 34px;
  height: 34px;
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border-strong);
  flex-shrink: 0;
}

#cpHex {
  flex: 1;
  min-width: 0;
  font-family: ui-monospace, monospace;
}
