/* ==== Variables & Reset ==== */
:root {
  --bg-dark: #121212;
  --bg-panel: #1e1e1e;
  --bg-panel-hover: #2c2c2c;
  --text-main: #f0f0f0;
  --text-muted: #a0a0a0;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --danger: #ef4444;
  --success: #10b981;
  --border: #333333;
  --border-light: #444;
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --border-radius: 8px;
  --transition: 0.2s ease;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-family);
  background-color: var(--bg-dark);
  color: var(--text-main);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-size: 14px;
}

/* ==== Header ==== */
.header {
  height: 60px;
  background-color: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
  z-index: 100;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  font-size: 24px;
  color: var(--primary);
}

.brand-name {
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 1px;
}

.brand-tag {
  background: var(--primary);
  color: white;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
}

.header-actions {
  display: flex;
  gap: 12px;
  align-items: center;
}

/* ==== Buttons & Inputs ==== */
button {
  cursor: pointer;
  border: none;
  background: none;
  font-family: inherit;
  color: inherit;
  transition: var(--transition);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-icon {
  width: 36px;
  height: 36px;
  border-radius: var(--border-radius);
  background: var(--bg-dark);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.btn-icon:not(:disabled):hover {
  background: var(--bg-panel-hover);
  border-color: var(--primary);
}

.btn-text {
  background: var(--primary);
  color: white;
  padding: 0 16px;
  height: 36px;
  border-radius: var(--border-radius);
  font-weight: 600;
}

.btn-text:not(:disabled):hover {
  background: var(--primary-hover);
}

.num-input,
.select-input,
.key-input,
.search-input {
  background: var(--bg-dark);
  border: 1px solid var(--border);
  color: var(--text-main);
  border-radius: 4px;
  padding: 8px 12px;
  font-size: 14px;
  width: 100%;
  outline: none;
  transition: border-color 0.2s;
}

.num-input:focus,
.select-input:focus,
.key-input:focus,
.search-input:focus {
  border-color: var(--primary);
}

/* ==== Layout ==== */
.app-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.sidebar {
  width: 300px;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  flex-shrink: 0;
}

.sidebar::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.panel {
  padding: 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.panel-title {
  font-weight: 600;
  font-size: 15px;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* UI Elements inside Panels */
.upload-area {
  border: 2px dashed var(--border-light);
  border-radius: var(--border-radius);
  padding: 30px 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  background: rgba(255, 255, 255, 0.02);
  position: relative;
}

.upload-area:hover,
.upload-area.dragover {
  border-color: var(--primary);
  background: rgba(99, 102, 241, 0.05);
}

.upload-icon {
  font-size: 32px;
  margin-bottom: 10px;
}

.upload-text {
  font-weight: 500;
  margin-bottom: 4px;
}

.upload-sub {
  font-size: 12px;
  color: var(--text-muted);
}

.preview-img {
  max-width: 100%;
  max-height: 200px;
  border-radius: 4px;
  pointer-events: none;
}

.hidden {
  display: none !important;
}

.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.setting-row label {
  color: var(--text-muted);
  white-space: nowrap;
}

.preset-btns {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.preset-btn {
  background: var(--bg-dark);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 12px;
}

.preset-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

/* Key Panel */
.key-input-row {
  display: flex;
  gap: 8px;
}

.btn-verify {
  background: var(--bg-dark);
  border: 1px solid var(--border);
  padding: 0 16px;
  border-radius: 4px;
  white-space: nowrap;
}

.btn-verify:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.key-status {
  font-size: 12px;
  color: var(--success);
}

.btn-convert {
  background: var(--success);
  color: white;
  width: 100%;
  padding: 12px 0;
  border-radius: var(--border-radius);
  font-size: 16px;
  font-weight: 600;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
}

.btn-convert:not(:disabled):hover {
  background: #059669;
}

/* Tools */
.tool-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.tool-btn {
  background: var(--bg-dark);
  border: 1px solid var(--border);
  padding: 10px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
}

.tool-btn.active {
  background: rgba(99, 102, 241, 0.1);
  border-color: var(--primary);
  color: var(--primary);
}

.toggle-row {
  display: flex;
  justify-content: space-between;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  color: var(--text-muted);
}

.toggle-label input {
  accent-color: var(--primary);
  cursor: pointer;
}

/* Stats */
.badge {
  background: var(--bg-dark);
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  color: var(--primary);
}

.stats-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 200px;
  overflow-y: auto;
}

.stat-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px;
  background: var(--bg-dark);
  border-radius: 4px;
}

.stat-color-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stat-color-box {
  width: 16px;
  height: 16px;
  border-radius: 2px;
  border: 1px solid var(--border);
}

.stat-code {
  font-size: 12px;
  font-weight: 600;
  width: 40px;
}

.stat-count {
  font-size: 12px;
  color: var(--text-muted);
}

/* Palette Sidebar */
.palette-sidebar {
  width: 250px;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.palette-sidebar>.panel-title {
  padding: 20px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0;
}

.color-search {
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
}

.palette-grid {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.palette-grid::-webkit-scrollbar {
  width: 6px;
}

.palette-grid::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.palette-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.2s;
}

.palette-item:hover {
  background: var(--bg-dark);
}

.palette-item.active {
  border-color: var(--primary);
  background: rgba(99, 102, 241, 0.1);
}

.palette-checkbox {
  cursor: pointer;
  accent-color: var(--primary);
}

.palette-preview {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.palette-info {
  display: flex;
  flex-direction: column;
}

.palette-code {
  font-size: 13px;
  font-weight: 600;
}

.palette-hex {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
}

/* Canvas Container */
.canvas-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #0a0a0a;
  position: relative;
  overflow: hidden;
}

.canvas-toolbar {
  position: absolute;
  top: 20px;
  right: 20px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  display: flex;
  padding: 4px;
  z-index: 10;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
}

.canvas-zoom-btn {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  font-weight: bold;
}

.canvas-zoom-btn:hover {
  background: var(--bg-dark);
  color: var(--primary);
}

.zoom-label {
  padding: 0 12px;
  display: flex;
  align-items: center;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}

.canvas-viewport {
  flex: 1;
  overflow: auto;
  cursor: grab;
  position: relative;
  /* Fixed bug: Allows absolute tip positioning relative to it if needed, though tip is in container */
}

.canvas-viewport:active {
  cursor: grabbing;
}

.canvas-wrap {
  margin: auto;
  transform-origin: center center;
  /* Optional now, kept for safety */
  position: relative;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
  background-color: white;
  /* Make background white for empty space */
}

canvas {
  display: block;
  /* Image rendering optimized in JS */
}

.canvas-tip {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: rgba(255, 255, 255, 0.3);
  font-size: 18px;
  pointer-events: none;
}

/* Loading & Toast */
.loading-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.loading-box {
  background: var(--bg-panel);
  padding: 30px 40px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.toast {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-panel);
  color: white;
  padding: 12px 24px;
  border-radius: 20px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
  border: 1px solid var(--border);
  z-index: 10000;
  transition: opacity 0.3s, bottom 0.3s;
  opacity: 1;
}

.toast.hidden {
  opacity: 0;
  bottom: 10px;
  pointer-events: none;
}

/* Responsive */
@media (max-width: 900px) {
  .app-layout {
    flex-direction: column;
    /* Fixed: standard column prevents top-overflow clipping */
  }

  .palette-sidebar {
    order: 1;
    /* top */
  }

  .canvas-container {
    order: 2;
    /* middle */
    flex: 1;
    min-height: 0;
    /* CRITICAL: prevents height from forcing other items out of viewport */
  }

  .sidebar {
    order: 3;
    /* bottom */
    width: 100%;
    border-right: none;
    border-left: none;
    border-top: 1px solid var(--border);
    flex: 0 0 auto;
    max-height: 40vh;
  }

  .palette-sidebar {
    width: 100%;
    border-right: none;
    border-left: none;
    border-top: 1px solid var(--border);
    flex: 0 0 auto;
    max-height: none;
  }

  /* hide search to save space on mobile */
  .color-search {
    display: none;
  }

  .palette-sidebar>.panel-title {
    display: none;
    /* Mobile screen logic: save space */
  }

  .palette-grid {
    flex: none;
    width: 100%;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 12px 15px;
    gap: 12px;
    min-height: 100px;
    /* guarantees sufficient height */
  }

  .palette-item {
    display: flex;
    flex: 0 0 auto;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 10px;
    background: var(--bg-dark);
    min-width: 75px;
    gap: 8px;
    border-radius: 8px;
  }

  .palette-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  .canvas-toolbar {
    top: auto;
    bottom: 20px;
  }
}