/**
 * Dayly — base layer and core components.
 *
 * Everything here consumes tokens.css. No literal colours, sizes or durations.
 *
 * Layout model:
 *   ≥1024px  left navigation rail + centred, max-width content column
 *   <1024px  full-width content + fixed bottom tab bar
 */

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

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

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--text-body);
  line-height: var(--leading-normal);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, h4, p, figure {
  margin: 0;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}

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

/* Keyboard focus is always visible; pointer focus is not. */
:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--radius-sm);
}

/* Available to screen readers, invisible on screen. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.hidden {
  display: none !important;
}

/* The hidden attribute must beat component display rules — otherwise
 * `.app { display: flex }` leaves the nav focusable behind the auth gate. */
[hidden] {
  display: none !important;
}

/* ---------------------------------------------------------------------------
 * App shell
 * ------------------------------------------------------------------------- */

.app {
  min-height: 100dvh;
  display: flex;
}

/* --- Desktop navigation rail --- */
.rail {
  display: none;
  flex-direction: column;
  gap: var(--space-1);
  width: var(--rail-width);
  flex-shrink: 0;
  padding: var(--space-6) var(--space-3);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100dvh;
}

.rail__brand {
  font-size: var(--text-title);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-display);
  padding: 0 var(--space-3) var(--space-6);
}

.rail__link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font-size: var(--text-small);
  font-weight: var(--weight-medium);
  transition: background var(--duration-fast) var(--ease-out),
              color var(--duration-fast) var(--ease-out);
}

.rail__link:hover {
  background: var(--surface-2);
  color: var(--text);
}

.rail__link[aria-current='page'] {
  background: var(--accent-soft);
  color: var(--accent);
}

.rail__icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.rail__spacer {
  flex: 1;
}

/* --- Content column --- */
.main {
  flex: 1;
  min-width: 0; /* allows children to shrink instead of overflowing */
  padding-bottom: calc(var(--tabbar-height) + env(safe-area-inset-bottom) + var(--space-6));
}

.container {
  width: 100%;
  max-width: var(--content-max);
  margin-inline: auto;
  padding-inline: var(--gutter);
}

/* --- Mobile bottom navigation --- */
.tabbar {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  background: color-mix(in srgb, var(--surface-1) 88%, transparent);
  backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.tabbar__link {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-1);
  /* 44px minimum touch target */
  padding: var(--space-2) 0 var(--space-3);
  min-height: 44px;
  color: var(--text-tertiary);
  font-size: var(--text-caption);
  font-weight: var(--weight-medium);
  transition: color var(--duration-fast) var(--ease-out);
}

.tabbar__link[aria-current='page'] {
  color: var(--accent);
}

@media (min-width: 1024px) {
  .rail { display: flex; }
  .tabbar { display: none; }
  .main { padding-bottom: var(--space-12); }
}

/* ---------------------------------------------------------------------------
 * Page header
 * ------------------------------------------------------------------------- */

.page-header {
  padding-block: var(--space-8) var(--space-6);
}

.page-header__eyebrow {
  color: var(--text-secondary);
  font-size: var(--text-small);
  font-weight: var(--weight-medium);
}

.page-header__title {
  font-size: var(--text-display);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-display);
  line-height: var(--leading-tight);
}

/* ---------------------------------------------------------------------------
 * Card
 * ------------------------------------------------------------------------- */

.card {
  background: var(--surface-1);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
}

.card--interactive {
  width: 100%;
  text-align: left;
  transition: background var(--duration-fast) var(--ease-out),
              transform var(--duration-fast) var(--ease-out);
}

.card--interactive:hover { background: var(--surface-2); }
.card--interactive:active { transform: scale(0.995); }

.card__header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.card__title {
  font-size: var(--text-small);
  font-weight: var(--weight-semibold);
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: var(--tracking-label);
}

/* Responsive card grid — single column on mobile, two up on tablet+. */
.grid {
  display: grid;
  gap: var(--space-4);
  grid-template-columns: 1fr;
  /* Cards size to their own content. Without this, grid stretches every item
   * to the tallest in its row, which left short cards (Water) padded out with
   * dead space to match tall neighbours (Habits). */
  align-items: start;
}

@media (min-width: 640px) {
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid__item--wide { grid-column: span 2; }
}

/* ---------------------------------------------------------------------------
 * Metric — a large value with a secondary label. The number is the content;
 * the label is supporting. Never the other way round.
 * ------------------------------------------------------------------------- */

.metric__value {
  font-size: var(--text-metric);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-display);
  line-height: var(--leading-tight);
  font-variant-numeric: tabular-nums; /* stops digits jittering as values change */
}

.metric__value--display { font-size: var(--text-display); }

.metric__unit {
  font-size: var(--text-small);
  font-weight: var(--weight-medium);
  color: var(--text-secondary);
}

.metric__label {
  font-size: var(--text-small);
  color: var(--text-secondary);
}

/* ---------------------------------------------------------------------------
 * Progress bar — thin, calm, and never red for "over".
 * ------------------------------------------------------------------------- */

.bar {
  height: 6px;
  border-radius: var(--radius-pill);
  background: var(--track);
  overflow: hidden;
}

.bar__fill {
  height: 100%;
  border-radius: var(--radius-pill);
  background: var(--accent);
  transition: width var(--duration-slow) var(--ease-out);
}

/* Exceeding a target is information, not an error — a distinct hatch rather
 * than a danger colour, so it never reads as a failure state. */
.bar__fill--over {
  background-image: repeating-linear-gradient(
    -45deg,
    transparent, transparent 4px,
    rgba(255, 255, 255, 0.25) 4px, rgba(255, 255, 255, 0.25) 8px
  );
}

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 44px;
  padding-inline: var(--space-4);
  border-radius: var(--radius-md);
  font-size: var(--text-small);
  font-weight: var(--weight-semibold);
  transition: background var(--duration-fast) var(--ease-out),
              opacity var(--duration-fast) var(--ease-out);
}

.btn:disabled { opacity: 0.5; cursor: default; }

.btn--primary { background: var(--accent); color: var(--accent-text); }
.btn--primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn--secondary { background: var(--surface-2); color: var(--text); }
.btn--secondary:hover:not(:disabled) { background: var(--surface-3); }

.btn--quiet { color: var(--accent); }
.btn--quiet:hover:not(:disabled) { background: var(--accent-soft); }

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

/* ---------------------------------------------------------------------------
 * Empty state — always says what to do next, never just "no data".
 * ------------------------------------------------------------------------- */

.empty {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-6) 0;
}

.empty__title {
  font-size: var(--text-body);
  font-weight: var(--weight-semibold);
}

.empty__body {
  font-size: var(--text-small);
  color: var(--text-secondary);
  max-width: 42ch;
}

/* ---------------------------------------------------------------------------
 * Sheet — bottom sheet on mobile, centred dialog on desktop. Used for all
 * detailed logging so the dashboard never becomes a form.
 * ------------------------------------------------------------------------- */

/* The dialog fills the viewport and centres its panel with flexbox.
 *
 * Do NOT position the panel absolutely: <dialog> defaults to
 * `height: fit-content`, so an out-of-flow child leaves the dialog zero-height
 * and the panel ends up positioned against nothing. */
.sheet {
  /* <dialog> has a UA rule setting `color: CanvasText`, which resets the
   * inherited colour to black inside the sheet. Restore it explicitly. */
  color: var(--text);
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: none;
  background: transparent;
  /* dialog is display:block when open; we need flex, so drive it off [open]
   * rather than overriding display unconditionally (which would show the
   * sheet while closed). */
  display: none;
}

.sheet[open] {
  display: flex;
  align-items: flex-end;    /* bottom sheet on mobile */
  justify-content: center;
}

.sheet::backdrop {
  background: var(--scrim);
  backdrop-filter: blur(2px);
}

.sheet__panel {
  width: 100%;
  background: var(--surface-1);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  box-shadow: var(--shadow-sheet);
  padding: var(--space-5) var(--space-5) calc(var(--space-6) + env(safe-area-inset-bottom));
  max-height: 88dvh;
  overflow-y: auto;
  animation: sheet-in var(--duration-slow) var(--ease-out);
}

.sheet__handle {
  width: 36px;
  height: 4px;
  border-radius: var(--radius-pill);
  background: var(--border-strong);
  margin: 0 auto var(--space-4);
}

@keyframes sheet-in {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

@keyframes dialog-in {
  from { opacity: 0; transform: scale(0.98); }
  to { opacity: 1; transform: scale(1); }
}

@media (min-width: 640px) {
  .sheet[open] { align-items: center; }

  .sheet__panel {
    width: min(480px, calc(100vw - var(--space-8)));
    border-radius: var(--radius-xl);
    max-height: 86dvh;
    padding: var(--space-6);
    animation: dialog-in var(--duration-base) var(--ease-out);
  }

  /* The drag handle only means something on a bottom sheet. */
  .sheet__handle { display: none; }
}

/* ---------------------------------------------------------------------------
 * Form controls
 * ------------------------------------------------------------------------- */

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.field__label {
  font-size: var(--text-small);
  font-weight: var(--weight-medium);
  color: var(--text-secondary);
}

.input {
  width: 100%;
  min-height: 44px;
  padding: var(--space-3);
  /* Explicit rather than inherited: form controls must never depend on an
   * ancestor's colour surviving a UA reset. */
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  transition: border-color var(--duration-fast) var(--ease-out);
}

.input:focus {
  outline: none;
  border-color: var(--accent);
}
