:root {
  --accent: #0a84ff;
  --glass: rgba(255, 255, 255, 0.74);
  --glass-border: rgba(0, 0, 0, 0.08);
  --glass-shadow: 0 10px 40px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.06);
  --text: #1d1d1f;
  --text-2: #6e6e73;
  --hover: rgba(0, 0, 0, 0.06);
  --active: rgba(10, 132, 255, 0.14);
  --track: rgba(0, 0, 0, 0.12);
  --paper: #fbfbf9;
  --radius: 18px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --glass: rgba(34, 34, 38, 0.72);
    --glass-border: rgba(255, 255, 255, 0.1);
    --glass-shadow: 0 10px 40px rgba(0, 0, 0, 0.5), 0 1px 2px rgba(0, 0, 0, 0.4);
    --text: #f5f5f7;
    --text-2: #a1a1a6;
    --hover: rgba(255, 255, 255, 0.08);
    --active: rgba(10, 132, 255, 0.28);
    --track: rgba(255, 255, 255, 0.18);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI Variable", "Segoe UI", Inter, Roboto, sans-serif;
  font-size: 13px; color: var(--text);
  -webkit-font-smoothing: antialiased;
  user-select: none; -webkit-user-select: none;
  background: var(--paper);
}
#app { position: fixed; inset: 0; }
canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#paper { background: var(--paper); }
#live { touch-action: none; }

.glass {
  background: var(--glass);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  backdrop-filter: blur(24px) saturate(180%);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  box-shadow: var(--glass-shadow);
}

button {
  font: inherit; color: inherit; background: none; border: 0; padding: 0; margin: 0; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* Top bar */
.topbar {
  position: absolute; top: max(14px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 6px; padding: 6px 8px 6px 16px; height: 48px;
  max-width: calc(100vw - 32px);
}
.doc-title {
  font: inherit; font-weight: 600; font-size: 14px; letter-spacing: -0.01em; color: var(--text);
  background: none; border: 0; outline: none; padding: 4px 8px; margin: 0 4px 0 -8px; width: 150px; border-radius: 8px;
  text-overflow: ellipsis; transition: background .15s;
}
.doc-title:hover { background: var(--hover); }
.doc-title:focus { background: var(--hover); box-shadow: 0 0 0 2px var(--accent); }
.group { display: flex; align-items: center; gap: 2px; }
.group + .group { margin-left: 6px; padding-left: 8px; border-left: 1px solid var(--glass-border); }
.icon-btn, .tool {
  width: 36px; height: 36px; border-radius: 11px; display: grid; place-items: center;
  transition: background .15s ease, transform .1s ease, opacity .15s ease;
}
.icon-btn:hover, .tool:hover { background: var(--hover); }
.icon-btn:active, .tool:active { transform: scale(0.94); }
.icon-btn:disabled { opacity: .3; pointer-events: none; }
.icon-btn.danger:hover { color: #ff3b30; }
.pill {
  height: 30px; padding: 0 12px; border-radius: 10px; font-variant-numeric: tabular-nums; font-weight: 500; color: var(--text-2);
  transition: background .15s;
}
.pill:hover { background: var(--hover); color: var(--text); }
.pill.small { height: 26px; padding: 0 10px; background: var(--active); color: var(--accent); font-weight: 600; }

.menu-wrap { position: relative; }
.menu {
  position: absolute; top: 44px; right: 0; min-width: 180px; padding: 6px; border-radius: 14px;
  display: flex; flex-direction: column; gap: 2px; z-index: 5;
  animation: pop .16s cubic-bezier(.2,.9,.3,1.2);
}
.menu[hidden] { display: none; }
.menu button { text-align: left; padding: 8px 12px; border-radius: 9px; font-weight: 500; white-space: nowrap; }
.menu button:hover { background: var(--accent); color: #fff; }
.menu button kbd { float: right; margin-left: 16px; font: inherit; font-size: 11px; opacity: .55; }
.menu-sep { height: 1px; margin: 4px 6px; background: var(--glass-border); }
.pill.ghost { background: var(--hover); color: var(--text); }
.icon-btn.active { color: var(--accent); }
.panel-menu { min-width: 224px; padding: 10px; gap: 6px; }
.panel-menu .row { display: flex; justify-content: space-between; align-items: center; padding: 4px 6px; font-weight: 500; cursor: default; }
.panel-menu .row .value { color: var(--text-2); min-width: 0; }
.panel-menu input[type=range].wide { width: 100%; margin: 2px 0 6px; }
.panel-menu .presets { display: flex; gap: 4px; }
.panel-menu .presets button { flex: 1; text-align: center; padding: 6px 0; border-radius: 8px; background: var(--hover); font-weight: 600; }
.panel-menu .presets button:hover { background: var(--active); color: var(--accent); }
.panel-menu .presets button.on { background: var(--accent); color: #fff; }
.switch {
  -webkit-appearance: none; appearance: none; width: 40px; height: 24px; border-radius: 12px; background: var(--track); position: relative;
  margin: 0; cursor: pointer; transition: background .2s; flex: none;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 20px; height: 20px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .2s cubic-bezier(.2,.9,.3,1.2);
}
.switch:checked { background: #34c759; }
.switch:checked::after { transform: translateX(16px); }
@keyframes pop { from { opacity: 0; transform: translateY(-6px) scale(.96); } to { opacity: 1; transform: none; } }

/* Left toolbar */
.toolbar {
  position: absolute; left: max(14px, env(safe-area-inset-left)); top: 50%; transform: translateY(-50%);
  display: flex; flex-direction: column; gap: 4px; padding: 8px;
}
.tool { width: 42px; height: 42px; border-radius: 13px; color: var(--text-2); }
.tool.active { background: var(--active); color: var(--accent); }
.tool.active:hover { background: var(--active); }
.sep { height: 1px; margin: 4px 8px; background: var(--glass-border); }

/* Properties panel */
.props {
  position: absolute; bottom: max(14px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 14px; padding: 10px 16px; height: 54px;
  max-width: calc(100vw - 32px); overflow-x: auto; overflow-y: hidden; scrollbar-width: none;
}
.props::-webkit-scrollbar { display: none; }
.prop { display: flex; align-items: center; gap: 8px; flex: none; }
.prop[hidden] { display: none; }
.divider { width: 1px; height: 26px; background: var(--glass-border); flex: none; }
.label { color: var(--text-2); font-weight: 500; }
.value { min-width: 34px; text-align: right; font-variant-numeric: tabular-nums; color: var(--text-2); }

.colors { gap: 6px; }
.swatch {
  width: 24px; height: 24px; border-radius: 50%; background: var(--c); position: relative; flex: none;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.12);
  transition: transform .15s cubic-bezier(.2,.9,.3,1.4);
}
.swatch:hover { transform: scale(1.12); }
.swatch.active::after {
  content: ""; position: absolute; inset: -4px; border-radius: 50%; border: 2px solid var(--accent);
}
.swatch.custom {
  background: conic-gradient(#ff3b30, #ffcc00, #34c759, #0a84ff, #af52de, #ff3b30);
  cursor: pointer; overflow: hidden; display: block;
}
.swatch.custom input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }
.swatch.custom.active::after { inset: -4px; }

.pill.select {
  -webkit-appearance: none; appearance: none; height: 30px; padding: 0 26px 0 12px; border: 0; background: var(--hover); color: var(--text);
  font: inherit; font-weight: 500; border-radius: 10px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238e8e93' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 8px center; background-size: 12px;
}
.pill.select:focus { outline: none; box-shadow: 0 0 0 2px var(--accent); }
.pill.toggle { height: 30px; width: 30px; padding: 0; background: var(--hover); color: var(--text); font-family: Georgia, "Times New Roman", serif; font-size: 16px; }
.pill.toggle.on { background: var(--accent); color: #fff; }

/* iOS-like sliders */
input[type=range] {
  -webkit-appearance: none; appearance: none; width: 110px; height: 4px; border-radius: 2px;
  background: linear-gradient(var(--accent), var(--accent)) 0/var(--fill, 50%) 100% no-repeat, var(--track);
  outline: none; margin: 0;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.25), 0 0 0 .5px rgba(0,0,0,.08); cursor: grab;
}
input[type=range]::-moz-range-thumb {
  width: 20px; height: 20px; border-radius: 50%; background: #fff; border: 0;
  box-shadow: 0 1px 4px rgba(0,0,0,.25), 0 0 0 .5px rgba(0,0,0,.08); cursor: grab;
}
input[type=range]:active::-webkit-slider-thumb { cursor: grabbing; }

/* First-run privacy hint */
.welcome {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 8;
  width: min(420px, calc(100vw - 32px)); padding: 26px 28px 24px; border-radius: 22px; text-align: center;
  animation: popCenter .22s cubic-bezier(.2,.9,.3,1.2);
}
@keyframes popCenter { from { opacity: 0; transform: translate(-50%, -50%) translateY(-6px) scale(.96); } to { opacity: 1; transform: translate(-50%, -50%); } }
.welcome[hidden] { display: none; }
.welcome-icon {
  width: 52px; height: 52px; margin: 0 auto 12px; border-radius: 16px; display: grid; place-items: center;
  background: var(--active); color: var(--accent);
}
.welcome-icon svg { width: 26px; height: 26px; }
.welcome h2 { margin: 0 0 8px; font-size: 18px; font-weight: 700; letter-spacing: -0.02em; }
.welcome p { margin: 0 0 10px; font-size: 13.5px; line-height: 1.45; color: var(--text); }
.welcome p.muted { color: var(--text-2); font-size: 12.5px; }
.pill.primary { height: 34px; padding: 0 22px; margin-top: 8px; background: var(--accent); color: #fff; font-weight: 600; border-radius: 11px; }
.pill.primary:hover { background: var(--accent); filter: brightness(1.08); color: #fff; }

/* Impressum */
.welcome.impressum { text-align: left; }
.welcome.impressum address { font-style: normal; font-size: 13.5px; line-height: 1.5; margin: 0 0 10px; }
.welcome.impressum a { color: var(--accent); }
.welcome.impressum .pill.primary { display: block; margin-left: auto; }

/* Toast */
.toast {
  position: absolute; bottom: 84px; left: 50%; transform: translateX(-50%);
  padding: 9px 16px; border-radius: 999px; background: rgba(28,28,30,.9); color: #fff; font-weight: 500;
  box-shadow: 0 8px 24px rgba(0,0,0,.2); animation: rise .25s cubic-bezier(.2,.9,.3,1.2); pointer-events: none;
  white-space: nowrap;
}
.toast[hidden] { display: none; }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* Layers panel */
.layers {
  position: absolute; right: max(14px, env(safe-area-inset-right)); top: 50%; transform: translateY(-50%);
  width: 228px; max-height: 70vh; display: flex; flex-direction: column; padding: 10px;
}
.layers[hidden] { display: none; }
.layers-head, .layers-foot { display: flex; align-items: center; gap: 2px; }
.layers-head { justify-content: space-between; padding: 2px 6px 8px 10px; font-weight: 600; }
.layers-foot { padding-top: 8px; margin-top: 6px; border-top: 1px solid var(--glass-border); }
.layers .spacer { flex: 1; }
.icon-btn.small { width: 30px; height: 30px; border-radius: 9px; }
.icon-btn.small svg { width: 17px; height: 17px; }
.layer-list { overflow-y: auto; display: flex; flex-direction: column; gap: 2px; min-height: 36px; }
.layer-row { display: flex; align-items: center; gap: 4px; padding: 4px 6px; border-radius: 10px; cursor: pointer; }
.layer-row:hover { background: var(--hover); }
.layer-row.active { background: var(--active); }
.layer-row .thumb { position: static; inset: auto; width: 44px; height: 28px; border-radius: 5px; background: #fff; box-shadow: inset 0 0 0 1px rgba(0,0,0,.12); flex: none; display: block; }
.layer-row.hidden-layer .thumb { opacity: .45; }
.layer-row .lname { flex: 1; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 3px 6px; border-radius: 6px; }
.layer-row .lname[contenteditable="true"] { background: var(--glass); outline: 2px solid var(--accent); text-overflow: clip; }
.layer-row.hidden-layer .lname { opacity: .45; }
.layer-row .eye, .layer-row .lock { width: 26px; height: 26px; border-radius: 7px; display: grid; place-items: center; color: var(--text-2); flex: none; }
.layer-row .eye svg, .layer-row .lock svg { width: 16px; height: 16px; }
.layer-row .eye:hover, .layer-row .lock:hover { background: var(--hover); color: var(--text); }
.layer-row .lock.on { color: var(--accent); }
.layer-row .eye.off { opacity: .5; }

/* Inline text editor (matches canvas text metrics) */
.text-editor {
  position: absolute; z-index: 4; box-sizing: border-box; margin: 0; border: 0; outline: 1.5px dashed var(--accent); outline-offset: 0;
  background: transparent; resize: none; overflow: hidden; white-space: pre; transform-origin: 0 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI Variable", "Segoe UI", Inter, Roboto, sans-serif; line-height: 1.25;
  min-width: 12px; caret-color: var(--accent);
}
.text-editor[hidden] { display: none; }

/* Shape bar (floats above the selection) */
.shapebar {
  position: absolute; z-index: 3; display: flex; align-items: center; gap: 2px; padding: 4px 6px;
  border-radius: 13px; transform: translate(-50%, -100%); white-space: nowrap; animation: pop .16s cubic-bezier(.2,.9,.3,1.2);
}
.shapebar[hidden] { display: none; }
.shapebar .label { margin: 0 6px; font-size: 12px; }
.shapebar .label[hidden] { display: none; }
.shapebar .bar-sep { width: 1px; height: 18px; margin: 0 4px; background: var(--glass-border); }
.shapebar .bar-sep[hidden] { display: none; }
.shapebar button.danger:hover { background: rgba(255,59,48,.14); color: #ff3b30; }
.shapebar button { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; color: var(--text-2); transition: background .15s; }
.shapebar button:hover { background: var(--active); color: var(--accent); }
.shapebar svg { width: 18px; height: 18px; }
.shapebar .bar-group { display: flex; align-items: center; gap: 2px; }
.shapebar .bar-group[hidden] { display: none; }
.shapebar #rotate3d { width: auto; padding: 0 10px 0 8px; gap: 6px; grid-auto-flow: column; font-weight: 600; font-size: 12px; }
.shapebar button.on { background: var(--accent); color: #fff; }
.shapebar button.on:hover { background: var(--accent); color: #fff; }

/* Library panel */
.overlay {
  position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.18);
  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px); z-index: 10; animation: fade .18s ease;
}
.overlay[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.panel {
  width: min(760px, calc(100vw - 32px)); max-height: calc(100vh - 64px); display: flex; flex-direction: column;
  border-radius: 22px; padding: 18px 20px 20px; animation: pop .2s cubic-bezier(.2,.9,.3,1.2);
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.panel-head h2 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -0.02em; }
.panel-actions { display: flex; align-items: center; gap: 6px; }
.doc-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 14px; overflow-y: auto; padding: 2px;
}
.doc-card {
  position: relative; display: flex; flex-direction: column; gap: 6px; padding: 8px; border-radius: 14px; cursor: pointer;
  border: 2px solid transparent; transition: background .15s, border-color .15s, transform .15s;
}
.doc-card:hover { background: var(--hover); }
.doc-card.current { border-color: var(--accent); }
.doc-card .thumb {
  aspect-ratio: 16 / 10; border-radius: 9px; background: #fff center/cover no-repeat; box-shadow: 0 1px 3px rgba(0,0,0,.15), inset 0 0 0 1px rgba(0,0,0,.06);
  display: grid; place-items: center; color: #bbb; font-size: 12px; overflow: hidden;
}
.doc-card .thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.doc-card .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-card .meta { color: var(--text-2); font-size: 11px; }
.doc-card .del {
  position: absolute; top: 12px; right: 12px; height: 24px; padding: 0 8px; border-radius: 8px; font-size: 11px; font-weight: 600;
  background: rgba(255,255,255,.9); color: #ff3b30; opacity: 0; transition: opacity .15s; box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.doc-card:hover .del, .doc-card .del.confirm { opacity: 1; }
.doc-card .del.confirm { background: #ff3b30; color: #fff; }
.empty { grid-column: 1 / -1; text-align: center; color: var(--text-2); padding: 30px 0; }

.drop-hint {
  position: absolute; inset: 16px; border: 2px dashed var(--accent); border-radius: 24px; display: grid; place-items: center;
  font-size: 18px; font-weight: 600; color: var(--accent); background: rgba(10,132,255,.06); pointer-events: none; z-index: 9;
}
.drop-hint[hidden] { display: none; }

@media (max-width: 720px) {
  .layers { top: auto; bottom: 150px; right: 14px; transform: none; width: 200px; max-height: 40vh; }
  .doc-title { width: 90px; }
  .topbar { padding-left: 8px; }
  .toolbar { top: auto; bottom: 82px; left: 50%; transform: translateX(-50%); flex-direction: row; }
  .sep { width: 1px; height: auto; margin: 8px 4px; }
  .props { gap: 10px; padding: 10px 12px; }
  input[type=range] { width: 80px; }
}
