:root {
  color-scheme: light;
  --ink: #171717;
  --muted: #6b6f76;
  --line: #d9dde3;
  --panel: #f7f8fa;
  --paper: #ffffff;
  --accent: #0f766e;
  --accent-dark: #0b5d57;
  --soft: #e9f5f3;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  color: var(--ink);
  background: #eef1f4;
}

.app {
  display: grid;
  grid-template-columns: 300px minmax(520px, 1fr) 320px;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 100vh;
  max-height: 100vh;
}

.topbar {
  grid-column: 1 / -1;
  grid-row: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  background: var(--panel);
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  background: var(--panel);
  padding: 12px;
  overflow-y: auto;
  overflow-x: hidden;
}

.panel-left {
  grid-column: 1;
  grid-row: 2;
  border-right: 1px solid var(--line);
}

.panel-right {
  grid-column: 3;
  grid-row: 2;
  border-left: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  min-width: 190px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 19px;
  line-height: 1.25;
}

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

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

.top-actions button {
  width: auto;
  margin: 0;
  white-space: nowrap;
}

.group {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  margin-bottom: 0;
  min-width: 0;
  max-height: none;
  overflow-y: auto;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
}

.group h2 {
  display: none;
  font-size: 14px;
  margin-bottom: 8px;
}

summary {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 38px;
  padding: 0 10px;
  border-bottom: 1px solid var(--line);
  background: #fff;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  list-style: none;
}

summary::-webkit-details-marker {
  display: none;
}

summary::after {
  content: "⌄";
  color: var(--muted);
  font-size: 14px;
}

details:not([open]) {
  overflow: hidden;
}

details:not([open]) summary {
  border-bottom: 0;
}

details:not([open]) summary::after {
  content: "›";
}

.group > :not(summary) {
  margin-left: 10px;
  margin-right: 10px;
}

.group > :last-child {
  margin-bottom: 10px;
}

.group-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 0;
}

.group-head h2 {
  margin: 0;
}

.group-head span {
  color: var(--muted);
  font-size: 13px;
}

.upload {
  display: grid;
  gap: 5px;
  border: 1px dashed #aab2bd;
  border-radius: 8px;
  padding: 12px;
  cursor: pointer;
  background: #fbfcfd;
  margin-bottom: 10px;
}

.upload.dragging,
.stage.dragging {
  border-color: var(--accent);
  background: #e9f5f3;
  box-shadow: inset 0 0 0 2px rgba(15, 118, 110, 0.18);
}

.compact-upload {
  padding: 9px 10px;
  margin-top: 10px;
  margin-bottom: 8px;
}

.upload input {
  display: none;
}

.upload span {
  font-weight: 700;
}

.upload small {
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.small-input {
  width: 100%;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 9px;
  margin-bottom: 8px;
  font: inherit;
  font-size: 13px;
  background: #fff;
}

.note {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
}

.template-list {
  display: grid;
  gap: 6px;
  max-height: 260px;
  overflow: auto;
}

.template-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 30px 30px;
  align-items: center;
  gap: 5px;
  width: 100%;
  height: 48px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  text-align: left;
}

.template-item.active {
  border-color: var(--accent);
  background: var(--soft);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.template-main {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  min-width: 0;
  height: 38px;
  padding: 0;
  background: transparent;
  text-align: left;
}

.thumb {
  width: 38px;
  height: 38px;
  border: 1px solid #cfd5de;
  border-radius: 5px;
  background-color: #f8fafc;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.template-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.template-action {
  height: 30px;
  padding: 0;
  border-radius: 6px;
  font-size: 12px;
}

.template-action {
  background: #eef1f4;
  color: #343942;
}

.template-action.delete {
  background: #f3eeee;
  color: #8a1f1f;
}

.field {
  display: grid;
  gap: 5px;
  margin-bottom: 8px;
}

.field span,
.check span {
  font-size: 13px;
  color: #343942;
}

input[type="text"],
input[type="number"],
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  height: 34px;
  padding: 0 10px;
  font: inherit;
  background: white;
}

input[type="range"] {
  width: 100%;
}

.two {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.mini-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
}

.mini {
  min-width: 0;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}

button {
  border: 0;
  border-radius: 7px;
  height: 34px;
  padding: 0 10px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.primary {
  width: 100%;
  margin-top: 8px;
  background: var(--accent);
  color: white;
}

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

.download-status {
  min-height: 20px;
  margin-top: 9px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.download-fallback {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.download-fallback[hidden] {
  display: none;
}

.download-fallback a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 34px;
  border-radius: 6px;
  background: #eef1f4;
  color: #14343a;
  font-weight: 700;
  text-decoration: none;
}

.download-fallback img {
  width: 100%;
  max-height: 180px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
}

.secondary {
  width: 100%;
  margin-top: 6px;
  background: var(--soft);
  color: #075b55;
}

.danger {
  width: 100%;
  margin-top: 6px;
  background: #f3eeee;
  color: #8a1f1f;
}

.icon-btn {
  width: 34px;
  flex: 0 0 auto;
  background: white;
  border: 1px solid var(--line);
  font-size: 20px;
}

.stage-wrap {
  grid-column: 2;
  grid-row: 2;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  min-height: 0;
}

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

.tool {
  background: #eef1f4;
  color: #2f343b;
}

.tool.active {
  background: #0f766e;
  color: white;
}

#hint {
  margin-left: 8px;
  color: var(--muted);
  font-size: 13px;
}

#currentTemplate {
  display: inline-flex;
  align-items: center;
  height: 30px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: #343942;
  font-size: 13px;
  font-weight: 700;
}

.stage {
  display: grid;
  place-items: center;
  padding: 18px;
  overflow: auto;
  border: 2px solid transparent;
  background-image: linear-gradient(45deg, #dde2e8 25%, transparent 25%),
    linear-gradient(-45deg, #dde2e8 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #dde2e8 75%),
    linear-gradient(-45deg, transparent 75%, #dde2e8 75%);
  background-size: 28px 28px;
  background-position: 0 0, 0 14px, 14px -14px, -14px 0;
}

canvas {
  width: min(78vh, 100%);
  max-width: 100%;
  height: auto;
  background: white;
  box-shadow: 0 16px 40px rgba(15, 23, 42, 0.18);
}

@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    max-height: none;
  }

  .topbar {
    grid-column: 1;
    grid-row: 1;
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .panel-left {
    grid-column: 1;
    grid-row: 2;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    max-height: 360px;
  }

  .stage-wrap {
    grid-column: 1;
    grid-row: 3;
    min-height: 620px;
  }

  .panel-right {
    grid-column: 1;
    grid-row: 4;
    border-left: 0;
    border-top: 1px solid var(--line);
  }

  canvas {
    width: min(86vw, 86vh);
  }

  .toolbar {
    flex-wrap: wrap;
  }
}

/* Compact workspace */
.app {
  grid-template-columns: 230px minmax(720px, 1fr) 240px;
}

.topbar {
  padding: 6px 10px;
  gap: 8px;
}

h1 {
  font-size: 16px;
}

.brand p {
  font-size: 11px;
  margin-top: 2px;
}

.top-actions {
  gap: 6px;
}

.panel {
  gap: 7px;
  padding: 8px;
}

.group {
  border-radius: 7px;
}

summary {
  min-height: 30px;
  padding: 0 8px;
  font-size: 13px;
}

.group > :not(summary) {
  margin-left: 8px;
  margin-right: 8px;
}

.group > :last-child {
  margin-bottom: 8px;
}

.compact-upload {
  padding: 7px 8px;
  margin-top: 7px;
  margin-bottom: 6px;
}

.small-input,
input[type="text"],
input[type="number"],
select {
  height: 28px;
  padding: 0 7px;
  font-size: 12px;
}

.note {
  font-size: 11px;
  line-height: 1.35;
}

.template-list {
  gap: 5px;
  max-height: 190px;
}

.template-item {
  grid-template-columns: minmax(0, 1fr) 25px 25px;
  height: 38px;
  padding: 3px;
}

.template-main {
  grid-template-columns: 30px minmax(0, 1fr);
  gap: 5px;
  height: 30px;
}

.thumb {
  width: 30px;
  height: 30px;
}

.template-action {
  height: 25px;
  font-size: 11px;
}

.field {
  gap: 3px;
  margin-bottom: 5px;
}

.field span,
.check span {
  font-size: 12px;
}

.mini-row {
  gap: 5px;
}

button {
  height: 28px;
  padding: 0 8px;
  border-radius: 6px;
  font-size: 12px;
}

.icon-btn {
  width: 28px;
}

.secondary,
.danger,
.primary {
  margin-top: 5px;
}

.toolbar {
  padding: 5px 8px;
  gap: 6px;
}

#currentTemplate {
  height: 24px;
  padding: 0 7px;
  font-size: 12px;
}

#hint {
  font-size: 11px;
}

.stage {
  padding: 6px;
}

canvas {
  width: min(92vh, 100%);
}
