/* ============================================================
   FrameIt — Batch Photo Framing App
   Design: Clean tool aesthetic, warm neutrals, teal accent
   ============================================================ */

/* --- Design Tokens --- */
:root {
  --text-xs:   clamp(0.75rem,  0.7rem  + 0.25vw, 0.875rem);
  --text-sm:   clamp(0.875rem, 0.8rem  + 0.35vw, 1rem);
  --text-base: clamp(1rem,     0.95rem + 0.25vw, 1.125rem);
  --text-lg:   clamp(1.125rem, 1rem    + 0.75vw, 1.5rem);
  --text-xl:   clamp(1.5rem,   1.2rem  + 1.25vw, 2.25rem);

  --space-1:  0.25rem;
  --space-2:  0.5rem;
  --space-3:  0.75rem;
  --space-4:  1rem;
  --space-5:  1.25rem;
  --space-6:  1.5rem;
  --space-8:  2rem;
  --space-10: 2.5rem;
  --space-12: 3rem;

  --font-body: 'General Sans', 'Helvetica Neue', sans-serif;

  --radius-sm: 0.375rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;

  --transition-interactive: 180ms cubic-bezier(0.16, 1, 0.3, 1);
}

/* Light mode (default) */
:root, [data-theme="light"] {
  --color-bg:             #f7f6f2;
  --color-surface:        #ffffff;
  --color-surface-2:      #f3f2ee;
  --color-border:         #d4d1ca;
  --color-divider:        #e8e6e1;
  --color-text:           #28251d;
  --color-text-muted:     #7a7974;
  --color-text-faint:     #bab9b4;
  --color-primary:        #01696f;
  --color-primary-hover:  #0c4e54;
  --color-primary-text:   #ffffff;
  --color-accent:         #437a22;
  --color-accent-hover:   #2e5c10;
  --color-accent-text:    #ffffff;
  --color-error:          #a12c7b;
  --color-success:        #437a22;
  --color-warning:        #964219;

  --shadow-sm: 0 1px 2px rgba(40,37,29,0.06);
  --shadow-md: 0 4px 12px rgba(40,37,29,0.08);
  --shadow-lg: 0 12px 32px rgba(40,37,29,0.12);

  --editor-bg: #e8e6e1;
  --checker-a: #e0ddd8;
  --checker-b: #d5d2cc;
}

/* Dark mode */
[data-theme="dark"] {
  --color-bg:             #171614;
  --color-surface:        #1c1b19;
  --color-surface-2:      #242320;
  --color-border:         #393836;
  --color-divider:        #2d2c2a;
  --color-text:           #cdccca;
  --color-text-muted:     #797876;
  --color-text-faint:     #5a5957;
  --color-primary:        #4f98a3;
  --color-primary-hover:  #227f8b;
  --color-primary-text:   #171614;
  --color-accent:         #6daa45;
  --color-accent-hover:   #4d8f25;
  --color-accent-text:    #171614;
  --color-error:          #d163a7;
  --color-success:        #6daa45;
  --color-warning:        #bb653b;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.3);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.4);

  --editor-bg: #111110;
  --checker-a: #1a1918;
  --checker-b: #222120;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --color-bg:             #171614;
    --color-surface:        #1c1b19;
    --color-surface-2:      #242320;
    --color-border:         #393836;
    --color-divider:        #2d2c2a;
    --color-text:           #cdccca;
    --color-text-muted:     #797876;
    --color-text-faint:     #5a5957;
    --color-primary:        #4f98a3;
    --color-primary-hover:  #227f8b;
    --color-primary-text:   #171614;
    --color-accent:         #6daa45;
    --color-accent-hover:   #4d8f25;
    --color-accent-text:    #171614;
    --color-error:          #d163a7;
    --color-success:        #6daa45;
    --color-warning:        #bb653b;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.3);
    --shadow-lg: 0 12px 32px rgba(0,0,0,0.4);
    --editor-bg: #111110;
    --checker-a: #1a1918;
    --checker-b: #222120;
  }
}

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

html {
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  scroll-behavior: smooth;
}

body {
  min-height: 100dvh;
  line-height: 1.6;
  font-family: var(--font-body);
  font-size: var(--text-base);
  color: var(--color-text);
  background-color: var(--color-bg);
  display: flex;
  flex-direction: column;
}

img, picture, video, canvas, svg { display: block; max-width: 100%; height: auto; }
input, button, textarea, select { font: inherit; color: inherit; }
h1, h2, h3, h4 { text-wrap: balance; line-height: 1.2; }
p { text-wrap: pretty; }
button { cursor: pointer; background: none; border: none; }

::selection {
  background: rgba(1,105,111,0.2);
  color: var(--color-text);
}

:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

a, button, [role="button"], input, textarea, select {
  transition: color var(--transition-interactive),
              background var(--transition-interactive),
              border-color var(--transition-interactive),
              box-shadow var(--transition-interactive);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Hidden attribute override — CSS display values break [hidden] */
[hidden] { display: none !important; }

/* --- Utility --- */
.text-faint { color: var(--color-text-faint); }
.text-muted { color: var(--color-text-muted); }
.text-xs { font-size: var(--text-xs); }
.text-sm { font-size: var(--text-sm); }
.link-text { color: var(--color-primary); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }

/* --- Buttons --- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: 500;
  border-radius: var(--radius-md);
  white-space: nowrap;
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
  line-height: 1.4;
}

.btn svg { flex-shrink: 0; }

.btn-sm { padding: var(--space-1) var(--space-3); font-size: var(--text-xs); }
.btn-xs { padding: 2px var(--space-2); font-size: var(--text-xs); }

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

.btn-accent {
  background: var(--color-accent);
  color: var(--color-accent-text);
}
.btn-accent:hover { background: var(--color-accent-hover); }

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

.btn-icon {
  padding: var(--space-2);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
}
.btn-icon:hover { color: var(--color-text); background: var(--color-surface-2); }

.btn:disabled { opacity: 0.5; pointer-events: none; }

/* --- Header --- */
.app-header {
  border-bottom: 1px solid var(--color-divider);
  background: var(--color-surface);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--space-3) var(--space-4);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: 700;
  font-size: var(--text-lg);
  color: var(--color-text);
  user-select: none;
}

.logo svg { color: var(--color-primary); }

.header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

/* --- Main --- */
.app-main {
  flex: 1;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

/* --- Step Panels --- */
.step-panel {
  background: var(--color-surface);
  border: 1px solid var(--color-divider);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.step-header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  cursor: pointer;
  user-select: none;
}

.step-number {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--color-primary);
  color: var(--color-primary-text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: var(--text-xs);
  flex-shrink: 0;
}

.step-title {
  font-size: var(--text-sm);
  font-weight: 600;
}

.step-desc {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-top: 1px;
}

.step-status {
  margin-left: auto;
  font-size: var(--text-xs);
  color: var(--color-success);
  font-weight: 500;
}

.step-body {
  padding: 0 var(--space-5) var(--space-5);
}

.step-panel[data-collapsed="true"] .step-body { display: none; }
.step-panel[data-collapsed="true"] .step-header { padding-bottom: var(--space-4); }

/* --- Upload Zone --- */
.upload-zone {
  border: 2px dashed var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-8) var(--space-4);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-text-muted);
}

.upload-zone:hover,
.upload-zone.drag-over {
  border-color: var(--color-primary);
  background: rgba(1,105,111,0.04);
}

[data-theme="dark"] .upload-zone:hover,
[data-theme="dark"] .upload-zone.drag-over {
  background: rgba(79,152,163,0.06);
}

.upload-zone svg { opacity: 0.5; }

.upload-zone-photos {
  padding: var(--space-6) var(--space-4);
}

/* --- Frame Preview --- */
.frame-preview-wrap {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-3);
  background: var(--color-surface-2);
  border-radius: var(--radius-md);
}

.frame-preview-wrap canvas {
  width: 64px;
  height: 64px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: repeating-conic-gradient(var(--checker-a) 0% 25%, var(--checker-b) 0% 50%) 50% / 12px 12px;
}

.frame-info {
  flex: 1;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

/* --- Mask Editor --- */
.mask-toolbar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
  flex-wrap: wrap;
}

.mask-canvas-container {
  position: relative;
  display: inline-block;
  width: 100%;
  max-width: 540px;
  margin: 0 auto;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--color-border);
  background: repeating-conic-gradient(var(--checker-a) 0% 25%, var(--checker-b) 0% 50%) 50% / 16px 16px;
  touch-action: none;
}

.mask-canvas-container canvas {
  width: 100%;
  height: auto;
  display: block;
}

.mask-rect {
  position: absolute;
  border: 2px solid var(--color-primary);
  background: rgba(1,105,111,0.12);
  cursor: move;
  touch-action: none;
}

[data-theme="dark"] .mask-rect {
  background: rgba(79,152,163,0.15);
}

.mask-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  background: var(--color-primary);
  border: 2px solid var(--color-surface);
  border-radius: 50%;
}

.mask-handle.nw { top: -7px; left: -7px; cursor: nw-resize; }
.mask-handle.ne { top: -7px; right: -7px; cursor: ne-resize; }
.mask-handle.sw { bottom: -7px; left: -7px; cursor: sw-resize; }
.mask-handle.se { bottom: -7px; right: -7px; cursor: se-resize; }

.mask-hint { text-align: center; margin-top: var(--space-2); }

/* --- Workspace --- */
.workspace {
  width: 100%;
}

.workspace-inner {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: var(--space-3);
  min-height: 400px;
}

/* Thumbnails */
.thumb-sidebar {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-height: 560px;
  overflow: hidden;
}

.thumb-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--color-divider);
}

.thumb-count {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  font-weight: 500;
  flex: 1;
}

.thumb-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  overflow-y: auto;
  flex: 1;
  padding-right: var(--space-1);
}

.thumb-list::-webkit-scrollbar { width: 4px; }
.thumb-list::-webkit-scrollbar-track { background: transparent; }
.thumb-list::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 2px; }

.thumb-item {
  position: relative;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 2px solid transparent;
  cursor: pointer;
  transition: border-color 0.15s;
  flex-shrink: 0;
}

.thumb-item:hover { border-color: var(--color-text-faint); }
.thumb-item.active { border-color: var(--color-primary); }

.thumb-item img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  display: block;
}

.thumb-item .thumb-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-size: 10px;
  padding: 2px 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thumb-item .thumb-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(0,0,0,0.5);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.15s;
}

.thumb-item:hover .thumb-remove { opacity: 1; }

/* Editor */
.editor-panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.editor-canvas-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  max-height: 480px;
  max-width: 480px;
  margin: 0 auto;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--color-border);
  background: var(--editor-bg);
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.editor-canvas-wrap:active { cursor: grabbing; }

.editor-canvas-wrap canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* Controls */
.editor-controls {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.control-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.control-label {
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--color-text-muted);
  min-width: 48px;
}

.range-input {
  flex: 1;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--color-border);
  border-radius: 2px;
  outline: none;
}

.range-input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--color-primary);
  border: 2px solid var(--color-surface);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}

.range-input::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--color-primary);
  border: 2px solid var(--color-surface);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}

.control-value {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  min-width: 44px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.control-buttons {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.export-buttons {
  padding-top: var(--space-2);
  border-top: 1px solid var(--color-divider);
}

/* --- Progress Overlay --- */
.progress-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}

.progress-card {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: var(--space-8);
  text-align: center;
  min-width: 280px;
  box-shadow: var(--shadow-lg);
}

.progress-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto var(--space-4);
}

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

.progress-text {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  margin-bottom: var(--space-3);
}

.progress-bar-wrap {
  height: 4px;
  background: var(--color-surface-2);
  border-radius: 2px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: var(--color-primary);
  border-radius: 2px;
  transition: width 0.3s;
  width: 0%;
}

/* --- Toast --- */
.toast-container {
  position: fixed;
  bottom: var(--space-4);
  right: var(--space-4);
  z-index: 1100;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.toast {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  box-shadow: var(--shadow-md);
  font-size: var(--text-sm);
  max-width: 320px;
  animation: toastIn 0.3s cubic-bezier(0.16,1,0.3,1);
}

.toast.error { border-left: 3px solid var(--color-error); }
.toast.success { border-left: 3px solid var(--color-success); }
.toast.info { border-left: 3px solid var(--color-primary); }

@keyframes toastIn {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* --- Footer --- */
.app-footer {
  text-align: center;
  padding: var(--space-4);
  font-size: var(--text-xs);
  color: var(--color-text-faint);
}

.app-footer a {
  color: inherit;
  text-decoration: none;
}

.app-footer a:hover {
  color: var(--color-text-muted);
  text-decoration: underline;
}

/* --- Responsive --- */
@media (max-width: 768px) {
  .app-main { padding: var(--space-3); gap: var(--space-3); }

  .step-header { padding: var(--space-3) var(--space-4); }
  .step-body { padding: 0 var(--space-4) var(--space-4); }

  .workspace-inner {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .thumb-sidebar {
    max-height: none;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    gap: var(--space-2);
    padding-bottom: var(--space-2);
  }

  .thumb-header {
    flex-direction: row;
    align-items: center;
    padding-bottom: 0;
    border-bottom: none;
    margin-bottom: var(--space-1);
  }

  .thumb-list {
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    gap: var(--space-2);
    padding-right: 0;
    padding-bottom: var(--space-1);
  }

  .thumb-item {
    min-width: 64px;
    max-width: 64px;
  }

  .editor-canvas-wrap {
    max-height: 360px;
    max-width: 360px;
  }

  .mask-handle {
    width: 20px;
    height: 20px;
  }

  .mask-handle.nw { top: -10px; left: -10px; }
  .mask-handle.ne { top: -10px; right: -10px; }
  .mask-handle.sw { bottom: -10px; left: -10px; }
  .mask-handle.se { bottom: -10px; right: -10px; }

  .export-buttons { flex-direction: column; }
  .export-buttons .btn { width: 100%; justify-content: center; }
}

@media (max-width: 480px) {
  .editor-canvas-wrap {
    max-height: 300px;
    max-width: 300px;
  }

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