:root {
  --bg: #f3f3f3;
  --panel: #ffffff;
  --panel-2: #ececec;
  --ink: #232323;
  --ink-soft: #666666;
  --line: #d5d5d5;
  --accent: #54B435;
  --accent-ink: #ffffff;
  --header-bg: #262626;
  --danger: #c0392b;
  --ok: #3c7a3c;
  --warn: #9a6b18;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ---- header ---- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 18px;
  background: var(--header-bg);
  color: #f5f5f5;
  border-bottom: 3px solid var(--accent);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
/* logo is dark artwork; a light rounded chip keeps it visible on the dark header */
.brand .logo {
  height: 34px;
  width: 34px;
  object-fit: contain;
  background: #fff;
  border-radius: 8px;
  padding: 3px;
}
.app-header h1 { font-size: 17px; margin: 0; font-weight: 600; letter-spacing: 0.2px; }
.header-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ---- buttons ---- */
.btn {
  font: inherit;
  font-size: 13px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: var(--radius);
  cursor: pointer;
  line-height: 1;
}
.btn:hover { background: #fff; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.small { padding: 4px 8px; font-size: 12px; }
.btn.icon { padding: 4px 7px; }
.btn.danger { color: var(--danger); }

/* ---- layout ---- */
.app-main {
  display: grid;
  /* editor (left) is the main, larger pane; preview (right) is smaller */
  grid-template-columns: minmax(0, 1fr) 460px;
  grid-template-rows: minmax(0, 1fr); /* pin row to viewport so panes scroll internally */
  gap: 0;
  flex: 1;
  min-height: 0;
}
.preview-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
  padding: 16px;
  gap: 12px;
}
.canvas-wrap {
  flex: 1;
  min-height: 0;
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.preview-canvas { display: block; width: 100%; height: 100%; }

.board-info {
  display: flex;
  align-items: center;
  gap: 18px;
  color: var(--ink);
  flex-wrap: wrap;
}
.thickness { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.thickness input { width: 64px; }
.board-size { font-size: 14px; font-weight: 600; }
.board-warnings { flex-basis: 100%; }

/* ---- controls pane ---- */
.controls-pane {
  background: var(--panel-2);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
.swatch-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.swatch {
  width: 46px;
  height: 40px;
  border: 2px solid rgba(0,0,0,0.15);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 3px;
}
.swatch.selected { border-color: var(--ink); box-shadow: 0 0 0 2px var(--accent); }
.swatch-code {
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.7);
}

.tabs { display: flex; background: var(--panel); border-bottom: 1px solid var(--line); }
.tab {
  flex: 1;
  font: inherit;
  font-size: 13px;
  padding: 10px 4px;
  border: none;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  border-bottom: 3px solid transparent;
}
.tab.selected { color: var(--ink); font-weight: 600; border-bottom-color: var(--accent); }

.tab-panels { flex: 1; overflow-y: auto; overflow-x: hidden; min-height: 0; }
.tab-panel { padding: 14px; }
.hidden { display: none; }

.panel-block { margin-bottom: 18px; }
.panel-block h3 { margin: 0 0 8px; font-size: 14px; }
.row-between { display: flex; align-items: center; justify-content: space-between; }
.hint { color: var(--ink-soft); font-size: 12px; margin: 2px 0 8px; }

/* ---- grid editor ---- */
.grid-toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; gap: 8px; }
/* horizontal scroll for wide boards; vertical scrolling is handled by .tab-panels */
.grid-scroll { overflow-x: auto; overflow-y: visible; }
.grid { display: flex; flex-direction: column; gap: 4px; min-width: min-content; }
.grid-row { display: flex; align-items: stretch; gap: 6px; }
.row-gutter { display: flex; align-items: center; gap: 3px; flex-shrink: 0; }
.dim-in { width: 52px; padding: 3px; font-size: 12px; }
.cells { display: flex; gap: 3px; flex: 1; min-height: 46px; }
.cell {
  flex-basis: 0;
  min-width: 34px;
  border-radius: 4px;
  border: 1px solid rgba(0,0,0,0.2);
  position: relative;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  user-select: none;
}
.cell.selected {
  outline: 3px solid var(--accent);
  outline-offset: -3px;
  z-index: 1;
}
.cell-code { font-size: 12px; font-weight: 700; }
.cell-w {
  width: 40px;
  font-size: 10px;
  padding: 1px;
  text-align: center;
  border: none;
  border-radius: 3px;
  background: rgba(255,255,255,0.75);
  color: #222;
  margin-top: 2px;
}
.cell-del {
  position: absolute;
  top: 1px; right: 1px;
  width: 15px; height: 15px;
  border: none;
  border-radius: 3px;
  background: rgba(0,0,0,0.35);
  color: #fff;
  font-size: 10px;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
}
.cell:hover .cell-del,
.cell.selected .cell-del { opacity: 1; }
.add-cell { align-self: center; flex-shrink: 0; }

/* ---- wood manager ---- */
.wood-list { display: flex; flex-direction: column; gap: 6px; }
.wood-row { display: flex; align-items: center; gap: 6px; }
.wood-row input[type="color"] { width: 34px; height: 30px; padding: 0; border: 1px solid var(--line); border-radius: 4px; }
.wood-row .code-in { width: 56px; }
.wood-row .name-in { flex: 1; }
.wood-row input[type="text"] { padding: 5px; font-size: 13px; }

/* ---- forms ---- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.form-grid label, .panel-block > label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--ink-soft); }
.form-grid input, .form-grid select, .panel-block > label input {
  padding: 5px; font-size: 13px; border: 1px solid var(--line); border-radius: 5px; background: #fff; color: var(--ink);
}
input, select, textarea { font-family: inherit; }

/* ---- text editor ---- */
.dsl {
  width: 100%;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  resize: vertical;
  background: #ffffff;
  color: var(--ink);
}
.dsl-status { margin-top: 8px; }
.msg { font-size: 12px; padding: 6px 9px; border-radius: 6px; margin-bottom: 6px; }
.msg ul { margin: 4px 0 0; padding-left: 18px; }
.msg.ok { background: #e6f0e0; color: var(--ok); }
.msg.warn { background: #f7edd4; color: var(--warn); }
.msg.err { background: #f6dcd6; color: var(--danger); }

/* ---- gallery ---- */
.save-row { display: flex; gap: 6px; margin-bottom: 10px; }
.save-row .save-name { flex: 1; padding: 6px; border: 1px solid var(--line); border-radius: 5px; }
.gallery-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.gallery-list li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 7px 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; }
.gallery-list li.empty { color: var(--ink-soft); justify-content: center; font-size: 13px; }
.g-name { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.g-actions { display: flex; gap: 5px; flex-shrink: 0; }

/* ---- version badge ---- */
.app-version {
  position: fixed;
  right: 8px;
  bottom: 6px;
  z-index: 40;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1;
  color: var(--ink-soft);
  opacity: 0.55;
  pointer-events: none;
  user-select: none;
}

/* ---- toast ---- */
.toast-host { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 50; }
.toast {
  background: #262626;
  color: #f5f5f5;
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.35);
  font-size: 13px;
  animation: toast-in 0.15s ease-out;
}
.toast.warn { background: var(--warn); }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---- responsive ---- */
@media (max-width: 900px) {
  /* stack: editor on top (main, scrollable), smaller preview below */
  .app-main { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) 40vh; }
  .controls-pane { border-right: none; border-bottom: 1px solid var(--line); }
}
