/* ===== Tokens ===== */
:root {
  --bg: #131313;
  --surface: #0a0a0a;
  --border: #232323;
  --border-strong: #3a3a3a;
  --text: #f0f0ed;
  --text-secondary: #b8b8b6;
  --text-muted: #6a6a68;
  --text-disabled: #4a4a48;
  --primary-bg: #f0f0ed;
  --primary-fg: #131313;
  --error: #7a2d2d;

  --font-display: "Bebas Neue", sans-serif;
  --font-body: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --radius-sm: 4px;
  --radius-canvas: 2px;
  --radius-frame: 8px;

  color-scheme: dark;
}

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

html, body {
  height: 100%;
}

body {
  font-family: var(--font-body);
  font-size: 13px;
  line-height: 1.4;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

button {
  font-family: inherit;
}

.hidden {
  display: none !important;
}

/* ===== Layout ===== */
.container {
  max-width: 800px;
  margin: 0 auto;
  border-radius: var(--radius-frame);
  overflow: hidden;
}

/* ===== Top bar ===== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 28px;
  border-bottom: 1px solid var(--border);
}

.topbar__brand {
  font-family: var(--font-display);
  font-size: 20px;
  letter-spacing: 0.08em;
  color: var(--text);
}

.topbar__right {
  position: relative;
  display: flex;
  align-items: center;
  gap: 14px;
}

/* ===== Help button + popover ===== */
.help-btn {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.help-btn:hover,
.help-btn[aria-expanded="true"] {
  border-color: var(--border-strong);
  color: var(--text);
}

.help-popover {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  width: 240px;
  background: #1a1a1a;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  z-index: 50; /* below error-banner (1000) and loading-overlay (2000) */
}

.help-popover__label {
  display: block;
  font-size: 9px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.help-popover__list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  line-height: 1.55;
  color: #c8c8c6;
}

/* ===== Stage ===== */
.stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  padding: 40px 28px;
}

#upload-section,
#result-section {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

#upload-section {
  padding: 40px 0;
}

/* ===== Drop zone ===== */
.drop-zone {
  width: 100%;
  max-width: 520px;
  aspect-ratio: 16 / 9;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  transition: border-color 0.15s;
}

.drop-zone:hover,
.drop-zone.drag-over {
  border-color: var(--border-strong);
}

.drop-zone__title {
  font-size: 13px;
  color: var(--text-secondary);
}

.drop-zone__hint {
  font-size: 11px;
  color: var(--text-muted);
}

/* ===== Canvas stack ===== */
.canvas-stack {
  position: relative;
  display: inline-block;
  max-width: 100%;
  line-height: 0;
}

.canvas-stack #result-canvas {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-canvas);
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: default;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.canvas-stack #selection-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  border-radius: var(--radius-canvas);
  background: transparent;
}

.canvas-stack.crop-mode #edit-input,
.canvas-stack.crop-mode #selection-canvas {
  display: none;
}

.canvas-stack #edit-input {
  position: absolute;
  font-family: "Bebas Neue", sans-serif;
  color: transparent;
  background: transparent;
  caret-color: #4a9eff;
  border: none;
  border-radius: 0;
  outline: none;
  padding: 0;
  margin: 0;
  text-align: left;
  text-transform: uppercase;
  resize: none;
  overflow: hidden;
  white-space: pre;
  letter-spacing: inherit;
}

/* ===== Toolbar ===== */
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 100%;
}

#result-section .canvas-stack,
#result-section .toolbar {
  width: min(100%, 600px);
}

/* ===== Swatch (color picker wrapper) ===== */
.swatch {
  position: relative;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  overflow: hidden;
  cursor: pointer;
  flex-shrink: 0;
  transition: border-color 0.15s;
}

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

.swatch input[type="color"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: none;
  padding: 0;
  margin: 0;
  background: none;
  cursor: pointer;
  opacity: 1;
}

/* ===== Icon button ===== */
.icon-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  cursor: pointer;
  flex-shrink: 0;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}

.icon-btn svg {
  width: 16px;
  height: 16px;
}

.icon-btn:hover:not(:disabled) {
  border-color: var(--border-strong);
  color: var(--text);
}

.icon-btn:active:not(:disabled) {
  transform: scale(0.97);
}

.icon-btn:disabled {
  color: var(--text-disabled);
  cursor: not-allowed;
}

.icon-btn--primary {
  margin-left: auto;
  background: var(--primary-bg);
  color: var(--primary-fg);
  border-color: var(--primary-bg);
  width: auto;
  padding: 0 14px;
  height: 32px;
  gap: 8px;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
}

.icon-btn--primary svg {
  width: 14px;
  height: 14px;
}

.icon-btn--primary:hover:not(:disabled) {
  background: #fff;
  border-color: #fff;
  color: var(--primary-fg);
}

/* ===== Word buttons (Done / Cancel during crop) ===== */
.btn {
  padding: 0 14px;
  height: 32px;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}

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

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--primary-bg);
  color: var(--primary-fg);
  border-color: var(--primary-bg);
  margin-left: auto;
}

.btn-primary:hover {
  background: #fff;
  border-color: #fff;
  color: var(--primary-fg);
}

/* ===== Debug details ===== */
.debug-details {
  margin-top: 4px;
  width: min(100%, 600px);
}

.debug-details summary {
  list-style: none;
  cursor: pointer;
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 6px 0;
}

.debug-details summary::-webkit-details-marker,
.debug-details summary::marker {
  display: none;
}

.debug-details summary::before {
  content: "▸";
  display: inline-block;
  margin-right: 8px;
  transition: transform 0.15s;
}

.debug-details[open] summary::before {
  transform: rotate(90deg);
}

.debug-details summary:hover {
  color: var(--text-secondary);
}

.debug-details__body {
  padding-top: 8px;
  display: flex;
  justify-content: center;
}

.debug-details__body canvas {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-canvas);
  border: 1px solid var(--border);
  background: var(--surface);
}

/* ===== Text button (Replace photo) ===== */
.text-btn {
  margin-top: 8px;
  padding: 6px 0;
  background: transparent;
  border: none;
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
  cursor: pointer;
  transition: color 0.15s;
  align-self: center;
}

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

/* ===== Loading overlay ===== */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(19, 19, 19, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.loading-overlay p {
  margin-top: 1.25rem;
  font-size: 13px;
  color: var(--text-secondary);
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid #2a2a2a;
  border-top-color: var(--text);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* ===== Error banner ===== */
.error-banner {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--error);
  color: var(--text);
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(255, 255, 255, 0.08);
  font-size: 13px;
  max-width: 90vw;
  z-index: 1000;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.5);
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  .spinner {
    animation: none;
  }

  .icon-btn,
  .btn,
  .swatch,
  .help-btn,
  .drop-zone,
  .debug-details summary::before {
    transition: none;
  }
}

/* ===== Responsive (≤480px) ===== */
@media (max-width: 480px) {
  /* App-like: lock the page to the viewport so it doesn't scroll/bounce
     and the canvas owns all touch gestures. */
  html, body {
    height: 100dvh;
    overflow: hidden;
    overscroll-behavior: none;
  }

  body {
    position: fixed;
    inset: 0;
    width: 100%;
  }

  .container {
    border-radius: 0;
    height: 100dvh;
    display: flex;
    flex-direction: column;
  }

  .stage {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .topbar {
    padding: 14px 18px;
  }

  .topbar__brand {
    font-size: 17px;
  }

  /* On mobile we hide nothing extra — the ? button stays present. */

  .stage {
    padding: 18px 16px;
    gap: 14px;
  }

  #upload-section,
  #result-section {
    gap: 14px;
  }

  .drop-zone {
    aspect-ratio: 4 / 3;
  }

  .toolbar {
    gap: 6px;
  }

  .swatch {
    width: 36px;
    height: 36px;
  }

  .icon-btn {
    width: 36px;
    height: 36px;
  }

  .icon-btn--primary {
    width: auto;
    height: 36px;
    padding: 0 12px;
  }

  .btn {
    height: 36px;
  }

  .help-popover {
    width: 220px;
  }
}
