:root {
  --bg:        #0d1013;
  --chrome:    #16191e;
  --chrome-2:  #1e232a;
  --line:      #2b313a;
  --text:      #eef1f5;
  --muted:     #9aa4b2;
  --accent:    #3b82f6;
  --accent-2:  #2563eb;
  --success:   #22c55e;
  --danger:    #ef4444;
  --radius:    12px;
  --tap:       46px;               /* minimum touch target */
  --topbar-h:  calc(52px + env(safe-area-inset-top));
}

* { box-sizing: border-box; }

/* The [hidden] attribute must always win, even over author display rules
   (.busy, .strength-row, .crop-bar set display:flex). */
[hidden] { display: none !important; }

html { height: 100%; scroll-behavior: smooth; }

html, body { margin: 0; }

body {
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;      /* kill the 300ms tap delay */
}

/* The editor is a full-screen "hero"; the info/help content flows below it. */
.app {
  position: relative;              /* positioning context for the tutorial finger */
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overscroll-behavior: none;       /* no pull-to-refresh bounce while editing */
}

button { font-family: inherit; cursor: pointer; }

/* ===================== TOP BAR ===================== */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  height: var(--topbar-h);
  padding: env(safe-area-inset-top) 12px 0;
  background: var(--chrome);
  border-bottom: 1px solid var(--line);
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand-name {
  font-weight: 700;
  font-size: 17px;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 38px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--chrome-2);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}
.icon-btn svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.icon-btn:disabled { opacity: 0.4; }
.icon-btn:active:not(:disabled) { transform: translateY(1px); }

/* ===================== STAGE ===================== */
.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: #0d1013;
}

/* Intro overlay — sits on top of the pre-loaded sample grid until dismissed.
   Uses margin:auto on the inner block so it centers when it fits and scrolls
   (never clips) when the enlarged copy is taller than the stage. */
.empty-state {
  position: absolute;
  inset: 0;
  z-index: 30;                     /* above the tutorial finger (25) */
  display: flex;
  overflow-y: auto;
  padding: 20px 22px;
  cursor: pointer;                 /* tap anywhere (but the button) to start editing */
}
.empty-inner {
  margin: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  max-width: 22em;
  padding: 16px 12px;
}
/* Per-line background: a rounded translucent pill hugs each wrapped line of text. */
.empty-title span, .empty-sub span, .empty-hint span {
  background: rgba(10, 12, 15, 0.66);
  padding: 0.05em 0.34em;
  border-radius: 9px;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}
/* Light halo on the text too, so it holds up over the pill edges. */
.empty-title, .empty-sub, .empty-hint {
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9);
}
.empty-title { margin: 0 0 14px; font-size: clamp(22px, 8vw, 44px); font-weight: 800; line-height: 1.3; white-space: nowrap; }
.empty-sub { margin: 0 0 20px; max-width: 15em; font-size: 22px; line-height: 1.5; }
.cta {
  min-height: 62px;
  padding: 0 30px;
  border: none;
  border-radius: 16px;
  background: var(--accent);
  color: #fff;
  font-size: 32px;
  font-weight: 700;
  white-space: nowrap;
  box-shadow: 0 6px 20px rgba(59, 130, 246, 0.35);
}
.cta:active { transform: translateY(1px); background: var(--accent-2); }
.empty-hint { margin: 16px 0 0; max-width: 15em; font-size: 22px; line-height: 1.5; }

/* Canvas */
.canvas-wrap {
  margin: auto;
  padding: 12px;
}
.canvas-holder {
  position: relative;
  line-height: 0;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}
#mainCanvas, #overlayCanvas {
  display: block;
  /* JS sets the on-screen width/height so the image fits the viewport. */
  max-width: 100%;
}
#overlayCanvas {
  position: absolute;
  left: 0;
  top: 0;
  touch-action: none;              /* we handle the gesture ourselves */
}

/* Live-tutorial finger overlay + tap ripple */
.demo-hand {
  position: absolute;
  z-index: 25;                            /* above the toolbar (20), below the hero copy (30) */
  pointer-events: none;
  transform-origin: 6% 6%;                /* pivot at the fingertip */
  transition: opacity 0.4s ease, transform 0.15s ease;
  /* No CSS filter or will-change: both promoted the finger to a compositor layer
     that ignored z-index on mobile. The shadow is baked into hand.png instead. */
}
.demo-hand.press { transform: scale(0.9); }
.demo-ripple {
  position: absolute;
  z-index: 26;                            /* just above the finger, still behind the hero copy */
  width: 46px; height: 46px;
  margin: -23px 0 0 -23px;                /* center on its left/top point */
  border-radius: 50%;
  border: 3px solid rgba(59, 130, 246, 0.9);
  background: rgba(59, 130, 246, 0.22);
  pointer-events: none;
  opacity: 0;
}
.demo-ripple.go { animation: demo-ripple 0.55s ease-out; }
@keyframes demo-ripple {
  from { opacity: 0.9; transform: scale(0.3); }
  to   { opacity: 0;   transform: scale(1.5); }
}

/* Busy overlay */
.busy {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: rgba(13, 16, 19, 0.72);
  backdrop-filter: blur(2px);
  z-index: 50;                     /* loading overlay sits above the finger + hero copy */
}
.busy-spinner {
  width: 46px; height: 46px;
  border: 4px solid rgba(255, 255, 255, 0.18);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.busy-label { color: var(--text); font-weight: 600; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===================== TOOLBAR ===================== */
.toolbar {
  flex: 0 0 auto;
  background: var(--chrome);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 20;                     /* below the finger (25); no `position` (avoids a
                                      Chrome-mobile paint bug with the scrollable tools) */
}

.strength-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px 2px;
  border-bottom: 1px solid var(--line);
}
.strength-row label { font-size: 13px; color: var(--muted); font-weight: 600; min-width: 58px; }
.strength-row output { font-size: 14px; font-weight: 700; min-width: 26px; text-align: right; font-variant-numeric: tabular-nums; }
.strength-row input[type="range"] { flex: 1; accent-color: var(--accent); height: var(--tap); }

.crop-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
}
.crop-size { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }

.btn {
  min-height: 42px;
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--chrome-2);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
}
.btn.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn:active { transform: translateY(1px); }

/* Tool row — horizontally scrollable on narrow screens */
.tools {
  display: flex;
  align-items: stretch;
  gap: 6px;
  padding: 10px 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.tools::-webkit-scrollbar { display: none; }

.tool {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 60px;
  min-height: var(--tap);
  padding: 6px 8px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}
.tool svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tool[aria-pressed="true"] {
  background: var(--chrome-2);
  border-color: var(--accent);
  color: var(--text);
}
.tool[aria-pressed="true"] svg { stroke: var(--accent); }
.tool:active { background: var(--chrome-2); }
.tool.is-active { background: var(--chrome-2); border-color: var(--accent); color: var(--text); }

.tool-sep {
  flex: 0 0 auto;
  width: 1px;
  align-self: center;
  height: 30px;
  background: var(--line);
  margin: 0 3px;
}

.hint {
  margin: 0;
  padding: 0 16px 6px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}
.learn-link {
  display: block;
  padding: 0 16px 10px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  text-decoration: none;
}
.learn-link:active { opacity: 0.7; }

/* Collapse the top actions to icons on narrow screens so the brand + Open + Save fit. */
@media (max-width: 459px) {
  #btnUndo .icon-btn-label { display: none; }
}
@media (max-width: 399px) {
  #btnOpenTop .icon-btn-label { display: none; }
}

/* ===================== LARGER SCREENS ===================== */
@media (min-width: 720px) {
  .brand-name { font-size: 19px; }
  .tools { justify-content: center; }
  .tool { min-width: 74px; font-size: 12px; }
  .hint { font-size: 13px; }
}

/* ===================== TOAST ===================== */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  max-width: min(90vw, 420px);
  padding: 11px 18px;
  background: rgba(30, 35, 42, 0.96);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 14px;
  font-weight: 600;
  text-align: center;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
  z-index: 40;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  .busy-spinner { animation-duration: 2s; }
  .cta:active, .btn:active, .icon-btn:active, .tool:active { transform: none; }
  .toast { transition: opacity 0.22s ease; }
  html { scroll-behavior: auto; }
}

/* ===================== INFO / HELP (original site copy) ===================== */
.info {
  background: #f6f8fb;
  color: #1b2530;
  padding: 44px 20px calc(56px + env(safe-area-inset-bottom));
  user-select: text;
  -webkit-user-select: text;
}
.info-inner { max-width: 720px; margin: 0 auto; }

.info a { color: #2563eb; }

.info-head { text-align: center; padding-bottom: 8px; }
.info-head h1 { font-size: 27px; line-height: 1.2; margin: 0 0 10px; letter-spacing: -0.01em; }
.info-head .with-fd { color: #2563eb; white-space: nowrap; }
.info .lead { font-size: 17px; color: #47535f; margin: 0 auto 14px; max-width: 34em; }
.video-link {
  display: inline-block;
  margin-top: 4px;
  padding: 10px 18px;
  border-radius: 10px;
  background: #2563eb;
  color: #fff !important;
  font-weight: 700;
  text-decoration: none;
}

.info h2 {
  font-size: 22px;
  margin: 40px 0 12px;
  padding-top: 20px;
  border-top: 1px solid #dde3ea;
}
.info h3 { font-size: 18px; margin: 26px 0 8px; }
.info p { font-size: 16px; line-height: 1.6; margin: 0 0 14px; color: #2b3742; }
.info ul, .info ol { font-size: 16px; line-height: 1.6; color: #2b3742; padding-left: 22px; margin: 0 0 14px; }
.info li { margin-bottom: 6px; }

.info img { max-width: 100%; height: auto; border-radius: 8px; }
.ui-shot {
  display: block;
  margin: 16px auto;
  max-height: 320px;
  border: 1px solid #e1e6ec;
  box-shadow: 0 6px 20px rgba(20, 30, 45, 0.12);
}
.banner { display: block; width: 100%; margin: 14px 0; border: 1px solid #e1e6ec; }

.gallery {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin: 16px 0;
}
.gallery img {
  flex: 1 1 30%;
  min-width: 140px;
  object-fit: cover;
  max-height: 180px;
  border: 1px solid #e1e6ec;
  box-shadow: 0 4px 14px rgba(20, 30, 45, 0.1);
}

.copyright { text-align: right; color: #8a95a1; font-size: 13px; margin-top: 36px; }

@media (prefers-color-scheme: dark) {
  .info { background: #12161b; color: #d8dee6; }
  .info p, .info ul, .info ol { color: #c2cad4; }
  .info .lead { color: #97a2ae; }
  .info h2 { border-top-color: #262d36; }
  .info a { color: #6ea8fe; }
  .ui-shot, .gallery img, .banner { border-color: #2a323c; box-shadow: 0 6px 20px rgba(0,0,0,0.4); }
}
