:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --text: #2b2620;
  --muted: #7a7064;
  --border: #e2dccf;
  --accent: #d9822b;
  --accent-text: #ffffff;
  --danger: #c0392b;
  --selected: #fbe9d4;
  --checker-a: #e6e6e6;
  --checker-b: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1b18;
    --surface: #2a2723;
    --text: #eee8de;
    --muted: #a59b8d;
    --border: #3d3832;
    --accent: #e8964a;
    --accent-text: #1d1b18;
    --danger: #e57565;
    --selected: #4a3a28;
    --checker-a: #3a3a3a;
    --checker-b: #2c2c2c;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 16px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.app-header h1 { margin: 0; font-size: 20px; }
.privacy { margin: 0; color: var(--muted); font-size: 13px; }

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

.stage { position: sticky; top: 16px; min-width: 0; }

.canvas-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
  max-height: calc(100vh - 140px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow: hidden;
}

#view {
  display: block;
  max-width: 100%;
  max-height: calc(100vh - 166px);
  background-color: var(--checker-b);
  background-image:
    linear-gradient(45deg, var(--checker-a) 25%, transparent 25%),
    linear-gradient(-45deg, var(--checker-a) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--checker-a) 75%),
    linear-gradient(-45deg, transparent 75%, var(--checker-a) 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  touch-action: none;
  cursor: default;
}
#view.picking { cursor: crosshair; }

.dropzone {
  position: absolute;
  inset: 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  border: 2px dashed var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  padding: 16px;
}
.dropzone.hidden { display: none; }
.canvas-wrap.dragover .dropzone,
.canvas-wrap.dragover { border-color: var(--accent); }
.dropzone .hint { font-size: 12px; }

.stage-hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; }

.panel { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
}
.card h2 { margin: 0 0 8px; font-size: 15px; }

details { border-top: 1px solid var(--border); padding: 6px 0; }
details:first-of-type { border-top: none; }
summary { cursor: pointer; font-weight: 600; padding: 4px 0; }

.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 6px 0; }

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 12px;
  font: inherit;
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.active { outline: 2px solid var(--accent); }

.field { display: flex; flex-direction: column; gap: 2px; margin: 6px 0; }
.field > span { color: var(--muted); font-size: 12px; }
.field.small { margin: 0; }
.field.small input { width: 84px; }
input[type="number"], select {
  font: inherit;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}
input[type="color"] { width: 40px; height: 30px; padding: 0; border: 1px solid var(--border); border-radius: 6px; background: none; }

.range { display: grid; grid-template-columns: 112px minmax(0, 1fr) 44px; align-items: center; gap: 8px; margin: 4px 0; }
.range span { font-size: 12px; color: var(--muted); }
.range output { font-size: 12px; text-align: right; font-variant-numeric: tabular-nums; }
.range input { width: 100%; accent-color: var(--accent); }

.check { display: flex; align-items: center; gap: 6px; margin: 6px 0; }
.note { color: var(--muted); font-size: 12px; margin: 4px 0; }

.layer-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.layer-list li {
  display: grid;
  grid-template-columns: 28px 44px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}
.layer-list li.selected { background: var(--selected); border-color: var(--accent); }
.layer-list .thumb {
  width: 44px; height: 33px; object-fit: contain;
  background-color: var(--checker-b);
  background-image: linear-gradient(45deg, var(--checker-a) 25%, transparent 25%, transparent 75%, var(--checker-a) 75%),
                    linear-gradient(45deg, var(--checker-a) 25%, transparent 25%, transparent 75%, var(--checker-a) 75%);
  background-size: 8px 8px;
  background-position: 0 0, 4px 4px;
  border-radius: 3px;
}
.layer-list .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.layer-list .name small { color: var(--muted); display: block; font-size: 11px; }
.layer-list .ops { display: flex; gap: 2px; }
.icon-btn {
  appearance: none; border: none; background: none; color: var(--text);
  width: 28px; height: 28px; border-radius: 4px; cursor: pointer; font-size: 14px;
}
.icon-btn:hover:not(:disabled) { background: var(--bg); }
.icon-btn:disabled { opacity: .3; cursor: default; }
.icon-btn.off { opacity: .4; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 8px 16px; border-radius: 20px; font-size: 13px;
  opacity: 0; pointer-events: none; transition: opacity .2s;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; }

@media (max-width: 820px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .stage { position: static; }
  .canvas-wrap { max-height: 60vh; }
  #view { max-height: calc(60vh - 26px); }
}
