/* Colorbook — crayon-box theme */

:root {
  --paper: #fff9ef;
  --ink: #43395c;
  --ink-soft: #8b81a3;
  --accent: #ff6b4a; /* crayon orange */
  --card: #ffffff;
  --shadow: 0 4px 14px rgba(67, 57, 92, 0.12);
  --radius: 20px;
  /* the crayon box — cycled across category cards */
  --crayon-1: #ff6b4a;
  --crayon-2: #4aa3ff;
  --crayon-3: #ffc93c;
  --crayon-4: #6fd08c;
  --crayon-5: #c78bff;
  --crayon-6: #ff8fb1;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: ui-rounded, -apple-system, "SF Pro Rounded", "Comic Sans MS", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- top bar ---------- */

.topbar {
  padding: 18px 24px 6px;
  display: flex;
  justify-content: center;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: var(--ink);
  text-decoration: none;
}

.brand-crayons {
  display: flex;
  gap: 3px;
  align-items: flex-end;
}

.brand-crayons i {
  width: 9px;
  height: 26px;
  border-radius: 4px 4px 2px 2px;
  position: relative;
}

/* crayon tips */
.brand-crayons i::before {
  content: "";
  position: absolute;
  top: -7px;
  left: 0;
  border-left: 4.5px solid transparent;
  border-right: 4.5px solid transparent;
  border-bottom: 7px solid currentColor;
}

.brand-crayons i:nth-child(1) { background: var(--crayon-1); color: var(--crayon-1); height: 22px; }
.brand-crayons i:nth-child(2) { background: var(--crayon-2); color: var(--crayon-2); height: 28px; }
.brand-crayons i:nth-child(3) { background: var(--crayon-3); color: var(--crayon-3); height: 24px; }
.brand-crayons i:nth-child(4) { background: var(--crayon-4); color: var(--crayon-4); height: 27px; }

/* ---------- layout ---------- */

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 18px 20px calc(40px + env(safe-area-inset-bottom));
}

.loading {
  text-align: center;
  color: var(--ink-soft);
  font-size: 1.2rem;
}

.page-head {
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 6px 0 18px;
}

.page-head h1 {
  margin: 0;
  font-size: 1.7rem;
  font-weight: 800;
}

.page-head .count {
  color: var(--ink-soft);
  font-size: 1rem;
  font-weight: 600;
}

.btn-back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 0 16px;
  border: none;
  border-radius: 999px;
  background: var(--card);
  box-shadow: var(--shadow);
  color: var(--ink);
  font-size: 1.1rem;
  font-weight: 700;
  font-family: inherit;
  text-decoration: none;
  cursor: pointer;
}

/* ---------- category cards (home) ---------- */

.cat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 20px;
}

.cat-card {
  display: block;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  text-decoration: none;
  color: var(--ink);
  transition: transform 0.15s ease;
  border-top: 8px solid var(--crayon, var(--accent));
}

.cat-card:active { transform: scale(0.97) rotate(-0.5deg); }

.cat-card img {
  width: 100%;
  aspect-ratio: 4 / 5;
  object-fit: cover;
  object-position: top;
  display: block;
  background: #fff;
}

.cat-card .cat-label {
  padding: 12px 16px 16px;
}

.cat-card h2 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
}

.cat-card .cat-count {
  color: var(--ink-soft);
  font-size: 0.95rem;
  font-weight: 600;
}

/* ---------- page grid (category view) ---------- */

.page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 20px;
}

.page-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.page-card .thumb-btn {
  border: none;
  padding: 0;
  background: #fff;
  cursor: pointer;
  display: block;
  width: 100%;
}

.page-card .thumb-btn img {
  width: 100%;
  aspect-ratio: 4 / 5;
  object-fit: cover;
  object-position: top;
  display: block;
}

.page-card .card-info {
  padding: 10px 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}

.page-card h3 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.25;
}

.card-row {
  margin-top: auto;
}

.card-row .btn-print {
  width: 100%;
}

.badge {
  display: inline-block;
  background: #f1ecff;
  color: #6a5aa8;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.85rem;
  font-weight: 700;
  white-space: nowrap;
}

.btn-print {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 18px;
  border: none;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--crayon-3) -20%, var(--accent) 45%, var(--crayon-6) 120%);
  color: #fff;
  font-size: 1rem;
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 3px 8px rgba(255, 107, 74, 0.35);
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}

.btn-print:hover {
  box-shadow: 0 4px 14px rgba(255, 107, 74, 0.45);
  filter: brightness(1.05);
}

.btn-print:active {
  transform: scale(0.94);
  filter: brightness(1.15) saturate(1.2);
  box-shadow:
    0 0 16px rgba(255, 107, 74, 0.75),
    0 0 34px rgba(255, 143, 177, 0.5);
}

/* ---------- pagination ---------- */

.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 26px;
}

.pager button {
  min-width: 110px;
  min-height: 52px;
  border: none;
  border-radius: 999px;
  background: var(--card);
  box-shadow: var(--shadow);
  color: var(--ink);
  font-size: 1.1rem;
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
}

.pager button:disabled {
  opacity: 0.35;
  cursor: default;
}

.pager .pager-status {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--ink-soft);
  min-width: 90px;
  text-align: center;
}

/* ---------- preview overlay ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(67, 57, 92, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}

.overlay[hidden] { display: none; }

.overlay-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
  max-width: min(560px, 100%);
  max-height: calc(100dvh - 40px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.overlay-card img {
  width: 100%;
  flex: 1;
  min-height: 0;
  object-fit: contain;
  background: #fff;
}

.overlay-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: rgba(67, 57, 92, 0.75);
  color: #fff;
  font-size: 1.2rem;
  cursor: pointer;
  z-index: 1;
}

.overlay-info {
  padding: 14px 18px 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.overlay-info h2 {
  margin: 0;
  font-size: 1.3rem;
  font-weight: 800;
  flex: 1;
  min-width: 140px;
}

.btn-print-big {
  min-height: 52px;
  padding: 0 26px;
  font-size: 1.15rem;
}

/* ---------- responsive ---------- */

@media (max-width: 480px) {
  .cat-grid, .page-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 14px;
  }
  .brand { font-size: 1.6rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

:focus-visible {
  outline: 3px solid var(--crayon-2);
  outline-offset: 2px;
}

/* ---------- confetti ---------- */

#confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 100;
}

/* ---------- printing ---------- */

#print-area { display: none; }

@media print {
  body > *:not(#print-area) { display: none !important; }
  html, body {
    height: auto;
    margin: 0;
    background: #fff;
    overflow: hidden;
  }
  #print-area {
    display: block;
  }
  #print-area img {
    display: block;
    width: 100%;
    /* physical inches, safely under one letter sheet: 11in minus the 0.5in
       of @page margins minus the printer's own non-printable strip — a
       whisker-over box adds a second (blank-ish) page on iOS */
    max-height: 9.85in;
    object-fit: contain;
    object-position: top center;
    break-inside: avoid;
  }
  /* landscape pages: rotate to fill the portrait sheet instead of asking the
     browser for landscape paper — @page size rules stall iOS print preview */
  #print-area.landscape { height: 9.9in; }
  #print-area.landscape img {
    width: 9.85in;   /* vertical extent once rotated */
    max-height: 8in; /* horizontal extent once rotated */
    transform: rotate(90deg) translateY(-100%);
    transform-origin: top left;
  }
  /* slim margin: just enough to clear printer non-printable edges.
     Never 0 — @page margin 0 breaks desktop Safari output entirely. */
  @page { margin: 0.25in; }
}
