:root {
  --workspace: #E9ECF0;
  --surface: #FFFFFF;
  --surface-2: #F6F7F9;
  --border: #D8DDE3;
  --border-strong: #C3CAD3;
  --text: #1C232B;
  --muted: #5E6A78;
  --accent: #1D5BB6;
  --accent-hover: #174C99;
  --accent-soft: #E6EEFA;
  --warn: #B42318;
  --ok: #1B7A45;
  --dot: rgba(28, 35, 43, 0.09);
  color-scheme: light;
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --workspace: #14181D;
    --surface: #1D232A;
    --surface-2: #232A32;
    --border: #313A44;
    --border-strong: #3E4853;
    --text: #E5E9EE;
    --muted: #97A2B0;
    --accent: #4D8EF0;
    --accent-hover: #6AA1F3;
    --accent-soft: #1F3150;
    --warn: #F28B7D;
    --ok: #6FCF97;
    --dot: rgba(229, 233, 238, 0.07);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --workspace: #14181D;
  --surface: #1D232A;
  --surface-2: #232A32;
  --border: #313A44;
  --border-strong: #3E4853;
  --text: #E5E9EE;
  --muted: #97A2B0;
  --accent: #4D8EF0;
  --accent-hover: #6AA1F3;
  --accent-soft: #1F3150;
  --warn: #F28B7D;
  --ok: #6FCF97;
  --dot: rgba(229, 233, 238, 0.07);
  color-scheme: dark;
}
:root[data-theme="light"] { color-scheme: light; }
*, *::before, *::after { box-sizing: inherit; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--workspace);
  color: var(--text);
  font: 400 14px/1.4 "IBM Plex Sans", "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.app { display: grid; grid-template-rows: auto 1fr auto; grid-template-columns: minmax(0, 1fr); height: 100%; }

/* Toolbar */
.toolbar {
  display: flex; align-items: center; gap: 12px;
  height: 54px; padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1 1 auto; }
.brand svg { flex: none; }
.brand h1 { margin: 0; font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand .size { color: var(--muted); font-size: 13px; white-space: nowrap; }
.divider { width: 1px; height: 24px; background: var(--border); flex: none; }
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  height: 34px; padding: 0 14px;
  border-radius: 6px; border: 1px solid var(--border-strong);
  background: var(--surface); font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { border-color: transparent; padding: 0 10px 0 6px; }
.btn svg { width: 16px; height: 16px; flex: none; }

/* Home: label type chooser */
.home { overflow-y: auto; padding: 36px clamp(16px, 4vw, 48px) 48px; }
.home-inner { max-width: 1120px; margin: 0 auto; }
.home h2 { margin: 0 0 4px; font-size: 22px; font-weight: 600; }
.home .lead { margin: 0 0 24px; color: var(--muted); }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.tile {
  display: flex; flex-direction: column; text-align: left;
  padding: 0; overflow: hidden; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  transition: border-color .15s, box-shadow .15s;
}
.tile:hover { border-color: var(--accent); box-shadow: 0 6px 20px -10px rgba(0,0,0,.35); }
.tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.thumb {
  height: 190px; display: grid; place-items: center;
  background-color: var(--surface-2);
  background-image: radial-gradient(var(--dot) 1px, transparent 1px);
  background-size: 14px 14px;
  border-bottom: 1px solid var(--border);
}
.thumb svg { background: #fff; border-radius: 3px; box-shadow: 0 0 0 1px rgba(0,0,0,.06), 0 6px 16px -8px rgba(0,0,0,.3); }
.tile-meta { padding: 14px 16px 16px; }
.tile-name { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.tile-name strong { font-size: 15px; font-weight: 600; }
.tile-name span { color: var(--muted); font-size: 13px; white-space: nowrap; }
.tile-desc { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

/* Editor */
.main { display: grid; grid-template-columns: 340px 1fr; min-height: 0; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); overflow-y: auto; }
.group { padding: 16px 18px 6px; border-bottom: 1px solid var(--border); }
.group h2 { margin: 0 0 12px; font-size: 13px; font-weight: 600; }
details.group > summary {
  cursor: pointer; list-style: none; font-size: 13px; font-weight: 600; margin: 0 0 12px;
  display: flex; align-items: center; gap: 6px;
}
details.group > summary::-webkit-details-marker { display: none; }
details.group > summary::before { content: "▸"; color: var(--muted); transition: transform .15s; }
details.group[open] > summary::before { transform: rotate(90deg); }
details.group:not([open]) { padding-bottom: 4px; }
/* Content inside <details> doesn't inherit box-sizing in Chrome, so set it here */
.more-body { display: block; min-width: 0; box-sizing: border-box; }
input, select, textarea, button { box-sizing: border-box; }

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; min-width: 0; }
.field > span, .field-head > span { font-size: 12.5px; font-weight: 500; color: var(--muted); }
.field small { color: var(--muted); font-size: 12px; }
input[type="text"], input[type="number"], input[type="date"], select, textarea {
  width: 100%; min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  font-variant-numeric: tabular-nums;
}
input[type="text"], input[type="number"], input[type="date"], select { height: 36px; padding: 0 10px; }
textarea { padding: 8px 10px; min-height: 160px; resize: vertical; line-height: 1.5; }
input[type="number"] { -moz-appearance: textfield; }
input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input:hover, select:hover, textarea:hover { border-color: var(--muted); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
button:focus-visible, summary:focus-visible, .switch input:focus-visible + .track, .seg input:focus-visible + span {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

.field-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.switch { display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--muted); cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.track { width: 28px; height: 16px; border-radius: 8px; background: var(--border-strong); position: relative; transition: background .15s; }
.track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 12px; height: 12px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(12px); }

/* Segmented control: one value / a list */
.seg { display: grid; grid-template-columns: repeat(3, 1fr); padding: 3px; gap: 3px; margin-bottom: 14px;
       background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; }
.seg label { position: relative; cursor: pointer; }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.seg span { display: block; text-align: center; padding: 6px 8px; border-radius: 6px; font-weight: 500; color: var(--muted); }
.seg input:checked + span { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 0 0 1px var(--border), 0 1px 2px rgba(0,0,0,.08); }

.stepper { display: flex; }
.stepper button { flex: none; width: 36px; height: 36px; border: 1px solid var(--border-strong); background: var(--surface-2); cursor: pointer; font-size: 16px; line-height: 1; }
.stepper button:hover { background: var(--border); }
.stepper button:first-child { border-radius: 6px 0 0 6px; }
.stepper button:last-child { border-radius: 0 6px 6px 0; }
.stepper input { border-radius: 0; border-left: 0; border-right: 0; text-align: center; }

/* Workspace */
.workspace {
  position: relative; min-height: 0; overflow: auto;
  background-color: var(--workspace);
  background-image: radial-gradient(var(--dot) 1px, transparent 1px);
  background-size: 16px 16px;
  display: grid; place-items: center;
  padding: 56px 24px 64px;
}
.chips { position: absolute; top: 14px; left: 16px; right: 16px; display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  font-size: 12px; font-weight: 500; color: var(--muted);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; padding: 3px 10px; font-variant-numeric: tabular-nums;
}
.sheet {
  display: block; background: #fff; border-radius: 4px;
  box-shadow: 0 0 0 1px rgba(0,0,0,.06), 0 2px 4px rgba(0,0,0,.06), 0 16px 40px -16px rgba(0,0,0,.3);
}
.pager {
  position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 3px;
  font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap;
}
.pager button { width: 30px; height: 28px; border: 0; background: transparent; border-radius: 5px; cursor: pointer; font-size: 16px; }
.pager button:hover:not(:disabled) { background: var(--surface-2); }
.pager button:disabled { opacity: .35; cursor: default; }
.pager span { padding: 0 8px; color: var(--muted); }

/* Status bar */
.statusbar {
  display: flex; align-items: center; gap: 16px;
  min-height: 28px; padding: 4px 16px;
  background: var(--surface); border-top: 1px solid var(--border);
  font-size: 12px; color: var(--muted);
}
.statusbar .msg { flex: 1; min-width: 0; }
.statusbar .info { font-variant-numeric: tabular-nums; white-space: nowrap; }
.theme-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 24px; height: 22px; margin-right: -6px; padding: 0;
  border: 0; border-radius: 5px; background: transparent; color: var(--muted); cursor: pointer;
}
.theme-btn:hover { background: var(--surface-2); color: var(--text); }
.theme-btn svg { width: 15px; height: 15px; }
.warn { color: var(--warn); font-weight: 500; }
.ok { color: var(--ok); font-weight: 500; }
.msg span + span { margin-left: 12px; }

@media (max-width: 820px) {
  html, body { height: auto; }
  .app { height: auto; min-height: 100%; }
  .main { grid-template-columns: minmax(0, 1fr); }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); overflow: visible; }
  .toolbar { padding: 0 10px; gap: 8px; }
  .btn { padding: 0 10px; }
  .brand .size, .divider, .hide-sm { display: none; }
  .statusbar { flex-wrap: wrap; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { transition: none !important; } }

/* Home sections */
.cat + .cat { margin-top: 32px; }
.cat h3 { margin: 0 0 12px; font-size: 13px; font-weight: 600; color: var(--muted); }

/* Custom label designer */
.btn-small { height: 30px; padding: 0 10px; font-size: 13px; }
.btn-danger { color: var(--warn); }
.add-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.el-list { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.el-item {
  display: flex; align-items: baseline; gap: 8px; width: 100%; text-align: left;
  padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); cursor: pointer;
}
.el-item:hover { background: var(--surface-2); }
.el-item.active { border-color: var(--accent); background: var(--accent-soft); }
.el-type { flex: none; font-size: 12px; font-weight: 600; color: var(--muted); min-width: 56px; }
.el-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hint { margin: 0 0 12px; color: var(--muted); font-size: 13px; }
.linkish { background: none; border: 0; padding: 0; color: var(--accent); font-size: 12.5px; font-weight: 500; cursor: pointer; }
.switch-line { margin: -2px 0 12px; }
.seg.seg-4 { grid-template-columns: repeat(4, 1fr); margin-bottom: 0; }
.sheet.designing { touch-action: none; outline: none; }
.sheet.designing:focus-visible { box-shadow: 0 0 0 2px var(--accent), 0 16px 40px -16px rgba(0,0,0,.3); }
.sheet rect[data-el] { stroke: transparent; stroke-width: 1px; vector-effect: non-scaling-stroke; cursor: move; }
.sheet rect[data-el]:hover { stroke: var(--accent); stroke-dasharray: 4 3; }
.sheet rect[data-el].sel { stroke: var(--accent); stroke-width: 1.5px; stroke-dasharray: none; }
.sheet .dguide { stroke: #E5484D; stroke-width: 1px; vector-effect: non-scaling-stroke; stroke-dasharray: 4 3; }
