:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1f2430;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #4e71dd;
  --accent-weak: rgba(78, 113, 221, 0.12);
  --accent-contrast: #ffffff;
  --danger: #d64545;
  --success: #2f9e63;
  --shadow: 0 10px 30px rgba(20, 28, 45, 0.08);
  --radius: 14px;
  --radius-sm: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --surface: #1c2027;
    --surface-2: #252a33;
    --text: #e6e8ec;
    --muted: #9aa3af;
    --border: #2f3540;
    --accent: #7d9bff;
    --accent-weak: rgba(125, 155, 255, 0.18);
    --accent-contrast: #0f1420;
    --danger: #ff7b7b;
    --success: #5cc98c;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  line-height: 1.6;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.app { width: min(1000px, 100% - 2rem); margin: 0 auto; padding: 1.5rem 0 3rem; }
.app-header { margin-bottom: 1.25rem; }
.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}
.back-link { text-decoration: none; font-size: 0.9rem; }
.back-link:hover { text-decoration: underline; }
.lang-label { display: inline-flex; align-items: center; gap: 0.4rem; }
select {
  min-height: 44px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.35rem 0.6rem;
}
h1 { margin: 0.25rem 0; font-size: clamp(1.5rem, 3vw, 2rem); line-height: 1.25; }
.subtitle { margin: 0; color: var(--muted); }
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}
h2 { display: flex; align-items: center; gap: 0.6rem; margin: 0; font-size: 1.05rem; }
.step {
  display: inline-grid;
  place-items: center;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 0.85rem;
  font-weight: 700;
}
.layout-row, .chip-row { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.chip {
  min-width: 44px;
  min-height: 44px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 999px;
  padding: 0.4rem 1rem;
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); }
.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}
.grid { display: grid; gap: 0.5rem; }
.cell {
  position: relative;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 2px dashed var(--border);
  background: var(--surface-2);
  cursor: pointer;
  outline: none;
}
.cell.empty { display: grid; place-items: center; color: var(--muted); }
.cell.empty:hover, .cell.empty:focus-visible {
  border-color: var(--accent);
  background: var(--accent-weak);
  color: var(--accent);
}
.cell .plus { font-size: clamp(1.5rem, 5vw, 2.5rem); line-height: 1; }
.cell.filled { border-style: solid; border-color: var(--border); cursor: default; }
.cell.selected { box-shadow: 0 0 0 3px var(--accent-weak); border-color: var(--accent); }
.cell-canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.cell.filled:hover .cell-canvas { cursor: grab; }
.cell.panning .cell-canvas { cursor: grabbing; }
.cell-actions, .cell-zoom {
  position: absolute;
  display: flex;
  gap: 0.25rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
}
.cell-actions { top: 0.4rem; right: 0.4rem; }
.cell-handle {
  position: absolute;
  top: 0.4rem;
  left: 0.4rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
  z-index: 2;
}
.cell:hover .cell-handle, .cell:focus-within .cell-handle {
  opacity: 1;
  pointer-events: auto;
}
.cell-zoom {
  bottom: 0.4rem;
  left: 50%;
  transform: translateX(-50%);
  align-items: center;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  border-radius: 999px;
  padding: 0.15rem 0.35rem;
  font-size: 0.75rem;
}
.cell:hover .cell-actions, .cell:focus-within .cell-actions,
.cell:hover .cell-zoom, .cell:focus-within .cell-zoom {
  opacity: 1;
  pointer-events: auto;
}
.icon-btn {
  border: 0;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  width: 1.75rem;
  height: 1.75rem;
  min-width: 44px;
  min-height: 44px;
  border-radius: 50%;
  cursor: pointer;
  display: grid;
  place-items: center;
  line-height: 1;
}
.icon-btn:hover { background: var(--accent); }
.handle { background: rgba(0, 0, 0, 0.65); cursor: grab; touch-action: none; }
.cell.dragging { opacity: 0.55; }
.cell.drop-target { outline: 3px solid var(--accent); outline-offset: -3px; }
.cell.loading::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}
.grid.file-drag { outline: 3px dashed var(--accent); outline-offset: 6px; }
.hint { margin: 0.75rem 0 0; color: var(--muted); font-size: 0.85rem; }
.image-count { color: var(--accent); font-weight: 600; }
.option-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem 1.5rem;
}
.option { display: flex; flex-direction: column; gap: 0.4rem; }
.option > label, .option > .label { font-size: 0.9rem; color: var(--muted); }
input[type="number"] {
  width: 100%;
  min-height: 44px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
}
.range-row { display: flex; align-items: center; gap: 0.75rem; }
input[type="range"] { flex: 1; accent-color: var(--accent); min-height: 44px; }
output {
  min-width: 3.5rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.segmented {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  width: fit-content;
}
.segmented label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 44px;
  padding: 0.4rem 0.75rem;
  cursor: pointer;
  background: var(--surface-2);
}
.segmented input { accent-color: var(--accent); }
.output-size { margin: 1rem 0 0; color: var(--muted); font-size: 0.9rem; }
.output-size.warn { color: var(--danger); }
.generate-actions { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  padding: 0.6rem 1.2rem;
  border: 1px solid transparent;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.btn-primary { background: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover { filter: brightness(1.06); }
.btn-primary:disabled { opacity: 0.45; cursor: not-allowed; filter: none; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }
.preview { margin: 1.25rem 0 0; text-align: center; }
.preview img {
  max-width: 100%;
  max-height: 70vh;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: repeating-conic-gradient(#e9edf3 0% 25%, #ffffff 0% 50%) 50% / 16px 16px;
}
.preview figcaption { margin-top: 0.5rem; color: var(--muted); font-size: 0.85rem; }
.app-footer { margin-top: 1.5rem; text-align: center; color: var(--muted); font-size: 0.85rem; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  transform: translate(-50%, 1rem);
  background: var(--text);
  color: var(--bg);
  padding: 0.6rem 1rem;
  border-radius: 999px;
  box-shadow: var(--shadow);
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
  max-width: min(90vw, 32rem);
}
.toast.visible { opacity: 1; transform: translate(-50%, 0); }
.toast[data-type="error"] { background: var(--danger); color: #fff; }
.toast[data-type="success"] { background: var(--success); color: #fff; }
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (hover: none) {
  .cell-actions, .cell-zoom, .cell-handle { opacity: 1; pointer-events: auto; }
}
@media (max-width: 640px) {
  .app { width: min(100% - 1.25rem, 1000px); padding-top: 1rem; }
  .panel { padding: 1rem; }
  .header-top { flex-wrap: wrap; }
  .option-grid { grid-template-columns: 1fr; }
  .cell-actions {
    top: 0.25rem;
    right: 0.25rem;
    width: calc(44px * 2 + 0.25rem);
    flex-wrap: wrap;
    justify-content: flex-end;
  }
  .cell-handle, .cell-zoom { display: none; }
}
