:root {
  --bg: #f3efe7;
  --bg2: #efe9dd;
  --card: #fffdf7;
  --ink: #e84a5f;
  --text: #2b2b33;
  --muted: #8a8578;
  --line: #e4ddcd;
  --shadow: 0 10px 40px rgba(60, 50, 30, 0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 50% -10%, #fbf7ee, transparent),
    linear-gradient(160deg, var(--bg), var(--bg2));
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* ---------- Landing ---------- */
.landing {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  background:
    radial-gradient(900px 500px at 50% 0%, #fbf7ee, transparent),
    linear-gradient(160deg, #f4efe6, #ece4d4);
  transition: opacity .6s ease, visibility .6s;
}
.landing.hidden { opacity: 0; visibility: hidden; }
.landing-card {
  text-align: center; max-width: 520px; padding: 48px 32px;
}
.bloom {
  font-size: 64px; color: var(--ink);
  animation: spin 24s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.landing h1 {
  font-size: 42px; margin: 12px 0 6px; font-weight: 700; letter-spacing: -0.5px;
}
.lede { font-size: 19px; color: #5a5346; margin: 0 0 14px; }
.sub { color: var(--muted); line-height: 1.6; margin: 0 auto 28px; max-width: 420px; }
.begin {
  border: 0; cursor: pointer; font-size: 17px; font-weight: 600;
  color: #fff; background: var(--ink);
  padding: 15px 34px; border-radius: 999px;
  box-shadow: 0 8px 24px rgba(232, 74, 95, 0.35);
  transition: transform .15s ease, box-shadow .15s ease;
}
.begin:hover { transform: translateY(-2px); box-shadow: 0 12px 30px rgba(232,74,95,.42); }
.tiny { margin-top: 22px; color: var(--muted); font-size: 13px; }

/* landing showcase of finished mandalas */
.showcase-cap {
  margin: 30px 0 14px; color: var(--muted); font-size: 12px;
  text-transform: uppercase; letter-spacing: .14em; font-weight: 600;
}
.showcase {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px;
  max-width: 440px; margin: 0 auto;
}
.show-tile {
  width: 100%; aspect-ratio: 1; border-radius: 16px; background: #fffdf7;
  border: 1px solid var(--line); box-shadow: 0 12px 30px rgba(60, 50, 30, .14);
  opacity: 0; transform: translateY(12px) scale(.94);
  animation: tileIn .7s cubic-bezier(.2,.8,.2,1) forwards;
}
@keyframes tileIn { to { opacity: 1; transform: none; } }
@media (max-width: 520px) {
  .showcase { gap: 10px; max-width: min(92vw, 360px); }
  .showcase-cap { margin-top: 24px; }
}

/* ---------- App shell ---------- */
.app { min-height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; border-bottom: 1px solid var(--line);
  background: rgba(255,253,247,.7); backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.brand .dot { color: var(--ink); }
.session { display: flex; align-items: center; gap: 10px; }
.session-label { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .08em; }
.timer { font-variant-numeric: tabular-nums; font-size: 22px; font-weight: 700; color: var(--ink); }
.mandala-tag { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.ghost {
  background: transparent; border: 1px solid var(--line); color: var(--muted);
  border-radius: 999px; padding: 5px 12px; font-size: 12px; cursor: pointer;
}
.ghost:hover { color: var(--text); border-color: var(--muted); }

/* ---------- Stage ---------- */
.stage {
  flex: 1; display: grid; grid-template-columns: 280px 1fr;
  gap: 24px; padding: 24px; max-width: 1280px; margin: 0 auto; width: 100%;
}

.panel {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 18px; padding: 20px; box-shadow: var(--shadow);
  align-self: start; position: sticky; top: 84px;
}
.group { margin-bottom: 22px; }
.group:last-child { margin-bottom: 0; }
.group h3 {
  margin: 0 0 12px; font-size: 13px; text-transform: uppercase;
  letter-spacing: .08em; color: var(--muted); font-weight: 600;
  display: flex; justify-content: space-between; align-items: center;
}
.val {
  background: var(--bg); border-radius: 6px; padding: 1px 8px;
  color: var(--text); font-size: 13px; font-variant-numeric: tabular-nums;
}

/* tools */
.tools { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 12px 4px; border: 1.5px solid var(--line); border-radius: 12px;
  background: #fff; cursor: pointer; font-size: 12px; font-weight: 600;
  color: var(--text); transition: all .15s ease;
}
.tool span { font-size: 22px; }
.tool:hover { border-color: var(--muted); }
.tool.active { border-color: var(--ink); background: #fff5f0; box-shadow: 0 0 0 3px rgba(232,74,95,.12); }

/* brush size */
input[type="range"] {
  width: 100%; accent-color: var(--ink); height: 6px;
}

/* palette */
.palette { display: grid; grid-template-columns: repeat(6, 1fr); gap: 7px; }
.swatch {
  aspect-ratio: 1; border-radius: 8px; border: 2px solid rgba(0,0,0,.06);
  cursor: pointer; padding: 0; transition: transform .1s ease;
}
.swatch:hover { transform: scale(1.12); }
.swatch.active { box-shadow: 0 0 0 3px var(--card), 0 0 0 5px var(--ink); transform: scale(1.05); }
.custom {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 12px; font-size: 13px; color: var(--muted);
}
.custom input[type="color"] {
  width: 44px; height: 30px; border: 1px solid var(--line);
  border-radius: 8px; background: none; cursor: pointer; padding: 2px;
}

/* actions */
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.action {
  padding: 10px; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; cursor: pointer; font-size: 13px; font-weight: 600; color: var(--text);
}
.action:hover { border-color: var(--muted); }
#save { grid-column: 1 / -1; }
.action.primary { grid-column: 1 / -1; background: var(--ink); color: #fff; border-color: var(--ink); }
.action.primary:hover { filter: brightness(1.05); }

/* share modal */
.share-modal {
  position: fixed; inset: 0; z-index: 60; display: none;
  align-items: center; justify-content: center; padding: 20px;
  background: rgba(40,35,25,.55); backdrop-filter: blur(6px);
}
.share-modal.open { display: flex; }
.share-card {
  position: relative; background: var(--card); border-radius: 20px;
  padding: 24px; width: min(380px, 92vw); text-align: center;
  box-shadow: var(--shadow); max-height: 92vh; overflow-y: auto;
}
.share-card h2 { margin: 0 0 4px; font-size: 20px; }
.share-sub { margin: 0 0 16px; color: var(--muted); font-size: 13px; }
.share-close {
  position: absolute; top: 12px; right: 12px; width: 32px; height: 32px;
  border-radius: 50%; border: 1px solid var(--line); background: #fff;
  cursor: pointer; color: var(--muted); font-size: 14px; line-height: 1;
}
.share-close:hover { color: var(--text); border-color: var(--muted); }
.share-preview {
  width: 200px; max-width: 60%; aspect-ratio: 9 / 16; object-fit: cover;
  border-radius: 14px; border: 1px solid var(--line);
  box-shadow: 0 8px 24px rgba(60,50,30,.15); margin: 0 auto 18px; display: block;
}
.share-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.share-btns.single { grid-template-columns: 1fr; }

/* add-to-home-screen card */
.install-card {
  position: fixed; left: 50%; bottom: 18px; transform: translate(-50%, 160%);
  z-index: 55; width: min(420px, calc(100vw - 28px));
  display: flex; align-items: center; gap: 14px;
  background: var(--card); border: 1px solid var(--line); border-radius: 18px;
  padding: 14px 16px; box-shadow: 0 14px 40px rgba(60,50,30,.22);
  transition: transform .45s cubic-bezier(.2,.8,.2,1); opacity: 0;
}
.install-card.show { transform: translate(-50%, 0); opacity: 1; }
.install-icon {
  flex: none; width: 46px; height: 46px; border-radius: 12px;
  display: grid; place-items: center; font-size: 26px;
  color: var(--ink); background: #fff5f0; border: 1px solid #ffe1d8;
}
.install-body { flex: 1; min-width: 0; }
.install-body strong { display: block; font-size: 14.5px; color: var(--text); }
.install-body p { margin: 3px 0 0; font-size: 12.5px; color: var(--muted); line-height: 1.45; }
.install-btn {
  flex: none; border: 0; background: var(--ink); color: #fff; cursor: pointer;
  font-weight: 700; font-size: 14px; padding: 10px 18px; border-radius: 999px;
}
.install-btn:hover { filter: brightness(1.05); }
.install-close {
  position: absolute; top: -10px; right: -10px; width: 26px; height: 26px;
  border-radius: 50%; border: 1px solid var(--line); background: #fff;
  color: var(--muted); cursor: pointer; font-size: 12px; line-height: 1;
  box-shadow: 0 2px 8px rgba(60,50,30,.12);
}
.install-close:hover { color: var(--text); }
.install-card.ios .install-btn { display: none; }
.share-tip { margin: 14px 0 0; color: var(--muted); font-size: 12.5px; line-height: 1.5; }

/* canvas */
.canvas-wrap { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.canvas-square {
  position: relative; width: min(72vh, 100%); aspect-ratio: 1;
  background: #fffdf7; border-radius: 18px; border: 1px solid var(--line);
  box-shadow: var(--shadow); overflow: hidden;
  touch-action: none;
}
.canvas-zoom {
  position: absolute; inset: 0; transform-origin: 0 0;
  will-change: transform;
}
.canvas-zoom canvas {
  position: absolute; inset: 0; width: 100%; height: 100%;
}
#line { pointer-events: none; }
#paint { cursor: crosshair; }

.zoom-hint { display: none; }

.below-canvas { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.nav {
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: 9px 16px; border-radius: 999px; cursor: pointer; font-size: 14px; font-weight: 600;
  box-shadow: 0 2px 8px rgba(60,50,30,.05);
}
.nav:hover { border-color: var(--ink); color: var(--ink); }

/* done note */
.done-note {
  position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 80px);
  background: var(--ink); color: #fff; padding: 14px 26px; border-radius: 999px;
  font-weight: 600; box-shadow: 0 12px 30px rgba(232,74,95,.4);
  opacity: 0; transition: all .5s ease; pointer-events: none;
}
.done-note.show { transform: translate(-50%, 0); opacity: 1; }

/* gallery */
.gallery {
  position: fixed; inset: 0; z-index: 40; background: rgba(40,35,25,.55);
  backdrop-filter: blur(6px); display: none; flex-direction: column; padding: 24px;
}
.gallery.open { display: flex; }
.gallery-head {
  display: flex; justify-content: space-between; align-items: center;
  color: #fff; max-width: 1100px; margin: 0 auto 16px; width: 100%;
}
.gallery-head h2 { margin: 0; font-size: 20px; }
.gallery-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px; overflow-y: auto; max-width: 1100px; margin: 0 auto; width: 100%;
  padding: 4px;
}
.thumb {
  width: 100%; aspect-ratio: 1; background: #fffdf7; border-radius: 12px;
  border: 2px solid transparent; cursor: pointer; transition: all .15s ease;
}
.thumb:hover { border-color: var(--ink); transform: translateY(-3px); }

/* floating tools button + bottom sheet — hidden on desktop */
.tools-fab { display: none; }
.sheet-backdrop { display: none; }
.sheet-header { display: none; }
.zoom-ctl { display: none; }
.fab-hint { display: none; }
.mtips { display: none; }
.mode-toggle { display: none; }
@keyframes fabPop { from { transform: scale(.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* once the session begins, stop the browser's pull-to-refresh / bounce so a
   downward swipe on the tools sheet can never reload the page */
body.app-active { overscroll-behavior: none; }

/* mobile onboarding tips overlay */
.mtips {
  position: fixed; inset: 0; z-index: 120;
  background: rgba(40,35,25,.55); backdrop-filter: blur(6px);
  align-items: center; justify-content: center; padding: 24px;
}
.mtips.show { display: flex; animation: mtipsFade .25s ease; }
@keyframes mtipsFade { from { opacity: 0; } to { opacity: 1; } }
.mtips-inner {
  width: 100%; max-width: 360px; background: var(--card);
  border-radius: 22px; padding: 22px 20px 20px; box-shadow: var(--shadow);
  animation: mtipsRise .3s cubic-bezier(.2,.8,.2,1);
}
@keyframes mtipsRise { from { transform: translateY(16px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.mtips-head {
  margin: 0 0 14px; text-align: center; font-weight: 700;
  font-size: 17px; color: var(--text);
}
.mtip-card {
  display: flex; gap: 13px; align-items: flex-start;
  padding: 13px; margin-bottom: 11px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 16px;
}
.mtip-ico { font-size: 26px; line-height: 1.2; flex: none; }
.mtip-card strong { display: block; font-size: 15px; margin-bottom: 3px; color: var(--text); }
.mtip-card p { margin: 0; font-size: 13.5px; line-height: 1.45; color: var(--muted); }
.mtips-btn {
  width: 100%; margin-top: 4px; border: 0; cursor: pointer;
  background: var(--ink); color: #fff; font-weight: 700; font-size: 15px;
  padding: 14px; border-radius: 14px;
}
.mtips-btn:active { transform: scale(.98); }

/* responsive */
@media (max-width: 860px) {
  /* big canvas on top, tools shown inline below it */
  .stage { display: flex; flex-direction: column; padding: 10px 10px 28px; }
  .canvas-wrap { order: 1; gap: 8px; }
  .panel { order: 2; position: static; top: auto; margin: 16px 0 0; }

  .canvas-square { width: min(96vw, 54dvh); border-radius: 16px; }

  .zoom-hint {
    display: block; margin: 0; color: var(--muted);
    font-size: 12px; letter-spacing: .01em;
  }
  .below-canvas { gap: 8px; }
  .nav { padding: 9px 14px; }

  /* compact top bar so the art gets more room */
  .topbar { padding: 10px 14px; }
  .session-label { display: none; }
  .brand { font-size: 16px; }
  .timer { font-size: 20px; }

  /* bigger tap targets */
  .tools { gap: 10px; }
  .tool { padding: 14px 4px; }
  .palette { grid-template-columns: repeat(8, 1fr); gap: 9px; }
  .actions { gap: 10px; }
  .action { padding: 13px; }

  /* zoom in/out controls — a clear indicator, always available */
  body.app-active .zoom-ctl {
    display: flex; flex-direction: column; gap: 7px;
    position: absolute; top: 10px; right: 10px; z-index: 20;
  }
  .zoom-ctl button {
    width: 42px; height: 42px; border-radius: 12px; line-height: 1;
    border: 1px solid var(--line); color: var(--text);
    background: rgba(255,253,247,.94); backdrop-filter: blur(5px);
    font-size: 24px; font-weight: 700; cursor: pointer;
    box-shadow: 0 4px 14px rgba(60,50,30,.16);
  }
  .zoom-ctl button:active { transform: scale(.9); }
  .zoom-ctl button:disabled { opacity: .35; }

  /* ---- when zoomed in: hide inline tools, use a floating menu + sheet ---- */
  body.zoomed .panel {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto;
    width: 100%; max-height: 84dvh; overflow-y: auto; margin: 0;
    overscroll-behavior: contain;     /* don't chain scroll to the page */
    border-radius: 24px 24px 0 0; padding: 0 20px 28px;
    box-shadow: 0 -14px 50px rgba(60,50,30,.28);
    transform: translateY(110%);
    transition: transform .38s cubic-bezier(.2,.8,.2,1); z-index: 70;
  }
  body.zoomed.sheet-open .panel { transform: translateY(0); }

  /* sticky sheet header: grab pill + clear close button */
  body.zoomed .sheet-header {
    display: flex; align-items: center; gap: 10px;
    position: sticky; top: 0; z-index: 2;
    margin: 0 -20px 6px; padding: 14px 16px 12px;
    background: var(--card); border-radius: 24px 24px 0 0;
    border-bottom: 1px solid var(--line);
    touch-action: none;               /* let JS handle the swipe-to-close */
  }
  .sheet-grip {
    position: absolute; top: 7px; left: 50%; transform: translateX(-50%);
    width: 44px; height: 5px; border-radius: 999px; background: var(--line);
  }
  .sheet-title { font-weight: 700; font-size: 15px; color: var(--text); }
  .sheet-done {
    margin-left: auto; border: 1px solid var(--line); cursor: pointer;
    background: var(--bg); color: var(--text); font-weight: 700;
    font-size: 14px; padding: 9px 16px; border-radius: 999px;
  }
  .sheet-done:active { transform: scale(.94); background: #fff5f0; border-color: var(--ink); color: var(--ink); }

  /* dim backdrop behind the sheet */
  .sheet-backdrop {
    display: block; position: fixed; inset: 0; z-index: 65;
    background: rgba(40,35,25,.45); backdrop-filter: blur(3px);
    opacity: 0; visibility: hidden;
    transition: opacity .38s ease, visibility .38s;
  }
  body.zoomed.sheet-open .sheet-backdrop { opacity: 1; visibility: visible; }

  /* floating menu button — only appears when zoomed in */
  body.zoomed.app-active .tools-fab {
    display: grid; place-items: center;
    position: fixed; right: 16px; bottom: 16px; z-index: 60;
    width: 60px; height: 60px; border-radius: 50%;
    border: 0; background: var(--ink); color: #fff; cursor: pointer;
    font-size: 26px; line-height: 1;
    box-shadow: 0 12px 30px rgba(232,74,95,.5);
    animation: fabPop .35s cubic-bezier(.2,.8,.2,1);
  }
  .tools-fab:active { transform: scale(.92); }
  body.zoomed.app-active .fab-hint {
    display: block; position: fixed; right: 84px; bottom: 33px; z-index: 60;
    background: var(--card); color: var(--text); border: 1px solid var(--line);
    padding: 7px 12px; border-radius: 999px; font-size: 12.5px; font-weight: 600;
    box-shadow: 0 6px 18px rgba(60,50,30,.18); white-space: nowrap;
    animation: fabPop .35s cubic-bezier(.2,.8,.2,1);
  }
  body.zoomed.sheet-open .tools-fab,
  body.zoomed.sheet-open .fab-hint {
    transform: translateY(150%); opacity: 0; pointer-events: none;
  }

  /* single-finger mode toggle — only when zoomed in */
  body.zoomed.app-active .mode-toggle {
    display: flex; gap: 4px;
    position: fixed; bottom: 18px; left: 16px; z-index: 60;
    padding: 4px; border-radius: 14px;
    background: rgba(255,253,247,.94); backdrop-filter: blur(5px);
    border: 1px solid var(--line); box-shadow: 0 4px 14px rgba(60,50,30,.16);
    animation: fabPop .3s cubic-bezier(.2,.8,.2,1);
  }
  .mode-toggle button {
    border: 0; background: transparent; cursor: pointer;
    font-size: 13px; font-weight: 700; color: var(--muted);
    padding: 8px 12px; border-radius: 10px; line-height: 1;
  }
  .mode-toggle button.active {
    background: var(--ink); color: #fff;
    box-shadow: 0 2px 8px rgba(232,74,95,.35);
  }
  body.zoomed.sheet-open .mode-toggle {
    transform: translateY(150%); opacity: 0; pointer-events: none;
    transition: transform .3s ease, opacity .3s ease;
  }
}

/* ---- desktop zoom: +/- controls always available, Draw/Move when zoomed ---- */
@media (min-width: 861px) {
  /* zoom in/out controls, anchored to the top-right of the canvas */
  body.app-active .zoom-ctl {
    display: flex; flex-direction: column; gap: 8px;
    position: absolute; top: 12px; right: 12px; z-index: 20;
  }
  .zoom-ctl button {
    width: 40px; height: 40px; border-radius: 12px; line-height: 1;
    border: 1px solid var(--line); color: var(--text);
    background: rgba(255,253,247,.94); backdrop-filter: blur(5px);
    font-size: 22px; font-weight: 700; cursor: pointer;
    box-shadow: 0 4px 14px rgba(60,50,30,.16);
    transition: border-color .15s ease, color .15s ease, transform .1s ease;
  }
  .zoom-ctl button:hover:not(:disabled) { border-color: var(--ink); color: var(--ink); }
  .zoom-ctl button:active { transform: scale(.9); }
  .zoom-ctl button:disabled { opacity: .35; cursor: default; }

  /* Draw / Move toggle — only useful (and shown) once zoomed in */
  body.zoomed.app-active .mode-toggle {
    display: flex; gap: 4px;
    position: absolute; top: 12px; left: 12px; z-index: 20;
    padding: 4px; border-radius: 14px;
    background: rgba(255,253,247,.94); backdrop-filter: blur(5px);
    border: 1px solid var(--line); box-shadow: 0 4px 14px rgba(60,50,30,.16);
    animation: fabPop .3s cubic-bezier(.2,.8,.2,1);
  }
  .mode-toggle button {
    border: 0; background: transparent; cursor: pointer;
    font-size: 13px; font-weight: 700; color: var(--muted);
    padding: 8px 12px; border-radius: 10px; line-height: 1;
  }
  .mode-toggle button.active {
    background: var(--ink); color: #fff;
    box-shadow: 0 2px 8px rgba(232,74,95,.35);
  }

  /* hint line under the canvas on desktop */
  .zoom-hint {
    display: block; margin: 2px 0 0; color: var(--muted);
    font-size: 12px; text-align: center;
  }

  /* pan-mode cursor affordance */
  body.pan-mode #paint { cursor: grab; }
  body.pan-mode #paint:active { cursor: grabbing; }
}
