/* ═══════════════════════════════════════════════════════
   Coloring Book — Figma Restyled
   ═══════════════════════════════════════════════════════ */

/* ─── Reset ──────────────────────────────────────────── */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  /* Stop scroll chaining from rubber-banding the whole document up under
     the Flutter header overlay (matches jigsaw/memory). */
  overscroll-behavior: none;
  font-family: 'Poppins', sans-serif;
  background: linear-gradient(135deg, #FFF5E6 0%, #FFEEF0 35%, #E8F8F8 65%, #F0E0F8 100%);
}

body {
  position: fixed;
}

canvas, button, div {
  -webkit-touch-callout: none;
}

/* ─── Utility ────────────────────────────────────────── */
.hidden {
  display: none !important;
}

/* ─── App Header (handled by Flutter — hidden in WebView) ── */
#app-header {
  display: none !important;
}

/* ─── Library View ───────────────────────────────────── */
#library-view {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  /* padding-top accounts for the Flutter header overlapping
     (140px matches the memory game's clearance) */
  padding-top: 140px;
  display: flex;
  flex-direction: column;
}

/* ─── Library Tabs (soft text tabs — flat, no track) ──
   Centered text labels; the active tab gets a soft teal-tint
   rounded label, inactive is muted. Deliberately NOT a pill bar,
   so it doesn't stack a second rounded-teal shape under the
   rounded teal Flutter header. */
#library-tabs {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-shrink: 0;
  /* Extra top margin so the tabs don't crowd the header's rounded bottom edge. */
  margin: 18px 0 6px;
  padding: 0 25px;
}

.tab-btn {
  border: none;
  background: transparent;
  font-family: 'Onest', sans-serif;
  font-weight: 600;
  font-size: 16px;
  color: #8FA3A3;
  padding: 9px 18px;
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, transform 0.1s;
}

.tab-btn:active {
  transform: scale(0.97);
}

.tab-btn.active {
  background: #CFEDE4;
  color: #057676;
}

#library-scroll {
  width: 100%;
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  /* Keep overscroll inside the scroller — never chain to the document */
  overscroll-behavior: contain;
  padding: 20px 25px;
  padding-bottom: calc(25px + env(safe-area-inset-bottom, 0px));
}

/* ─── Category Sections ──────────────────────────────── */
.category-section {
  margin-bottom: 24px;
}

.category-header {
  display: flex;
  align-items: center;
  gap: 12px;
}

.category-header span {
  font-family: 'Onest', sans-serif;
  font-weight: 600;
  font-size: 20px;
  color: #000;
  white-space: nowrap;
}

.category-header .category-line {
  flex: 1;
  height: 2px;
  background: #E0E0E0;
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
  margin-top: 16px;
}

/* ─── Page Cards ─────────────────────────────────────── */
/* Grow-and-fade entry, matching the memory game's "bounce-in" populate. */
@keyframes bounce-in {
  0% { transform: scale(0); opacity: 0; }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); opacity: 1; }
}

/* Page + creation cards share the jigsaw gallery treatment:
   white outline, soft shadow, thumbnail on top, title label below. */
.page-card,
.creation-card {
  background: #fff;
  border: 3px solid #fff;
  border-radius: 15px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.18);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  animation: bounce-in 0.5s ease-out both;
  position: relative;
  display: flex;
  flex-direction: column;
}

.page-card:active,
.creation-card:active {
  transform: scale(0.96);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

/* aspect-ratio lives on the wrapper div, NOT the img — WKWebView collapses
   aspect-ratio on imgs inside grid items. */
.card-thumb {
  width: 100%;
  /* Square crop to match the jigsaw gallery thumbnails. The img below uses
     object-fit: cover, so non-square art pages are centre-cropped to 1:1. */
  aspect-ratio: 1 / 1;
  position: relative;
  overflow: hidden;
  border-radius: 12px 12px 0 0;
}

.card-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.card-label {
  padding: 8px 10px;
  font-family: 'Onest', sans-serif;
  font-weight: 600;
  font-size: 14px;
  text-align: center;
  color: #057676;
  background: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ─── Locked Page Cards (paygate) ────────────────────── */
.page-card.locked .card-thumb img {
  opacity: 0.5;
}

/* Subtle scrim so the lock badge stays readable over any thumbnail. */
.page-card.locked .card-thumb::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.12);
  pointer-events: none;
}

.lock-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
  pointer-events: none;
  z-index: 2;
}

/* ─── Creation Cards ─────────────────────────────────── */
.creation-delete {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  border: none;
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
}

.creation-delete:active {
  transform: scale(0.9);
}

/* ─── My Creations: empty state ──────────────────────── */
.creations-empty {
  text-align: center;
  padding: 48px 24px;
}

.creations-empty-title {
  font-family: 'Onest', sans-serif;
  font-weight: 600;
  font-size: 22px;
  color: #057676;
  margin-bottom: 8px;
}

.creations-empty-sub {
  font-size: 15px;
  color: #666;
  margin-bottom: 24px;
}

.creations-empty-cta {
  background: #FF7639;
  color: #fff;
  border: none;
  border-radius: 15px;
  padding: 14px 28px;
  font-family: 'Poppins', sans-serif;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(255, 118, 57, 0.4);
}

.creations-empty-cta:active {
  transform: scale(0.96);
}

/* ─── Canvas View ────────────────────────────────────── */
#canvas-view {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  /* padding-top accounts for the Flutter header overlapping
     (140px matches the memory game's clearance) */
  padding-top: 140px;
  display: flex;
  flex-direction: column;
}

/* ─── Canvas Card ────────────────────────────────────── */
#canvas-card {
  background: #fff;
  border-radius: 15px;
  box-shadow: 4px 5px 13.2px rgba(0, 0, 0, 0.37);
  margin: 8px 12px 0;
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
}

#canvas-container {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  touch-action: none;
  background: #fff;
}

#color-canvas,
#line-canvas {
  position: absolute;
  touch-action: none;
  transform-origin: center center;
}

#color-canvas {
  z-index: 1;
}

#line-canvas {
  z-index: 2;
  pointer-events: none;
}

/* ─── Controls Area (below canvas) ───────────────────── */
#controls {
  flex-shrink: 0;
  padding: 0 20px;
  padding-bottom: env(safe-area-inset-bottom, 8px);
}

/* ─── Color Palette ──────────────────────────────────── */
#color-palette {
  background: #fff;
  border-radius: 23px;
  box-shadow: 0 4px 45.1px rgba(0, 101, 101, 0.43);
  padding: 12px 16px;
  margin: 8px 12px 0;
}

.color-row {
  display: flex;
  justify-content: space-between;
}

.color-row:first-child {
  margin-bottom: 8px;
}

.color-swatch {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s;
  flex-shrink: 0;
}

.color-swatch.active {
  border-color: #00B5B5;
  transform: scale(1.15);
}

.color-swatch:active {
  transform: scale(0.9);
}

.color-swatch[data-color="#FFFFFF"],
.color-swatch.white-swatch {
  border: 1px solid #000;
}

.color-swatch[data-color="#FFFFFF"].active,
.color-swatch.white-swatch.active {
  border: 3px solid #00B5B5;
}

/* ─── Icon Bar (all tool buttons in one row) ─────────── */
#icon-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 8px 12px;
  gap: 8px;
}

/* ─── Icon Buttons (card PNGs, shadow added via CSS) ──── */
.icon-btn {
  background: transparent;
  border: 3px solid transparent;
  border-radius: 18px;
  padding: 0;
  cursor: pointer;
  flex: 1 1 0;
  max-width: 68px;
  transition: transform 0.15s, border-color 0.15s;
  box-shadow: 0 4px 16px rgba(0, 86, 86, 0.25);
}

.icon-btn img {
  width: 100%;
  aspect-ratio: 1;
  display: block;
  border-radius: 15px;
  pointer-events: none;
}

.icon-btn.active {
  border-color: #00B5B5;
}

.icon-btn:active {
  transform: scale(0.92);
}

/* ─── Size Slider Card ───────────────────────────────── */
#size-slider-card {
  height: 44px;
  background: #fff;
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(0, 86, 86, 0.2);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  margin: 0 12px 4px;
  transition: opacity 0.2s;
}

/* The slider only affects the brush — dimmed while the bucket is active */
#size-slider-card.inactive {
  opacity: 0.45;
}

/* Small/large dots at the slider ends signal "this changes size" */
.size-dot {
  flex-shrink: 0;
  border-radius: 50%;
  background: #057676;
}

.size-dot-min {
  width: 8px;
  height: 8px;
}

.size-dot-max {
  width: 20px;
  height: 20px;
}

/* Live preview well: shows the brush at its current size and color */
#size-preview {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid #E0E0E0;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

#size-preview-dot {
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: #FF8205;
  /* Keep white visible against the white well */
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
  transition: width 0.1s, height 0.1s;
}

#size-slider {
  flex: 1 1 auto;
  width: auto;
  min-width: 0;
  height: 8px;
  -webkit-appearance: none;
  appearance: none;
  background: #B3BFE0;
  border-radius: 4px;
  outline: none;
  cursor: pointer;
}

#size-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 23px;
  height: 23px;
  border-radius: 50%;
  background: #fff;
  border: 2.3px solid #00ACAC;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  margin-top: -7.5px;
}

#size-slider::-moz-range-thumb {
  width: 23px;
  height: 23px;
  border-radius: 50%;
  background: #fff;
  border: 2.3px solid #00ACAC;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
  cursor: pointer;
}

#size-slider::-webkit-slider-runnable-track {
  height: 8px;
  border-radius: 4px;
  background: #B3BFE0;
}

#size-slider::-moz-range-track {
  height: 8px;
  border-radius: 4px;
  background: #B3BFE0;
  border: none;
}

/* ─── Save Overlay ───────────────────────────────────── */
#save-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
}

#save-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
}

#save-modal {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 385px;
  max-width: 90vw;
  max-height: 85vh;
  border-radius: 23px;
  background: linear-gradient(180deg, #fff 50%, #D5FFFF 100%);
  box-shadow: 0 4px 45.1px rgba(0, 0, 0, 0.25);
  overflow-y: auto;
  padding: 48px 40px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ─── Save Preview ───────────────────────────────────── */
#save-preview-wrap {
  width: 100%;
  border-radius: 20px;
  overflow: hidden;
}

#save-preview-wrap canvas {
  width: 100%;
  display: block;
}

/* ─── Save Badge ─────────────────────────────────────── */
/* Celebratory headline — deliberately NOT button-shaped (no border,
   background or shadow) so it doesn't read as tappable. */
#save-badge {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  padding: 4px 0 0;
  margin: 14px auto 10px;
}

#save-badge span {
  font-family: 'Poppins', sans-serif;
  font-weight: 900;
  font-size: 28px;
  letter-spacing: 0.5px;
  background: linear-gradient(90deg, #00B5B5, #FF7639);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

/* ─── Save Buttons ───────────────────────────────────── */
#save-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.save-btn {
  width: 304px;
  max-width: 100%;
  height: 50px;
  border-radius: 15px;
  font-family: 'Poppins', sans-serif;
  font-size: 17px;
  cursor: pointer;
  margin-bottom: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.save-btn:last-child {
  margin-bottom: 0;
}

.save-btn:active {
  transform: scale(0.97);
}

.save-btn.primary {
  background: #FF7639;
  border: 2px solid #FF7639;
  color: #fff;
  font-weight: 800;
}

.save-btn.outline {
  background: #fff;
  border: 2px solid #FF7639;
  color: #000;
  font-weight: 500;
}

/* "Return" — white with a teal outline so it clearly reads as tappable
   (the old solid gray looked like a disabled button). */
.save-btn.muted {
  background: #fff;
  border: 2px solid #00B5B5;
  color: #057676;
  font-weight: 600;
  box-shadow: 0 3px 8px rgba(0, 86, 86, 0.15);
}

.save-btn.muted:active {
  background: #E5F8F8;
}

/* Saved state for the primary button: dimmed, with a check glyph */
.save-btn.saved {
  opacity: 0.7;
}

.save-btn.saved::after {
  content: '✓';
  margin-left: 8px;
  font-weight: 900;
}

/* ─── Confirm Dialog ─────────────────────────────────── */
#confirm-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

/* Cream card matches the storybook's leave confirmation (shared "leave?"
   design across games). */
#confirm-box {
  background: #FFEED3;
  width: 100%;
  max-width: 340px;
  border-radius: 24px;
  padding: 28px 24px 24px;
  text-align: center;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.25);
}

#confirm-message {
  font-family: 'Poppins', sans-serif;
  font-size: 17px;
  color: #333;
  margin-bottom: 20px;
  line-height: 1.4;
}

#confirm-buttons {
  display: flex;
  gap: 10px;
}

#confirm-buttons button {
  flex: 1;
  padding: 12px 16px;
  border-radius: 24px;
  border: none;
  font-family: 'Poppins', sans-serif;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

/* Cancel — outlined teal, matching the storybook leave dialog. */
#confirm-no {
  background: #fff;
  color: #00B5B5;
  border: 2px solid #00B5B5;
}

/* Destructive confirm (Start Over / Delete) keeps the warning red. */
#confirm-yes {
  background: #FF4444;
  color: #fff;
}

/* Non-destructive confirmations (e.g. "Leave" — progress is saved) use the
   shared teal so the leave dialog matches the other games. */
#confirm-yes.primary {
  background: #00B5B5;
}

#confirm-no:active,
#confirm-yes:active {
  transform: scale(0.95);
}

/* ─── Toast (save confirmation) ──────────────────────── */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(36px + env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 16px);
  opacity: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  gap: 10px;
  background: #00B5B5;
  color: #fff;
  padding: 12px 22px;
  border-radius: 999px;
  box-shadow: 0 8px 24px rgba(0, 86, 86, 0.35);
  font-family: 'Poppins', sans-serif;
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
  pointer-events: none;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

#toast.show {
  transform: translate(-50%, 0);
  opacity: 1;
}

.toast-icon {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* Checkmark draws itself in as the toast appears */
.toast-icon svg path {
  stroke-dasharray: 24;
  stroke-dashoffset: 24;
}

#toast.show .toast-icon svg path {
  animation: toast-check 0.35s ease-out 0.15s forwards;
}

@keyframes toast-check {
  to { stroke-dashoffset: 0; }
}

/* ═══════════════════════════════════════════════════════
   Tablet Layout (iPad / 600px+)
   ═══════════════════════════════════════════════════════ */
@media (min-width: 600px) {

  /* Two-column: palette left, tools+slider stacked right, nothing squeezed */
  #controls {
    display: grid;
    grid-template-columns: auto auto;
    grid-template-rows: auto auto;
    gap: 8px;
    justify-content: center;
    padding: 8px 16px;
    padding-bottom: env(safe-area-inset-bottom, 12px);
  }

  /* Palette left, spanning both rows */
  #color-palette {
    grid-column: 1;
    grid-row: 1 / 3;
    margin: 0;
    padding: 14px 24px;
    align-self: center;
  }

  .color-row {
    gap: 10px;
  }

  .color-swatch {
    width: 46px;
    height: 46px;
  }

  .color-row:first-child {
    margin-bottom: 10px;
  }

  /* Tools top-right, keep their natural size */
  #icon-bar {
    grid-column: 2;
    grid-row: 1;
    margin: 0;
    gap: 8px;
    align-self: end;
  }

  .icon-btn {
    max-width: 76px;
  }

  /* Slider bottom-right */
  #size-slider-card {
    grid-column: 2;
    grid-row: 2;
    margin: 0;
    height: 48px;
    align-self: start;
  }

  #size-slider::-webkit-slider-thumb {
    width: 28px;
    height: 28px;
    margin-top: -10px;
  }

  /* ─── Library grid: more columns on tablet ────────── */
  .category-grid {
    grid-template-columns: repeat(3, 1fr);
  }

}
