:root {
  --bg: #121212; --panel: #1a1a1b; --panel2: #222224; --border: #2e2e31; --fg: #f4f4f5; --muted: #9a9aa2;
  --accent: #3b82f6; --red: #e31937; --radius: 12px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); font: 14px/1.4 Inter, system-ui, -apple-system, "Segoe UI", sans-serif; overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { opacity: .35; cursor: default; }
input, select, textarea { font: inherit; color: var(--fg); background: var(--panel2); border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; }
input[type=color] { padding: 2px; width: 40px; height: 30px; }
input[type=range] { padding: 0; accent-color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: #ff6b6b; min-height: 1em; }

.app { display: grid; height: 100vh; grid-template-columns: 180px 1fr 300px; grid-template-rows: 56px 1fr; grid-template-areas: "top top top" "panels stage side"; }
.topbar { grid-area: top; display: flex; align-items: center; gap: 12px; padding: 0 12px; border-bottom: 1px solid var(--border); background: #161617; }
.logo { display: flex; }
.vehicle-picker { display: flex; align-items: center; background: var(--panel2); border: 1px solid var(--border); border-radius: 10px; padding: 2px 4px; }
.vehicle-picker select { background: transparent; border: 0; font-weight: 600; padding: 6px 4px; max-width: 200px; }
.vehicle-picker .sep { width: 1px; height: 20px; background: var(--border); }
.title-wrap { flex: 1; display: flex; justify-content: center; }
.design-name { background: transparent; border: 1px solid transparent; text-align: center; font-weight: 600; font-size: 15px; width: min(320px, 100%); }
.design-name:hover, .design-name:focus { border-color: var(--border); outline: none; }
.top-actions { display: flex; align-items: center; gap: 6px; }
.icon-btn { width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--border); background: var(--panel2); display: grid; place-items: center; }
.icon-btn:hover:not(:disabled) { background: #2c2c30; }
.split-btn { display: flex; gap: 4px; background: var(--panel2); border: 1px solid var(--border); padding: 3px; border-radius: 11px; }
.split-btn .icon-btn { border: 0; height: 30px; }
.btn { border: 1px solid var(--border); background: var(--panel2); padding: 7px 14px; border-radius: 9px; font-weight: 600; }
.btn.light { background: #fff; color: #111; border-color: #fff; }
.btn.light:hover { background: #e6e6e6; }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: #26262a; }
.btn.sm { padding: 4px 10px; font-size: 12px; }
.btn.block { width: 100%; }
.btn.danger, .menu .danger { color: #ff6b6b; }
.lang-btn { border: 0; background: none; color: var(--muted); padding: 6px 8px; }
.lang-btn:hover { color: var(--fg); }

.menu-wrap { position: relative; }
.menu { display: none; position: absolute; top: calc(100% + 6px); right: 0; z-index: 30; min-width: 200px; background: #1d1d20; border: 1px solid var(--border); border-radius: 12px; padding: 6px; box-shadow: 0 16px 40px rgba(0,0,0,.5); }
.menu-wrap.open > .menu { display: block; }
.menu > button, .menu > a { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 8px 10px; border-radius: 8px; color: var(--fg); text-decoration: none; }
.menu > button:hover, .menu > a:hover { background: #2a2a2e; }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 6px 0; }
.menu.wide { width: 290px; left: 0; right: auto; padding: 12px; }
.menu-wrap.up .menu { top: auto; bottom: calc(100% + 8px); left: 0; right: auto; }
.menu.grid { display: none; grid-template-columns: repeat(5, 40px); gap: 6px; min-width: 0; }
.menu-wrap.open > .menu.grid { display: grid; }
.menu.grid button { width: 40px; height: 40px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel2); font-size: 18px; padding: 0; }
.menu.grid button:hover { border-color: var(--accent); }
.menu.grid .pat { background-size: 24px; }
#patternMenu { grid-template-columns: repeat(4, 48px); }
#patternMenu button { width: 48px; height: 48px; }

.panels { grid-area: panels; border-right: 1px solid var(--border); overflow-y: auto; padding: 10px; background: #151516; }
.panel-tile { display: flex; flex-direction: column; align-items: center; gap: 4px; width: 100%; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 8px 4px; font-size: 11px; color: var(--muted); text-align: center; }
.panel-tile canvas { width: 100%; height: auto; max-height: 60px; object-fit: contain; }
.panel-tile.full { flex-direction: column; font-size: 13px; font-weight: 600; color: var(--fg); margin-bottom: 10px; }
.panel-tile.full canvas { max-height: 90px; }
.panel-tile:hover { border-color: #46464c; color: var(--fg); }
.panel-tile.active { border-color: var(--accent); background: #17233a; color: var(--fg); }
.panel-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.stage { grid-area: stage; position: relative; overflow: hidden; background: radial-gradient(circle at 50% 40%, #232326, #161617 70%); }
.stage-head { position: absolute; top: 12px; left: 0; right: 0; display: flex; justify-content: center; align-items: center; gap: 10px; z-index: 5; pointer-events: none; }
.stage-head > * { pointer-events: auto; }
#focusLabel { font-weight: 600; }
.chip-btn { display: flex; align-items: center; gap: 8px; background: var(--panel); border: 1px solid var(--border); border-radius: 9px; padding: 6px 10px; }
.swatch { width: 16px; height: 16px; border-radius: 4px; border: 1px solid #555; }
.swatches { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; margin-bottom: 12px; }
.sw { aspect-ratio: 1; border-radius: 8px; border: 2px solid #333; }
.sw:hover { border-color: #fff; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 8px; color: var(--muted); }
.seg { display: flex; background: var(--panel2); border-radius: 8px; padding: 2px; }
.seg button { border: 0; background: none; padding: 4px 8px; border-radius: 6px; font-size: 12px; color: var(--muted); }
.seg button.active { background: #3a3a40; color: #fff; }
.canvas-host { position: absolute; inset: 0; }
.canvas-host canvas { display: block; touch-action: none; }
.canvas-host.drop { outline: 2px dashed var(--accent); outline-offset: -12px; }
.zoom { position: absolute; right: 14px; bottom: 84px; display: flex; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.zoom button { border: 0; background: none; padding: 6px 10px; }
.zoom button:hover { background: #2a2a2e; }
.toolbar { position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%); display: flex; align-items: center; gap: 4px; background: rgba(28,28,30,.96); border: 1px solid var(--border); border-radius: 14px; padding: 6px; max-width: calc(100% - 24px); flex-wrap: wrap; justify-content: center; }
.toolbar .tb, .toolbar .tool { display: flex; align-items: center; gap: 6px; border: 1px solid transparent; background: none; padding: 7px 10px; border-radius: 9px; white-space: nowrap; }
.toolbar .tb:hover, .toolbar .tool:hover { background: #2a2a2e; }
.toolbar .active { background: #2c3b57; border-color: #3b5b92; }
.toolbar .divider { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }
.brush-opts { display: none; align-items: center; gap: 6px; padding-left: 6px; border-left: 1px solid var(--border); }
.brush-opts.show { display: flex; }
.brush-opts input[type=range] { width: 90px; }
#brushSizeLabel { width: 26px; color: var(--muted); font-size: 12px; }

.side { grid-area: side; border-left: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; padding: 10px; overflow: hidden; background: #151516; }
.preview-card, .props-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); }
.preview-card { display: flex; flex-direction: column; height: 280px; flex-shrink: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 10px; font-weight: 600; border-bottom: 1px solid var(--border); }
.preview-host { position: relative; flex: 1; min-height: 0; background: radial-gradient(circle at 50% 35%, #3a3b40, #18181a 75%); border-radius: 0 0 var(--radius) var(--radius); overflow: hidden; }
.preview-host canvas { display: block; }
.preview-modes { position: absolute; top: 8px; right: 8px; display: flex; flex-direction: column; align-items: flex-end; gap: 6px; z-index: 2; }
.preview-modes button { background: rgba(20,20,22,.85); border: 1px solid var(--border); border-radius: 8px; padding: 4px 8px; font-size: 12px; }
.preview-modes button.on { background: #fff; color: #111; }
body.preview-expanded .preview-card { position: fixed; inset: 70px 20px 20px 200px; height: auto; z-index: 20; box-shadow: 0 30px 80px rgba(0,0,0,.6); }
.props-card { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tabs button { flex: 1; border: 0; background: none; padding: 9px; color: var(--muted); font-weight: 600; }
.tabs button.active { color: var(--fg); box-shadow: inset 0 -2px 0 var(--accent); }
.tab-body { overflow-y: auto; padding: 10px; flex: 1; }
.prop-title { font-weight: 700; margin-bottom: 8px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; color: var(--muted); font-size: 12px; }
.field input:not([type=checkbox]), .field select { width: 100%; }
.field input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.input { width: 100%; margin-bottom: 8px; }
textarea.input { resize: vertical; }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.swatch-row { display: flex; gap: 6px; margin-bottom: 8px; }
.layer-row { display: flex; align-items: center; gap: 6px; padding: 6px; border-radius: 8px; cursor: pointer; }
.layer-row:hover { background: #26262a; }
.layer-row.sel { background: #1d2a44; }
.layer-row .ic { width: 22px; text-align: center; color: var(--muted); }
.layer-row .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mini { border: 0; background: none; padding: 2px 4px; color: var(--muted); font-size: 12px; }
.mini:hover { color: var(--fg); }

.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: grid; place-items: center; z-index: 50; }
.modal { background: #1b1b1e; border: 1px solid var(--border); border-radius: 16px; width: min(520px, calc(100vw - 32px)); max-height: calc(100vh - 60px); display: flex; flex-direction: column; box-shadow: 0 30px 80px rgba(0,0,0,.6); }
.modal.wide { width: min(900px, calc(100vw - 32px)); }
.modal.narrow { width: min(380px, calc(100vw - 32px)); }
.modal.start { width: min(560px, calc(100vw - 32px)); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 8px 16px 16px; overflow-y: auto; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.x { font-size: 16px; }
.start-card { position: relative; display: grid; grid-template-columns: 150px 1fr; gap: 14px; width: 100%; text-align: left; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 10px; margin-bottom: 10px; }
.start-card:hover { border-color: #55555c; }
.start-card h4 { margin: 4px 0 6px; font-size: 15px; }
.start-card p { margin: 6px 0 0; font-size: 12px; }
.sc-img { border-radius: 8px; background: #2a2a2e center/cover no-repeat; min-height: 86px; }
.badge { position: absolute; top: 8px; right: 8px; font-size: 11px; background: #3b3220; color: #f2c46d; padding: 2px 8px; border-radius: 6px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chips span { font-size: 11px; background: #2a2a2e; border-radius: 6px; padding: 2px 7px; color: var(--muted); }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.g-item { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 6px; display: flex; flex-direction: column; gap: 4px; font-size: 12px; }
.g-item img { width: 100%; aspect-ratio: 1; object-fit: contain; background: #2a2a2e; border-radius: 6px; }
.g-item:hover { border-color: var(--accent); }
.keys { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; margin-bottom: 12px; }
.keys div { display: flex; gap: 10px; align-items: center; }
kbd { background: #2a2a2e; border: 1px solid #3a3a40; border-radius: 6px; padding: 2px 6px; font: 12px ui-monospace, monospace; min-width: 70px; text-align: center; }
.multi-img { width: 100%; border-radius: 10px; }
.link-btn { border: 0; background: none; color: var(--accent); margin-top: 10px; padding: 0; }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px); background: #fff; color: #111; padding: 9px 16px; border-radius: 10px; font-weight: 600; opacity: 0; transition: .25s; pointer-events: none; z-index: 60; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 1100px) {
  .app { grid-template-columns: 130px 1fr 260px; }
  .toolbar .tb span:last-child { display: none; }
}
.m-rail, .m-3d, .m-scrim { display: none; }

/* ---- phone editor: full-screen canvas, floating rail, bottom sheets ---- */
@media (max-width: 760px) {
  html, body { overflow: hidden; overscroll-behavior: none; }
  .app { height: 100dvh; width: 100vw; grid-template-columns: minmax(0, 1fr); grid-template-rows: 52px minmax(0, 1fr); grid-template-areas: "top" "stage"; }
  .topbar { gap: 8px; padding: 0 10px; padding-top: env(safe-area-inset-top); min-width: 0; }
  .logo img { width: 26px; height: 26px; }
  .vehicle-picker { flex: 1; min-width: 0; }
  .vehicle-picker select { width: 0; max-width: none; flex: 1 1 0; min-width: 0; font-size: 13px; padding: 6px 2px; text-overflow: ellipsis; }
  .vehicle-picker select:first-child { flex: .8 1 0; }
  .top-actions { flex: none; }
  .title-wrap, #undoBtn, #redoBtn, #helpBtn, .lang-btn, #saveBtn { display: none; }
  .split-btn { padding: 2px; }
  #downloadBtn { padding: 7px 10px; }
  #downloadBtn span { display: none; }
  #downloadBtn::before { content: "⇩"; }
  #userBtn { padding: 7px 8px; font-size: 12px; max-width: 76px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .stage-head { top: 8px; }
  #focusLabel { display: none; }
  .chip-btn { padding: 6px 10px; font-size: 13px; }
  .zoom { bottom: calc(76px + env(safe-area-inset-bottom)); right: 10px; }
  .toolbar { left: 8px; right: 8px; bottom: calc(8px + env(safe-area-inset-bottom)); transform: none; max-width: none; flex-wrap: nowrap; justify-content: flex-start; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .toolbar::-webkit-scrollbar { display: none; }
  .toolbar .tb, .toolbar .tool { flex: none; min-width: 44px; min-height: 44px; justify-content: center; font-size: 18px; padding: 6px 10px; }
  .toolbar .tb span:last-child { display: none; }
  .brush-opts.show { position: fixed; left: 8px; right: 8px; bottom: calc(74px + env(safe-area-inset-bottom)); background: rgba(28,28,30,.97); border: 1px solid var(--border); border-radius: 12px; padding: 8px 12px; }
  .brush-opts input[type=range] { flex: 1; width: auto; }
  .menu-wrap.up .menu { position: fixed; left: 8px; right: 8px; bottom: calc(74px + env(safe-area-inset-bottom)); top: auto; }
  .menu.grid { grid-template-columns: repeat(5, 1fr); }
  #patternMenu { grid-template-columns: repeat(4, 1fr); }
  .menu.grid button, #patternMenu button { width: 100%; height: 48px; }
  .menu.wide { position: fixed; left: 8px; right: 8px; width: auto; top: 100px; }

  .m-rail { display: flex; flex-direction: column; gap: 6px; position: absolute; left: 8px; top: 56px; z-index: 6; background: rgba(24,24,26,.92); border: 1px solid var(--border); border-radius: 14px; padding: 5px; }
  .m-rail button { width: 46px; min-height: 46px; border: 0; background: none; border-radius: 10px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; font-size: 17px; }
  .m-rail button small { font-size: 9px; color: var(--muted); }
  .m-rail button.on { background: #2c3b57; }
  .m-3d { display: grid; place-items: center; position: absolute; right: 10px; bottom: calc(124px + env(safe-area-inset-bottom)); z-index: 6; width: 52px; height: 52px; border-radius: 50%; border: 0; background: #fff; color: #111; font-weight: 800; box-shadow: 0 8px 24px rgba(0,0,0,.5); }

  /* sheets: panels list, layers/properties, 3D preview */
  .panels, .side { position: fixed; z-index: 30; left: 0; right: 0; bottom: 0; border: 0; border-top: 1px solid var(--border); border-radius: 18px 18px 0 0;
    background: #17171a; transform: translateY(105%); transition: transform .22s ease; box-shadow: 0 -20px 50px rgba(0,0,0,.5); padding-bottom: env(safe-area-inset-bottom); }
  .panels { max-height: 52dvh; overflow-y: auto; padding: 14px 12px; }
  .panels::before, .side::before { content: ""; display: block; width: 40px; height: 4px; border-radius: 4px; background: #3a3a40; margin: 0 auto 10px; }
  .panel-tile.full { flex-direction: row; justify-content: center; }
  .panel-tile.full canvas { max-height: 40px; width: auto; }
  .panel-grid { grid-template-columns: repeat(4, 1fr); }
  .side { height: 62dvh; padding: 10px; display: flex; flex-direction: column; overflow: hidden; }
  .side .preview-card { display: none; }
  .props-card { flex: 1; }
  body.m-panels .panels, body.m-layers .side { transform: none; }
  .m-scrim { position: fixed; inset: 0; z-index: 29; background: rgba(0,0,0,.4); }
  body.m-panels .m-scrim, body.m-layers .m-scrim { display: block; }

  /* full-screen 3D */
  body.m-3dview .side { transform: none; height: 100dvh; border-radius: 0; top: 0; padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom); }
  body.m-3dview .side::before { display: none; }
  body.m-3dview .side .preview-card { display: flex; flex: 1; height: auto; border: 0; border-radius: 0; }
  body.m-3dview .side .props-card { display: none; }
  body.m-3dview .m-3d { position: fixed; z-index: 40; top: calc(10px + env(safe-area-inset-top)); bottom: auto; right: 10px; }
  body.m-3dview .m-3d span { font-size: 0; }
  body.m-3dview .m-3d span::before { content: "✕"; font-size: 18px; }
  #expandBtn { display: none; }
  body.preview-expanded .preview-card { inset: 0; }
  .modal { width: 100% !important; max-height: 88dvh; border-radius: 18px 18px 0 0; align-self: end; }
  .modal-back { place-items: end center; }
  input, select, textarea { font-size: 16px; }
}
.ai-modes { margin-bottom: 10px; flex-wrap: wrap; }
.link-btn { color: var(--accent); text-decoration: none; }
