:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #12171c;
  --muted: #5c6570;
  --line: #d7dce2;
  --accent: #1f4b7a;
  --danger: #9b2c2c;
  --ok: #1f6b4a;
  --topbar-h: 58px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101418;
    --panel: #1a2128;
    --ink: #f2f4f6;
    --muted: #a0a8b0;
    --line: #2c3640;
    --accent: #6ea8e0;
    --danger: #e07070;
    --ok: #6cbc95;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", "Noto Sans TC", sans-serif;
  background: var(--bg);
  color: var(--ink);
  height: 100vh;
  overflow: hidden;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem 1rem;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.85rem;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  min-height: var(--topbar-h);
}

.topbar-brand h1 {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.2;
}

.help-details {
  margin-top: 0.15rem;
}

.help-details summary {
  cursor: pointer;
  font-size: 0.75rem;
  color: var(--muted);
}

.help-details p {
  margin: 0.35rem 0 0;
  max-width: 22rem;
  font-size: 0.8rem;
  line-height: 1.4;
}

.muted {
  color: var(--muted);
}

.topbar-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.75rem;
  align-items: flex-end;
  flex: 1;
  justify-content: center;
}

.inline-field {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  font-size: 0.72rem;
  color: var(--muted);
  margin: 0;
}

.inline-field select,
.inline-field input[type="number"] {
  width: auto;
  min-width: 5.5rem;
  margin: 0;
  padding: 0.35rem 0.45rem;
  font-size: 0.85rem;
}

.inline-field select {
  min-width: 8.5rem;
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
}

.layout {
  display: grid;
  grid-template-columns: 200px minmax(0, 1fr) 280px;
  height: calc(100vh - var(--topbar-h));
  min-height: 0;
}

.panel {
  background: var(--panel);
  padding: 0.75rem;
  overflow: auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.panel-left {
  border-right: 1px solid var(--line);
}

.panel-right {
  border-left: 1px solid var(--line);
}

.panel section {
  margin: 0;
}

.panel section.grow {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.panel h2 {
  margin: 0 0 0.45rem;
  font-size: 0.88rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 0.82rem;
}

input,
select {
  width: 100%;
  margin-top: 0.2rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
}

.xy-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.xy-row label {
  margin-bottom: 0;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.btn-col {
  flex-direction: column;
}

.btn-col .button {
  width: 100%;
  justify-content: center;
}

.button {
  appearance: none;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 0.4rem 0.65rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.button.secondary {
  background: transparent;
  color: var(--ink);
  border-color: var(--line);
}

.button.danger {
  border-color: var(--danger);
  color: var(--danger);
  width: 100%;
  margin-top: 0.5rem;
}

.file-label {
  cursor: pointer;
}

.hint {
  font-size: 0.78rem;
  color: var(--muted);
  margin: 0.35rem 0 0.55rem;
  line-height: 1.4;
}

.mono {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.72rem;
  word-break: break-all;
}

.item-list {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1;
  overflow: auto;
  min-height: 4rem;
}

.item-list li {
  padding: 0.4rem 0.45rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  margin-bottom: 0.3rem;
  cursor: pointer;
  font-size: 0.8rem;
}

.item-list li.active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

.item-list li.bad {
  border-color: var(--danger);
}

.canvas-wrap {
  padding: 0.5rem 0.65rem 0.65rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
}

.canvas-stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-tools {
  position: absolute;
  top: auto;
  bottom: 0.5rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  align-items: center;
  justify-content: center;
  padding: 0.35rem 0.45rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(6px);
  box-shadow: 0 2px 10px rgb(0 0 0 / 8%);
  /* container must not steal clicks from canvas / thin template lines */
  pointer-events: none;
}

.preview-tools[hidden] {
  display: none !important;
}

.preview-tools .button {
  padding: 0.28rem 0.5rem;
  font-size: 0.78rem;
  border-radius: 999px;
  pointer-events: auto;
}

.toolbar-sep {
  width: 1px;
  height: 1.1rem;
  background: var(--line);
  margin: 0 0.15rem;
}

#bezel-canvas {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1100 / 820;
  background: #e8eaed;
  border-radius: 10px;
  border: 1px solid var(--line);
  touch-action: none;
  cursor: default;
}

#bezel-canvas.cursor-rotate {
  cursor: grab;
}

#bezel-canvas.cursor-resize {
  cursor: nwse-resize;
}

#bezel-canvas.cursor-move {
  cursor: move;
}

@media (prefers-color-scheme: dark) {
  #bezel-canvas {
    background: #0d1116;
  }
}

.status {
  min-height: 1.15rem;
  font-size: 0.85rem;
  flex-shrink: 0;
}

.status.error {
  color: var(--danger);
}

.status.ok {
  color: var(--ok);
}

.legend {
  font-size: 0.75rem;
  margin: 0;
  flex-shrink: 0;
  text-align: center;
}

.meta details summary {
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
}

.meta details[open] summary {
  margin-bottom: 0.4rem;
}

@media (max-width: 960px) {
  body {
    height: auto;
    overflow: auto;
  }

  .layout {
    grid-template-columns: 1fr;
    height: auto;
  }

  .panel-left,
  .panel-right {
    border: none;
    border-bottom: 1px solid var(--line);
    max-height: none;
  }

  .canvas-wrap {
    min-height: 0;
  }

  .canvas-stage {
    min-height: 0;
  }

  #bezel-canvas {
    width: 100%;
    height: auto;
    max-height: none;
    aspect-ratio: 1100 / 820;
  }

  .topbar-controls {
    justify-content: flex-start;
    order: 3;
    width: 100%;
  }
}
