/*
 * Base: non-colour tokens, page frame, top bar, controls, status, toasts, dialogs, drawer.
 * Colours come only from /brand.css (--fm-*), so a branding save or theme switch restyles
 * everything. Spec: docs/ART-DIRECTION.md. Motion rules: docs/MOTION.md.
 */

:root {
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;
  --space-8: 64px;
  --gutter: 24px;
  --margin: 32px;
  --fm-radius: 4px;

  /* Type: 16 -> 26 -> 42 on the 1.618 ratio; 13 for meta (16/1.618 is illegible in Arabic). */
  --type-h1: 600 42px/64px var(--fm-font);
  --type-h2: 600 26px/40px var(--fm-font);
  --type-body: 400 16px/24px var(--fm-font);
  --type-label: 600 16px/24px var(--fm-font);
  --type-small: 400 13px/20px var(--fm-font);

  --motion-instant: 80ms;
  --motion-fast: 120ms;
  --motion-base: 240ms;
  --motion-exit: 160ms;
  --motion-slow: 380ms;
  --motion-content: 500ms;
  --motion-ripple: 4000ms;
  --ease-standard: cubic-bezier(.4, 0, .2, 1);
  --ease-out: cubic-bezier(.2, .8, .2, 1);
  --ease-light: cubic-bezier(.16, 1, .3, 1);
  --ease-flash: cubic-bezier(.5, 0, 1, 1);
  --ease-decay: cubic-bezier(0, 0, .2, 1);

  --topbar-h: 60px;
  --actionbar-h: 72px;
  --dir: 1;
  --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='128' height='128'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .5 0 0 0 0 .5 0 0 0 0 .5 0 0 0 1.2 -.1'/%3E%3C/filter%3E%3Crect width='128' height='128' filter='url(%23n)'/%3E%3C/svg%3E");
}
[dir='rtl'] { --dir: -1; }

@media (max-width: 1279px) { :root { --margin: 24px; } }
@media (max-width: 639px) { :root { --margin: 16px; --gutter: 16px; --topbar-h: 56px; --actionbar-h: 64px; } }

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

html {
  background: var(--fm-bg);
  color: var(--fm-text);
  font: var(--type-body);
  font-variant-numeric: tabular-nums;
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
}

/* A column, so the sticky action bar rests at the bottom even on short pages. */
body { margin: 0; min-height: 100vh; overflow-x: hidden; display: flex; flex-direction: column; }
main { flex: 1 0 auto; }

/* Static grain behind content, multiply mode (guideline p.40). */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background-image: var(--grain);
  opacity: var(--fm-grain-opacity);
  mix-blend-mode: multiply;
}

h1, h2, h3, p, ol, ul, dl, dd, figure { margin: 0; }
h1 { font: var(--type-h1); }
h2 { font: var(--type-h2); }
h3 { font: var(--type-label); }
strong, b { font-weight: 600; }
img, svg, video { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--fm-link); text-underline-offset: 3px; }
bdi[dir='ltr'], .ltr { line-height: 1.3; }

.container { max-width: 1440px; margin-inline: auto; padding-inline: var(--margin); }
.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%);
  white-space: nowrap;
}
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.icon { width: 20px; height: 20px; fill: currentColor; flex: none; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--fm-focus); outline-offset: 2px; }
:focus:not(:focus-visible) { outline: none; }

.skip-link {
  position: fixed; inset-block-start: var(--space-2); inset-inline-start: var(--space-2); z-index: 300;
  padding: var(--space-2) var(--space-4);
  background: var(--fm-ink); color: var(--fm-ink-text);
  border-radius: var(--fm-radius);
  transform: translateY(-200%);
}
.skip-link:focus { transform: none; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; inset-block-start: 0; z-index: 50;
  display: flex; align-items: center; gap: 28px;
  height: var(--topbar-h);
  padding-inline: 28px;
  background: var(--fm-topbar-bg);
  color: var(--fm-topbar-fg);
  border-block-end: 1px solid var(--fm-border);
}
.brand { display: flex; align-items: center; flex: none; padding-block: var(--space-2); }
.logo { height: 28px; width: auto; }
.logo--on-dark { display: none; }
:root[data-theme='dark'] .logo--on-light { display: none; }
:root[data-theme='dark'] .logo--on-dark { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .logo--on-light { display: none; }
  :root:not([data-theme='light']) .logo--on-dark { display: block; }
}

.tabs { display: flex; align-items: center; gap: var(--space-1); margin-inline-start: var(--space-3); }
/* Pill tabs (approved concept C): the active one sits on a raised chip. */
.tab {
  display: flex; align-items: center; gap: var(--space-2);
  position: relative;
  padding: 8px 14px; border-radius: 999px;
  font: 400 15px/24px var(--fm-font);
  color: var(--fm-text-secondary);
  text-decoration: none;
  transition: color var(--motion-fast) var(--ease-out), background-color var(--motion-fast) var(--ease-out);
}
.tab .icon { display: none; }
.tab[aria-current='page'] { color: var(--fm-topbar-fg); font-weight: 600; background: var(--fm-surface-raised); box-shadow: inset 0 0 0 1px var(--fm-border); }
@media (hover: hover) and (pointer: fine) { .tab:hover { color: var(--fm-topbar-fg); } }

.conn {
  font: var(--type-small);
  color: var(--fm-offline);
  display: flex; align-items: center; gap: var(--space-2);
}
/* How many faces are online, live. */
.live-pill {
  display: inline-flex; align-items: center; gap: var(--space-2);
  margin-inline-start: auto;
  padding: 6px 12px; border: 1px solid var(--fm-border); border-radius: 999px;
  font: var(--type-small); color: var(--fm-text-secondary); white-space: nowrap;
}
.live-pill-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--fm-online); box-shadow: 0 0 8px var(--fm-online); }
.live-pill[data-none] .live-pill-dot { background: transparent; box-shadow: inset 0 0 0 1.5px var(--fm-wall-offline); }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; flex: none;
  padding: 0; border: 0; border-radius: var(--fm-radius);
  background: transparent; color: inherit; cursor: pointer;
  transition: background-color var(--motion-fast) var(--ease-out);
}
@media (hover: hover) and (pointer: fine) { .icon-btn:hover { background: var(--fm-primary-tint); } }
.theme-toggle { width: 34px; height: 34px; border: 1px solid var(--fm-border); border-radius: 50%; color: var(--fm-text-secondary); }
.theme-toggle .icon { width: 16px; height: 16px; }
/* Without the pill (no screens yet) the controls still sit at the inline end. */
.live-pill[hidden] + .theme-toggle { margin-inline-start: auto; }

/* Mobile: the tabs become the bottom navigation. */
@media (max-width: 639px) {
  .topbar { gap: var(--space-4); }
  .theme-toggle { width: 44px; height: 44px; }
  .tabs {
    position: fixed; inset-inline: 0; inset-block-end: 0; z-index: 60;
    height: 56px; gap: 0;
    padding-block-end: env(safe-area-inset-bottom);
    background: var(--fm-topbar-bg);
    border-block-start: 1px solid var(--fm-border);
  }
  .tabs { margin: 0; }
  .tab { flex: 1; flex-direction: column; justify-content: center; gap: 2px; padding: 0; border-radius: 0; font: var(--type-small); font-weight: 600; min-height: 44px; }
  .tab .icon { display: block; }
  .tab[aria-current='page'] { background: none; box-shadow: none; }
  .tab[aria-current='page']::after { content: ''; position: absolute; inset-inline: 0; inset-block-start: -1px; height: 2px; background: var(--fm-primary-text); }
  .live-pill { padding: 4px 8px; }
  .conn { position: fixed; inset-inline: 0; inset-block-start: var(--topbar-h); justify-content: center; padding: var(--space-1); background: var(--fm-surface-raised); z-index: 49; }
  body { padding-block-end: calc(56px + env(safe-area-inset-bottom)); }
}

/* ---------- Page headings ---------- */
.page-head { padding-block: var(--space-7) var(--space-5); }
.page-head .lead { color: var(--fm-text-secondary); margin-block-start: var(--space-2); }
.text-link { display: inline-flex; align-items: center; min-height: 44px; font: var(--type-label); color: var(--fm-link); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  height: 44px; padding: 0 20px;
  border: 1px solid transparent; border-radius: var(--fm-radius);
  font: var(--type-label); white-space: nowrap; text-decoration: none;
  cursor: pointer;
  transition: background-color var(--motion-fast) var(--ease-out), border-color var(--motion-fast) var(--ease-out), transform var(--motion-fast) var(--ease-out);
}
.btn:active:not(:disabled) { transform: scale(.98); transition-duration: var(--motion-instant); }
.btn--primary { background: var(--fm-primary); color: var(--fm-on-primary); }
.btn--secondary { background: transparent; color: var(--fm-text); border-color: var(--fm-border-strong); }
.btn--danger { background: transparent; color: var(--fm-offline); border-color: var(--fm-border-strong); }
.btn--ghost { background: transparent; color: var(--fm-text-secondary); }
@media (hover: hover) and (pointer: fine) {
  .btn--primary:hover:not(:disabled) { background: var(--fm-primary-dark); }
  .btn--secondary:hover:not(:disabled), .btn--danger:hover:not(:disabled) { border-color: var(--fm-text); background: var(--fm-surface-raised); }
  .btn--ghost:hover:not(:disabled) { color: var(--fm-text); background: var(--fm-primary-tint); }
}
.btn:disabled, .btn[aria-disabled='true'] {
  background: var(--fm-disabled-bg); color: var(--fm-disabled-text); border-color: transparent; cursor: not-allowed;
}
.btn[aria-busy='true'] { cursor: progress; }

/* ---------- Inputs ---------- */
.field { display: grid; gap: var(--space-2); }
.field label, .field-label { font: var(--type-label); }
.input, select.input {
  width: 100%; min-height: 44px;
  padding: 0 var(--space-3);
  border: 1px solid var(--fm-border-strong); border-radius: var(--fm-radius);
  background: var(--fm-surface-raised); color: var(--fm-text);
  box-shadow: var(--fm-elev-2);
}
.hint { font: var(--type-small); color: var(--fm-text-secondary); }

/* ---------- Status: an 8px shape + a label; colour is never the only signal ---------- */
.status {
  --st-online: var(--fm-online);
  --st-playing: var(--fm-playing);
  --st-warning: var(--fm-warning);
  --st-offline: var(--fm-offline);
  display: inline-flex; align-items: center; gap: var(--space-2);
  font: var(--type-small);
}
.status-shape { position: relative; width: 8px; height: 8px; flex: none; }
.status--playing .status-shape { border-radius: 50%; background: var(--st-playing); color: var(--st-playing); }
.status--online .status-shape { border-radius: 50%; background: var(--st-online); color: var(--st-online); }
.status--warning .status-shape { background: var(--st-warning); transform: rotate(45deg) scale(.85); }
.status--offline .status-shape { border-radius: 50%; border: 1.5px solid var(--st-offline); }
.status--idle .status-shape { border-radius: 50%; background: var(--st-online); }
.status--downloading .status-shape { border-radius: 50%; background: var(--fm-accent); }
/* The ripple: one small ring per live status, transform + opacity only. */
.status--playing .status-shape::after,
.status--online .status-shape::after {
  content: '';
  position: absolute; inset: 0;
  border: 1px solid currentColor; border-radius: 50%;
  opacity: 0;
  animation: fm-ripple var(--motion-ripple) var(--ease-decay) infinite;
  animation-delay: calc(var(--pos, 0) * 250ms);
}
html.is-hidden .status-shape::after { animation-play-state: paused; }
@keyframes fm-ripple {
  0% { transform: scale(1); opacity: .4; }
  60%, 100% { transform: scale(3.2); opacity: 0; }
}

/* ---------- Toasts (L3) ---------- */
.toasts {
  position: fixed; z-index: 200;
  inset-inline-end: var(--space-5);
  inset-block-end: calc(var(--actionbar-h) + var(--space-5));
  display: grid; gap: var(--space-2);
  width: min(400px, calc(100vw - 2 * var(--space-5)));
  pointer-events: none;
}
.toast {
  display: flex; align-items: flex-start; gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--fm-ink-border); border-radius: var(--fm-radius);
  background: var(--fm-ink); color: var(--fm-ink-text);
  box-shadow: var(--fm-elev-3);
  pointer-events: auto;
  opacity: 0; transform: translateY(12px);
  transition: opacity var(--motion-base) var(--ease-light), transform var(--motion-base) var(--ease-light);
}
.toast.is-in { opacity: 1; transform: none; }
.toast.is-out { opacity: 0; transform: translateY(4px); transition-duration: var(--motion-exit); transition-timing-function: var(--ease-standard); }
.toast .icon { margin-block-start: 2px; }
.toast--error .icon { color: var(--fm-offline); }
.toast-text { flex: 1; }
.toast .icon-btn { width: 32px; height: 32px; margin: -4px; color: inherit; }
@media (max-width: 639px) { .toast .icon-btn { width: 44px; height: 44px; margin: -8px -10px; } }
@media (max-width: 639px) {
  .toasts { inset-inline: var(--margin); width: auto; inset-block-end: calc(56px + var(--actionbar-h) + var(--space-2) + env(safe-area-inset-bottom)); }
}

/* ---------- Dialogs (L3) ---------- */
.dialog {
  max-width: min(880px, calc(100vw - 2 * var(--margin)));
  padding: 0;
  border: 1px solid var(--fm-ink-border); border-radius: var(--fm-radius);
  background: var(--fm-surface-raised); color: var(--fm-text);
  box-shadow: var(--fm-elev-3);
}
.dialog::backdrop { background: color-mix(in srgb, var(--fm-veil) 60%, transparent); }
.dialog[open] { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.dialog-media { background: var(--fm-stage-bg); display: grid; place-items: center; min-height: 320px; }
.dialog-media img, .dialog-media video { max-height: 70vh; width: auto; }
.dialog-side { display: grid; align-content: start; gap: var(--space-5); padding: var(--space-5); }
.dialog-side h2 { overflow-wrap: anywhere; }
.dialog-actions { display: flex; flex-wrap: wrap; gap: var(--space-3); }
.dialog--confirm[open] { display: grid; grid-template-columns: 1fr; gap: var(--space-5); padding: var(--space-5); max-width: 460px; }
@media (max-width: 639px) {
  .dialog[open] { grid-template-columns: 1fr; }
  .dialog-media { min-height: 200px; }
  .dialog-media img, .dialog-media video { max-height: 45vh; }
}

.facts { display: grid; grid-template-columns: auto 1fr; gap: var(--space-2) var(--space-4); font: var(--type-small); }
.facts dt { color: var(--fm-text-secondary); }
.facts dd { color: var(--fm-text); overflow-wrap: anywhere; }

/* ---------- Drawer (from the inline end) ---------- */
.drawer-scrim { position: fixed; inset: 0; z-index: 90; background: color-mix(in srgb, var(--fm-veil) 40%, transparent); opacity: 0; transition: opacity var(--motion-base) var(--ease-light); }
.drawer {
  position: fixed; z-index: 100;
  inset-block: 0; inset-inline-end: 0;
  width: min(440px, 100vw);
  display: flex; flex-direction: column;
  background: var(--fm-surface-raised); color: var(--fm-text);
  border-inline-start: 1px solid var(--fm-ink-border);
  box-shadow: var(--fm-elev-3);
  opacity: 0; transform: translateX(calc(var(--dir) * 24px));
  transition: opacity var(--motion-base) var(--ease-light), transform var(--motion-base) var(--ease-light);
}
.drawer.is-open, .drawer-scrim.is-open { opacity: 1; transform: none; }
.drawer.is-closing, .drawer-scrim.is-closing { transition-duration: var(--motion-exit); transition-timing-function: var(--ease-standard); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); padding: var(--space-4) var(--space-5); border-block-end: 1px solid var(--fm-border); }
.drawer-body { flex: 1; overflow-y: auto; padding: var(--space-5); display: grid; align-content: start; gap: var(--space-6); }
.drawer-section { display: grid; gap: var(--space-4); padding-block-start: var(--space-5); border-block-start: 1px solid var(--fm-border); }
.drawer-section:first-child { padding-block-start: 0; border-block-start: 0; }
.drawer-section h3 { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); }
.drawer-form { display: grid; gap: var(--space-4); }
.drawer-row { display: flex; flex-wrap: wrap; align-items: end; gap: var(--space-3); }
.drawer-row .field { flex: 1 1 180px; }
