/* ============================================================
   Layout and components. Values come from tokens.css only.
   Direction-agnostic: logical properties throughout so dir="rtl"
   mirrors without a second stylesheet.
   ============================================================ */

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

/* Component rules below set display on elements that are sometimes hidden
   (.field__error is display:block), which would otherwise out-rank the UA
   rule for the hidden attribute and leave an empty error visible. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
}

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

body {
  font-family: var(--font-sans);
  font-size: var(--text-body);
  line-height: var(--lh-body);
  color: var(--c-text);
  background: var(--c-surface);
  min-height: 100vh;
  min-height: 100dvh;
  padding-block-end: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px) + var(--space-4));
  overflow-x: hidden;
}

h1, h2, h3 { margin: 0; font-weight: var(--weight-bold); }
h1 { font-size: var(--text-title); line-height: var(--lh-title); }
h2 { font-size: var(--text-subtitle); line-height: var(--lh-subtitle); }
h3 { font-size: var(--text-body); line-height: var(--lh-body); }
p  { margin: 0; }
ul, ol { margin: 0; padding: 0; list-style: none; }

/* --- Focus: one visible ring everywhere, never removed ------ */
:focus-visible {
  outline: 3px solid var(--c-focus);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}
:focus:not(:focus-visible) { outline: none; }

.skip-link {
  position: absolute;
  inset-inline-start: var(--space-4);
  inset-block-start: calc(-1 * var(--space-16));
  z-index: 100;
  background: var(--c-accent);
  color: var(--c-accent-text);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-sm);
  font-weight: var(--weight-medium);
  text-decoration: none;
  transition: inset-block-start var(--motion-fast) var(--ease-standard);
}
.skip-link:focus { inset-block-start: var(--space-4); }

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

/* --- App shell --------------------------------------------- */
.app-header {
  position: sticky;
  inset-block-start: 0;
  z-index: 20;
  background: var(--c-bg);
  border-block-end: 1px solid var(--c-divider);
  padding-block: var(--space-3);
  padding-inline: var(--space-4);
  padding-block-start: calc(var(--space-3) + env(safe-area-inset-top, 0px));
}
.app-header__inner {
  max-width: var(--content-max);
  margin-inline: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}
.app-title {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-subtitle);
  line-height: var(--lh-subtitle);
}
.app-title svg { flex: none; }

.main {
  max-width: var(--content-max);
  margin-inline: auto;
  padding-inline: var(--space-4);
  padding-block-start: var(--space-4);
}

/* --- Navigation: bottom tab bar on phone, top bar on desktop */
.nav {
  position: fixed;
  inset-inline: 0;
  inset-block-end: 0;
  z-index: 30;
  background: var(--c-bg);
  border-block-start: 1px solid var(--c-divider);
  padding-block-end: env(safe-area-inset-bottom, 0px);
}
.nav__list {
  display: flex;
  max-width: var(--content-max);
  margin-inline: auto;
}
.nav__item { flex: 1 1 0; }
.nav__link {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  min-height: var(--nav-height);
  padding-block: var(--space-2);
  padding-inline: var(--space-1);
  width: 100%;
  border: 0;
  background: none;
  color: var(--c-text-muted);
  font: inherit;
  font-size: var(--text-caption);
  line-height: var(--lh-caption);
  cursor: pointer;
  border-block-start: 3px solid transparent;
  transition: color var(--motion-fast) var(--ease-standard);
}
.nav__link[aria-current="page"] {
  color: var(--c-accent);
  font-weight: var(--weight-bold);
  border-block-start-color: var(--c-accent);
}
.nav__link svg { flex: none; }

/* --- Cards -------------------------------------------------- */
.card {
  background: var(--c-surface-raised);
  border: 1px solid var(--c-divider);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  box-shadow: var(--shadow-1);
  margin-block-end: var(--space-4);
}
.card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-block-end: var(--space-3);
  flex-wrap: wrap;
}
.card__title { font-size: var(--text-subtitle); line-height: var(--lh-subtitle); }

/* --- Buttons ------------------------------------------------ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--target-min);
  min-width: var(--target-min);
  padding-inline: var(--space-4);
  padding-block: var(--space-2);
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  font: inherit;
  font-weight: var(--weight-medium);
  cursor: pointer;
  text-align: center;
  transition: background-color var(--motion-fast) var(--ease-standard),
              transform var(--motion-fast) var(--ease-standard);
}
.btn:active { transform: scale(0.98); }
.btn--primary { background: var(--c-accent); color: var(--c-accent-text); }
.btn--secondary {
  background: var(--c-surface-raised);
  color: var(--c-text);
  border-color: var(--c-border);
}
.btn--danger { background: var(--c-danger); color: var(--c-danger-text); }
.btn--quiet {
  background: none;
  color: var(--c-accent);
  border-color: transparent;
  padding-inline: var(--space-3);
}
.btn--block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn svg { flex: none; }

.btn-row {
  display: flex;
  gap: var(--space-3);
  flex-wrap: wrap;
}
.btn-row--end { justify-content: flex-end; }
/* Destructive actions are kept away from the primary action. */
.btn-row--split { justify-content: space-between; }

/* --- Segmented control (tabs, ranges, language, theme) ------ */
.segmented {
  display: inline-flex;
  border: 1px solid var(--c-border);
  border-radius: var(--radius-full);
  padding: 0;
  gap: var(--space-1);
  background: var(--c-surface-raised);
  max-width: 100%;
  overflow: hidden;
}
/* Each segment is its own target, so it carries the full 44x44 itself
   rather than relying on the group around it. */
.segmented__btn {
  min-height: var(--target-min);
  min-width: var(--target-min);
  padding-inline: var(--space-4);
  border: 0;
  border-radius: var(--radius-full);
  background: none;
  color: var(--c-text-muted);
  font: inherit;
  font-size: var(--text-caption);
  font-weight: var(--weight-medium);
  cursor: pointer;
  white-space: nowrap;
}
.segmented__btn[aria-pressed="true"],
.segmented__btn[aria-checked="true"] {
  background: var(--c-accent);
  color: var(--c-accent-text);
  font-weight: var(--weight-bold);
}
/* Segmented controls sit inside a >=44px row so the tap area stays legal. */
.segmented-row {
  display: flex;
  align-items: center;
  min-height: var(--target-min);
  gap: var(--space-3);
  flex-wrap: wrap;
}

/* --- Forms -------------------------------------------------- */
.field { margin-block-end: var(--space-4); }
.field__label {
  display: block;
  font-size: var(--text-caption);
  line-height: var(--lh-caption);
  font-weight: var(--weight-medium);
  color: var(--c-text);
  margin-block-end: var(--space-1);
}
.field__hint {
  display: block;
  font-size: var(--text-caption);
  line-height: var(--lh-caption);
  color: var(--c-text-muted);
  margin-block-start: var(--space-1);
}
.field__error {
  display: block;
  font-size: var(--text-caption);
  line-height: var(--lh-caption);
  color: var(--c-danger);
  font-weight: var(--weight-medium);
  margin-block-start: var(--space-1);
}
.field__error::before { content: "\26A0\FE0E  "; }

.input, .select, .textarea {
  width: 100%;
  min-height: var(--target-min);
  padding-inline: var(--space-3);
  padding-block: var(--space-2);
  font: inherit;
  font-size: var(--text-body);
  color: var(--c-text);
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-md);
  text-align: start;
}
.input[aria-invalid="true"], .select[aria-invalid="true"] {
  border-color: var(--c-danger);
  border-width: 2px;
}
.select {
  appearance: none;
  padding-inline-end: var(--space-8);
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: right calc(var(--space-4) + 4px) center,
                       right var(--space-4) center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
[dir="rtl"] .select {
  background-position: left var(--space-4) center,
                       left calc(var(--space-4) + 4px) center;
  background-image: linear-gradient(135deg, currentColor 50%, transparent 50%),
                    linear-gradient(45deg, transparent 50%, currentColor 50%);
}
.input[type="date"] { min-width: 0; }
.input[type="number"] { font-variant-numeric: tabular-nums; }

.grid-2 {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0 var(--space-3);
}
.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0 var(--space-3);
}

/* Radio group rendered as chips — each chip is its own 44px target. */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  border: 0;
  padding: 0;
  margin: 0;
}
.chips legend {
  padding: 0;
  font-size: var(--text-caption);
  line-height: var(--lh-caption);
  font-weight: var(--weight-medium);
  margin-block-end: var(--space-2);
}
.chip { position: relative; }
.chip input {
  position: absolute;
  opacity: 0;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}
.chip span {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--target-min);
  padding-inline: var(--space-4);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-full);
  color: var(--c-text);
  background: var(--c-surface-raised);
  font-size: var(--text-caption);
  font-weight: var(--weight-medium);
}
.chip input:checked + span {
  background: var(--c-accent);
  color: var(--c-accent-text);
  border-color: var(--c-accent);
  font-weight: var(--weight-bold);
}
.chip input:checked + span::before { content: "\2713\FE0E"; }
.chip input:focus-visible + span {
  outline: 3px solid var(--c-focus);
  outline-offset: 2px;
}

/* --- Today summary ------------------------------------------ */
.summary {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.summary__figure {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  flex-wrap: wrap;
}
.summary__value {
  font-size: var(--text-display);
  line-height: var(--lh-display);
  font-weight: var(--weight-bold);
  font-variant-numeric: tabular-nums;
}
.summary__of {
  font-size: var(--text-subtitle);
  color: var(--c-text-muted);
  font-variant-numeric: tabular-nums;
}
.summary__status {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding-inline: var(--space-3);
  padding-block: var(--space-2);
  border-radius: var(--radius-full);
  font-size: var(--text-caption);
  font-weight: var(--weight-bold);
  align-self: flex-start;
}
.summary__status--ok   { background: var(--c-accent-soft); color: var(--c-accent); }
.summary__status--over { background: var(--c-danger-soft); color: var(--c-danger); }
.summary__status--none { background: var(--c-warn-soft);   color: var(--c-warn); }

.bar {
  height: 12px;
  border-radius: var(--radius-full);
  background: var(--c-track);
  overflow: hidden;
  display: flex;
}
.bar__fill {
  height: 100%;
  background: var(--c-accent);
  transition: width var(--motion-slow) var(--ease-standard);
}
.bar__fill--over { background: var(--c-danger); }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-3);
}
.stat {
  background: var(--c-surface);
  border: 1px solid var(--c-divider);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  text-align: start;
}
.stat__label {
  display: block;
  font-size: var(--text-caption);
  line-height: var(--lh-caption);
  color: var(--c-text-muted);
}
.stat__value {
  display: block;
  font-size: var(--text-subtitle);
  line-height: var(--lh-subtitle);
  font-weight: var(--weight-bold);
  font-variant-numeric: tabular-nums;
  margin-block-start: var(--space-1);
}

/* --- Date navigator ----------------------------------------- */
.datenav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-block-end: var(--space-4);
}
.datenav__label {
  flex: 1 1 auto;
  text-align: center;
  font-weight: var(--weight-bold);
  font-size: var(--text-subtitle);
}
.datenav__today {
  display: block;
  font-size: var(--text-caption);
  font-weight: var(--weight-regular);
  color: var(--c-text-muted);
}
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--target-min);
  height: var(--target-min);
  border-radius: var(--radius-full);
  border: 1px solid var(--c-border);
  background: var(--c-surface-raised);
  color: var(--c-text);
  cursor: pointer;
  flex: none;
}
.icon-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Directional glyphs. Two separate classes rather than one class plus an
   inline transform: an inline style out-ranks the [dir="rtl"] rule, which
   left the "previous day" chevron pointing the wrong way in Arabic. */
.icon-dir { display: flex; }
[dir="rtl"] .icon-dir { transform: scaleX(-1); }
.icon-dir-back { display: flex; transform: scaleX(-1); }
[dir="rtl"] .icon-dir-back { transform: none; }

/* --- Entry lists -------------------------------------------- */
.group { margin-block-end: var(--space-4); }
.group__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  padding-block-end: var(--space-2);
  border-block-end: 1px solid var(--c-divider);
  margin-block-end: var(--space-2);
}
.group__total {
  font-size: var(--text-caption);
  color: var(--c-text-muted);
  font-variant-numeric: tabular-nums;
}

.entry {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding-block: var(--space-2);
  border-block-end: 1px solid var(--c-divider);
}
.entry:last-child { border-block-end: 0; }
.entry__main { flex: 1 1 auto; min-width: 0; }
.entry__name {
  font-weight: var(--weight-medium);
  overflow-wrap: anywhere;
}
.entry__meta {
  font-size: var(--text-caption);
  line-height: var(--lh-caption);
  color: var(--c-text-muted);
  font-variant-numeric: tabular-nums;
}
.entry__kcal {
  font-weight: var(--weight-bold);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.entry__actions { display: flex; gap: var(--space-1); flex: none; }

/* --- Empty / loading / error states -------------------------- */
.state {
  text-align: center;
  padding-block: var(--space-8);
  padding-inline: var(--space-4);
  color: var(--c-text-muted);
}
.state__icon { color: var(--c-text-muted); margin-block-end: var(--space-3); }
.state__title {
  color: var(--c-text);
  font-weight: var(--weight-bold);
  font-size: var(--text-subtitle);
  margin-block-end: var(--space-1);
}
.state__body { margin-block-end: var(--space-4); }
.state--error {
  text-align: start;
  background: var(--c-danger-soft);
  border: 1px solid var(--c-danger);
  border-radius: var(--radius-md);
  padding: var(--space-4);
  color: var(--c-text);
}
.state--error .state__title { color: var(--c-danger); }
.state--warn {
  text-align: start;
  background: var(--c-warn-soft);
  border: 1px solid var(--c-warn);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  color: var(--c-text);
  margin-block-end: var(--space-4);
  font-size: var(--text-caption);
  line-height: var(--lh-caption);
}

.skeleton {
  background: var(--c-track);
  border-radius: var(--radius-sm);
  animation: pulse 1.4s var(--ease-standard) infinite;
}
.skeleton--line { height: 16px; margin-block-end: var(--space-2); }
.skeleton--title { height: 28px; width: 60%; margin-block-end: var(--space-3); }
.skeleton--block { height: 180px; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.55; } }

/* --- Charts -------------------------------------------------- */
.chart-wrap { inline-size: 100%; }
.chart { display: block; inline-size: 100%; height: auto; touch-action: pan-y; }
.chart__grid   { stroke: var(--c-divider); stroke-width: 1; }
.chart__axis   { fill: var(--c-text-muted); font-size: 12px; font-family: var(--font-sans); }
.chart__line   { fill: none; stroke: var(--c-accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart__trend  { fill: none; stroke: var(--c-text-muted); stroke-width: 2; stroke-dasharray: 6 5; }
.chart__area   { fill: var(--c-accent); opacity: 0.14; }
.chart__dot    { fill: var(--c-accent); stroke: var(--c-surface-raised); stroke-width: 2; }
.chart__bar    { fill: var(--c-accent); }
.chart__bar--over { fill: var(--c-danger); }
.chart__target { stroke: var(--c-warn); stroke-width: 2; stroke-dasharray: 4 4; fill: none; }
.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-block-start: var(--space-2);
  font-size: var(--text-caption);
  color: var(--c-text-muted);
}
.chart-legend__item { display: inline-flex; align-items: center; gap: var(--space-2); }
.chart-legend__swatch { inline-size: 16px; block-size: 4px; border-radius: 2px; flex: none; }

/* --- Tables --------------------------------------------------- */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table {
  inline-size: 100%;
  border-collapse: collapse;
  font-size: var(--text-caption);
  line-height: var(--lh-caption);
}
.table caption {
  text-align: start;
  color: var(--c-text-muted);
  padding-block-end: var(--space-2);
}
.table th, .table td {
  text-align: start;
  padding: var(--space-2) var(--space-3);
  border-block-end: 1px solid var(--c-divider);
  white-space: nowrap;
}
.table th { font-weight: var(--weight-bold); color: var(--c-text); }
.table td { font-variant-numeric: tabular-nums; }

/* --- Dialog ---------------------------------------------------- */
dialog.sheet {
  border: 0;
  padding: 0;
  background: transparent;
  max-width: 100%;
  max-height: 100%;
  inline-size: 100%;
  margin: 0;
  margin-block-start: auto;
  color: var(--c-text);
}
dialog.sheet::backdrop { background: var(--c-overlay); }
.sheet__panel {
  background: var(--c-surface-raised);
  border-start-start-radius: var(--radius-lg);
  border-start-end-radius: var(--radius-lg);
  padding: var(--space-4);
  padding-block-end: calc(var(--space-4) + env(safe-area-inset-bottom, 0px));
  box-shadow: var(--shadow-3);
  max-height: 88vh;
  max-height: 88dvh;
  overflow-y: auto;
}
.sheet__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-block-end: var(--space-4);
}

/* --- Toast ----------------------------------------------------- */
.toast-region {
  position: fixed;
  inset-inline: var(--space-4);
  inset-block-end: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px) + var(--space-4));
  z-index: 40;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  align-items: center;
  pointer-events: none;
}
.toast {
  background: var(--c-text);
  color: var(--c-bg);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-2);
  font-size: var(--text-caption);
  font-weight: var(--weight-medium);
  max-width: var(--content-max);
}

/* --- Misc ------------------------------------------------------ */
.muted { color: var(--c-text-muted); }
.caption { font-size: var(--text-caption); line-height: var(--lh-caption); }
.num { font-variant-numeric: tabular-nums; }
.stack > * + * { margin-block-start: var(--space-3); }
.divider { block-size: 1px; background: var(--c-divider); margin-block: var(--space-4); border: 0; }
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding-inline: var(--space-2);
  padding-block: 2px;
  border-radius: var(--radius-sm);
  font-size: var(--text-caption);
  font-weight: var(--weight-medium);
  background: var(--c-surface);
  border: 1px solid var(--c-divider);
  color: var(--c-text-muted);
}
.badge--accent { background: var(--c-accent-soft); color: var(--c-accent); border-color: transparent; }
.badge--danger { background: var(--c-danger-soft); color: var(--c-danger); border-color: transparent; }

/* --- Breakpoints: sm 640 / md 768 / lg 1024 / xl 1280 ---------- */
@media (min-width: 640px) {
  .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .main { padding-inline: var(--space-6); }
}

@media (min-width: 768px) {
  body { padding-block-end: var(--space-16); }
  .nav {
    position: sticky;
    inset-block-start: 0;
    inset-block-end: auto;
    border-block-start: 0;
    border-block-end: 1px solid var(--c-divider);
    padding-block-end: 0;
  }
  .nav__list { justify-content: flex-start; gap: var(--space-2); padding-inline: var(--space-6); }
  .nav__item { flex: 0 0 auto; }
  .nav__link {
    flex-direction: row;
    gap: var(--space-2);
    min-height: 56px;
    padding-inline: var(--space-4);
    font-size: var(--text-body);
    border-block-start: 0;
    border-block-end: 3px solid transparent;
  }
  .nav__link[aria-current="page"] { border-block-end-color: var(--c-accent); }
  dialog.sheet {
    inline-size: min(560px, calc(100% - var(--space-8)));
    margin: auto;
  }
  .sheet__panel { border-radius: var(--radius-lg); }
  .toast-region { inset-block-end: var(--space-6); }
}

@media (min-width: 1024px) {
  :root { --content-max: 880px; }
}

@media (min-width: 1280px) {
  :root { --content-max: 1040px; }
  .main { padding-inline: var(--space-8); }
}

/* Rule 12: honour the OS reduced-motion setting. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .skeleton { animation: none; opacity: 0.85; }
}

/* Forced-colors (Windows high contrast): keep boundaries visible. */
@media (forced-colors: active) {
  .card, .input, .select, .chip span, .btn { border: 1px solid ButtonBorder; }
  .nav__link[aria-current="page"] { border-block-start-color: Highlight; }
}
