@layer tokens, base, layout, components, motion;

/* ==========================================================================
   Half-life — a darkroom for text that deletes itself.
   ========================================================================== */

@layer tokens {
  @font-face {
    font-family: "Bricolage Grotesque";
    src: url("/fonts/bricolage-grotesque.woff2") format("woff2");
    font-weight: 400 800;
    font-stretch: 75% 100%;
    font-display: swap;
    unicode-range: U+0000-00FF, U+2000-206F, U+2122, U+2191, U+2193, U+2212;
  }

  @font-face {
    font-family: "JetBrains Mono";
    src: url("/fonts/jetbrains-mono.woff2") format("woff2");
    font-weight: 400 700;
    font-display: swap;
    unicode-range: U+0000-00FF, U+2000-206F, U+2122, U+2191, U+2193, U+2212;
  }

  :root {
    color-scheme: dark light;

    /* Darkroom: blue-black ground, amber safelight. */
    --ink: #0c0e13;
    --panel: #141821;
    --panel-sunk: #10141c;
    --rule: #262c39;
    --rule-soft: #1c2130;
    --paper: #e8e6e1;
    --dim: #7c8493;
    --gutter-fg: #454d5f;
    --safelight: #ff5f35;
    --critical: #ff3b2f;
    --shadow: 0 1px 0 rgba(255, 255, 255, 0.02), 0 24px 60px -32px rgba(0, 0, 0, 0.9);

    --font-display: "Bricolage Grotesque", "Helvetica Neue", Helvetica, sans-serif;
    --font-mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;

    --fs-code: 0.875rem;
    --fs-ui: 0.6875rem;
    --fs-body: 0.8125rem;
    --fs-tagline: 0.875rem;
    --line: 1.65;
    --track: 0.09em;

    --pad: 1rem;
    --gap: 0.75rem;
    --frame-max: 62rem;
    --radius: 3px;
  }

  @media (prefers-color-scheme: light) {
    :root {
      /* Prints under gallery light — cool paper, not cream. */
      --ink: #eef0f2;
      --panel: #ffffff;
      --panel-sunk: #f0f3f6;
      --rule: #d3d8de;
      --rule-soft: #e3e7eb;
      --paper: #11151c;
      --dim: #5c6672;
      --gutter-fg: #8b96a3;
      --safelight: #e8471b;
      --critical: #d81f0d;
      --shadow: 0 1px 0 rgba(255, 255, 255, 0.8), 0 20px 50px -34px rgba(17, 21, 28, 0.5);
    }
  }
}

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

  html {
    height: 100%;
  }

  body {
    height: 100%;
    margin: 0;
    background: var(--ink);
    color: var(--paper);
    font-family: var(--font-mono);
    font-size: var(--fs-body);
    line-height: var(--line);
    -webkit-font-smoothing: antialiased;
  }

  /* Faint horizontal grain — the ambient texture of the darkroom. */
  body::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    background: radial-gradient(
      120% 80% at 50% -10%,
      color-mix(in srgb, var(--safelight) 7%, transparent),
      transparent 60%
    );
  }

  a {
    color: inherit;
  }

  button {
    font: inherit;
    color: inherit;
  }

  :focus-visible {
    outline: 2px solid var(--safelight);
    outline-offset: 2px;
    border-radius: 1px;
  }
}

@layer layout {
  .frame {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    width: 100%;
    max-width: var(--frame-max);
    height: 100dvh;
    margin: 0 auto;
    padding: 1.75rem 1.25rem 1.25rem;
    /* Bound the slab so text scrolls inside it and the countdown stays put. */
    overflow: hidden;
  }

  /* Too short to bound anything — let the document scroll instead. */
  @media (max-height: 30rem) {
    .frame {
      height: auto;
      min-height: 100dvh;
      overflow: visible;
    }
  }

  .home,
  .paste {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 1rem;
    min-height: 0;
  }

  .gone {
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    text-align: center;
    padding-bottom: 4rem;
  }
}

@layer components {
  /* --- The decay rule: the page's own top edge, spending its lifetime ----- */

  .decay-track {
    position: fixed;
    inset: 0 0 auto;
    z-index: 10;
    height: 2px;
    background: var(--rule);
  }

  .decay {
    height: 100%;
    background: var(--safelight);
    transform: scaleX(1);
    transform-origin: 0 50%;
    will-change: transform;
  }

  .decay-track.is-critical .decay {
    background: var(--critical);
  }

  /* --- Masthead ---------------------------------------------------------- */

  .bar {
    display: flex;
    align-items: baseline;
    gap: 0.875rem;
    flex-wrap: wrap;
  }

  .wordmark {
    font-family: var(--font-display);
    font-size: 1.0625rem;
    font-weight: 700;
    font-variation-settings: "wdth" 88;
    letter-spacing: -0.015em;
    text-decoration: none;
    white-space: nowrap;
  }

  .wordmark::after {
    content: ".";
    color: var(--safelight);
  }

  .tagline {
    margin: 0;
    padding-left: 0.875rem;
    border-left: 1px solid var(--rule);
    font-family: var(--font-display);
    font-size: var(--fs-tagline);
    font-weight: 400;
    color: var(--dim);
  }

  .bar-meta {
    margin: 0 0 0 auto;
    font-size: var(--fs-ui);
    letter-spacing: var(--track);
    text-transform: uppercase;
    color: var(--dim);
  }

  .bar-meta a {
    text-decoration: none;
    border-bottom: 1px solid var(--rule);
  }

  .bar-meta a:hover {
    color: var(--paper);
    border-bottom-color: var(--safelight);
  }

  /* --- Slab: the panel that holds text ----------------------------------- */

  .slab {
    position: relative;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    flex: 1;
    min-height: 8rem;
    background: var(--panel);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
    transition: border-color 180ms ease, box-shadow 180ms ease;
  }

  .slab:focus-within {
    border-color: color-mix(in srgb, var(--safelight) 32%, var(--rule));
    box-shadow: var(--shadow), 0 0 0 3px color-mix(in srgb, var(--safelight) 9%, transparent);
  }

  .gutter {
    padding: var(--pad) 0.75rem var(--pad) 1rem;
    border-right: 1px solid var(--rule-soft);
    background: var(--panel-sunk);
    color: var(--gutter-fg);
    font-family: var(--font-mono);
    font-size: var(--fs-code);
    line-height: var(--line);
    font-variant-numeric: tabular-nums;
    text-align: right;
    white-space: pre;
    overflow: hidden;
    user-select: none;
  }

  .slab--bare .gutter {
    display: none;
  }

  .field,
  .content {
    margin: 0;
    padding: var(--pad) 1.25rem;
    background: transparent;
    color: var(--paper);
    border: 0;
    font-family: var(--font-mono);
    font-size: var(--fs-code);
    line-height: var(--line);
    white-space: pre;
    overflow: auto;
    overscroll-behavior: contain;
    tab-size: 2;
  }

  .field {
    resize: none;
    outline: none;
    caret-color: var(--safelight);
  }

  .field::placeholder {
    color: var(--gutter-fg);
  }

  .field::selection,
  .content::selection {
    background: color-mix(in srgb, var(--safelight) 30%, transparent);
  }

  /* --- Controls ---------------------------------------------------------- */

  .controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
  }

  .label {
    font-size: var(--fs-ui);
    letter-spacing: var(--track);
    text-transform: uppercase;
    color: var(--dim);
    white-space: nowrap;
  }

  .expiry-group {
    display: flex;
    align-items: center;
    gap: 0.75rem;
  }

  .expiry {
    position: relative;
    display: flex;
    padding: 3px;
    background: var(--panel-sunk);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
  }

  .expiry-glide {
    position: absolute;
    top: 3px;
    left: 0;
    bottom: 3px;
    width: 0;
    background: color-mix(in srgb, var(--safelight) 16%, transparent);
    border: 1px solid color-mix(in srgb, var(--safelight) 55%, transparent);
    border-radius: 2px;
    pointer-events: none;
  }

  .expiry button {
    position: relative;
    z-index: 1;
    padding: 0.4rem 0.75rem;
    background: none;
    border: 0;
    border-radius: 2px;
    font-size: var(--fs-ui);
    letter-spacing: var(--track);
    text-transform: uppercase;
    color: var(--dim);
    cursor: pointer;
    transition: color 160ms ease;
  }

  .expiry button:hover {
    color: var(--paper);
  }

  .expiry button[aria-checked="true"] {
    color: var(--safelight);
  }

  /* Size meter */

  .meter {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.375rem;
    min-width: 9.5rem;
  }

  .meter-count {
    font-size: var(--fs-ui);
    letter-spacing: 0.04em;
    color: var(--dim);
    font-variant-numeric: tabular-nums;
  }

  .meter-track {
    width: 100%;
    height: 1px;
    background: var(--rule);
  }

  .meter-fill {
    display: block;
    width: 100%;
    height: 100%;
    background: var(--dim);
    transform: scaleX(0);
    transform-origin: 0 50%;
    transition: transform 200ms ease, background-color 200ms ease;
  }

  .meter.is-near .meter-count,
  .meter.is-near .meter-fill {
    color: var(--safelight);
    background: var(--safelight);
  }

  /* --- Buttons ----------------------------------------------------------- */

  .btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 1rem;
    min-height: 2.75rem;
    background: transparent;
    color: var(--paper);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    font-size: var(--fs-ui);
    letter-spacing: var(--track);
    text-transform: uppercase;
    text-decoration: none;
    cursor: pointer;
    transition: border-color 160ms ease, color 160ms ease, background-color 160ms ease;
  }

  .btn:hover {
    border-color: color-mix(in srgb, var(--safelight) 60%, var(--rule));
    color: var(--safelight);
  }

  .btn.is-done {
    border-color: var(--safelight);
    color: var(--safelight);
  }

  .btn--primary {
    padding: 0.6rem 1.25rem;
    background: var(--safelight);
    border-color: var(--safelight);
    color: #12060a;
    font-weight: 700;
  }

  .btn--primary:hover {
    background: color-mix(in srgb, var(--safelight) 85%, var(--paper));
    border-color: color-mix(in srgb, var(--safelight) 85%, var(--paper));
    color: #12060a;
  }

  .btn--primary:disabled {
    background: transparent;
    border-color: var(--rule);
    color: var(--gutter-fg);
    cursor: not-allowed;
  }

  .submit-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
  }

  .note {
    margin: 0;
    font-size: var(--fs-ui);
    letter-spacing: 0.03em;
    color: var(--dim);
  }

  .error {
    margin: 0;
    min-height: 1.2em;
    font-size: var(--fs-ui);
    letter-spacing: var(--track);
    text-transform: uppercase;
    color: var(--critical);
  }

  /* --- Paste page -------------------------------------------------------- */

  .timer {
    display: flex;
    align-items: baseline;
    gap: 0.75rem;
  }

  .timer-value {
    font-family: var(--font-display);
    font-size: clamp(2.5rem, 9vw, 4.25rem);
    font-weight: 700;
    font-variation-settings: "wdth" 82, "opsz" 48;
    letter-spacing: -0.03em;
    line-height: 1;
    font-variant-numeric: tabular-nums;
    color: var(--paper);
    transition: color 400ms ease;
  }

  .timer-label {
    font-size: var(--fs-ui);
    letter-spacing: var(--track);
    text-transform: uppercase;
    color: var(--dim);
  }

  .paste.is-urgent .timer-value {
    color: var(--safelight);
  }

  .paste.is-critical .timer-value {
    color: var(--critical);
  }

  /* Paste IDs are case-sensitive — never transform them. */
  .slug {
    margin: 0 0 0 auto;
    font-size: var(--fs-ui);
    letter-spacing: 0.04em;
    text-transform: none;
    color: var(--dim);
  }

  .actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
  }

  /* --- Gone page --------------------------------------------------------- */

  .gone h1 {
    margin: 0;
    font-family: var(--font-display);
    font-size: clamp(1.75rem, 7vw, 3rem);
    font-weight: 700;
    font-variation-settings: "wdth" 82;
    letter-spacing: -0.03em;
    line-height: 1.05;
  }

  .gone p {
    margin: 0;
    color: var(--dim);
  }

  .gone .btn {
    margin-top: 1.5rem;
  }
}

@layer motion {
  /* Page-load sequence: masthead, then slab, then controls. */
  .bar,
  .slab,
  .controls,
  .submit-row,
  .timer,
  .actions,
  .gone > * {
    animation: rise 460ms cubic-bezier(0.16, 1, 0.3, 1) backwards;
  }

  .bar,
  .timer {
    animation-delay: 60ms;
  }
  .slab,
  .gone h1 {
    animation-delay: 140ms;
  }
  .controls,
  .actions,
  .gone p {
    animation-delay: 220ms;
  }
  .submit-row,
  .gone .btn {
    animation-delay: 280ms;
  }

  @keyframes rise {
    from {
      opacity: 0;
      transform: translateY(8px);
    }
  }

  .decay {
    animation: unveil 700ms cubic-bezier(0.16, 1, 0.3, 1) backwards;
  }

  @keyframes unveil {
    from {
      transform: scaleX(0);
    }
  }

  /* Nothing left to spend — the track shows, the bar does not. */
  .decay--spent {
    transform: scaleX(0);
    animation: none;
  }

  /* Sliding indicator on the expiration selector. */
  .expiry-glide.is-ready {
    transition: transform 320ms cubic-bezier(0.16, 1, 0.3, 1),
      width 320ms cubic-bezier(0.16, 1, 0.3, 1);
  }

  /* The sealing sweep: covers the round-trip when a paste is created. */
  .scan {
    position: absolute;
    inset: 0;
    pointer-events: none;
    opacity: 0;
  }

  .scan::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent, var(--safelight), transparent);
  }

  .slab.is-sealing {
    border-color: color-mix(in srgb, var(--safelight) 55%, var(--rule));
  }

  .slab.is-sealing .field {
    color: var(--dim);
    pointer-events: none;
  }

  .slab.is-sealing .scan {
    opacity: 1;
    animation: sweep 620ms cubic-bezier(0.4, 0, 0.2, 1) infinite;
  }

  @keyframes sweep {
    from {
      transform: translateY(-2px);
    }
    to {
      transform: translateY(100%);
    }
  }

  /* One-shot pulse along the decay rule, fired on copy. */
  .decay-track.is-pulsing .decay {
    animation: pulse 520ms ease-out;
  }

  @keyframes pulse {
    50% {
      background: var(--paper);
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .bar,
    .slab,
    .controls,
    .submit-row,
    .timer,
    .actions,
    .gone > *,
    .decay {
      animation: none;
    }

    .expiry-glide.is-ready {
      transition: none;
    }

    .slab.is-sealing .scan {
      animation: none;
      opacity: 0;
    }

    .decay-track.is-pulsing .decay {
      animation: none;
    }
  }

  @media (max-width: 34rem) {
    .frame {
      padding: 1.25rem 0.875rem 1rem;
      gap: 1rem;
    }

    .tagline {
      display: none;
    }

    .controls,
    .submit-row {
      align-items: stretch;
      flex-direction: column;
    }

    .meter {
      align-items: stretch;
      min-width: 0;
    }

    .meter-count {
      text-align: right;
    }

    .submit-row .btn--primary {
      justify-content: center;
    }

    .actions .btn {
      flex: 1;
      justify-content: center;
    }
  }
}
