:root {
  color-scheme: light;
  --bg: #eef2f6;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --border: #cbd5e1;
  --text: #172033;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-soft: #dbeafe;
  --primary-soft-text: #075985;
  --danger: #dc2626;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.08);
  --drop-border: #94a3b8;
  --progress-bg: #e2e8f0;
  --checker-base: #ffffff;
  --checker-mark: #d9e2ec;
  --toast-border: #16a34a;
  --toast-bg: #dcfce7;
  --toast-text: #14532d;
  --radius: 6px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #111827;
    --surface: #1f2937;
    --surface-soft: #172033;
    --border: #40506a;
    --text: #eef2ff;
    --muted: #aab7cc;
    --primary: #60a5fa;
    --primary-soft: #1d3f68;
    --primary-soft-text: #dbeafe;
    --danger: #f87171;
    --shadow: 0 2px 14px rgba(0, 0, 0, 0.35);
    --drop-border: #64748b;
    --progress-bg: #334155;
    --checker-base: #1f2937;
    --checker-mark: #2f3b4d;
    --toast-border: #22c55e;
    --toast-bg: #12351f;
    --toast-text: #bbf7d0;
  }
}

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

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}

button,
input,
select {
  font: inherit;
}

button,
select {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.app {
  width: min(1560px, calc(100% - 24px));
  margin: 0 auto;
  padding: 22px 0 32px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(24px, 3vw, 34px);
}

.subtitle {
  margin: 6px 0 0;
  color: var(--muted);
  font-weight: 700;
}

.layout {
  display: grid;
  grid-template-columns: minmax(310px, 380px) minmax(0, 1fr) auto;
  gap: 16px;
  align-items: start;
}

.sidebar,
.workspace,
.history-sidebar {
  display: grid;
  gap: 14px;
  min-width: 0;
}

.panel,
.canvas-viewport {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel {
  display: grid;
  gap: 12px;
  padding: 14px;
}

details.panel {
  display: block;
}

.history-drawer summary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  font-weight: 900;
  list-style: none;
}

.history-drawer summary::-webkit-details-marker {
  display: none;
}

.history-drawer summary::before {
  content: "▶";
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
  color: var(--primary);
  font-size: 11px;
}

.history-drawer[open] summary::before {
  content: "▼";
}

.history-sidebar {
  position: sticky;
  top: 16px;
  align-self: start;
}

.history-panel {
  width: min(320px, 22vw);
  max-height: calc(100vh - 44px);
  overflow: auto;
  transition: width 0.18s ease;
}

.history-panel:not([open]) {
  width: 48px;
  min-height: 110px;
  padding: 10px;
  overflow: hidden;
}

.history-panel:not([open]) summary {
  writing-mode: vertical-rl;
  min-height: 84px;
  justify-content: flex-start;
  white-space: nowrap;
}

.history-panel:not([open]) summary::before {
  content: "◀";
}

.history-body {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-title span,
.status-text,
.note-panel p {
  color: var(--muted);
}

.panel-title button {
  min-height: 30px;
  padding: 0 10px;
  font-weight: 800;
}

.drop-zone {
  display: grid;
  place-items: center;
  gap: 10px;
  min-height: 190px;
  padding: 20px;
  border: 2px dashed var(--drop-border);
  border-radius: var(--radius);
  background: var(--surface-soft);
  text-align: center;
  outline: none;
}

.drop-zone span {
  max-width: 280px;
  color: var(--muted);
  line-height: 1.5;
}

.drop-zone.dragging,
.drop-zone:focus-visible {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.16);
}

.drop-zone button,
.primary-button {
  min-height: 40px;
  padding: 0 14px;
  border-color: var(--primary);
  background: var(--primary);
  color: #ffffff;
  font-weight: 800;
}

.progress-track {
  height: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--progress-bg);
}

.progress-track div {
  width: 0;
  height: 100%;
  background: var(--primary);
  transition: width 0.2s ease;
}

.tool-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.tool-help {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
}

.tool-grid button {
  min-height: 38px;
  padding: 0 10px;
  font-weight: 800;
}

.tool-grid button.active {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary-soft-text);
}

.slider-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px 10px;
  color: var(--muted);
  font-weight: 800;
}

.slider-row output {
  color: var(--text);
}

.slider-row input {
  grid-column: 1 / -1;
  width: 100%;
  accent-color: var(--primary);
}

.check-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-weight: 800;
}

.check-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
}

.field {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 800;
}

.field select {
  height: 36px;
  padding: 0 10px;
}

.action-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.action-row button {
  min-height: 34px;
  padding: 0 8px;
  font-weight: 800;
}

.history-list {
  display: grid;
  gap: 8px;
}

.history-item {
  display: grid;
  grid-template-columns: minmax(96px, 1fr) 64px;
  gap: 8px;
  align-items: stretch;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.history-thumb {
  display: grid;
  place-items: center;
  width: 100%;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

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

.history-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  min-width: 0;
}

.history-actions button {
  min-height: 30px;
  padding: 0 6px;
  font-weight: 800;
}

.empty-note {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.canvas-viewport {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: min(760px, calc(100vh - 190px));
  min-height: 560px;
  overflow: hidden;
}

.view-switch {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 5;
  display: inline-flex;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.view-switch button {
  min-height: 34px;
  padding: 0 12px;
  border: 0;
  border-right: 1px solid var(--border);
  border-radius: 0;
  font-weight: 800;
}

.view-switch button:last-child {
  border-right: 0;
}

.view-switch button.active {
  background: var(--primary);
  color: #ffffff;
}

.canvas-actions,
.canvas-zoom,
.canvas-rotate {
  position: absolute;
  z-index: 5;
  display: inline-flex;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.canvas-output {
  display: inline-flex;
  flex: 0 0 auto;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.canvas-actions {
  left: 12px;
  bottom: 12px;
}

.canvas-zoom {
  right: 12px;
  bottom: 12px;
}

.canvas-rotate {
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
}

.canvas-output select,
.canvas-output button,
.canvas-actions button,
.canvas-zoom button,
.canvas-rotate button,
.rotate-angle {
  min-height: 34px;
  padding: 0 12px;
  border: 0;
  border-right: 1px solid var(--border);
  border-radius: 0;
  font-weight: 800;
}

.canvas-output select {
  min-width: 104px;
  padding-right: 28px;
}

.canvas-output button {
  background: var(--primary);
  color: #ffffff;
}

.rotate-angle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 54px;
  color: var(--muted);
}

.canvas-actions button:last-child,
.canvas-zoom button:last-child,
.canvas-rotate button:last-child {
  border-right: 0;
}

.toast {
  position: fixed;
  right: 12px;
  bottom: 12px;
  z-index: 20;
  max-width: min(360px, calc(100% - 24px));
  padding: 10px 14px;
  border: 1px solid var(--toast-border);
  border-radius: var(--radius);
  background: var(--toast-bg);
  color: var(--toast-text);
  font-weight: 800;
  box-shadow: var(--shadow);
}

.checker {
  background-color: var(--checker-base);
  background-image:
    linear-gradient(45deg, var(--checker-mark) 25%, transparent 25%),
    linear-gradient(-45deg, var(--checker-mark) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--checker-mark) 75%),
    linear-gradient(-45deg, transparent 75%, var(--checker-mark) 75%);
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
  background-size: 24px 24px;
}

.canvas-viewport.white {
  background: #ffffff;
}

.canvas-viewport.black {
  background: #111827;
}

.canvas-viewport.gray {
  background: #8f99a8;
}

.canvas-stack {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
  transform-origin: 0 0;
}

.canvas-stack canvas {
  position: absolute;
  inset: 0;
  display: block;
}

#boundaryCanvas {
  pointer-events: none;
}

.empty-state {
  display: grid;
  gap: 6px;
  color: var(--muted);
  text-align: center;
}

.empty-state strong {
  color: var(--text);
  font-size: 20px;
}

@media (max-width: 980px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .history-sidebar {
    position: static;
  }

  .history-panel,
  .history-panel:not([open]) {
    width: 100%;
    max-height: none;
  }

  .history-panel:not([open]) {
    min-height: 0;
    padding: 14px;
  }

  .history-panel:not([open]) summary {
    writing-mode: horizontal-tb;
    min-height: 0;
  }

  .history-panel:not([open]) summary::before {
    content: "▶";
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .canvas-viewport {
    min-height: 560px;
  }

  .view-switch,
  .canvas-actions,
  .canvas-zoom,
  .canvas-rotate {
    max-width: calc(100% - 24px);
  }

  .history-item {
    grid-template-columns: 1fr;
  }

  .history-thumb,
  .history-actions {
    grid-column: auto;
    grid-row: auto;
  }

  .history-actions {
    min-width: 0;
  }

  .view-switch button,
  .canvas-output select,
  .canvas-output button,
  .canvas-actions button,
  .canvas-zoom button,
  .canvas-rotate button,
  .rotate-angle {
    padding: 0 9px;
    font-size: 12px;
  }
}
