/* ============================================================================
   04 · COMPONENTS, reusable atoms and molecules.
   Every screen composes these. Change a component here and it changes
   everywhere, which is the point of keeping this file separate.
   ========================================================================== */

/* ---------- button ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  height: var(--density-control); padding: 0 var(--sp-4);
  border: var(--border-width) solid transparent; border-radius: var(--radius-sm);
  font-size: var(--text-sm); font-weight: var(--weight-medium);
  white-space: nowrap; cursor: pointer;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease),
              color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.btn:active { transform: translateY(1px); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .5; pointer-events: none; }

.btn--primary   { background: var(--brand); color: var(--brand-on); }
.btn--primary:hover { background: var(--brand-hover); }
.btn--secondary { background: transparent; border-color: var(--border-strong); color: var(--text); }
.btn--secondary:hover { background: var(--bg-raised); border-color: var(--text-subtle); }
.btn--ghost     { background: transparent; color: var(--text-muted); }
.btn--ghost:hover { background: var(--bg-raised); color: var(--text); }
.btn--danger    { background: var(--danger); color: #fff; }
.btn--danger:hover { filter: brightness(1.08); }
.btn--sm { height: var(--density-control-sm); padding: 0 var(--sp-3); font-size: var(--text-xs); }
.btn--icon { width: var(--density-control); padding: 0; }
.btn--icon.btn--sm { width: var(--density-control-sm); }
.btn--block { width: 100%; }

/* ---------- form controls ---------- */
.field { display: block; }
.field + .field { margin-top: var(--sp-4); }
.field__label {
  display: block; font-size: var(--text-sm); color: var(--text-muted); margin-bottom: var(--sp-2);
}
.field__hint { font-size: var(--text-xs); color: var(--text-subtle); margin-top: var(--sp-2); }
.input, .select, .textarea {
  width: 100%; min-height: var(--density-control); padding: var(--sp-2) var(--sp-3);
  background: var(--bg-sunken); color: var(--text);
  border: var(--border-width) solid var(--border-strong); border-radius: var(--radius-sm);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.input::placeholder, .textarea::placeholder { color: var(--text-subtle); }
.input:hover, .select:hover, .textarea:hover { border-color: var(--text-subtle); }
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-ring);
}
.textarea { min-height: 96px; resize: vertical; }
.select { appearance: none; padding-right: var(--sp-8); cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 17px) 16px, calc(100% - 12px) 16px;
  background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; }

.search { position: relative; display: flex; align-items: center; }
.search .input { padding-left: var(--sp-8); }
.search__icon {
  position: absolute; left: var(--sp-3); width: 15px; height: 15px;
  color: var(--text-subtle); pointer-events: none;
}

/* ---------- card ---------- */
.card {
  background: var(--bg-surface);
  border: var(--border-width) solid var(--border);
  border-radius: var(--radius-lg);
}
.card__head {
  display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5); border-bottom: var(--border-width) solid var(--border);
}
.card__body { padding: var(--sp-5); }
.card__foot {
  padding: var(--sp-3) var(--sp-5); border-top: var(--border-width) solid var(--border);
  background: var(--bg-sunken);
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}
.card--pad { padding: var(--sp-5); }

/* ---------- stat tile ---------- */
.stat { display: flex; flex-direction: column; gap: var(--sp-2); }
.stat__value { font-size: var(--text-3xl); font-weight: var(--weight-semi);
  letter-spacing: -.015em; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat__label { font-size: var(--text-sm); color: var(--text-muted); }
.stat__delta { display: inline-flex; align-items: center; gap: 4px;
  font-size: var(--text-xs); font-family: var(--font-mono); }
.stat__delta--up   { color: var(--success); }
.stat__delta--down { color: var(--danger); }
.stat__delta--flat { color: var(--text-subtle); }

/* ---------- badge / status pill ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  height: 22px; padding: 0 var(--sp-2);
  border-radius: var(--radius-pill); border: var(--border-width) solid var(--neutral-bd);
  background: var(--neutral-bg); color: var(--text-muted);
  font-size: var(--text-2xs); font-weight: var(--weight-medium);
  letter-spacing: .02em; white-space: nowrap;
}
.badge::before { content: ""; width: 5px; height: 5px; border-radius: 50%;
  background: currentColor; flex: none; }
.badge--plain::before { display: none; }
.badge--success { background: var(--success-bg); border-color: var(--success-bd); color: var(--success); }
.badge--warning { background: var(--warning-bg); border-color: var(--warning-bd); color: var(--warning); }
.badge--danger  { background: var(--danger-bg);  border-color: var(--danger-bd);  color: var(--danger); }
.badge--info    { background: var(--info-bg);    border-color: var(--info-bd);    color: var(--info); }
.badge--brand   { background: var(--brand-soft); border-color: var(--brand-ring); color: var(--brand); }

/* ---------- avatar ---------- */
.avatar {
  display: grid; place-items: center; flex: none;
  width: 32px; height: 32px; border-radius: var(--radius-pill);
  background: var(--bg-raised); border: var(--border-width) solid var(--border);
  font-size: var(--text-xs); font-weight: var(--weight-medium); color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .02em;
}
.avatar--sm { width: 26px; height: 26px; font-size: var(--text-2xs); }
.avatar--lg { width: 44px; height: 44px; font-size: var(--text-md); }
.avatar--brand { background: var(--brand-soft); border-color: var(--brand-ring); color: var(--brand); }
.avatar-stack { display: flex; }
.avatar-stack .avatar + .avatar { margin-left: -8px; }

/* ---------- table ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); min-width: 640px; }
.table th, .table td {
  padding: 0 var(--sp-4); height: var(--density-row); text-align: left;
  border-bottom: var(--border-width) solid var(--border); white-space: nowrap;
}
.table thead th {
  background: var(--bg-sunken); color: var(--text-subtle);
  font-family: var(--font-mono); font-size: var(--text-2xs);
  font-weight: var(--weight-regular); letter-spacing: .07em; text-transform: uppercase;
  position: sticky; top: 0; z-index: 1;
}
.table tbody tr { transition: background var(--dur-fast) var(--ease); }
.table tbody tr:hover { background: var(--bg-raised); }
.table tbody tr:last-child td { border-bottom: 0; }
.table .num { font-variant-numeric: tabular-nums; text-align: right; }
.table .wrap-cell { white-space: normal; min-width: 220px; }

/* ---------- progress / meter ---------- */
.meter { height: 6px; border-radius: var(--radius-pill); background: var(--neutral-bg); overflow: hidden; }
.meter__fill { height: 100%; border-radius: inherit; background: var(--brand);
  transition: width var(--dur-slow) var(--ease); }
.meter__fill--success { background: var(--success); }
.meter__fill--warning { background: var(--warning); }
.meter__fill--danger  { background: var(--danger); }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: var(--sp-1); border-bottom: var(--border-width) solid var(--border); }
.tab {
  padding: var(--sp-3) var(--sp-3); font-size: var(--text-sm); color: var(--text-muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
  transition: color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--brand); border-bottom-color: var(--brand); }

/* ---------- empty state ---------- */
.empty { text-align: center; padding: var(--sp-12) var(--sp-5); color: var(--text-subtle); }
.empty__icon { width: 34px; height: 34px; margin: 0 auto var(--sp-4); opacity: .5; }

/* ---------- modal ---------- */
.modal[hidden] { display: none; }
.modal {
  position: fixed; inset: 0; z-index: var(--z-modal);
  display: grid; place-items: center; padding: var(--sp-4);
  background: var(--bg-overlay);
}
.modal__panel {
  width: min(100%, 520px); max-height: 88vh; overflow-y: auto;
  background: var(--bg-surface); border: var(--border-width) solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
}

/* ---------- misc ---------- */
.divider { height: 1px; background: var(--border); border: 0; margin: var(--sp-5) 0; }
.dot-list { display: flex; align-items: center; gap: var(--sp-2); color: var(--text-subtle);
  font-size: var(--text-xs); }
.dot-list > * + *::before { content: "·"; margin-right: var(--sp-2); }
.kbd {
  display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 5px;
  background: var(--bg-raised); border: var(--border-width) solid var(--border-strong);
  border-radius: var(--radius-xs); font-family: var(--font-mono); font-size: var(--text-2xs);
  color: var(--text-subtle);
}

/* ---------- preview banner shared by the login screen and the app ---------- */
.preview-bar {
  display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap;
  padding: var(--sp-2) var(--sp-4); background: var(--brand-soft);
  border-bottom: var(--border-width) solid var(--brand-ring);
  font-size: var(--text-xs); color: var(--brand);
}
.preview-bar a { text-decoration: underline; text-underline-offset: 2px; }
.preview-bar__spacer { margin-left: auto; }

/* ==========================================================================
   ACTION LAYER
   Every action button in a preview opens one of three things: a detail sheet,
   a chat thread, or a confirm dialog. The sheet behaves as a screen, full
   bleed inside a phone, a side panel on a desktop app, so "open" and
   "message" navigate somewhere instead of doing nothing.
   ========================================================================== */

.sheet[hidden] { display: none; }
.sheet {
  position: fixed; inset: 0; z-index: var(--z-modal);
  display: flex; justify-content: flex-end;
  background: var(--bg-overlay);
}
.sheet__panel {
  display: flex; flex-direction: column;
  width: min(100%, 460px); height: 100%;
  background: var(--bg-surface);
  border-left: var(--border-width) solid var(--border);
  box-shadow: var(--shadow-lg);
}
.sheet__head {
  flex: none; display: flex; align-items: flex-start; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5);
  border-bottom: var(--border-width) solid var(--border);
}
.sheet__title { font-size: var(--text-lg); font-weight: 550; letter-spacing: -.01em; }
.sheet__sub { font-size: var(--text-xs); color: var(--text-muted); margin-top: 2px; }
.sheet__body { flex: 1; overflow-y: auto; padding: var(--sp-5); }
.sheet__body > * + * { margin-top: var(--sp-4); }
.sheet__foot {
  flex: none; display: flex; gap: var(--sp-2); justify-content: flex-end;
  padding: var(--sp-4) var(--sp-5);
  border-top: var(--border-width) solid var(--border);
}

/* a labelled fact from the row the action came from */
.sheet-fact { display: flex; justify-content: space-between; gap: var(--sp-4);
  padding: 9px 0; border-bottom: var(--border-width) solid var(--border); }
.sheet-fact:last-child { border-bottom: 0; }
.sheet-fact dt { font-size: var(--text-xs); color: var(--text-muted); }
.sheet-fact dd { font-size: var(--text-sm); text-align: right; }

/* chat thread */
.chat { display: flex; flex-direction: column; gap: var(--sp-3); }
.chat__msg { max-width: 78%; padding: 9px 13px; border-radius: 16px;
  font-size: var(--text-sm); line-height: 1.45; }
.chat__msg--them { align-self: flex-start; background: var(--bg-raised);
  border-bottom-left-radius: 5px; }
.chat__msg--me { align-self: flex-end; background: var(--brand);
  color: var(--brand-on); border-bottom-right-radius: 5px; }
.chat__when { font-size: var(--text-2xs); color: var(--text-subtle); margin-top: 3px; }
.chat__compose { display: flex; gap: var(--sp-2); align-items: center; width: 100%; }
.chat__compose .input { flex: 1; }

/* toast: a state change with nowhere to navigate still needs to be visible */
.toast[hidden] { display: none; }
.toast {
  position: fixed; left: 50%; bottom: 24px; translate: -50% 0; z-index: var(--z-modal);
  display: flex; align-items: center; gap: var(--sp-3);
  padding: 11px 16px; border-radius: var(--radius-lg);
  background: var(--bg-raised); border: var(--border-width) solid var(--border-strong);
  box-shadow: var(--shadow-lg); font-size: var(--text-sm); max-width: min(92vw, 420px);
}
.toast__dot { flex: none; width: 7px; height: 7px; border-radius: 50%; background: var(--brand); }

/* Inside a phone the sheet is a screen, not a side panel. .ios is a
   containing block whenever it is scaled, so absolute keeps it predictable. */
.ios .sheet { position: absolute; }
.ios .sheet__panel { width: 100%; border-left: 0; }
.ios .sheet__head, .ios .sheet__foot { padding: 14px 20px; }
.ios .sheet__body { padding: 18px 20px; }
.ios .toast { position: absolute; bottom: 96px; }

/* a row you can open needs to look like one */
.table tbody tr[data-openable] { cursor: pointer; }
.table tbody tr[data-openable]:hover { background: var(--bg-raised); }
.table tbody tr[data-openable]:focus-visible {
  outline: 2px solid var(--brand); outline-offset: -2px;
}

/* ==========================================================================
   CHARTS
   Inline SVG built at page-build time, no charting library, no runtime cost,
   and it renders identically offline. Colours come from the token layer, so a
   rebrand or a theme switch carries the charts with it.
   ========================================================================== */

.chart { display: block; width: 100%; }
.chart__svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart__grid { stroke: var(--border); stroke-width: 1; vector-effect: non-scaling-stroke; }
.chart__line { fill: none; stroke: var(--brand); stroke-width: 2;
  stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; }
.chart__area { fill: var(--brand); opacity: .12; }
.chart__dot  { fill: var(--brand); }
.chart__axis { fill: var(--text-subtle); font-family: var(--font-mono);
  font-size: 9px; letter-spacing: .04em; }
.chart__band { fill: var(--bg-raised); }

.chart-head { display: flex; align-items: baseline; justify-content: space-between;
  gap: var(--sp-3); margin-bottom: var(--sp-3); }
.chart-key { display: flex; align-items: center; gap: var(--sp-2);
  font-size: var(--text-xs); color: var(--text-muted); }
.chart-key i { width: 8px; height: 8px; border-radius: 2px; background: var(--brand);
  display: inline-block; }

/* sparkline inside a stat card */
.spark { display: block; width: 100%; height: 26px; margin-top: 8px; overflow: visible; }
.spark path { fill: none; stroke: var(--brand); stroke-width: 1.6;
  stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; }
.spark .spark__fill { fill: var(--brand); opacity: .13; stroke: none; }

/* donut with a centred figure */
.donut-wrap { display: flex; align-items: center; gap: var(--sp-5); flex-wrap: wrap; }
.donut { position: relative; flex: none; width: 148px; height: 148px; }
.donut svg { transform: rotate(-90deg); display: block; }
.donut__hole { position: absolute; inset: 0; display: grid; place-items: center;
  text-align: center; }
.donut__num { font-size: var(--text-2xl); font-weight: 650; letter-spacing: -.02em;
  line-height: 1.1; }
.donut__cap { font-size: var(--text-2xs); color: var(--text-muted); }
.donut-legend { flex: 1; min-width: 150px; display: flex; flex-direction: column;
  gap: var(--sp-2); }
.donut-legend div { display: flex; align-items: center; gap: var(--sp-2);
  font-size: var(--text-sm); }
.donut-legend i { width: 9px; height: 9px; border-radius: 2px; flex: none; }
.donut-legend b { margin-left: auto; font-variant-numeric: tabular-nums;
  font-weight: 550; }

/* labelled horizontal bars */
.barlist { display: flex; flex-direction: column; gap: var(--sp-3); }
.barlist__row { display: grid; grid-template-columns: minmax(88px,auto) 1fr auto;
  align-items: center; gap: var(--sp-3); font-size: var(--text-sm); }
.barlist__track { height: 8px; border-radius: 4px; background: var(--bg-raised);
  overflow: hidden; }
.barlist__fill { display: block; height: 100%; border-radius: 4px; background: var(--brand); }
.barlist__val { font-variant-numeric: tabular-nums; color: var(--text-muted);
  font-size: var(--text-xs); }

/* Proportional stack: parts of a whole that must visibly add up to 100%.
   A row of individual bars cannot show that, and scaling each to the largest
   makes a 25% weight look like a full bar. */
.stackbar { display: flex; height: 12px; border-radius: 6px; overflow: hidden;
  background: var(--bg-raised); }
.stackbar i { display: block; height: 100%; }
.stackbar i + i { box-shadow: inset 1px 0 0 var(--bg-surface); }

/* One column across a 1100px card put 985px between a label and its own
   value. Columns keep the pair readable as a pair. */
.weights { display: grid; grid-template-columns: repeat(auto-fit, minmax(272px, 1fr));
  gap: var(--sp-4) var(--sp-5); margin-top: var(--sp-4); }
.weights__row { display: grid; grid-template-columns: 10px 1fr auto;
  align-items: baseline; gap: var(--sp-2) var(--sp-3); min-width: 0; }
.weights__row > span { min-width: 0; }
.weights__row i { width: 10px; height: 10px; border-radius: 3px; align-self: center; }
.weights__row b { font-weight: 550; font-size: var(--text-sm); }
.weights__row span { display: block; font-size: var(--text-xs); color: var(--text-muted);
  margin-top: 2px; }
.weights__pct { font-variant-numeric: tabular-nums; font-size: var(--text-sm);
  font-weight: 550; }

/* month calendar --------------------------------------------------------- */
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal__dow {
  text-align: center; font-size: var(--text-2xs); letter-spacing: .06em;
  text-transform: uppercase; color: var(--text-subtle); padding-bottom: var(--sp-2);
}
.cal__day {
  position: relative; aspect-ratio: 1; display: flex; align-items: center;
  justify-content: center; border-radius: var(--radius-xs); font-size: var(--text-md);
  color: var(--text); border: var(--border-width) solid transparent;
}
.cal__day--out { color: var(--text-subtle); }
.cal__day--has::after {
  content: ""; position: absolute; bottom: 6px; width: 4px; height: 4px;
  border-radius: 50%; background: var(--brand);
}
.cal__day--on { background: var(--brand); color: var(--brand-on); font-weight: 600; }
.cal__day--on::after { background: var(--brand-on); }
.cal__day--today { border-color: var(--border-strong); }

/* filter chips ----------------------------------------------------------- */
.tag-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.tag {
  display: inline-flex; align-items: center; padding: 3px 10px; white-space: nowrap;
  border: var(--border-width) solid var(--border); border-radius: 999px;
  font-size: var(--text-2xs); color: var(--text-muted); background: var(--bg-surface);
}
