@font-face {
  font-family: "Noto Nastaliq Urdu";
  src: url("assets/NotoNastaliqUrdu.woff2") format("woff2");
  font-weight: 400 700;
  font-style: normal;
  font-display: block;
}

/* ------------------------------------------------------------------
   Tokens

   The artwork is maximal: gold, teal, magenta, cream. So the interface
   deliberately is not. One warm near-neutral ground, no gradients, no
   brand colour in the chrome — the artwork stays the only saturated
   thing on screen.

   The one exception is --cream, used for the writing surface: that is
   the same paper the text lands on, so the writer shows the real ink
   and script rather than an approximation of them.
------------------------------------------------------------------- */
:root {
  --bg:      #111110;
  --surface: #1a1917;
  --raised:  #232120;
  --paper:   #f4f1ea;
  --cream:   #f9f0d6;
  --cream-ink: #1b1b1b;

  --ink:     #ece8e0;
  --muted:   #a09a91;

  --line: rgba(244, 241, 234, 0.10);

  --amber: #e0a63f;

  --s1: 4px;  --s2: 8px;  --s3: 12px;
  --s4: 16px; --s5: 20px; --s6: 24px;

  --r-sm: 10px;
  --r-md: 14px;
  --r-lg: 18px;
  --r-full: 999px;

  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --urdu: "Noto Nastaliq Urdu", "Jameel Noori Nastaleeq", serif;
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.app {
  /* No max-width: any cap letterboxes the app on a landscape phone or in
     "desktop site" mode, where the viewport is wider than any phone.

     --app-h is only ever set while the on-screen keyboard is up. The rest of
     the time `dvh` governs, which tracks the browser's own URL bar natively. */
  height: var(--app-h, 100vh);
  height: var(--app-h, 100dvh);
  padding: max(var(--s2), env(safe-area-inset-top)) var(--s4) var(--s2);
  display: flex;
  flex-direction: column;
}

/* ------------------------------------------------------------------
   Phones stay full width, in portrait, in landscape, and in "desktop
   site" mode (which reports ~980px). Only genuinely large screens get a
   cap, so the text keeps a readable measure.
------------------------------------------------------------------- */
@media (min-width: 1100px) {
  .app {
    max-width: 900px;
    margin-inline: auto;
  }
}

/* ------------------------------------------------------------------
   Views
------------------------------------------------------------------- */
.view { display: none; }

.app[data-view="write"]   .view-write,
.app[data-view="preview"] .view-preview {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
  flex-direction: column;
  gap: var(--s3);
}

/* ------------------------------------------------------------------
   Write — the surface is the artwork's own paper, so the text you type
   is already in the ink and script it will land in
------------------------------------------------------------------- */
.sheet {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  border-radius: var(--r-lg);
  background: var(--cream);
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.4),
    0 2px 4px rgba(0, 0, 0, 0.35),
    0 12px 32px -10px rgba(0, 0, 0, 0.6),
    0 32px 64px -28px rgba(0, 0, 0, 0.75);
  transition: box-shadow 180ms var(--ease);
}

.sheet:focus-within {
  box-shadow:
    0 0 0 3px rgba(224, 166, 63, 0.45),
    0 2px 4px rgba(0, 0, 0, 0.35),
    0 12px 32px -10px rgba(0, 0, 0, 0.6),
    0 32px 64px -28px rgba(0, 0, 0, 0.75);
}

#text {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  resize: none;
  border: none;
  outline: none;
  background: transparent;
  padding: var(--s5);
  color: var(--cream-ink);
  font-family: var(--urdu);
  font-size: 1.25rem;
  line-height: 2.3;
  caret-color: var(--cream-ink);
}

#text::placeholder { color: rgba(27, 27, 27, 0.38); }

/* the sheet's ring is the focus indicator here */
#text:focus-visible { outline: none; }

/* ------------------------------------------------------------------
   Preview
------------------------------------------------------------------- */
.close {
  flex: 0 0 auto;
  align-self: flex-start;
  width: 44px;
  height: 44px;
  margin-inline-start: calc(var(--s2) * -1);
  display: grid;
  place-items: center;
  border: none;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color 140ms var(--ease), transform 120ms var(--ease);
}

.close:active { transform: scale(0.92); }

.stage {
  /* basis 0, not auto: the artwork is measured FROM the stage, so the stage
     must never size itself from the artwork */
  flex: 1 1 0%;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.frame {
  position: relative;
  border-radius: var(--r-lg);
  overflow: hidden;
  background: var(--surface);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
  box-shadow:
    0 0 0 1px var(--line),
    0 2px 4px rgba(0, 0, 0, 0.45),
    0 10px 28px -8px rgba(0, 0, 0, 0.6),
    0 32px 64px -24px rgba(0, 0, 0, 0.75);
}

.app[data-view="preview"] .frame { animation: settle 380ms var(--ease) both; }

.frame.dragging { cursor: grabbing; }
.frame[hidden] { display: none; }

@keyframes settle {
  from { opacity: 0; transform: scale(0.97); }
  to   { opacity: 1; transform: scale(1); }
}

#bg {
  display: block;
  width: 100%;
  height: 100%;
}

/* positioned by script; only its x/y change while dragging */
#fg {
  position: absolute;
  left: 0;
  top: 0;
  display: block;
}

/* affordance: this object can be moved. Never drawn into the download. */
.handle {
  position: absolute;
  left: 0;
  top: 0;
  border: 1.5px dashed rgba(224, 166, 63, 0.9);
  border-radius: 10px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 180ms var(--ease);
}

.app[data-view="preview"] .handle { opacity: 0.4; }
.frame.dragging .handle { opacity: 1; }
.handle[hidden] { display: none; }

/* ------------------------------------------------------------------
   Loading + missing image
------------------------------------------------------------------- */
.skeleton {
  width: min(100%, 360px);
  aspect-ratio: 1;
  border-radius: var(--r-lg);
  background: var(--surface);
  animation: breathe 1500ms ease-in-out infinite;
}

.skeleton[hidden] { display: none; }

@keyframes breathe {
  0%, 100% { opacity: 0.45; }
  50%      { opacity: 0.85; }
}

.fallback {
  width: min(100%, 360px);
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--s5);
  text-align: center;
  padding: var(--s5);
  border: 1px dashed var(--line);
  border-radius: var(--r-lg);
}

.fallback[hidden] { display: none; }

.fallback-text {
  margin: 0;
  font-size: 0.875rem;
  line-height: 1.9;
  color: var(--muted);
}

.fallback-text code {
  display: inline-block;
  margin-top: var(--s2);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.75rem;
  padding: 4px 9px;
  border-radius: var(--r-sm);
  background: var(--raised);
  color: var(--muted);
}

/* ------------------------------------------------------------------
   Toolbar (on the image, not the writer)
------------------------------------------------------------------- */
.toolbar {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: var(--s3);
}

.sizebar {
  display: flex;
  align-items: center;
  gap: var(--s2);
}

.step {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  cursor: pointer;
  transition: color 140ms var(--ease), border-color 140ms var(--ease),
    transform 120ms var(--ease);
}

.step:active { transform: scale(0.93); }

#size {
  flex: 1 1 auto;
  appearance: none;
  height: 44px;
  background: transparent;
  cursor: pointer;
}

#size::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: var(--r-full);
  background: var(--raised);
}

#size::-webkit-slider-thumb {
  appearance: none;
  width: 26px;
  height: 26px;
  margin-top: -11px;
  border-radius: 50%;
  background: var(--paper);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.6), 0 4px 12px -4px rgba(0, 0, 0, 0.8);
}

#size::-moz-range-track {
  height: 4px;
  border-radius: var(--r-full);
  background: var(--raised);
}

#size::-moz-range-thumb {
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 50%;
  background: var(--paper);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
}

/* ------------------------------------------------------------------
   Landscape / short viewports. Stacking the controls under the artwork
   leaves a square poster only ~200px tall, so put them beside it and
   give the poster the full height instead.
------------------------------------------------------------------- */
@media (orientation: landscape) and (max-height: 560px) {
  .app[data-view="preview"] .view-preview {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(200px, 38%);
    grid-template-rows: auto minmax(0, 1fr);
    gap: var(--s2) var(--s4);
  }

  .view-preview .close {
    grid-column: 2;
    grid-row: 1;
    justify-self: end;
    align-self: center;
    margin-inline-start: 0;
  }

  .view-preview .stage {
    grid-column: 1;
    grid-row: 1 / -1;
  }

  .view-preview .toolbar {
    grid-column: 2;
    grid-row: 2;
    align-self: center;
  }
}

/* ------------------------------------------------------------------
   Buttons
------------------------------------------------------------------- */
.btn {
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  padding: 14px var(--s5);
  min-height: 48px;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 120ms var(--ease), background 160ms var(--ease),
    border-color 160ms var(--ease);
}

.btn:active { transform: scale(0.975); }
.btn:disabled { opacity: 0.4; pointer-events: none; }

.btn-paper {
  flex: 0 0 auto;
  background: var(--paper);
  color: #14130f;
}

@media (hover: hover) {
  .step:hover, .close:hover { color: var(--ink); }
  .step:hover { border-color: rgba(244, 241, 234, 0.22); }
  .btn-paper:hover { background: #ffffff; }
}

/* ------------------------------------------------------------------
   Focus — visible, offset, never removed
------------------------------------------------------------------- */
:focus-visible {
  outline: 2px solid var(--amber);
  outline-offset: 3px;
  border-radius: var(--r-sm);
}

/* ------------------------------------------------------------------
   Toast
------------------------------------------------------------------- */
.toast {
  position: fixed;
  inset-inline: 0;
  bottom: calc(env(safe-area-inset-bottom) + var(--s6));
  margin: 0 auto;
  width: max-content;
  max-width: calc(100% - var(--s6) * 2);
  padding: var(--s3) var(--s5);
  border-radius: var(--r-md);
  background: var(--raised);
  border: 1px solid var(--line);
  color: var(--ink);
  font-size: 0.8125rem;
  text-align: center;
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 200ms var(--ease), transform 200ms var(--ease);
  z-index: 20;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
