/* AX: ROLE=app-styles | HANDLES=semantic tokens mapped from Hyperstudio (app/design), layout, components | RELATED=app/index.html,app/app.js,app/design/variables.css,app/design/DESIGN.md | SIG=3 */

/* JetBrains Mono (SIL OFL, pinned) for Latin letters, digits and symbols only. */
@font-face {
  font-family: "Meta Mono";
  src: url("https://cdn.jsdelivr.net/npm/@fontsource/jetbrains-mono@5.3.0/files/jetbrains-mono-latin-400-normal.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
  unicode-range: U+0021-007E, U+00B7, U+2013-2014, U+2026;
}

/* ==========================================================================
   Semantic tokens. Raw values live in design/variables.css (Hyperstudio,
   dark-only). To re-skin, change the mapping below — components only use
   these names.

   Deviations from DESIGN.md (health-app needs the spec doesn't cover):
   - --warn / --crit: status dots for coaching alerts (never fills, always with
     a text label). Values from the dataviz status palette.
   - --live: Pulse Green, used only for "saved / online" live status, as the
     spec allows.
   - Chart stays monochrome per the spec: trend = Chalk line, readings = Smoke
     dots. Series identity is carried by mark shape + legend + table view.
   - Fonts: Aeonik/Input are licensed. Body = Pretendard (built on Inter, the
     spec's Aeonik substitute, with Korean from Source Han Sans). Meta/labels =
     JetBrains Mono (a listed Input substitute) for Latin and digits; Hangul in
     labels falls back to Pretendard, since monospace Hangul reads poorly in UI.
     The mono face is limited to visible ASCII (no U+0020) so word spaces in
     Korean labels come from Pretendard instead of a wide monospace space.
   ========================================================================== */
:root {
  color-scheme: dark;
  --bg: var(--color-obsidian);
  --surface: var(--color-carbon);
  --surface-raised: #1a1a1a; /* status-badge fill from DESIGN.md */
  --text: var(--color-chalk);
  --text-2: var(--color-smoke);
  --text-3: #6f6f6f;
  --line: var(--color-graphite);
  --line-strong: var(--color-iron);
  --icon: var(--color-chalk);
  --icon-accent: var(--color-compass-gold);
  --action: var(--color-signal-white);
  --action-ink: var(--color-obsidian);
  --series-trend: var(--color-chalk);
  --series-point: var(--color-smoke);
  --live: #98ff38;
  --warn: #fab219;
  --crit: #d03b3b;

  --font: "Pretendard Variable", Pretendard, -apple-system, BlinkMacSystemFont, system-ui, "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  --font-meta: "Meta Mono", "Pretendard Variable", Pretendard, "Apple SD Gothic Neo", sans-serif;
  --r-tag: var(--radius-tags);
  --r-card: var(--radius-cards);
  --gap: var(--spacing-12);
  --tap: 44px; /* minimum touch target */
}

/* ==========================================================================
   Base
   ========================================================================== */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--text-body);
  font-weight: 400;
  line-height: 1.43;
  font-feature-settings: "ss01" on, "cv11" on;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  word-break: keep-all; /* Korean: break between words, not inside them */
  overflow-wrap: anywhere;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
strong, b, h1, h2, h3 { font-weight: 400; } /* weight 400 everywhere — scale carries hierarchy */
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-2); }
.small { font-size: 14px; }
.meta { font-family: var(--font-meta); font-size: 13px; letter-spacing: -0.022em; color: var(--text-2); }
.hidden { display: none !important; }
svg.i { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }

:focus-visible { outline: 1px solid var(--text); outline-offset: 3px; }

/* Header */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + env(safe-area-inset-top)) 20px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: var(--text-heading-xs); margin: 0; }
.topbar .right { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  min-width: var(--tap); min-height: var(--tap);
  border: 0; background: transparent; border-radius: var(--radius-icons);
  display: grid; place-items: center; color: var(--icon);
}
.icon-btn:disabled { opacity: .3; }

/* Status badge (DESIGN.md "Status Badge") */
.badge {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--surface-raised); border: 1px solid var(--line); border-radius: var(--r-tag);
  padding: 6px 12px; font-family: var(--font-meta); font-size: 12px; color: var(--text-2);
}
.badge .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-3); }
.badge.live .dot { background: var(--live); }

main { max-width: 560px; margin: 0 auto; padding: 16px 20px 0; }
.view { display: none; }
.view.active { display: block; }

/* Cards: hairline only, no shadow, 8px max radius */
.card {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  padding: 20px;
  margin-bottom: var(--gap);
}
.card h2 { margin: 0 0 12px; }
.section-label { font-family: var(--font-meta); font-size: 13px; letter-spacing: -0.022em; color: var(--text-2); }

/* Bottom tab bar — primary navigation in thumb reach */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--bg);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  min-height: 60px; border: 0; background: transparent;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  color: var(--text-3); font-size: 12px;
}
.tabbar button[aria-selected="true"] { color: var(--text); }

/* Date navigator */
.datenav { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--gap); }
.datenav .label { font-size: var(--text-heading-sm); letter-spacing: -0.2px; }

/* Field rows */
.row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 56px; border-bottom: 1px solid var(--line);
}
.row:last-child { border-bottom: 0; }
.row .hint { display: block; font-family: var(--font-meta); font-size: 12px; color: var(--text-3); margin-top: 2px; }

.stepper { display: flex; align-items: center; gap: 4px; }
.stepper button {
  width: var(--tap); height: var(--tap); border-radius: var(--radius-icons);
  border: 1px solid var(--line-strong); background: transparent; color: var(--icon);
  display: grid; place-items: center;
}
.stepper input {
  width: 76px; text-align: center; font-size: var(--text-heading-xs);
  border: 0; background: transparent; -moz-appearance: textfield;
}
.stepper input::placeholder { color: var(--text-3); }
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* Selectors: outlined tags; selected = Chalk outline + text (no colored fill) */
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button, .chip {
  min-height: 36px; min-width: 40px; padding: 0 12px;
  border-radius: var(--r-tag); border: 1px solid var(--line); background: transparent; color: var(--text-2);
}
.seg button[aria-pressed="true"], .chip[aria-pressed="true"] { border-color: var(--text); color: var(--text); background: var(--surface-raised); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 12px 0 4px; }

.switch { width: 52px; height: 30px; border-radius: 999px; border: 1px solid var(--line-strong); background: transparent; position: relative; }
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px;
  border-radius: 50%; background: var(--text-2); transition: transform .15s;
}
.switch[aria-checked="true"] { background: var(--action); border-color: var(--action); }
.switch[aria-checked="true"]::after { transform: translateX(22px); background: var(--action-ink); }

/* Coaching alerts: status dot + label + action text, never a colored fill */
.alert {
  display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; align-items: baseline;
  padding: 12px 14px; border-radius: var(--r-card);
  border: 1px solid var(--line); margin-bottom: 6px;
}
.alert .tag { display: inline-flex; align-items: center; gap: 6px; font-family: var(--font-meta); font-size: 12px; color: var(--text-2); }
.alert .tag::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--warn); }
.alert.critical { border-color: var(--line-strong); }
.alert.critical .tag::before { background: var(--crit); }
.alert .title { font-size: var(--text-body); }
.alert .body { grid-column: 2; color: var(--text-2); font-size: 13px; }

/* Stats */
.hero-num { font-size: var(--text-heading-lg); line-height: var(--leading-heading-lg); letter-spacing: var(--tracking-heading-lg); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--line); border-radius: var(--r-card); }
.stat { padding: 14px 12px; }
.stat + .stat { border-left: 1px solid var(--line); }
.stat .v { font-size: var(--text-heading-sm); letter-spacing: -0.2px; }
.stat .k { font-family: var(--font-meta); font-size: 12px; color: var(--text-2); margin-top: 4px; }

.ring-wrap { display: flex; align-items: center; gap: 20px; }

.bar { height: 6px; border-radius: 999px; background: var(--line); position: relative; overflow: hidden; }
.bar > i { position: absolute; inset: 0 auto 0 0; border-radius: 999px; background: var(--text); }
.bar > b { position: absolute; top: -4px; bottom: -4px; width: 1px; background: var(--text-2); }

/* Chart */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; touch-action: pan-y; }
.legend { display: flex; gap: 16px; font-family: var(--font-meta); font-size: 12px; color: var(--text-2); margin-bottom: 8px; }
.legend i { display: inline-block; width: 14px; height: 2px; vertical-align: middle; margin-right: 6px; }
.legend i.dot { width: 8px; height: 8px; border-radius: 50%; }
.tooltip {
  position: absolute; top: 0; pointer-events: none;
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--r-tag);
  padding: 8px 10px; font-size: 13px; white-space: nowrap;
}
.tooltip strong { font-size: 15px; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th { font-family: var(--font-meta); font-weight: 400; color: var(--text-2); font-size: 12px; }
table.data th, table.data td { text-align: right; padding: 8px 4px; border-bottom: 1px solid var(--line); }
table.data th:first-child, table.data td:first-child { text-align: left; }

/* Buttons: primary = white pill; secondary = ghost outline, 8px radius */
.btn {
  min-height: var(--tap); padding: 10px 20px; border-radius: var(--r-card);
  border: 1px solid var(--action); background: transparent; color: var(--action);
  font-size: 14px; letter-spacing: 0.02em;
}
.btn.primary { background: var(--action); color: var(--action-ink); border-radius: 9999px; padding: 12px 24px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; }
.link { border: 0; background: none; color: var(--text); padding: 0; min-height: 0; text-decoration: underline; text-underline-offset: 3px; font-size: 13px; }

textarea.field, input.field {
  width: 100%; border-radius: var(--r-tag); border: 1px solid var(--line); background: transparent; padding: 10px 12px;
}
textarea.field::placeholder, input.field::placeholder { color: var(--text-3); }

/* Diet */
.photo-preview { width: 100%; border-radius: var(--r-card); display: block; margin-bottom: 12px; border: 1px solid var(--line); }
.guide { font-size: var(--text-heading-xs); line-height: 1.45; white-space: pre-wrap; margin: 0; }
.est-items { margin: 12px 0 0; padding: 0; list-style: none; font-size: 14px; }
.est-items li { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--line); color: var(--text-2); }
.est-items li span:last-child { color: var(--text); }

/* Chat: no colored bubbles — outline for user, Carbon for coach */
.chat { display: flex; flex-direction: column; gap: 10px; padding-bottom: 140px; }
.msg { max-width: 88%; padding: 12px 14px; border-radius: var(--r-card); white-space: pre-wrap; }
.msg.user { align-self: flex-end; border: 1px solid var(--line-strong); }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--line); }
.msg .by { display: block; font-family: var(--font-meta); font-size: 11px; color: var(--text-3); margin-top: 6px; }
.composer {
  position: fixed; left: 0; right: 0; bottom: calc(60px + env(safe-area-inset-bottom));
  background: var(--bg); padding: 10px 20px; border-top: 1px solid var(--line);
}
.composer .inner { max-width: 560px; margin: 0 auto; }
.composer form { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; min-height: var(--tap); max-height: 120px; }

/* Settings dialog */
dialog {
  border: 1px solid var(--line-strong); border-radius: var(--r-card); padding: 0;
  width: min(520px, calc(100% - 32px)); background: var(--bg); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .7); }
dialog .body { padding: 24px; }
dialog h2 { font-size: var(--text-heading-sm); margin: 0 0 8px; }
dialog label { display: block; font-family: var(--font-meta); font-size: 13px; color: var(--text-2); margin: 16px 0 6px; }
dialog input { min-height: var(--tap); }
dialog .actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 20px; }
dialog hr { border: 0; border-top: 1px solid var(--line); margin: 24px 0; }

.toast {
  /* Top, below the header: the bottom is taken by the tab bar and chat composer */
  position: fixed; left: 50%; top: calc(64px + env(safe-area-inset-top)); transform: translateX(-50%);
  background: var(--action); color: var(--action-ink); padding: 10px 18px; border-radius: 9999px; font-size: 14px; z-index: 20;
  max-width: calc(100% - 40px);
}

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
