/* Nutshell Health — components.css
 * Reusable UI primitives: cards, buttons, forms, progress, badges, sheets,
 * toasts, tables, skeletons and empty states.
 */

/* ------------------------------------------------------------------ card */

.card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--sp-5);
  box-shadow: var(--shadow-sm), var(--edge-light);
}
.card--flush { padding: 0; overflow: hidden; }
.card--soft { background: var(--surface-2); box-shadow: none; }
.card--accent { border-color: var(--accent-border); background: var(--accent-soft); box-shadow: none; }

/* Cards that are themselves a control get a gentle lift on hover. */
.card--interactive { cursor: pointer; transition: transform var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease); }
.card--interactive:hover { transform: translateY(-2px); box-shadow: var(--shadow-md), var(--edge-light); }

/* A card that carries decorative artwork behind its content. */
.card--art { overflow: hidden; }
.card--art > * { position: relative; z-index: 1; }
.mesh-art {
  position: absolute;
  inset: -20% -10% auto -10%;
  width: 120%;
  height: 180%;
  z-index: 0;
  pointer-events: none;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
  flex-wrap: wrap;
}
.card-title { font-size: var(--fs-md); font-weight: 660; letter-spacing: -.015em; }
.card-sub {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  margin-top: 3px;
  line-height: var(--lh-normal);
  max-width: 62ch;
}

.grid { display: grid; gap: var(--sp-4); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-auto { grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); }

@media (max-width: 780px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .card { padding: var(--sp-4); }
}

.stack { display: flex; flex-direction: column; gap: var(--sp-4); }
.stack-sm { display: flex; flex-direction: column; gap: var(--sp-2); }
.row { display: flex; align-items: center; gap: var(--sp-3); }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); }
.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }

/* --------------------------------------------------------------- buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4);
  min-height: var(--touch);
  border-radius: var(--r-md);
  border: 1px solid transparent;
  font-size: var(--fs-base);
  font-weight: 600;
  white-space: nowrap;
  transition: background var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease),
              transform var(--dur-fast) var(--ease),
              color var(--dur-fast) var(--ease);
}
.btn:active { transform: translateY(1px); }
.btn svg { width: 17px; height: 17px; flex: none; }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .5; pointer-events: none; }

.btn-primary { background: var(--accent); color: #04231b; }
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--text);
}
.btn-secondary:hover { background: var(--bg-soft); }

.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--bg-soft); color: var(--text); }

.btn-danger { background: var(--alert-soft); color: var(--alert); border-color: transparent; }
.btn-danger:hover { background: var(--alert); color: #fff; }

.btn-sm { padding: var(--sp-2) var(--sp-3); min-height: 34px; font-size: var(--fs-sm); border-radius: var(--r-sm); }
.btn-lg { padding: var(--sp-4) var(--sp-6); min-height: 52px; font-size: var(--fs-md); }
.btn-block { width: 100%; }

.icon-btn {
  display: grid;
  place-items: center;
  width: var(--touch);
  height: var(--touch);
  border-radius: var(--r-md);
  color: var(--text-muted);
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.icon-btn:hover { background: var(--bg-soft); color: var(--text); }
.icon-btn svg { width: 19px; height: 19px; }
.icon-btn--sm { width: 34px; height: 34px; }
.icon-btn--sm svg { width: 16px; height: 16px; }

/* ----------------------------------------------------------------- forms */

.field { display: flex; flex-direction: column; gap: var(--sp-2); }
.field-label { font-size: var(--fs-sm); font-weight: 600; color: var(--text); }
.field-hint { font-size: var(--fs-xs); color: var(--text-muted); }

.field-error {
  font-size: var(--fs-xs);
  color: var(--alert);
  display: flex;
  align-items: center;
  gap: var(--sp-1);
  font-weight: 550;
}
.field-error svg { width: 13px; height: 13px; flex: none; }

.input, .select, .textarea {
  width: 100%;
  padding: var(--sp-3);
  min-height: var(--touch);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  color: var(--text);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.input:hover, .select:hover { border-color: var(--text-subtle); }
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.input[aria-invalid="true"], .select[aria-invalid="true"] {
  border-color: var(--alert);
  box-shadow: 0 0 0 3px var(--alert-soft);
}
.input::placeholder, .textarea::placeholder { color: var(--text-subtle); }
.textarea { min-height: 90px; resize: vertical; }

.select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2378879a' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--sp-3) center;
  background-size: 17px;
  padding-right: var(--sp-8);
}

.input-group { display: flex; gap: var(--sp-2); }
.input-group .input { flex: 1; min-width: 0; }

/* Segmented control — used for units, themes, ranges. */
.segmented {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: var(--bg-soft);
  border-radius: var(--r-md);
  border: 1px solid var(--border);
}
.segmented button {
  padding: var(--sp-2) var(--sp-4);
  border-radius: calc(var(--r-md) - 4px);
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text-muted);
  min-height: 34px;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.segmented button:hover { color: var(--text); }
.segmented button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.segmented--block { display: flex; width: 100%; }
.segmented--block button { flex: 1; }

/* Choice tiles for onboarding and goal selection. */
.choice {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-3);
  width: 100%;
  text-align: left;
  padding: var(--sp-4);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface);
  transition: border-color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}
.choice:hover { border-color: var(--border-strong); background: var(--surface-2); }
.choice[aria-pressed="true"], .choice[aria-checked="true"] {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: 0 0 0 1px var(--accent);
}
.choice-icon {
  width: 34px; height: 34px;
  border-radius: var(--r-sm);
  background: var(--bg-soft);
  display: grid; place-items: center;
  color: var(--text-muted);
  flex: none;
}
.choice[aria-pressed="true"] .choice-icon,
.choice[aria-checked="true"] .choice-icon { background: var(--surface); color: var(--accent-text); }
.choice-title { font-weight: 620; font-size: var(--fs-base); }
.choice-sub { font-size: var(--fs-sm); color: var(--text-muted); margin-top: 1px; }
.choice-check { margin-left: auto; color: var(--accent); flex: none; }

.switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 46px; height: 27px;
  border-radius: var(--r-full);
  background: var(--border-strong);
  transition: background var(--dur) var(--ease);
  flex: none;
}
.switch::after {
  content: "";
  position: absolute;
  left: 3px;
  width: 21px; height: 21px;
  border-radius: var(--r-full);
  background: #fff;
  box-shadow: var(--shadow-sm);
  transition: transform var(--dur) var(--ease);
}
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"]::after { transform: translateX(19px); }

.switch-row {
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  width: 100%;
  text-align: left;
  padding: var(--sp-3) 0;
}
.switch-row + .switch-row { border-top: 1px solid var(--border); }

/* ---------------------------------------------------------------- chips */

.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  min-height: 34px;
  border-radius: var(--r-full);
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: var(--fs-sm);
  font-weight: 550;
  color: var(--text-muted);
  transition: all var(--dur-fast) var(--ease);
}
.chip:hover { border-color: var(--border-strong); color: var(--text); }
.chip[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent-border);
  color: var(--accent-text);
  font-weight: 620;
}
.chip svg { width: 14px; height: 14px; }
.chip-row { display: flex; flex-wrap: wrap; gap: var(--sp-2); }

.chip-scroll {
  display: flex;
  gap: var(--sp-2);
  overflow-x: auto;
  padding-bottom: var(--sp-2);
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}
.chip-scroll::-webkit-scrollbar { height: 4px; }
.chip-scroll::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 2px; }
.chip-scroll > * { flex: none; }

/* --------------------------------------------------------------- badges */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  padding: 3px var(--sp-2);
  border-radius: var(--r-sm);
  font-size: var(--fs-xs);
  font-weight: 640;
  letter-spacing: .01em;
  background: var(--bg-soft);
  color: var(--text-muted);
  white-space: nowrap;
}
.badge svg { width: 12px; height: 12px; flex: none; }
.badge--good { background: var(--good-soft); color: var(--good); }
.badge--ok { background: var(--ok-soft); color: var(--ok); }
.badge--warn { background: var(--warn-soft); color: var(--warn); }
.badge--alert { background: var(--alert-soft); color: var(--alert); }
.badge--info { background: var(--info-soft); color: var(--info); }
.badge--accent { background: var(--accent-soft); color: var(--accent-text); }
.badge--outline { background: transparent; border: 1px solid var(--border-strong); }

/* Source provenance marker — always visible next to nutrition data. */
.source-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  font-size: var(--fs-xs);
  color: var(--text-subtle);
  font-weight: 550;
}
.source-badge svg { width: 12px; height: 12px; flex: none; }
.source-badge--high { color: var(--good); }
.source-badge--medium { color: var(--warn); }
.source-badge--low { color: var(--text-subtle); }

/* --------------------------------------------------------------- progress */

.bar {
  position: relative;
  height: 8px;
  border-radius: var(--r-full);
  background: var(--bg-soft);
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  border-radius: var(--r-full);
  background: var(--accent);
  transform-origin: left;
  transition: width var(--dur-slow) var(--ease), background var(--dur) var(--ease);
}
.bar--tall { height: 11px; }
.bar-fill--good { background: var(--good); }
.bar-fill--ok { background: var(--ok); }
.bar-fill--warn { background: var(--warn); }
.bar-fill--alert { background: var(--alert); }

/* The overflow segment when a limit has been passed. */
.bar-over {
  position: absolute;
  top: 0; bottom: 0;
  background: repeating-linear-gradient(
    -45deg,
    var(--alert) 0 5px,
    color-mix(in srgb, var(--alert) 65%, transparent) 5px 10px
  );
  border-radius: var(--r-full);
}

.metric-row { display: flex; flex-direction: column; gap: var(--sp-2); }
.metric-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-3);
}
.metric-name {
  font-size: var(--fs-sm);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}
.metric-dot { width: 8px; height: 8px; border-radius: var(--r-full); flex: none; }
.metric-values { font-size: var(--fs-sm); color: var(--text-muted); font-variant-numeric: tabular-nums; }
.metric-values strong { color: var(--text); font-weight: 660; }

/* ------------------------------------------------------------------ list */

.list { display: flex; flex-direction: column; }
.list-item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  width: 100%;
  text-align: left;
  border-bottom: 1px solid var(--border);
  transition: background var(--dur-fast) var(--ease);
  min-height: 60px;
}
.list-item:last-child { border-bottom: none; }
.list-item:hover { background: var(--surface-2); }
.list-item--button { cursor: pointer; }

.list-thumb {
  flex: none;
  border-radius: 11px;
  overflow: hidden;
  box-shadow: 0 0 0 1px var(--border);
  display: block;
}
@media (max-width: 480px) {
  /* On the narrowest screens the name and numbers matter more than the picture. */
  .list-thumb { display: none; }
}

.list-main { flex: 1; min-width: 0; }
.list-title {
  font-weight: 580;
  font-size: var(--fs-base);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.list-sub {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  margin-top: 1px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.list-meta { text-align: right; flex: none; font-variant-numeric: tabular-nums; }
.list-meta-main { font-weight: 640; font-size: var(--fs-base); }
.list-meta-sub { font-size: var(--fs-xs); color: var(--text-muted); }
.list-actions { display: flex; gap: var(--sp-1); flex: none; }

/* ---------------------------------------------------------------- table */

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--r-md);
}
.table { font-size: var(--fs-sm); min-width: 100%; }
.table th, .table td {
  padding: var(--sp-3);
  text-align: right;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.table th:first-child, .table td:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); }
.table thead th {
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-subtle);
  font-weight: 640;
  background: var(--surface);
}
.table tbody tr:hover td { background: var(--surface-2); }
.table td { font-variant-numeric: tabular-nums; }
.table .num { font-weight: 580; }
.table-group td {
  background: var(--bg-soft);
  font-weight: 660;
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
}
.cell-best { color: var(--good); font-weight: 700; }
.cell-diff { font-size: var(--fs-xs); color: var(--text-subtle); font-weight: 500; }

/* ---------------------------------------------------------------- sheet */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(8, 12, 18, .45);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-4);
  animation: fadeIn var(--dur-fast) var(--ease);
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-xl);
  width: min(560px, 100%);
  max-height: min(86vh, 900px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: sheetIn var(--dur) var(--ease);
}
.sheet--wide { width: min(880px, 100%); }
@keyframes sheetIn {
  from { opacity: 0; transform: translateY(12px) scale(.98); }
  to { opacity: 1; transform: none; }
}

.sheet-head {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-3);
  padding: var(--sp-5) var(--sp-5) var(--sp-4);
  border-bottom: 1px solid var(--border);
}
.sheet-title { font-size: var(--fs-lg); font-weight: 660; letter-spacing: -.01em; }
.sheet-sub { font-size: var(--fs-sm); color: var(--text-muted); margin-top: 2px; }
.sheet-body { padding: var(--sp-5); overflow-y: auto; flex: 1; }
.sheet-foot {
  display: flex;
  gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5);
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}
.sheet-foot .btn { flex: 1; }

@media (max-width: 600px) {
  .overlay { padding: 0; align-items: flex-end; }
  .sheet {
    width: 100%;
    max-height: 92vh;
    border-radius: var(--r-xl) var(--r-xl) 0 0;
    animation: sheetUp var(--dur) var(--ease);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  @keyframes sheetUp {
    from { transform: translateY(100%); }
    to { transform: none; }
  }
}

/* ---------------------------------------------------------------- toast */

.toast-region {
  position: fixed;
  bottom: var(--sp-5);
  left: 50%;
  transform: translateX(-50%);
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  width: min(440px, calc(100vw - var(--sp-8)));
  pointer-events: none;
}
@media (max-width: 900px) {
  .toast-region { bottom: calc(var(--bottom-nav-h) + var(--sp-4) + env(safe-area-inset-bottom, 0px)); }
}

.toast {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  border-radius: var(--r-md);
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow-lg);
  font-size: var(--fs-sm);
  font-weight: 550;
  pointer-events: auto;
  animation: toastIn var(--dur) var(--ease);
}
@keyframes toastIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}
.toast--leaving { animation: toastOut var(--dur-fast) var(--ease) forwards; }
@keyframes toastOut { to { opacity: 0; transform: translateY(6px); } }

.toast-icon { flex: none; display: grid; place-items: center; }
.toast-icon svg { width: 17px; height: 17px; }
.toast-msg { flex: 1; min-width: 0; }
.toast-action {
  flex: none;
  font-weight: 700;
  color: var(--accent);
  padding: var(--sp-1) var(--sp-2);
  border-radius: var(--r-sm);
}
.toast-action:hover { background: rgba(255, 255, 255, .12); }
.toast--alert { background: var(--alert); color: #fff; }
.toast--alert .toast-action { color: #fff; text-decoration: underline; }

/* ------------------------------------------------------- states + skeleton */

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: var(--sp-2);
  padding: var(--sp-8) var(--sp-5) var(--sp-10);
  color: var(--text-muted);
}
.empty-art { margin-bottom: var(--sp-2); opacity: .95; }
.scene-art { display: block; max-width: 100%; height: auto; }
.empty-title { font-weight: 660; color: var(--text); font-size: var(--fs-lg); letter-spacing: -.01em; }
.empty-text { font-size: var(--fs-sm); max-width: 44ch; line-height: var(--lh-relaxed); }

.skeleton {
  background: linear-gradient(90deg, var(--bg-soft) 25%, var(--surface-2) 50%, var(--bg-soft) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite linear;
  border-radius: var(--r-sm);
}
@keyframes shimmer {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}
.skeleton-row { height: 60px; border-radius: var(--r-md); margin-bottom: var(--sp-2); }
.skeleton-text { height: 12px; margin-bottom: var(--sp-2); }

.notice {
  display: flex;
  gap: var(--sp-3);
  padding: var(--sp-4);
  border-radius: var(--r-md);
  background: var(--info-soft);
  color: var(--text);
  font-size: var(--fs-sm);
  line-height: var(--lh-relaxed);
  border: 1px solid transparent;
}
.notice svg { width: 17px; height: 17px; flex: none; margin-top: 2px; color: var(--info); }
.notice--warn { background: var(--warn-soft); }
.notice--warn svg { color: var(--warn); }
.notice--alert { background: var(--alert-soft); }
.notice--alert svg { color: var(--alert); }
.notice--good { background: var(--good-soft); }
.notice--good svg { color: var(--good); }
.notice-title { font-weight: 640; margin-bottom: 2px; }

.disclaimer {
  font-size: var(--fs-xs);
  color: var(--text-subtle);
  line-height: var(--lh-relaxed);
  padding: var(--sp-4);
  border-top: 1px solid var(--border);
  margin-top: var(--sp-6);
  display: flex;
  gap: var(--sp-2);
}
.disclaimer svg { width: 14px; height: 14px; flex: none; margin-top: 2px; }

/* -------------------------------------------------------------- tooltip */

.tip { position: relative; display: inline-flex; }
.tip-btn {
  width: 17px; height: 17px;
  border-radius: var(--r-full);
  background: var(--bg-soft);
  color: var(--text-subtle);
  display: grid; place-items: center;
  font-size: 10px;
  font-weight: 800;
  flex: none;
}
.tip-btn:hover { background: var(--border-strong); color: var(--text); }
.tip-pop {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  width: max-content;
  max-width: 260px;
  padding: var(--sp-3);
  border-radius: var(--r-md);
  background: var(--text);
  color: var(--bg);
  font-size: var(--fs-xs);
  line-height: var(--lh-normal);
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  text-align: left;
  white-space: normal;
}
.tip-pop::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: var(--text);
}

/* --------------------------------------------------------------- utility */

.muted { color: var(--text-muted); }
.subtle { color: var(--text-subtle); }
.small { font-size: var(--fs-sm); }
.xs { font-size: var(--fs-xs); }
.strong { font-weight: 660; }
.num { font-variant-numeric: tabular-nums; }
.center { text-align: center; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mt-2 { margin-top: var(--sp-2); }
.mt-4 { margin-top: var(--sp-4); }
.mt-6 { margin-top: var(--sp-6); }
.mb-4 { margin-bottom: var(--sp-4); }
.hidden { display: none !important; }
