/* ================================================================
   Screenshot Color Picker — Stylesheet
   ================================================================ */

/* -- Reset & Base ------------------------------------------------ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:           #0d1117;
  --surface:      #161b22;
  --surface-2:    #1c2128;
  --border:       #30363d;
  --text:         #e6edf3;
  --text-muted:   #7d8590;
  --accent:       #58a6ff;
  --accent-hover: #79c0ff;
  --accent-bg:    rgba(88, 166, 255, 0.1);
  --accent-text:  #0d1117;

  --r-sm: 4px;
  --r-md: 8px;
  --r-lg: 12px;

  --gap:    1rem;
  --gap-sm: 0.5rem;
  --gap-lg: 1.5rem;
  --gap-xl: 2rem;

  --t: 0.15s ease;
}

html {
  height: 100%;
  scroll-behavior: smooth;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

[hidden] { display: none !important; }

#fileInput {
  position: absolute;
  left: -9999px;
  opacity: 0;
  pointer-events: none;
}

#extractCanvas {
  position: absolute;
  left: -9999px;
}

/* -- App Header -------------------------------------------------- */
.app-header {
  flex-shrink: 0;
  padding: var(--gap-lg) var(--gap-xl);   /* matches footer vertical padding */
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-header-inner {
  max-width: 1920px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: var(--gap);
}

.app-title {
  font-size: 0.875rem;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
}

.app-subtitle {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* -- Main Layout ------------------------------------------------- */
.app-main {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  width: 100%;
  max-width: 1920px;
  margin: 0 auto;
  padding: var(--gap-lg) var(--gap-xl);
  display: flex;
  flex-direction: column;
  gap: var(--gap-lg);
}

/* -- Card -------------------------------------------------------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
}

/* Collapsible cards — hide everything except the header */
.card.is-collapsed > *:not(.card-header) { display: none; }

.collapse-btn { padding: 0.2rem 0.35rem; line-height: 0; }
.collapse-btn svg { display: block; transition: transform 0.2s ease; }
.card.is-collapsed .collapse-btn svg { transform: rotate(-90deg); }

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: 0.875rem var(--gap-lg);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  background: var(--surface-2);
}

.card-title { font-size: 0.9375rem; font-weight: 600; }

.card-header-end {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  flex-wrap: wrap;
}

.card-body { padding: var(--gap-lg); }

/* -- Drop Zone --------------------------------------------------- */
.drop-zone {
  padding: 4rem 2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.625rem;
  cursor: pointer;
  text-align: center;
  border: 2px dashed var(--border);
  border-radius: calc(var(--r-lg) - 1px);
  transition: background var(--t), border-color var(--t);
  user-select: none;
}

.drop-zone:hover, .drop-zone.is-over {
  background: var(--accent-bg);
  border-color: var(--accent);
}

.drop-zone-icon {
  color: var(--text-muted);
  line-height: 0;
  transition: color var(--t), transform var(--t);
}

.drop-zone:hover .drop-zone-icon,
.drop-zone.is-over .drop-zone-icon {
  color: var(--accent);
  transform: scale(1.05);
}

.drop-zone-title { font-size: 1.0625rem; font-weight: 600; }
.drop-zone-hint  { font-size: 0.875rem; color: var(--text-muted); }
.drop-zone-types { font-size: 0.8rem; color: var(--text-muted); letter-spacing: 0.03em; }

.link-btn {
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  font-weight: 500;
}
.link-btn:hover { color: var(--accent-hover); }

/* -- Workspace (50/50 split) ------------------------------------- */
.workspace {
  display: flex;
  gap: var(--gap-lg);
  flex: 1;
  min-height: 0;
  align-items: stretch;
}

/* Left column: preview card + history card stacked vertically */
.workspace-left {
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: var(--gap-lg);
}

.workspace-preview {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.workspace-preview .image-container {
  flex: 1;
  height: 0; /* flex:1 fills remaining space */
}

/* History section sits below the preview at its natural height */
#historySection { flex-shrink: 0; }

.workspace-sidebar {
  flex: 1 1 0;
  min-width: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--gap-lg);
  padding-bottom: var(--gap-lg);
}

/* Cards must not shrink — sidebar scrolls instead of clipping */
.workspace-sidebar > .card { flex-shrink: 0; }

/* Palette + Texture side by side */
.palette-texture-row {
  display: flex;
  gap: var(--gap-lg);
  flex-shrink: 0;
  min-width: 0;
}
.palette-texture-row > .card {
  flex: 1 1 0;
  min-width: 0;
}

/* -- Preview Bar ------------------------------------------------- */
.preview-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: 0.625rem var(--gap-lg);
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  flex-wrap: wrap;
}

.file-name {
  font-size: 0.8125rem;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 50vw;
}

.preview-bar-end {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.control-label { font-size: 0.8rem; color: var(--text-muted); white-space: nowrap; }

/* -- Image Container --------------------------------------------- */
.image-container {
  width: 100%;
  height: 800px; /* standalone default; overridden inside .workspace */
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.image-container img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
}

.image-container.is-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* -- Options Grid ------------------------------------------------ */
.options-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: var(--gap-lg);
  margin-bottom: var(--gap-lg);
}

.option-group   { display: flex; flex-direction: column; gap: 0.4rem; }
.option-hint    { font-size: 0.75rem; color: var(--text-muted); }

.option-label {
  font-size: 0.74rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent-bg);
  color: var(--accent);
  border-radius: 1em;
  padding: 0 0.45em;
  font-size: 0.8rem;
  font-weight: 700;
  min-width: 1.6em;
  text-transform: none;
  letter-spacing: 0;
  font-family: monospace;
}

/* -- Select ----------------------------------------------------- */
.select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.45rem 2rem 0.45rem 0.75rem;
  border-radius: var(--r-md);
  font-size: 0.875rem;
  font-family: inherit;
  cursor: pointer;
  outline: none;
  transition: border-color var(--t);
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%237d8590' stroke-width='2.5'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.65rem center;
  width: 100%;
}
.select:hover, .select:focus { border-color: var(--accent); }

/* -- Range ------------------------------------------------------ */
.range-input {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
  height: 4px;
}

/* -- Checkboxes -------------------------------------------------- */
.check-stack { display: flex; flex-direction: column; gap: 0.4rem; }

.check-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  cursor: pointer;
}

.check-item input[type="checkbox"] {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
  cursor: pointer;
  flex-shrink: 0;
}

/* -- Buttons ----------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.55rem 1.1rem;
  border-radius: var(--r-md);
  font-size: 0.875rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background var(--t), border-color var(--t), color var(--t), opacity var(--t);
  white-space: nowrap;
  line-height: 1;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-ghost { background: transparent; color: var(--text-muted); border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

.btn-sm { font-size: 0.8125rem; padding: 0.35rem 0.7rem; }

/* -- Button Group ------------------------------------------------ */
.btn-group {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  overflow: hidden;
}

.btn-group-item {
  background: var(--surface-2);
  color: var(--text-muted);
  border: none;
  border-right: 1px solid var(--border);
  padding: 0.35rem 0.75rem;
  font-size: 0.8125rem;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--t), color var(--t);
  white-space: nowrap;
}
.btn-group-item:last-child { border-right: none; }
.btn-group-item:hover { background: var(--surface); color: var(--text); }
.btn-group-item.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }

/* -- History Strip ----------------------------------------------- */
.history-strip {
  display: flex;
  gap: var(--gap-sm);
  padding: var(--gap-sm) var(--gap);
  overflow-x: auto;
  min-height: 82px;
  align-items: flex-start;
}

.history-thumb {
  flex-shrink: 0;
  width: 72px;
  cursor: pointer;
  border-radius: var(--r-sm);
  border: 2px solid transparent;
  overflow: hidden;
  position: relative;
  transition: border-color var(--t);
  background: var(--surface-2);
}
.history-thumb:hover  { border-color: var(--border); }
.history-thumb.active { border-color: var(--accent); }

.history-thumb img {
  width: 100%;
  height: 50px;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.history-thumb-name {
  padding: 2px 4px;
  font-size: 0.65rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: var(--surface-2);
}

.history-thumb-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 16px;
  height: 16px;
  background: rgba(0,0,0,0.7);
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
  opacity: 0;
  transition: opacity var(--t);
}
.history-thumb:hover .history-thumb-remove { opacity: 1; }

/* -- Palette Grid ------------------------------------------------ */
.palette-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: var(--gap-sm);
  padding: var(--gap);
}

/* ---- Swatch ---- */
.swatch {
  border-radius: var(--r-md);
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.05);
  transition: transform var(--t), box-shadow var(--t), border-color var(--t), opacity var(--t);
  cursor: default;
}
.swatch:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0,0,0,0.5);
  border-color: rgba(255,255,255,0.15);
}

/* Swatch already added to the texture — dimmed */
.swatch.in-texture         { opacity: 0.4; }
.swatch.in-texture:hover   { opacity: 0.65; }

/* The coloured block */
.swatch-color {
  display: block;
  height: 58px;
  width: 100%;
  position: relative;
}

/* "+" / "×" overlay button */
.swatch-add {
  position: absolute;
  bottom: 4px;
  right: 4px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.25);
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  opacity: 0;
  transition: opacity var(--t), background var(--t), border-color var(--t);
}
.swatch:hover .swatch-add        { opacity: 1; }
.swatch-add:hover                { background: var(--accent); border-color: var(--accent); opacity: 1; }
/* Remove-mode (already in texture) */
.swatch-add.is-remove:hover      { background: #c0392b; border-color: #e74c3c; opacity: 1; }

/* Info row below colour block */
.swatch-info {
  background: var(--surface-2);
  padding: 0.3rem 0.45rem;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.swatch-hex {
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 0.65rem;
  color: var(--text);
}

.swatch-pct { font-size: 0.6rem; color: var(--text-muted); }

/* List view */
.palette-grid.is-list {
  grid-template-columns: 1fr;
  gap: 3px;
  padding: 0.5rem var(--gap);
}
.palette-grid.is-list .swatch {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  height: 37px;
  border-radius: var(--r-sm);
}
.palette-grid.is-list .swatch-color {
  height: 100%;
  width: 51px;
  flex-shrink: 0;
}
.palette-grid.is-list .swatch-add {
  width: 14px;
  height: 14px;
  font-size: 11px;
  bottom: 3px;
  right: 3px;
  opacity: 1;           /* always visible in compact list view */
}
.palette-grid.is-list .swatch-info {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.75rem;
  padding: 0 0.75rem;
  flex: 1;
}
.palette-grid.is-list .swatch-hex { font-size: 0.8rem; }
.palette-grid.is-list .swatch-pct { font-size: 0.75rem; }

/* -- Texture Builder -------------------------------------------- */

/* Custom grid-size number input */
.tex-grid-input {
  width: 48px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.3rem 0.4rem;
  border-radius: var(--r-md);
  font-size: 0.8125rem;
  font-family: inherit;
  text-align: center;
  -moz-appearance: textfield;
}
.tex-grid-input::-webkit-outer-spin-button,
.tex-grid-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.tex-grid-input:hover, .tex-grid-input:focus { border-color: var(--accent); outline: none; }

.texture-hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 0.5rem var(--gap-lg) 0;
}

.texture-grid-wrap {
  padding: var(--gap);
  overflow-x: auto;
}

.texture-grid {
  display: grid;
  gap: 2px;
  width: 100%;
}

.texture-slot {
  aspect-ratio: 1;
  position: relative;
  border-radius: 2px;
  transition: outline 0.1s;
  min-width: 10px;
}

.texture-slot.empty {
  background: var(--surface-2);
  border: 1px dashed var(--border);
  cursor: default;
}

.texture-slot.filled {
  cursor: grab;
}
.texture-slot.filled:active { cursor: grabbing; }

.texture-slot.drag-over {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.texture-slot.is-dragging { opacity: 0.35; }

/* Remove (×) button inside a filled slot — matches .swatch-add style */
.slot-remove {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.25);
  cursor: pointer;
  font-size: 13px;
  display: none;           /* shown on hover via parent:hover below */
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
  transition: background var(--t), border-color var(--t);
}
.texture-slot.filled:hover .slot-remove { display: flex; }
.slot-remove:hover { background: #c0392b; border-color: #e74c3c; }

/* Export row at the bottom of texture section */
.texture-export-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--gap-lg);
  padding: var(--gap) var(--gap-lg) var(--gap-lg);
  border-top: 1px solid var(--border);
}

/* -- Export Row -------------------------------------------------- */
.export-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--gap-lg);
  margin-bottom: var(--gap-lg);
}

/* -- Toast ------------------------------------------------------- */
.toast {
  position: fixed;
  bottom: 1.75rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.6rem 1.25rem;
  border-radius: 2em;
  font-size: 0.875rem;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0,0,0,0.45);
  z-index: 9999;
  pointer-events: none;
  white-space: nowrap;
  animation: toast-in 0.2s ease;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(6px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* -- Footer ------------------------------------------------------ */
.app-footer {
  flex-shrink: 0;
  text-align: center;
  padding: var(--gap-lg);
  color: var(--text-muted);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
}

/* -- Responsive -------------------------------------------------- */
@media (max-width: 900px) {
  .app-header { padding: var(--gap) var(--gap-lg); }
  .app-main   { padding: var(--gap) var(--gap-lg); }
  .card-body  { padding: var(--gap); }
  .options-grid { grid-template-columns: 1fr 1fr; }

  .workspace { flex-direction: column; flex: none; height: auto; }
  .workspace-left { flex: none; }
  .workspace-preview { flex: none; }
  .workspace-preview .image-container { height: 56vw; flex: none; }
  .palette-texture-row { flex-direction: column; }
  .workspace-sidebar { overflow-y: visible; }
}

@media (max-width: 560px) {
  .app-header { padding: var(--gap); }
  .app-main   { padding: var(--gap); gap: var(--gap); }
  .options-grid { grid-template-columns: 1fr; }
  .export-row, .texture-export-row { gap: var(--gap); }
  .file-name { max-width: 55vw; }
  .app-subtitle { padding-left: 0; }
}
