/* ==========================================================================
   DuneForge — base + comic primitives
   Reset, typography, and the comic grammar every page shares:
   .panel  .caption  .balloon  .sfx  .btn  .halftone  .lang-switch
   Class names and their box behaviour are a contract used by several modules.
   ========================================================================== */

/* ---- Reset --------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
  scroll-padding-top: 88px;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-text);
  font-size: var(--fs-base);
  line-height: var(--lh-body);
  color: var(--ink);
  background: var(--sky-noon-top);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  overflow-x: clip;
}

img, svg, video, canvas, picture { display: block; max-width: 100%; }
img { height: auto; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; text-underline-offset: 0.18em; text-decoration-thickness: 2px; }
a:hover { text-decoration-thickness: 3px; }

h1, h2, h3, h4, h5, h6 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 400; /* Dela Gothic One ships one heavy weight; never fake-bold it */
  line-height: var(--lh-snug);
  text-wrap: balance;
}
h1 { font-size: var(--fs-h1); }
h2 { font-size: var(--fs-h2); }
h3 { font-size: var(--fs-h3); }

p { margin: 0; text-wrap: pretty; }
p + p { margin-top: 0.85em; }
strong, b { font-weight: 700; }

ul[role="list"], ol[role="list"] { list-style: none; margin: 0; padding: 0; }

::selection { background: var(--molten); color: var(--ink); }

/* Focus: a two-tone ring, an ink line between two paper rims (paper 0-2px, ink 2-5px,
   paper 5-7px). The ink passes on sky, sand, paper and molten; the paper rims pass on
   dusk and night, so the default works on every ground of the day without overrides.
   An area may still recolour the line (outline-color: var(--molten) on night).
   The rims are a zero-specificity box-shadow: a component's own box-shadow wins (buttons
   compose both below). A component that draws its own indicator sets outline: none and
   box-shadow: none. Script-only focus targets (tabindex="-1") get the line only. */
:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 2px;
  border-radius: 2px;
}
:where(:focus-visible:not([tabindex="-1"])) { box-shadow: 0 0 0 7px var(--paper); }

/* Arabic script must never be letter-spaced (it breaks the joins). */
:lang(ar) { letter-spacing: 0 !important; }
:lang(ar) * { letter-spacing: 0 !important; }

/* ---- Utilities ----------------------------------------------------------- */
.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute;
  inset-inline-start: var(--s-4);
  inset-block-start: -200px;
  z-index: 100;
  padding: var(--s-3) var(--s-4);
  background: var(--molten);
  color: var(--ink);
  border: var(--bw) solid var(--ink);
  font-weight: 700;
  text-decoration: none;
}
.skip-link:focus { inset-block-start: var(--s-4); }

.wrap {
  width: min(100% - 2 * var(--page-pad), var(--container));
  margin-inline: auto;
}

.measure { max-width: var(--measure); }

/* ---- Panel: the comic frame --------------------------------------------- */
.panel {
  --panel-bg: var(--paper);
  --panel-fg: var(--ink);
  --panel-line: var(--ink);
  position: relative;
  isolation: isolate;
  overflow: hidden;
  background: var(--panel-bg);
  color: var(--panel-fg);
  border: var(--bw) solid var(--panel-line);
  border-radius: var(--r-panel);
  padding: clamp(20px, 3vw, 40px);
}
.panel--night {
  --panel-bg: var(--night-2);
  --panel-fg: var(--paper);
  --panel-line: var(--night-line);
}
.panel--molten { --panel-bg: var(--molten); }
.panel--dune   { --panel-bg: var(--dune); }
.panel--bleed  { padding: 0; }

/* A row of panels separated by gutters */
.strip {
  display: grid;
  gap: var(--gutter);
}

/* ---- Caption: narration box --------------------------------------------- */
.caption {
  display: inline-block;
  max-width: 40ch;
  padding: 0.38em 0.8em 0.42em;
  background: var(--molten);
  color: var(--ink);
  border: var(--bw-thin) solid var(--ink);
  font-family: var(--font-letter);
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.3;
}
/* Pinned into a panel's top-start corner, overlapping the frame like printed comics */
.caption--corner {
  position: absolute;
  z-index: 2;
  inset-block-start: calc(var(--bw) * -1);
  inset-inline-start: calc(var(--bw) * -1);
}
.caption--paper { background: var(--paper); }

/* ---- Balloon: speech ----------------------------------------------------- */
.balloon {
  --balloon-bg: var(--paper-white);
  --tail-pos: 22%;
  position: relative;
  display: inline-block;
  max-width: 34ch;
  padding: 0.7em 1.1em 0.75em;
  background: var(--balloon-bg);
  color: var(--ink);
  border: var(--bw-thin) solid var(--ink);
  border-radius: 1.5em;
  font-family: var(--font-letter);
  font-size: 1.0625rem;
  font-weight: 500;
  line-height: 1.38;
}
/* Tail: an ink triangle with a fill triangle on top */
.balloon::before,
.balloon::after {
  content: "";
  position: absolute;
  inset-inline-start: var(--tail-pos);
  width: 0; height: 0;
  border-style: solid;
}
.balloon::before {
  inset-block-start: 100%;
  border-width: 18px 16px 0 0;
  border-color: var(--ink) transparent transparent transparent;
}
.balloon::after {
  inset-block-start: calc(100% - 1px);
  margin-inline-start: 2.5px;
  border-width: 13px 11.5px 0 0;
  border-color: var(--balloon-bg) transparent transparent transparent;
}
[dir="rtl"] .balloon::before { border-width: 18px 0 0 16px; border-color: var(--ink) transparent transparent transparent; }
[dir="rtl"] .balloon::after  { border-width: 13px 0 0 11.5px; border-color: var(--balloon-bg) transparent transparent transparent; }

.balloon--tail-end { --tail-pos: 70%; }
.balloon--no-tail::before, .balloon--no-tail::after { display: none; }
.balloon--whisper { border-style: dashed; }

/* ---- SFX lettering -------------------------------------------------------- */
.sfx {
  display: inline-block;
  font-family: var(--font-display);
  font-weight: 400;
  line-height: 0.9;
  color: var(--molten);
  -webkit-text-stroke: 3px var(--ink);
  paint-order: stroke fill;
  transform: rotate(-6deg);
  user-select: none;
}

/* ---- Buttons -------------------------------------------------------------- */
.btn {
  --btn-bg: var(--molten);
  --btn-fg: var(--ink);
  --btn-line: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.55em;
  min-height: 48px;
  padding: 0.62em 1.25em 0.68em;
  background: var(--btn-bg);
  color: var(--btn-fg);
  border: var(--bw) solid var(--btn-line);
  border-radius: var(--r-btn);
  box-shadow: 0 4px 0 var(--btn-line);
  font-family: var(--font-letter);
  font-size: 1.0625rem;
  font-weight: 700;
  line-height: 1.1;
  text-align: center;
  text-decoration: none;
  /* One line whenever the label fits. If it cannot (200% text, a very narrow column),
     the label wraps into balanced lines inside the button instead of being clipped. */
  max-width: 100%;
  white-space: normal;
  overflow-wrap: break-word;
  text-wrap: balance;
  transition: transform var(--dur-1) var(--ease-out), box-shadow var(--dur-1) var(--ease-out), background-color var(--dur-2) var(--ease-out);
}
.btn:hover  { transform: translateY(-2px); box-shadow: 0 6px 0 var(--btn-line); }
.btn:active { transform: translateY(3px);  box-shadow: 0 1px 0 var(--btn-line); }
.btn svg { width: 1.15em; height: 1.15em; flex: none; }

.btn--ember { --btn-bg: var(--ember); }
.btn--paper { --btn-bg: var(--paper); }
.btn--night { --btn-bg: var(--night-2); --btn-fg: var(--paper); --btn-line: var(--paper); }
.btn--small { min-height: 40px; padding: 0.45em 0.95em 0.5em; font-size: 0.9375rem; box-shadow: 0 3px 0 var(--btn-line); }

/* Focused buttons keep their key-press edge and get the paper rims of the focus ring */
.btn:focus-visible        { box-shadow: 0 4px 0 var(--btn-line), 0 0 0 7px var(--paper); }
.btn--small:focus-visible { box-shadow: 0 3px 0 var(--btn-line), 0 0 0 7px var(--paper); }

/* ---- Halftone shading ---------------------------------------------------- */
/* Dots for light and shadow, never as wallpaper. Fade direction via --ht-fade. */
.halftone {
  --ht-dot: var(--ink);
  --ht-size: 9px;
  --ht-fade: linear-gradient(to bottom, transparent 0%, #000 100%);
  background-image: radial-gradient(circle at center, var(--ht-dot) 0 32%, transparent 34%);
  background-size: var(--ht-size) var(--ht-size);
  -webkit-mask-image: var(--ht-fade);
  mask-image: var(--ht-fade);
}

/* ---- Language switch ----------------------------------------------------- */
.lang-switch {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border: var(--bw-thin) solid currentColor;
  border-radius: 999px;
}
.lang-switch button {
  appearance: none;
  min-width: 40px;
  min-height: 34px;
  padding: 0.2em 0.65em;
  background: transparent;
  color: inherit;
  border: 0;
  border-radius: 999px;
  font-family: var(--font-letter);
  font-size: 0.875rem;
  font-weight: 700;
  line-height: 1;
}
.lang-switch button:hover { background: color-mix(in srgb, currentColor 14%, transparent); }
.lang-switch button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }

/* "ع": base.css owns this label on every page (page styles need no rule for it).
   English and Russian pages draw it with a system face that has Arabic, so they never
   download an Arabic web font for one letter; Arabic pages use the page's own text face.
   The bottom padding lifts the letter (it sits low in its em box) to the optical centre. */
html[lang] .lang-switch button[data-set-lang="ar"] {
  font-family: var(--font-text);
  font-size: 1.0625rem;
  font-weight: 600;
  line-height: 1;
  padding-block: 0 0.62em;
}
html:not([lang="ar"]) .lang-switch button[data-set-lang="ar"] {
  font-family: "Segoe UI", Tahoma, "Geeza Pro", "Noto Sans Arabic", "Noto Naskh Arabic", system-ui, Arial, sans-serif;
  padding-block: 0 0.46em;
}

/* Touch screens: 44px targets (the pill grows to 54px, still inside a 60px bar) */
@media (pointer: coarse) {
  .lang-switch button { min-width: 44px; min-height: 44px; }
  .btn--small { min-height: 44px; }
}

/* Forced colours (Windows contrast themes) drop backgrounds: mark the current language
   with the system highlight instead */
@media (forced-colors: active) {
  .lang-switch button[aria-pressed="true"] {
    forced-color-adjust: none;
    background: Highlight !important;
    color: HighlightText !important;
  }
}

/* ---- Mail chooser (assets/js/mail.js) -------------------------------------
   A click on a mailto: link with a mouse opens this balloon: Gmail, Outlook,
   the mail app, or copy the address. */
.mailpick {
  position: absolute;
  z-index: var(--z-modal);
  width: min(300px, calc(100vw - 24px));
  padding: 14px 12px 10px;
  background: var(--paper-white);
  color: var(--ink);
  border: var(--bw-thin) solid var(--ink);
  border-radius: 18px;
  box-shadow: 0 4px 0 var(--ink);
  font-family: var(--font-letter);
  text-align: start;
  animation: mailpick-in var(--dur-2) var(--ease-out);
}
.mailpick.is-above { animation-name: mailpick-in-above; }
@keyframes mailpick-in { from { opacity: 0; transform: translateY(-6px); } }
@keyframes mailpick-in-above { from { opacity: 0; transform: translateY(6px); } }
.mailpick__to {
  margin: 0 2.4em 8px 0;
  padding-inline: 6px;
  font-size: 0.9375rem;
  line-height: 1.35;
  color: var(--ink-soft);
}
.mailpick__email {
  display: block;
  color: var(--ink);
  font-size: 1rem;
  font-weight: 700;
  overflow-wrap: anywhere;
  user-select: all;
}
.mailpick__list { display: grid; gap: 2px; margin: 0; padding: 0; list-style: none; }
.mailpick__opt {
  display: flex;
  align-items: center;
  gap: 0.6em;
  width: 100%;
  min-height: 44px;
  padding: 0.45em 0.7em;
  background: transparent;
  color: var(--ink);
  border: 0;
  border-radius: 10px;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.2;
  text-align: start;
  text-decoration: none;
  cursor: pointer;
}
.mailpick__opt:hover { background: var(--molten); }
.mailpick__opt svg { width: 20px; height: 20px; flex: none; }
.mailpick__close {
  position: absolute;
  inset-block-start: 6px;
  inset-inline-end: 6px;
  width: 36px;
  height: 36px;
  padding: 0;
  background: transparent;
  color: var(--ink);
  border: 0;
  border-radius: 999px;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}
.mailpick__close:hover { background: color-mix(in srgb, var(--ink) 10%, transparent); }

.mailpick-toast {
  position: fixed;
  inset-inline: 0;
  bottom: 24px;
  z-index: var(--z-toast);
  width: max-content;
  max-width: calc(100vw - 32px);
  margin-inline: auto;
  padding: 0.6em 1.1em 0.65em;
  background: var(--ink);
  color: var(--paper);
  border-radius: 12px;
  font-family: var(--font-letter);
  font-weight: 600;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity var(--dur-2) var(--ease-out), transform var(--dur-2) var(--ease-out);
  pointer-events: none;
}
.mailpick-toast.is-visible { opacity: 1; transform: none; }

/* ---- Reduced motion ------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

/* Hide body until translations are applied (only set for non-English first paint) */
html.i18n-pending body { opacity: 0; }
