/* ============================================================================
   Life Command Center — Base layer
   Reset, native-app ergonomics, and shared primitives.
   ========================================================================== */

@layer base {

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

  * { margin: 0; padding: 0; }

  html {
    /* Stops iOS from re-scaling text when the device rotates. */
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;

    /* Removes the grey flash iOS/Android paint over every tapped element.
       Feedback is provided deliberately by :active states instead — leaving
       this on is one of the loudest "this is a web page" tells. */
    -webkit-tap-highlight-color: transparent;

    height: 100%;
  }

  body {
    min-height: 100dvh;
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: var(--leading-normal);
    color: var(--text-primary);
    background: var(--surface-0);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;

    /* Kills the rubber-band scroll that reveals a blank page behind the app —
       the single clearest giveaway that a PWA is a browser view. */
    overscroll-behavior-y: none;

    /* App chrome is not selectable; long-pressing a tab bar should not pop a
       text selection handle. Content areas opt back in below. */
    -webkit-user-select: none;
    user-select: none;
  }

  /* Anything the user might legitimately want to copy. */
  .selectable,
  input, textarea,
  .note-body, .journal-body, .card-prose {
    -webkit-user-select: text;
    user-select: text;
  }

  /* Removes the 300ms click delay without disabling pinch-zoom entirely. */
  a, button, label, summary, [role="button"], input, select, textarea {
    touch-action: manipulation;
  }

  img, svg, video, canvas { display: block; max-width: 100%; }

  button, input, select, textarea {
    font: inherit;
    color: inherit;
    background: none;
    border: none;
  }

  button { cursor: pointer; }

  a { color: var(--accent); text-decoration: none; }

  ul, ol { list-style: none; }

  h1, h2, h3, h4 {
    line-height: var(--leading-tight);
    letter-spacing: var(--tracking-tight);
    font-weight: var(--weight-semi);
    text-wrap: balance;
  }

  /* Numbers must not jitter as they animate or update. */
  .tnum, .metric, time, [data-metric] {
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum" 1;
  }

  /* ------------------------------------------------- user-content wrapping --
     Anything that renders text the USER typed must be able to break mid-word.
     A single long unbroken string — a pasted URL, a German compound, someone
     leaning on the keyboard — otherwise forces the whole page to scroll
     sideways on a phone, because `text-wrap: pretty` will not break a word
     that has no break opportunity in it.

     `min-width: 0` on the flex/grid children matters just as much: a flex item
     defaults to `min-width: auto`, which refuses to shrink below its content,
     so the wrapping rule alone would not save the layout. */
  .focus__title,
  .timeline__title, .timeline__where,
  .next-up__title, .next-up__where,
  .check-row__label, .check-row__detail,
  .goal__title, .goal__detail,
  .workout-row__title, .workout-row__detail,
  .config-row__label,
  .coach-card__headline, .coach-card__body,
  .sleep__note, .sleep__verdict,
  .journal-entry__body,
  .stat-tile__label,
  .toast__message,
  .sheet__title, .sheet-message {
    overflow-wrap: anywhere;
    word-break: normal;
  }

  .focus__toggle, .focus__add,
  .timeline__row, .timeline__body,
  .next-up, .next-up__meta,
  .check-row, .check-row__text,
  .workout-row, .workout-row__main, .workout-row__text,
  .config-row, .config-row__main,
  .goal, .goal__head,
  .card__head, .appbar__lead {
    min-width: 0;
  }

  /* Focus rings appear for keyboard users only — a visible ring after every
     finger tap looks broken on mobile. */
  :focus { outline: none; }

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

  ::selection {
    background: var(--accent-subtle);
    color: var(--text-primary);
  }

  ::placeholder { color: var(--text-tertiary); opacity: 1; }

  /* Scrollbars: invisible on touch, slim and themed on desktop. */
  @media (pointer: fine) {
    * { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
    ::-webkit-scrollbar { width: 10px; height: 10px; }
    ::-webkit-scrollbar-track { background: transparent; }
    ::-webkit-scrollbar-thumb {
      background: var(--border-strong);
      border: 3px solid transparent;
      border-radius: var(--radius-full);
      background-clip: content-box;
    }
  }

  .visually-hidden {
    position: absolute;
    width: 1px; height: 1px;
    padding: 0; margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
  }
}

/* ==========================================================================
   Shared primitives
   ========================================================================== */

@layer components {

  /* ------------------------------------------------------------------ card
     A hairline border alongside the shadow. On a light background a
     shadow-only card has no defined edge and the whole page reads as soft
     grey mush; the border is what makes it look drawn rather than blurred.
     In dark mode --elev-1 already supplies a ring, so the border is
     effectively invisible there. */
  /* --card-pad is published so descendants can bleed to the card edge (a
     horizontally scrolling row, a full-bleed divider) with a negative margin
     that stays correct at every breakpoint. */
  .card {
    --card-pad: var(--space-5);

    background: var(--surface-1);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-xl);
    box-shadow: var(--elev-1);
    padding: var(--card-pad);
  }

  @media (min-width: 768px) {
    .card { --card-pad: var(--space-6); }
  }

  /* ---------------------------------------------------------------- button
     Every button clears the 44px minimum. The primary action is 52px on
     touch devices because it is the one target that must never be missed. */
  .btn {
    --btn-bg: var(--surface-2);
    --btn-fg: var(--text-primary);

    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);

    min-height: var(--tap-min);
    padding-inline: var(--space-5);

    font-size: var(--text-base);
    font-weight: var(--weight-semi);
    letter-spacing: var(--tracking-normal);

    color: var(--btn-fg);
    background: var(--btn-bg);
    border-radius: var(--radius-lg);

    transition:
      transform var(--dur-instant) var(--ease-out),
      background-color var(--dur-fast) var(--ease-out),
      opacity var(--dur-fast) var(--ease-out),
      box-shadow var(--dur-fast) var(--ease-out);
  }

  /* The press state IS the haptic on iOS, which gives web apps no vibration
     API. It has to be immediate and physical. */
  .btn:active:not(:disabled) { transform: scale(0.965); }

  .btn:disabled { opacity: 0.45; cursor: not-allowed; }

  .btn--primary {
    --btn-bg: var(--accent);
    --btn-fg: var(--text-on-accent);
    box-shadow: var(--elev-1);
  }
  .btn--primary:hover:not(:disabled) { --btn-bg: var(--accent-hover); }

  .btn--ghost { --btn-bg: transparent; }
  .btn--ghost:hover:not(:disabled) { --btn-bg: var(--surface-2); }

  .btn--danger {
    --btn-bg: var(--danger);
    --btn-fg: light-dark(oklch(100% 0 0), oklch(14% 0.015 265));
  }

  .btn--block { display: flex; width: 100%; }

  .btn--lg { min-height: 3.25rem; font-size: var(--text-md); }

  /* Icon-only buttons keep a 44px hit area while looking smaller, by
     expanding the target with a pseudo-element instead of padding. */
  .btn--icon {
    padding-inline: 0;
    width: 2.25rem;
    min-height: 2.25rem;
    border-radius: var(--radius-full);
    position: relative;
  }
  .btn--icon::after {
    content: "";
    position: absolute;
    inset: 50% auto auto 50%;
    translate: -50% -50%;
    width: var(--tap-min);
    height: var(--tap-min);
  }

  .btn__spinner {
    width: 1.05rem;
    height: 1.05rem;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: var(--radius-full);
    animation: spin 620ms linear infinite;
  }

  @keyframes spin { to { rotate: 360deg; } }

  /* ----------------------------------------------------------------- field */
  .field { display: grid; gap: var(--space-2); }

  .field__label {
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--text-secondary);
    padding-inline-start: var(--space-1);
  }

  .field__control { position: relative; display: flex; align-items: center; }

  .input {
    width: 100%;
    min-height: 3.25rem;
    padding: var(--space-3) var(--space-4);

    /* 16px minimum. Anything smaller makes iOS Safari zoom the viewport on
       focus, and the app never zooms back out cleanly. */
    font-size: var(--text-md);

    color: var(--text-primary);
    background: var(--surface-2);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);

    transition:
      border-color var(--dur-fast) var(--ease-out),
      box-shadow var(--dur-fast) var(--ease-out),
      background-color var(--dur-fast) var(--ease-out);
  }

  .input:focus {
    outline: none;
    background: var(--surface-1);
    border-color: var(--accent);
    box-shadow: 0 0 0 4px var(--accent-ring);
  }

  .input[aria-invalid="true"] {
    border-color: var(--danger);
    box-shadow: 0 0 0 4px light-dark(oklch(56% 0.195 25 / 0.2), oklch(71% 0.155 25 / 0.24));
  }

  .input--with-affix { padding-inline-end: 3.25rem; }

  /* Trailing control inside an input (password reveal). */
  .field__affix {
    position: absolute;
    inset-inline-end: var(--space-2);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--tap-min);
    height: var(--tap-min);
    color: var(--text-tertiary);
    border-radius: var(--radius-md);
    transition: color var(--dur-fast) var(--ease-out),
                background-color var(--dur-fast) var(--ease-out);
  }
  .field__affix:hover { color: var(--text-secondary); background: var(--surface-3); }
  .field__affix:active { transform: scale(0.92); }

  .field__error {
    font-size: var(--text-sm);
    color: var(--danger);
    padding-inline-start: var(--space-1);
  }

  /* ---------------------------------------------------------------- switch
     Native-styled toggle. The whole row is the target, so the switch itself
     does not have to be finger-sized. */
  .switch-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    min-height: var(--tap-min);
    cursor: pointer;
  }

  .switch-row__label {
    font-size: var(--text-base);
    color: var(--text-secondary);
  }

  .switch {
    position: relative;
    flex: none;
    width: 3.125rem;
    height: 1.875rem;
    background: var(--surface-3);
    border-radius: var(--radius-full);
    transition: background-color var(--dur-base) var(--ease-out);
  }

  .switch::after {
    content: "";
    position: absolute;
    top: 3px;
    inset-inline-start: 3px;
    width: 1.5rem;
    height: 1.5rem;
    background: light-dark(oklch(100% 0 0), oklch(94% 0.004 260));
    border-radius: var(--radius-full);
    box-shadow: 0 1px 3px oklch(20% 0.02 265 / 0.3);
    transition: translate var(--dur-base) var(--ease-spring);
  }

  .switch__input { position: absolute; opacity: 0; pointer-events: none; }

  .switch__input:checked + .switch { background: var(--accent); }
  .switch__input:checked + .switch::after { translate: 1.25rem 0; }
  .switch__input:focus-visible + .switch {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }

  /* ---------------------------------------------------------------- banner */
  .banner {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    font-size: var(--text-sm);
    line-height: var(--leading-snug);
    border-radius: var(--radius-lg);
  }

  .banner--danger  { background: var(--danger-subtle);  color: var(--danger); }
  .banner--warn    { background: var(--warn-subtle);    color: var(--warn); }
  .banner--success { background: var(--success-subtle); color: var(--success); }
  .banner--info    { background: var(--info-subtle);    color: var(--info); }

  .banner__icon { flex: none; margin-top: 0.1em; }

  /* A short, sharp shake reads as "wrong" faster than any colour change. */
  .shake { animation: shake 380ms var(--ease-out); }

  @keyframes shake {
    0%, 100% { translate: 0; }
    18%  { translate: -6px; }
    38%  { translate:  5px; }
    58%  { translate: -3px; }
    78%  { translate:  2px; }
  }
}
