/*!
 * css-continuous-corners
 * Curvature-continuous rounded corners with CSS corner-shape.
 * https://github.com/SeraphicQAQ/css-continuous-corners  (MIT License)
 * Write-up: https://blog.seraphic.top/posts/continuous-corners/
 *
 * border-radius joins a straight edge to a circular arc, so curvature jumps from 0 to 1/r at
 * the tangent point. corner-shape: superellipse(k) with k > 1 lets curvature rise from 0
 * instead. Three tokens control the result:
 *
 *   --corner-k            the k of superellipse(k); 1 is the circle
 *   --corner-scale        radius multiplier that keeps each corner's depth on the 45° diagonal
 *   --concentric-factor   how much an inset element's radius shrinks per unit of gap
 *
 * They default to the circle (1, 1, 1) and only change where corner-shape is supported, so
 * browsers without it render the plain border-radius design at its original radii.
 *
 * Rule of thumb: never set corner-shape on its own. Use the classes below, which always change
 * the radius and the shape together.
 */

/* ---------- Tokens ---------- */

:root {
  /* The design's radii as they were with circular corners. */
  --radius-large: 1.75rem;
  --radius-medium: 1.4rem;
  --radius-block-base: 0.75rem;
  /* Inset elements never go below this radius. */
  --radius-concentric-min: 0.5rem;

  --corner-k: 1;
  --corner-scale: 1;
  --concentric-factor: 1;
}

@supports (corner-shape: superellipse(2)) {
  :root {
    /* superellipse(2), radius × 1.84: about Figma's 100 % corner smoothing. */
    --corner-k: 2;
    --corner-scale: 1.84;
    --concentric-factor: 1.75;
  }

  /* Presets for a part of the page: <section data-corners="ios"> … */
  [data-corners="ios"] {
    /* superellipse(1.3), radius × 1.19: about Figma's 60 % ("iOS") smoothing. Very subtle. */
    --corner-k: 1.3;
    --corner-scale: 1.19;
    --concentric-factor: 1.17;
  }

  [data-corners="soft"] {
    --corner-k: 1.6;
    --corner-scale: 1.43;
    --concentric-factor: 1.39;
  }

  [data-corners="strong"] {
    --corner-k: 2;
    --corner-scale: 1.84;
    --concentric-factor: 1.75;
  }
}

[data-corners="round"] {
  --corner-k: 1;
  --corner-scale: 1;
  --concentric-factor: 1;
}

/*
 * Derived tokens, recomputed on :root and on every [data-corners] scope, so a scope that
 * overrides the three tokens above gets its own shape and radii.
 */
:where(:root, [data-corners]) {
  --corner-shape: superellipse(var(--corner-k));
  --radius-surface: calc(var(--radius-large) * var(--corner-scale));
  --radius-surface-medium: calc(var(--radius-medium) * var(--corner-scale));
  --radius-block: calc(var(--radius-block-base) * var(--corner-scale));
}

/* ---------- Utilities ---------- */

/* Large surfaces: cards, panels, dialogs, page containers, article images. */
.rounded-surface {
  border-radius: var(--radius-surface);
  corner-shape: var(--corner-shape);
}

/* Medium blocks: photos and tiles inside a card. */
.rounded-surface-medium {
  border-radius: var(--radius-surface-medium);
  corner-shape: var(--corner-shape);
}

/* Code blocks and other small blocks of content. */
.rounded-block {
  border-radius: var(--radius-block);
  corner-shape: var(--corner-shape);
}

/*
 * Concentric: an element inset by --inset from the corner of a .rounded-surface.
 * Inner radius = outer radius − gap × factor. The inward offset of a superellipse is not a
 * superellipse, so the plain "− gap" leaves the corner gap up to ~2 px too thick; the factor
 * keeps it within 0.25 px of the edge gap.
 */
.rounded-concentric {
  border-radius: max(
    calc(var(--radius-surface) - var(--inset, 0.75rem) * var(--concentric-factor)),
    var(--radius-concentric-min)
  );
  corner-shape: var(--corner-shape);
}

/* Overlays, hover layers and highlight rings that follow their parent's corner. */
.rounded-inherit {
  border-radius: inherit;
  corner-shape: inherit;
}
