:root {
  --bg: #f3f2ef;
  --panel: #fbfaf8;
  --card: #ffffff;
  --line: #e3e0da;
  --line-strong: #cfcac2;
  --ink: #1d2733;
  --ink-2: #4a5563;
  --muted: #7b8490;
  --accent: #1d2733;
  --accent-ink: #ffffff;
  --accent-soft: #e8b6d6;
  --focus: #3b6fd8;
  --ok: #1f7a5a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(20, 24, 31, .05), 0 6px 20px rgba(20, 24, 31, .06);
  --font: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
svg { display: block; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ layout */
.app {
  display: grid;
  grid-template-columns: 272px minmax(0, 1fr) 344px;
  grid-template-rows: 60px minmax(0, 1fr);
  height: 100dvh;
}
.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px 0 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 2;
}
.library, .inspector {
  background: var(--panel);
  overflow-y: auto;
  overscroll-behavior: contain;
}
.library { border-right: 1px solid var(--line); padding: 16px 14px; }
.inspector { border-left: 1px solid var(--line); padding: 6px 18px 28px; }
.stage { position: relative; min-width: 0; min-height: 0; }
.viewport { position: absolute; inset: 0; }
.viewport canvas { display: block; width: 100%; height: 100%; touch-action: none; outline: none; }

.app[data-mode="client"] { grid-template-columns: minmax(0, 1fr); }
.app[data-mode="client"] .studio-only { display: none !important; }
.app[data-mode="studio"] .client-only { display: none !important; }

/* ------------------------------------------------------------ topbar */
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 10px;
  background: var(--ink); color: #fff;
  display: grid; place-items: center; flex: none;
}
.brand-mark svg { width: 22px; height: 22px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-text strong { font-size: 15px; font-weight: 700; letter-spacing: -.01em; }
.brand-text span { font-size: 12px; color: var(--muted); }
.project-title {
  display: flex; flex-direction: column; line-height: 1.25;
  padding-left: 16px; margin-left: 2px; border-left: 1px solid var(--line); min-width: 0;
}
.project-title strong { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.project-title span { font-size: 12px; color: var(--muted); }
.topbar-actions { margin-left: auto; display: flex; gap: 8px; }

/* ------------------------------------------------------------ buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 36px; padding: 0 14px; border-radius: 9px;
  border: 1px solid var(--line-strong); background: var(--card);
  font-weight: 500; font-size: 13.5px; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn svg { width: 17px; height: 17px; }
.btn:hover { border-color: var(--ink-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { background: #2c3a4a; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn.ghost:hover { background: #ece9e4; color: var(--ink); }
.btn.small { height: 30px; padding: 0 10px; font-size: 12.5px; border-radius: 8px; }
.btn:focus-visible, .icon-btn:focus-visible, .seg button:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--focus); outline-offset: 2px;
}
.icon-btn {
  width: 36px; height: 36px; border-radius: 9px; border: 0; background: transparent;
  display: grid; place-items: center; cursor: pointer; color: var(--ink-2);
}
.icon-btn svg { width: 19px; height: 19px; }
.icon-btn:hover { background: rgba(29, 39, 51, .07); color: var(--ink); }
.icon-btn[aria-pressed="true"] { background: var(--ink); color: #fff; }

/* ------------------------------------------------------------ library */
.panel-head { display: flex; align-items: baseline; justify-content: space-between; margin: 2px 4px 12px; }
.panel-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; margin: 0; color: var(--ink-2); }
.lib-group { font-size: 12px; color: var(--muted); margin: 14px 6px 6px; }
.library-list { display: flex; flex-direction: column; gap: 6px; }
.lib-item {
  display: grid; grid-template-columns: 64px 1fr; align-items: center; gap: 10px;
  padding: 6px 10px 6px 6px; border-radius: var(--radius);
  border: 1px solid transparent; background: transparent; cursor: pointer; text-align: left;
  transition: background .15s, border-color .15s;
}
.lib-item:hover { background: #f0eee9; }
.lib-item.on { background: var(--card); border-color: var(--line-strong); box-shadow: var(--shadow); }
.lib-thumb {
  width: 64px; height: 76px; border-radius: 9px;
  background: radial-gradient(90% 80% at 50% 30%, #ffffff, #ebe8e2);
  display: grid; place-items: center; overflow: hidden;
}
.lib-thumb img { width: 100%; height: 100%; object-fit: contain; }
.lib-meta { min-width: 0; }
.lib-meta strong { display: block; font-size: 13.5px; font-weight: 600; }
.lib-meta span { display: block; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-badge {
  display: inline-block; margin-top: 3px; font-size: 11px; font-weight: 600; color: var(--ok);
  background: #e3f1ea; border-radius: 99px; padding: 1px 7px;
}
.library-foot { font-size: 12px; margin: 18px 6px 0; }

/* ------------------------------------------------------------ stage */
.stage-info {
  position: absolute; left: 20px; top: 16px; display: flex; flex-direction: column;
  pointer-events: none; text-shadow: 0 1px 0 rgba(255,255,255,.6);
}
.stage-info strong { font-size: 17px; letter-spacing: -.01em; }
.stage-info span { font-size: 12.5px; color: var(--ink-2); }
.app.dark-bg .stage-info { color: #f1f1f1; text-shadow: none; }
.app.dark-bg .stage-info span, .app.dark-bg .hint { color: #b9bec6; }

.toolbar {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 4px; padding: 5px;
  background: rgba(255, 255, 255, .86); backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 0, 0, .06); border-radius: 14px; box-shadow: var(--shadow);
}
.tb-sep { width: 1px; height: 22px; background: var(--line); margin: 0 3px; }
.seg { display: flex; gap: 2px; }
.seg button {
  height: 34px; padding: 0 13px; border: 0; border-radius: 9px; background: transparent;
  font-size: 13px; font-weight: 500; color: var(--ink-2); cursor: pointer;
}
.seg button:hover { background: rgba(29, 39, 51, .06); color: var(--ink); }
.seg button.on { background: var(--ink); color: #fff; }
.hint {
  position: absolute; right: 18px; bottom: 26px; margin: 0; font-size: 12px; color: var(--muted);
  pointer-events: none;
}
.loading {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-radius: 99px;
  background: rgba(255, 255, 255, .9); box-shadow: var(--shadow); font-size: 13px;
}
.spinner {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--line-strong); border-top-color: var(--ink);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.dropveil {
  position: absolute; inset: 14px; border: 2px dashed var(--ink-2); border-radius: 18px;
  background: rgba(255, 255, 255, .72); display: grid; place-items: center; text-align: center;
  pointer-events: none;
}
.dropveil strong { display: block; font-size: 18px; margin-bottom: 4px; }
.dropveil span { color: var(--ink-2); }

/* client item switcher */
.client-items {
  position: absolute; left: 50%; bottom: 76px; transform: translateX(-50%);
  display: flex; gap: 8px; max-width: calc(100% - 32px); overflow-x: auto; padding: 4px;
}
.client-item {
  display: flex; align-items: center; gap: 8px; padding: 4px 12px 4px 4px; flex: none;
  border-radius: 12px; border: 1px solid rgba(0,0,0,.06); background: rgba(255,255,255,.86);
  backdrop-filter: blur(10px); cursor: pointer; font-size: 13px; font-weight: 500;
}
.client-item img { width: 36px; height: 44px; object-fit: contain; border-radius: 8px; background: #f2f0ec; }
.client-item.on { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ------------------------------------------------------------ inspector */
.sec { padding: 16px 0 18px; border-bottom: 1px solid var(--line); }
.sec:last-child { border-bottom: 0; }
.sec-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.sec-head h3 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-2); }
.row { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.row.two { flex-direction: row; align-items: center; gap: 10px; }
.row.two > * { flex: 1; }
.field-label { font-size: 12.5px; color: var(--ink-2); }
select {
  height: 36px; border-radius: 9px; border: 1px solid var(--line-strong); background: var(--card);
  padding: 0 10px; cursor: pointer;
}
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-2); cursor: pointer; margin-top: 10px; }
.row.two .check { margin-top: 0; }
.check input { width: 16px; height: 16px; accent-color: var(--ink); }

.slot {
  border: 1px solid var(--line); background: var(--card); border-radius: var(--radius);
  padding: 10px; margin-bottom: 10px;
}
.slot-top { display: grid; grid-template-columns: 74px 1fr; gap: 12px; align-items: center; }
.slot-preview {
  width: 74px; height: 74px; border-radius: 9px; display: grid; place-items: center; overflow: hidden;
  background:
    repeating-conic-gradient(#f1efeb 0 25%, #ffffff 0 50%) 0 0 / 12px 12px;
  border: 1px dashed var(--line-strong); cursor: pointer; color: var(--muted);
}
.slot-preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
.slot-preview svg { width: 22px; height: 22px; }
.slot.has .slot-preview { border-style: solid; }
.slot-meta strong { display: block; font-size: 13.5px; }
.slot-meta span { display: block; font-size: 12px; color: var(--muted); }
.slot-meta .file { color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 190px; }
.slot-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.slot-opts { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); display: grid; gap: 8px; }
.slot-opts .seg button { height: 28px; padding: 0 9px; font-size: 12px; background: #f1efeb; }
.slot-opts .seg button.on { background: var(--ink); }
.range { display: grid; grid-template-columns: 64px 1fr 40px; align-items: center; gap: 8px; font-size: 12px; color: var(--ink-2); }
.range input { width: 100%; accent-color: var(--ink); }
.range output { text-align: right; font-variant-numeric: tabular-nums; }

.color-group { margin-bottom: 12px; }
.color-group:last-child { margin-bottom: 0; }
.color-group-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 7px; font-size: 13px; }
.color-group-head code { font-size: 11.5px; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.sw {
  width: 24px; height: 24px; border-radius: 50%; border: 1px solid rgba(0,0,0,.14); cursor: pointer; padding: 0;
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.5);
}
.sw.on { outline: 2px solid var(--ink); outline-offset: 2px; }
.sw-custom {
  position: relative; width: 24px; height: 24px; border-radius: 50%; overflow: hidden; cursor: pointer;
  background: conic-gradient(#f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00); border: 1px solid rgba(0,0,0,.14);
}
.sw-custom input { position: absolute; inset: -6px; width: 40px; height: 40px; opacity: 0; cursor: pointer; }

.chips { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.chip {
  height: 36px; border-radius: 9px; border: 1px solid var(--line-strong); background: var(--card);
  font-size: 12.5px; font-weight: 500; cursor: pointer; color: var(--ink-2);
}
.chip:hover { border-color: var(--ink-2); color: var(--ink); }
.chip.on { background: var(--ink); border-color: var(--ink); color: #fff; }
.liquid { display: flex; align-items: center; justify-content: space-between; margin-top: 4px; }
.liquid input[type="color"] { width: 34px; height: 26px; border: 1px solid var(--line-strong); border-radius: 7px; padding: 2px; background: var(--card); cursor: pointer; margin-top: 10px; }

.bg-swatches { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.bg-sw {
  height: 46px; border-radius: 10px; border: 1px solid var(--line-strong); cursor: pointer;
  display: flex; align-items: flex-end; padding: 5px 7px; font-size: 11.5px; font-weight: 500; color: var(--ink-2);
}
.bg-sw.dark { color: #ddd; }
.bg-sw.on { outline: 2px solid var(--ink); outline-offset: 2px; }

/* ------------------------------------------------------------ modal / toast */
.modal { border: 0; padding: 0; border-radius: 16px; box-shadow: 0 20px 60px rgba(0,0,0,.25); width: min(520px, calc(100vw - 32px)); }
.modal::backdrop { background: rgba(20, 24, 31, .35); }
.modal-card { padding: 18px 20px 20px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-foot { display: flex; justify-content: flex-end; margin-top: 14px; }
.file-btn { cursor: pointer; border-color: var(--line-strong); }
.project-list { display: grid; gap: 8px; }
.project-row {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--card);
}
.project-row strong { display: block; }
.project-row span { font-size: 12px; color: var(--muted); }
.share-url {
  display: flex; gap: 8px; margin: 8px 0 12px;
}
.share-url input {
  flex: 1; height: 36px; border: 1px solid var(--line-strong); border-radius: 9px; padding: 0 10px; background: #f6f5f2;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px;
}
.modal p { margin: 6px 0; color: var(--ink-2); }
.modal ol { margin: 6px 0; padding-left: 20px; color: var(--ink-2); }
.modal code { background: #f1efeb; padding: 1px 5px; border-radius: 5px; font-size: 12.5px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0;
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13px;
  transition: opacity .2s, transform .2s; pointer-events: none; z-index: 10; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ------------------------------------------------------------ responsive */
@media (max-width: 1180px) {
  .app { grid-template-columns: 228px minmax(0, 1fr) 312px; }
  .lib-item { grid-template-columns: 52px 1fr; }
  .lib-thumb { width: 52px; height: 62px; }
  .hint { display: none; }
}
@media (max-width: 900px) {
  .app, .app[data-mode="client"] {
    display: flex; flex-direction: column; height: auto; min-height: 100dvh;
  }
  .topbar { position: sticky; top: 0; height: 58px; padding: 0 12px; flex: none; }
  .brand-text span { display: none; }
  .topbar-actions .btn { padding: 0 10px; }
  .topbar-actions .btn.ghost { display: none; }
  .library { border-right: 0; border-bottom: 1px solid var(--line); padding: 10px 12px; overflow-x: auto; }
  .library .panel-head, .library-foot, .lib-group { display: none; }
  .library-list { flex-direction: row; gap: 6px; }
  .lib-item { grid-template-columns: 1fr; justify-items: center; width: 92px; flex: none; padding: 6px; text-align: center; }
  .lib-thumb { width: 64px; height: 72px; }
  .lib-meta span { display: none; }
  .lib-meta strong { font-size: 12px; }
  .stage { height: 62vh; min-height: 380px; flex: none; }
  .app[data-mode="client"] .stage { height: calc(100dvh - 58px); }
  .inspector { border-left: 0; padding: 4px 16px 32px; overflow: visible; }
  .stage-info { left: 16px; top: 12px; }
  .stage-info strong { font-size: 15px; }
  .toolbar { bottom: 12px; }
  .seg button { padding: 0 10px; }
  .client-items { bottom: 68px; }
  .project-title { display: none; }
  .app[data-mode="client"] .project-title { display: flex; border-left: 0; padding-left: 0; }
  .app[data-mode="client"] .brand { display: none; }
}
