/* ============================================================================
   Mealovate Legendary — Atoms
   ----------------------------------------------------------------------------
   The smallest reusable units: buttons, inputs, badges, tags, labels, the
   typography base, skeleton loaders, spinners. Every rule is scoped under
   `html.legendary` so legacy pages are untouched.

   Rule: no hardcoded color, space, radius, duration, or z-index. If it
   isn't a token from tokens.css, it isn't a value. Add the token first.
   ============================================================================ */

html.legendary {
    /* Base document */
    background: var(--background-color);
    color: var(--text-color);
    font-family: var(--font-body);
    font-size: 100%;
    line-height: var(--leading-normal);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

html.legendary body {
    background: var(--background-color);
    color: var(--text-color);
    font-family: var(--font-body);
    font-weight: var(--weight-regular);
    font-size: var(--text-base);
    line-height: var(--leading-normal);
    margin: 0;
}

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

html.legendary ::selection {
    background: var(--accent-muted);
    color: var(--text-color);
}

/* ---------------------------------------------------------------------------
   Typography atoms
   --------------------------------------------------------------------------- */
html.legendary h1,
html.legendary h2,
html.legendary h3,
html.legendary h4,
html.legendary h5,
html.legendary h6,
html.legendary .ds-display,
html.legendary .ds-heading {
    font-family: var(--font-display);
    font-weight: var(--weight-bold);
    line-height: var(--leading-tight);
    letter-spacing: var(--tracking-tight);
    color: var(--text-color);
    margin: 0;
}

html.legendary h1 { font-size: var(--text-2xl); }
html.legendary h2 { font-size: var(--text-xl); }
html.legendary h3 { font-size: var(--text-lg); }
html.legendary h4 { font-size: var(--text-md); font-family: var(--font-body); }
html.legendary h5 { font-size: var(--text-base); font-family: var(--font-body); font-weight: var(--weight-semibold); }
html.legendary h6 {
    font-size: var(--text-xs);
    font-family: var(--font-body);
    font-weight: var(--weight-semibold);
    text-transform: uppercase;
    letter-spacing: var(--tracking-caps);
    color: var(--text-muted);
}

html.legendary .ds-display-xl { font-size: var(--text-4xl); line-height: var(--leading-tight); letter-spacing: var(--tracking-tight); }
html.legendary .ds-display-lg { font-size: var(--text-3xl); line-height: var(--leading-tight); letter-spacing: var(--tracking-tight); }
html.legendary .ds-display-md { font-size: var(--text-2xl); line-height: var(--leading-snug); }
html.legendary .ds-display-sm { font-size: var(--text-xl); line-height: var(--leading-snug); }

html.legendary .ds-eyebrow {
    font-family: var(--font-body);
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    text-transform: uppercase;
    letter-spacing: var(--tracking-caps);
    color: var(--text-brand);
    line-height: var(--leading-tight);
}

html.legendary .ds-lead {
    font-family: var(--font-body);
    font-size: var(--text-md);
    line-height: var(--leading-relaxed);
    color: var(--text-secondary);
    max-width: var(--width-prose);
}

html.legendary p,
html.legendary .ds-body {
    font-family: var(--font-body);
    font-size: var(--text-base);
    line-height: var(--leading-relaxed);
    color: var(--text-color);
    margin: 0;
}

html.legendary .ds-caption {
    font-family: var(--font-body);
    font-size: var(--text-sm);
    line-height: var(--leading-normal);
    color: var(--text-muted);
}

html.legendary .ds-mono {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
    letter-spacing: var(--tracking-normal);
}

html.legendary a {
    color: var(--text-brand);
    text-decoration-color: var(--accent-muted);
    text-decoration-thickness: 1px;
    text-underline-offset: 3px;
    transition: var(--transition-colors);
}
html.legendary a:hover {
    color: var(--accent-hover);
    text-decoration-color: currentColor;
}
html.legendary a:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
    border-radius: var(--radius-xs);
}

/* ---------------------------------------------------------------------------
   Button — the workhorse atom
   ---------------------------------------------------------------------------
   Variants:  primary | secondary | ghost | subtle | link
   Sizes:     sm (36px) | md (44px default) | lg (52px)
   States:    hover | active | focus-visible | disabled | loading
   Modifiers: .is-block (full width), .is-icon (square icon-only)
   --------------------------------------------------------------------------- */
html.legendary .ds-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    height: var(--space-11);                      /* 44px = iOS min tap target */
    padding: 0 var(--space-5);
    font-family: var(--font-body);
    font-size: var(--text-sm);
    font-weight: var(--weight-semibold);
    letter-spacing: var(--tracking-snug);
    line-height: 1;
    border: 1px solid transparent;
    border-radius: var(--radius-pill);
    background: transparent;
    color: var(--text-color);
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
    user-select: none;
    transition:
        background-color var(--motion-fast) var(--ease-out),
        border-color var(--motion-fast) var(--ease-out),
        color var(--motion-fast) var(--ease-out),
        transform var(--motion-base) var(--ease-spring),
        box-shadow var(--motion-base) var(--ease-spring);
}
html.legendary .ds-button:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}
html.legendary .ds-button:active:not(:disabled):not(.is-loading) {
    transform: scale(0.97);
}
html.legendary .ds-button:disabled,
html.legendary .ds-button[aria-disabled="true"] {
    opacity: 0.48;
    cursor: not-allowed;
    transform: none !important;
}

/* --- variants --- */
html.legendary .ds-button--primary {
    background: var(--accent-color);
    color: var(--accent-contrast);
    border-color: var(--accent-color);
    box-shadow: var(--shadow-sm);
}
html.legendary .ds-button--primary:hover:not(:disabled) {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
    box-shadow: var(--glow-accent);
}
html.legendary .ds-button--primary:active:not(:disabled) {
    background: var(--accent-active);
    border-color: var(--accent-active);
}

html.legendary .ds-button--secondary {
    background: var(--surface-secondary);
    color: var(--text-color);
    border-color: var(--border-strong);
}
html.legendary .ds-button--secondary:hover:not(:disabled) {
    background: var(--surface-tertiary);
    border-color: var(--accent-color);
    color: var(--text-brand);
}

html.legendary .ds-button--ghost {
    background: transparent;
    color: var(--text-color);
    border-color: var(--border-color);
}
html.legendary .ds-button--ghost:hover:not(:disabled) {
    background: var(--surface-secondary);
    border-color: var(--border-strong);
}

html.legendary .ds-button--subtle {
    background: var(--accent-muted);
    color: var(--text-brand);
    border-color: transparent;
}
html.legendary .ds-button--subtle:hover:not(:disabled) {
    background: var(--accent-subtle);
    color: var(--accent-hover);
}

html.legendary .ds-button--link {
    background: transparent;
    color: var(--text-brand);
    border-color: transparent;
    padding-left: var(--space-2);
    padding-right: var(--space-2);
    height: auto;
}
html.legendary .ds-button--link:hover:not(:disabled) {
    color: var(--accent-hover);
    text-decoration: underline;
    text-underline-offset: 3px;
}

/* --- sizes --- */
html.legendary .ds-button--sm {
    height: var(--space-9);         /* 36px */
    padding: 0 var(--space-4);
    font-size: var(--text-xs);
}
html.legendary .ds-button--lg {
    height: calc(var(--space-12) + var(--space-1));  /* 52px */
    padding: 0 var(--space-7);
    font-size: var(--text-base);
}

/* --- modifiers --- */
html.legendary .ds-button.is-block {
    width: 100%;
}
html.legendary .ds-button.is-icon {
    padding: 0;
    width: var(--space-11);
    aspect-ratio: 1;
    flex: 0 0 auto;
}
html.legendary .ds-button.is-icon.ds-button--sm { width: var(--space-9); }
html.legendary .ds-button.is-icon.ds-button--lg { width: calc(var(--space-12) + var(--space-1)); }

html.legendary .ds-button.is-loading {
    position: relative;
    color: transparent !important;
    pointer-events: none;
}
html.legendary .ds-button.is-loading::after {
    content: "";
    position: absolute;
    inset: 50% auto auto 50%;
    width: 1.125em;
    height: 1.125em;
    margin: -0.5625em 0 0 -0.5625em;
    border-radius: 50%;
    border: 2px solid currentColor;
    border-top-color: transparent;
    color: var(--accent-contrast);
    animation: ds-spin 0.7s linear infinite;
}
html.legendary .ds-button--secondary.is-loading::after,
html.legendary .ds-button--ghost.is-loading::after,
html.legendary .ds-button--subtle.is-loading::after,
html.legendary .ds-button--link.is-loading::after {
    color: var(--text-brand);
}

@keyframes ds-spin {
    to { transform: rotate(360deg); }
}

/* ---------------------------------------------------------------------------
   Badge — compact status/metadata chip, not interactive
   --------------------------------------------------------------------------- */
html.legendary .ds-badge {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    padding: 2px var(--space-2);
    font-family: var(--font-body);
    font-size: var(--text-2xs);
    font-weight: var(--weight-semibold);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;
    line-height: 1.4;
    border-radius: var(--radius-sm);
    background: var(--surface-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
}
html.legendary .ds-badge--accent {
    background: var(--accent-muted);
    color: var(--text-brand);
    border-color: transparent;
}
html.legendary .ds-badge--success { background: var(--color-success-bg); color: var(--color-success); border-color: transparent; }
html.legendary .ds-badge--warning { background: var(--color-warning-bg); color: var(--color-warning); border-color: transparent; }
html.legendary .ds-badge--error   { background: var(--color-error-bg);   color: var(--color-error);   border-color: transparent; }
html.legendary .ds-badge--info    { background: var(--color-info-bg);    color: var(--color-info);    border-color: transparent; }

html.legendary .ds-badge--solid.ds-badge--accent {
    background: var(--accent-color);
    color: var(--accent-contrast);
}
html.legendary .ds-badge--outline {
    background: transparent;
    border-color: var(--border-strong);
    color: var(--text-secondary);
}

/* ---------------------------------------------------------------------------
   Tag — slightly larger than badge, can be interactive
   --------------------------------------------------------------------------- */
html.legendary .ds-tag {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-1) var(--space-3);
    font-family: var(--font-body);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    line-height: 1.4;
    border-radius: var(--radius-pill);
    background: var(--surface-secondary);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    text-decoration: none;
    white-space: nowrap;
    transition: var(--transition-colors);
}
html.legendary .ds-tag:hover {
    border-color: var(--accent-color);
    color: var(--text-brand);
}
html.legendary .ds-tag.is-active {
    background: var(--accent-color);
    color: var(--accent-contrast);
    border-color: var(--accent-color);
}
html.legendary .ds-tag__dismiss {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--space-4);
    height: var(--space-4);
    margin-right: calc(-1 * var(--space-1));
    border-radius: var(--radius-pill);
    border: none;
    background: transparent;
    color: inherit;
    cursor: pointer;
    font-size: inherit;
    line-height: 1;
}
html.legendary .ds-tag__dismiss:hover {
    background: rgba(0, 0, 0, 0.12);
}

/* ---------------------------------------------------------------------------
   Form atoms — labels and inputs
   --------------------------------------------------------------------------- */
html.legendary .ds-label {
    display: inline-block;
    font-family: var(--font-body);
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: var(--space-2);
}

html.legendary .ds-input,
html.legendary .ds-textarea {
    display: block;
    width: 100%;
    padding: var(--space-3) var(--space-4);
    font-family: var(--font-body);
    font-size: var(--text-base);
    line-height: var(--leading-normal);
    color: var(--text-color);
    background: var(--surface-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    transition: var(--transition-colors);
    appearance: none;
}
html.legendary .ds-input::placeholder,
html.legendary .ds-textarea::placeholder {
    color: var(--text-muted);
}
html.legendary .ds-input:hover,
html.legendary .ds-textarea:hover {
    border-color: var(--border-strong);
}
html.legendary .ds-input:focus,
html.legendary .ds-textarea:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: var(--focus-ring);
}
html.legendary .ds-input:disabled,
html.legendary .ds-textarea:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}
html.legendary .ds-textarea {
    min-height: calc(var(--space-16) + var(--space-4));
    resize: vertical;
}

html.legendary .ds-input-group {
    position: relative;
    display: flex;
    align-items: center;
}
html.legendary .ds-input-group .ds-input {
    padding-left: var(--space-10);
}
html.legendary .ds-input-group__icon {
    position: absolute;
    left: var(--space-4);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    color: var(--text-muted);
}
html.legendary .ds-input-group__trailing {
    position: absolute;
    right: var(--space-2);
}

/* ---------------------------------------------------------------------------
   Skeleton — loading placeholders that match token shapes
   --------------------------------------------------------------------------- */
html.legendary .ds-skeleton {
    display: block;
    background: linear-gradient(
        90deg,
        var(--surface-secondary) 0%,
        var(--surface-tertiary) 50%,
        var(--surface-secondary) 100%
    );
    background-size: 200% 100%;
    border-radius: var(--radius-sm);
    animation: ds-shimmer 1.6s linear infinite;
}
html.legendary .ds-skeleton--text { height: var(--text-base); border-radius: var(--radius-xs); }
html.legendary .ds-skeleton--heading { height: var(--text-xl); border-radius: var(--radius-sm); }
html.legendary .ds-skeleton--rect { aspect-ratio: 16 / 9; border-radius: var(--radius-lg); }
html.legendary .ds-skeleton--circle { aspect-ratio: 1; border-radius: var(--radius-pill); }
html.legendary .ds-skeleton--avatar-md { width: var(--space-12); height: var(--space-12); border-radius: var(--radius-pill); }

@keyframes ds-shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ---------------------------------------------------------------------------
   Spinner — for inline loading indicators (non-button)
   --------------------------------------------------------------------------- */
html.legendary .ds-spinner {
    display: inline-block;
    width: var(--space-5);
    height: var(--space-5);
    border: 2px solid var(--border-color);
    border-top-color: var(--accent-color);
    border-radius: 50%;
    animation: ds-spin 0.8s linear infinite;
    vertical-align: middle;
}
html.legendary .ds-spinner--sm { width: var(--space-4); height: var(--space-4); border-width: 2px; }
html.legendary .ds-spinner--lg { width: var(--space-8); height: var(--space-8); border-width: 3px; }

/* ---------------------------------------------------------------------------
   Keyboard shortcut display
   --------------------------------------------------------------------------- */
html.legendary .ds-kbd {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: var(--space-5);
    padding: 2px var(--space-2);
    font-family: var(--font-mono);
    font-size: var(--text-2xs);
    font-weight: var(--weight-medium);
    line-height: 1;
    color: var(--text-muted);
    background: var(--surface-secondary);
    border: 1px solid var(--border-color);
    border-bottom-width: 2px;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-xs);
}

/* ---------------------------------------------------------------------------
   Divider — thin horizontal rule
   --------------------------------------------------------------------------- */
html.legendary .ds-divider {
    height: 1px;
    background: var(--divider-color);
    border: none;
    margin: var(--space-8) 0;
}
html.legendary .ds-divider--thick {
    height: 2px;
    background: var(--border-strong);
}

/* ---------------------------------------------------------------------------
   Utility: focus-visible for any focusable element
   ---------------------------------------------------------------------------
   Applied globally inside the legendary scope so every button, link, and
   input gets the same terracotta focus ring without opting in per-component.
   --------------------------------------------------------------------------- */
html.legendary :focus-visible {
    outline: none;
}
html.legendary button:focus-visible,
html.legendary [role="button"]:focus-visible,
html.legendary [tabindex]:focus-visible {
    box-shadow: var(--focus-ring);
    border-radius: var(--radius-sm);
}
