* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; }
/* Keep toolbar fixed; allow mosaic area to scroll */
html, body {
  /* Prevent the page itself from scrolling; the grid's main area should handle
     scrolling if ever needed. This also avoids 1px scrollbars from rounding. */
  overflow: hidden;
}

body {
  /* Constrain the layout to the viewport to avoid tiny 1–2px overflows that
     cause an annoying vertical scrollbar. Prefer dvh when supported. */
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  margin: 0;
  /* Flex column (not grid) so that when the #toolBarDiv / #textBarDiv
     sub-toolbars become visible, <main> simply shrinks by their height.
     With a 3-row grid the toolbar stole the 1fr row from <main> and forced
     a scrollbar; flex-column avoids that entirely because <main> is the
     only flex: 1 child. */
  display: flex;
  flex-direction: column;
  background: radial-gradient(circle at top left, #1e293b, #020617 52%);
  color: #0f172a;
}
header { position: sticky; top: 0; z-index: 10; background: #fff; padding: 6px 2px 8px 2px; margin: 0px 0px 5px 0px;  border-bottom: 3px solid rgba(15,23,42,0.35);
 display: flex; align-items: center; gap: 10px; flex-wrap: wrap;   backdrop-filter: blur(10px);
}
.auth-controls { display: inline-flex; gap: 6px; align-items: center; }
.auth-controls .auth-primary { background: #2563eb; color: #fff; border-color: #1d4ed8; }
.toolbar-auth-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 32px;
  padding: 0 12px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #ffffff;
  color: #0f172a;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  box-sizing: border-box;
}
.toolbar-auth-btn:hover { background: #f8fafc; }
.toolbar-auth-btn-primary {
  background: #2563eb;
  border-color: #1d4ed8;
  color: #ffffff;
}
.toolbar-auth-btn-primary:hover { background: #1d4ed8; }
.toolbar-auth-btn[hidden] { display: none !important; }
#authOpen[hidden] { display: none !important; }
#authOpen {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
}
header {
  display: flex;
  justify-content: center;
}
header h1 {
  margin: 0 16px 0 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: #0f172a; /* high contrast on light header bg */
  /* Long mosaic titles like "Honor Flight SOCO 2026 Tribute Mosaic" used to
     stay on a single line and push the toolbar past the screen edge on
     narrower viewports (e.g. 16:9 laptops). Cap the title's max width
     aggressively — we'd rather sacrifice the title to two/three lines
     than wrap the toolbar onto a second row. The previous 240px cap was
     too generous: even a single-word title like "Honor" + a few more
     could occupy ~200px on one line. 140px forces almost any multi-word
     title to break before the toolbar feels any pressure. */
  max-width: 140px;
  line-height: 1.15;
  white-space: normal;
  overflow-wrap: break-word;
  word-break: break-word;
  flex: 0 0 auto;
}
header .toolbar {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(15,23,42,0.93);
  box-shadow: 0 10px 30px rgba(15,23,42,0.75);
}
main {
  /* Grow/shrink to fill the remaining vertical space after header, any
     visible sub-toolbar, and footer. min-height:0 lets it shrink below
     its intrinsic content size so the canvas can be resized down. */
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 0;
  display: block;
}
.splash-backdrop { position: fixed; inset: 0; display: flex; align-items: flex-start; justify-content: center; padding-top: 20vh; z-index: 900; background: #f1f5f9; }
.splash-backdrop[hidden] { display: none !important; }
.splash-backdrop .splash-title { color: #334155; font-size: 24px; font-weight: 600; letter-spacing: 0.3px; text-shadow: 0 1px 2px rgba(0,0,0,0.06); text-align: center; }
.mosaic-wrap { position: relative; width: max-content; min-height: max-content; margin: 0 auto;   border-radius: 22px;
  background: radial-gradient(circle at top, rgba(15,23,42,0.96), rgba(15,23,42,0.98));
  /* Use a centered glow instead of a big downward shadow, which looked like a grey overlay below the canvas. */
  box-shadow: 0 0 50px rgba(15,23,42,0.75);
}

/* Loading overlay shown during large chunked mosaic loads */
.load-overlay[hidden] { display: none !important; }
.load-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px;
  background: rgba(2,6,23,0.60);
  backdrop-filter: blur(2px);
  z-index: 50;
  pointer-events: none;
}
.load-thumb {
  max-width: min(720px, 92vw);
  max-height: min(520px, 70vh);
  width: auto;
  height: auto;
  image-rendering: pixelated;
  border-radius: 12px;
  border: 1px solid rgba(148,163,184,0.5);
  box-shadow: 0 24px 60px rgba(15,23,42,0.85);
  background: #020617;
}
.load-card {
  max-width: min(720px, 92vw);
  background: rgba(15,23,42,0.92);
  border: 1px solid rgba(148,163,184,0.35);
  border-radius: 12px;
  padding: 10px 12px;
  box-shadow: 0 12px 30px rgba(15,23,42,0.75);
  text-align: center;
}
.load-title {
  color: #e5e7eb;
  font-size: 14px;
  font-weight: 700;
}
.load-sub {
  margin-top: 2px;
  color: #cbd5e1;
  font-size: 12px;
}
.load-progress {
  margin-top: 6px;
  color: #93c5fd;
  font-size: 12px;
}
.load-progress-track {
  width: 100%;
  height: 8px;
  border-radius: 999px;
  background: rgba(15,23,42,0.9);
  border: 1px solid rgba(148,163,184,0.7);
  overflow: hidden;
}
.load-progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #3b82f6, #22c55e);
  transition: width 120ms linear;
}
.load-progress-label {
  margin-top: 2px;
  font-size: 11px;
  color: #bfdbfe;
  text-align: center;
}
#mosaic {
  border: 1px solid #1e293b;
  image-rendering: pixelated;
  /* Required for reliable Pointer Events on touch screens. Browser panning/
     pinch gestures on the drawing surface are replaced by app gestures in
     stage 2; outside the canvas, normal page scrolling is unchanged. */
  touch-action: none;
  -webkit-touch-callout: none;
  max-width: none;
  height: auto;
  display: block;
  background: #020617;
  /* Shadow is handled by .mosaic-wrap; keeping a second shadow here made the bottom look like an overlay. */
  box-shadow: none;
}

#imgEdCanvas {
  touch-action: none;
  -webkit-touch-callout: none;
}
#overlay { position: absolute; left: 0; top: 0; image-rendering: pixelated; pointer-events: none; }
#cursorOverlay { position: absolute; left: 0; top: 0; image-rendering: pixelated; pointer-events: none; }
button { padding: 4px 10px; font-size: 12px; border-radius: 999px; border: 1px solid rgba(148,163,184,0.6); background:#0f172a; color:#e5e7eb; cursor:pointer; }
button:hover { background:#1e293b; }
/* Generic disabled state */
button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.size-info { color: #94a3b8; font-size: 12px; }
.funds-info { color: #22c55e; font-size: 12px; margin-left: 8px; }
.tools { display: inline-flex; gap: 6px; margin-left: 8px; }
.tool-btn {
  border: 1px solid rgba(148,163,184,0.6);
  background: rgba(15,23,42,0.9);
  color: #e5e7eb;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border-radius: 999px;
  padding: 3px 9px;
  transition: background-color 120ms ease, border-color 120ms ease, box-shadow 120ms ease, transform 80ms ease;
}
.tool-btn .icon { display: inline-flex; width: 16px; height: 16px; }
.tool-btn .label { font-size: 12px; }
.tool-btn:hover { background: #111827; border-color: #60a5fa; }
.tool-btn:focus-visible { outline: none; box-shadow: 0 0 0 2px rgba(96,165,250,0.9); }
.tool-btn.active {
  background: linear-gradient(135deg,#3b82f6,#22c55e);
  color: #0b1120;
  border-color: rgba(15,23,42,0.85);
  box-shadow: 0 6px 20px rgba(15,23,42,0.9);
  transform: translateY(-1px);
}
.tool-btn.active .icon { color: currentColor; }
.tool-btn.active .label { font-weight: 600; }
/* Playback combo control */
.playback-controls {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

/* Playback speed pills */
.playback-speed {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.playback-speed-label {
  font-size: 11px;
  color: #94a3b8;
}

.speed-pills {
  display: inline-flex;
  background: rgba(15,23,42,0.85);
  border: 1px solid rgba(148,163,184,0.4);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
}

.speed-pill {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  border: none;
  background: transparent;
  color: #cbd5e1;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}

.speed-pill:hover {
  background: rgba(148,163,184,0.15);
}

.speed-pill.active {
  background: linear-gradient(135deg,#3b82f6,#22c55e);
  color: #020617;
  font-weight: 600;
}

/* Playback speed pills */
.playback-speed {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.playback-speed-label {
  font-size: 11px;
  color: #94a3b8;
}

.speed-pills {
  display: inline-flex;
  background: rgba(15,23,42,0.85);
  border: 1px solid rgba(148,163,184,0.4);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
}

.speed-pill {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  border: none;
  background: transparent;
  color: #cbd5e1;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}

.speed-pill:hover {
  background: rgba(148,163,184,0.15);
}

.speed-pill.active {
  background: linear-gradient(135deg,#3b82f6,#22c55e);
  color: #020617;
  font-weight: 600;
}


/* Primary actions */
#commit {
  background: transparent;
  border-color: rgba(34,197,94,0.7);
  color: #bbf7d0;
}
#commit:hover {
  background: rgba(22,163,74,0.15);
  border-color: rgba(22,163,74,0.9);
}
#revert {
  background: transparent;
  border-color: rgba(248,113,113,0.6);
  color:#fecaca;
}

/* Commit preview status (confirm modal) */
.commit-status {
  font-weight: 600;
}
.commit-status.pending {
  color: #dc2626; /* red */
}
.commit-status.ready {
  color: #16a34a; /* green */
}

.commit-progress {
  width: 100%;
  height: 10px;
  border-radius: 999px;
  background: #e5e7eb;
  overflow: hidden;
  border: 1px solid #cbd5e1;
  margin: 10px 0 6px 0;
}
.commit-progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #3b82f6, #22c55e);
  transition: width 120ms linear;
}
.commit-progress-label {
  font-size: 12px;
  color: #334155;
  margin: 0 0 6px 0;
  text-align: right;
}

/* Yellow warning banner shown inside the commit-confirm dialog when the
 * user clicks past the sparse-commit modal. Reminds them that these
 * pixels won't be permanently protected. Text comes from the env-backed
 * MOSAIC_SPARSE_CONFIRM_BANNER config. */
.sparse-confirm-banner {
  margin: 8px 0;
  padding: 8px 10px;
  background: #fef3c7;
  border: 1px solid #fbbf24;
  border-left: 4px solid #f59e0b;
  border-radius: 6px;
  color: #78350f;
  font-size: 12px;
  line-height: 1.35;
}
.sparse-confirm-banner[hidden] { display: none !important; }

#commitYes:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
#uploadImage { background: rgba(15,23,42,0.85); }
/* When Upload is used as a tool (image tool active), make its active state
   look like the other tools and keep the label/icon clearly visible. */
#uploadImage.tool-btn.active {
  background: linear-gradient(135deg,#3b82f6,#22c55e);
  color: #0b1120;
  border-color: rgba(15,23,42,0.85);
  box-shadow: 0 6px 20px rgba(15,23,42,0.9);
  transform: translateY(-1px);
}
.toolbar .spacer { flex: 1 1 auto; }
/* In view-only mode (no auth), hide the Upload button entirely as a safety net */
body.view-only #uploadImage { display: none !important; }
/* In view-only mode, hide pixel change footer text */
body.view-only #pixelChangeInfo { display: none !important; }
/* In edit mode, make pixel-change info more prominent */
body:not(.view-only) #pixelChangeInfo {
  color: #f9fafb;
  font-weight: 600;
}

#mosaic.cursor-pen { cursor: none; }
#mosaic.cursor-eraser { cursor: none; }
#mosaic.cursor-dropper { cursor: copy; }
#mosaic.cursor-line { cursor: none; }
#mosaic.cursor-rect, #mosaic.cursor-oval { cursor: crosshair; }
#mosaic.cursor-select, #mosaic.cursor-lasso { cursor: crosshair; }
#mosaic.cursor-text { cursor: text; }
#mosaic.cursor-lock { cursor: crosshair; }

/* Image mini toolbar */
.mini-toolbar[hidden] { display: none; }
.mini-toolbar { position: absolute; left: 0; top: 0; display: inline-flex; gap: 6px; background: #fff; border: 1px solid #cbd5e1; border-radius: 4px; padding: 4px 6px; box-shadow: 0 6px 18px rgba(0,0,0,0.15); z-index: 2; pointer-events: auto; }
.mini-toolbar button { padding: 4px 8px; font-size: 12px; }

/* Modal/panel styles (non-dimming, draggable) */
.modal-backdrop[hidden] { display: none; }
.modal-backdrop { position: fixed; inset: 0; background: transparent; display: block; z-index: 1000; pointer-events: none; }
#partialCommitModal:not([hidden]) {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 80px;
  background: rgba(15,23,42,0.5);
  backdrop-filter: blur(2px);
  pointer-events: auto;
}
#partialCommitModal .modal {
  position: relative;
  top: auto;
  right: auto;
  left: auto;
}
.modal {
  position: absolute;
  top: 64px;          /* was 16px – drop it below the header/toolbar */
  right: 16px;
  background: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  padding: 12px;
  min-width: 360px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.2);
  pointer-events: auto;
}
.modal h2 {
  margin: 0 0 6px 0;
  font-size: 16px;
  cursor: grab;      /* was: move */
  user-select: none;
  padding-right: 18px;
}
.modal.panel-dragging h2 {
  cursor: grabbing;
}
body.panel-dragging {
  user-select: none;
}
.modal .modal-close { position: absolute; top: 4px; right: 6px; border: none; background: transparent; padding: 0; font-size: 14px; line-height: 1; cursor: pointer; color: #64748b; }
.modal .modal-close:hover { color: #0f172a; }
.modal .modal-close:focus-visible { outline: 2px solid #2563eb; outline-offset: 2px; }
.modal .row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; align-items: center; }
.modal .row label { display: flex; gap: 6px; align-items: center; }
.modal .row.erase-options { flex-direction: column; align-items: flex-start; }
.modal .row.erase-options label { width: 100%; justify-content: space-between; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }

/* Tool panel Apply/Cancel should be compact (pen/line overlays) */
#toolPendingActions {
  gap: 6px;
  margin-top: 10px;
}
#toolPendingActions button {
  padding: 2px 8px;
  font-size: 11px;
  border-radius: 10px;
}

/* Centered, larger, embellished auth panel */
#authPanel.modal-backdrop { background: rgba(15, 23, 42, 0.35); backdrop-filter: blur(1px); }
#authPanel .modal {
  top: 50%; right: auto; left: 50%; transform: translate(-50%, -50%);
  min-width: 440px; max-width: 560px;
  padding: 18px 20px;
  border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.18), 0 2px 8px rgba(0,0,0,0.08);
}
#resetPanel .modal {
  top: 50%; right: auto; left: 50%; transform: translate(-50%, -50%);
  min-width: 420px; max-width: 520px;
  padding: 18px 20px;
  border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.18), 0 2px 8px rgba(0,0,0,0.08);
}
#adminChangeModal .modal {
  top: 50%; right: auto; left: 50%; transform: translate(-50%, -50%);
  min-width: 360px; max-width: 520px;
}
#authPanel .modal h2 {
  margin: 0 0 10px 0;
  font-size: 20px;
  letter-spacing: 0.2px;
  border-bottom: 1px solid #e5e7eb;
  padding-bottom: 8px;
}
#authPanel .modal .row label { gap: 8px; }
#authPanel input[type="email"], #authPanel input[type="password"] {
  padding: 8px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
}
#authPanel .actions { margin-top: 14px; }
#authPanel .auth-subtext { margin: 10px 0 0 0; color: #64748b; font-size: 12px; }

/* Lock timer bar at bottom of page */
.lock-timer {
  position: fixed;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
  background: #0f172a;
  color: #e5e7eb;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  box-shadow: 0 4px 12px rgba(15,23,42,0.6);
  z-index: 2000;
}
.lock-timer[hidden] { display: none; }

/* Global busy overlay shown while large pixel batches are pushed down to the base canvas */
.busy-overlay[hidden] { display: none; }
.busy-overlay {
  position: fixed;
  inset: 0;
  /* Strong dimming tint so the overlay is clearly visible over the dark
     mosaic background. */
  background: rgba(15,23,42,0.75);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  pointer-events: auto;
}

/* Keep the commit confirm modal above the busy overlay so we can show progress
   while the rest of the UI is blocked. */
#commitConfirm.modal-backdrop {
  z-index: 10000;
}

/* Insufficient-funds modal must be above the commit panel (which can still be
   open when billing hold fails). */
#addFundsModal.modal-backdrop {
  z-index: 11000;
  background: rgba(15, 23, 42, 0.35);
  backdrop-filter: blur(1px);
  pointer-events: auto;
}
#addFundsModal .modal {
  top: 50%;
  right: auto;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.18), 0 2px 8px rgba(0,0,0,0.08);
}
.busy-overlay .busy-panel {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(15,23,42,0.96);
  color: #e5e7eb;
  box-shadow: 0 18px 40px rgba(15,23,42,0.9);
}
.busy-overlay .busy-text {
  font-size: 13px;
}
.busy-spinner {
  width: 18px;
  height: 18px;
  border-radius: 999px;
  border: 2px solid rgba(148,163,184,0.6);
  border-top-color: #60a5fa;
  animation: busy-spin 0.8s linear infinite;
}
@keyframes busy-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* Transient notice for very large pushdowns (non-interactive, auto-hides) */
.pushdown-hint[hidden] { display: none; }
.pushdown-hint {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(15,23,42,0.96);
  color: #e5e7eb;
  font-size: 12px;
  box-shadow: 0 10px 30px rgba(15,23,42,0.75);
  z-index: 3200;
  pointer-events: none;
}

/* Higher-visibility warning variant (used for protected/no-overwrite alerts) */
.pushdown-hint.warn {
  background: #f59e0b;          /* orange */
  color: #0b1220;               /* near-black */
  border: 1px solid rgba(11,18,32,0.85);
  font-size: 14px;
  font-weight: 700;
  padding: 8px 14px;
  box-shadow: 0 12px 36px rgba(15,23,42,0.55);
}

/* Error variant — replaces alert() for failures */
.pushdown-hint.error {
  background: #dc2626;
  color: #fff;
  border: 1px solid rgba(255,255,255,0.15);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 14px;
  box-shadow: 0 12px 36px rgba(15,23,42,0.65);
}

/* Info variant — replaces alert() for neutral confirmations */
.pushdown-hint.info {
  background: #0ea5e9;
  color: #fff;
  border: 1px solid rgba(255,255,255,0.15);
  font-size: 13px;
  font-weight: 500;
  padding: 8px 14px;
  box-shadow: 0 12px 36px rgba(15,23,42,0.55);
}


#guestViewNotice.modal-backdrop {
  z-index: 10500;
  background: rgba(15, 23, 42, 0.42);
  backdrop-filter: blur(2px);
  pointer-events: auto;
}
#guestViewNotice .modal {
  top: 50%;
  right: auto;
  left: 50%;
  transform: translate(-50%, -50%);
  min-width: 380px;
  max-width: min(520px, calc(100vw - 32px));
  padding: 18px 20px;
  border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.22), 0 2px 8px rgba(0,0,0,0.08);
}
#guestViewNotice .modal h2 {
  margin: 0 0 10px 0;
  font-size: 20px;
  letter-spacing: 0.2px;
  border-bottom: 1px solid #e5e7eb;
  padding-bottom: 8px;
  cursor: default;
}
#guestViewNotice .actions {
  margin-top: 16px;
  justify-content: flex-end;
}


#tryoutCommitModal.modal-backdrop {
  z-index: 10500;
  background: rgba(15, 23, 42, 0.42);
  backdrop-filter: blur(2px);
  pointer-events: auto;
}
#tryoutCommitModal .modal {
  top: 50%;
  right: auto;
  left: 50%;
  transform: translate(-50%, -50%);
  min-width: 380px;
  max-width: min(520px, calc(100vw - 32px));
  padding: 18px 20px;
  border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.22), 0 2px 8px rgba(0,0,0,0.08);
}
#tryoutCommitModal .modal h2 {
  margin: 0 0 10px 0;
  font-size: 20px;
  letter-spacing: 0.2px;
  border-bottom: 1px solid #e5e7eb;
  padding-bottom: 8px;
  cursor: default;
}
#tryoutCommitModal .actions {
  margin-top: 16px;
  justify-content: flex-end;
}
#tryoutCommitModal .actions .auth-primary {
  background: #2563eb;
  color: #fff;
  border-color: #1d4ed8;
}
#tryoutCommitModal .actions .auth-primary:hover { background: #1d4ed8; }

#editorHelpModal.modal-backdrop {
  z-index: 10480;
  background: rgba(15, 23, 42, 0.42);
  backdrop-filter: blur(2px);
  pointer-events: auto;
}
#editorHelpModal .modal {
  top: 50%;
  right: auto;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(860px, calc(100vw - 32px));
  min-width: 320px;
  max-height: calc(100vh - 48px);
  overflow: auto;
  padding: 18px 20px 16px;
  border-radius: 12px;
  box-shadow: 0 18px 42px rgba(0,0,0,0.24), 0 2px 8px rgba(0,0,0,0.08);
}
#editorHelpModal .modal h2 {
  margin: 0 0 10px 0;
  font-size: 22px;
  letter-spacing: 0.2px;
  border-bottom: 1px solid #e5e7eb;
  padding-bottom: 8px;
  cursor: default;
}
.editor-help-intro {
  color: #334155;
  line-height: 1.55;
  margin-bottom: 12px;
}
.editor-help-intro p {
  margin: 0 0 10px 0;
}
.editor-help-guest-note {
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  color: #1e3a8a;
}
.editor-help-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 14px 0 14px 0;
}
.editor-help-tab {
  border-radius: 999px;
  background: #e2e8f0;
  color: #0f172a;
  border: 1px solid #cbd5e1;
  padding: 6px 12px;
  font-size: 12px;
}
.editor-help-tab:hover {
  background: #dbeafe;
  border-color: #93c5fd;
}
.editor-help-tab.active {
  background: linear-gradient(135deg,#3b82f6,#22c55e);
  color: #0b1120;
  border-color: rgba(15,23,42,0.85);
  font-weight: 700;
}
.editor-help-panels {
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  background: #f8fafc;
  padding: 14px 16px;
}
.editor-help-panel h3 {
  margin: 0 0 8px 0;
  font-size: 18px;
  color: #0f172a;
}
.editor-help-panel p,
.editor-help-panel li {
  color: #334155;
  line-height: 1.6;
  font-size: 14px;
}
.editor-help-panel ul,
.editor-help-panel ol {
  margin: 0 0 10px 18px;
  padding: 0;
}
.editor-help-panel p:last-child {
  margin-bottom: 0;
}
#editorHelpModal .actions {
  margin-top: 14px;
  justify-content: flex-end;
}



/* Enhanced resize handles */
.resize-handle {
    width: 16px;
    height: 16px;
    background: #ffffff;
    border: 2px solid #0f172a;
    border-radius: 3px;
    position: absolute;
    z-index: 20;
    box-shadow: 0 2px 6px rgba(0,0,0,0.35);
}
.resize-handle::before {
    content: '';
    position: absolute;
    inset: -8px;
}
.resize-handle.nw { cursor: nwse-resize; }
.resize-handle.ne { cursor: nesw-resize; }
.resize-handle.sw { cursor: nesw-resize; }
.resize-handle.se { cursor: nwse-resize; }
.resize-handle:hover {
    transform: scale(1.15);
}



/* Cursor fixes for resize handles */
.resize-handle {
    cursor: pointer;
}
.resize-handle.nw,
.resize-handle.se {
    cursor: nwse-resize;
}
.resize-handle.ne,
.resize-handle.sw {
    cursor: nesw-resize;
}
/* ensure hitbox does not block cursor */
.resize-handle::before {
    pointer-events: none;
}


/* Keep the container cursor in sync while hovering image resize corners. */
.mosaic-wrap {
  cursor: inherit;
}



/* Desktop editor layout: reserve a right-side dock for active tool panels */
body.editor-side-panel-open main {
  padding-right: 372px;
  transition: padding-right 160ms ease;
}
main {
  transition: padding-right 160ms ease;
}

/* Make the header and toolbar handle tighter widths more gracefully */
header {
  justify-content: flex-start;
  padding-left: 10px;
  padding-right: 10px;
}
header .toolbar {
  flex: 1 1 720px;
  min-width: 0;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  gap: 4px 5px;
  border-radius: 22px;
  padding: 8px 10px;
}
.tools {
  flex-wrap: wrap;
  margin-left: 0;
  max-width: 100%;
}
.tool-btn {
  flex: 0 0 auto;
}
.toolbar > * {
  min-width: 0;
}

/* Convert the tool panels into a vertical dock on larger screens */
@media (min-width: 1180px) {
  #toolPanel,
  #textPanel,
  #imagePanel {
    position: fixed;
    top: 84px;
    right: 0;
    bottom: 10px;
    left: auto;
    width: 360px;
    background: transparent;
    z-index: 1200;
    pointer-events: none;
  }
  #toolPanel .modal,
  #textPanel .modal,
  #imagePanel .modal {
    position: absolute !important;
    top: 0 !important;
    right: 16px !important;
    left: auto !important;
    bottom: 0 !important;
    transform: none !important;
    width: 340px !important;
    min-width: 0 !important;
    max-width: 340px !important;
    max-height: calc(100vh - 110px) !important;
    overflow: auto;
    border-radius: 18px;
    padding: 14px 14px 12px;
    box-shadow: 0 16px 40px rgba(2, 6, 23, 0.30), 0 3px 10px rgba(2, 6, 23, 0.16);
  }
  #toolPanel .modal .row,
  #textPanel .modal .row,
  #imagePanel .modal .row {
    flex-direction: column;
    align-items: stretch;
  }
  #toolPanel .modal .row label,
  #textPanel .modal .row label,
  #imagePanel .modal .row label {
    justify-content: space-between;
  }
  #toolPanel .modal input[type="range"],
  #textPanel .modal input[type="range"],
  #imagePanel .modal input[type="range"],
  #toolPanel .modal select,
  #textPanel .modal select,
  #imagePanel .modal select {
    width: 100%;
  }
  #toolPanel .modal .actions,
  #textPanel .modal .actions,
  #imagePanel .modal .actions {
    justify-content: flex-start;
    flex-wrap: wrap;
  }
  #toolPanel .modal .modal-close,
  #textPanel .modal .modal-close,
  #imagePanel .modal .modal-close {
    display: none;
  }
  #toolPanel .modal h2,
  #textPanel .modal h2,
  #imagePanel .modal h2 {
    cursor: default;
    font-size: 18px;
    margin-bottom: 10px;
    padding-right: 0;
  }
}

/* Medium widths: tighten toolbar and hide some labels before it gets cramped */
@media (max-width: 1279px) {
  .tool-btn .label {
    font-size: 11px;
  }
  header h1 {
    margin-right: 8px;
  }
}

/* Smaller editor widths: make the toolbar horizontally scrollable and use compact panels */
@media (max-width: 1179px) {
  body.editor-side-panel-open main {
    padding-right: 0;
  }
  header {
    gap: 8px;
  }
  header .toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }
  .tools {
    flex-wrap: nowrap;
  }
  #toolPanel,
  #textPanel,
  #imagePanel {
    z-index: 1200;
  }
  #toolPanel .modal,
  #textPanel .modal,
  #imagePanel .modal {
    width: min(420px, calc(100vw - 24px)) !important;
    max-width: calc(100vw - 24px) !important;
    min-width: 0 !important;
    top: 84px !important;
    right: 12px !important;
    left: auto !important;
    bottom: auto !important;
    max-height: calc(100vh - 120px) !important;
    overflow: auto;
  }
}

/* Very narrow screens: focus on icons and keep panels full-width near the bottom */
@media (max-width: 760px) {
  .tool-btn {
    padding: 4px 8px;
  }
  .tool-btn .label {
    display: none;
  }
  header h1 {
    font-size: 14px;
    margin-right: 4px;
  }
  #toolPanel .modal,
  #textPanel .modal,
  #imagePanel .modal {
    left: 12px !important;
    right: 12px !important;
    width: auto !important;
  }
}



/* Desktop editor layout: reserve a narrower right-side dock for active tool panels */
body.editor-side-panel-open main {
  padding-right: clamp(220px, 18vw, 280px);
  transition: padding-right 160ms ease;
}
main {
  transition: padding-right 160ms ease;
}

/* Make the header and toolbar handle tighter widths more gracefully */
header {
  justify-content: flex-start;
  padding-left: 10px;
  padding-right: 10px;
}
header .toolbar {
  flex: 1 1 720px;
  min-width: 0;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  gap: 4px 5px;
  border-radius: 22px;
  padding: 8px 10px;
}
.tools {
  flex-wrap: wrap;
  margin-left: 0;
  max-width: 100%;
}
.tool-btn {
  flex: 0 0 auto;
}
.toolbar > * {
  min-width: 0;
}

/* Convert the tool panels into a vertical dock on larger screens */
@media (min-width: 1180px) {
  #toolPanel,
  #textPanel,
  #imagePanel {
    position: fixed;
    top: 84px;
    right: 0;
    bottom: 10px;
    left: auto;
    width: clamp(220px, 18vw, 280px);
    background: transparent;
    z-index: 1200;
    pointer-events: none;
  }
  #toolPanel .modal,
  #textPanel .modal,
  #imagePanel .modal {
    position: absolute !important;
    top: 0 !important;
    right: 12px !important;
    left: auto !important;
    bottom: 0 !important;
    transform: none !important;
    width: clamp(208px, 17vw, 268px) !important;
    min-width: 0 !important;
    max-width: clamp(208px, 17vw, 268px) !important;
    max-height: calc(100vh - 110px) !important;
    overflow: auto;
    border-radius: 18px;
    padding: 12px 12px 10px;
    box-shadow: 0 16px 40px rgba(2, 6, 23, 0.30), 0 3px 10px rgba(2, 6, 23, 0.16);
  }
  #toolPanel .modal h2,
  #textPanel .modal h2,
  #imagePanel .modal h2 {
    position: sticky;
    top: 0;
    background: #fff;
    z-index: 2;
    margin: -12px -12px 10px -12px;
    padding: 12px 36px 10px 12px;
    border-bottom: 1px solid #e2e8f0;
  }
  #toolPanel .modal .modal-close,
  #textPanel .modal .modal-close,
  #imagePanel .modal .modal-close {
    top: 10px;
    right: 10px;
    width: 24px;
    height: 24px;
    border-radius: 999px;
    background: #f8fafc;
    border: 1px solid #cbd5e1;
    color: #334155;
    font-size: 16px;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    z-index: 3;
  }
  #toolPanel .modal .row,
  #textPanel .modal .row,
  #imagePanel .modal .row {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    margin-top: 6px;
  }
  #toolPanel .modal .row label,
  #textPanel .modal .row label,
  #imagePanel .modal .row label {
    justify-content: space-between;
    gap: 8px;
  }
  #toolPanel .modal input[type="color"],
  #textPanel .modal input[type="color"],
  #imagePanel .modal input[type="color"] {
    width: 46px;
    min-width: 46px;
    height: 28px;
    padding: 0;
  }
  #toolPanel .modal input[type="range"],
  #textPanel .modal input[type="range"],
  #imagePanel .modal input[type="range"],
  #toolPanel .modal select,
  #textPanel .modal select,
  #imagePanel .modal select {
    width: 100%;
    max-width: 100%;
  }
  #toolPanel .modal input[type="number"],
  #textPanel .modal input[type="number"],
  #imagePanel .modal input[type="number"] {
    width: 72px;
    max-width: 72px;
  }
  #toolPanel .modal .actions,
  #textPanel .modal .actions,
  #imagePanel .modal .actions {
    justify-content: stretch;
  }
  #toolPanel .modal .actions > *,
  #textPanel .modal .actions > *,
  #imagePanel .modal .actions > * {
    flex: 1 1 auto;
  }
}

/* On medium widths, keep panels floating but let toolbar scroll instead of crowding */
@media (max-width: 1179px) {
  header .toolbar {
    overflow-x: auto;
    overflow-y: hidden;
    flex-wrap: nowrap;
    scrollbar-width: thin;
  }
  .tools {
    flex-wrap: nowrap;
  }
}

/* On tighter screens, reduce label footprint to avoid toolbar collisions */
@media (max-width: 920px) {
  .tool-btn .label {
    display: none;
  }
  header .toolbar {
    gap: 6px;
    padding: 8px;
  }
}


/* Tighter docked panel control layout */
#toolPanel .modal label,
#textPanel .modal label,
#imagePanel .modal label {
  font-size: 13px;
}
#toolPanel .modal .row + .row,
#textPanel .modal .row + .row,
#imagePanel .modal .row + .row {
  margin-top: 4px;
}



/* Dock spacing polish */
@media (min-width: 1180px) {
  #toolPanel,
  #textPanel,
  #imagePanel {
    top: 108px !important;
    bottom: auto !important;
    min-height: 0 !important;
    height: auto !important;
  }

  #toolPanel .modal,
  #textPanel .modal,
  #imagePanel .modal {
    top: 0 !important;
    bottom: auto !important;
    max-height: calc(100vh - 156px) !important;
    min-height: 260px !important;
    height: auto !important;
    padding: 16px 14px 14px !important;
  }

  #toolPanel .modal h2,
  #textPanel .modal h2,
  #imagePanel .modal h2 {
    margin: -16px -14px 16px -14px !important;
    padding: 14px 40px 14px 14px !important;
  }

  #toolPanel .modal .modal-close,
  #textPanel .modal .modal-close,
  #imagePanel .modal .modal-close {
    top: 12px !important;
    right: 10px !important;
  }

  #toolPanel .modal .row,
  #textPanel .modal .row,
  #imagePanel .modal .row {
    gap: 8px !important;
    margin-top: 10px !important;
  }

  #toolPanel .modal .row:first-of-type,
  #textPanel .modal .row:first-of-type,
  #imagePanel .modal .row:first-of-type {
    margin-top: 2px !important;
  }

  #toolPanel .modal label,
  #textPanel .modal label,
  #imagePanel .modal label {
    margin-bottom: 2px !important;
  }

  #toolPanel .modal .actions,
  #textPanel .modal .actions,
  #imagePanel .modal .actions {
    margin-top: 14px !important;
  }
}



/* Dock tighten pass */
@media (min-width: 1180px) {
  body.editor-side-panel-open main {
    padding-right: clamp(190px, 16vw, 242px) !important;
  }

  #toolPanel,
  #textPanel,
  #imagePanel {
    width: clamp(196px, 16vw, 248px) !important;
  }

  #toolPanel .modal,
  #textPanel .modal,
  #imagePanel .modal {
    right: 8px !important;
    width: clamp(188px, 15.5vw, 236px) !important;
    max-width: clamp(188px, 15.5vw, 236px) !important;
    padding: 10px 10px 12px !important;
  }

  #toolPanel .modal h2,
  #textPanel .modal h2,
  #imagePanel .modal h2 {
    margin: -10px -10px 10px -10px !important;
    padding: 10px 36px 8px 12px !important;
  }

  #toolPanel .modal .row:first-of-type,
  #textPanel .modal .row:first-of-type,
  #imagePanel .modal .row:first-of-type {
    margin-top: 8px !important;
  }

  #toolPanel .modal .row,
  #textPanel .modal .row,
  #imagePanel .modal .row {
    gap: 6px !important;
    margin-top: 8px !important;
  }

  #toolPanel .modal input[type="color"],
  #textPanel .modal input[type="color"],
  #imagePanel .modal input[type="color"] {
    margin-top: 2px !important;
  }
}



/* Server-update notification banner */
.server-update-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 10px 12px 0;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(245, 158, 11, 0.45);
  background: rgba(120, 53, 15, 0.14);
  color: #fde68a;
  box-shadow: 0 6px 18px rgba(2, 6, 23, 0.16);
  position: relative;
  z-index: 25;
}

@media (min-width: 1180px) {
  body.editor-side-panel-open .server-update-banner {
    margin-right: calc(clamp(190px, 16vw, 242px) + 18px) !important;
  }
}
.server-update-banner[hidden] {
  display: none !important;
}
.server-update-banner__content {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  align-items: center;
  line-height: 1.45;
  min-width: 0;
}
.server-update-banner__content strong {
  color: #fef3c7;
  white-space: nowrap;
}
.server-update-banner__content span {
  color: #fde68a;
}
.server-update-banner__actions {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  flex: 0 0 auto;
}
.server-update-banner__actions button {
  border: 1px solid rgba(251, 191, 36, 0.55);
  background: rgba(30, 41, 59, 0.72);
  color: #fef3c7;
  border-radius: 999px;
  padding: 8px 12px;
  font-weight: 600;
  cursor: pointer;
}
.server-update-banner__actions button.secondary {
  background: transparent;
  border-color: rgba(148, 163, 184, 0.45);
  color: #e2e8f0;
}
.server-update-banner__actions button:hover {
  filter: brightness(1.08);
}
@media (max-width: 920px) {
  .server-update-banner {
    flex-direction: column;
    align-items: stretch;
  }
  .server-update-banner__actions {
    width: 100%;
    justify-content: flex-end;
  }
}




/* Hide toolbar contents until auth + page state are initialized to avoid the
   login/tools flicker before we know the real session state. */
body.app-loading header .toolbar {
  visibility: hidden;
}
body.app-loading #toolPanel,
body.app-loading #textPanel,
body.app-loading #imagePanel {
  display: none !important;
}




/* Temporary lock-zone warning */
.lock-warning-toast {
  position: fixed;
  left: 50%;
  top: 92px;
  transform: translateX(-50%);
  z-index: 1700;
  padding: 10px 14px;
  border-radius: 12px;
  background: rgba(91, 33, 182, 0.94);
  color: #ffffff;
  border: 1px solid rgba(255,255,255,0.18);
  box-shadow: 0 10px 28px rgba(2,6,23,0.28);
  font-weight: 600;
  line-height: 1.35;
  pointer-events: none;
  opacity: 1;
  transition: opacity 160ms ease, transform 160ms ease;
}
.lock-warning-toast[hidden] {
  display: none !important;
}
.lock-warning-toast.is-hiding {
  opacity: 0;
  transform: translateX(-50%) translateY(-8px);
}
@media (max-width: 920px) {
  .lock-warning-toast {
    top: 82px;
    max-width: calc(100vw - 24px);
    text-align: center;
  }
}




/* Single help button next to hamburger */
.toolbar-sep {
  display: none !important;
}
#editorHelpOpen {
  min-width: 24px !important;
  width: 24px !important;
  height: 24px !important;
  padding: 0 !important;
  display: inline-flex !important;
  align-items: center !important;
  justify-content: center !important;
  font-weight: 700 !important;
  font-size: 13px !important;
  line-height: 1 !important;
}
#editorHelpOpen .icon,
#editorHelpOpen .label,
#editorHelpOpen::before,
#editorHelpOpen::after {
  display: none !important;
  content: none !important;
}



/* Help spacing tweak */
.editor-help-panel p + p {
  margin-top: 12px; /* more space from paragraph above */
}

.editor-help-panel p.help-subtext {
  margin-top: 14px;
  margin-bottom: 6px; /* tighter to buttons below */
}



/* Help spacing tweak v2 */
.editor-help-panel .help-subtext {
  display: block;
  margin-top: 14px;
  margin-bottom: 2px !important;
}
.editor-help-nav {
  margin-top: 8px !important;
}



/* Guest help banner */
.editor-help-guest-banner {
  margin: 12px 0 16px 0;
  padding: 12px 14px;
  border-radius: 10px;
  background: rgba(37, 99, 235, 0.18);
  border: 1px solid rgba(37, 99, 235, 0.35);
  color: #dbeafe;
  font-weight: 500;
}
.editor-help-guest-banner strong {
  display: block;
  font-weight: 700;
  margin-bottom: 4px;
}

/* ── Tool-panel-bar mode (MOSAIC_TOOL_PANEL=bar) ─────────────────────────── */
/* The tool/text sub-toolbars sit below <header> as a sticky strip. Pen,
 * eraser, line, rect, oval, selection, text — every tool must render in the
 * SAME fixed-height strip, regardless of how many .row / .actions blocks
 * were moved in from its source .modal. This prevents the canvas from
 * shifting and avoids a vertical scrollbar appearing.
 *
 * Mechanism:
 *   1. Fixed height; box-sizing: border-box so padding doesn't inflate it.
 *   2. flex-wrap: nowrap + overflow-x: auto — if a tool has too many
 *      controls for the viewport, the bar scrolls horizontally instead of
 *      growing taller. Height stays constant across every tool.
 *   3. Override .modal .row.erase-options { flex-direction: column } which
 *      would otherwise stack the eraser's controls vertically and blow out
 *      the bar height.
 *   4. Zero margin-top on all .row and .actions children (they inherit
 *      8px / 12px top-margin from the .modal base styles).
 *   5. Hide auxiliary modal chrome that travels with the moved contents
 *      (h2 title, modal-close button, help paragraphs).
 */
#toolBarDiv,
#textBarDiv {
  /* Hidden until the matching panel opens */
  display: none;
  position: sticky;
  top: 0;
  z-index: 9;
  background: #0f172a;
  border-bottom: 1px solid #334155;

  /* Fixed strip — same height for every tool. Kept compact to reclaim
     vertical space for the mosaic below. */
  height: 30px;
  box-sizing: border-box;
  padding: 2px 10px;
  gap: 10px;
  align-items: center;

  /* Never wrap vertically; scroll horizontally if content overflows */
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  white-space: nowrap;
}
#toolBarDiv:not([hidden]),
#textBarDiv:not([hidden]) { display: flex; }

/* Chrome that travels with the moved .modal contents but has no place on
 * the bar: the panel title, the close button, and the guest-help paragraphs. */
#toolBarDiv > h2,
#textBarDiv > h2,
#toolBarDiv .modal-close,
#textBarDiv .modal-close,
#toolBarDiv > p,
#textBarDiv > p { display: none; }

/* Each .row / .actions becomes one flex item on the bar. They must stay on
 * a single line and must NOT inherit the column stacking that .modal gives
 * to .row.erase-options. */
#toolBarDiv .row,
#toolBarDiv .actions,
#textBarDiv .row,
#textBarDiv .actions {
  display: flex;
  flex: 0 0 auto;
  flex-direction: row;   /* overrides .modal .row.erase-options column mode */
  flex-wrap: nowrap;
  align-items: center;
  gap: 8px;
  margin: 0;             /* kill .modal .row margin-top and .actions margin-top */
  padding: 0;
  min-width: 0;
}

/* Inside the bar, erase-options labels shouldn't stretch to 100% width
 * (that's the modal-only behavior on .modal .row.erase-options label). */
#toolBarDiv .row.erase-options label,
#textBarDiv .row.erase-options label {
  width: auto;
  justify-content: flex-start;
}

/* Compact, uniform control sizing across every bar. Sized to fit a 30px
 * strip with ~2px of vertical padding on each side. */
#toolBarDiv label,
#textBarDiv label {
  color: #94a3b8;
  font-size: 12px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin: 0;
  white-space: nowrap;
}
#toolBarDiv input[type="number"],
#textBarDiv input[type="number"] {
  width: 48px; height: 22px;
  background: #1e293b; color: #e2e8f0;
  border: 1px solid #334155; border-radius: 4px;
  padding: 0 4px; font-size: 12px; line-height: 1;
  box-sizing: border-box;
}
#toolBarDiv input[type="range"],
#textBarDiv input[type="range"] {
  accent-color: #60a5fa; width: 72px; height: 16px; margin: 0;
}
#toolBarDiv input[type="color"],
#textBarDiv input[type="color"] {
  width: 26px; height: 20px; padding: 0;
  border: 1px solid #334155; border-radius: 3px; cursor: pointer;
}
#toolBarDiv input[type="checkbox"],
#textBarDiv input[type="checkbox"] { margin: 0; }
#toolBarDiv button,
#textBarDiv button {
  font-size: 12px; line-height: 1;
  padding: 3px 10px; height: 22px;
  white-space: nowrap;
}
#toolBarDiv select,
#textBarDiv select {
  background: #1e293b; color: #e2e8f0;
  border: 1px solid #334155; border-radius: 4px;
  font-size: 12px; line-height: 1;
  padding: 0 4px; height: 22px;
  box-sizing: border-box;
}
#toolBarDiv span[id$="Value"],
#textBarDiv span[id$="Value"] {
  font-size: 11px; color: #94a3b8; min-width: 24px;
}

/* In horizontal bar mode (MOSAIC_TOOL_PANEL=bar), the added row of controls
 * pushes the canvas down and can force a vertical scrollbar. The
 * under-canvas caption lines are informational only, so hide them while a
 * sub-toolbar is visible to reclaim that vertical space.
 *
 * Scoped to `body.tool-panel-bar` so it does NOT fire in vbar mode, where
 * the sub-toolbar is on the side and doesn't compete for vertical space.
 * Hiding captions there was a bug: it made layoutMosaic's reservedH swing
 * each time a tool was picked, which jumped the mosaic between integer
 * scale steps. */
body.tool-panel-bar:has(#toolBarDiv:not([hidden])) #underCanvasText,
body.tool-panel-bar:has(#toolBarDiv:not([hidden])) #underCanvasText2,
body.tool-panel-bar:has(#textBarDiv:not([hidden])) #underCanvasText,
body.tool-panel-bar:has(#textBarDiv:not([hidden])) #underCanvasText2 {
  display: none !important;
}

/* ── Tool-panel-vbar mode (MOSAIC_TOOL_PANEL=vbar) ────────────────────────── */
/* Same content as bar mode — the panel contents are moved into #toolBarDiv
 * and #textBarDiv — but rendered as a narrow VERTICAL strip floating on
 * the right-hand side of the viewport.
 *
 * Key design choice: the vbar is position:fixed and does NOT take a column
 * in the page layout. That means <main> keeps its full width and the
 * mosaic stays centered in the viewport — it only shrinks (via
 * layoutMosaic) if its natural size would grow into the vbar's lane.
 * For a small mosaic with empty space around it, toggling the vbar on or
 * off produces no visible shift at all.
 *
 * The --header-h and --footer-h CSS custom properties are set from JS
 * (see main.js) so the vbar's top/bottom match the actual header/footer
 * heights — fallbacks are provided for the first paint before JS runs.
 *
 * Internally the vbar is deliberately narrow:
 *   - Each .row stacks its children (labels) vertically — one item per line.
 *   - Each <label> stacks its text above its control, so long label text
 *     like "Brush Size" gets its own line rather than needing horizontal
 *     room beside the control. This is what lets the bar be ~120px wide.
 *   - Range sliders are rendered vertically (~22×80px) so they fit the
 *     narrow column without overflowing.
 *   - Apply/Cancel stack vertically too (they don't fit side-by-side at
 *     this width).
 */
body.tool-panel-vbar #toolBarDiv,
body.tool-panel-vbar #textBarDiv {
  /* Float above <main> on the right, anchored between header and footer. */
  position: fixed;
  top: var(--header-h, 56px);
  right: 0;
  bottom: var(--footer-h, 18px);
  left: auto;
  width: 120px;
  height: auto;
  min-height: 0;
  padding: 10px 12px;
  gap: 10px;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: stretch;
  justify-content: flex-start;
  overflow-x: hidden;
  overflow-y: auto;
  white-space: normal;       /* allow wrapping inside the column */
  border-bottom: none;       /* no bottom border — we're on the side */
  border-left: 1px solid #334155;
  z-index: 20;               /* above main content, below header (z:10) if overlapping */
  box-shadow: -6px 0 18px rgba(2, 6, 23, 0.35);
}

/* Show the panel title at the top of the vbar. In horizontal-bar mode this
 * was hidden because there was no room; vertically it's the natural header. */
body.tool-panel-vbar #toolBarDiv > h2,
body.tool-panel-vbar #textBarDiv > h2 {
  display: block;
  margin: 0 0 2px 0;
  padding: 0;
  color: #e2e8f0;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: default;
  text-align: center;
}

/* Each .row is a vertical stack of labels, one per line. */
body.tool-panel-vbar #toolBarDiv .row,
body.tool-panel-vbar #textBarDiv .row {
  flex: 0 0 auto;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: stretch;
  justify-content: flex-start;
  gap: 8px;
  width: 100%;
}

/* Apply/Cancel also stack vertically — they don't fit side-by-side in
 * a 120px-wide column. Place them right under the title — using flex
 * `order` so the DOM stays unchanged (panel/bar modes still get the
 * default end-of-modal placement). The order values are explicit on
 * h2 and .row too so any future additions don't accidentally land
 * above the action buttons. */
body.tool-panel-vbar #toolBarDiv > h2,
body.tool-panel-vbar #textBarDiv > h2 {
  order: 0;
}
body.tool-panel-vbar #toolBarDiv .actions,
body.tool-panel-vbar #textBarDiv .actions {
  order: 1;                  /* right under the title */
  flex: 0 0 auto;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: stretch;
  justify-content: flex-start;
  gap: 6px;
  width: 100%;
  margin-top: 0;             /* no longer pinned to the bottom */
  padding-top: 4px;
  padding-bottom: 6px;
  border-bottom: 1px solid #334155;  /* visual divider between actions and the option rows */
}
body.tool-panel-vbar #toolBarDiv .row,
body.tool-panel-vbar #textBarDiv .row {
  order: 2;                  /* below the actions block */
}

/* Every <label> stacks its text above its control so the label never has
 * to share horizontal space with a long caption. Overrides the inline
 * style="display:inline-flex; align-items:center" on individual labels. */
body.tool-panel-vbar #toolBarDiv label,
body.tool-panel-vbar #textBarDiv label {
  display: flex !important;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 3px;
  width: 100%;
  text-align: center;
}

/* Render range sliders vertically. Narrow + tall so they fit the 120px
 * column and give decent pointer travel. writing-mode + direction:rtl is
 * the modern standard (Chromium 119+, Firefox 4+, Safari 14.1+) and
 * supersedes the deprecated -webkit-appearance: slider-vertical, which
 * Chromium has scheduled for removal. */
body.tool-panel-vbar #toolBarDiv input[type="range"],
body.tool-panel-vbar #textBarDiv input[type="range"] {
  writing-mode: vertical-lr;            /* modern Chromium / Firefox / Safari */
  direction: rtl;                       /* max value at top (usual convention) */
  width: 22px;
  height: 80px;
  flex: 0 0 auto;
  min-width: 0;
  margin: 2px 0;
}

/* Number inputs: uniform width centered in the column. */
body.tool-panel-vbar #toolBarDiv input[type="number"],
body.tool-panel-vbar #textBarDiv input[type="number"] {
  width: 60px;
}

/* Font <select> in the text vbar: fill the column width. */
body.tool-panel-vbar #textBarDiv select {
  width: 100%;
  max-width: 100%;
}

/* Button flash feedback */
@keyframes btn-flash {
  0%   { filter: brightness(1); }
  30%  { filter: brightness(1.8); }
  100% { filter: brightness(1); }
}
.btn-flash { animation: btn-flash 0.4s ease-out; }

/* ──────────────────────────────────────────────────────────────────────────
   Icon picker panel
   ────────────────────────────────────────────────────────────────────────── */
.icon-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 6px;
  padding: 4px;
  background: rgba(15, 23, 42, 0.4);
  border: 1px solid #334155;
  border-radius: 8px;
}
.icon-grid button {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1 / 1;
  background: rgba(30, 41, 59, 0.6);
  border: 1px solid #475569;
  border-radius: 6px;
  cursor: pointer;
  color: #e2e8f0;
  padding: 8px;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.icon-grid button:hover {
  background: rgba(99, 102, 241, 0.25);
  border-color: #818cf8;
  color: #fff;
}
.icon-grid button:focus-visible {
  outline: 2px solid #6366f1;
  outline-offset: 1px;
}
.icon-grid button:active { transform: translateY(1px); }
.icon-grid button svg {
  width: 100%;
  height: 100%;
  pointer-events: none; /* clicks land on the button, not the SVG */
}

/* ──────────────────────────────────────────────────────────────────────────
   Zoom cluster — tighten the (−, Zoom, +, Reset, 100%) group so it doesn't
   eat half the toolbar. The cluster is its own inline-flex with smaller
   gaps and tighter button padding than the rest of the toolbar.
   ────────────────────────────────────────────────────────────────────────── */
.zoom-controls {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  flex: 0 0 auto;
}
.zoom-controls > button {
  /* All zoom buttons (− Zoom + Reset) share this tighter look. The single-
     character ± buttons get extra-tight padding via the more specific rule
     below. tool-btn styling still applies (border, background, hover). */
  padding: 3px 7px;
}
.zoom-controls > button#zoomOut,
.zoom-controls > button#zoomIn {
  /* Single-glyph buttons don't need the same horizontal breathing room. */
  padding: 3px 6px;
  min-width: 24px;
  justify-content: center;
}
.zoom-info {
  font-size: 11px;
  color: rgba(226,232,240,0.55);
  margin-left: 2px;
  font-variant-numeric: tabular-nums;
  min-width: 32px;
  text-align: right;
  white-space: nowrap;
}

/* Hide the redundant zoom percentage indicator on narrower viewports
   (below 1600px). The − / Reset / + buttons still show zoom state, and
   the canvas itself visually scales — losing the literal percent number
   reclaims ~35px and lets Commit/REVERT/Funds stay on the same toolbar
   row for typical 16:9 laptops. Wide monitors keep the indicator. */
@media (max-width: 1599px) {
  .zoom-info { display: none; }
}

/* ──────────────────────────────────────────────────────────────────────────
   Mobile responsive chrome (FE 4.43.4)

   The previous narrow-screen rule kept the entire editor toolbar on one
   horizontally scrolling pill. That technically fit, but on a phone it made
   controls overlap visually and hid actions off-screen. Keep desktop/tablet
   behavior unchanged; on portrait phones allow sensible wrapping, give touch
   targets room, and keep the drawing-tool strip independently scrollable.
   ────────────────────────────────────────────────────────────────────────── */
@media (max-width: 760px) and (orientation: portrait) {
  header {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: 6px;
    padding: 7px 8px 8px;
    margin-bottom: 3px;
  }

  header h1 {
    width: 100%;
    max-width: none;
    margin: 0;
    padding: 0 2px;
    font-size: 18px;
    line-height: 1.2;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  header .toolbar {
    width: 100%;
    flex: 0 0 auto;
    min-width: 0;
    flex-wrap: wrap;
    overflow-x: visible;
    overflow-y: visible;
    gap: 6px;
    padding: 7px;
    border-radius: 15px;
    box-shadow: 0 5px 16px rgba(15,23,42,0.45);
  }

  /* Desktop spacer elements are useful for spreading groups across a long
     row, but create giant gaps when that row wraps on a phone. */
  header .toolbar > .spacer {
    display: none;
  }

  /* Drawing tools get their own compact, horizontally scrollable lane. This
     prevents a dozen tool buttons from making the header several rows tall. */
  header .toolbar .tools {
    flex: 1 1 100%;
    width: 100%;
    min-width: 0;
    margin: 0;
    padding: 0 0 2px;
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 5px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  header .toolbar .tools::-webkit-scrollbar { display: none; }

  /* Finger-sized targets without the oversized overlapping pill appearance. */
  header .toolbar button,
  header .toolbar .tool-btn,
  header .toolbar .toolbar-auth-btn {
    min-width: 40px;
    min-height: 40px;
    padding: 6px 9px;
    border-radius: 10px;
    flex: 0 0 auto;
  }

  header .toolbar .tool-btn .label {
    display: none;
  }

  .zoom-controls {
    gap: 4px;
    flex: 0 0 auto;
  }
  .zoom-controls > button,
  .zoom-controls > button#zoomOut,
  .zoom-controls > button#zoomIn {
    min-width: 40px;
    min-height: 40px;
    padding: 6px 9px;
    border-radius: 10px;
  }

  .auth-controls {
    margin-left: auto;
    gap: 5px;
  }

  #authOpen,
  #editorHelpOpen {
    min-width: 40px !important;
    width: 40px !important;
    height: 40px !important;
  }

  /* Let bottom content clear gesture/navigation areas on devices that expose
     safe-area insets. */
  footer {
    padding-bottom: max(2px, env(safe-area-inset-bottom));
  }
}

/* Phone landscape mode: maximize vertical room for the mosaic. Once Android
   rotates the viewport, this rule switches the title + toolbar back to a
   compact single row and the existing canvas resize code refits automatically. */
@media (orientation: landscape) and (max-height: 600px) {
  header {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: flex-start;
    gap: 6px;
    padding: 4px 6px;
    margin-bottom: 2px;
  }

  header h1 {
    flex: 0 1 110px;
    max-width: 110px;
    margin: 0;
    font-size: 13px;
    line-height: 1.1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  header .toolbar {
    flex: 1 1 auto;
    width: auto;
    min-width: 0;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 4px;
    padding: 4px 6px;
    border-radius: 12px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  header .toolbar::-webkit-scrollbar { display: none; }
  header .toolbar > .spacer { display: none; }

  header .toolbar .tools {
    flex: 0 0 auto;
    width: auto;
    margin: 0;
    flex-wrap: nowrap;
    overflow: visible;
    gap: 4px;
  }

  header .toolbar button,
  header .toolbar .tool-btn,
  header .toolbar .toolbar-auth-btn,
  .zoom-controls > button,
  .zoom-controls > button#zoomOut,
  .zoom-controls > button#zoomIn {
    min-width: 34px;
    min-height: 34px;
    padding: 4px 7px;
    border-radius: 9px;
  }

  header .toolbar .tool-btn .label { display: none; }
  #authOpen,
  #editorHelpOpen {
    min-width: 34px !important;
    width: 34px !important;
    height: 34px !important;
  }
}

/* ──────────────────────────────────────────────────────────────────────────
   Mobile chrome revision (FE 4.43.5)

   Stage 1.1 proved that simply wrapping the desktop toolbar makes portrait
   mode far too tall and still lets landscape controls disappear off-screen.
   Keep the desktop DOM/appearance intact via display:contents, but regroup the
   existing controls into deliberate mobile rows. No editor behavior changes.
   ────────────────────────────────────────────────────────────────────────── */
.media-tools,
.history-controls,
.commit-controls {
  display: contents;
}

/* Desktop/tablet guests keep the explicit Home/Mosaics/Login/Sign Up links.
   The guest hamburger is used only by the phone layouts below. */
#authControls[data-auth-state="guest"] #authOpen {
  display: none !important;
}

@media (max-width: 760px) and (orientation: portrait) {
  header {
    padding: 6px 7px 7px;
    gap: 5px;
    margin-bottom: 3px;
  }

  header h1 {
    width: 100%;
    max-width: none;
    margin: 0;
    padding: 0 2px;
    font-size: 18px;
    line-height: 1.15;
  }

  /* Three intentional rows:
       1. drawing tools (side-scroll if a future tool is added)
       2. insert/history/zoom
       3. Commit/Revert + Help/Menu
     This replaces the many-row desktop-toolbar wrap visible in the phone
     screenshot while keeping every editor action reachable. */
  header .toolbar {
    display: grid !important;
    grid-template-columns: auto auto minmax(0, 1fr) auto;
    grid-template-areas:
      "tools  tools   tools  tools"
      "media  history zoom   zoom"
      "commit commit  commit auth";
    align-items: center;
    justify-content: stretch;
    width: 100%;
    min-width: 0;
    gap: 5px 6px;
    padding: 6px;
    overflow: visible;
    border-radius: 14px;
  }

  header .toolbar > .spacer { display: none !important; }

  header .toolbar .tools {
    grid-area: tools;
    display: flex;
    width: 100%;
    min-width: 0;
    margin: 0;
    padding: 0 0 2px;
    gap: 4px;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  header .toolbar .tools::-webkit-scrollbar { display: none; }

  .media-tools {
    grid-area: media;
    display: inline-flex;
    align-items: center;
    gap: 4px;
  }
  .history-controls {
    grid-area: history;
    display: inline-flex;
    align-items: center;
    gap: 4px;
  }
  .history-controls > span[style] { display: none !important; }
  .history-controls #playbackInlineControls { margin-left: 0 !important; }

  .zoom-controls {
    grid-area: zoom;
    justify-self: end;
    display: inline-flex;
    align-items: center;
    gap: 3px;
  }

  .commit-controls {
    grid-area: commit;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    min-width: 0;
  }

  .auth-controls {
    grid-area: auth;
    justify-self: end;
    margin: 0;
    gap: 4px;
  }

  /* Navigation is a menu on phones. That removes four wide text buttons from
     the editor chrome without changing the desktop guest experience. */
  #authControls[data-auth-state="guest"] #toolbarHome,
  #authControls[data-auth-state="guest"] #toolbarMosaics,
  #authControls[data-auth-state="guest"] #toolbarLogin,
  #authControls[data-auth-state="guest"] #toolbarSignup {
    display: none !important;
  }
  #authControls[data-auth-state="guest"] #authOpen {
    display: inline-flex !important;
  }

  header .toolbar button,
  header .toolbar .tool-btn,
  header .toolbar .toolbar-auth-btn,
  .zoom-controls > button,
  .zoom-controls > button#zoomOut,
  .zoom-controls > button#zoomIn {
    min-width: 36px;
    min-height: 36px;
    height: 36px;
    padding: 4px 7px;
    border-radius: 9px;
    flex: 0 0 auto;
  }

  /* Primary drawing tools get a little more finger room because they are the
     buttons used continuously while editing. */
  header .toolbar .tools .tool-btn {
    min-width: 40px;
    width: 40px;
    min-height: 40px;
    height: 40px;
    padding: 5px;
  }

  header .toolbar .tool-btn .label { display: none !important; }

  #zoomBox {
    min-width: 48px !important;
    width: 48px;
  }
  /* Use a compact reset glyph in the constrained mobile toolbar. The desktop
     label remains "Reset" and its accessible/title text is unchanged. */
  #zoomReset {
    min-width: 36px !important;
    width: 36px;
    font-size: 0;
  }
  #zoomReset::before {
    content: "↺";
    font-size: 18px;
    line-height: 1;
  }

  #commit,
  #revert {
    width: auto;
    min-width: 76px;
    padding-left: 11px;
    padding-right: 11px;
  }

  #fundsInfo {
    font-size: 11px;
    white-space: nowrap;
  }

  #authOpen,
  #editorHelpOpen {
    min-width: 36px !important;
    width: 36px !important;
    min-height: 36px !important;
    height: 36px !important;
  }
}

/* Very small portrait phones get one extra compact row rather than squeezing
   insert/history/zoom controls together. */
@media (max-width: 360px) and (orientation: portrait) {
  header .toolbar {
    grid-template-columns: auto auto 1fr auto;
    grid-template-areas:
      "tools  tools   tools  tools"
      "media  history .      ."
      "zoom   zoom    zoom   zoom"
      "commit commit  commit auth";
  }
  .zoom-controls {
    justify-self: start;
  }
}

/* Landscape phones have plenty of horizontal space but very little vertical
   space. Hide the clipped title, use one row for drawing tools and a second
   compact row for all secondary actions. Nothing is allowed to disappear off
   the right edge. */
@media (orientation: landscape) and (max-height: 600px) {
  header {
    display: block;
    padding: 3px 5px 4px;
    margin-bottom: 2px;
  }

  header h1 {
    display: none !important;
  }

  header .toolbar {
    display: flex !important;
    width: 100%;
    min-width: 0;
    flex: 0 0 auto;
    flex-wrap: wrap !important;
    justify-content: center;
    align-items: center;
    gap: 3px 4px;
    padding: 4px 5px;
    overflow: visible !important;
    border-radius: 11px;
  }

  header .toolbar > .spacer { display: none !important; }

  header .toolbar .tools {
    order: 10;
    flex: 1 1 100%;
    width: 100%;
    min-width: 0;
    display: flex;
    justify-content: center;
    gap: 4px;
    margin: 0;
    padding: 0 0 1px;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  header .toolbar .tools::-webkit-scrollbar { display: none; }

  .media-tools {
    order: 20;
    display: inline-flex;
    gap: 3px;
  }
  .history-controls {
    order: 21;
    display: inline-flex;
    gap: 3px;
    align-items: center;
  }
  .history-controls > span[style] { display: none !important; }
  .history-controls #playbackInlineControls { margin-left: 0 !important; }

  .zoom-controls {
    order: 22;
    display: inline-flex;
    gap: 2px;
  }
  .commit-controls {
    order: 23;
    display: inline-flex;
    align-items: center;
    gap: 4px;
  }
  .auth-controls {
    order: 24;
    display: inline-flex;
    gap: 3px;
    margin: 0;
  }

  #authControls[data-auth-state="guest"] #toolbarHome,
  #authControls[data-auth-state="guest"] #toolbarMosaics,
  #authControls[data-auth-state="guest"] #toolbarLogin,
  #authControls[data-auth-state="guest"] #toolbarSignup {
    display: none !important;
  }
  #authControls[data-auth-state="guest"] #authOpen {
    display: inline-flex !important;
  }

  header .toolbar button,
  header .toolbar .tool-btn,
  header .toolbar .toolbar-auth-btn,
  .zoom-controls > button,
  .zoom-controls > button#zoomOut,
  .zoom-controls > button#zoomIn {
    min-width: 32px;
    min-height: 32px;
    height: 32px;
    padding: 3px 6px;
    border-radius: 8px;
    flex: 0 0 auto;
  }
  header .toolbar .tools .tool-btn {
    width: 34px;
    min-width: 34px;
    height: 34px;
    min-height: 34px;
    padding: 4px;
  }
  header .toolbar .tool-btn .label { display: none !important; }

  #zoomBox { min-width: 46px !important; }
  #zoomReset {
    width: 32px;
    min-width: 32px !important;
    font-size: 0;
  }
  #zoomReset::before {
    content: "↺";
    font-size: 16px;
    line-height: 1;
  }

  #commit,
  #revert {
    width: auto;
    min-width: 66px;
    padding-left: 8px;
    padding-right: 8px;
  }
  #fundsInfo { font-size: 10px; white-space: nowrap; }

  #authOpen,
  #editorHelpOpen {
    min-width: 32px !important;
    width: 32px !important;
    min-height: 32px !important;
    height: 32px !important;
  }
}

/* ──────────────────────────────────────────────────────────────────────────
   Mobile workspace rails (FE 4.43.7)

   Portrait phones use a Photopea-style editor workspace: drawing tools on a
   left rail, secondary actions on a right rail, Commit/Revert at the bottom,
   and only title/help/menu in the top bar. Landscape returns to a compact
   horizontal toolbar. Desktop/tablet layout is unchanged.
   ────────────────────────────────────────────────────────────────────────── */
.secondary-tools {
  display: contents;
}

@media (max-width: 760px) and (orientation: portrait) {
  :root {
    --mobile-rail-w: 48px;
    --mobile-rail-gap: 5px;
    --mobile-bottom-actions-h: 48px;
  }

  header {
    backdrop-filter: none !important;
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    gap: 6px !important;
    min-height: 44px;
    padding: 5px 7px !important;
    margin: 0 !important;
    border-bottom-width: 2px;
  }

  header h1 {
    display: block !important;
    flex: 1 1 auto !important;
    width: auto !important;
    min-width: 0 !important;
    max-width: none !important;
    margin: 0 !important;
    padding: 0 4px !important;
    font-size: 17px !important;
    line-height: 1.15 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  /* Flatten the desktop toolbar. Its groups become independent workspace
     chrome: left rail, right rail, bottom actions, and top-right auth/menu. */
  header .toolbar {
    display: contents !important;
    width: auto !important;
    min-width: 0 !important;
    padding: 0 !important;
    margin: 0 !important;
    background: none !important;
    box-shadow: none !important;
    border-radius: 0 !important;
    overflow: visible !important;
  }
  header .toolbar > .spacer { display: none !important; }

  /* Main drawing tools: long left edge, just like a mobile image editor. */
  header .toolbar .tools {
    position: fixed !important;
    z-index: 45;
    left: var(--mobile-rail-gap) !important;
    top: calc(var(--header-h, 46px) + var(--mobile-rail-gap)) !important;
    bottom: calc(var(--footer-h, 18px) + var(--mobile-bottom-actions-h) + 2 * var(--mobile-rail-gap)) !important;
    width: var(--mobile-rail-w) !important;
    height: auto !important;
    margin: 0 !important;
    padding: 4px 3px !important;
    display: flex !important;
    flex-direction: column !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    justify-content: flex-start !important;
    gap: 4px !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    scrollbar-width: none !important;
    -webkit-overflow-scrolling: touch;
    border: 1px solid rgba(148,163,184,0.28);
    border-radius: 11px;
    background: rgba(15,23,42,0.96);
    box-shadow: 0 6px 18px rgba(2,6,23,0.38);
  }
  header .toolbar .tools::-webkit-scrollbar { display: none; }

  /* Secondary controls become a matching right rail. */
  .secondary-tools {
    position: fixed !important;
    z-index: 45;
    right: var(--mobile-rail-gap) !important;
    top: calc(var(--header-h, 46px) + var(--mobile-rail-gap)) !important;
    bottom: calc(var(--footer-h, 18px) + var(--mobile-bottom-actions-h) + 2 * var(--mobile-rail-gap)) !important;
    width: var(--mobile-rail-w) !important;
    padding: 4px 3px !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: flex-start !important;
    gap: 4px !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    scrollbar-width: none !important;
    -webkit-overflow-scrolling: touch;
    border: 1px solid rgba(148,163,184,0.28);
    border-radius: 11px;
    background: rgba(15,23,42,0.96);
    box-shadow: 0 6px 18px rgba(2,6,23,0.38);
  }
  .secondary-tools::-webkit-scrollbar { display: none; }
  .secondary-tools > .spacer { display: none !important; }

  .media-tools,
  .history-controls,
  .zoom-controls {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    width: 100% !important;
    gap: 4px !important;
    margin: 0 !important;
  }
  .history-controls > span[style] { display: none !important; }
  .history-controls #playbackInlineControls {
    margin: 0 !important;
    max-width: 42px;
    overflow: hidden;
  }
  .zoom-controls {
    border-top: 1px solid rgba(148,163,184,0.28);
    padding-top: 5px;
    margin-top: 1px !important;
  }
  .history-controls {
    border-top: 1px solid rgba(148,163,184,0.28);
    padding-top: 5px;
    margin-top: 1px !important;
  }

  /* Uniform 40px finger targets in both rails. */
  header .toolbar .tools .tool-btn,
  .secondary-tools button,
  .secondary-tools .tool-btn,
  .zoom-controls > button,
  .zoom-controls > button#zoomOut,
  .zoom-controls > button#zoomIn {
    flex: 0 0 40px !important;
    width: 40px !important;
    min-width: 40px !important;
    height: 40px !important;
    min-height: 40px !important;
    padding: 0 !important;
    border-radius: 8px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
  }
  header .toolbar .tool-btn .label,
  .secondary-tools .tool-btn .label { display: none !important; }
  header .toolbar .tool-btn .icon,
  .secondary-tools .tool-btn .icon {
    width: 19px !important;
    height: 19px !important;
    align-items: center;
    justify-content: center;
  }
  header .toolbar .tool-btn .icon svg,
  .secondary-tools .tool-btn .icon svg {
    width: 19px !important;
    height: 19px !important;
  }

  #zoomBox {
    font-size: 0 !important;
  }
  #zoomBox::before {
    content: "⌕";
    font-size: 23px;
    line-height: 1;
  }
  #zoomReset {
    font-size: 0 !important;
  }
  #zoomReset::before {
    content: "↺";
    font-size: 20px;
    line-height: 1;
  }
  .zoom-info { display: none !important; }

  /* Commit/Revert are prominent but don't steal canvas height at the top. */
  .commit-controls {
    position: fixed !important;
    z-index: 46;
    left: calc(var(--mobile-rail-w) + 2 * var(--mobile-rail-gap)) !important;
    right: calc(var(--mobile-rail-w) + 2 * var(--mobile-rail-gap)) !important;
    bottom: calc(var(--footer-h, 18px) + var(--mobile-rail-gap)) !important;
    height: var(--mobile-bottom-actions-h) !important;
    min-width: 0 !important;
    padding: 4px !important;
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    align-items: stretch !important;
    gap: 5px !important;
    border: 1px solid rgba(148,163,184,0.28);
    border-radius: 11px;
    background: rgba(15,23,42,0.96);
    box-shadow: 0 6px 18px rgba(2,6,23,0.38);
  }
  #commit,
  #revert {
    width: 100% !important;
    min-width: 0 !important;
    height: 38px !important;
    min-height: 38px !important;
    padding: 0 6px !important;
    border-radius: 8px !important;
    font-size: 12px !important;
  }
  #fundsInfo {
    position: absolute;
    right: 2px;
    top: -18px;
    margin: 0 !important;
    padding: 1px 5px;
    border-radius: 5px;
    background: rgba(2,6,23,0.85);
    font-size: 10px !important;
    line-height: 15px;
    white-space: nowrap;
  }

  /* Help/menu remain in the normal top bar. Guest navigation is inside Menu. */
  .auth-controls {
    position: static !important;
    flex: 0 0 auto !important;
    display: inline-flex !important;
    align-items: center !important;
    gap: 4px !important;
    margin: 0 !important;
  }
  #authControls[data-auth-state="guest"] #toolbarHome,
  #authControls[data-auth-state="guest"] #toolbarMosaics,
  #authControls[data-auth-state="guest"] #toolbarLogin,
  #authControls[data-auth-state="guest"] #toolbarSignup {
    display: none !important;
  }
  #authControls[data-auth-state="guest"] #authOpen {
    display: inline-flex !important;
  }
  #authOpen,
  #editorHelpOpen {
    flex: 0 0 36px !important;
    width: 36px !important;
    min-width: 36px !important;
    height: 36px !important;
    min-height: 36px !important;
    padding: 0 !important;
    border-radius: 9px !important;
  }

  /* Reserve real workspace lanes so the rails don't cover the mosaic. The JS
     fit code subtracts these paddings when choosing a canvas scale. */
  main,
  body.editor-side-panel-open main {
    padding: 4px calc(var(--mobile-rail-w) + 2 * var(--mobile-rail-gap))
             calc(var(--mobile-bottom-actions-h) + 2 * var(--mobile-rail-gap)) !important;
    transition: none !important;
  }

  .mosaic-wrap {
    border-radius: 8px;
  }

  /* Tool option sheets should float inside the usable center workspace rather
     than underneath either rail. */
  #toolPanel .modal,
  #textPanel .modal,
  #imagePanel .modal {
    left: calc(var(--mobile-rail-w) + 2 * var(--mobile-rail-gap)) !important;
    right: calc(var(--mobile-rail-w) + 2 * var(--mobile-rail-gap)) !important;
    width: auto !important;
    top: calc(var(--header-h, 46px) + 6px) !important;
    max-height: calc(100dvh - var(--header-h, 46px) - var(--footer-h, 18px) - var(--mobile-bottom-actions-h) - 20px) !important;
  }
}

/* Landscape: compact horizontal chrome. At normal phone-landscape widths all
   controls fit in one shallow row. Very narrow landscape screens may use two
   shallow rows rather than hiding controls off-screen. */
@media (orientation: landscape) and (max-height: 600px) {
  header {
    display: block !important;
    min-height: 0 !important;
    padding: 3px 5px !important;
    margin: 0 0 2px !important;
  }
  header h1 { display: none !important; }

  header .toolbar {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    justify-content: center !important;
    width: 100% !important;
    min-width: 0 !important;
    gap: 3px !important;
    padding: 4px 5px !important;
    overflow: hidden !important;
    border-radius: 10px !important;
  }
  header .toolbar > .spacer { display: none !important; }
  .secondary-tools { display: contents !important; }

  header .toolbar .tools,
  .media-tools,
  .history-controls,
  .zoom-controls,
  .commit-controls,
  .auth-controls {
    position: static !important;
    width: auto !important;
    height: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    display: inline-flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    gap: 3px !important;
    overflow: visible !important;
    border: 0 !important;
    background: none !important;
    box-shadow: none !important;
  }
  .history-controls > span[style] { display: none !important; }
  .history-controls #playbackInlineControls { margin-left: 0 !important; }
  .zoom-info { display: none !important; }

  #authControls[data-auth-state="guest"] #toolbarHome,
  #authControls[data-auth-state="guest"] #toolbarMosaics,
  #authControls[data-auth-state="guest"] #toolbarLogin,
  #authControls[data-auth-state="guest"] #toolbarSignup {
    display: none !important;
  }
  #authControls[data-auth-state="guest"] #authOpen { display: inline-flex !important; }

  header .toolbar button,
  header .toolbar .tool-btn,
  header .toolbar .toolbar-auth-btn,
  .zoom-controls > button,
  .zoom-controls > button#zoomOut,
  .zoom-controls > button#zoomIn {
    flex: 0 0 30px !important;
    width: 30px !important;
    min-width: 30px !important;
    height: 30px !important;
    min-height: 30px !important;
    padding: 0 !important;
    border-radius: 7px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
  }
  header .toolbar .tool-btn .label { display: none !important; }
  #zoomBox {
    width: 38px !important;
    min-width: 38px !important;
    font-size: 0 !important;
  }
  #zoomBox::before { content: "⌕"; font-size: 19px; }
  #zoomReset {
    width: 30px !important;
    min-width: 30px !important;
    font-size: 0 !important;
  }
  #zoomReset::before { content: "↺"; font-size: 17px; }

  #commit,
  #revert {
    width: auto !important;
    min-width: 58px !important;
    padding: 0 7px !important;
    font-size: 10px !important;
  }
  #fundsInfo { display: none !important; }
  #authOpen,
  #editorHelpOpen {
    width: 30px !important;
    min-width: 30px !important;
    height: 30px !important;
    min-height: 30px !important;
  }

  main,
  body.editor-side-panel-open main {
    padding: 0 !important;
  }
}

@media (orientation: landscape) and (max-height: 600px) and (max-width: 760px) {
  header .toolbar {
    flex-wrap: wrap !important;
    row-gap: 3px !important;
  }
  header .toolbar .tools {
    flex: 1 1 100% !important;
    justify-content: center !important;
  }
}


/* FE 4.43.7 — mobile visibility-state safety net.
   The mobile layout uses !important for geometry, but authorization and
   per-tool feature logic still own visibility.  JS expresses that state with
   inline display:none (and body.view-only).  Never let portrait/landscape
   layout rules resurrect a control that application state hid. */
@media (max-width: 760px), (orientation: landscape) and (max-height: 600px) {
  header .toolbar .tools [style*="display:none"],
  header .toolbar .tools [style*="display: none"],
  header .toolbar .secondary-tools [style*="display:none"],
  header .toolbar .secondary-tools [style*="display: none"],
  header .toolbar .commit-controls [style*="display:none"],
  header .toolbar .commit-controls [style*="display: none"],
  header .toolbar [hidden] {
    display: none !important;
  }

  /* In true view-only mode the editor chrome must not appear merely because
     a mobile media query lays it out.  Zoom (and optional playback) remain. */
  body.view-only header .toolbar .tools,
  body.view-only header .toolbar .media-tools,
  body.view-only header .toolbar .commit-controls {
    display: none !important;
  }
  body.view-only #undo,
  body.view-only #redo,
  body.view-only #toolPlayback {
    display: none !important;
  }
}

@media (max-width: 760px) and (orientation: portrait) {
  /* With the left editor rail and bottom Commit/Revert absent, let a view-only
     visitor use that space.  Keep the right rail for zoom/playback. */
  body.view-only main,
  body.view-only.editor-side-panel-open main {
    padding-left: 4px !important;
    padding-bottom: 4px !important;
  }
}


/* Portrait phones: tool/text options use the center-bottom workspace rather
   than the desktop vbar lane on the right.  This is intentionally scoped to
   vbar mode + portrait only; desktop and landscape retain their existing
   panel geometry. */
@media (max-width: 760px) and (orientation: portrait) {
  :root {
    --mobile-tool-sheet-h: 140px;
  }

  body.tool-panel-vbar #toolBarDiv,
  body.tool-panel-vbar #textBarDiv {
    top: auto !important;
    left: calc(var(--mobile-rail-w) + 2 * var(--mobile-rail-gap)) !important;
    right: calc(var(--mobile-rail-w) + 2 * var(--mobile-rail-gap)) !important;
    bottom: calc(var(--footer-h, 18px) + var(--mobile-bottom-actions-h) + 2 * var(--mobile-rail-gap)) !important;
    width: auto !important;
    height: var(--mobile-tool-sheet-h) !important;
    min-height: 0 !important;
    max-height: var(--mobile-tool-sheet-h) !important;
    box-sizing: border-box !important;
    padding: 7px 9px !important;
    gap: 6px !important;
    flex-direction: column !important;
    flex-wrap: nowrap !important;
    align-items: stretch !important;
    justify-content: flex-start !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    white-space: normal !important;
    border: 1px solid rgba(148,163,184,0.35) !important;
    border-radius: 11px !important;
    box-shadow: 0 -5px 18px rgba(2,6,23,0.38) !important;
  }

  body.tool-panel-vbar #toolBarDiv > h2,
  body.tool-panel-vbar #textBarDiv > h2,
  body.tool-panel-vbar #toolBarDiv .modal-close,
  body.tool-panel-vbar #textBarDiv .modal-close {
    display: none !important;
  }

  body.tool-panel-vbar #toolBarDiv .row,
  body.tool-panel-vbar #textBarDiv .row {
    order: 1 !important;
    display: flex !important;
    flex: 1 1 auto !important;
    flex-direction: row !important;
    flex-wrap: wrap !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 6px 10px !important;
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  body.tool-panel-vbar #toolBarDiv .actions,
  body.tool-panel-vbar #textBarDiv .actions {
    order: 2 !important;
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 7px !important;
    width: 100% !important;
    margin: 0 !important;
    padding: 5px 0 0 !important;
    border-top: 1px solid #334155 !important;
    border-bottom: 0 !important;
  }

  body.tool-panel-vbar #toolBarDiv label,
  body.tool-panel-vbar #textBarDiv label {
    display: inline-flex !important;
    flex-direction: row !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 5px !important;
    width: auto !important;
    margin: 0 !important;
    text-align: left !important;
    white-space: nowrap !important;
  }

  body.tool-panel-vbar #toolBarDiv input[type="range"],
  body.tool-panel-vbar #textBarDiv input[type="range"] {
    writing-mode: horizontal-tb !important;
    direction: ltr !important;
    width: 96px !important;
    height: 20px !important;
    margin: 0 !important;
  }

  body.tool-panel-vbar #toolBarDiv input[type="number"],
  body.tool-panel-vbar #textBarDiv input[type="number"] {
    width: 58px !important;
  }

  body.tool-panel-vbar #textBarDiv select {
    width: auto !important;
    max-width: 150px !important;
  }

  body.tool-panel-vbar #toolBarDiv .actions button,
  body.tool-panel-vbar #textBarDiv .actions button {
    width: 100% !important;
    min-width: 0 !important;
    height: 32px !important;
    padding: 0 8px !important;
  }

  /* Reserve the bottom-sheet lane only while a tool/text sheet is visible,
     so layoutMosaic fits the image above it instead of moving it sideways. */
  body.tool-panel-vbar:has(#toolBarDiv:not([hidden])) main,
  body.tool-panel-vbar:has(#textBarDiv:not([hidden])) main,
  body.tool-panel-vbar:has(#toolBarDiv:not([hidden])).editor-side-panel-open main,
  body.tool-panel-vbar:has(#textBarDiv:not([hidden])).editor-side-panel-open main {
    padding-bottom: calc(var(--mobile-bottom-actions-h) + var(--mobile-tool-sheet-h) + 3 * var(--mobile-rail-gap)) !important;
  }
}
