/* Coloring Fun -- phone-first styles.
 * Layout rules: nothing scrolls except the page gallery and the paint palette,
 * every tap target is at least 48px, and the canvas is sized in JS so it always
 * fits between the toolbars on any phone or tablet.
 */

:root {
  --sky: #eaf4ff;
  --sky-2: #ffeef7;
  --ink: #23314d;
  --ink-soft: #5b6b8c;
  --card: #ffffff;
  --line: #d8e3f3;
  --accent: #ff5ea8;
  --accent-2: #4aa8ff;
  --shadow: 0 4px 0 rgba(35, 49, 77, .16);
  --radius: 20px;
  --bar: rgba(255, 255, 255, .92);
  --font: ui-rounded, "SF Pro Rounded", "Baloo 2", "Comic Sans MS", "Trebuchet MS",
    system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: var(--font);
  color: var(--ink);
  background: linear-gradient(160deg, var(--sky) 0%, var(--sky-2) 100%);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

/* ---------------------------------------------------------------- gallery */

.gallery-page { min-height: 100dvh; display: flex; flex-direction: column; }

.hero {
  padding: max(18px, env(safe-area-inset-top)) 18px 6px;
  text-align: center;
}

.hero h1 {
  margin: 6px 0 2px;
  font-size: clamp(28px, 7vw, 46px);
  letter-spacing: -.5px;
}

.hero h1 .pop { color: var(--accent); }

.hero p {
  margin: 0 0 4px;
  color: var(--ink-soft);
  font-size: clamp(15px, 3.6vw, 19px);
}

.grid {
  flex: 1;
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  padding: 14px 16px calc(28px + env(safe-area-inset-bottom));
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
  align-content: start;
}

.card {
  display: block;
  background: var(--card);
  border: 3px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 10px 12px;
  text-decoration: none;
  color: inherit;
  box-shadow: var(--shadow);
  transition: transform .12s ease, border-color .12s ease;
  position: relative;
}

.card:active { transform: translateY(2px) scale(.99); }
@media (hover: hover) { .card:hover { border-color: var(--accent-2); } }

.card.blank {
  border-style: dashed;
  border-color: var(--accent-2);
  background: linear-gradient(180deg, #ffffff 0%, #f2f8ff 100%);
}

.card .thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  display: block;
  border-radius: 14px;
  background: #fff;
  overflow: hidden;
}

.card .thumb svg,
.card .thumb img { width: 100%; height: 100%; display: block; object-fit: cover; }

.card .label {
  margin-top: 8px;
  font-size: 16px;
  line-height: 1.2;
  text-align: center;
  font-weight: 700;
}

.card .badge {
  position: absolute;
  top: -10px;
  right: -6px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  padding: 5px 10px;
  border-radius: 999px;
  box-shadow: var(--shadow);
}

.foot {
  text-align: center;
  color: var(--ink-soft);
  font-size: 13px;
  padding: 0 16px calc(16px + env(safe-area-inset-bottom));
}

/* ---------------------------------------------------------- coloring page */

.color-page {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  touch-action: none;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) 10px 8px;
  background: var(--bar);
  border-bottom: 3px solid var(--line);
  flex: none;
}

.topbar .title {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-size: clamp(15px, 4vw, 22px);
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.iconbtn {
  min-width: 52px;
  height: 52px;
  padding: 0 10px;
  border-radius: 16px;
  background: #fff;
  border: 3px solid var(--line);
  box-shadow: var(--shadow);
  font-size: 22px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.iconbtn .txt { font-size: 15px; font-weight: 700; }
.iconbtn:active { transform: translateY(2px); box-shadow: none; }
.iconbtn[disabled] { opacity: .38; box-shadow: none; }

.main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.stage {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 8px;
  position: relative;
}

/* Pinch-zoom lives on this inner box so the frame and its rounded corners
   stay put while the picture scales inside them. */
.zoomer {
  position: absolute;
  inset: 0;
  transform-origin: 0 0;
  will-change: transform;
}

.fitbtn {
  position: absolute;
  right: 16px;
  bottom: 16px;
  z-index: 25;
  background: rgba(255, 255, 255, .93);
  border: 3px solid var(--line);
  border-radius: 16px;
  padding: 12px 16px;
  font-weight: 700;
  box-shadow: var(--shadow);
}

.fitbtn[hidden] { display: none !important; }

#mirror { margin-left: 14px; }
#extras[hidden] { display: none !important; }
#extras .tool { border-color: #cfe4ff; }

.canvas-wrap {
  position: relative;
  background: #fff;
  border: 3px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 6px 18px rgba(35, 49, 77, .13);
  overflow: hidden;
  touch-action: none;
}

.canvas-wrap canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

#lines { pointer-events: none; }

.dock {
  flex: none;
  background: var(--bar);
  border-top: 3px solid var(--line);
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#tools {
  flex-wrap: wrap;
  justify-content: center;
  overflow: visible;
}

.row {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 2px;
  touch-action: pan-x;
}

.row::-webkit-scrollbar { display: none; }

.tool {
  flex: none;
  width: 60px;
  height: 60px;
  border-radius: 18px;
  background: #fff;
  border: 3px solid var(--line);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  gap: 0;
  font-size: 24px;
  line-height: 1;
}

.tool .name { font-size: 10px; font-weight: 700; color: var(--ink-soft); }
.tool[aria-pressed="true"] { border-color: var(--accent); background: #fff2f8; }
.tool:active { transform: translateY(2px); box-shadow: none; }

.swatch {
  flex: none;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 3px solid #fff;
  box-shadow: 0 0 0 3px var(--line), var(--shadow);
}

.swatch[aria-pressed="true"] {
  box-shadow: 0 0 0 4px var(--accent), var(--shadow);
  transform: scale(1.06);
}

.swatch:active { transform: scale(.94); }

.sizes { display: flex; gap: 8px; flex: none; }

.size {
  width: 52px;
  height: 52px;
  border-radius: 16px;
  background: #fff;
  border: 3px solid var(--line);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
}

.size i { display: block; border-radius: 50%; background: var(--ink); }
.size[aria-pressed="true"] { border-color: var(--accent); background: #fff2f8; }

.hint {
  text-align: center;
  font-size: 13px;
  color: var(--ink-soft);
  margin: 0;
}

/* Landscape (tablet, desktop, phone on its side): the tools move beside the
 * picture so the picture itself stays as big as the screen allows. Portrait
 * keeps the dock at the bottom, within thumb reach. */
@media (orientation: landscape) and (min-width: 560px) {
  :root { --dock-w: clamp(184px, 23vw, 260px); }
  .main { flex-direction: row; }
  .stage { flex: 1; padding: 12px; }
  .dock {
    width: var(--dock-w);
    border-top: 0;
    border-left: 3px solid var(--line);
    padding: 12px 10px calc(12px + env(safe-area-inset-bottom));
    overflow-y: auto;
  }
  .dock .row { flex-wrap: wrap; overflow: visible; justify-content: center; }
  .dock .sizes { justify-content: center; width: 100%; }
  .scroller::after { display: none; }
}

/* A phone held sideways is very short -- shrink the furniture to suit. */
@media (orientation: landscape) and (max-height: 520px) {
  .tool, .size { width: 52px; height: 52px; font-size: 20px; border-radius: 14px; }
  .tool .name { font-size: 9px; }
  .swatch { width: 36px; height: 36px; }
  .iconbtn { min-width: 46px; height: 46px; font-size: 19px; }
  .topbar { padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: 4px; }
  :root { --dock-w: 240px; }
  .dock { gap: 6px; }
  .hint { display: none; }
}

/* Fade at the edge of the colour strip so it reads as "swipe for more". */
.scroller { position: relative; min-width: 0; }

.scroller::after {
  content: "";
  position: absolute;
  top: 0;
  right: -2px;
  bottom: 0;
  width: 34px;
  pointer-events: none;
  background: linear-gradient(to right, rgba(255, 255, 255, 0), var(--bar) 80%);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(22px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 50;
}

.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ------------------------------------------------------------- full screen */

/* The bar stops taking up a row and floats over the corners instead, so the
 * picture gets the whole screen. The buttons stay tappable, the strip itself
 * does not, so a stroke that starts up there still lands on the paper. */
body.fs .topbar {
  position: absolute;
  top: 0;
  left: 0;
  /* In landscape the dock is a sidebar, so stop the floating buttons at its
   * edge instead of letting them land on top of the tools. */
  right: var(--dock-w, 0px);
  z-index: 30;
  background: none;
  border-bottom: 0;
  pointer-events: none;
  padding: max(6px, env(safe-area-inset-top)) 8px 6px;
}

body.fs .topbar { transition: opacity .25s ease; }
body.fs.drawing .topbar { opacity: .25; }
body.fs .topbar .title { display: none; }

body.fs .topbar .iconbtn {
  pointer-events: auto;
  background: rgba(255, 255, 255, .9);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

body.fs .topbar .iconbtn:first-child { margin-right: auto; }
body.fs .hint { display: none; }
body.fs .stage { padding: 4px; }
body.fs .dock { padding-top: 6px; }

/* ------------------------------------------------------------ action sheet */

.sheet[hidden] { display: none !important; }

.sheet {
  position: fixed;
  inset: 0;
  background: rgba(35, 49, 77, .38);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 60;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
}

.sheet-card {
  width: min(420px, 100%);
  background: #fff;
  border-radius: 24px;
  border: 3px solid var(--line);
  padding: 10px;
  display: grid;
  gap: 8px;
  box-shadow: 0 12px 40px rgba(35, 49, 77, .3);
}

.sheet-btn {
  min-height: 58px;
  border-radius: 16px;
  background: #f4f8ff;
  border: 3px solid var(--line);
  font-size: 18px;
  font-weight: 700;
}

.sheet-btn:active { transform: translateY(2px); }
.sheet-btn.danger { background: #fff0f4; border-color: #ffc9dd; color: #c81e5b; }

@media (min-width: 820px) {
  .sheet { align-items: center; }
}

@media (orientation: landscape) and (max-height: 520px) {
  .sheet { align-items: center; }
  .sheet-btn { min-height: 48px; font-size: 16px; }
}

@media print {
  @page { margin: 12mm; }
  html, body { height: auto !important; background: #fff !important; }
  .topbar, .dock, .hint, .toast, .sheet { display: none !important; }
  .color-page, .main, .stage {
    display: block !important;
    height: auto !important;
    overflow: visible !important;
    padding: 0 !important;
  }
  .canvas-wrap {
    position: relative;
    border: 0;
    box-shadow: none;
    width: 100% !important;
    height: auto !important;
    aspect-ratio: 1 / 1;
    max-height: 23cm;
    break-inside: avoid;
  }
}
