:root {
  --background: #ffffff;
  --foreground: #0f172a;
  --secondary: #f1f5f9;
  --secondary-hover: #e2e8f0;
  --muted-foreground: #64748b;
  --border: #e2e8f0;
  --input: #e2e8f0;
  --radius: 0.375rem;
  --primary-foreground: #f8fafc;
  --canvas-bg: #f1f5f9;
  --page-bg: #f8fafc;
  --btn-hover: #1e293b;
  --overlay-bg: rgba(255, 255, 255, 0.9);
  --spinner-bg: rgba(255, 255, 255, 0.6);
  --thumb-remove-bg: rgba(15, 23, 42, 0.7);
  --shadow-card: rgba(0, 0, 0, 0.1);
  --shadow-info: rgba(0, 0, 0, 0.06);
  --accent: #3b82f6;
  --empty-text: #94a3b8;
}

/* System dark default */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --background: #0f172a;
    --foreground: #f1f5f9;
    --secondary: #1e293b;
    --secondary-hover: #334155;
    --muted-foreground: #94a3b8;
    --border: #1e293b;
    --input: #1e293b;
    --primary-foreground: #0f172a;
    --canvas-bg: #0f172a;
    --page-bg: #020617;
    --btn-hover: #e2e8f0;
    --overlay-bg: rgba(15, 23, 42, 0.9);
    --spinner-bg: rgba(15, 23, 42, 0.6);
    --thumb-remove-bg: rgba(241, 245, 249, 0.15);
    --shadow-card: rgba(0, 0, 0, 0.4);
    --shadow-info: rgba(0, 0, 0, 0.3);
    --accent: #60a5fa;
    --empty-text: #475569;
  }
}

/* Explicit dark override (user toggled) */
:root[data-theme="dark"] {
  --background: #0f172a;
  --foreground: #f1f5f9;
  --secondary: #1e293b;
  --secondary-hover: #334155;
  --muted-foreground: #94a3b8;
  --border: #1e293b;
  --input: #1e293b;
  --primary-foreground: #0f172a;
  --canvas-bg: #0f172a;
  --page-bg: #020617;
  --btn-hover: #e2e8f0;
  --overlay-bg: rgba(15, 23, 42, 0.9);
  --spinner-bg: rgba(15, 23, 42, 0.6);
  --thumb-remove-bg: rgba(241, 245, 249, 0.15);
  --shadow-card: rgba(0, 0, 0, 0.4);
  --shadow-info: rgba(0, 0, 0, 0.3);
  --accent: #60a5fa;
  --empty-text: #475569;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, sans-serif;
  font-size: 13px;
  background: var(--page-bg);
  color: var(--foreground);
  height: 100vh;
  display: flex;
  overflow: hidden;
}

/* ── SIDEBAR ── */
#sidebar {
  width: 280px;
  height: 100vh;
  background: var(--background);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

#sidebar-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#sidebar-header h1 {
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

#themeToggle {
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 0.25rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  cursor: pointer;
  color: var(--muted-foreground);
  flex-shrink: 0;
  transition:
    background 0.15s,
    color 0.15s;
}
#themeToggle:hover {
  background: var(--secondary);
  color: var(--foreground);
}

#sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.125rem;
}

/* ── FORM ELEMENTS ── */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.form-group > label {
  font-size: 0.6875rem;
  font-weight: 500;
  color: var(--muted-foreground);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  user-select: none;
}

input[type="text"],
input[type="number"],
select {
  height: 2rem;
  width: 100%;
  padding: 0 0.625rem;
  border: 1px solid var(--input);
  border-radius: var(--radius);
  background: var(--background);
  font-size: 0.8125rem;
  color: var(--foreground);
  transition: border-color 0.15s;
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--foreground);
}

input[type="range"] {
  -webkit-appearance: none;
  width: 100%;
  height: 4px;
  background: var(--secondary);
  border-radius: 2px;
  cursor: pointer;
  margin-top: 2px;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--foreground);
  border: 2px solid var(--background);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

input[type="color"] {
  -webkit-appearance: none;
  width: 100%;
  height: 2rem;
  border: 1px solid var(--input);
  border-radius: var(--radius);
  padding: 2px;
  cursor: pointer;
}

input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}
input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: calc(var(--radius) - 2px);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  height: 2rem;
}

input[type="checkbox"] {
  width: 0.875rem;
  height: 0.875rem;
  accent-color: var(--foreground);
  flex-shrink: 0;
}

.checkbox-label {
  font-size: 0.8125rem;
  line-height: 1;
  cursor: pointer;
}

/* ── BUTTONS ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 2rem;
  width: 100%;
  padding: 0 0.875rem;
  border-radius: var(--radius);
  font-size: 0.8125rem;
  font-weight: 500;
  line-height: 1;
  cursor: pointer;
  border: 1px solid transparent;
  transition:
    background-color 0.15s,
    color 0.15s,
    border-color 0.15s,
    opacity 0.15s;
  white-space: nowrap;
  user-select: none;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn:active:not(:disabled) {
  opacity: 0.9;
}
.btn svg {
  flex-shrink: 0;
}

.btn-default {
  background: var(--foreground);
  color: var(--primary-foreground);
  border-color: var(--foreground);
}
.btn-default:hover:not(:disabled) {
  background: var(--btn-hover);
  border-color: var(--btn-hover);
}
.btn-outline {
  background: var(--background);
  color: var(--foreground);
  border-color: var(--input);
}
.btn-outline:hover:not(:disabled) {
  background: var(--secondary);
}
.btn-secondary {
  background: var(--secondary);
  color: var(--foreground);
  border-color: var(--secondary);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--secondary-hover);
  border-color: var(--secondary-hover);
}

.btn-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.divider {
  height: 1px;
  background: var(--border);
}
.row {
  display: flex;
  gap: 0.625rem;
}
.row .form-group {
  flex: 1;
}

/* ── IMAGE COUNT BADGE ── */
#imgCount {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.125rem;
  height: 1.125rem;
  padding: 0 0.25rem;
  background: var(--foreground);
  color: var(--primary-foreground);
  border-radius: 999px;
  font-size: 0.625rem;
  font-weight: 600;
  margin-left: 0.375rem;
  line-height: 1;
}

/* ── POPOVER ── */
#popoverBackdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 40;
}
#popoverBackdrop.open {
  display: block;
}

#imgPopover {
  display: none;
  position: fixed;
  width: 320px;
  background: var(--background);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  box-shadow:
    0 8px 30px rgba(0, 0, 0, 0.12),
    0 2px 8px rgba(0, 0, 0, 0.06);
  z-index: 50;
  flex-direction: column;
  overflow: hidden;
}
#imgPopover.open {
  display: flex;
}

#popoverHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#popoverHeader h2 {
  font-size: 0.8125rem;
  font-weight: 600;
}

#popoverClose {
  width: 1.5rem;
  height: 1.5rem;
  padding: 0;
  border-radius: 0.25rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  cursor: pointer;
  color: var(--muted-foreground);
  transition:
    background 0.15s,
    color 0.15s;
}
#popoverClose:hover {
  background: var(--secondary);
  color: var(--foreground);
}

#thumbGrid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.75rem;
  overflow-y: auto;
  max-height: 280px;
  min-height: 80px;
}
#thumbGrid:empty::after {
  content: "No images added yet.";
  width: 100%;
  text-align: center;
  color: var(--muted-foreground);
  font-size: 0.75rem;
  padding: 1.5rem 0;
}

.thumb-item {
  position: relative;
  width: calc((100% - 1rem) / 3);
  aspect-ratio: 1;
  border-radius: 0.25rem;
  overflow: hidden;
  background: var(--secondary);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.thumb-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.thumb-remove {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 1.25rem;
  height: 1.25rem;
  border-radius: 50%;
  background: var(--thumb-remove-bg);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--foreground);
  opacity: 0;
  transition: opacity 0.15s;
  padding: 0;
}
.thumb-item:hover .thumb-remove {
  opacity: 1;
}

#popoverFooter {
  padding: 0.625rem 0.75rem;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}
#popoverFooter .btn {
  flex: 1;
}
#fileInput {
  display: none;
}

/* ── MAIN ── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--canvas-bg);
  position: relative;
  height: 100vh;
}

#preview-info {
  position: absolute;
  top: 0.875rem;
  right: 0.875rem;
  z-index: 20;
  background: var(--overlay-bg);
  backdrop-filter: blur(8px);
  padding: 0.375rem 0.875rem;
  border-radius: 2rem;
  border: 1px solid var(--border);
  font-size: 0.6875rem;
  color: var(--muted-foreground);
  font-weight: 500;
  display: flex;
  gap: 0.875rem;
  box-shadow: 0 2px 6px var(--shadow-info);
}
.stat-badge {
  display: flex;
  gap: 0.25rem;
}
.stat-badge span {
  color: var(--foreground);
  font-weight: 600;
}

#viewport-wrapper {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 2rem;
}

#canvas-container {
  position: absolute;
  top: 50%;
  left: 50%;
  background: var(--background);
  box-shadow: 0 10px 25px var(--shadow-card);
  transform-origin: center center;
  border-radius: 2px;
  cursor: default;
}

/* ── SPINNER OVERLAY ── */
#spinner-overlay {
  display: none;
  position: absolute;
  inset: 0;
  background: var(--spinner-bg);
  backdrop-filter: blur(2px);
  justify-content: center;
  align-items: center;
  z-index: 10;
  border-radius: 2px;
  pointer-events: none;
}
#spinner-overlay.visible {
  display: flex;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 2.5px solid var(--border);
  border-top-color: var(--foreground);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ── TOAST NOTIFICATION ── */
#toast {
  position: fixed;
  bottom: 1.25rem;
  left: 50%;
  transform: translateX(-50%) translateY(0.5rem);
  background: var(--foreground);
  color: var(--primary-foreground);
  padding: 0.5rem 1rem;
  border-radius: 2rem;
  font-size: 0.75rem;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 0.2s,
    transform 0.2s;
  z-index: 100;
  white-space: nowrap;
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── PHOTO CARDS ── */
.photo-card {
  position: absolute;
  overflow: visible;
  opacity: 0;
  cursor: grab;
  transition: opacity 0.3s ease;
}

.photo-card img {
  display: block;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

.photo-card::before {
  content: "";
  position: absolute;
  inset: 0;
  border: 1.5px solid transparent;
  border-radius: 1px;
  pointer-events: none;
  transition: border-color 0.12s;
}
.photo-card:hover::before,
.photo-card.selected::before {
  border-color: var(--accent);
}

.resize-handle {
  position: absolute;
  bottom: -5px;
  right: -5px;
  width: 10px;
  height: 10px;
  background: var(--background);
  border: 1.5px solid var(--accent);
  border-radius: 2px;
  cursor: nwse-resize;
  opacity: 0;
  transition: opacity 0.12s;
  z-index: 2;
}
.photo-card:hover .resize-handle,
.photo-card.selected .resize-handle {
  opacity: 1;
}
.photo-card.dragging {
  cursor: grabbing;
}
