/* public/css/base.css — M3 shell: reset, typography, layout primitives, tabbar,
   takeover rules, thumb-zone helper, shared list/card/segment/button primitives.
   Colors, z-index, and spacing come ONLY from tokens.css variables (§7, §8 gate 7). */

/* ---------- Reset ---------- */

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

* { margin: 0; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

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

button {
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

input, select, textarea {
  background: var(--surface-2);
  border: 1px solid var(--outline);
  border-radius: var(--radius-sm);
  min-height: var(--tap-min);
  padding: var(--sp-2) var(--sp-3);
}

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

ul, ol { list-style: none; padding: 0; }

h1 { font-size: 22px; font-weight: 700; line-height: 1.2; }
h2 { font-size: 18px; font-weight: 600; line-height: 1.25; }
h3 { font-size: 16px; font-weight: 600; }

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

.text-dim { color: var(--text-dim); }

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

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

/* ---------- Layout: screen + safe areas (standalone PWA, viewport-fit=cover) ---------- */

#screen, .screen {
  display: block;
  min-height: 100vh;
  min-height: 100dvh;
  padding-top: calc(var(--sp-4) + env(safe-area-inset-top, 0px));
  padding-left: calc(var(--sp-4) + env(safe-area-inset-left, 0px));
  padding-right: calc(var(--sp-4) + env(safe-area-inset-right, 0px));
  /* clears the fixed tabbar */
  padding-bottom: calc(var(--tap-primary) + var(--sp-5) + env(safe-area-inset-bottom, 0px));
}

#overlay-root {
  position: fixed;
  inset: 0;
  pointer-events: none;
  /* position:fixed makes this a stacking context, so children's inline
     var(--z-*) values only order INSIDE it: the root itself must sit above
     the sibling tabbar (--z-nav) or the tabbar paints over and steals taps
     from every sheet/numpad/dialog (§7 stack). Children keep the §7 order. */
  z-index: var(--z-sheet);
}

#overlay-root > * { pointer-events: auto; }

/* ---------- Tabbar ---------- */

#tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: var(--z-nav);
  display: flex;
  align-items: stretch;
  background: var(--surface);
  border-top: 1px solid var(--outline);
  padding: var(--sp-1) var(--sp-1) calc(var(--sp-1) + env(safe-area-inset-bottom, 0px));
}

.tabbar-btn {
  flex: 1 1 0;
  min-width: var(--tap-min);
  min-height: calc(var(--tap-min) + var(--sp-2));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--sp-1);
  color: var(--text-dim);
  font-size: 11px;
  font-weight: 600;
  border-radius: var(--radius-sm);
}

.tabbar-btn svg { width: 24px; height: 24px; }

.tabbar-btn.active { color: var(--accent); }

/* ---------- Takeover mode (active workout, R3.2) ---------- */

body.takeover #tabbar { display: none; }

body.takeover #screen, body.takeover .screen {
  min-height: 100vh;
  min-height: 100dvh;
  padding-bottom: calc(var(--sp-4) + env(safe-area-inset-bottom, 0px));
}

/* ---------- Thumb zone helper (R2.1): pins content to the bottom 40% ---------- */

.thumb-anchor {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: var(--thumb-zone);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: var(--sp-3);
  padding: var(--sp-4);
  /* sits above the tabbar when it is visible */
  padding-bottom: calc(var(--tap-min) + var(--sp-2) + var(--sp-4) + env(safe-area-inset-bottom, 0px));
  pointer-events: none;
}

.thumb-anchor > * { pointer-events: auto; }

body.takeover .thumb-anchor {
  padding-bottom: calc(var(--sp-4) + env(safe-area-inset-bottom, 0px));
}

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: var(--tap-min);
  min-width: var(--tap-min);
  padding: var(--sp-2) var(--sp-4);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--outline);
  font-weight: 600;
  user-select: none;
  -webkit-user-select: none;
}

.btn:active { filter: brightness(1.15); }

.btn[disabled] { opacity: 0.5; pointer-events: none; }

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: transparent;
  min-height: var(--tap-primary);
  width: 100%;
  font-size: 18px;
  border-radius: var(--radius);
}

.btn-danger {
  background: var(--danger);
  color: var(--accent-contrast);
  border-color: transparent;
}

/* ---------- List rows ---------- */

.row {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-height: var(--tap-min);
  padding: var(--sp-3) var(--sp-4);
  background: var(--surface);
  border: 1px solid var(--outline);
  border-radius: var(--radius-sm);
}

.row + .row { margin-top: var(--sp-2); }

.row-main { flex: 1 1 auto; min-width: 0; }

.row-sub { color: var(--text-dim); font-size: 14px; }

/* ---------- Cards ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--outline);
  border-radius: var(--radius);
  padding: var(--sp-4);
}

.card + .card { margin-top: var(--sp-3); }

/* ---------- Segment strip (Exercises | Routines, Meals | Water | ... ) ---------- */

.seg {
  display: flex;
  background: var(--surface-2);
  border-radius: var(--radius);
  padding: var(--sp-1);
  gap: var(--sp-1);
}

.seg > button, .seg > a {
  flex: 1 1 0;
  min-height: var(--tap-min);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  font-weight: 600;
  text-decoration: none;
}

.seg > .active, .seg > [aria-selected="true"] {
  background: var(--accent);
  color: var(--accent-contrast);
}

/* ---------- Small utilities ---------- */

.stack > * + * { margin-top: var(--sp-3); }

.ghost { opacity: var(--ghost-opacity); }
