:root {
  --bg-1: #192d38;
  --bg-2: #211f2f;
  --panel: #131321;
  --panel-2: #1f1c2c;
  --field-bg: #211f2f;
  --accent: #08ffbd;
  --warn: #ffd908;
  --muted: #a8c9cc;
  --line: #3a3a4a;
  --danger: #6d1a39;
  --font-ui: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Noto Sans SC", system-ui, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  --font-mono: "JetBrains Mono", "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  padding: 4vmin 5vmin 5vmin;
  background: radial-gradient(ellipse at center, var(--bg-1) 0, var(--bg-2) 100%);
  background-attachment: fixed;
  color: #fff;
  font: 300 16px/1.6 var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

button,
input {
  font: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* 滚动条：浏览器默认的浅灰太跳，改成细窄的主题色 */
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  border: 2px solid transparent;
  border-radius: 999px;
  background: rgba(8, 255, 189, .3);
  background-clip: content-box;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba(8, 255, 189, .6);
  background-clip: content-box;
}

::-webkit-scrollbar-corner {
  background: transparent;
}

/* Firefox 不认上面的伪元素，用标准属性兜底 */
@supports not selector(::-webkit-scrollbar) {
  html {
    scrollbar-width: thin;
    scrollbar-color: rgba(8, 255, 189, .32) transparent;
  }
}

/* 页头 */
.page-header {
  margin-bottom: 32px;
  text-align: center;
}

.page-title {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 0;
  font-size: clamp(24px, 3.4vw, 36px);
  font-weight: 700;
  text-shadow: 0 2px 5px #000;
}

.logo {
  flex: 0 0 auto;
  fill: var(--accent);
  vertical-align: middle;
}

.page-subtitle {
  margin: 8px 0 0;
  color: var(--warn);
  font-size: 15px;
  opacity: .9;
}

/* 主布局 */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 32px;
  align-items: start;
  max-width: 1400px;
  margin: 0 auto;
}

/* 网格预览区 */
.stage {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr);
  height: clamp(300px, 62vh, 640px);
  min-width: 0;
}

/* 行输入条：顶部让出列输入条那 46px，才能和右侧格子逐行对齐 */
.row-units {
  grid-column: 1;
  display: grid;
  min-width: 0;
  min-height: 0;
  padding-top: 46px;
}

/* 列数多、间距大时网格会比容器宽，交给这层横向滚动，别把列压成一条线 */
.stage-scroll {
  grid-column: 2;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
}

.stage-grid {
  display: grid;
  grid-template-rows: 46px minmax(0, 1fr);
  height: 100%;
  /* --stage-min-w 由脚本按列数与间距算出，装得下时不起作用 */
  width: max(100%, var(--stage-min-w, 0px));
}

.col-units {
  grid-row: 1;
  display: grid;
  min-width: 0;
}

.unit-cell {
  position: relative;
  display: flex;
  align-items: center;
  min-width: 0;
  min-height: 0;
  padding: 0 3px;
}

.unit-input {
  width: 100%;
  min-width: 0;
  padding: 5px 2px;
  border: 1px solid #666;
  border-radius: 4px;
  background: var(--field-bg);
  color: #fff;
  font-size: 13px;
  text-align: center;
}

/* 行方向的输入框高度跟随行高，否则行数多 + 间距大时会把 1fr 撑开，与右侧格子错位 */
.row-units .unit-input {
  height: 100%;
  max-height: 32px;
  min-height: 0;
  padding: 0 2px;
  line-height: 1.2;
}

.unit-input.has-error {
  border-color: #ff8080;
}

.unit-error {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 6px);
  z-index: 6;
  width: max-content;
  max-width: 170px;
  padding: 6px 10px;
  border-radius: 4px;
  background: var(--danger);
  color: #fff;
  font-size: 11px;
  line-height: 1.4;
  transform: translateX(-50%);
}

.row-units .unit-error {
  left: calc(100% + 8px);
  bottom: auto;
  top: 50%;
  transform: translateY(-50%);
}

.grid-frame {
  grid-row: 2;
  position: relative;
  border: 1px solid var(--accent);
  background: linear-gradient(180deg, var(--panel) 0, var(--panel-2) 100%);
  box-shadow: 0 2px 20px 0 #000;
  overflow: hidden;
}

.grid-layer {
  position: absolute;
  inset: 0;
  display: grid;
  grid-auto-flow: row dense;
}

.box-layer {
  z-index: 1;
}

.child-layer {
  z-index: 2;
  pointer-events: none;
}

/* 划区时的实时预览 */
.ghost-layer {
  z-index: 3;
  pointer-events: none;
}

.ghost-layer[hidden] {
  display: none;
}

.ghost-block {
  border: 2px dashed var(--accent);
  background: rgba(8, 255, 189, .22);
}

.box {
  position: relative;
  border: 1px dotted rgba(255, 255, 255, .55);
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg width='8' height='8' viewBox='0 0 6 6' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M5 0h1L0 6V5zm1 5v1H5z' fill='%239C92AC' fill-opacity='.35' fill-rule='evenodd'/%3E%3C/svg%3E");
  opacity: .5;
  cursor: crosshair;
  transition: background-color .2s ease, opacity .2s ease;
  user-select: none;
  /* 纵向留给页面滚动，横向拖动用来划区 */
  touch-action: pan-y;
}

.box:hover {
  opacity: 1;
  background-color: rgba(8, 255, 189, .18);
}

.box.is-start {
  opacity: 1;
  background-color: rgba(8, 255, 189, .38);
}

.child {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 2px;
  /* 作为 grid item 不能因长标签、删除按钮而撑开所属轨道 */
  min-width: 0;
  min-height: 0;
  padding: 2px;
  border: 1px solid #ddd;
  background: hsl(var(--hue) 62% 42% / .8);
  cursor: move;
  user-select: none;
  pointer-events: auto;
  touch-action: pan-y;
}

.child.is-selected {
  z-index: 3;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(8, 255, 189, .5);
}

.child.is-dragging {
  z-index: 4;
  opacity: .88;
}

.child.is-conflict {
  animation: conflict-flash .5s ease;
}

@keyframes conflict-flash {
  0%,
  100% {
    box-shadow: 0 0 0 1px rgba(255, 128, 128, 0);
  }

  45% {
    box-shadow: 0 0 0 4px rgba(255, 128, 128, .85);
  }
}

.child-label {
  flex: 1 1 auto;
  min-width: 0;
  padding: 0 4px;
  overflow: hidden;
  border-radius: 3px;
  background: rgba(0, 0, 0, .28);
  color: #fff;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.6;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: text;
}

/* 区域可能窄到只剩几个像素，输入框浮在区域上方定宽，否则一个字都看不见 */
.child-name-input {
  position: absolute;
  top: 2px;
  left: 2px;
  z-index: 5;
  width: max(140px, 100%);
  padding: 0 4px;
  border: 1px solid var(--accent);
  border-radius: 3px;
  background: #11111a;
  color: #fff;
  font-family: var(--font-mono);
  font-size: 12px;
}

.child-remove {
  display: none;
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, .45);
  color: #fff;
  font-size: 16px;
  line-height: 24px;
  opacity: .8;
  cursor: pointer;
}

.child:hover .child-remove,
.child:focus-within .child-remove,
.child.is-selected .child-remove {
  display: block;
}

.child-remove:hover {
  background: #b32b4f;
  opacity: 1;
}

/* 选中后才能拖角缩放 */
.child-handle {
  position: absolute;
  display: none;
  width: 12px;
  height: 12px;
  border: 1px solid #0b1a20;
  border-radius: 2px;
  background: var(--accent);
}

.child.is-selected .child-handle {
  display: block;
}

.child-handle-nw {
  top: -6px;
  left: -6px;
  cursor: nwse-resize;
}

.child-handle-ne {
  top: -6px;
  right: -6px;
  cursor: nesw-resize;
}

.child-handle-sw {
  bottom: -6px;
  left: -6px;
  cursor: nesw-resize;
}

.child-handle-se {
  bottom: -6px;
  right: -6px;
  cursor: nwse-resize;
}

/* 右侧控制面板 */
.panel {
  display: flex;
  flex-direction: column;
  gap: 22px;
  min-width: 0;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 68px;
  align-items: center;
  gap: 12px;
}

.field-label {
  color: #fff;
}

.hint {
  opacity: .65;
  font-size: 13px;
}

.field input {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid #666;
  border-radius: 4px;
  background: var(--field-bg);
  color: #fff;
  font-size: 15px;
  text-align: center;
}

/* 行列数量：滑杆 + 右侧实时数值 */
.field-slider {
  grid-template-columns: 44px minmax(0, 1fr) 34px;
  gap: 10px;
}

.field-slider input[type="range"] {
  width: 100%;
  height: 20px;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}

.field-slider input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .2);
}

.field-slider input[type="range"]::-webkit-slider-thumb {
  width: 14px;
  height: 14px;
  margin-top: -5px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 6px rgba(8, 255, 189, .55);
  -webkit-appearance: none;
  appearance: none;
}

.field-slider input[type="range"]::-moz-range-track {
  height: 4px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .2);
}

.field-slider input[type="range"]::-moz-range-progress {
  height: 4px;
  border-radius: 999px;
  background: var(--accent);
}

.field-slider input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
}

.field-value {
  color: var(--accent);
  font: 14px/1 var(--font-mono);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.tips {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.75;
}

.actions {
  display: flex;
  gap: 8px;
}

/* 三个按钮平分一行，不留折行的余地 */
.actions .btn {
  flex: 1 1 0;
  min-width: 0;
  padding: 6px 2px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.btn {
  padding: 6px 22px;
  border: 1px solid var(--accent);
  border-radius: 1000px;
  background: transparent;
  color: var(--accent);
  font-size: 15px;
  cursor: pointer;
  transition: all .3s;
}

.btn:hover {
  background: var(--accent);
  color: #000;
}

.btn-ghost {
  border-color: #aaa;
  color: #aaa;
}

.btn-ghost:hover {
  background: #aaa;
  color: #000;
}

.btn-small {
  padding: 4px 14px;
  font-size: 13px;
}

.text-link {
  align-self: flex-start;
  padding: 0;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 16px;
  text-decoration: underline dotted;
  text-underline-offset: 4px;
  cursor: pointer;
  opacity: .9;
}

.text-link:hover {
  color: var(--accent);
}

/* 弹窗 */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  z-index: 200;
  max-width: min(90vw, 420px);
  padding: 10px 18px;
  border: 1px solid var(--accent);
  border-radius: 1000px;
  background: rgba(19, 19, 33, .96);
  color: #fff;
  font-size: 14px;
  line-height: 1.5;
  opacity: 0;
  transform: translate(-50%, 12px);
  transition: opacity .2s ease, transform .2s ease;
  pointer-events: none;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.modal-mask {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, .75);
  overflow: auto;
}

.modal-mask[hidden] {
  display: none;
}

.modal {
  display: flex;
  flex-direction: column;
  width: min(820px, 100%);
  max-height: 86vh;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #1c1b29;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .6);
}

.modal-header {
  padding: 18px 24px;
  border-bottom: 1px solid var(--line);
}

.modal-header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
}

.modal-body {
  padding: 20px 24px;
  overflow: auto;
  line-height: 1.75;
}

.modal-body p {
  margin: 0 0 12px;
}

.modal-body a {
  color: var(--accent);
  text-decoration: none;
}

.modal-body a:hover {
  text-decoration: underline;
}

.modal-body strong {
  color: var(--warn);
}

.modal-footer {
  padding: 0 24px 20px;
  text-align: right;
}

.code-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.code-tools {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* 代码写法只影响生成结果，所以放在代码弹窗里 */
.code-mode {
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 1000px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
}

.code-mode:hover,
.code-mode:focus-visible {
  border-color: var(--accent);
  color: var(--accent);
}

.code-mode option {
  background: #1c1b29;
  color: #fff;
}

.code-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tab {
  padding: 4px 14px;
  border: 1px solid var(--line);
  border-radius: 1000px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  transition: all .2s;
}

.tab:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.tab[aria-selected="true"] {
  border-color: var(--accent);
  background: rgba(8, 255, 189, .14);
  color: var(--accent);
}

.code-block {
  max-height: 50vh;
  margin: 0;
  padding: 15px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: linear-gradient(180deg, var(--panel) 0, var(--panel-2) 100%);
  overflow: auto;
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.6;
  white-space: pre;
  tab-size: 2;
}

/* 代码 + 右侧类名清单 */
.code-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 172px;
  gap: 16px;
  align-items: start;
}

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

.name-panel-title {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.name-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 50vh;
  margin: 0;
  padding: 2px;
  overflow: auto;
  list-style: none;
}

.name-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* 与画布上的区域同色，方便对应 */
.name-swatch {
  flex: none;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: hsl(var(--hue) 62% 42%);
}

.name-input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--field-bg);
  color: #fff;
  font-family: var(--font-mono);
  font-size: 13px;
}

.name-input:focus {
  border-color: var(--accent);
  outline: none;
}

.name-input.has-error {
  border-color: #ff8080;
}

.cname {
  color: #ffea18;
}

.ckey {
  color: #6af2d7;
}

.cprop {
  color: #f8a384;
}

.ccomment {
  color: #7d8a8f;
}

.cstring {
  color: #f8a384;
}

.credit {
  margin-top: 16px;
  color: var(--muted);
  font-size: 13px;
}

/* 窄屏 */
@media (max-width: 860px) {
  body {
    padding: 20px 16px 40px;
  }

  .layout {
    grid-template-columns: minmax(0, 1fr);
    gap: 24px;
  }

  .stage {
    grid-template-columns: 56px minmax(0, 1fr);
    height: clamp(260px, 48vh, 460px);
  }

  .unit-input {
    font-size: 12px;
    padding: 4px 1px;
  }

  .field {
    grid-template-columns: minmax(0, 1fr) 76px;
  }

  .field-slider {
    grid-template-columns: 40px minmax(0, 1fr) 34px;
  }

  /* 触摸屏上把手柄放大，方便拖拽 */
  .child-handle {
    width: 18px;
    height: 18px;
  }

  .child-handle-nw {
    top: -9px;
    left: -9px;
  }

  .child-handle-ne {
    top: -9px;
    right: -9px;
  }

  .child-handle-sw {
    bottom: -9px;
    left: -9px;
  }

  .child-handle-se {
    bottom: -9px;
    right: -9px;
  }

  .modal-mask {
    padding: 12px;
  }

  .modal-body {
    padding: 16px;
  }

  /* 窄屏下类名清单放到代码下面 */
  .code-layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .name-list {
    max-height: 28vh;
  }
}
