/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: 'Comic Neue', 'Comic Sans MS', cursive, sans-serif;
  background: #fdf6e3;
  color: #333;
}
button { cursor: pointer; font-family: inherit; }
h1, h2, h3 { font-family: 'Fredoka One', 'Comic Neue', cursive; }

/* ===== SCREENS ===== */
.screen { display: none; height: 100vh; flex-direction: column; }
.screen.active { display: flex; }

/* ===== HOME SCREEN ===== */
.home-header {
  text-align: center;
  padding: 48px 16px 24px;
  background: linear-gradient(135deg, #ffe082, #ffcc80, #ffab91);
}
.capy-mascot { line-height: 1; animation: bounce 2s ease-in-out infinite; display: inline-block; }
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-12px); }
}
.logo-title {
  font-size: 56px;
  color: #5d4037;
  text-shadow: 3px 3px 0 #fff, 4px 4px 0 rgba(0,0,0,0.08);
  letter-spacing: 2px;
}
.logo-subtitle { font-size: 20px; color: #6d4c41; margin-top: 4px; }

.home-main { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px; }
.action-cards { display: flex; gap: 24px; flex-wrap: wrap; justify-content: center; }
.action-card {
  background: #fff;
  border: 3px solid #ffe0b2;
  border-radius: 20px;
  padding: 32px 24px;
  width: 220px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 4px 12px rgba(0,0,0,0.06);
}
.action-card:hover { transform: translateY(-6px) scale(1.03); box-shadow: 0 8px 24px rgba(0,0,0,0.12); border-color: #ffb74d; }
.card-icon { font-size: 48px; }
.card-label { font-family: 'Fredoka One', cursive; font-size: 20px; color: #5d4037; }
.card-desc { font-size: 13px; color: #888; text-align: center; }

.home-footer { text-align: center; padding: 16px; font-size: 13px; color: #aaa; }

/* ===== LEVEL BANNER ===== */
.level-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 12px 24px;
  background: linear-gradient(135deg, #fff8e1, #fff3e0);
  border-bottom: 2px solid #ffe0b2;
  transition: background 0.15s;
}
.level-banner:hover {
  background: linear-gradient(135deg, #fff3e0, #ffe0b2);
}
.level-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: 'Fredoka One', cursive;
  font-size: 16px;
  color: #5d4037;
}
.level-badge-icon { font-size: 28px; }
.level-badge-info { display: flex; flex-direction: column; }
.level-badge-title { font-size: 15px; color: #5d4037; }
.level-badge-subtitle { font-size: 11px; color: #a08060; font-family: 'Comic Neue', cursive; font-weight: 400; }
.level-progress {
  display: flex;
  align-items: center;
  gap: 8px;
}
.level-progress-bar {
  width: 140px;
  height: 12px;
  background: #e0d5c5;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid #d4c4a8;
}
.level-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #ffcc02, #ff9800);
  border-radius: 6px;
  transition: width 0.5s ease;
}
.level-progress-text {
  font-size: 11px;
  color: #a08060;
  white-space: nowrap;
  font-family: 'Comic Neue', cursive;
  font-weight: 700;
}
.level-next-unlock {
  font-size: 12px;
  color: #8d6e63;
  font-family: 'Comic Neue', cursive;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Level-up toast animation */
.level-up-toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.5);
  background: linear-gradient(135deg, #fff8e1, #ffe082);
  border: 4px solid #ff9800;
  border-radius: 24px;
  padding: 32px 44px;
  text-align: center;
  z-index: 300;
  box-shadow: 0 12px 48px rgba(0,0,0,0.3);
  opacity: 0;
  animation: levelUpPop 0.4s ease forwards;
}
@keyframes levelUpPop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
  60% { transform: translate(-50%, -50%) scale(1.08); }
  100% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}
.level-up-toast .lu-icon { font-size: 56px; display: block; margin-bottom: 8px; }
.level-up-toast .lu-title { font-family: 'Fredoka One', cursive; font-size: 22px; color: #5d4037; }
.level-up-toast .lu-subtitle { font-size: 14px; color: #8d6e63; margin-top: 4px; }
.level-up-toast .lu-unlocked { font-size: 13px; color: #e65100; margin-top: 10px; font-weight: 700; }
.level-up-toast .lu-dismiss {
  margin-top: 16px;
  background: #ff9800;
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 8px 28px;
  font-family: 'Fredoka One', cursive;
  font-size: 14px;
  cursor: pointer;
}

/* ===== SCREEN TOP BAR ===== */
.screen-topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: #fff;
  border-bottom: 2px solid #ffe0b2;
}
.screen-topbar h2 { font-size: 22px; color: #5d4037; }
.back-btn {
  background: #ffe0b2;
  border: none;
  border-radius: 10px;
  padding: 8px 16px;
  font-weight: 700;
  font-size: 14px;
  color: #5d4037;
  transition: background 0.15s;
}
.back-btn:hover { background: #ffcc80; }

/* ===== TEMPLATE GRID ===== */
.template-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 24px;
  padding: 28px;
  overflow-y: auto;
  background: linear-gradient(180deg, #fdf6e3 0%, #f5e6d3 100%);
}
.template-card {
  background: #fff;
  border: 3px solid #e0e0e0;
  border-radius: 20px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s ease, border-color 0.2s, box-shadow 0.2s;
  box-shadow: 0 4px 14px rgba(0,0,0,0.06);
  position: relative;
}
.template-card:hover {
  transform: translateY(-6px) scale(1.02);
  border-color: #ffb74d;
  box-shadow: 0 12px 32px rgba(0,0,0,0.14);
}
.template-preview {
  width: 100%;
  aspect-ratio: 4/3;
  position: relative;
  overflow: hidden;
}
.template-preview .preview-bg {
  position: absolute;
  inset: 0;
  opacity: 0.15;
  pointer-events: none;
}
.preview-emoji {
  position: absolute;
  line-height: 1;
  pointer-events: none;
  filter: drop-shadow(2px 2px 2px rgba(0,0,0,0.12));
  transition: transform 0.3s ease;
}
.template-card:hover .preview-emoji {
  transform: scale(1.12);
}
.preview-emoji-hero {
  z-index: 2;
  filter: drop-shadow(3px 3px 4px rgba(0,0,0,0.2));
}
.template-preview .preview-sparkle {
  position: absolute;
  font-size: 16px;
  opacity: 0;
  animation: sparkleFloat 2.5s ease-in-out infinite;
  pointer-events: none;
}
.template-preview .preview-sparkle:nth-child(2) { animation-delay: 0.5s; }
.template-preview .preview-sparkle:nth-child(3) { animation-delay: 1.0s; }
.template-preview .preview-sparkle:nth-child(4) { animation-delay: 1.5s; }
@keyframes sparkleFloat {
  0%, 100% { opacity: 0; transform: translateY(0) scale(0.8); }
  50% { opacity: 0.7; transform: translateY(-8px) scale(1.1); }
}
.template-card-body {
  padding: 10px 14px 14px;
  text-align: center;
}
.template-card-label {
  padding: 0;
  text-align: center;
  font-family: 'Fredoka One', cursive;
  font-size: 16px;
  color: #5d4037;
}
.template-card-desc {
  padding: 4px 0 0;
  text-align: center;
  font-size: 12px;
  color: #999;
}
.template-card-tags {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin-top: 6px;
  flex-wrap: wrap;
}
.template-tag {
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 8px;
  font-weight: 700;
  letter-spacing: 0.3px;
}

/* ===== REWARDS / LEVELS SCREEN ===== */
.rewards-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  background: linear-gradient(180deg, #fdf6e3 0%, #f5e6d3 100%);
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.rewards-status {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 20px;
  background: linear-gradient(135deg, #fff8e1, #ffe082);
  border-radius: 16px;
  border: 3px solid #ffb74d;
  box-shadow: 0 4px 12px rgba(0,0,0,0.06);
}
.rewards-status-icon { font-size: 42px; }
.rewards-status-info { display: flex; flex-direction: column; }
.rewards-status-title { font-family: 'Fredoka One', cursive; font-size: 20px; color: #5d4037; }
.rewards-status-sub { font-size: 13px; color: #8d6e63; }
.reward-level-card {
  background: #fff;
  border: 3px solid #e0e0e0;
  border-radius: 16px;
  overflow: hidden;
  transition: border-color 0.2s;
}
.reward-level-card.current { border-color: #ff9800; box-shadow: 0 0 0 2px #fff, 0 0 0 5px #ff980060; }
.reward-level-card.unlocked { border-color: #4caf50; }
.reward-level-card.locked { opacity: 0.7; }
.reward-level-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: #fafafa;
  border-bottom: 1px solid #eee;
}
.reward-level-card.current .reward-level-header { background: #fff8e1; }
.reward-level-card.unlocked .reward-level-header { background: #e8f5e9; }
.reward-level-icon { font-size: 28px; }
.reward-level-title { font-family: 'Fredoka One', cursive; font-size: 15px; color: #5d4037; flex: 1; }
.reward-level-req { font-size: 11px; color: #999; background: #f5f5f5; padding: 2px 8px; border-radius: 8px; }
.reward-level-check, .reward-level-lock { font-size: 18px; }
.reward-level-body {
  display: flex;
  gap: 12px;
  padding: 12px 16px;
  align-items: flex-start;
}
.reward-section { flex: 1; min-width: 0; }
.reward-section-stamps { flex: 0 0 auto; max-width: 140px; }
.reward-section-other {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  min-width: 0;
}
.reward-group { min-width: 70px; }
.reward-section-label { font-size: 11px; font-weight: 700; color: #8d6e63; margin-bottom: 4px; white-space: nowrap; }
.reward-items { display: flex; flex-wrap: wrap; gap: 6px; }
.reward-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 5px 6px;
  background: #fafafa;
  border: 2px solid #e8e8e8;
  border-radius: 10px;
  min-width: 60px;
  transition: transform 0.15s;
}
.reward-item:not(.reward-item-locked):hover { transform: scale(1.05); }
.reward-item-locked { opacity: 0.45; filter: grayscale(0.5); }
.reward-item-preview { font-size: 16px; line-height: 1.3; text-align: center; }
.reward-item-label { font-size: 9px; color: #888; text-align: center; white-space: nowrap; }
.reward-scene-preview, .reward-filter-preview { border-radius: 4px; display: block; width: 48px; height: 36px; }
.reward-color-swatch {
  display: block;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid rgba(0,0,0,0.1);
}

/* ===== MY STORIES — BOOKSHELF ===== */
.stories-list {
  flex: 1;
  padding: 24px 32px;
  overflow-y: auto;
  background: linear-gradient(180deg, #f5e6d3 0%, #e8d5bd 100%);
}
.empty-state {
  text-align: center;
  color: #a08060;
  font-size: 18px;
  margin-top: 80px;
}

.bookshelf {
  display: flex;
  flex-wrap: wrap;
  gap: 0;
  padding: 0;
}

.shelf-row {
  width: 100%;
  display: flex;
  align-items: flex-end;
  padding: 20px 16px 0;
  min-height: 220px;
  gap: 12px;
  flex-wrap: wrap;
}

/* The wooden shelf plank */
.shelf-plank {
  width: 100%;
  height: 18px;
  background: linear-gradient(180deg, #a0724e 0%, #8b5e3c 40%, #7a5030 100%);
  border-radius: 3px 3px 0 0;
  box-shadow: 0 4px 8px rgba(0,0,0,0.25), inset 0 2px 0 rgba(255,255,255,0.15);
  margin-bottom: 8px;
}

/* Each book spine */
.book-spine {
  width: 52px;
  height: 180px;
  border-radius: 4px 6px 6px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: transform 0.2s, box-shadow 0.2s;
  box-shadow: 2px 2px 6px rgba(0,0,0,0.2), inset -2px 0 4px rgba(0,0,0,0.1);
  overflow: hidden;
  flex-shrink: 0;
}
.book-spine::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 4px;
  background: rgba(0,0,0,0.12);
  border-radius: 4px 0 0 4px;
}
.book-spine::after {
  content: '';
  position: absolute;
  right: 2px; top: 8px; bottom: 8px;
  width: 1px;
  background: rgba(255,255,255,0.18);
}
.book-spine:hover {
  transform: translateY(-12px) rotate(-2deg);
  box-shadow: 4px 6px 16px rgba(0,0,0,0.3);
  z-index: 2;
}

.book-title {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  font-family: 'Fredoka One', cursive;
  font-size: 11px;
  color: #fff;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.3);
  letter-spacing: 1px;
  max-height: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 4px 0;
}

.book-icon {
  font-size: 18px;
  margin-bottom: 4px;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.2));
}

/* Decorative filler books */
.deco-book { pointer-events: none; }
.deco-book:hover { transform: none; box-shadow: 2px 2px 6px rgba(0,0,0,0.2), inset -2px 0 4px rgba(0,0,0,0.1); }

/* ===== EDITOR TOP BAR ===== */
.editor-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: #fff;
  border-bottom: 2px solid #ffe0b2;
  z-index: 10;
}
.story-title-input {
  flex: 1;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  padding: 6px 14px;
  font-family: 'Fredoka One', cursive;
  font-size: 18px;
  color: #5d4037;
  outline: none;
  transition: border-color 0.15s;
}
.story-title-input:focus { border-color: #ffb74d; }
.topbar-actions { display: flex; gap: 8px; }
.tool-btn {
  background: #ffe082;
  border: none;
  border-radius: 10px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 700;
  color: #5d4037;
  transition: background 0.15s;
}
.tool-btn:hover { background: #ffd54f; }
.btn-delete-story { background: #ffcdd2; color: #c62828; }
.btn-delete-story:hover { background: #ef9a9a; }

/* ===== EDITOR BODY ===== */
.editor-body {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* ===== TOOL PALETTE (LEFT) ===== */
.tool-palette {
  width: 64px;
  background: #fff;
  border-right: 2px solid #ffe0b2;
  padding: 8px 6px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.tool-group h3 {
  font-size: 9px;
  text-transform: uppercase;
  color: #aaa;
  margin-bottom: 4px;
  letter-spacing: 1px;
  text-align: center;
}
.palette-btn {
  width: 44px;
  height: 44px;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  background: #fafafa;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 4px;
  transition: border-color 0.15s, background 0.15s;
}
.palette-btn:hover { border-color: #ffb74d; }
.palette-btn.active { border-color: #ff9800; background: #fff3e0; }

/* Color swatches */
.color-swatches { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid transparent;
  transition: border-color 0.15s, transform 0.1s;
}
.swatch:hover { transform: scale(1.15); }
.swatch.active { border-color: #ff9800; box-shadow: 0 0 0 2px #fff, 0 0 0 4px #ff9800; }

.range-label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: #888;
}
.range-label input { margin-top: 4px; }

select#bubble-style {
  width: 100%;
  padding: 6px 8px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-family: inherit;
  font-size: 14px;
  margin-bottom: 8px;
  background: #fafafa;
}

.fill-hint {
  font-size: 11px;
  color: #aaa;
  font-style: italic;
  margin-top: 4px;
}

/* Scene background swatches */
.scene-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 8px;
}
.scene-swatch {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  padding: 3px;
  background: #fafafa;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
}
.scene-swatch:hover { border-color: #ffb74d; transform: scale(1.05); }
.scene-swatch.active { border-color: #ff9800; background: #fff3e0; box-shadow: 0 0 0 2px #fff, 0 0 0 4px #ff9800; }
.scene-swatch.locked { cursor: not-allowed; opacity: 0.55; border-color: #ccc; background: #f0f0f0; }
.scene-swatch.locked:hover { border-color: #ccc; transform: none; }
.scene-swatch canvas {
  border-radius: 4px;
  display: block;
  width: 48px;
  height: 36px;
}
.scene-swatch span {
  font-size: 9px;
  color: #888;
  white-space: nowrap;
}

/* Locked filter items */
.filter-item.locked { cursor: not-allowed; opacity: 0.55; border-color: #ccc; background: #f0f0f0; }
.filter-item.locked:hover { border-color: #ccc; transform: none; }

/* ===== CONTENT PANEL (SECOND COLUMN) ===== */
.content-panel {
  width: 200px;
  background: #fff;
  border-right: 2px solid #ffe0b2;
  padding: 12px 10px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.content-section h3 {
  font-size: 12px;
  text-transform: uppercase;
  color: #aaa;
  margin-bottom: 4px;
  letter-spacing: 1px;
}

/* Bubble picker (drag-and-drop items) */
.bubble-picker {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}
.bubble-pick-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  background: #fafafa;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  font-size: 14px;
  font-family: 'Comic Neue', cursive;
  font-weight: 700;
  color: #5d4037;
  transition: border-color 0.15s, transform 0.1s;
}
.bubble-pick-item:hover { border-color: #ffb74d; transform: scale(1.03); }
.bubble-pick-item:active { cursor: grabbing; }
.bubble-pick-icon { font-size: 22px; flex-shrink: 0; }

/* Image upload UI */
.image-upload-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 12px;
  border: 2px dashed #ffb74d;
  border-radius: 10px;
  background: #fff8e1;
  cursor: pointer;
  font-family: 'Fredoka One', cursive;
  font-size: 13px;
  color: #5d4037;
  transition: background 0.15s;
  margin-bottom: 10px;
}
.image-upload-btn:hover { background: #fff3e0; }
.image-upload-btn.disabled {
  opacity: 0.5;
  cursor: default;
  border-color: #ccc;
}

.image-upload-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 14px 0;
  font-family: 'Fredoka One', cursive;
  font-size: 13px;
  color: #8d6e63;
  animation: pulse-loading 1s ease-in-out infinite;
}
@keyframes pulse-loading {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.image-upload-preview { margin-top: 4px; }
.image-preview-thumb {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 8px;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  background: #fafafa;
  cursor: grab;
  user-select: none;
  transition: border-color 0.15s;
}
.image-preview-thumb:hover { border-color: #ffb74d; }
.image-preview-thumb:active { cursor: grabbing; }
.image-preview-thumb img {
  max-width: 100%;
  max-height: 120px;
  border-radius: 6px;
  object-fit: contain;
  pointer-events: none;
}
.image-preview-thumb span {
  font-size: 10px;
  color: #aaa;
  font-style: italic;
}

/* Filter grid */
.filter-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  margin-top: 6px;
}
.filter-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 4px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  background: #fafafa;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
}
.filter-item:hover { border-color: #ffb74d; transform: scale(1.05); }
.filter-item:active { transform: scale(0.97); }
.filter-item.active { border-color: #ff9800; background: #fff3e0; }
.filter-item canvas {
  border-radius: 4px;
  display: block;
  width: 56px;
  height: 42px;
}
.filter-item span {
  font-size: 9px;
  color: #888;
  white-space: nowrap;
  text-align: center;
}
.filter-lock-label {
  font-size: 8px;
  color: #b71c1c;
  font-weight: 700;
  white-space: nowrap;
}
.scene-lock-label {
  font-size: 8px;
  color: #b71c1c;
  font-weight: 700;
  white-space: nowrap;
}

/* ===== CANVAS AREA (CENTER) ===== */
.canvas-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px;
  overflow: auto;
  background: #f0ebe3;
}
.page-navigation {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}
.page-nav-btn {
  background: #ffe082;
  border: none;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 16px;
  font-weight: 700;
  color: #5d4037;
}
.page-nav-btn:disabled { opacity: 0.35; cursor: default; }
#page-indicator { font-size: 14px; color: #888; font-weight: 700; }

.comic-page {
  width: 680px;
  min-height: 880px;
  background: #fff;
  border: 3px solid #222;
  border-radius: 8px;
  position: relative;
  box-shadow: 4px 4px 0 rgba(0,0,0,0.1);
  overflow: hidden;
}

/* ===== LAYOUT PALETTE (now inside left toolbar) ===== */
.layout-grid { display: flex; flex-direction: column; gap: 6px; }
.layout-option {
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  padding: 4px;
  cursor: pointer;
  background: #fafafa;
  transition: border-color 0.15s;
}
.layout-option:hover { border-color: #ffb74d; }
.layout-option.active { border-color: #ff9800; background: #fff3e0; }
.layout-thumb {
  width: 100%;
  aspect-ratio: 3/4;
  display: grid;
  gap: 1px;
}
.layout-thumb div { background: #e0e0e0; border-radius: 2px; }
.layout-option-label {
  text-align: center;
  font-size: 10px;
  color: #888;
  margin-top: 2px;
}

/* ===== PAGES PALETTE (RIGHT) ===== */
.pages-palette {
  width: 160px;
  background: #fff;
  border-left: 2px solid #ffe0b2;
  padding: 12px 10px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.pages-palette h3 {
  font-size: 12px;
  text-transform: uppercase;
  color: #aaa;
  margin-bottom: 4px;
  letter-spacing: 1px;
}
.pages-list { display: flex; flex-direction: column; gap: 8px; flex: 1; overflow-y: auto; }

.page-thumb {
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  padding: 4px;
  cursor: pointer;
  background: #fafafa;
  transition: border-color 0.15s, transform 0.1s;
  position: relative;
}
.page-thumb:hover { border-color: #ffb74d; transform: scale(1.03); }
.page-thumb.active-page { border-color: #ff9800; background: #fff3e0; box-shadow: 0 0 0 1px #ff9800; }

.page-thumb-delete {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 50%;
  background: #ffcdd2;
  color: #c62828;
  font-size: 11px;
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  line-height: 1;
  z-index: 2;
}
.page-thumb:hover .page-thumb-delete { display: flex; }
.page-thumb-grid {
  width: 100%;
  aspect-ratio: 3/4;
  display: grid;
  gap: 1px;
  background: #fff;
  border: 1px solid #ccc;
  border-radius: 4px;
  overflow: hidden;
}
.page-thumb-grid div { background: #f5f5f5; border-radius: 1px; }
.page-thumb-label {
  text-align: center;
  font-size: 10px;
  color: #888;
  font-weight: 700;
  margin-top: 3px;
}

.add-page-thumb-btn {
  background: #ffe082;
  border: 2px dashed #ffb74d;
  border-radius: 8px;
  padding: 8px;
  font-family: 'Fredoka One', cursive;
  font-size: 12px;
  color: #5d4037;
  cursor: pointer;
  transition: background 0.15s;
}
.add-page-thumb-btn:hover { background: #ffd54f; }

/* Pages panel toggle */
.pages-palette {
  transition: width 0.25s ease, padding 0.25s ease, opacity 0.2s ease;
}
.editor-body.pages-hidden .pages-palette {
  width: 0;
  padding: 0;
  opacity: 0;
  overflow: hidden;
  border-left: none;
}
#btn-toggle-pages {
  font-family: 'Fredoka One', cursive;
  font-size: 13px;
  margin-left: 8px;
}
#btn-toggle-pages.pages-active {
  background: #ffcc80;
}

/* ===== MODAL ===== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal {
  background: #fff;
  border-radius: 20px;
  padding: 28px;
  max-width: 480px;
  width: 90%;
  position: relative;
  box-shadow: 0 12px 40px rgba(0,0,0,0.2);
}
.modal-close {
  position: absolute;
  top: 12px;
  right: 14px;
  background: none;
  border: none;
  font-size: 22px;
  color: #aaa;
}
.modal-body h3 { margin-bottom: 12px; color: #5d4037; }
.modal-body textarea {
  width: 100%;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  padding: 10px;
  font-family: 'Comic Neue', cursive;
  font-size: 16px;
  resize: vertical;
  min-height: 80px;
  outline: none;
}
.modal-body textarea:focus { border-color: #ffb74d; }
.modal-body .modal-actions { display: flex; gap: 8px; margin-top: 14px; justify-content: flex-end; }
.modal-body .btn-primary {
  background: #ff9800;
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 8px 20px;
  font-weight: 700;
  font-size: 14px;
}
.modal-body .btn-secondary {
  background: #eee;
  color: #555;
  border: none;
  border-radius: 10px;
  padding: 8px 20px;
  font-weight: 700;
  font-size: 14px;
}

/* ===== Scrollbar styling ===== */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ddd; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #bbb; }

/* ===== LANGUAGE SWITCHER ===== */
.lang-switcher-btn {
  position: fixed;
  top: 12px;
  right: 12px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 3px solid #ffe0b2;
  background: #fff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  z-index: 200;
}
.lang-switcher-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  border-color: #ffb74d;
}
/* Hide globe when the editor is open */
#editor-screen.active ~ .lang-switcher-btn { display: none; }

/* iOS install banner */
.ios-install-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff8e1;
  border-top: 3px solid #ff9800;
  padding: 14px 48px 14px 18px;
  font-family: 'Comic Neue', cursive;
  font-size: 15px;
  color: #4a3600;
  z-index: 500;
  box-shadow: 0 -4px 16px rgba(0,0,0,0.1);
  text-align: center;
}
.ios-install-close {
  position: absolute;
  top: 50%;
  right: 12px;
  transform: translateY(-50%);
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #999;
  padding: 4px 8px;
}

.lang-picker {
  display: none;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  border: 3px solid #ffe0b2;
  border-radius: 20px;
  padding: 12px 8px;
  z-index: 300;
  box-shadow: 0 12px 40px rgba(0,0,0,0.2);
  min-width: 200px;
}
.lang-picker.open {
  display: flex;
  flex-direction: column;
  gap: 4px;
  animation: langPickerIn 0.2s ease-out;
}
@keyframes langPickerIn {
  from { opacity: 0; transform: translate(-50%, -50%) scale(0.9); }
  to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}
.lang-option {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border: none;
  background: none;
  border-radius: 12px;
  font-family: 'Fredoka One', cursive;
  font-size: 18px;
  color: #5d4037;
  cursor: pointer;
  transition: background 0.12s;
}
.lang-option:hover {
  background: #fff3e0;
}
.lang-option.active {
  background: #ffe0b2;
}
.lang-flag {
  font-size: 28px;
  line-height: 1;
}
.lang-name {
  font-size: 16px;
}

/* ===== RESPONSIVE: SMALL SCREENS ===== */
@media (max-width: 768px) {
  /* Editor body becomes vertical */
  .editor-body {
    flex-direction: column;
  }

  /* Tool palette: horizontal bar */
  .tool-palette {
    width: 100%;
    flex-direction: row;
    border-right: none;
    border-bottom: 2px solid #ffe0b2;
    padding: 4px 8px;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 2px;
  }
  .tool-palette .tool-group {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 2px;
  }
  .tool-palette .tool-group h3 {
    display: none;
  }
  .palette-btn {
    width: 38px;
    height: 38px;
    font-size: 18px;
    margin: 0;
  }

  /* Content panel: overlay from left */
  .content-panel {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 50;
    width: 220px;
    max-width: 80vw;
    box-shadow: 4px 0 20px rgba(0,0,0,0.15);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }
  .content-panel.mobile-open {
    transform: translateX(0);
  }

  /* Canvas area takes remaining space */
  .canvas-area {
    flex: 1;
    min-height: 0;
  }

  /* Topbar compact */
  .editor-topbar {
    gap: 6px;
    padding: 6px 10px;
    flex-wrap: wrap;
  }
  .story-title-input {
    font-size: 14px;
    padding: 4px 10px;
    min-width: 0;
  }
  .tool-btn {
    padding: 4px 10px;
    font-size: 12px;
  }

  /* Pages palette: horizontal strip at bottom instead of side column */
  .pages-palette {
    width: 100% !important;
    flex-direction: row;
    border-left: none;
    border-top: 2px solid #ffe0b2;
    padding: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 8px;
    max-height: 130px;
  }
  .pages-palette h3 {
    writing-mode: vertical-lr;
    margin: 0 2px 0 0;
    font-size: 10px;
  }
  .pages-list {
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 8px;
  }
  .page-thumb {
    min-width: 80px;
    max-width: 80px;
  }
  .add-page-thumb-btn {
    white-space: nowrap;
    min-width: 80px;
    padding: 6px 10px;
    font-size: 10px;
    align-self: center;
  }
  .editor-body.pages-hidden .pages-palette {
    height: 0;
    max-height: 0;
    padding: 0;
    opacity: 0;
    overflow: hidden;
    border-top: none;
  }

  /* Bookshelf: smaller books on narrow screens */
  .stories-list {
    padding: 16px 12px;
  }
  .shelf-row {
    min-height: 140px;
    padding: 12px 8px 0;
    gap: 8px;
  }
  .book-spine {
    width: 42px;
    height: 120px;
  }
  .book-title {
    font-size: 9px;
    max-height: 90px;
  }
  .book-icon {
    font-size: 14px;
  }
  .shelf-plank {
    height: 14px;
    margin-bottom: 4px;
  }
}
