:root {
  --bg: #f4f4f2;
  --panel: #ffffff;
  --line: #e4e4e0;
  --text: #1b1b1a;
  --muted: #6f6f6a;
  --accent: #1f6feb;
  --accent-ink: #ffffff;
  --danger: #c4372b;
  --ok: #1f9d55;
  --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-weight: 400; }
kbd, code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
  background: #efefec;
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 0 4px;
}

/* Buttons and controls */
.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.1s, border-color 0.1s;
}
.btn:hover:not(:disabled) { background: #f6f6f4; border-color: #d4d4cf; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.small { padding: 4px 9px; font-size: 13px; }
.btn.ghost { background: transparent; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: #1a62d4; border-color: #1a62d4; }
.btn.icon { width: 32px; padding: 6px 0; }
.btn.danger { color: var(--danger); }

select, input[type="number"], input:not([type]), input[type="text"] {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 9px;
  background: var(--panel);
  width: 100%;
  min-width: 0;
}
select:focus, input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent); outline-offset: 0; border-color: var(--accent); }
input[type="range"] { width: 100%; accent-color: var(--accent); }

.segmented { display: inline-flex; background: #ececea; border-radius: 8px; padding: 2px; gap: 2px; }
.segmented.full { display: flex; }
.segmented.full label { flex: 1; }
.segmented label { cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; text-align: center; padding: 4px 10px; border-radius: 6px; font-size: 13px; color: var(--muted); }
.segmented input:checked + span { background: var(--panel); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / 0.12); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; }
.logo { width: 22px; height: 16px; border-radius: 4px; background: var(--text); position: relative; }
.logo::after { content: ""; position: absolute; left: 4px; right: 6px; top: 6px; height: 3px; background: #fff; border-radius: 2px; }
.connection { display: flex; align-items: center; gap: 8px; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: #c9c9c4; }
.status-dot.on { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 20%, transparent); }
.chips { display: flex; gap: 6px; }
.chip { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: #efefec; color: var(--muted); }
.chip.bad { background: #fde8e6; color: var(--danger); }

/* Layout */
.layout { flex: 1; display: grid; grid-template-columns: 250px 1fr 300px; min-height: 0; }

.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 8px; }
.sidebar-head h2, .panel h3 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.template-list { list-style: none; margin: 0; padding: 4px 8px; overflow-y: auto; flex: 1; }
.template-item {
  display: grid; grid-template-columns: 54px 1fr auto; gap: 10px; align-items: center;
  padding: 6px; border-radius: 8px; cursor: pointer;
}
.template-item:hover { background: #f5f5f3; }
.template-item.active { background: color-mix(in srgb, var(--accent) 10%, white); }
.template-item .thumb {
  width: 54px; height: 40px; border-radius: 4px; background: #fff; border: 1px solid var(--line);
  display: grid; place-items: center; overflow: hidden;
}
.template-item .thumb img { max-width: 100%; max-height: 100%; }
.template-item .name { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.template-item .meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.template-item .actions { display: flex; opacity: 0; }
.template-item:hover .actions, .template-item.active .actions { opacity: 1; }
.template-item .actions button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 4px; border-radius: 6px; font-size: 13px; }
.template-item .actions button:hover { background: #ececea; color: var(--text); }
.sidebar-hint { margin: 0; padding: 12px 14px 16px; font-size: 12px; color: var(--muted); line-height: 1.5; border-top: 1px solid var(--line); }

/* Stage */
.stage { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.stage-toolbar { display: flex; align-items: center; gap: 12px; padding: 10px 16px; }
.title-input { font-size: 16px !important; font-weight: 600; border-color: transparent !important; background: transparent !important; max-width: 280px; }
.title-input:hover { border-color: var(--line) !important; }
.title-input:focus { background: var(--panel) !important; }
.toolbar-actions { margin-left: auto; display: flex; gap: 4px; }
.stage-canvas { flex: 1; display: grid; place-items: center; padding: 24px; min-height: 0; overflow: hidden; }
.label-frame {
  background: #fff; border-radius: 10px; box-shadow: 0 1px 2px rgb(0 0 0 / 0.08), 0 8px 30px rgb(0 0 0 / 0.08);
  overflow: hidden; display: grid; line-height: 0;
}
#preview { display: block; }
.stage-info { text-align: center; color: var(--muted); font-size: 12px; padding: 0 16px 8px; }
.warnings { padding: 0 16px 16px; display: flex; flex-direction: column; gap: 6px; align-items: center; }
.warning { background: #fff6e0; border: 1px solid #f1dfab; color: #6b5310; border-radius: 8px; padding: 6px 10px; font-size: 13px; max-width: 640px; }

.empty { max-width: 520px; color: var(--muted); line-height: 1.6; }
.empty h1 { color: var(--text); font-size: 20px; margin: 0 0 10px; }
.empty ol { padding-left: 20px; margin: 0; }
.empty li { margin-bottom: 8px; }

/* Panel */
.panel { background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.panel-section { padding: 14px 16px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.panel > .panel-section:nth-child(2) { flex: 1; overflow-y: auto; }
.panel h3 { margin-bottom: 2px; }
.panel.disabled .panel-section { opacity: 0.45; pointer-events: none; }
.row { display: flex; gap: 6px; }
.field-label { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; margin-top: 4px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 6px; }
.field-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.field-head .name { font-size: 12px; font-weight: 600; }
.align { display: inline-flex; gap: 2px; }
.align button { border: 1px solid transparent; background: none; border-radius: 5px; padding: 2px 5px; cursor: pointer; color: var(--muted); line-height: 0; }
.align button.on { background: #ececea; color: var(--text); }
.align svg { width: 14px; height: 14px; }
.no-fields { font-size: 13px; color: var(--muted); line-height: 1.5; margin: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.advanced summary { cursor: pointer; font-size: 13px; color: var(--muted); margin-top: 6px; }
.advanced[open] summary { margin-bottom: 4px; }
.advanced #alignmentTest { margin-top: 10px; display: block; }
.help { font-size: 12px; color: var(--muted); margin: 6px 0 0; line-height: 1.4; }

.print-bar { padding: 14px 16px; display: grid; grid-template-columns: auto 1fr; gap: 10px; align-items: center; }
.copies { display: flex; align-items: center; gap: 4px; }
.copies input { width: 46px; text-align: center; padding: 6px 2px; }
.print-bar .primary { padding: 10px; font-size: 15px; }
.progress { grid-column: 1 / -1; height: 4px; background: #ececea; border-radius: 99px; overflow: hidden; }
#progressBar { height: 100%; width: 0; background: var(--accent); transition: width 0.1s; }
.print-hint { grid-column: 1 / -1; font-size: 12px; color: var(--muted); text-align: center; min-height: 1em; }

/* Dialog */
dialog { border: 0; border-radius: 14px; padding: 0; box-shadow: 0 20px 60px rgb(0 0 0 / 0.25); width: min(460px, 92vw); }
dialog::backdrop { background: rgb(0 0 0 / 0.25); }
.dialog-body { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.dialog-body h2 { margin: 0; font-size: 17px; }
.dialog-body p { margin: 0; font-size: 13px; }
.size-list { list-style: none; padding: 0; margin: 0; border: 1px solid var(--line); border-radius: 8px; max-height: 260px; overflow-y: auto; }
.size-list li { display: flex; justify-content: space-between; align-items: center; padding: 7px 10px; border-bottom: 1px solid var(--line); }
.size-list li:last-child { border-bottom: 0; }
.add-size { display: grid; grid-template-columns: 1.4fr 1fr auto 1fr auto; gap: 6px; align-items: center; }
.round-option { grid-column: 1 / -1; font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.label-frame.round { border-radius: 50%; }
.dialog-actions { display: flex; justify-content: flex-end; }

/* Overlays */
.drop-overlay {
  position: fixed; inset: 0; background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 3px dashed var(--accent); display: grid; place-items: center; z-index: 10; pointer-events: none;
}
.drop-overlay div { background: var(--panel); padding: 14px 22px; border-radius: 10px; font-weight: 600; box-shadow: 0 8px 30px rgb(0 0 0 / 0.15); }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0;
  background: #1b1b1a; color: #fff; padding: 9px 16px; border-radius: 10px; font-size: 13px;
  transition: opacity 0.2s, transform 0.2s; pointer-events: none; z-index: 20; max-width: 80vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }
.measure { position: absolute; left: -100000px; top: 0; visibility: hidden; pointer-events: none; }

@media (max-width: 960px) {
  .layout { grid-template-columns: 200px 1fr 270px; }
  .connection .chips { display: none; }
}
