:root {
  color-scheme: light;
  --bg: #f7f5f1;
  --panel: #ffffff;
  --panel-soft: #fbfaf7;
  --ink: #181512;
  --muted: #716860;
  --line: #ded8cf;
  --line-strong: #cfc5b9;
  --accent: #9a5937;
  --accent-dark: #784027;
  --accent-soft: #f0e5dd;
  --shadow: 0 16px 45px rgba(68, 50, 34, 0.08);
  --left-pane: 30%;
  --top-pane: 42%;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", sans-serif;
  overflow: hidden;
}

.app {
  width: calc(100vw - 24px);
  height: calc(100vh - 24px);
  margin: 12px auto;
  display: grid;
  grid-template-columns: minmax(340px, var(--left-pane)) 8px minmax(0, 1fr);
  grid-template-rows: minmax(260px, var(--top-pane)) 8px minmax(360px, 1fr);
  gap: 8px;
  align-items: stretch;
}

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

.input-panel {
  grid-column: 1;
  grid-row: 1;
  align-self: start;
  height: 100%;
  overflow: auto;
}

.preview-panel {
  grid-column: 1 / -1;
  grid-row: 3;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.splitter {
  min-width: 0;
  min-height: 0;
  border-radius: 999px;
  background: transparent;
  position: relative;
  z-index: 5;
}

.splitter::after {
  content: "";
  position: absolute;
  border-radius: 999px;
  background: #b9b0a5;
  opacity: 0.7;
  transition: background 0.15s ease, opacity 0.15s ease;
}

.splitter:hover::after,
.splitter.dragging::after {
  background: var(--accent);
  opacity: 1;
}

.vertical-splitter {
  grid-column: 2;
  grid-row: 1;
  cursor: col-resize;
}

.vertical-splitter::after {
  top: 42%;
  bottom: 42%;
  left: 2px;
  right: 2px;
}

.horizontal-splitter {
  grid-column: 1 / -1;
  grid-row: 2;
  cursor: row-resize;
}

.horizontal-splitter::after {
  left: 42%;
  right: 42%;
  top: 2px;
  bottom: 2px;
}

.title-row,
.result-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
}

.preview-note {
  color: #b3261e;
  font-size: 13px;
  font-weight: 800;
  line-height: 1.35;
  margin-left: auto;
}

.placement-note {
  color: #b3261e;
  font-size: 13px;
  font-weight: 800;
  line-height: 1.35;
  margin-left: auto;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 20px;
  line-height: 1.35;
  letter-spacing: 0;
}

h2 {
  font-size: 20px;
}

h3 {
  margin: 18px 0 8px;
  font-size: 14px;
}

p {
  margin-top: 0;
  color: var(--muted);
  line-height: 1.65;
  font-size: 13px;
}

.field {
  display: grid;
  gap: 8px;
  margin-top: 8px;
  font-size: 13px;
  font-weight: 700;
}

textarea,
input,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}

textarea {
  min-height: 184px;
  resize: vertical;
  padding: 12px;
  line-height: 1.6;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 13px;
}

input,
select {
  height: 42px;
  padding: 0 10px;
}

input[type="file"] {
  height: auto;
  min-height: 36px;
  padding: 6px;
}

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

.compact {
  margin-top: 8px;
}

.wide {
  grid-column: 1 / -1;
}

.actions {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.options-panel {
  margin-top: 10px;
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.options-panel summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

button {
  border: 0;
  border-radius: 8px;
  height: 42px;
  padding: 0 16px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

button:hover {
  background: var(--accent-dark);
}

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

.ghost-button {
  background: var(--accent-soft);
  color: var(--ink);
}

.ghost-button:hover {
  background: #e2d4c5;
}

.preview-list {
  display: flex;
  gap: 10px;
  margin-top: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0 4px 8px 0;
  min-height: 0;
  flex: 1;
  align-items: stretch;
}

.preview-card {
  flex: 0 0 150px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  padding: 6px;
  position: relative;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto auto auto;
  gap: 5px;
  min-height: 0;
}

.preview-card.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(154, 89, 55, 0.18);
}

.preview-card.needs-break {
  border-color: #d3392f;
}

.preview-canvas-wrap {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: grid;
  place-items: center;
  background:
    linear-gradient(45deg, #f1f1f1 25%, transparent 25%),
    linear-gradient(-45deg, #f1f1f1 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #f1f1f1 75%),
    linear-gradient(-45deg, transparent 75%, #f1f1f1 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  border-radius: 8px;
  overflow: hidden;
  padding: 5px;
}

canvas {
  max-width: 100%;
  max-height: 100%;
  height: auto;
}

.preview-image {
  display: block;
  width: auto;
  height: auto;
  max-width: none;
  max-height: none;
}

.meta {
  font-size: 10px;
  color: var(--muted);
  line-height: 1.55;
  overflow-wrap: anywhere;
}

.warning {
  position: absolute;
  top: 7px;
  left: 7px;
  z-index: 1;
  padding: 2px 5px;
  border-radius: 999px;
  background: #d3392f;
  color: #fff;
  box-shadow: 0 2px 8px rgba(100, 30, 20, 0.16);
  pointer-events: none;
  min-height: 0;
  font-size: 9px;
  line-height: 1.25;
  font-weight: 800;
}

.warning:empty {
  display: none;
}

.preview-note,
.placement-note {
  color: #b3261e;
}

.card-details {
  font-size: 10px;
  color: var(--muted);
}

.card-details summary {
  cursor: pointer;
  font-weight: 700;
  color: var(--muted);
}

.detail-tools {
  margin-top: 5px;
}

.mini-button {
  width: 100%;
  height: 24px;
  padding: 0 6px;
  border-radius: 999px;
  font-size: 10px;
  background: var(--accent-soft);
  color: var(--ink);
}

.card-actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(3, 30px);
  grid-auto-flow: column;
  gap: 4px;
}

.card-actions button {
  width: 100%;
  height: 30px;
  padding: 2px 0;
  border-radius: 16px;
  font-size: 10px;
  line-height: 1;
  writing-mode: vertical-rl;
  text-orientation: upright;
  letter-spacing: 0;
}

.footer-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 5px;
}

.source-button,
.save-button {
  width: 100%;
  height: 31px;
  border-radius: 16px;
  padding: 0 4px;
  font-size: 11px;
}

.source-button {
  background: var(--accent-soft);
  color: var(--ink);
}

.source-button:hover {
  background: #e2d4c5;
}

.save-button {
  background: #000;
  color: #fff;
}

.save-button:hover {
  background: #2b2b2b;
}

.placement-panel {
  grid-column: 3;
  grid-row: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.placement-layout {
  display: grid;
  grid-template-columns: 74px minmax(0, 1fr) minmax(198px, 242px);
  grid-template-rows: minmax(0, 1fr);
  gap: 8px;
  margin-top: 6px;
  min-height: 0;
  flex: 1;
}

.placement-side {
  border-right: 1px solid var(--line);
  padding-right: 20px;
  min-width: 0;
}

.tool-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  min-width: 0;
}

.tool-row button {
  flex: 1;
  min-width: 0;
}

.scene-list,
.box-shelf {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: none;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 2px 4px 2px 0;
}

.scene-item,
.box-item {
  flex: 0 0 auto;
  width: 62px;
  min-height: 45px;
  height: auto;
  border: 1px solid var(--line);
  background: var(--panel-soft);
  color: var(--ink);
  display: grid;
  grid-template-columns: 1fr;
  align-items: center;
  gap: 0;
  padding: 4px;
  text-align: left;
  border-radius: 10px;
}

.box-item {
  grid-template-columns: 44px 1fr;
  grid-template-rows: auto auto;
}

.scene-item:hover,
.box-item:hover {
  background: #fff;
  border-color: var(--line-strong);
}

.scene-item.active,
.box-item.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(141, 78, 49, 0.15);
}

.scene-thumb,
.box-thumb {
  width: 52px;
  height: 37px;
  object-fit: cover;
  background: #eee;
  border-radius: 6px;
}

.box-thumb {
  object-fit: contain;
}

.box-place-actions {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 5px;
}

.box-place-actions button {
  height: 26px;
  padding: 0 4px;
  font-size: 11px;
}

.item-name {
  display: none;
  min-width: 0;
  font-size: 12px;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.placement-toolbar {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  grid-template-rows: auto auto auto;
  align-content: start;
  align-items: stretch;
  padding: 7px;
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: 12px;
  grid-column: 3;
  grid-row: 1;
  overflow: auto;
}

.placement-toolbar > .tool-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  margin-top: 0;
}

.placement-toolbar > .tool-row:last-child {
  grid-template-columns: 1fr;
  align-self: center;
}

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

.preset-grid button,
.placement-toolbar button {
  height: 30px;
  padding: 0 4px;
  white-space: nowrap;
  font-size: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.placement-toolbar .export-row button {
  width: 100%;
  height: 34px;
  border-radius: 8px;
  font-size: 11px;
  text-overflow: clip;
}

.placement-toolbar .export-row button.active {
  background: #2f2a25;
}

.placement-toolbar .file-control {
  margin-bottom: 0;
}

.stage-wrap {
  grid-column: 2;
  grid-row: 1;
  margin-top: 0;
  border: 1px solid var(--line);
  background: #f0ece5;
  overflow: hidden;
  max-height: none;
  min-height: 0;
  border-radius: 12px;
  padding: 10px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

#stageCanvas {
  display: block;
  width: auto;
  height: auto;
  max-width: none;
  max-height: none;
  aspect-ratio: 16 / 9;
  background: #fff;
  border-radius: 6px;
}

#placementStatus {
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-control {
  display: grid;
  gap: 6px;
  font-size: 12px;
  font-weight: 700;
}

.file-control input {
  width: 100%;
}

.scene-strip {
  grid-column: 1;
  grid-row: 1;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  align-items: stretch;
  gap: 6px;
  min-width: 0;
  min-height: 0;
}

.box-strip-head {
  display: none;
}

.box-shelf {
  display: none;
}

@media (max-width: 1520px) {
  .app {
    grid-template-columns: minmax(340px, var(--left-pane)) 8px minmax(0, 1fr);
  }

  .placement-toolbar .tool-row {
    margin-top: 0;
  }

}

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

  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    height: auto;
  }

  .splitter {
    display: none;
  }

  .input-panel {
    position: static;
  }

  .input-panel,
  .preview-panel,
  .placement-panel {
    grid-column: 1;
    grid-row: auto;
  }

  .preview-panel {
    height: 720px;
  }

  .placement-layout,
  .placement-toolbar {
    grid-template-columns: 1fr;
  }

  .placement-toolbar,
  .stage-wrap,
  .scene-strip {
    grid-column: 1;
    grid-row: auto;
  }

  .placement-side {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding-right: 0;
    padding-bottom: 16px;
  }
}
