:root {
  color-scheme: light;
  --ink: #24211c;
  --muted: #69625a;
  --line: #d8d1c4;
  --paper: #f6f0e5;
  --panel: #fffaf1;
  --panel-strong: #fff5e3;
  --accent: #1c6d72;
  --accent-strong: #155157;
  --accent-soft: #d9eeee;
  --danger: #a84232;
  --shadow: 0 12px 30px rgba(39, 33, 24, 0.11);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  font-family: "Segoe UI", "Yu Gothic UI", "Meiryo", sans-serif;
  color: var(--ink);
  background: var(--paper);
}

button,
input,
select {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  background: #fffdf8;
  color: var(--ink);
  border-radius: 8px;
  min-height: 36px;
  padding: 0 12px;
  cursor: pointer;
}

button:hover:not(:disabled) {
  border-color: var(--accent);
}

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

h1,
h2,
p {
  margin: 0;
}

h2 {
  font-size: 15px;
  letter-spacing: 0;
}

h3 {
  margin: 0;
  letter-spacing: 0;
}

.app-shell {
  height: 100dvh;
  min-height: 560px;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 10px;
  padding: 10px;
}

.topbar {
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.brand h1 {
  font-size: 22px;
  line-height: 1.15;
  letter-spacing: 0;
}

.brand p {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.top-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.top-actions button {
  min-width: 72px;
}

.primary-button {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(190px, 250px) minmax(360px, 1fr) minmax(280px, 340px);
  gap: 10px;
}

.panel {
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.sidebar,
.controls {
  overflow: auto;
  padding: 12px;
}

.preview-panel {
  min-width: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.panel-head span,
.hint {
  color: var(--muted);
  font-size: 12px;
}

.drop-zone {
  min-height: 88px;
  display: grid;
  place-items: center;
  gap: 3px;
  margin-bottom: 10px;
  border: 2px dashed #bdb4a5;
  border-radius: 8px;
  background: rgba(255, 250, 241, 0.7);
  color: var(--muted);
  text-align: center;
}

.drop-zone.dragover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-strong);
}

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

.image-item {
  width: 100%;
  display: grid;
  grid-template-columns: 48px 1fr;
  align-items: center;
  gap: 8px;
  padding: 7px;
  text-align: left;
  border-radius: 8px;
}

.image-item.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.image-item img {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 6px;
  background: #eee;
}

.image-name {
  display: block;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-weight: 700;
}

.image-status {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.preview-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-strong);
}

.segmented {
  display: flex;
  gap: 3px;
  padding: 3px;
  border-radius: 8px;
  background: #eee3d3;
}

.mode-button {
  min-height: 32px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  padding: 0 10px;
}

.mode-button.active,
.active-preset {
  background: #fffdf8;
  color: var(--accent-strong);
  box-shadow: 0 1px 4px rgba(39, 33, 24, 0.14);
}

.toolbar-field,
.toggle,
.field,
.range-field {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.toolbar-field {
  grid-template-columns: auto 128px;
  align-items: center;
}

.toolbar-field output {
  color: var(--ink);
  font-weight: 700;
}

.zoom-buttons {
  display: flex;
  gap: 5px;
}

.zoom-buttons button {
  min-height: 32px;
  padding: 0 9px;
}

.view-hint {
  flex: 1 1 260px;
  min-width: 220px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  color: var(--muted);
  font-size: 12px;
}

.toggle {
  grid-auto-flow: column;
  grid-auto-columns: max-content;
  align-items: center;
}

.toggle.wide {
  justify-content: start;
  margin-bottom: 8px;
}

.canvas-stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  background-color: var(--preview-bg, #fdfaf4);
}

.canvas-stage.checker {
  background-image:
    linear-gradient(45deg, var(--checker-a, #d7d7d7) 25%, transparent 25%),
    linear-gradient(-45deg, var(--checker-a, #d7d7d7) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--checker-a, #d7d7d7) 75%),
    linear-gradient(-45deg, transparent 75%, var(--checker-a, #d7d7d7) 75%);
  background-color: var(--checker-b, #fdfaf4);
  background-size: 28px 28px;
  background-position: 0 0, 0 14px, 14px -14px, -14px 0;
}

.canvas-layer {
  position: absolute;
  left: var(--layer-left, 50%);
  top: var(--layer-top, 50%);
  width: var(--preview-width, 1px);
  height: var(--preview-height, 1px);
  transform: translate(-50%, -50%);
}

canvas {
  position: absolute;
  inset: 0 auto auto 0;
  display: block;
  max-width: none;
  image-rendering: auto;
}

#maskCanvas {
  display: none;
}

.compare-handle {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 18px;
  margin-left: -9px;
  background: linear-gradient(to right, transparent 8px, var(--accent) 8px, var(--accent) 10px, transparent 10px);
  box-shadow: 0 0 14px rgba(0,0,0,0.14);
  cursor: ew-resize;
  pointer-events: auto;
  touch-action: none;
}

.compare-handle::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 24px;
  height: 24px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--accent);
  transform: translate(-50%, -50%);
}

.side-layer {
  display: grid;
  grid-template-columns: repeat(2, minmax(260px, 1fr));
  gap: 14px;
  min-width: max-content;
  min-height: 100%;
  padding: 16px;
}

.side-frame {
  position: relative;
  min-width: 260px;
  min-height: 260px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background-color: transparent;
  overflow: auto;
}

.side-label {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 6px 8px;
  background: rgba(255,250,241,0.95);
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  font-weight: 700;
}

.side-frame canvas {
  position: relative;
  inset: auto;
  margin: 14px;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 8px;
  padding: 20px;
  color: var(--muted);
  text-align: center;
}

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

.controls {
  display: grid;
  align-content: start;
  gap: 14px;
}

.inline-help {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  padding: 10px;
}

.inline-help summary {
  cursor: pointer;
  font-weight: 700;
}

.inline-help ol,
.inline-help p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.inline-help ol {
  padding-left: 1.3em;
}

.inline-help p {
  padding-left: 0;
}

.control-block {
  display: grid;
  gap: 9px;
}

.subhead {
  color: var(--ink);
  font-size: 13px;
}

.section-help,
.mini-help p {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
}

.mini-help {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  padding: 7px 9px;
}

.mini-help summary {
  cursor: pointer;
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 700;
}

.mini-help p {
  margin-top: 6px;
}

.preset-grid,
.scope-row,
.brush-power-row,
.brush-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.scope-row .toggle {
  grid-template-columns: auto minmax(0, 1fr);
  grid-auto-flow: initial;
  grid-auto-columns: initial;
  align-items: start;
  min-width: 0;
  padding: 7px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
}

.option-text {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.option-text strong {
  color: var(--ink);
  font-size: 12px;
  line-height: 1.25;
}

.option-text small {
  color: var(--muted);
  font-size: 11px;
  line-height: 1.25;
}

.brush-power-row {
  grid-template-columns: repeat(2, 1fr);
}

.brush-row {
  grid-template-columns: repeat(2, 1fr);
}

.swatches {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
}

.swatch {
  min-height: 34px;
  border-radius: 8px;
  border: 1px solid var(--line);
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.7);
}

.swatch.active {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.color-row {
  display: grid;
  grid-template-columns: 48px 1fr 1fr;
  gap: 6px;
}

input[type="color"] {
  width: 48px;
  min-height: 36px;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.field input[type="text"] {
  width: 100%;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  padding: 0 10px;
  color: var(--ink);
}

.range-field output {
  color: var(--ink);
  font-weight: 700;
}

.wide-button {
  width: 100%;
}

.picking .canvas-stage {
  cursor: crosshair;
}

.brushing .canvas-stage {
  cursor: crosshair;
}

.custom-tooltip {
  position: fixed;
  z-index: 1000;
  max-width: min(320px, calc(100vw - 24px));
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #24211c;
  color: #fffaf1;
  font-size: 12px;
  line-height: 1.55;
  box-shadow: 0 10px 24px rgba(0,0,0,0.2);
  pointer-events: none;
}

.brush-preview {
  position: fixed;
  z-index: 999;
  border: 2px solid var(--accent);
  border-radius: 50%;
  background: rgba(28, 109, 114, 0.12);
  transform: translate(-50%, -50%);
  pointer-events: none;
  box-shadow: 0 0 0 1px rgba(255,255,255,0.85);
}

.brush-preview.erase {
  border-color: var(--danger);
  background: rgba(168, 66, 50, 0.13);
}

@media (max-width: 1180px) {
  body {
    overflow: auto;
  }

  .app-shell {
    height: auto;
    min-height: 100dvh;
  }

  .workspace {
    grid-template-columns: minmax(190px, 250px) minmax(360px, 1fr);
  }

  .controls {
    grid-column: 1 / -1;
    grid-template-columns: repeat(2, minmax(260px, 1fr));
    max-height: none;
  }

  .inline-help {
    grid-column: 1 / -1;
  }

  .canvas-stage {
    min-height: 520px;
  }
}

@media (max-width: 760px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions {
    justify-content: start;
  }

  .workspace,
  .controls {
    grid-template-columns: 1fr;
  }

  .sidebar {
    max-height: 320px;
  }

  .preview-panel {
    min-height: 520px;
  }

  .canvas-stage {
    min-height: 430px;
  }
}
