:root {
  --bg-subtle: #F8FAFC;
  --border-color: #E2E8F0;
  --text-main: #1E293B;
  --text-muted: #64748B;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  font-family: 'Nunito', system-ui, -apple-system, sans-serif;
  background-color: var(--bg-subtle);
  color: var(--text-main);
  user-select: none;
  -webkit-user-select: none;
}

/* Fullscreen 3-Area Layout */
.studio-root {
  display: flex;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

/* 1. LEFT SIDEBAR: 2-Column Square Grid for Tools & Actions */
.left-tools-sidebar {
  width: 172px;
  height: 100vh;
  background: #FFFFFF;
  border-right: 2px solid var(--border-color);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 10px 8px;
  gap: 6px;
  z-index: 30;
  box-shadow: 2px 0 12px rgba(0, 0, 0, 0.03);
}

.sidebar-section-label {
  font-size: 10.5px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: 2px;
}

/* 2-Column Grid of Chunky Square Cards */
.tools-square-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
}

.square-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border-radius: 14px;
  background: #FFFFFF;
  border: 2px solid #E2E8F0;
  box-shadow: 0 3px 0 #CBD5E1;
  cursor: pointer;
  color: #475569;
  font-weight: 800;
  font-size: 11px;
  letter-spacing: 0.2px;
  transition: all 0.12s ease;
  position: relative;
  padding: 4px;
}

.square-card:hover {
  background: #F8FAFC;
  transform: translateY(-2px);
  box-shadow: 0 5px 0 #CBD5E1;
  color: #0F172A;
}

.square-card:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #CBD5E1;
}

.badge-icon {
  width: 30px;
  height: 30px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease;
}

.badge-icon svg {
  width: 18px;
  height: 18px;
  stroke-width: 2.3px;
}

.square-card:hover .badge-icon {
  transform: scale(1.12);
}

/* Specific Badge Colors */
.square-card[data-tool="freedraw"] .badge-icon {
  background: #E0F2FE;
  color: #0284C7;
}

.square-card[data-tool="eraser"] .badge-icon {
  background: #FFEDD5;
  color: #EA580C;
}

.square-card[data-tool="bucket"] .badge-icon {
  background: #DBEAFE;
  color: #2563EB;
}

.square-card[data-tool="puppy"] .badge-icon {
  background: #F3E8FF;
  color: #9333EA;
}

.square-card[data-tool="confetti"] .badge-icon {
  background: #FCE7F3;
  color: #DB2777;
}

.square-card[data-tool="rainbow"] .badge-icon {
  background: #DCFCE7;
  color: #16A34A;
}

.square-card[data-tool="stars"] .badge-icon {
  background: #FEF3C7;
  color: #D97706;
}

.square-card[data-tool="bubbles"] .badge-icon {
  background: #CFFAFE;
  color: #0891B2;
}

.square-card[data-tool="laser"] .badge-icon {
  background: #FFE4E6;
  color: #E11D48;
}

/* Active Tool State */
.square-card.active {
  transform: translateY(2px);
  border-color: #222222;
  box-shadow: 0 1px 0 #222222;
  background: #FFFFFF;
  color: #111827;
}

.square-card.active[data-tool="freedraw"] {
  background: #E0F2FE;
  border-color: #0284C7;
  box-shadow: 0 1px 0 #0369A1;
}

.square-card.active[data-tool="eraser"] {
  background: #FFEDD5;
  border-color: #EA580C;
  box-shadow: 0 1px 0 #C2410C;
}

.square-card.active[data-tool="bucket"] {
  background: #DBEAFE;
  border-color: #2563EB;
  box-shadow: 0 1px 0 #1D4ED8;
}

.square-card.active[data-tool="puppy"] {
  background: #F3E8FF;
  border-color: #9333EA;
  box-shadow: 0 1px 0 #7E22CE;
}

.square-card.active[data-tool="confetti"] {
  background: #FCE7F3;
  border-color: #DB2777;
  box-shadow: 0 1px 0 #BE185D;
}

.square-card.active[data-tool="rainbow"] {
  background: #DCFCE7;
  border-color: #16A34A;
  box-shadow: 0 1px 0 #15803D;
}

.square-card.active[data-tool="stars"] {
  background: #FEF3C7;
  border-color: #D97706;
  box-shadow: 0 1px 0 #B45309;
}

.square-card.active[data-tool="bubbles"] {
  background: #CFFAFE;
  border-color: #0891B2;
  box-shadow: 0 1px 0 #0E7490;
}

.square-card.active[data-tool="laser"] {
  background: #FFE4E6;
  border-color: #E11D48;
  box-shadow: 0 1px 0 #BE123C;
}

/* Action Buttons in 2-Column Grid (Save, Color Page, Undo, Redo, Clear) */
.action-square-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  height: 52px;
  border-radius: 12px;
  border: 2px solid #E2E8F0;
  background: #FFFFFF;
  color: #334155;
  font-weight: 800;
  font-size: 11px;
  cursor: pointer;
  box-shadow: 0 2.5px 0 #CBD5E1;
  transition: all 0.12s ease;
  padding: 4px;
}

.action-square-btn svg {
  width: 17px;
  height: 17px;
  stroke-width: 2.3px;
}

.action-square-btn:hover {
  background: #F8FAFC;
  transform: translateY(-1px);
  box-shadow: 0 4px 0 #CBD5E1;
}

.action-square-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #CBD5E1;
}

.action-square-btn.save-act {
  color: #059669;
  border-color: #A7F3D0;
  background: #ECFDF5;
  box-shadow: 0 2.5px 0 #6EE7B7;
}

.action-square-btn.colorbook-act {
  color: #7C3AED;
  border-color: #DDD6FE;
  background: #FAF5FF;
  box-shadow: 0 2.5px 0 #C4B5FD;
}

.action-square-btn.danger-act {
  color: #EF4444;
  border-color: #FECACA;
  background: #FFF5F5;
  box-shadow: 0 2.5px 0 #FCA5A5;
  grid-column: span 2;
}

.divider-sep {
  width: 100%;
  height: 2px;
  background: var(--border-color);
  border-radius: 9999px;
  margin: 2px 0;
}

/* 2. CENTER: Canvas Viewport */
.center-canvas-area {
  flex: 1 1 0;
  min-width: 0;
  height: 100vh;
  position: relative;
  background: #FFFFFF;
  overflow: hidden;
}

whiteboard-element {
  width: 100%;
  height: 100%;
  display: block;
  background-color: #FFFFFF;
}

/* 3. RIGHT SIDEBAR: Colors & Tip Sizes */
.right-colors-sidebar {
  width: 136px;
  height: 100vh;
  background: #FFFFFF;
  border-left: 2px solid var(--border-color);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 10px 8px;
  gap: 8px;
  z-index: 30;
  box-shadow: -2px 0 12px rgba(0, 0, 0, 0.03);
}

.colors-scroll-container {
  overflow-y: auto;
  max-height: calc(100vh - 130px);
  padding: 1px;
}

.colors-scroll-container::-webkit-scrollbar {
  width: 4px;
}

.colors-scroll-container::-webkit-scrollbar-thumb {
  background: #CBD5E1;
  border-radius: 4px;
}

/* 3 Columns of Compact Square Color Swatches (~34px) - Max visibility on screen */
.right-colors-grid {
  display: grid;
  grid-template-columns: repeat(3, 35px);
  gap: 5px;
  justify-content: center;
  align-content: start;
}

.swatch-circle {
  width: 35px;
  height: 35px;
  border-radius: 8px;
  cursor: pointer;
  border: 2px solid rgba(0, 0, 0, 0.08);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.12);
  transition: all 0.12s cubic-bezier(0.2, 0.8, 0.2, 1);
  position: relative;
}

.swatch-circle.rainbow-picker {
  background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
  display: flex;
  align-items: center;
  justify-content: center;
}

.swatch-circle.rainbow-picker input[type="color"] {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

.swatch-circle:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.18);
  z-index: 5;
}

.swatch-circle.active {
  transform: scale(1.08);
  border: 2px solid #FFFFFF;
  box-shadow: 0 0 0 2.5px #2563EB, 0 3px 8px rgba(37, 99, 235, 0.25);
  z-index: 10;
}

/* Tip Size Group in Right Sidebar */
.tip-size-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
}

.tip-pill {
  flex: 1;
  height: 38px;
  border-radius: 10px;
  background: #F8FAFC;
  border: 2px solid #E2E8F0;
  box-shadow: 0 2px 0 #CBD5E1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.12s ease;
}

.tip-pill .dot {
  background: #475569;
  border-radius: 50%;
}

.tip-pill.active {
  background: #EFF6FF;
  border-color: #3B82F6;
  box-shadow: 0 1px 0 #2563EB;
  transform: translateY(1px);
}

.tip-pill.active .dot {
  background: #2563EB;
}

#bgFileInput {
  display: none;
}

/* Visual Coloring Book Modal */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(8px);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-backdrop.open {
  display: flex;
}

.coloring-modal-content {
  background: #FFFFFF;
  border-radius: 28px;
  padding: 24px;
  max-width: 640px;
  width: 100%;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
  border: 2px solid #E2E8F0;
  animation: modalPop 0.2s cubic-bezier(0.2, 0.8, 0.2, 1);
}

@keyframes modalPop {
  0% {
    transform: scale(0.92);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.modal-header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.modal-title-text {
  font-size: 19px;
  font-weight: 900;
  color: #0F172A;
}

.modal-close-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid #E2E8F0;
  background: #FFFFFF;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #64748B;
  transition: all 0.12s ease;
}

.modal-close-btn:hover {
  background: #F1F5F9;
  color: #0F172A;
}

/* Kid-Friendly SVG Coloring Thumbnails Grid */
.coloring-pages-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 12px;
  overflow-y: auto;
  padding: 4px;
}

.coloring-thumb-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 10px;
  border-radius: 18px;
  border: 2px solid #E2E8F0;
  background: #F8FAFC;
  cursor: pointer;
  box-shadow: 0 3px 0 #CBD5E1;
  transition: all 0.15s ease;
}

.coloring-thumb-card:hover {
  background: #FFFFFF;
  border-color: #3B82F6;
  transform: translateY(-3px);
  box-shadow: 0 6px 0 #93C5FD;
}

.coloring-thumb-card img {
  width: 80px;
  height: 80px;
  object-fit: contain;
}

.coloring-thumb-card span {
  font-size: 12px;
  font-weight: 800;
  color: #334155;
}

/* ==========================================================================
   MOBILE & TABLET RESPONSIVE LAYOUTS - TIGHT & ICON-ONLY
   ========================================================================== */

/* Tablets / Medium Screens (<= 900px) */
@media (max-width: 900px) {
  .left-tools-sidebar {
    width: 64px;
    padding: 6px 4px;
  }

  .tools-square-grid {
    grid-template-columns: 1fr;
    gap: 5px;
  }

  .square-card,
  .action-square-btn {
    width: 48px;
    height: 48px;
    padding: 0;
    border-radius: 10px;
    justify-content: center;
    align-items: center;
  }

  .square-card[style*="span 2"] {
    grid-column: span 1 !important;
  }

  .badge-icon {
    width: 32px;
    height: 32px;
    border-radius: 8px;
  }

  .badge-icon svg,
  .action-square-btn svg {
    width: 18px;
    height: 18px;
  }

  .square-card span,
  .action-square-btn span,
  .sidebar-section-label {
    display: none !important;
  }

  .right-colors-sidebar {
    width: 82px;
    padding: 6px 4px;
  }

  .right-colors-grid {
    grid-template-columns: repeat(2, 34px);
    gap: 4px;
  }

  .swatch-circle {
    width: 34px;
    height: 34px;
  }
}

/* Mobile Screens (<= 640px): Ultra-tight top & bottom app docks */
@media (max-width: 640px) {
  .studio-root {
    flex-direction: column;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
  }

  /* 1. TOP TOOLBAR DOCK: Tight single-row scrollable tools */
  .left-tools-sidebar {
    width: 100vw;
    height: 48px;
    min-height: 48px;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
    flex-direction: row;
    align-items: center;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 4px 8px;
    gap: 6px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
    background: #FFFFFF;
    -webkit-overflow-scrolling: touch;
  }

  .left-tools-sidebar::-webkit-scrollbar {
    display: none;
  }

  .sidebar-section-label,
  .divider-sep,
  .square-card span,
  .action-square-btn span {
    display: none !important;
  }

  .left-tools-sidebar > div {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 5px;
    flex-shrink: 0;
  }

  .tools-square-grid {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 5px;
    flex-shrink: 0;
  }

  .square-card,
  .action-square-btn {
    width: 38px;
    height: 38px;
    min-width: 38px;
    flex-shrink: 0;
    padding: 0;
    border-radius: 8px;
    justify-content: center;
    align-items: center;
    box-shadow: 0 1px 0 #CBD5E1;
  }

  .square-card[style*="span 2"] {
    grid-column: auto !important;
  }

  .badge-icon {
    width: 28px;
    height: 28px;
    border-radius: 6px;
  }

  .badge-icon svg,
  .action-square-btn svg {
    width: 16px;
    height: 16px;
  }

  /* 2. CENTER CANVAS AREA */
  .center-canvas-area {
    flex: 1;
    width: 100vw;
    height: calc(100vh - 96px); /* 48px top dock + 48px bottom dock */
    position: relative;
    overflow: hidden;
  }

  /* 3. BOTTOM COLORS & TIP SIZES DOCK */
  .right-colors-sidebar {
    width: 100vw;
    height: 48px;
    min-height: 48px;
    border-left: none;
    border-top: 1px solid var(--border-color);
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 4px 8px;
    gap: 8px;
    box-shadow: 0 -1px 4px rgba(0, 0, 0, 0.05);
    background: #FFFFFF;
  }

  .colors-scroll-container {
    max-height: none;
    overflow-x: auto;
    overflow-y: hidden;
    flex: 1;
    display: flex;
    align-items: center;
    -webkit-overflow-scrolling: touch;
  }

  .colors-scroll-container::-webkit-scrollbar {
    display: none;
  }

  .right-colors-grid {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 5px;
    flex-shrink: 0;
  }

  .swatch-circle {
    width: 32px;
    height: 32px;
    min-width: 32px;
    flex-shrink: 0;
    border-radius: 6px;
    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.12);
  }

  .swatch-circle.active {
    transform: scale(1.1);
  }

  /* Tip size buttons right-aligned in bottom bar */
  .tip-size-row {
    display: flex;
    flex-shrink: 0;
    gap: 4px;
    align-items: center;
    border-left: 1px solid var(--border-color);
    padding-left: 6px;
  }

  .tip-pill {
    width: 32px;
    height: 32px;
    min-width: 32px;
    padding: 0;
    border-radius: 6px;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 1px 0 #CBD5E1;
  }

  /* Keep the stroke dot circles visible and centered on mobile */
  .tip-pill .dot {
    display: block !important;
  }

  /* Coloring Modal on mobile */
  .modal-dialog {
    width: 94vw;
    max-height: 82vh;
    padding: 14px;
    border-radius: 14px;
  }

  .coloring-thumb-card {
    padding: 6px;
    border-radius: 12px;
  }

  .coloring-thumb-card img {
    width: 60px;
    height: 60px;
  }
}
