/* ══════════════════════════════════════════════════════════
   RESET & CSS VARIABLES
══════════════════════════════════════════════════════════ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-canvas:          #f0ece4;
  --bg-toolbar:         #ffffff;
  --bg-toolbar-2:       #f5f5f5;
  --bg-active:          #e8f0fe;
  --border:             #d0d0d0;
  --text-primary:       #1a1a1a;
  --text-secondary:     #555555;
  --accent:             #3a7bfd;
  --accent-hover:       #2563eb;
  --shadow:             0 2px 8px rgba(0,0,0,.12);
  --shadow-heavy:       0 4px 20px rgba(0,0,0,.18);
  --radius:             10px;
  --radius-sm:          6px;
  --toolbar-width:      64px;
  --toolbar-expanded:   224px;
  --layers-panel-width: 220px;
  --transition:         180ms ease;
}
[data-theme="dark"] {
  --bg-canvas:        #1a1a2e;
  --bg-toolbar:       #16213e;
  --bg-toolbar-2:     #0f2a4a;
  --bg-active:        #264a7a;   /* WCAG: was #1e3a5f, raised for clearer active state */
  --border:           #2a3a5c;
  --text-primary:     #e8e8f0;
  --text-secondary:   #a8a8c8;   /* WCAG AA: was #9090b0 (3.8:1) → #a8a8c8 (~5.0:1) */
  --accent:           #4d8aff;
  --accent-hover:     #6ba0ff;
  --shadow:           0 2px 8px rgba(0,0,0,.4);
  --shadow-heavy:     0 4px 20px rgba(0,0,0,.6);
}

html, body {
  width: 100%; height: 100%; overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 14px; color: var(--text-primary); background: var(--bg-canvas);
  -webkit-user-select: none; user-select: none;
}

/* ══════════════════════════════════════════════════════════
   APP SHELL
══════════════════════════════════════════════════════════ */
#app { display: flex; width: 100%; height: 100dvh; min-height: 100vh; position: relative; }
/* Reserve a usable canvas lane (incl. the float-bar) so expanded plugin columns
   can't squeeze the drawing area to zero. Plugin columns yield space + scroll instead. */
#canvas-area { min-width: 320px; }

/* ══════════════════════════════════════════════════════════
   PLUGIN COLUMNS — one extra column per plugin with placement=column
   Collapsed (icon-only) by default. The container yields space and scrolls
   horizontally so expanded columns never squeeze the canvas/float-bar lane.
══════════════════════════════════════════════════════════ */
#plugin-columns {
  display: flex; flex-direction: row;
  height: 100%; flex-shrink: 1; min-width: 0;
  overflow-x: auto; overflow-y: hidden;
}
#plugin-columns:empty { display: none; }

.plugin-column {
  display: flex; flex-direction: column;
  width: var(--toolbar-expanded); height: 100%;
  background: var(--bg-toolbar); border-right: 1px solid var(--border);
  box-shadow: var(--shadow); z-index: 95;
  overflow: hidden; flex-shrink: 0;
  transition: width var(--transition);
  animation: plugin-column-in 200ms ease-out;
}
.plugin-column.collapsed { width: var(--toolbar-width); }
@keyframes plugin-column-in {
  from { width: 0; opacity: 0; }
  to   { width: var(--toolbar-expanded); opacity: 1; }
}
.plugin-column-header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
  min-height: 56px; flex-shrink: 0;
}
.plugin-column.collapsed .plugin-column-header {
  padding: 12px 6px; gap: 4px; justify-content: center;
}
.plugin-column-icon {
  width: 28px; height: 28px; display: flex; align-items: center;
  justify-content: center; font-size: 18px; flex-shrink: 0;
}
.plugin-column-title {
  font-weight: 700; font-size: 14px; color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  flex: 1;
  transition: opacity var(--transition), width var(--transition);
}
.plugin-column.collapsed .plugin-column-title {
  opacity: 0; width: 0; flex: 0;
}
.plugin-column-toggle {
  background: none; border: none; cursor: pointer;
  color: var(--text-secondary); font-size: 18px; line-height: 1;
  padding: 4px 6px; border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
  flex-shrink: 0;
}
.plugin-column-toggle:hover { background: var(--bg-toolbar-2); color: var(--accent); }
.plugin-column-scroll {
  flex: 1; overflow-y: auto; overflow-x: hidden;
}
.plugin-column-scroll::-webkit-scrollbar { width: 3px; }
.plugin-column-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }
/* When expanded: tool buttons show their label (like the expanded sidebar) */
.plugin-column:not(.collapsed) .tool-btn .label { opacity: 1; width: auto; }
.plugin-column:not(.collapsed) .section-label { opacity: 1; height: auto; }
/* When collapsed: behave like the collapsed sidebar — icons only, tooltips on hover */
.plugin-column.collapsed .tool-btn .label { opacity: 0; width: 0; }
.plugin-column.collapsed .section-label { opacity: 0; height: 0; }
.plugin-column.collapsed .tool-btn[data-tooltip]:hover::after { opacity: 1; }

/* ══════════════════════════════════════════════════════════
   SIDEBAR
══════════════════════════════════════════════════════════ */
#sidebar {
  display: flex; flex-direction: column;
  width: var(--toolbar-width); height: 100%;
  background: var(--bg-toolbar); border-right: 1px solid var(--border);
  box-shadow: var(--shadow); z-index: 100;
  transition: width var(--transition);
  overflow: hidden; flex-shrink: 0;
}
#sidebar.expanded { width: var(--toolbar-expanded); }

#sidebar-header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 10px; border-bottom: 1px solid var(--border); min-height: 56px;
}
#app-logo {
  width: 36px; height: 36px; background: var(--accent);
  border-radius: var(--radius-sm); display: flex; align-items: center;
  justify-content: center; color: #fff; font-weight: 800; font-size: 16px; flex-shrink: 0;
}
#app-title {
  font-weight: 700; font-size: 15px; color: var(--text-primary);
  white-space: nowrap; opacity: 0; transition: opacity var(--transition);
}
#sidebar.expanded #app-title { opacity: 1; }

#sidebar-scroll { flex: 1; overflow-y: auto; overflow-x: hidden; }
#sidebar-scroll::-webkit-scrollbar { width: 3px; }
#sidebar-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

#toggle-sidebar {
  display: flex; align-items: center; justify-content: center;
  width: 100%; height: 48px; background: none; border: none;
  border-top: 1px solid var(--border); cursor: pointer;
  color: var(--text-secondary); font-size: 18px; flex-shrink: 0;
  transition: background var(--transition), color var(--transition);
}
#toggle-sidebar:hover { background: var(--bg-toolbar-2); color: var(--accent); }
#toggle-sidebar svg { transition: transform var(--transition); }
#sidebar.expanded #toggle-sidebar svg { transform: rotate(180deg); }

.tool-section { padding: 6px 8px; border-bottom: 1px solid var(--border); }
.section-label {
  font-size: 10px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-secondary);
  padding: 4px 4px 6px; white-space: nowrap;
  opacity: 0; height: 0; overflow: hidden;
  transition: opacity var(--transition), height var(--transition);
}
#sidebar.expanded .section-label { opacity: 1; height: auto; }
.tool-group { display: flex; flex-direction: column; gap: 2px; }

.tool-btn {
  display: flex; align-items: center; gap: 10px;
  width: 100%; min-height: 48px; padding: 8px;
  background: none; border: 2px solid transparent;
  border-radius: var(--radius-sm); cursor: pointer;
  color: var(--text-primary); position: relative; overflow: hidden;
  transition: background var(--transition), border-color var(--transition), color var(--transition);
}
.tool-btn:hover { background: var(--bg-toolbar-2); color: var(--accent); }
.tool-btn.active { background: var(--bg-active); border-color: var(--accent); color: var(--accent); }
.tool-btn .icon {
  width: 30px; height: 30px; display: flex; align-items: center;
  justify-content: center; font-size: 20px; flex-shrink: 0; line-height: 1;
}
.tool-btn .icon svg {
  width: 22px; height: 22px; display: block;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}
.tool-btn .label {
  font-size: 13px; font-weight: 500; white-space: nowrap;
  opacity: 0; width: 0; transition: opacity var(--transition), width var(--transition);
}
#sidebar.expanded .tool-btn .label { opacity: 1; width: auto; }

/* Submenu chevron — indicates the button has hidden options (long-press / dbl-click) */
.tool-btn.has-submenu .submenu-chevron {
  position: absolute; top: 3px; right: 4px;
  font-size: 10px; line-height: 1; color: var(--accent);
  opacity: 0.65; pointer-events: none;
  transition: opacity 120ms ease, transform 120ms ease;
}
.tool-btn.has-submenu:hover .submenu-chevron { opacity: 1; transform: translateY(1px); }
.tool-btn.has-submenu.active .submenu-chevron { opacity: 1; }

.tool-btn[data-tooltip]::after {
  content: attr(data-tooltip); position: absolute; left: calc(100% + 10px); top: 50%;
  transform: translateY(-50%); background: var(--text-primary); color: var(--bg-toolbar);
  font-size: 12px; padding: 4px 8px; border-radius: var(--radius-sm);
  white-space: nowrap; pointer-events: none;
  opacity: 0; transition: opacity 120ms ease; z-index: 200;
}
#sidebar:not(.expanded) .tool-btn[data-tooltip]:hover::after { opacity: 1; }

.ripple {
  position: absolute; border-radius: 50%; background: var(--accent);
  opacity: .25; pointer-events: none; transform: scale(0);
  animation: ripple-anim 400ms ease-out forwards;
}
@keyframes ripple-anim { to { transform: scale(4); opacity: 0; } }

/* Sub-options (eraser mode, font size) */
.sub-options {
  display: flex; gap: 3px; padding: 0 4px 4px;
  overflow: hidden; max-height: 0; opacity: 0;
  transition: max-height 200ms ease, opacity 200ms ease;
}
#sidebar.expanded .sub-options { max-height: 44px; opacity: 1; }
.sub-btn {
  flex: 1; height: 36px; background: var(--bg-toolbar-2);
  border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer; font-size: 11px; font-weight: 600;
  color: var(--text-secondary); transition: all 150ms ease;
}
.sub-btn:hover { background: var(--bg-active); color: var(--accent); }
.sub-btn.active { border-color: var(--accent); background: var(--bg-active); color: var(--accent); }

/* Color panel */
#color-panel { padding: 8px; border-bottom: 1px solid var(--border); }
#current-color-row { display: flex; align-items: center; gap: 6px; padding: 4px 4px 6px; }
#current-color-dot {
  width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid var(--border); background: #222222;
  cursor: pointer; flex-shrink: 0; transition: transform 100ms ease;
}
#current-color-dot:hover { transform: scale(1.1); }
#current-color-label {
  font-size: 11px; color: var(--text-secondary); flex: 1;
  opacity: 0; transition: opacity var(--transition); overflow: hidden;
}
#sidebar.expanded #current-color-label { opacity: 1; }
#hex-input {
  width: 0; padding: 0; border: none; background: none;
  font-size: 11px; color: var(--text-primary); opacity: 0;
  transition: all var(--transition); outline: none;
}
#sidebar.expanded #hex-input {
  width: 70px; padding: 3px 5px;
  background: var(--bg-toolbar-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); opacity: 1;
}
.color-swatches {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px;
  overflow: hidden; max-height: 0; transition: max-height 200ms ease;
}
#sidebar.expanded .color-swatches { max-height: 200px; }
.color-swatch {
  width: 100%; aspect-ratio: 1; border-radius: 50%;
  border: 2px solid transparent; cursor: pointer;
  transition: transform 100ms ease, border-color 100ms ease;
}
.color-swatch:hover { transform: scale(1.15); }
.color-swatch.active { border-color: var(--accent); transform: scale(1.1); }

#opacity-row {
  display: flex; align-items: center; gap: 8px; padding: 6px 4px 2px;
  overflow: hidden; max-height: 0; opacity: 0;
  transition: max-height 200ms ease, opacity 200ms ease;
}
#sidebar.expanded #opacity-row { max-height: 32px; opacity: 1; }
#opacity-label { font-size: 11px; color: var(--text-secondary); white-space: nowrap; }
#opacity-slider { flex: 1; height: 3px; cursor: pointer; accent-color: var(--accent); }
#opacity-value { font-size: 11px; color: var(--text-secondary); width: 28px; text-align: right; }

.stroke-group {
  display: flex; gap: 4px; padding: 4px;
  overflow: hidden; max-height: 0; transition: max-height 200ms ease;
}
#sidebar.expanded .stroke-group { max-height: 110px; flex-wrap: wrap; }
.stroke-btn {
  flex: 0 0 calc(25% - 3px); height: 44px; background: var(--bg-toolbar-2);
  border: 2px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: border-color var(--transition), background var(--transition);
  color: var(--text-primary);
}
.stroke-btn:hover { background: var(--bg-active); }
.stroke-btn.active { border-color: var(--accent); background: var(--bg-active); }
/* Línies visuals a la sidebar per a cada gruix */
.stroke-btn::before {
  content: ''; display: block; width: 60%; border-radius: 4px;
  background: var(--text-primary); height: 2px;
}
.stroke-btn[data-stroke="medium"]::before { height: 5px; }
.stroke-btn[data-stroke="thick"]::before  { height: 9px; }
.stroke-btn[data-stroke="xl"]::before     { height: 13px; }
.stroke-btn[data-stroke="xxl"]::before    { height: 18px; }
.stroke-btn[data-stroke="xxxl"]::before   { height: 24px; }
.stroke-btn[data-stroke="huge"]::before   { height: 30px; }
.stroke-btn[data-stroke="mega"]::before   { height: 36px; border-radius: 3px; }
#color-picker-input { display: none; }

/* ══════════════════════════════════════════════════════════
   CANVAS AREA
══════════════════════════════════════════════════════════ */
#canvas-area { flex: 1; position: relative; overflow: hidden; background: var(--bg-canvas); touch-action: none; }
#canvas-transform { position: absolute; top: 0; left: 0; transform-origin: 0 0; will-change: transform; }
#canvas-main { display: block; box-shadow: var(--shadow-heavy); }
#canvas-overlay { position: absolute; top: 0; left: 0; pointer-events: none; }

/* ══════════════════════════════════════════════════════════
   STATUS BAR
══════════════════════════════════════════════════════════ */
#status-bar {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  background: var(--bg-toolbar); border: 1px solid var(--border);
  border-radius: 20px; padding: 4px 14px;
  display: flex; align-items: center; gap: 12px;
  font-size: 12px; color: var(--text-secondary);
  box-shadow: var(--shadow); z-index: 50; pointer-events: none; opacity: .8;
}
#status-bar span { white-space: nowrap; }
#user-indicator { pointer-events: auto; }

/* Autosave chip (status bar) ─────────────────────────────────── */
#autosave-chip {
  pointer-events: auto;
  font-size: 11px; padding: 2px 8px; border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-toolbar-2);
  color: var(--text-secondary);
  display: inline-flex; align-items: center; gap: 4px;
}
#autosave-chip[data-status="saving"],
#autosave-chip[data-status="pending"] {
  color: var(--accent); border-color: var(--accent);
  animation: autosave-pulse 1.4s ease-in-out infinite;
}
#autosave-chip[data-status="saved"]    { color: #4ade80; border-color: #4ade8055; }
#autosave-chip[data-status="failed"],
#autosave-chip[data-status="toolarge"] { color: #f87171; border-color: #f8717155; }
@keyframes autosave-pulse {
  0%, 100% { opacity: 0.7; }
  50%      { opacity: 1; }
}

/* Recovery banner (top of canvas area, shown only when local autosave has data) */
#recovery-banner {
  position: absolute; top: 56px; left: 50%; transform: translateX(-50%);
  background: var(--bg-toolbar); border: 1px solid var(--accent);
  border-radius: var(--radius); padding: 10px 14px;
  display: flex; align-items: center; gap: 10px;
  box-shadow: var(--shadow-heavy); z-index: 60;
  font-size: 13px; color: var(--text-primary);
  pointer-events: auto;
  animation: modal-pop 220ms cubic-bezier(.2,.8,.2,1);
}
/* Specificity fix: ID rule above defines display:flex, which would beat
   .hidden-initially (display:none). Override explicitly. */
#recovery-banner.hidden-initially,
#autosave-chip.hidden-initially { display: none; }
#recovery-banner button {
  font-size: 12px; padding: 4px 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg-toolbar-2);
  color: var(--text-primary); cursor: pointer;
}
#recovery-banner #recovery-yes {
  border-color: var(--accent); color: var(--accent);
}
#recovery-banner button:hover { background: var(--bg-active); }

/* ══════════════════════════════════════════════════════════
   FLOAT BAR
══════════════════════════════════════════════════════════ */
#float-bar {
  position: absolute; bottom: 16px; right: 16px;
  display: flex; flex-flow: column wrap-reverse;
  justify-content: flex-end; align-content: flex-end;
  gap: 6px 12px; z-index: 100;
  max-height: calc(100dvh - 24px);
}
/* Buttons participate directly in the float-bar flex layout (display:contents),
   so only the exact buttons that don't fit reflow into a new column — not whole
   groups. The group wrappers stay in the DOM purely for semantics. */
.fb-group { display: contents; }
/* Reduce button size when vertical room is tight */
@media (max-height: 760px) {
  #float-bar .float-btn { width: 40px; height: 40px; font-size: 16px; }
  #float-bar { gap: 4px 10px; bottom: 10px; right: 10px; max-height: calc(100dvh - 20px); }
  .fb-group { gap: 6px; }
  .fb-group + .fb-group { padding-top: 9px; }
}
@media (max-height: 560px) {
  #float-bar .float-btn { width: 34px; height: 34px; font-size: 14px; }
  #float-bar { gap: 3px; }
  .fb-group { gap: 5px; }
  .fb-group + .fb-group { padding-top: 7px; }
}
.float-btn {
  width: 48px; height: 48px; background: var(--bg-toolbar);
  border: 1px solid var(--border); border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 18px; box-shadow: var(--shadow);
  color: var(--text-primary);
  transition: background var(--transition), color var(--transition), transform 100ms ease;
}
.float-btn:hover { background: var(--bg-active); color: var(--accent); transform: scale(1.08); }
.float-btn:active { transform: scale(.95); }
.float-btn.active { background: var(--bg-active); color: var(--accent); border-color: var(--accent); }
/* Coherent monochrome SVG icons (scale with button font-size) */
.float-btn svg {
  width: 1.2em; height: 1.2em; display: block; pointer-events: none;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}
/* Theme toggle: moon in light mode, sun in dark mode */
#btn-theme .ic-sun { display: none; }
:root[data-theme="dark"] #btn-theme .ic-moon { display: none; }
:root[data-theme="dark"] #btn-theme .ic-sun { display: block; }
/* Touch toggle: hand by default (auto/on), mouse when explicitly off */
#btn-touch-mode .ic-mouse { display: none; }
body.no-touch-mode #btn-touch-mode .ic-hand { display: none; }
body.no-touch-mode #btn-touch-mode .ic-mouse { display: block; }
.float-btn.touch-auto { border-style: dashed; }
.float-btn.touch-auto.active { background: var(--bg-active); color: var(--accent); border-color: var(--accent); border-style: dashed; }

/* ══════════════════════════════════════════════════════════
   EXPORT PANEL
══════════════════════════════════════════════════════════ */
#export-panel {
  display: none; position: absolute; bottom: 16px; right: 72px;
  background: var(--bg-toolbar); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px;
  box-shadow: var(--shadow-heavy); z-index: 200;
  flex-direction: column; gap: 4px; min-width: 170px;
}
#export-panel.open { display: flex; }
.export-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  cursor: pointer; background: none; border: none;
  font-size: 13px; color: var(--text-primary); text-align: left;
  transition: background var(--transition);
}
.export-item:hover { background: var(--bg-toolbar-2); }
.export-item .eicon { font-size: 18px; width: 24px; text-align: center; }

/* ══════════════════════════════════════════════════════════
   TEXT EDITOR OVERLAY
══════════════════════════════════════════════════════════ */
#text-editor-wrap { display: none; position: absolute; z-index: 300; pointer-events: all; }
#text-editor {
  background: transparent; border: 1.5px dashed var(--accent); outline: none;
  resize: none; font-family: inherit; color: var(--text-primary);
  padding: 2px 4px; min-width: 80px; min-height: 30px; overflow: hidden; line-height: 1.3;
}
[data-theme="dark"] #text-editor { color: #e8e8f0; }

/* ══════════════════════════════════════════════════════════
   LASER POINTER
══════════════════════════════════════════════════════════ */
#laser-dot {
  display: none; position: absolute; width: 16px; height: 16px;
  border-radius: 50%; pointer-events: none; z-index: 500;
  transform: translate(-50%, -50%);
  background: rgba(255,30,30,.9);
  box-shadow: 0 0 0 3px rgba(255,30,30,.3), 0 0 12px 4px rgba(255,30,30,.5);
  animation: laser-pulse 1s ease-in-out infinite;
}
@keyframes laser-pulse {
  0%,100% { box-shadow: 0 0 0 3px rgba(255,30,30,.3), 0 0 12px 4px rgba(255,30,30,.5); }
  50%      { box-shadow: 0 0 0 6px rgba(255,30,30,.15), 0 0 20px 8px rgba(255,30,30,.7); }
}
body.laser-active #laser-dot { display: block; }
body.laser-active * { cursor: none !important; }

/* ══════════════════════════════════════════════════════════
   PRESENTATION MODE
══════════════════════════════════════════════════════════ */
body.presentation-mode #sidebar,
body.presentation-mode #status-bar { display: none; }
#presentation-bar {
  display: none; position: absolute; bottom: 20px; left: 50%;
  transform: translateX(-50%); background: rgba(0,0,0,.75);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border-radius: 30px; padding: 8px 16px; gap: 10px; z-index: 200;
}
body.presentation-mode #presentation-bar { display: flex; }
.pres-btn {
  width: 44px; height: 44px; border-radius: 50%;
  background: rgba(255,255,255,.15); border: none;
  color: #fff; font-size: 20px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 150ms ease;
}
.pres-btn:hover { background: rgba(255,255,255,.3); }
.pres-btn.active { background: rgba(255,80,80,.6); }

/* ══════════════════════════════════════════════════════════
   MODAL
══════════════════════════════════════════════════════════ */
#modal-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.45); backdrop-filter: blur(3px);
  z-index: 500; align-items: center; justify-content: center;
}
#modal-overlay.open { display: flex; }
.modal-box {
  background: var(--bg-toolbar); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px; min-width: 280px;
  max-width: 90vw; box-shadow: var(--shadow-heavy);
  display: flex; flex-direction: column; gap: 16px;
}
.modal-title { font-size: 16px; font-weight: 700; color: var(--text-primary); }
.modal-body  { font-size: 14px; color: var(--text-secondary); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
.btn {
  padding: 9px 18px; border-radius: var(--radius-sm);
  font-size: 13px; font-weight: 600; cursor: pointer; border: none;
  transition: opacity 150ms ease, transform 100ms ease;
}
.btn:active { transform: scale(.97); }
.btn-ghost { background: var(--bg-toolbar-2); border: 1px solid var(--border); color: var(--text-primary); }
.btn-ghost:hover { background: var(--bg-active); }
.btn-danger { background: #ef4444; color: #fff; }
.btn-danger:hover { background: #dc2626; }

/* ══════════════════════════════════════════════════════════
   TOOL OPTIONS MODAL (double-tap / dblclick)
══════════════════════════════════════════════════════════ */
#tool-modal-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.5); backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 600; align-items: center; justify-content: center;
}
#tool-modal-overlay.open { display: flex; }

#tool-modal {
  background: var(--bg-toolbar); border: 1px solid var(--border);
  border-radius: 20px; padding: 22px 24px;
  width: min(460px, 94vw); max-height: 90vh;
  overflow-y: auto; box-shadow: var(--shadow-heavy);
  display: flex; flex-direction: column; gap: 20px;
  animation: modal-pop 200ms cubic-bezier(.34,1.5,.64,1) forwards;
}
@keyframes modal-pop {
  from { transform: scale(.82) translateY(20px); opacity: 0; }
  to   { transform: scale(1)   translateY(0);    opacity: 1; }
}
/* Unified entrance animation for all modal boxes (consistency across panels) */
#auth-modal, #designs-modal,
#sym-modal, #fn-modal, #poly-modal, #transform-modal, #axes-modal,
#plugin-help-modal, #m3d-modal, #plugins-modal {
  animation: modal-pop 200ms cubic-bezier(.34,1.5,.64,1) forwards;
}
#tool-modal::-webkit-scrollbar { width: 4px; }
#tool-modal::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

#tmo-header {
  display: flex; align-items: center; gap: 12px;
  padding-bottom: 16px; border-bottom: 1px solid var(--border);
}
#tmo-icon {
  width: 44px; height: 44px; background: var(--bg-active);
  border: 1.5px solid var(--border); border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 24px; flex-shrink: 0;
}
#tmo-title { flex: 1; font-size: 17px; font-weight: 700; color: var(--text-primary); }
#tmo-close {
  width: 34px; height: 34px; border-radius: 50%; border: none;
  background: var(--bg-toolbar-2); cursor: pointer;
  color: var(--text-secondary); font-size: 15px;
  display: flex; align-items: center; justify-content: center;
  transition: background 150ms, color 150ms;
}
#tmo-close:hover { background: #ef4444; color: #fff; }

.tmo-section { display: flex; flex-direction: column; gap: 10px; }
.tmo-label {
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .07em; color: var(--text-secondary);
}

/* Colour swatches – 6 per row (larger than sidebar) */
#tmo-swatches {
  display: grid; grid-template-columns: repeat(6, 1fr); gap: 9px;
}
.tmo-swatch {
  width: 100%; aspect-ratio: 1; border-radius: 50%;
  border: 2.5px solid transparent; cursor: pointer;
  transition: transform 120ms ease, border-color 120ms ease;
  box-shadow: 0 1px 3px rgba(0,0,0,.15);
}
.tmo-swatch:hover  { transform: scale(1.2); }
.tmo-swatch.active { border-color: var(--accent); transform: scale(1.12);
                     box-shadow: 0 0 0 3px rgba(58,123,253,.2); }

#tmo-color-row {
  display: flex; align-items: center; gap: 10px;
}
#tmo-color-dot {
  width: 44px; height: 44px; border-radius: 50%;
  border: 2px solid var(--border); cursor: pointer;
  flex-shrink: 0; transition: transform 100ms;
}
#tmo-color-dot:hover { transform: scale(1.1); }
#tmo-hex {
  flex: 1; padding: 9px 12px;
  background: var(--bg-toolbar-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); font-size: 14px;
  color: var(--text-primary); outline: none; font-family: monospace;
}
#tmo-hex:focus { border-color: var(--accent); }
#tmo-native-picker-modal { display: none; }

#tmo-opacity-row {
  display: flex; align-items: center; gap: 10px;
}
.tmo-range-label { font-size: 12px; color: var(--text-secondary); white-space: nowrap; }
#tmo-opacity { flex: 1; accent-color: var(--accent); cursor: pointer; }
#tmo-opacity-val { font-size: 12px; color: var(--text-secondary); width: 38px; text-align: right; }

/* Stroke / eraser size – 3 wide buttons with SVG preview */
#tmo-strokes { display: grid; grid-template-columns: repeat(4,1fr); gap: 8px; }
.tmo-stroke {
  height: 60px; background: var(--bg-toolbar-2);
  border: 2px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  color: var(--text-primary);
  transition: border-color 150ms, background 150ms;
}
.tmo-stroke:hover   { background: var(--bg-active); }
.tmo-stroke.active  { border-color: var(--accent); background: var(--bg-active); }

/* Generic option row (eraser mode, etc.) */
.tmo-opt-row { display: flex; gap: 8px; }
.tmo-opt-btn {
  flex: 1; min-height: 52px; padding: 8px 4px;
  background: var(--bg-toolbar-2); border: 2px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer;
  font-size: 13px; font-weight: 600; line-height: 1.4;
  color: var(--text-secondary); transition: all 150ms;
}
.tmo-opt-btn:hover  { background: var(--bg-active); color: var(--accent); }
.tmo-opt-btn.active { border-color: var(--accent); background: var(--bg-active); color: var(--accent); }

/* Font size buttons in modal */
.tmo-font-row { display: flex; gap: 8px; }
.tmo-font-btn {
  flex: 1; height: 60px; background: var(--bg-toolbar-2);
  border: 2px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer; font-weight: 700; color: var(--text-secondary);
  display: flex; align-items: center; justify-content: center;
  transition: all 150ms;
}
.tmo-font-btn:hover  { background: var(--bg-active); }
.tmo-font-btn.active { border-color: var(--accent); background: var(--bg-active); color: var(--accent); }

@media (max-width: 480px) {
  :root { --toolbar-width: 56px; --toolbar-expanded: 180px; }
  #tmo-swatches { grid-template-columns: repeat(4, 1fr); }
}

/* ══════════════════════════════════════════════════════════
   LAYERS PANEL (right sidebar)
══════════════════════════════════════════════════════════ */
#layers-panel {
  display: flex;
  flex-direction: column;
  width: var(--layers-panel-width);
  height: 100%;
  background: var(--bg-toolbar);
  border-left: 1px solid var(--border);
  flex-shrink: 0;
  overflow: hidden;
  transition: width var(--transition);
  position: relative;
  z-index: 100;
  padding-left: 28px;
}
#layers-panel.collapsed { width: 28px; }

#layers-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 10px 8px;
  border-bottom: 1px solid var(--border);
  min-height: 48px;
  flex-shrink: 0;
  overflow: hidden;
  white-space: nowrap;
}
#layers-panel.collapsed #layers-header { opacity: 0; pointer-events: none; }

#layers-title {
  font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-secondary);
}
#layers-header-btns { display: flex; gap: 4px; }
#layers-header-btns button {
  width: 28px; height: 28px;
  background: var(--bg-toolbar-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-primary);
  transition: background var(--transition), color var(--transition);
}
#layers-header-btns button:hover { background: var(--bg-active); color: var(--accent); }

#layers-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 0;
}
#layers-panel.collapsed #layers-list { opacity: 0; pointer-events: none; }
#layers-list::-webkit-scrollbar { width: 3px; }
#layers-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

.layer-row {
  display: flex;
  flex-direction: column;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background var(--transition);
  user-select: none;
  border-left: 3px solid transparent;
}
.layer-row:hover { background: var(--bg-toolbar-2); }
.layer-row.active { background: var(--bg-active); border-left-color: var(--accent); }
.layer-row.active.locked { border-left-color: #f97316; }

.layer-row-top {
  display: flex;
  align-items: center;
  gap: 6px;
}

.layer-drag-handle {
  cursor: grab; color: var(--text-secondary);
  font-size: 14px; padding: 2px; flex-shrink: 0;
  opacity: .45; transition: opacity 150ms;
}
.layer-drag-handle:hover { opacity: 1; }
.layer-drag-handle:active { cursor: grabbing; }

.layer-thumb {
  width: 48px; height: 32px;
  border: 1px solid var(--border);
  border-radius: 4px; flex-shrink: 0;
  background: #fff; display: block;
}
[data-theme="dark"] .layer-thumb { background: #1e1e2e; }

.layer-name {
  flex: 1; font-size: 12px; font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  outline: none; border-radius: 3px; padding: 1px 3px; min-width: 0;
}
.layer-name[contenteditable="true"] {
  background: var(--bg-toolbar-2);
  border: 1px solid var(--accent);
  white-space: normal; overflow: visible;
}

.layer-controls { display: flex; gap: 3px; flex-shrink: 0; }
.layer-btn-vis, .layer-btn-lock, .layer-btn-flatten {
  width: 24px; height: 24px;
  background: none; border: none; cursor: pointer;
  font-size: 13px; border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  opacity: .6; transition: opacity 150ms, background 150ms;
}
.layer-btn-vis:hover, .layer-btn-lock:hover, .layer-btn-flatten:hover { opacity: 1; background: var(--bg-toolbar-2); }
.layer-row.hidden .layer-thumb { opacity: 0.3; }
.layer-row.hidden .layer-name  { opacity: 0.5; }
.layer-row.locked .layer-btn-lock { opacity: 1; color: var(--accent); }

.layer-opacity-row {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 2px 0;
  max-height: 0; overflow: hidden; opacity: 0;
  transition: opacity 200ms, max-height 200ms;
}
.layer-row.active .layer-opacity-row { max-height: 28px; opacity: 1; }
.layer-opacity-label { font-size: 10px; color: var(--text-secondary); white-space: nowrap; }
.layer-opacity-slider { flex: 1; accent-color: var(--accent); cursor: pointer; }
.layer-opacity-value { font-size: 10px; color: var(--text-secondary); width: 30px; text-align: right; }

/* Drop target indicator when dragging */
.layer-row.drop-target { border-top: 2px solid var(--accent); }

#toggle-layers-panel {
  display: flex; align-items: center; justify-content: center;
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 28px;
  background: var(--bg-toolbar-2); border: none;
  border-right: 1px solid var(--border);
  cursor: pointer; color: var(--text-secondary);
  transition: background var(--transition), color var(--transition);
  z-index: 2;
}
#toggle-layers-panel:hover { background: var(--bg-active); color: var(--accent); }
#layers-panel.collapsed #toggle-layers-panel { border-right: none; }
#toggle-layers-panel svg { transform: rotate(180deg); transition: transform var(--transition); }
#layers-panel.collapsed #toggle-layers-panel svg { transform: none; }

/* Locked flash animation */
@keyframes layer-locked-shake {
  0%,100% { transform: translateX(0); }
  20%      { transform: translateX(-5px); }
  60%      { transform: translateX(5px); }
}
#layers-panel.locked-flash { animation: layer-locked-shake 300ms ease; }

/* Presentation mode: hide layers panel */
body.presentation-mode #layers-panel { display: none; }


/* ══════════════════════════════════════════════════════════
   COLOR TOOL BUTTON
══════════════════════════════════════════════════════════ */
#color-tool-icon {
  display: flex; align-items: center; justify-content: center;
}
#color-tool-dot {
  width: 22px; height: 22px; border-radius: 50%;
  background: #222222;
  border: 2px solid rgba(255,255,255,.35);
  box-shadow: 0 0 0 1.5px rgba(0,0,0,.25);
  display: inline-block;
  transition: background 150ms;
}

/* ══════════════════════════════════════════════════════════
   SHAPE PICKER (inside tool modal)
══════════════════════════════════════════════════════════ */
.tmo-shape-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px;
  margin-bottom: 6px;
}
.tmo-shape-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; padding: 8px 4px;
  border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg-toolbar-2); cursor: pointer;
  transition: border-color var(--transition), background var(--transition), color var(--transition);
  color: var(--text-primary);
}
.tmo-shape-btn:hover { border-color: var(--accent); background: var(--bg-active); }
.tmo-shape-btn.active {
  border-color: var(--accent); background: var(--bg-active); color: var(--accent);
}
.tmo-shape-icon { font-size: 20px; line-height: 1; }
.tmo-shape-name { font-size: 10px; font-weight: 500; white-space: nowrap; }

/* ══════════════════════════════════════════════════════════
   SIDEBAR COLOR PICKER
══════════════════════════════════════════════════════════ */
#sidebar-color { padding: 4px 6px; }
#sidebar-swatches {
  display: grid; grid-template-columns: repeat(6, 1fr); gap: 3px;
  margin-bottom: 8px;
}
#sidebar-swatches .sb-swatch {
  width: 100%; aspect-ratio: 1; border-radius: 4px; border: 2px solid transparent;
  cursor: pointer; transition: border-color 150ms, transform 150ms;
}
#sidebar-swatches .sb-swatch:hover { transform: scale(1.15); }
#sidebar-swatches .sb-swatch.active { border-color: var(--accent); transform: scale(1.1); }

#sidebar-color-row {
  display: flex; align-items: center; gap: 6px; margin-bottom: 6px;
}
#sidebar-color-dot {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--border);
  cursor: pointer; flex-shrink: 0; transition: border-color 150ms;
}
#sidebar-color-dot:hover { border-color: var(--accent); }
#sidebar-hex {
  flex: 1; min-width: 0; padding: 4px 6px; border-radius: 4px;
  border: 1px solid var(--border); background: var(--bg-toolbar-2);
  color: var(--text-primary); font-size: 12px; font-family: monospace;
}
#sidebar-hex:focus { border-color: var(--accent); outline: none; }
#sidebar-native-picker { position: absolute; width: 0; height: 0; opacity: 0; pointer-events: none; }

#sidebar-opacity-row {
  display: flex; align-items: center; gap: 6px;
}
.sidebar-range-label { font-size: 10px; color: var(--text-secondary); white-space: nowrap; }
#sidebar-opacity { flex: 1; accent-color: var(--accent); cursor: pointer; }
#sidebar-opacity-val { font-size: 10px; color: var(--text-secondary); width: 30px; text-align: right; }

/* Hide sidebar color details when collapsed */
#sidebar:not(.expanded) #sidebar-color-row,
#sidebar:not(.expanded) #sidebar-opacity-row { display: none; }
#sidebar:not(.expanded) #sidebar-swatches { grid-template-columns: repeat(2, 1fr); }

/* ══════════════════════════════════════════════════════════
   QUICK COLOR BAR (bottom center)
══════════════════════════════════════════════════════════ */
#quick-colors {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 8px; z-index: 60;
  background: var(--bg-toolbar); border: 1px solid var(--border);
  border-radius: 20px; padding: 6px 14px;
  box-shadow: var(--shadow);
}
.qc-btn {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid transparent; cursor: pointer;
  transition: transform 150ms, border-color 150ms, box-shadow 150ms;
}
.qc-btn:hover { transform: scale(1.15); }
.qc-btn.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--bg-toolbar), 0 0 0 4px var(--accent);
  transform: scale(1.1);
}
.qc-tool-btn {
  width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid transparent; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  background: transparent; color: var(--text-primary);
  transition: transform 150ms, background 150ms, border-color 150ms;
}
.qc-tool-btn svg {
  width: 68%; height: 68%; display: block; pointer-events: none;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}
.qc-tool-btn:hover { background: var(--bg-hover); transform: scale(1.1); }
.qc-tool-btn.active {
  border-color: var(--accent); background: var(--bg-hover);
  box-shadow: 0 0 0 2px var(--bg-toolbar), 0 0 0 4px var(--accent);
}
.qc-sep {
  width: 1px; background: var(--border); margin: 2px 2px; border-radius: 1px;
}
body.presentation-mode #quick-colors { display: none; }

/* ══════════════════════════════════════════════════════════
   USER INDICATOR (status bar)
══════════════════════════════════════════════════════════ */
#user-indicator { margin-left: auto; }
#btn-user {
  background: none; border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 3px 10px; font-size: 12px; cursor: pointer;
  color: var(--text-secondary); transition: background var(--transition), color var(--transition);
}
#btn-user:hover { background: var(--bg-active); color: var(--accent); }
#btn-user.logged-in { color: var(--accent); border-color: var(--accent); }
#btn-logout {
  background: none; border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 3px 10px; font-size: 12px; cursor: pointer;
  color: var(--text-secondary); margin-left: 4px;
  transition: background var(--transition), color var(--transition);
}
#btn-logout:hover { background: rgba(239,68,68,.1); color: #ef4444; border-color: #ef4444; }

/* ══════════════════════════════════════════════════════════
   AUTH MODAL
══════════════════════════════════════════════════════════ */
#auth-overlay, #designs-overlay {
  display: none; position: fixed; inset: 0; z-index: 3000;
  background: rgba(0,0,0,.55); backdrop-filter: blur(3px);
  align-items: center; justify-content: center;
}
#auth-overlay.open, #designs-overlay.open { display: flex; }

#auth-modal, #designs-modal {
  background: var(--bg-toolbar); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-heavy);
  width: min(400px, 92vw); max-height: 85vh;
  display: flex; flex-direction: column; overflow: hidden;
}

#auth-header, #designs-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 10px; border-bottom: 1px solid var(--border);
}
#auth-title, #designs-title { font-weight: 700; font-size: 15px; }
#auth-close, #designs-close {
  background: none; border: none; cursor: pointer; font-size: 16px;
  color: var(--text-secondary); border-radius: 4px; padding: 2px 6px;
  transition: background var(--transition);
}
#auth-close:hover, #designs-close:hover { background: var(--bg-toolbar-2); color: var(--text-primary); }

#auth-tabs {
  display: flex; border-bottom: 1px solid var(--border); padding: 0 12px;
}
.atab {
  background: none; border: none; border-bottom: 2px solid transparent;
  padding: 9px 16px; cursor: pointer; font-size: 13px; font-weight: 500;
  color: var(--text-secondary); transition: color var(--transition), border-color var(--transition);
  margin-bottom: -1px;
}
.atab:hover { color: var(--text-primary); }
.atab.active { color: var(--accent); border-bottom-color: var(--accent); }

.atab-panel {
  padding: 16px; display: flex; flex-direction: column; gap: 10px;
}

#auth-modal input[type="text"],
#auth-modal input[type="password"],
#designs-modal input[type="text"] {
  width: 100%; padding: 9px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg-toolbar-2);
  color: var(--text-primary); font-size: 13px; outline: none;
  transition: border-color var(--transition);
}
#auth-modal input:focus, #designs-modal input:focus { border-color: var(--accent); }

.auth-msg, .designs-msg {
  font-size: 12px; min-height: 16px; padding: 2px 4px; border-radius: 4px;
}
.auth-msg.error, .designs-msg.error { color: #ef4444; }
.auth-msg.success, .designs-msg.success { color: #22c55e; }

.auth-btn-primary, .designs-btn-primary {
  padding: 9px 18px; border-radius: var(--radius-sm); font-size: 13px;
  font-weight: 600; cursor: pointer; border: none;
  background: var(--accent); color: #fff;
  transition: background var(--transition);
}
.auth-btn-primary:hover, .designs-btn-primary:hover { background: var(--accent-hover); }

/* ══════════════════════════════════════════════════════════
   DESIGNS MODAL
══════════════════════════════════════════════════════════ */
#designs-modal { width: min(480px, 94vw); }

#designs-save-row {
  display: flex; gap: 8px; padding: 14px 16px 8px;
}
#designs-save-row input { flex: 1; }

#dsave-msg { padding: 0 16px; }

#dload-list {
  padding: 8px 16px 16px; overflow-y: auto; max-height: 50vh;
  display: flex; flex-direction: column; gap: 6px;
}
.dload-item {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg-toolbar-2);
  cursor: pointer; transition: border-color var(--transition), background var(--transition);
}
.dload-item:hover { border-color: var(--accent); background: var(--bg-active); }
.dload-item-info { flex: 1; min-width: 0; }
.dload-item-name {
  font-weight: 600; font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dload-item-meta { font-size: 11px; color: var(--text-secondary); margin-top: 2px; }
.dload-item-btns { display: flex; gap: 4px; flex-shrink: 0; }
.dload-item-btn {
  background: none; border: 1px solid var(--border); border-radius: 4px;
  font-size: 13px; padding: 3px 7px; cursor: pointer;
  color: var(--text-secondary); transition: background var(--transition), color var(--transition);
}
.dload-item-btn:hover { background: var(--bg-toolbar); color: var(--text-primary); }
.dload-item-btn.danger:hover { color: #ef4444; border-color: #ef4444; }
.dload-empty {
  font-size: 13px; color: var(--text-secondary); text-align: center; padding: 28px 0;
}

/* ═══════════════════════════════════════════════════════════
   MATH TOOLS — MODALS
══════════════════════════════════════════════════════════ */

/* Symbol palette */
#sym-overlay, #fn-overlay, #poly-overlay, #transform-overlay, #axes-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  display: none; align-items: center; justify-content: center;
  z-index: 2000;
}
#sym-overlay.open, #fn-overlay.open, #poly-overlay.open, #transform-overlay.open, #axes-overlay.open {
  display: flex;
}
#sym-modal, #fn-modal, #poly-modal, #transform-modal, #axes-modal {
  background: var(--bg-toolbar);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-heavy);
  padding: 16px;
  min-width: 280px;
  max-width: 460px;
  width: 90vw;
}
#sym-header, #fn-header, #poly-header, #transform-header, #axes-header {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 14px; font-weight: 600;
  margin-bottom: 12px; padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
#sym-close, #fn-close, #poly-close, #transform-close, #axes-close {
  background: none; border: none; cursor: pointer;
  color: var(--text-secondary); font-size: 16px; padding: 2px 6px;
  border-radius: var(--radius-sm);
}
#sym-close:hover, #fn-close:hover, #poly-close:hover, #transform-close:hover, #axes-close:hover {
  background: var(--bg-toolbar-2);
}

/* Axes modal — type tab buttons */
.axes-type-group { display: flex; gap: 4px; flex-wrap: wrap; }
.axes-tab {
  padding: 5px 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); cursor: pointer;
  font-size: 12px; background: var(--bg-main); color: var(--text-primary);
  transition: background 120ms, border-color 120ms;
  touch-action: manipulation;
}
.axes-tab:hover { background: var(--bg-toolbar-2); }
.axes-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Axes modal — checkbox row */
.axes-check-label {
  display: flex; align-items: center; gap: 6px;
  font-size: 13px; cursor: pointer;
}
.axes-check-label input[type="checkbox"] { width: 15px; height: 15px; cursor: pointer; }

/* Symbol grid */
#sym-grid {
  display: grid; grid-template-columns: repeat(8, 1fr);
  gap: 4px; max-height: 300px; overflow-y: auto;
}
.sym-btn {
  aspect-ratio: 1; min-height: 36px;
  background: var(--bg-main); border: 1px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer;
  font-size: 16px; display: flex; align-items: center; justify-content: center;
  transition: background 120ms, border-color 120ms;
  touch-action: manipulation;
}
.sym-btn:hover { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Function / polygon modal rows */
.fn-row {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 10px; flex-wrap: wrap;
}
.fn-row label {
  font-size: 12px; color: var(--text-secondary); white-space: nowrap; min-width: 60px;
}
.fn-row input[type="text"], .fn-row input[type="number"] {
  flex: 1; min-width: 60px;
  background: var(--bg-main); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text-primary);
  font-size: 13px; padding: 6px 8px;
}
.fn-actions {
  display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px;
}
.fn-actions button {
  padding: 7px 16px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); cursor: pointer;
  font-size: 13px; background: var(--bg-toolbar-2); color: var(--text-primary);
  touch-action: manipulation;
}
.fn-actions button:last-child {
  background: var(--accent); color: #fff; border-color: var(--accent);
}
#fn-msg { font-size: 12px; color: #ef4444; min-height: 16px; margin: 4px 0; }

/* Transform body rows */
#transform-body .fn-row { margin-bottom: 8px; }

/* Snap indicator on canvas */
#snap-indicator {
  position: absolute; pointer-events: none;
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid #22c55e;
  transform: translate(-50%, -50%);
  display: none;
  z-index: 5;
}

/* Construction hint in status bar */
#status-measure {
  color: var(--accent); font-variant-numeric: tabular-nums;
}

/* Snap toggle active state (same as grid-toggle) */
#btn-snap-toggle.snap-active .icon { color: var(--accent); }

/* ══════════════════════════════════════════════════════════
   INLINE STYLE MIGRATIONS
   (inline style="" attributes removed for CSP unsafe-inline)
══════════════════════════════════════════════════════════ */

/* Text tool icon: bold italic serif "T" */
.icon-text-tool {
  font-style: italic;
  font-family: serif;
  font-weight: 700;
  font-size: 22px;
}

/* Math tool icons with reduced font size */
.icon-sm-14 { font-size: 14px; }
.icon-sm-12 { font-size: 12px; }

/* Function tool icon: italic f(x) */
.icon-fn {
  font-size: 13px;
  font-style: italic;
}

/* Grid tool icon: monospace # */
.icon-grid {
  font-size: 16px;
  font-family: monospace;
}

/* Elements hidden by default that JS shows/hides via element.style.display.
   Using display:none here gives the correct initial state;
   JS overrides with element.style.display = '' / 'none' as needed. */
.hidden-initially { display: none; }

/* ══════════════════════════════════════════════════════════
   PLUGINS PANEL MODAL
══════════════════════════════════════════════════════════ */
#plugins-overlay {
  display: none; position: fixed; inset: 0; z-index: 3000;
  background: rgba(0,0,0,.55); backdrop-filter: blur(3px);
  align-items: center; justify-content: center;
}
#plugins-overlay.open { display: flex; }

#plugins-modal {
  background: var(--bg-toolbar); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-heavy);
  width: 340px; max-width: 92vw;
  display: flex; flex-direction: column; overflow: hidden;
}

#plugins-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 12px; border-bottom: 1px solid var(--border);
  font-size: 15px; font-weight: 600;
}
#plugins-close {
  background: none; border: none; cursor: pointer;
  color: var(--text-secondary); font-size: 16px; padding: 2px 6px;
  border-radius: var(--radius-sm);
}
#plugins-close:hover { background: var(--bg-toolbar-2); }

/* Autosave toggle row inside plugins modal */
.autosave-row {
  display: grid; grid-template-columns: auto 1fr; gap: 4px 10px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  cursor: pointer; align-items: center;
}
.autosave-row input[type="checkbox"] {
  grid-row: 1 / span 2;
  width: 18px; height: 18px; cursor: pointer; accent-color: var(--accent);
}
.autosave-row .autosave-label  { font-size: 13px; font-weight: 500; }
.autosave-row .autosave-help   { font-size: 11px; color: var(--text-secondary); grid-column: 2; }
.autosave-row:hover .autosave-label { color: var(--accent); }

#plugins-list {
  padding: 8px 0; min-height: 60px; max-height: 320px; overflow-y: auto;
}
#plugins-list:empty::after {
  content: attr(data-empty);
  display: block; text-align: center;
  color: var(--text-secondary); font-size: 13px; padding: 24px 0;
}

.plugin-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  transition: background 120ms;
}
.plugin-row:last-child { border-bottom: none; }
.plugin-row:hover { background: var(--bg-toolbar-2); }

.plugin-icon {
  font-size: 22px; width: 32px; text-align: center; flex-shrink: 0;
}
.plugin-info {
  flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0;
}
.plugin-name {
  font-size: 14px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.plugin-meta {
  font-size: 11px; color: var(--text-secondary);
}
.plugin-toggle {
  flex-shrink: 0; width: 36px; height: 20px;
  appearance: none; -webkit-appearance: none;
  background: var(--border); border-radius: 10px;
  cursor: pointer; position: relative; transition: background 180ms;
}
.plugin-toggle::after {
  content: ''; position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%;
  background: #fff; transition: transform 180ms;
  box-shadow: 0 1px 3px rgba(0,0,0,.25);
}
.plugin-toggle:checked { background: var(--accent); }
.plugin-toggle:checked::after { transform: translateX(16px); }

/* Dropdown per triar ubicació del plugin (sidebar | columna) */
.plugin-placement {
  flex-shrink: 0;
  padding: 4px 6px;
  font-size: 12px;
  background: var(--bg-toolbar-2);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  appearance: auto;
  transition: border-color 120ms, background 120ms;
}
.plugin-placement:hover { border-color: var(--accent); }
.plugin-placement:focus { outline: none; border-color: var(--accent); }

#plugins-footer {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-top: 1px solid var(--border);
  gap: 8px;
}
#plugins-hint {
  font-size: 11px; color: var(--text-secondary); flex: 1;
}
#plugins-apply {
  background: var(--accent); color: #fff; border: none;
  border-radius: var(--radius-sm); padding: 7px 14px;
  font-size: 13px; font-weight: 500; cursor: pointer;
  transition: background 120ms;
}
#plugins-apply:hover { background: var(--accent-hover); }
#plugins-apply:disabled {
  background: var(--border); color: var(--text-secondary); cursor: default;
}

/* Botó d'ajuda (?) a cada fila de plugin */
.plugin-help-btn {
  flex-shrink: 0;
  width: 26px; height: 26px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px; font-weight: 700;
  cursor: pointer; line-height: 1;
  transition: background 120ms, color 120ms, border-color 120ms;
  display: flex; align-items: center; justify-content: center;
}
.plugin-help-btn:hover {
  background: var(--accent); border-color: var(--accent); color: #fff;
}

/* ══════════════════════════════════════════════════════════
   PLUGIN HELP OVERLAY
══════════════════════════════════════════════════════════ */
#plugin-help-overlay {
  position: fixed; inset: 0; z-index: 3500;
  background: rgba(0,0,0,.55); backdrop-filter: blur(4px);
  display: none; align-items: center; justify-content: center;
}
#plugin-help-overlay.open { display: flex; }
#plugin-help-modal {
  background: var(--bg-toolbar);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 24px 60px rgba(0,0,0,.5);
  width: min(520px, 92vw);
  max-height: 80vh;
  display: flex; flex-direction: column;
  overflow: hidden;
}
#plugin-help-header {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#plugin-help-icon { font-size: 22px; }
#plugin-help-title {
  flex: 1; font-size: 15px; font-weight: 600; color: var(--text-primary);
}
#plugin-help-close {
  background: transparent; border: none; cursor: pointer;
  color: var(--text-secondary); font-size: 16px;
  width: 28px; height: 28px; border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  transition: background 120ms, color 120ms;
}
#plugin-help-close:hover { background: var(--bg-toolbar-2); color: var(--text-primary); }
#plugin-help-body {
  overflow-y: auto; padding: 16px 20px;
  display: flex; flex-direction: column; gap: 18px;
}
.phelp-section { display: flex; flex-direction: column; gap: 8px; }
.phelp-heading {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .06em; color: var(--accent);
}
.phelp-item {
  display: flex; gap: 10px; align-items: flex-start;
  font-size: 13px; line-height: 1.5; color: var(--text-primary);
}
.phelp-item-icon {
  flex-shrink: 0; font-size: 15px; width: 22px; text-align: center;
  margin-top: 1px;
}
.phelp-item-text { flex: 1; }
.phelp-item-text strong { color: var(--accent); font-weight: 600; }

/* ══════════════════════════════════════════════════════════
   MATH 3D PLUGIN — CONFIG MODAL
══════════════════════════════════════════════════════════ */
#m3d-overlay {
  display: none; position: fixed; inset: 0; z-index: 3000;
  background: rgba(0,0,0,.55); backdrop-filter: blur(3px);
  align-items: center; justify-content: center;
}
#m3d-overlay.open { display: flex; }

#m3d-modal {
  background: var(--bg-toolbar); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-heavy);
  width: 360px; max-width: 94vw; max-height: 90vh;
  display: flex; flex-direction: column; overflow: hidden;
}

.m3d-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 13px 16px 11px; border-bottom: 1px solid var(--border);
  font-size: 14px; font-weight: 600; flex-shrink: 0;
}
#m3d-close {
  background: none; border: none; cursor: pointer;
  color: var(--text-secondary); font-size: 16px; padding: 2px 6px;
  border-radius: var(--radius-sm);
}
#m3d-close:hover { background: var(--bg-toolbar-2); }

.m3d-body {
  padding: 10px 16px; overflow-y: auto; flex: 1;
  display: flex; flex-direction: column; gap: 8px;
}

.m3d-row {
  display: flex; align-items: center; gap: 8px;
}
.m3d-row label {
  font-size: 12px; color: var(--text-secondary);
  width: 120px; flex-shrink: 0;
}
.m3d-row select,
.m3d-row input[type="number"] {
  flex: 1; padding: 5px 8px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg-toolbar-2);
  color: var(--text-primary); font-size: 13px;
}
.m3d-row select:focus,
.m3d-row input[type="number"]:focus {
  outline: none; border-color: var(--accent);
}

.m3d-check-row {
  display: flex; align-items: center; gap: 8px;
  padding: 2px 0;
}
.m3d-check-row input[type="checkbox"] { width: 15px; height: 15px; cursor: pointer; }
.m3d-check-row label { font-size: 12px; color: var(--text-primary); cursor: pointer; }

.m3d-sep {
  height: 1px; background: var(--border); margin: 4px 0;
}

.m3d-footer {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 10px 16px; border-top: 1px solid var(--border); flex-shrink: 0;
}
.m3d-btn-cancel {
  background: var(--bg-toolbar-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 7px 14px;
  font-size: 13px; cursor: pointer; color: var(--text-primary);
}
.m3d-btn-cancel:hover { background: var(--border); }
.m3d-btn-ok {
  background: var(--accent); color: #fff; border: none;
  border-radius: var(--radius-sm); padding: 7px 16px;
  font-size: 13px; font-weight: 500; cursor: pointer;
}
.m3d-btn-ok:hover { background: var(--accent-hover); }

/* Quick-color buttons: static background colors */
.qc-color-black { background: #222222; }
.qc-color-red   { background: #ef4444; }
.qc-color-blue  { background: #3b82f6; }
.qc-color-green  { background: #22c55e; }
.qc-color-yellow { background: #facc15; }

/* =========================================================================
   TOUCH MODE / ORBYS adaptations
   - Millora la usabilitat en pantalles tàctils grans (ORBYS 4K)
   - Manté el comportament normal en ordinadors
   - Resol el problema del panell de capes / barra dreta fora de pantalla
   ========================================================================= */

button, .tool-btn, .float-btn, .qc-btn, .layer-row, .m3d-btn {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Overlays i modals: elimina el delay de 300ms del navegador per a taps */
#tool-modal-overlay, #tool-modal,
#modal-overlay,
#auth-overlay, #designs-overlay,
#sym-overlay, #fn-overlay, #poly-overlay, #transform-overlay, #axes-overlay {
  touch-action: manipulation;
}

/* Feedback tàctil més visible (sense trencar hover de desktop) */
.tool-btn:active,
.float-btn:active,
.qc-btn:active,
.m3d-btn:active {
  transform: scale(0.93);
  filter: brightness(0.85);
  transition: transform 0.08s ease, filter 0.08s ease;
}

/* Animació de long-press: anell que s'omple en 500ms */
@keyframes long-press-ring {
  from { box-shadow: 0 0 0 0px var(--accent, #7c3aed), 0 0 0 3px var(--accent, #7c3aed); opacity: 0.3; }
  to   { box-shadow: 0 0 0 6px var(--accent, #7c3aed), 0 0 0 3px var(--accent, #7c3aed); opacity: 0.85; }
}
.tool-btn.long-pressing {
  animation: long-press-ring 500ms ease-out forwards;
  transform: scale(0.96);
}

/* --- Mode tàctil (activat manualment o automàticament) --- */
body.touch-mode {
  --toolbar-width: 84px;
  --layers-panel-width: 260px;
}
body.touch-mode .tool-btn {
  min-height: 64px;
  font-size: 28px;
}
body.touch-mode .tool-btn .tool-label {
  font-size: 14px;
}
body.touch-mode .float-btn {
  width: 64px;
  height: 64px;
  font-size: 26px;
}
body.touch-mode .fb-group { gap: 10px; }
body.touch-mode .fb-group + .fb-group { padding-top: 16px; }
body.touch-mode .fb-group + .fb-group::before { width: 30px; }
body.touch-mode .qc-btn,
body.touch-mode .qc-tool-btn {
  width: 40px;
  height: 40px;
}
body.touch-mode #layers-panel {
  font-size: 16px;
}
body.touch-mode .layer-row {
  min-height: 48px;
  padding: 8px 10px;
}
body.touch-mode .layer-row button,
body.touch-mode .layer-controls button {
  min-width: 40px;
  min-height: 40px;
  font-size: 18px;
}

/* Auto-activa mode tàctil si el dispositiu és tàctil primari */
@media (pointer: coarse) {
  body:not(.no-touch-mode) {
    --toolbar-width: 84px;
    --layers-panel-width: 260px;
  }
  body:not(.no-touch-mode) .tool-btn { min-height: 64px; font-size: 28px; }
  body:not(.no-touch-mode) .float-btn { width: 64px; height: 64px; font-size: 26px; }
  body:not(.no-touch-mode) .fb-group { gap: 10px; }
  body:not(.no-touch-mode) .fb-group + .fb-group { padding-top: 16px; }
  body:not(.no-touch-mode) .fb-group + .fb-group::before { width: 30px; }
  body:not(.no-touch-mode) .qc-btn,
  body:not(.no-touch-mode) .qc-tool-btn { width: 40px; height: 40px; }
  body:not(.no-touch-mode) .layer-row { min-height: 48px; }
}

/* Escalat per a ORBYS 4K (viewport molt ample) */
@media (min-width: 2000px) {
  :root {
    --toolbar-width: 110px;
    --layers-panel-width: 320px;
  }
  body { font-size: 22px; }
  .tool-btn { min-height: 84px; font-size: 36px; }
  .tool-btn .tool-label { font-size: 16px; }
  .float-btn { width: 84px; height: 84px; font-size: 34px; }
  .fb-group { gap: 12px; }
  .fb-group + .fb-group { padding-top: 20px; }
  .fb-group + .fb-group::before { width: 36px; }
  .qc-btn, .qc-tool-btn { width: 52px; height: 52px; }
  #layers-panel { font-size: 20px; }
  .layer-row { min-height: 56px; padding: 10px 14px; }
  .layer-row button,
  .layer-controls button { min-width: 48px; min-height: 48px; font-size: 22px; }
}


