:root {
  --gold: #c5a059;
  --gold-dark: #a9812f;
  --gold-deep: #8a6a24;
  --charcoal: #2b2b2b;
  --cream-bg: #faf6ec;
  --cream-bg-2: #f3e9d4;
  --canvas-white: #fffdf9;
  --red: #9c1a22;
  --navy: #0e2b54;
  --green: #2a4b2a;
  --submit-green: #22472e;
  --radius-card: 14px;
  --radius-panel: 22px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: var(--charcoal);
  font-family: 'Poppins', -apple-system, sans-serif;
  user-select: none;
}

/* ---------- Decorative page background ----------
   Built entirely with CSS gradients (no raster image) so it scales cleanly
   to any screen size / aspect ratio / orientation without cropping. */
body {
  background-color: var(--cream-bg);
  background-image:
    /* soft warm glow, upper area */
    radial-gradient(ellipse 120% 60% at 15% 0%, rgba(197, 160, 89, 0.16), transparent 60%),
    radial-gradient(ellipse 90% 55% at 100% 8%, rgba(197, 160, 89, 0.14), transparent 65%),
    linear-gradient(180deg, #fdfbf6 0%, var(--cream-bg) 45%, var(--cream-bg-2) 100%);
  background-attachment: fixed;
}

/* An admin-uploaded background photo replaces the CSS gradient entirely
   (set inline via JS: document.body.style.backgroundImage) - this class just
   switches sizing/position to suit a real photo instead of a gradient. */
body.has-custom-background {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

/* Ribbon / light-streak sweep - a soft diagonal gold band across the lower
   portion of the screen, plus a dot-pattern anchored bottom-left. Both live
   behind #app-root and scale with vw/vh so they hold up in any orientation. */
body::before,
body::after {
  content: "";
  position: fixed;
  pointer-events: none;
  z-index: 0;
}

/* Ribbon sweep */
body::before {
  left: -10%;
  right: -10%;
  bottom: -8%;
  height: 42vmax;
  background:
    linear-gradient(115deg,
      transparent 0%,
      rgba(197, 160, 89, 0.05) 30%,
      rgba(197, 160, 89, 0.22) 46%,
      rgba(255, 255, 255, 0.55) 52%,
      rgba(197, 160, 89, 0.22) 58%,
      rgba(197, 160, 89, 0.05) 74%,
      transparent 100%);
  transform: rotate(-3deg);
  opacity: 0.9;
}

/* Dot-pattern (halftone) bottom-left, fading toward the center */
body::after {
  left: 0;
  bottom: 0;
  width: min(46vw, 46vh, 420px);
  height: min(30vw, 34vh, 300px);
  background-image: radial-gradient(circle, rgba(169, 129, 47, 0.55) 0 2.1px, transparent 2.3px);
  background-size: 15px 15px;
  -webkit-mask-image: radial-gradient(ellipse 100% 100% at 0% 100%, rgba(0,0,0,0.9) 0%, rgba(0,0,0,0.55) 55%, transparent 85%);
  mask-image: radial-gradient(ellipse 100% 100% at 0% 100%, rgba(0,0,0,0.9) 0%, rgba(0,0,0,0.55) 55%, transparent 85%);
}

#app-root {
  position: relative;
  z-index: 1;
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: clamp(14px, 2vh, 30px) clamp(20px, 3vw, 48px);
  gap: clamp(10px, 1.6vh, 20px);
}

/* ---------- Header: emblem logo + Arabic/English wordmark ---------- */
.ghaf-header {
  flex: 0 0 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 2.4vw, 34px);
  padding: clamp(2px, 0.6vh, 10px) 0;
}
/* Logos are capped by BOTH a height clamp and a vw-based max-width, so they
   scale down correctly whenever the viewport is narrow - not just when it's
   short. Without the max-width cap, a narrow-but-tall viewport (e.g. a phone
   held upright) computes a height from vh that's taller than the available
   width can fit, and the logos clip off the edge of the screen. Using
   height:auto/width:auto + max-height/max-width (both set, nothing else)
   lets the browser shrink-to-fit within whichever constraint is tighter,
   while preserving aspect ratio - this is the standard technique for a
   replaced element that must never overflow its container in either axis. */
.ghaf-header__emblem {
  height: auto;
  width: auto;
  max-height: clamp(40px, 8vh, 88px);
  max-width: 30vw;
  object-fit: contain;
  flex: 0 1 auto;
  min-width: 0;
}
.ghaf-header__wordmark {
  height: auto;
  width: auto;
  max-height: clamp(30px, 6.5vh, 68px);
  max-width: 46vw;
  object-fit: contain;
  flex: 0 1 auto;
  min-width: 0;
}
/* No event logo uploaded yet (or reset back to "no logo") - collapse the
   <img> out of layout entirely rather than showing a broken/empty image
   box, so the header simply has nothing there instead of an ugly gap. */
.ghaf-header__emblem.hidden,
.ghaf-header__wordmark.hidden,
.ghaf-sidebar__logo.hidden {
  display: none;
}

/* ---------- Main layout: writing stage + tools sidebar ---------- */
.ghaf-main {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  gap: clamp(14px, 2vw, 28px);
}

/* ---------- Writing stage ---------- */
.stage {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  border-radius: var(--radius-panel);
  border: 1.5px solid rgba(197, 160, 89, 0.55);
  background: var(--canvas-white);
  box-shadow: 0 10px 34px rgba(169, 129, 47, 0.12);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.stage-watermark {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 62%;
  max-width: 46%;
  opacity: 0.16;
  object-fit: contain;
  object-position: left bottom;
  pointer-events: none;
}

.stage-placeholder {
  position: relative;
  z-index: 1;
  color: var(--gold-dark);
  /* Amiri is a proper Arabic serif - Playfair Display has no Arabic glyphs
     and would silently fall back to an ugly system default (plus italic
     renders broken on Arabic script), so it's listed first for this
     Arabic-only placeholder. Playfair/serif stay as the fallback chain. */
  font-family: 'Amiri', 'Playfair Display', serif;
  font-style: normal;
  font-weight: 500;
  font-size: clamp(20px, 4vh, 42px);
  text-align: center;
  padding: 20px;
  opacity: 0.75;
  transition: opacity 0.15s ease;
  pointer-events: none;
}
.stage-placeholder.hidden { opacity: 0; }

#handwriting-canvas {
  touch-action: none;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  position: absolute;
  inset: 0;
  z-index: 1;
}
#handwriting-canvas.erasing {
  cursor: cell;
}

/* ---------- Sidebar: logo, ink colors, tool buttons ---------- */
.ghaf-sidebar {
  flex: 0 0 clamp(150px, 16vw, 210px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(10px, 1.6vh, 18px);
  padding: clamp(6px, 1vh, 14px) 0;
}

.ghaf-sidebar__logo {
  width: clamp(64px, 8vw, 96px);
  height: auto;
  object-fit: contain;
  flex: 0 0 auto;
}

.spacer { flex: 1 1 auto; }

/* Circular icon action buttons: Eraser / Clear (outlined cream) and
   Submit (filled gold, per reference design). */
.tool-row {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(10px, 1.6vh, 18px);
}

.tool-btn {
  width: clamp(48px, 6.4vh, 68px);
  height: clamp(48px, 6.4vh, 68px);
  border-radius: 50%;
  border: 1.5px solid rgba(197, 160, 89, 0.65);
  background: var(--cream-bg);
  color: var(--gold-deep);
  font-size: clamp(16px, 2.4vh, 24px);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.15s ease, background 0.15s ease;
  box-shadow: 0 4px 12px rgba(169, 129, 47, 0.10);
}
.tool-btn:active { transform: scale(0.94); }
.tool-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.tool-btn.active {
  background: var(--gold-dark);
  color: #ffffff;
  box-shadow: 0 0 0 4px rgba(197, 160, 89, 0.25);
}

.tool-btn--submit {
  background: linear-gradient(160deg, var(--gold) 0%, var(--gold-dark) 100%);
  color: #ffffff;
  border-color: transparent;
}
.tool-btn--submit:disabled { opacity: 0.35; }

/* Each button is paired with its label in a single .tool-item wrapper so
   spacing stays correct regardless of clamp() sizing or flex direction
   (row in portrait, column in landscape - see media queries below). */
.tool-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.tool-item__label {
  font-size: clamp(9px, 1.05vh, 11.5px);
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--gold-dark);
  text-transform: uppercase;
  white-space: nowrap;
}

/* ---------- Pen size picker (tap sheet, opened from the sidebar) ---------- */
.pen-size-sheet {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(43, 43, 43, 0.38);
  z-index: 80;
  padding: 20px;
}
.pen-size-sheet.show { display: flex; }

.pen-size-sheet__panel {
  background: var(--cream-bg);
  border-radius: var(--radius-panel);
  border: 1.5px solid rgba(197, 160, 89, 0.55);
  box-shadow: 0 20px 50px rgba(43, 43, 43, 0.28);
  padding: clamp(18px, 2.6vh, 30px) clamp(20px, 3vw, 36px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(14px, 2vh, 22px);
  max-width: 92vw;
}

.pen-size-sheet__title {
  font-weight: 700;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  font-size: clamp(13px, 1.6vh, 16px);
  color: var(--gold-dark);
}

.pen-size-sheet__options {
  display: flex;
  gap: clamp(12px, 2.2vw, 24px);
  flex-wrap: wrap;
  justify-content: center;
}

.pen-size-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: clamp(64px, 9vw, 84px);
  padding: clamp(10px, 1.6vh, 16px) 6px;
  border-radius: 16px;
  border: 1.5px solid rgba(197, 160, 89, 0.4);
  background: var(--canvas-white);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.12s ease;
}
.pen-size-option:active { transform: scale(0.95); }
.pen-size-option.active {
  border-color: var(--gold-dark);
  background: rgba(197, 160, 89, 0.14);
}

/* The dot preview uses the kiosk's actual ink colour (a CSS var set from JS
   at init, mirroring CFG.inkColor) so what the guest picks here visually
   matches what lands on the canvas. */
.pen-size-option__dot {
  border-radius: 50%;
  background: var(--ink-preview-color, var(--gold-dark));
  flex: 0 0 auto;
}
.pen-size-option__label {
  font-size: clamp(10px, 1.3vh, 13px);
  font-weight: 600;
  color: var(--charcoal);
}

/* ---------- Footer ---------- */
.ghaf-footer {
  flex: 0 0 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(6px, 1.5vw, 18px);
  flex-wrap: wrap;
  row-gap: 2px;
}
.ghaf-footer .footer-line {
  flex: 1 1 24px;
  min-width: 10px;
  max-width: 260px;
  height: 1px;
  background: rgba(197, 160, 89, 0.55);
}
/* Font-size previously scaled only with vh, so on a narrow-but-tall
   viewport (e.g. a phone held upright) the text stayed full-width while the
   container got narrower, overflowing past the screen edge (clipped by the
   page's overflow:hidden with zero warning). Adding a vw term makes it
   shrink with width too; flex-wrap above plus a min() escape hatch below
   are the last-resort safety net if it's still too tight to fit on one line
   at the most extreme aspect ratios. */
.ghaf-footer .footer-text {
  font-size: clamp(8px, min(1.1vh, 2.6vw), 13px);
  font-weight: 600;
  letter-spacing: 1.5px;
  color: var(--gold-dark);
  text-transform: uppercase;
  white-space: nowrap;
}
.ghaf-footer .footer-icon {
  color: var(--gold-dark);
  font-size: clamp(12px, min(1.7vh, 3.6vw), 20px);
  flex: 0 0 auto;
}

/* ---------- Success overlay ---------- */
.success-overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(250, 246, 236, 0.97);
  z-index: 50;
  flex-direction: column;
  gap: 18px;
  text-align: center;
  padding: 40px;
}
.success-overlay.show { display: flex; }
.success-overlay i {
  font-size: clamp(48px, 8vh, 90px);
  color: var(--gold-dark);
  animation: leafPulse 1.6s ease-in-out infinite;
}
.success-overlay h2 {
  /* Amiri covers the Arabic line properly (Playfair has no Arabic glyphs);
     both lines share this block since the message is bilingual (Arabic +
     English separated by a <br>). */
  font-family: 'Amiri', 'Playfair Display', serif;
  font-size: clamp(18px, 3vh, 36px);
  line-height: 1.5;
  color: var(--submit-green);
  margin: 0;
  max-width: 85%;
}
@keyframes leafPulse {
  0%, 100% { transform: scale(1); opacity: 0.9; }
  50% { transform: scale(1.12); opacity: 1; }
}

/* ---------- VIP mode: hidden trigger + badge (no PIN) ---------- */
/* The footer tree icon doubles as the hidden VIP trigger (long-press) - no
   visual change to it at rest, so guests have zero reason to suspect it. */
.ghaf-footer .footer-icon#vip-trigger {
  cursor: default;
}

.vip-badge {
  position: fixed;
  top: clamp(10px, 2vh, 20px);
  right: clamp(12px, 2vw, 24px);
  display: none;
  align-items: center;
  gap: 7px;
  background: linear-gradient(160deg, #d4af37 0%, #9c1a22 120%);
  color: #fff;
  font-weight: 700;
  font-size: clamp(12px, 1.6vh, 16px);
  letter-spacing: 1px;
  padding: 8px 16px;
  border-radius: 999px;
  box-shadow: 0 6px 18px rgba(156, 26, 34, 0.35);
  z-index: 70;
  animation: vipPulse 1.8s ease-in-out infinite;
}
.vip-badge.show { display: flex; }
.vip-badge i { font-size: 0.95em; }
@keyframes vipPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

/* ---------- Toast (errors) ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #fdecea;
  border: 1px solid var(--red);
  color: #7a1a1a;
  padding: 14px 26px;
  border-radius: 12px;
  font-size: 15px;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 60;
  pointer-events: none;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* =========================================================================
   Responsive: portrait orientation (tall tablet) — stack sidebar under the
   stage as a horizontal control bar instead of a vertical side column.
   ========================================================================= */
@media (orientation: portrait) {
  #app-root {
    padding: clamp(10px, 1.6vh, 20px) clamp(14px, 3vw, 28px);
    gap: clamp(8px, 1.4vh, 14px);
  }
  .ghaf-header {
    gap: clamp(10px, 3vw, 22px);
  }
  .ghaf-header__emblem { height: clamp(40px, 6.5vh, 64px); }
  .ghaf-header__wordmark { height: clamp(28px, 5vh, 46px); }

  .ghaf-main {
    flex-direction: column;
    gap: clamp(10px, 1.4vh, 16px);
  }

  .stage { flex: 1 1 auto; }
  .stage-watermark { height: 46%; max-width: 40%; }
  .stage-placeholder { font-size: clamp(18px, 3.4vh, 32px); }

  .ghaf-sidebar {
    flex: 0 0 auto;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    gap: clamp(16px, 4vw, 32px);
    width: 100%;
    padding: clamp(4px, 0.8vh, 10px) 0;
  }
  .ghaf-sidebar__logo { width: clamp(44px, 8vh, 60px); }
  .spacer { display: none; }
  .tool-row {
    flex-direction: row;
    gap: clamp(14px, 3vw, 26px);
  }
}

/* ---------- Landscape fine-tuning for short (wide, low-height) viewports ----------
   The sidebar's button sizes/gaps are built on vh-based clamp() floors tuned
   for a normal kiosk aspect ratio. On a short landscape viewport (a phone
   held sideways, or a small embedded webview) those floors don't shrink
   enough: 4 buttons (pen size / eraser / clear / submit) at their 48px
   minimum plus labels plus the sidebar's own logo can add up to MORE
   vertical space than the viewport actually has, which silently pushes the
   Submit button below the fold with zero warning. These two tiers
   progressively shrink the sidebar's floors (and, at the most extreme
   heights, drop its purely-decorative logo - the same emblem is already
   shown in the header) so every control stays fully visible and reachable
   no matter how short the viewport gets. */
@media (orientation: landscape) and (max-height: 480px) {
  .ghaf-header__emblem { max-height: clamp(30px, 9vh, 50px); }
  .ghaf-header__wordmark { max-height: clamp(22px, 7vh, 36px); }
  .ghaf-sidebar {
    flex: 0 0 clamp(100px, 12vw, 160px);
    gap: clamp(3px, 1vh, 12px);
    padding: clamp(2px, 0.6vh, 10px) 0;
  }
  .ghaf-sidebar__logo { width: clamp(32px, 5.5vh, 56px); }
  .tool-row { gap: clamp(4px, 1vh, 13px); }
  .tool-btn {
    width: clamp(30px, 5.6vh, 52px);
    height: clamp(30px, 5.6vh, 52px);
    font-size: clamp(12px, 2.2vh, 18px);
  }
  .tool-item { gap: 3px; }
  .tool-item__label { font-size: clamp(6.5px, 1.15vh, 10px); }
}

/* Extreme case: phone-sized landscape heights. Even the shrunk floors above
   can't fit a logo + 4 full buttons+labels in ~300px of height, so the
   sidebar's logo (purely decorative - identical emblem already sits in the
   header) is dropped entirely here to guarantee the Submit button is never
   clipped. Verified by automated test at 320px viewport height. */
@media (orientation: landscape) and (max-height: 360px) {
  .ghaf-sidebar { gap: 2px; padding: 2px 0; }
  .ghaf-sidebar__logo { display: none; }
  .spacer { display: none; }
  .tool-row {
    flex: 1 1 auto;
    justify-content: center;
    gap: clamp(3px, 0.8vh, 8px);
  }
  .tool-btn {
    width: clamp(26px, 5.8vh, 40px);
    height: clamp(26px, 5.8vh, 40px);
    font-size: clamp(10px, 2.3vh, 15px);
  }
  .tool-item { gap: 2px; }
  .tool-item__label { font-size: clamp(5.5px, 1vh, 8px); }
}
