/* ============================================================
   Drop of Life — shared design system
   Tokens, resets, primitives and motion used by every page.
   ============================================================ */

@layer tokens, reset, base, components, motion, utils;

/* ---------- tokens ---------------------------------------- */
@layer tokens {
  :root {
    /* brand — deep clinical crimson */
    --blood-950: #180207;
    --blood-900: #2a040d;
    --blood-800: #470714;
    --blood-700: #720f21;
    --blood-600: #a3122b;
    --blood-500: #d21e3c;
    --blood-400: #f0496a;
    --blood-300: #ff8ba1;
    --blood-200: #ffc3ce;

    /* neutrals — warm, so the red never reads as pure hospital grey */
    --ink-950: #0a0508;
    --ink-900: #120a0f;
    --ink-800: #1c1117;
    --ink-700: #2a1a22;
    --ink-600: #3d2831;
    --ink-400: #6d5560;
    --ink-300: #9a838d;
    --ink-200: #c9b8bf;
    --cream: #fff4f1;
    --white: #ffffff;

    /* semantic accents */
    --mint-500: #1fd39c;
    --mint-400: #34e9b0;
    --mint-glow: rgba(52, 233, 176, 0.45);
    --amber-500: #ffb020;
    --amber-400: #ffc861;
    --sky-400: #56b8ff;
    --plasma: #ffd166;

    /* surfaces */
    --surface-0: rgba(255, 255, 255, 0.035);
    --surface-1: rgba(255, 255, 255, 0.06);
    --surface-2: rgba(255, 255, 255, 0.1);
    --stroke-1: rgba(255, 255, 255, 0.1);
    --stroke-2: rgba(255, 255, 255, 0.18);
    --stroke-hot: rgba(240, 73, 106, 0.55);

    /* type */
    --font-ui: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
      "Helvetica Neue", Arial, sans-serif;
    --font-display: "Avenir Next", "Futura", "Trebuchet MS", var(--font-ui);
    --font-mono: "SF Mono", ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace;
    --font-ar: "SF Arabic", "Geeza Pro", "Noto Naskh Arabic", "Segoe UI", Tahoma,
      var(--font-ui);

    /* geometry */
    --r-xs: 8px;
    --r-sm: 12px;
    --r-md: 18px;
    --r-lg: 26px;
    --r-xl: 34px;
    --r-pill: 999px;

    /* elevation */
    --sh-sm: 0 2px 8px rgba(0, 0, 0, 0.35);
    --sh-md: 0 12px 32px -8px rgba(0, 0, 0, 0.55);
    --sh-lg: 0 30px 70px -20px rgba(0, 0, 0, 0.7);
    --glow-blood: 0 0 0 1px var(--stroke-hot), 0 8px 40px -6px rgba(210, 30, 60, 0.55);
    --glow-mint: 0 0 0 1px rgba(52, 233, 176, 0.5), 0 8px 40px -6px var(--mint-glow);

    /* motion */
    --e-out: cubic-bezier(0.16, 1, 0.3, 1);
    --e-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
    --e-in-out: cubic-bezier(0.65, 0, 0.35, 1);
    --t-fast: 140ms;
    --t-med: 260ms;
    --t-slow: 520ms;

    color-scheme: dark;
  }

  /* Arabic gets its own face + slightly larger optical size */
  :root[dir="rtl"] {
    --font-ui: var(--font-ar);
    --font-display: var(--font-ar);
  }
}

/* ---------- reset ----------------------------------------- */
@layer reset {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }
  * {
    margin: 0;
    padding: 0;
  }
  html {
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
  }
  body {
    min-height: 100svh;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
  }
  img,
  svg,
  canvas {
    display: block;
    max-width: 100%;
  }
  button,
  input,
  select,
  textarea {
    font: inherit;
    color: inherit;
  }
  button {
    background: none;
    border: none;
    cursor: pointer;
  }
  a {
    color: inherit;
    text-decoration: none;
  }
  ul,
  ol {
    list-style: none;
  }
  :focus-visible {
    outline: 2px solid var(--blood-300);
    outline-offset: 3px;
    border-radius: 4px;
  }
  ::selection {
    background: var(--blood-500);
    color: var(--white);
  }
}

/* ---------- base ------------------------------------------ */
@layer base {
  body {
    font-family: var(--font-ui);
    color: var(--cream);
    background: var(--ink-950);
    overflow-x: hidden;
  }

  /* Living backdrop: two crimson auroras + a fine cell grid. */
  .backdrop {
    position: fixed;
    inset: 0;
    z-index: -2;
    background:
      radial-gradient(70rem 46rem at 12% -8%, rgba(163, 18, 43, 0.5), transparent 62%),
      radial-gradient(56rem 40rem at 92% 8%, rgba(114, 15, 33, 0.46), transparent 60%),
      radial-gradient(48rem 40rem at 60% 108%, rgba(31, 211, 156, 0.12), transparent 62%),
      linear-gradient(170deg, var(--ink-950), #150409 45%, var(--ink-950));
  }
  .backdrop::after {
    content: "";
    position: absolute;
    inset: -20%;
    background-image: radial-gradient(rgba(255, 255, 255, 0.045) 1px, transparent 1px);
    background-size: 34px 34px;
    mask-image: radial-gradient(70% 60% at 50% 40%, #000 30%, transparent 78%);
    animation: driftGrid 46s linear infinite;
  }
  /* Canvas layer where floating blood cells are drawn (js/core/fx.js). */
  .backdrop-cells {
    position: fixed;
    inset: 0;
    z-index: -1;
    pointer-events: none;
    opacity: 0.75;
  }

  h1,
  h2,
  h3,
  h4 {
    font-family: var(--font-display);
    font-weight: 800;
    line-height: 1.08;
    letter-spacing: -0.022em;
    text-wrap: balance;
  }
  h1 {
    font-size: clamp(2.4rem, 6vw, 4.4rem);
  }
  h2 {
    font-size: clamp(1.6rem, 3.4vw, 2.4rem);
  }
  h3 {
    font-size: 1.2rem;
  }
  p {
    text-wrap: pretty;
  }
  small,
  .t-sm {
    font-size: 0.83rem;
  }
  .t-mut {
    color: var(--ink-200);
  }
  .t-dim {
    color: var(--ink-300);
  }
  .num {
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum" 1;
  }
}

/* ---------- components ------------------------------------ */
@layer components {
  .shell {
    width: min(1180px, 100% - 2.5rem);
    margin-inline: auto;
  }

  /* --- top bar --- */
  .topbar {
    position: sticky;
    top: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.85rem 0;
    backdrop-filter: blur(16px) saturate(150%);
    background: linear-gradient(to bottom, rgba(10, 5, 8, 0.86), rgba(10, 5, 8, 0.32));
    border-bottom: 1px solid var(--stroke-1);
  }
  .topbar__inner {
    display: flex;
    align-items: center;
    gap: 1rem;
    width: min(1180px, 100% - 2.5rem);
    margin-inline: auto;
  }
  .brand {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    font-family: var(--font-display);
    font-weight: 800;
    letter-spacing: -0.02em;
  }
  .brand img {
    width: 38px;
    height: 38px;
    filter: drop-shadow(0 3px 10px rgba(210, 30, 60, 0.6));
    animation: pulseDrop 3.4s var(--e-in-out) infinite;
  }
  .brand__name {
    font-size: 1.06rem;
    line-height: 1.15;
  }
  .brand__tag {
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color: var(--blood-300);
  }
  .spacer {
    flex: 1;
  }

  /* --- buttons --- */
  .btn {
    --btn-bg: var(--surface-1);
    --btn-fg: var(--cream);
    --btn-bd: var(--stroke-2);
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.55rem;
    padding: 0.7rem 1.25rem;
    border-radius: var(--r-pill);
    border: 1px solid var(--btn-bd);
    background: var(--btn-bg);
    color: var(--btn-fg);
    font-weight: 700;
    font-size: 0.94rem;
    letter-spacing: -0.005em;
    white-space: nowrap;
    isolation: isolate;
    overflow: hidden;
    transition: transform var(--t-fast) var(--e-spring),
      box-shadow var(--t-med) var(--e-out), background var(--t-med) var(--e-out),
      border-color var(--t-med) var(--e-out), opacity var(--t-fast) linear;
  }
  .btn::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: -1;
    background: linear-gradient(to bottom, rgba(255, 255, 255, 0.16), transparent 60%);
    opacity: 0.7;
  }
  .btn:hover:not(:disabled) {
    transform: translateY(-2px);
    border-color: var(--stroke-hot);
    box-shadow: var(--sh-md);
  }
  .btn:active:not(:disabled) {
    transform: translateY(0) scale(0.975);
  }
  .btn:disabled {
    opacity: 0.42;
    cursor: not-allowed;
  }
  .btn--primary {
    --btn-bg: linear-gradient(140deg, var(--blood-400), var(--blood-600) 70%);
    --btn-bd: rgba(255, 139, 161, 0.5);
    box-shadow: 0 10px 28px -12px rgba(210, 30, 60, 0.95);
  }
  .btn--primary:hover:not(:disabled) {
    box-shadow: 0 16px 38px -12px rgba(240, 73, 106, 0.95);
  }
  .btn--mint {
    --btn-bg: linear-gradient(140deg, var(--mint-400), #0e9c76 75%);
    --btn-bd: rgba(52, 233, 176, 0.5);
    --btn-fg: #04231b;
  }
  .btn--ghost {
    --btn-bg: transparent;
    --btn-bd: var(--stroke-2);
  }
  .btn--danger {
    --btn-bg: rgba(210, 30, 60, 0.14);
    --btn-bd: rgba(240, 73, 106, 0.45);
    --btn-fg: var(--blood-200);
  }
  .btn--lg {
    padding: 1rem 1.9rem;
    font-size: 1.06rem;
  }
  .btn--sm {
    padding: 0.44rem 0.85rem;
    font-size: 0.82rem;
  }
  .btn--icon {
    padding: 0.55rem;
    width: 2.45rem;
    height: 2.45rem;
    border-radius: var(--r-pill);
  }

  /* keycap hint chip inside buttons */
  .kbd {
    display: inline-grid;
    place-items: center;
    min-width: 1.5em;
    padding: 0.1em 0.42em;
    border-radius: 6px;
    border: 1px solid var(--stroke-2);
    border-bottom-width: 2px;
    background: rgba(0, 0, 0, 0.42);
    font-family: var(--font-mono);
    font-size: 0.74em;
    font-weight: 700;
    line-height: 1.5;
    color: var(--ink-200);
  }

  /* --- cards & panels --- */
  .card {
    position: relative;
    border-radius: var(--r-lg);
    border: 1px solid var(--stroke-1);
    background: linear-gradient(160deg, var(--surface-1), var(--surface-0));
    backdrop-filter: blur(14px);
    box-shadow: var(--sh-md);
  }
  .panel {
    padding: 1.35rem;
    border-radius: var(--r-md);
    border: 1px solid var(--stroke-1);
    background: rgba(255, 255, 255, 0.045);
  }

  .chip {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.28rem 0.72rem;
    border-radius: var(--r-pill);
    border: 1px solid var(--stroke-2);
    background: var(--surface-1);
    font-size: 0.76rem;
    font-weight: 700;
    letter-spacing: 0.02em;
  }
  .chip--easy {
    color: var(--mint-400);
    border-color: rgba(52, 233, 176, 0.35);
    background: rgba(52, 233, 176, 0.1);
  }
  .chip--medium {
    color: var(--amber-400);
    border-color: rgba(255, 176, 32, 0.35);
    background: rgba(255, 176, 32, 0.1);
  }
  .chip--hard {
    color: var(--blood-300);
    border-color: rgba(240, 73, 106, 0.4);
    background: rgba(240, 73, 106, 0.12);
  }

  /* --- form controls --- */
  .field {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
  }
  .field > label,
  .label {
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.055em;
    text-transform: uppercase;
    color: var(--ink-300);
  }
  .input,
  .select,
  .textarea {
    width: 100%;
    padding: 0.66rem 0.85rem;
    border-radius: var(--r-sm);
    border: 1px solid var(--stroke-2);
    background: rgba(0, 0, 0, 0.32);
    color: var(--cream);
    transition: border-color var(--t-fast) var(--e-out),
      box-shadow var(--t-fast) var(--e-out), background var(--t-fast) var(--e-out);
  }
  .textarea {
    resize: vertical;
    min-height: 4.6rem;
    line-height: 1.5;
  }
  .input:focus,
  .select:focus,
  .textarea:focus {
    outline: none;
    border-color: var(--blood-400);
    background: rgba(0, 0, 0, 0.5);
    box-shadow: 0 0 0 4px rgba(210, 30, 60, 0.2);
  }
  .input::placeholder,
  .textarea::placeholder {
    color: var(--ink-400);
  }
  .select {
    appearance: none;
    padding-inline-end: 2.2rem;
    background-image: linear-gradient(45deg, transparent 50%, var(--ink-200) 50%),
      linear-gradient(135deg, var(--ink-200) 50%, transparent 50%);
    background-position: right 1rem center, right 0.7rem center;
    background-size: 5px 5px, 5px 5px;
    background-repeat: no-repeat;
  }
  :root[dir="rtl"] .select {
    background-position: left 0.7rem center, left 1rem center;
  }
  .select option {
    background: var(--ink-800);
    color: var(--cream);
  }

  .switch {
    display: inline-flex;
    align-items: center;
    gap: 0.7rem;
    cursor: pointer;
    user-select: none;
  }
  .switch input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
  }
  .switch__track {
    position: relative;
    width: 46px;
    height: 26px;
    flex: none;
    border-radius: var(--r-pill);
    border: 1px solid var(--stroke-2);
    background: rgba(0, 0, 0, 0.42);
    transition: background var(--t-med) var(--e-out), border-color var(--t-med) var(--e-out);
  }
  .switch__track::after {
    content: "";
    position: absolute;
    inset-inline-start: 3px;
    top: 2px;
    width: 19px;
    height: 19px;
    border-radius: 50%;
    background: var(--ink-200);
    transition: transform var(--t-med) var(--e-spring), background var(--t-med) var(--e-out);
  }
  .switch input:checked + .switch__track {
    background: linear-gradient(140deg, var(--mint-400), #0e9c76);
    border-color: rgba(52, 233, 176, 0.55);
  }
  .switch input:checked + .switch__track::after {
    transform: translateX(20px);
    background: #04231b;
  }
  :root[dir="rtl"] .switch input:checked + .switch__track::after {
    transform: translateX(-20px);
  }
  .switch input:focus-visible + .switch__track {
    outline: 2px solid var(--blood-300);
    outline-offset: 3px;
  }

  /* --- toasts --- */
  .toasts {
    position: fixed;
    inset-block-end: 1.3rem;
    inset-inline-end: 1.3rem;
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    pointer-events: none;
  }
  .toast {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.75rem 1.05rem;
    border-radius: var(--r-sm);
    border: 1px solid var(--stroke-2);
    background: rgba(20, 10, 14, 0.94);
    backdrop-filter: blur(12px);
    box-shadow: var(--sh-lg);
    font-size: 0.9rem;
    font-weight: 600;
    animation: toastIn 0.42s var(--e-spring) both;
  }
  .toast--ok {
    border-color: rgba(52, 233, 176, 0.45);
  }
  .toast--ok .toast__dot {
    background: var(--mint-400);
  }
  .toast--err {
    border-color: rgba(240, 73, 106, 0.5);
  }
  .toast--err .toast__dot {
    background: var(--blood-400);
  }
  .toast__dot {
    width: 9px;
    height: 9px;
    flex: none;
    border-radius: 50%;
    background: var(--amber-400);
    box-shadow: 0 0 12px currentColor;
  }
  .toast.is-out {
    animation: toastOut 0.3s var(--e-in-out) both;
  }

  /* --- modal --- */
  .modal {
    position: fixed;
    inset: 0;
    z-index: 150;
    display: grid;
    place-items: center;
    padding: 1.5rem;
    background: rgba(6, 2, 4, 0.72);
    backdrop-filter: blur(10px);
    animation: fadeIn var(--t-med) var(--e-out) both;
  }
  .modal[hidden] {
    display: none;
  }
  .modal__box {
    width: min(560px, 100%);
    max-height: 88svh;
    overflow: auto;
    padding: 1.6rem;
    border-radius: var(--r-lg);
    border: 1px solid var(--stroke-2);
    background: linear-gradient(160deg, #22101a, #150a10);
    box-shadow: var(--sh-lg);
    animation: popIn 0.44s var(--e-spring) both;
  }
  .modal__title {
    margin-bottom: 0.35rem;
  }
  .modal__actions {
    display: flex;
    gap: 0.6rem;
    justify-content: flex-end;
    margin-top: 1.3rem;
    flex-wrap: wrap;
  }

  /* --- progress / meters --- */
  .meter {
    height: 8px;
    border-radius: var(--r-pill);
    background: rgba(0, 0, 0, 0.4);
    overflow: hidden;
    border: 1px solid var(--stroke-1);
  }
  .meter__fill {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, var(--blood-500), var(--blood-300));
    transition: width var(--t-slow) var(--e-out);
  }

  .hearts {
    display: flex;
    gap: 0.3rem;
  }
  .heart {
    width: 22px;
    height: 22px;
    transition: transform var(--t-med) var(--e-spring), opacity var(--t-med) var(--e-out),
      filter var(--t-med) var(--e-out);
    filter: drop-shadow(0 0 8px rgba(240, 73, 106, 0.75));
  }
  .heart.is-lost {
    opacity: 0.22;
    filter: grayscale(1);
    transform: scale(0.78);
  }
}

/* ---------- motion ---------------------------------------- */
@layer motion {
  @keyframes fadeIn {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
  @keyframes popIn {
    from {
      opacity: 0;
      transform: translateY(18px) scale(0.94);
    }
    to {
      opacity: 1;
      transform: none;
    }
  }
  @keyframes toastIn {
    from {
      opacity: 0;
      transform: translateY(16px) scale(0.95);
    }
    to {
      opacity: 1;
      transform: none;
    }
  }
  @keyframes toastOut {
    to {
      opacity: 0;
      transform: translateY(-6px) scale(0.97);
    }
  }
  @keyframes pulseDrop {
    0%,
    100% {
      transform: scale(1);
    }
    18% {
      transform: scale(1.16);
    }
    34% {
      transform: scale(0.98);
    }
    52% {
      transform: scale(1.07);
    }
  }
  @keyframes driftGrid {
    to {
      transform: translate3d(34px, 34px, 0);
    }
  }
  @keyframes floatY {
    0%,
    100% {
      transform: translateY(0);
    }
    50% {
      transform: translateY(-12px);
    }
  }
  @keyframes shakeX {
    10%,
    90% {
      transform: translateX(-2px);
    }
    20%,
    80% {
      transform: translateX(4px);
    }
    30%,
    50%,
    70% {
      transform: translateX(-7px);
    }
    40%,
    60% {
      transform: translateX(7px);
    }
  }
  @keyframes riseIn {
    from {
      opacity: 0;
      transform: translateY(26px);
    }
    to {
      opacity: 1;
      transform: none;
    }
  }
  @keyframes spinSlow {
    to {
      transform: rotate(360deg);
    }
  }

  .rise {
    animation: riseIn 0.7s var(--e-out) both;
  }
  .rise-1 { animation-delay: 0.06s; }
  .rise-2 { animation-delay: 0.13s; }
  .rise-3 { animation-delay: 0.2s; }
  .rise-4 { animation-delay: 0.27s; }
  .rise-5 { animation-delay: 0.34s; }
  .rise-6 { animation-delay: 0.41s; }
  .float {
    animation: floatY 6s var(--e-in-out) infinite;
  }
  .shake {
    animation: shakeX 0.5s var(--e-in-out) both;
  }

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

/* ---------- utils ----------------------------------------- */
@layer utils {
  .row {
    display: flex;
    align-items: center;
    gap: 0.6rem;
  }
  .col {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
  }
  .wrap {
    flex-wrap: wrap;
  }
  .center {
    display: grid;
    place-items: center;
  }
  .grow {
    flex: 1;
  }
  .hidden {
    display: none !important;
  }
  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }
  .mt-1 { margin-top: 0.5rem; }
  .mt-2 { margin-top: 1rem; }
  .mt-3 { margin-top: 1.6rem; }
  .mt-4 { margin-top: 2.4rem; }
  .text-c { text-align: center; }
}
