/* ==========================================================================
   Design tokens: warm paper-stone light theme + deep spruce accent
   ========================================================================== */

:root {
  --font-sans: system-ui, -apple-system, "Noto Sans Thai", "Sukhumvit Set", "Leelawadee UI", sans-serif;

  /* Hex fallbacks */
  --bg: #f6f4ef;
  --surface: #fcfbf8;
  --surface-2: #edeae3;
  --surface-press: #e2ded5;
  --stage-bg: #e4e1d9;

  --ink: #1b1916;
  --ink-muted: #534f49;
  --border: #d5d1c8;
  --border-strong: #918c83;

  --accent: #145a43;
  --accent-hover: #104d39;
  --accent-active: #0c3e2e;
  --accent-soft: #dfefe8;
  --accent-ink: #113c2e;
  --on-accent: #fbfaf7;
  --focus-ring: #145a43;

  --danger-bg: #fbece9;
  --danger-border: #c96a5d;
  --danger-ink: #8f221b;
  --danger-press: #f4d8d2;

  --warn-bg: #f9f1db;
  --warn-border: #bfa056;
  --warn-ink: #573f0c;

  --toast-bg: #24211d;
  --toast-ink: #f8f6f2;

  /* 8 group colours: hex fallbacks (>= 5.4:1 with white digits, CVD-spaced) */
  --g1: #1d56c2;
  --g2: #ba420c;
  --g3: #0b6e68;
  --g4: #a81e70;
  --g5: #855d00;
  --g6: #6838b8;
  --g7: #1e7328;
  --g8: #b81d38;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-6: 24px;
  --space-8: 32px;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-pill: 999px;

  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --t-fast: 150ms;
  --t-base: 200ms;
}

@supports (color: oklch(0.5 0.1 0)) {
  :root {
    --bg: oklch(0.966 0.006 82);
    --surface: oklch(0.99 0.004 82);
    --surface-2: oklch(0.938 0.008 82);
    --surface-press: oklch(0.90 0.009 82);
    --stage-bg: oklch(0.91 0.009 82);

    --ink: oklch(0.19 0.012 75);
    --ink-muted: oklch(0.42 0.014 75);
    --border: oklch(0.855 0.010 80);
    --border-strong: oklch(0.64 0.014 78);

    --accent: oklch(0.40 0.105 162);
    --accent-hover: oklch(0.35 0.10 162);
    --accent-active: oklch(0.30 0.09 162);
    --accent-soft: oklch(0.925 0.026 162);
    --accent-ink: oklch(0.28 0.075 162);
    --on-accent: oklch(0.985 0.004 82);
    --focus-ring: oklch(0.40 0.105 162);

    --danger-bg: oklch(0.95 0.025 22);
    --danger-border: oklch(0.68 0.11 22);
    --danger-ink: oklch(0.41 0.155 24);
    --danger-press: oklch(0.90 0.04 22);

    --warn-bg: oklch(0.955 0.032 82);
    --warn-border: oklch(0.72 0.10 78);
    --warn-ink: oklch(0.36 0.085 68);

    --toast-bg: oklch(0.23 0.014 75);
    --toast-ink: oklch(0.975 0.005 82);

    --g1: oklch(0.47 0.18 260);
    --g2: oklch(0.51 0.18 38);
    --g3: oklch(0.48 0.105 188);
    --g4: oklch(0.47 0.185 345);
    --g5: oklch(0.49 0.12 85);
    --g6: oklch(0.45 0.19 294);
    --g7: oklch(0.49 0.145 144);
    --g8: oklch(0.48 0.185 16);
  }
}

/* ==========================================================================
   Reset & base
   ========================================================================== */

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
  background: var(--bg);
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

button,
input {
  font: inherit;
  color: inherit;
}

p,
h1,
ol {
  margin: 0;
}

.ico,
.ico-sm {
  display: block;
  flex-shrink: 0;
}

/* ==========================================================================
   App shell & state visibility
   ========================================================================== */

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  padding-top: env(safe-area-inset-top);
}

.empty-view,
.photo-view,
.panel-crop,
.panel-working,
.panel-result {
  display: none;
}

body[data-state="empty"] .empty-view {
  display: flex;
}

body[data-state="empty"] .dock {
  order: -1;
  position: static;
  background: transparent;
  border-top: 0;
  box-shadow: none;
  padding: 0;
}

body[data-state="empty"] .error-panel {
  margin: var(--space-3) var(--space-4) 0;
}

body[data-state="crop"] .photo-view,
body[data-state="working"] .photo-view,
body[data-state="result"] .photo-view {
  display: flex;
}

body[data-state="crop"] .panel-crop {
  display: flex;
}

body[data-state="working"] .panel-working {
  display: flex;
}

body[data-state="result"] .panel-result {
  display: flex;
}

body[data-state="result"] .photo-meta {
  display: flex;
}

body:not([data-state="crop"]) #crop-box {
  display: none;
}

body[data-state="working"] #stage {
  pointer-events: none;
}

body[data-state="working"] #view {
  opacity: 0.42;
}

body[data-state="result"] #view {
  touch-action: manipulation;
}

/* ==========================================================================
   Buttons & shared interactive primitives
   ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 44px;
  min-width: 44px;
  padding: 0 var(--space-4);
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  font-size: 0.9375rem;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  text-decoration: none;
  cursor: pointer;
  user-select: none;
  transition:
    background-color var(--t-fast) var(--ease-out),
    border-color var(--t-fast) var(--ease-out),
    color var(--t-fast) var(--ease-out),
    transform var(--t-fast) var(--ease-out),
    opacity var(--t-fast) var(--ease-out);
}

.btn:focus-visible,
.group-chip:focus-visible,
.move-chip:focus-visible,
.menu-item:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.btn:not(:disabled):active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: var(--on-accent);
  border-color: var(--accent);
}

.btn-primary:not(:disabled):active {
  background: var(--accent-active);
  border-color: var(--accent-active);
}

.btn-secondary {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--border-strong);
}

.btn-secondary:not(:disabled):active {
  background: var(--surface-press);
}

.btn-ghost {
  background: transparent;
  color: var(--ink-muted);
  border-color: transparent;
  padding-inline: var(--space-3);
}

.btn-ghost:not(:disabled):active {
  background: var(--surface-2);
  color: var(--ink);
}

.btn-quiet {
  background: var(--surface-2);
  color: var(--ink);
  border-color: transparent;
  min-height: 44px;
  padding: 0 10px;
  gap: 5px;
  font-size: 0.8125rem;
  font-weight: 500;
  border-radius: var(--radius-sm);
}

.btn-quiet:not(:disabled):active {
  background: var(--surface-press);
}

.btn-danger-subtle {
  background: var(--danger-bg);
  color: var(--danger-ink);
  border-color: var(--danger-border);
  padding: 0 var(--space-3);
  font-size: 0.875rem;
}

.btn-danger-subtle:not(:disabled):active {
  background: var(--danger-press);
}

.btn-icon {
  background: transparent;
  color: var(--ink-muted);
  padding: 0;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
}

.btn-icon:not(:disabled):active {
  background: var(--surface-press);
  color: var(--ink);
}

@media (hover: hover) {
  .btn-primary:not(:disabled):hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
  }

  .btn-secondary:not(:disabled):hover {
    background: var(--surface-2);
  }

  .btn-ghost:not(:disabled):hover,
  .btn-icon:not(:disabled):hover {
    background: var(--surface-2);
    color: var(--ink);
  }

  .btn-quiet:not(:disabled):hover {
    background: var(--surface-press);
  }

  .btn-danger-subtle:not(:disabled):hover {
    background: var(--danger-press);
  }
}

/* ==========================================================================
   State: empty (first screen)
   ========================================================================== */

.empty-view {
  flex: 1;
  flex-direction: column;
  justify-content: center;
  width: 100%;
  max-width: 480px;
  margin-inline: auto;
  padding:
    var(--space-6)
    max(var(--space-4), env(safe-area-inset-right))
    max(var(--space-6), env(safe-area-inset-bottom))
    max(var(--space-4), env(safe-area-inset-left));
  gap: var(--space-6);
}

.empty-brand {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  flex-shrink: 0;
  border-radius: var(--radius-md);
  background: var(--accent-soft);
  color: var(--accent-ink);
}

.brand-copy {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.app-title {
  font-size: 1.75rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.01em;
  text-wrap: balance;
}

.empty-lead {
  font-size: 1rem;
  line-height: 1.55;
  color: var(--ink-muted);
  word-break: normal;
  overflow-wrap: break-word;
  max-width: 34ch;
}

.model-status {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  align-self: flex-start;
  padding: 6px var(--space-3);
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  color: var(--ink-muted);
  font-size: 0.875rem;
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
  word-break: normal;
  overflow-wrap: break-word;
}

.model-status::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}

.empty-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.btn-hero {
  width: 100%;
  min-height: 56px;
  font-size: 1.0625rem;
  border-radius: var(--radius-lg);
}

.btn-secondary.btn-hero {
  min-height: 52px;
  font-size: 1rem;
}

.tips-list {
  list-style: none;
  padding: var(--space-4) 0 0;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.tips-list li {
  display: flex;
  align-items: baseline;
  gap: var(--space-3);
  font-size: 0.9375rem;
  line-height: 1.55;
  color: var(--ink);
  word-break: normal;
  overflow-wrap: break-word;
}

.tip-num {
  display: inline-grid;
  place-items: center;
  width: 24px;
  height: 24px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--ink-muted);
  font-size: 0.8125rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.reassure {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  font-size: 0.875rem;
  line-height: 1.55;
  color: var(--ink-muted);
  word-break: normal;
  overflow-wrap: break-word;
}

.reassure .ico-sm {
  margin-top: 3px;
  color: var(--accent);
}

/* ==========================================================================
   Photo workspace & crop overlay
   ========================================================================== */

.photo-view {
  flex: 1;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.stage-wrap {
  width: 100%;
  background: var(--stage-bg);
}

#stage {
  position: relative;
  width: 100%;
  overflow: hidden;
  background: var(--stage-bg);
}

#view {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  transition: opacity var(--t-base) var(--ease-out);
}

#crop-box {
  position: absolute;
  touch-action: none;
  border: 2px solid var(--surface);
  box-shadow:
    0 0 0 1px rgba(24, 21, 18, 0.72),
    inset 0 0 0 1px rgba(24, 21, 18, 0.45),
    0 0 0 9999px rgba(20, 18, 15, 0.56);
  cursor: move;
}

.crop-handle {
  position: absolute;
  width: 44px;
  height: 44px;
  touch-action: none;
}

.crop-handle::before,
.crop-handle::after {
  content: "";
  position: absolute;
  background: var(--surface);
  border-radius: 2px;
  box-shadow: 0 0 0 1.5px rgba(20, 18, 15, 0.85);
}

.crop-handle::before {
  width: 20px;
  height: 4px;
}

.crop-handle::after {
  width: 4px;
  height: 20px;
}

.crop-handle[data-h="nw"] {
  top: 0;
  left: 0;
  cursor: nwse-resize;
}

.crop-handle[data-h="nw"]::before,
.crop-handle[data-h="nw"]::after {
  top: 2px;
  left: 2px;
}

.crop-handle[data-h="ne"] {
  top: 0;
  right: 0;
  cursor: nesw-resize;
}

.crop-handle[data-h="ne"]::before,
.crop-handle[data-h="ne"]::after {
  top: 2px;
  right: 2px;
}

.crop-handle[data-h="sw"] {
  bottom: 0;
  left: 0;
  cursor: nesw-resize;
}

.crop-handle[data-h="sw"]::before,
.crop-handle[data-h="sw"]::after {
  bottom: 2px;
  left: 2px;
}

.crop-handle[data-h="se"] {
  bottom: 0;
  right: 0;
  cursor: nwse-resize;
}

.crop-handle[data-h="se"]::before,
.crop-handle[data-h="se"]::after {
  bottom: 2px;
  right: 2px;
}

.photo-meta {
  display: none;
  flex-direction: column;
  gap: var(--space-2);
  width: 100%;
  max-width: 560px;
  padding: var(--space-3) var(--space-4) var(--space-4);
}

.empty-result {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  color: var(--warn-ink);
  font-size: 0.9375rem;
  font-weight: 500;
  line-height: 1.55;
  word-break: normal;
  overflow-wrap: break-word;
}

.hint {
  font-size: 0.875rem;
  line-height: 1.55;
  color: var(--ink-muted);
  word-break: normal;
  overflow-wrap: break-word;
}

.hint-skipped {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
}

.dashed-sample {
  display: inline-block;
  width: 18px;
  height: 12px;
  flex-shrink: 0;
  border: 2px dashed var(--ink-muted);
  border-radius: 4px;
  transform: translateY(1px);
}

/* ==========================================================================
   Sticky bottom dock
   ========================================================================== */

.dock {
  position: sticky;
  bottom: 0;
  z-index: 20;
  width: 100%;
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 18px rgba(27, 25, 22, 0.06);
  padding-bottom: max(var(--space-3), env(safe-area-inset-bottom));
}

.dock-inner {
  width: 100%;
  max-width: 560px;
  margin-inline: auto;
}

/* Inline error banner */
.error-panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  margin: var(--space-3) var(--space-4) 0;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  color: var(--danger-ink);
}

.error-body {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
}

.error-ico {
  margin-top: 2px;
}

.error-text {
  font-size: 0.9375rem;
  line-height: 1.55;
  word-break: normal;
  overflow-wrap: break-word;
}

.btn-error-retry {
  align-self: flex-start;
  background: var(--surface);
  color: var(--danger-ink);
  border-color: var(--danger-border);
  min-height: 44px;
  padding: 0 var(--space-4);
  font-size: 0.9375rem;
}

.btn-error-retry:not(:disabled):active {
  background: var(--danger-press);
}

/* ==========================================================================
   Panel: crop
   ========================================================================== */

.panel-crop {
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4) 0;
}

.crop-help {
  font-size: 0.9375rem;
  line-height: 1.5;
  color: var(--ink-muted);
  word-break: normal;
  overflow-wrap: break-word;
}

.crop-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.crop-actions #btn-crop-apply {
  flex: 1;
  min-height: 48px;
}

.crop-actions #btn-crop-all,
.crop-actions #btn-crop-cancel {
  min-height: 48px;
}

/* ==========================================================================
   Panel: working
   ========================================================================== */

.panel-working {
  flex-direction: column;
  padding: var(--space-4);
}

.progress-box {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.progress-text {
  font-size: 1rem;
  font-weight: 500;
  line-height: 1.5;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  word-break: normal;
  overflow-wrap: break-word;
}

.progress-bar {
  appearance: none;
  -webkit-appearance: none;
  display: block;
  width: 100%;
  height: 10px;
  border: 0;
  border-radius: var(--radius-pill);
  overflow: hidden;
  background: var(--surface-2);
}

.progress-bar::-webkit-progress-bar {
  background: var(--surface-2);
  border-radius: var(--radius-pill);
}

.progress-bar::-webkit-progress-value {
  background: var(--accent);
  border-radius: var(--radius-pill);
  transition: width var(--t-base) var(--ease-out);
}

.progress-bar::-moz-progress-bar {
  background: var(--accent);
  border-radius: var(--radius-pill);
}

.progress-bar:not([value]),
.progress-bar:indeterminate {
  background-color: var(--accent-soft);
  background-image: linear-gradient(
    90deg,
    var(--accent-soft) 0%,
    var(--accent) 45%,
    var(--accent) 55%,
    var(--accent-soft) 100%
  );
  background-size: 200% 100%;
  animation: progress-slide 1.35s linear infinite;
}

.progress-bar:not([value])::-webkit-progress-bar {
  background: transparent;
}

.progress-bar:not([value])::-moz-progress-bar,
.progress-bar:indeterminate::-moz-progress-bar {
  background: transparent;
}

@keyframes progress-slide {
  from {
    background-position: 100% 0;
  }
  to {
    background-position: -100% 0;
  }
}

/* ==========================================================================
   Panel: result
   ========================================================================== */

.panel-result {
  flex-direction: column;
  gap: var(--space-2);
  padding-top: var(--space-3);
}

/* Row 1: Total + group management */
.total-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  padding-inline: var(--space-4);
}

.total-summary {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.total-label {
  font-size: 0.75rem;
  font-weight: 500;
  line-height: 1.25;
  color: var(--ink-muted);
  white-space: nowrap;
}

.total-figure {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

#total-count {
  font-size: 2.25rem;
  font-weight: 700;
  line-height: 1.05;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  white-space: nowrap;
}

.total-unit {
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--ink-muted);
  white-space: nowrap;
}

.group-tools {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

/* Row 2: Horizontal scrollable group list */
.group-list {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  overflow-x: auto;
  overscroll-behavior-x: contain;
  padding: 2px var(--space-4);
  scroll-padding-inline: var(--space-4);
  scrollbar-width: none;
}

.group-list::-webkit-scrollbar {
  display: none;
}

.group-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 12px;
  flex-shrink: 0;
  border-radius: var(--radius-pill);
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  font-size: 0.9375rem;
  font-weight: 500;
  line-height: 1.2;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  transition:
    background-color var(--t-fast) var(--ease-out),
    border-color var(--t-fast) var(--ease-out),
    transform var(--t-fast) var(--ease-out);
}

.group-chip:active {
  transform: scale(0.97);
}

.group-chip[aria-pressed="true"] {
  border-width: 2px;
  border-color: var(--c, var(--accent));
  background: var(--surface-2);
  font-weight: 600;
}

@supports (background: color-mix(in oklch, red 12%, white)) {
  .group-chip[aria-pressed="true"] {
    background: color-mix(in oklch, var(--c, var(--accent)) 12%, var(--surface));
  }
}

.swatch {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--c, var(--g1));
  box-shadow: inset 0 0 0 1px rgba(27, 25, 22, 0.18);
}

.group-name {
  white-space: nowrap;
}

.group-count {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 2px 7px;
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  color: var(--ink);
  font-size: 0.875rem;
}

.group-chip[aria-pressed="true"] .group-count {
  background: var(--c, var(--accent));
  color: var(--on-accent);
}

/* Selection bar: replaces action row while keeping total visible */
.selection-bar {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin: 2px var(--space-4) 0;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  border: 1px solid var(--border);
}

#selection-bar:not([hidden]) ~ .action-row {
  display: none;
}

.selection-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.selection-label {
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.selection-actions {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  flex-shrink: 0;
}

.move-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}

.move-label {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--ink-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.move-targets {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  padding: 2px 0;
  scrollbar-width: none;
  flex: 1;
  min-width: 0;
}

.move-targets::-webkit-scrollbar {
  display: none;
}

.move-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 12px;
  flex-shrink: 0;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink);
  font-size: 0.875rem;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  transition:
    background-color var(--t-fast) var(--ease-out),
    transform var(--t-fast) var(--ease-out);
}

.move-chip:active {
  background: var(--surface-press);
  transform: scale(0.97);
}

.move-name {
  white-space: nowrap;
}

/* Row 3: Action row (4 items: undo, crop, more, share) */
.action-row {
  display: flex;
  align-items: stretch;
  gap: 6px;
  padding: 2px var(--space-4) 0;
}

.btn-tool {
  flex-direction: column;
  gap: 3px;
  min-height: 48px;
  padding: 5px 8px;
  background: var(--surface);
  color: var(--ink);
  border-color: var(--border);
  font-size: 0.75rem;
  font-weight: 500;
  border-radius: var(--radius-md);
}

.btn-tool:not(:disabled):active {
  background: var(--surface-press);
}

.more-menu {
  position: relative;
  display: flex;
}

.more-menu > summary {
  list-style: none;
}

.more-menu > summary::-webkit-details-marker {
  display: none;
}

.more-menu[open] > summary {
  background: var(--surface-2);
  border-color: var(--border-strong);
}

.more-popover {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  z-index: 30;
  min-width: 184px;
  padding: 6px;
  border-radius: var(--radius-md);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  box-shadow: 0 10px 28px rgba(27, 25, 22, 0.14);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  min-height: 48px;
  padding: 0 var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink);
  font-size: 0.9375rem;
  font-weight: 500;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color var(--t-fast) var(--ease-out);
}

.menu-item:active {
  background: var(--surface-press);
}

.btn-share {
  flex: 1;
  min-height: 48px;
  padding-inline: var(--space-3);
  font-size: 0.9375rem;
  font-weight: 600;
}

@media (hover: hover) {
  .btn-tool:not(:disabled):hover,
  .move-chip:hover {
    background: var(--surface-2);
  }

  .menu-item:hover {
    background: var(--surface-2);
  }

  .group-chip:hover {
    border-color: var(--border-strong);
  }
}

/* ==========================================================================
   Toast (top-anchored so it never covers total or action row)
   ========================================================================== */

#toast {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 12px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  max-width: calc(100vw - 32px);
  padding: 10px var(--space-4);
  border-radius: var(--radius-pill);
  background: var(--toast-bg);
  color: var(--toast-ink);
  font-size: 0.9375rem;
  font-weight: 500;
  line-height: 1.4;
  text-align: center;
  box-shadow: 0 6px 20px rgba(27, 25, 22, 0.22);
  pointer-events: none;
  word-break: normal;
  overflow-wrap: break-word;
}

/* ==========================================================================
   Desktop & larger viewports (>= 640px)
   ========================================================================== */

@media (min-width: 640px) {
  body[data-state="empty"] .error-panel {
    max-width: 480px;
    margin: var(--space-6) auto 0;
  }

  .stage-wrap {
    background: transparent;
    padding: var(--space-6) var(--space-6) 0;
    display: flex;
    justify-content: center;
  }

  #stage {
    max-width: 880px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
  }

  .photo-meta {
    padding: var(--space-3) var(--space-6) var(--space-6);
  }

  .dock {
    width: calc(100% - 32px);
    max-width: 560px;
    margin: 0 auto var(--space-4);
    bottom: var(--space-4);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: 0 8px 28px rgba(27, 25, 22, 0.09);
    padding-bottom: var(--space-3);
  }

  body[data-state="empty"] .dock {
    width: 100%;
    max-width: none;
    margin: 0;
    border: 0;
    border-radius: 0;
  }

  .btn-tool {
    flex-direction: row;
    gap: 6px;
    padding: 0 var(--space-3);
    font-size: 0.875rem;
  }
}

/* ==========================================================================
   Reduced motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }

  .btn:not(:disabled):active,
  .group-chip:active,
  .move-chip:active {
    transform: none;
  }

  .progress-bar:not([value]),
  .progress-bar:indeterminate {
    animation: none;
    background-image: repeating-linear-gradient(
      -45deg,
      var(--accent) 0 12px,
      var(--accent-soft) 12px 24px
    );
    background-size: auto;
  }
}
