:root {
  --bg: #fafafa;
  --surface: rgba(255, 255, 255, 0.88);
  --surface-hover: rgba(0, 0, 0, 0.06);
  --surface-active: #18181b;
  --surface-active-fg: #ffffff;
  --border: rgba(0, 0, 0, 0.08);
  --fg: #18181b;
  --fg-muted: #71717a;
  --track: rgba(0, 0, 0, 0.12);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.08), 0 1px 3px rgba(0, 0, 0, 0.05);
  --accent: #4f46e5;
}

html[data-theme="dark"] {
  --bg: #0e1015;
  --surface: rgba(24, 27, 34, 0.88);
  --surface-hover: rgba(255, 255, 255, 0.08);
  --surface-active: #f4f4f5;
  --surface-active-fg: #0e1015;
  --border: rgba(255, 255, 255, 0.1);
  --fg: #f4f4f5;
  --fg-muted: #a1a1aa;
  --track: rgba(255, 255, 255, 0.16);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.3);
  --accent: #818cf8;
}

* {
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  touch-action: none;
}

#gl-canvas, #text-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#gl-canvas {
  cursor: crosshair;
}

#text-canvas {
  pointer-events: none;
}

#editor-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 6;
}

.inline-text-editor {
  position: fixed;
  pointer-events: auto;
  background: rgba(99, 102, 241, 0.04);
  border: 1.5px dashed rgba(99, 102, 241, 0.75);
  border-radius: 6px;
  outline: none;
  resize: none;
  overflow: hidden;
  padding: 2px 6px;
  margin: -3px 0 0 -7px;
  min-width: 90px;
  min-height: 1.35em;
  line-height: 1.32;
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  white-space: pre;
  user-select: text;
  -webkit-user-select: text;
  box-shadow: 0 2px 12px rgba(99, 102, 241, 0.14);
}
.inline-text-editor::placeholder {
  color: var(--fg-muted);
  opacity: 0.65;
}

/* Selected Image Overlay */
.image-overlay {
  position: fixed;
  z-index: 8;
  pointer-events: none;
  border: 1.5px solid var(--accent);
  border-radius: 4px;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.35);
}

.image-overlay.hidden {
  display: none;
}

.image-controls {
  position: absolute;
  top: -44px;
  left: 50%;
  transform: translateX(-50%);
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 10px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  white-space: nowrap;
}

.ctrl-icon {
  width: 14px;
  height: 14px;
  color: var(--fg-muted);
  flex-shrink: 0;
}

#img-opacity-slider {
  width: 78px;
  accent-color: var(--accent);
  cursor: pointer;
  margin: 0;
}

.img-opacity-val {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--fg);
  min-width: 30px;
}

.img-mode-pill {
  font-size: 10.5px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(99, 102, 241, 0.12);
  color: var(--accent);
}

.img-mode-pill.under {
  background: rgba(245, 158, 11, 0.15);
  color: #d97706;
}

.img-ctrl-del {
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--fg-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}

.img-ctrl-del svg {
  width: 13px;
  height: 13px;
}

.img-ctrl-del:hover {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.12);
}

.image-resize-handle {
  position: absolute;
  right: -7px;
  bottom: -7px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
  pointer-events: auto;
  cursor: nwse-resize;
}

/* Floating Glassmorphic Toolbar */
.toolbar {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  max-width: calc(100vw - 24px);
}

.tool-group, .color-group, .action-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.divider {
  width: 1px;
  height: 22px;
  background: var(--border);
  margin: 0 4px;
  flex-shrink: 0;
}

.tool-btn, .icon-btn {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--fg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.14s ease, transform 0.1s ease, color 0.14s ease;
  flex-shrink: 0;
}

.tool-btn svg, .icon-btn svg {
  width: 17px;
  height: 17px;
}

.tool-btn:hover, .icon-btn:hover {
  background: var(--surface-hover);
}

.tool-btn:active, .icon-btn:active {
  transform: scale(0.93);
}

.tool-btn.active {
  background: var(--surface-active);
  color: var(--surface-active-fg);
}

.icon-btn.danger:hover {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}

/* Swatches */
.swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid transparent;
  background: var(--sw);
  cursor: pointer;
  padding: 0;
  transition: transform 0.14s ease, box-shadow 0.14s ease;
  flex-shrink: 0;
  position: relative;
}

.swatch:hover {
  transform: scale(1.14);
}

.swatch.active {
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--fg);
}

.custom-swatch {
  background: conic-gradient(#ef4444, #f59e0b, #10b981, #3b82f6, #8b5cf6, #ec4899, #ef4444);
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.custom-swatch input[type="color"] {
  opacity: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
  border: none;
  padding: 0;
}

/* Tapered Isosceles Trapezoid Brush Size Slider */
.size-slider-wrap {
  position: relative;
  width: 98px;
  height: 28px;
  display: flex;
  align-items: center;
  cursor: ew-resize;
  flex-shrink: 0;
  outline: none;
}

.size-wedge-svg {
  width: 100%;
  height: 22px;
  display: block;
  overflow: visible;
}

.wedge-track {
  fill: var(--track);
}

.wedge-fill {
  fill: var(--fg);
  opacity: 0.35;
}

.size-thumb {
  position: absolute;
  top: 50%;
  left: 20px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--fg);
  border: 2px solid var(--bg);
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.28);
  transform: translate(-50%, -50%);
  pointer-events: none;
  transition: box-shadow 0.12s ease;
}

.size-slider-wrap.dragging .size-thumb {
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.35), 0 2px 8px rgba(0, 0, 0, 0.32);
}

.size-bubble {
  position: absolute;
  top: 38px;
  left: 50%;
  transform: translateX(-50%) translateY(-4px);
  padding: 6px 10px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  display: flex;
  align-items: center;
  gap: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.14s ease, transform 0.14s ease;
  white-space: nowrap;
}

.size-slider-wrap.dragging .size-bubble {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.size-bubble-dot {
  border-radius: 50%;
  background: var(--fg);
  display: inline-block;
  flex-shrink: 0;
}

.size-bubble-label {
  font-size: 11.5px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--fg);
}

/* Top-Right Utility Bar */
.utility-bar {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

html[data-theme="light"] .icon-sun { display: none; }
html[data-theme="dark"] .icon-moon { display: none; }

/* Bottom-Right Zoom & Coordinates Pill */
.zoom-hud {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px 8px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.hud-btn {
  border: none;
  background: transparent;
  color: var(--fg);
  border-radius: 8px;
  height: 26px;
  min-width: 26px;
  padding: 0 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.14s ease;
}

.hud-btn:hover {
  background: var(--surface-hover);
}

.zoom-readout {
  min-width: 48px;
  font-size: 12px;
}

.coords {
  color: var(--fg-muted);
  font-size: 11px;
  padding-left: 6px;
  margin-left: 4px;
  border-left: 1px solid var(--border);
  min-width: 72px;
  text-align: right;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 64px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  z-index: 30;
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--surface-active);
  color: var(--surface-active-fg);
  font-size: 12.5px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  box-shadow: var(--shadow);
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 768px) {
  .toolbar {
    top: 10px;
    padding: 5px 8px;
    gap: 4px;
  }
  .utility-bar {
    top: auto;
    bottom: 14px;
    left: 14px;
    right: auto;
  }
  .coords {
    display: none;
  }
}
