/* ==========================================================================
   Components — inset-grouped lists, cards, controls, sheets
   ========================================================================== */

/* --- Section heading ------------------------------------------------------ */
.section {
  margin: var(--sp-6) 0 var(--sp-2);
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-3);
}

.section__title {
  font: var(--t-title3);
  letter-spacing: -0.015em;
}

.section__title--plain {
  font: var(--t-footnote);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--label-secondary);
  padding-left: var(--sp-4);
}

.section__action {
  font: var(--t-subhead);
  color: var(--accent);
  white-space: nowrap;
}
.section__action:hover { text-decoration: underline; }

/* --- Inset grouped list --------------------------------------------------- */
.list {
  background: var(--bg-content);
  border-radius: var(--r-md);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.list + .list { margin-top: var(--sp-4); }

.list__footnote {
  padding: var(--sp-2) var(--sp-4) 0;
  font: var(--t-footnote);
  color: var(--label-secondary);
}

.row {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  width: 100%;
  min-height: 46px;
  padding: 9px var(--sp-4);
  text-align: left;
  color: var(--label);
  background: none;
  transition: background var(--dur-fast) var(--ease-ios);
}

.row + .row::before {
  content: "";
  position: absolute;
  left: var(--sp-4);
  right: 0;
  top: 0;
  height: 0.5px;
  background: var(--separator);
}

.row--indent + .row::before { left: 60px; }

@media (hover: hover) { button.row:hover, a.row:hover, .row.is-tappable:hover { background: var(--bg-hover); } }
button.row:active, a.row:active, .row.is-tappable:active { background: var(--bg-active); }

.row__icon {
  display: grid;
  place-items: center;
  width: 29px;
  height: 29px;
  flex: none;
  border-radius: var(--r-xs);
  background: var(--accent);
  color: #fff;
}
.row__icon svg { width: 17px; height: 17px; }

.row__body { flex: 1; min-width: 0; }
.row__title {
  font: var(--t-body);
  letter-spacing: -0.01em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row__subtitle {
  font: var(--t-footnote);
  color: var(--label-secondary);
  margin-top: 1px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row__value {
  font: var(--t-body);
  color: var(--label-secondary);
  font-variant-numeric: tabular-nums;
  text-align: right;
  flex: none;
}
.row__chevron { color: var(--label-tertiary); flex: none; }
.row__chevron svg { width: 15px; height: 15px; }

.row--destructive .row__title { color: var(--sys-red); }
.row--accent .row__title { color: var(--accent); }
.row--center { justify-content: center; }
.row--center .row__title { text-align: center; color: var(--accent); }

/* --- Cards ---------------------------------------------------------------- */
.card {
  background: var(--bg-content);
  border-radius: var(--r-lg);
  padding: var(--sp-4);
  box-shadow: var(--shadow-sm);
}

.card--glass {
  background: var(--glass-panel);
  -webkit-backdrop-filter: var(--glass-blur);
  backdrop-filter: var(--glass-blur);
  box-shadow: var(--shadow-md), inset 0 0 0 0.5px var(--border-glass);
}

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

.card__title { font: var(--t-headline); letter-spacing: -0.01em; }

.grid {
  display: grid;
  gap: var(--sp-3);
  grid-template-columns: repeat(auto-fill, minmax(var(--min, 160px), 1fr));
}

/* --- Buttons -------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: 44px;
  padding: 0 var(--sp-5);
  border-radius: var(--r-md);
  font: var(--t-headline);
  letter-spacing: -0.01em;
  background: var(--bg-inset);
  color: var(--label);
  transition: transform var(--dur-fast) var(--ease-ios), background var(--dur-fast) var(--ease-ios), opacity var(--dur-fast);
  user-select: none;
}

.btn:active { transform: scale(0.97); }
.btn[disabled] { opacity: 0.45; pointer-events: none; }
.btn svg { width: 18px; height: 18px; }

.btn--filled { background: var(--accent); color: var(--accent-text); box-shadow: var(--shadow-sm); }
@media (hover: hover) { .btn--filled:hover { filter: brightness(1.06); } }
.btn--tinted { background: var(--accent-soft); color: var(--accent); }
.btn--plain { background: none; color: var(--accent); min-height: 34px; padding: 0 var(--sp-2); }
.btn--destructive { background: color-mix(in srgb, var(--sys-red) 15%, transparent); color: var(--sys-red); }
.btn--destructive-filled { background: var(--sys-red); color: #fff; }
.btn--sm { min-height: 32px; padding: 0 var(--sp-3); font: var(--t-subhead); border-radius: var(--r-sm); }
.btn--lg { min-height: 50px; font: var(--t-title3); border-radius: var(--r-lg); }
.btn--full { width: 100%; }
.btn--glass {
  background: var(--glass-control);
  -webkit-backdrop-filter: var(--glass-blur);
  backdrop-filter: var(--glass-blur);
  box-shadow: inset 0 0 0 0.5px var(--border-glass);
}

.btn-row { display: flex; gap: var(--sp-2); flex-wrap: wrap; }
.btn-row > .btn { flex: 1 1 auto; }

/* Floating action button (mobile) */
.fab {
  position: fixed;
  z-index: 55;
  right: max(var(--sp-4), var(--safe-right));
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + var(--sp-4));
  width: 56px;
  height: 56px;
  border-radius: var(--r-full);
  background: var(--accent);
  color: var(--accent-text);
  display: grid;
  place-items: center;
  box-shadow: var(--shadow-lg);
  transition: transform var(--dur-base) var(--ease-ios);
}
.fab:active { transform: scale(0.9); }
.fab svg { width: 26px; height: 26px; }
@media (min-width: 820px) { .fab { display: none; } }

/* --- Segmented control ------------------------------------------------------ */
.segmented {
  display: inline-grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 2px;
  padding: 2px;
  border-radius: var(--r-sm);
  background: var(--bg-inset);
  width: 100%;
  position: relative;
}

.segmented__item {
  position: relative;
  z-index: 1;
  padding: 6px var(--sp-3);
  border-radius: 8px;
  font: var(--t-subhead);
  font-weight: 500;
  color: var(--label-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background var(--dur-base) var(--ease-ios), color var(--dur-fast), box-shadow var(--dur-base);
}

/* The chosen segment must stand out in both themes (iOS: white on light,
   #636366 on dark — the card colour was barely visible on dark). */
.segmented__item.is-active {
  background: var(--segment-active);
  color: var(--label);
  font-weight: 600;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12), 0 1px 1px rgba(0, 0, 0, 0.16), 0 0 0 0.5px rgba(0, 0, 0, 0.04);
}
.segmented__item:active { transform: scale(0.96); }

/* --- Chips / filter pills --------------------------------------------------- */
.chips {
  display: flex;
  gap: var(--sp-2);
  overflow-x: auto;
  padding: var(--sp-1) 0 var(--sp-2);
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.chips::-webkit-scrollbar { display: none; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px var(--sp-3);
  border-radius: var(--r-full);
  background: var(--bg-inset);
  color: var(--label);
  font: var(--t-subhead);
  white-space: nowrap;
  flex: none;
  transition: background var(--dur-fast) var(--ease-ios), transform var(--dur-fast);
}
.chip:active { transform: scale(0.95); }
.chip.is-active { background: var(--accent); color: var(--accent-text); }
.chip svg { width: 14px; height: 14px; }
.chip__dot { width: 10px; height: 10px; border-radius: 50%; flex: none; box-shadow: inset 0 0 0 0.5px rgba(0,0,0,.2); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: var(--r-full);
  background: var(--bg-inset);
  color: var(--label-secondary);
  font: var(--t-caption2);
  white-space: nowrap;
}
.badge--accent { background: var(--accent-soft); color: var(--accent); }
.badge--green { background: color-mix(in srgb, var(--sys-green) 18%, transparent); color: color-mix(in srgb, var(--sys-green) 82%, var(--label)); }
.badge--orange { background: color-mix(in srgb, var(--sys-orange) 20%, transparent); color: color-mix(in srgb, var(--sys-orange) 78%, var(--label)); }
.badge--red { background: color-mix(in srgb, var(--sys-red) 16%, transparent); color: color-mix(in srgb, var(--sys-red) 82%, var(--label)); }
.badge--gray { background: var(--bg-inset); color: var(--label-secondary); }

/* --- Search field ------------------------------------------------------------ */
.search {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  height: 36px;
  padding: 0 var(--sp-3);
  border-radius: var(--r-sm);
  background: var(--bg-inset);
  color: var(--label-secondary);
}
.search svg { width: 16px; height: 16px; flex: none; }
.search input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  outline: none;
  color: var(--label);
  font: var(--t-callout);
}
.search input::placeholder { color: var(--label-tertiary); }
.search__clear { display: grid; place-items: center; width: 20px; height: 20px; border-radius: 50%; color: var(--label-tertiary); }
.search__clear:hover { color: var(--label-secondary); }

/* --- Forms --------------------------------------------------------------------- */
.field {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-height: 46px;
  padding: 8px var(--sp-4);
  position: relative;
}
.field + .field::before {
  content: "";
  position: absolute;
  left: var(--sp-4);
  right: 0;
  top: 0;
  height: 0.5px;
  background: var(--separator);
}

.field__label {
  flex: 0 0 38%;
  max-width: 180px;
  font: var(--t-body);
  color: var(--label);
}

.field__control { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: flex-end; gap: var(--sp-2); }

.field input[type="text"],
.field input[type="number"],
.field input[type="password"],
.field input[type="date"],
.field input[type="url"],
.field select,
.field textarea {
  width: 100%;
  border: 0;
  background: none;
  outline: none;
  color: var(--label);
  font: var(--t-body);
  text-align: right;
  min-width: 0;
  padding: 4px 0;
  appearance: none;
  -webkit-appearance: none;
}
.field textarea { text-align: left; resize: vertical; min-height: 72px; font: var(--t-callout); }
.field select { text-align: right; direction: rtl; padding-right: 2px; color: var(--label); }
.field select option { direction: ltr; }
.field input::placeholder, .field textarea::placeholder { color: var(--label-tertiary); }
.field input[type="number"] { font-variant-numeric: tabular-nums; }
.field--stack { flex-direction: column; align-items: stretch; gap: var(--sp-1); }
.field--stack .field__label { flex: none; max-width: none; font: var(--t-footnote); color: var(--label-secondary); }
.field--stack input, .field--stack textarea, .field--stack select { text-align: left; direction: ltr; }
.field__suffix { color: var(--label-tertiary); font: var(--t-subhead); flex: none; }
.field__hint { font: var(--t-caption); color: var(--label-tertiary); }

/* Stepper */
.stepper {
  display: inline-flex;
  align-items: center;
  border-radius: var(--r-sm);
  background: var(--bg-inset);
  overflow: hidden;
}
.stepper button { width: 34px; height: 30px; display: grid; place-items: center; color: var(--label); }
@media (hover: hover) { .stepper button:hover { background: var(--bg-hover); } }
.stepper span { width: 1px; height: 18px; background: var(--separator); }

/* Switch */
.switch {
  position: relative;
  width: 51px;
  height: 31px;
  flex: none;
  border-radius: var(--r-full);
  background: var(--bg-inset-strong);
  transition: background var(--dur-base) var(--ease-ios);
}
.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 3px 8px rgba(0,0,0,.15), 0 1px 1px rgba(0,0,0,.16);
  transition: transform var(--dur-base) var(--ease-ios);
}
.switch.is-on { background: var(--sys-green); }
.switch.is-on::after { transform: translateX(20px); }

/* Color picker swatches */
.swatches { display: flex; flex-wrap: wrap; gap: var(--sp-2); }
.swatch {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 0.5px rgba(0,0,0,.18);
  transition: transform var(--dur-fast) var(--ease-ios);
}
.swatch.is-active { box-shadow: 0 0 0 2.5px var(--bg-content), 0 0 0 5px var(--accent); }
.swatch:active { transform: scale(0.9); }

/* --- Sheets / modals ------------------------------------------------------------ */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--bg-scrim);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity var(--dur-base) var(--ease-ios);
}
.scrim.is-open { opacity: 1; }

.sheet {
  position: fixed;
  z-index: 101;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg-base);
  border-radius: var(--r-xl) var(--r-xl) 0 0;
  box-shadow: var(--shadow-sheet);
  transform: translateY(100%);
  transition: transform var(--dur-slow) var(--ease-ios);
  padding-bottom: var(--safe-bottom);
  overscroll-behavior: contain;
}
.sheet.is-open { transform: translateY(0); }

.sheet__grabber {
  width: 36px;
  height: 5px;
  border-radius: var(--r-full);
  background: var(--label-quaternary);
  margin: var(--sp-2) auto 0;
  flex: none;
}

.sheet__bar {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  flex: none;
}
.sheet__bar .sheet__title {
  flex: 1;
  text-align: center;
  font: var(--t-headline);
  letter-spacing: -0.01em;
}
.sheet__bar button { color: var(--accent); font: var(--t-body); white-space: nowrap; }
.sheet__bar button:disabled { color: var(--label-tertiary); }
.sheet__bar button.is-strong { font-weight: 600; }

.sheet__body {
  flex: 1;
  overflow-y: auto;
  padding: 0 var(--sp-4) var(--sp-6);
  -webkit-overflow-scrolling: touch;
}

@media (min-width: 700px) {
  .sheet {
    left: 50%;
    bottom: auto;
    top: 50%;
    width: min(560px, calc(100vw - 48px));
    max-height: min(86dvh, 860px);
    border-radius: var(--r-xl);
    transform: translate(-50%, -46%) scale(0.96);
    opacity: 0;
    transition: transform var(--dur-base) var(--ease-ios), opacity var(--dur-base) var(--ease-ios);
    padding-bottom: 0;
  }
  .sheet.is-open { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  .sheet__grabber { display: none; }
  .sheet--wide { width: min(880px, calc(100vw - 48px)); }
}

/* --- Action sheet / alert ---------------------------------------------------------- */
.actionsheet {
  position: fixed;
  z-index: 101;
  left: max(var(--sp-2), var(--safe-left));
  right: max(var(--sp-2), var(--safe-right));
  bottom: calc(var(--safe-bottom) + var(--sp-2));
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  transform: translateY(110%);
  transition: transform var(--dur-slow) var(--ease-ios);
}
.actionsheet.is-open { transform: translateY(0); }

.actionsheet__group {
  border-radius: var(--r-lg);
  overflow: hidden;
  background: var(--glass-bar-solid);
  -webkit-backdrop-filter: var(--glass-blur-strong);
  backdrop-filter: var(--glass-blur-strong);
  box-shadow: var(--shadow-lg);
}

.actionsheet__title {
  padding: var(--sp-3) var(--sp-4);
  font: var(--t-footnote);
  color: var(--label-secondary);
  text-align: center;
  border-bottom: 0.5px solid var(--separator);
}

.actionsheet__item {
  width: 100%;
  min-height: 54px;
  padding: var(--sp-3) var(--sp-4);
  font: var(--t-title3);
  font-weight: 400;
  color: var(--accent);
  text-align: center;
  border-bottom: 0.5px solid var(--separator);
  transition: background var(--dur-fast);
}
.actionsheet__item:last-child { border-bottom: 0; }
@media (hover: hover) { .actionsheet__item:hover { background: var(--bg-hover); } }
.actionsheet__item--destructive { color: var(--sys-red); }
.actionsheet__item--cancel { font-weight: 600; }

@media (min-width: 700px) {
  .actionsheet {
    left: 50%;
    right: auto;
    top: 50%;
    bottom: auto;
    width: min(400px, calc(100vw - 48px));
    transform: translate(-50%, -46%) scale(0.96);
    opacity: 0;
    transition: transform var(--dur-base) var(--ease-ios), opacity var(--dur-base) var(--ease-ios);
  }
  .actionsheet.is-open { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}

.alert {
  position: fixed;
  z-index: 102;
  left: 50%;
  top: 50%;
  width: min(300px, calc(100vw - 64px));
  transform: translate(-50%, -50%) scale(0.9);
  opacity: 0;
  border-radius: var(--r-lg);
  overflow: hidden;
  background: var(--glass-bar-solid);
  -webkit-backdrop-filter: var(--glass-blur-strong);
  backdrop-filter: var(--glass-blur-strong);
  box-shadow: var(--shadow-lg);
  transition: transform var(--dur-base) var(--ease-ios), opacity var(--dur-base) var(--ease-ios);
  text-align: center;
}
.alert.is-open { transform: translate(-50%, -50%) scale(1); opacity: 1; }
.alert__content { padding: var(--sp-5) var(--sp-4) var(--sp-4); }
.alert__title { font: var(--t-headline); margin-bottom: var(--sp-1); }
.alert__message { font: var(--t-footnote); color: var(--label-secondary); }
.alert__input { margin-top: var(--sp-3); width: 100%; padding: 8px 10px; border-radius: var(--r-xs); border: 0.5px solid var(--separator-opaque); background: var(--bg-content); color: var(--label); font: var(--t-callout); outline: none; }
.alert__actions { display: flex; border-top: 0.5px solid var(--separator); }
.alert__actions button {
  flex: 1;
  min-height: 44px;
  color: var(--accent);
  font: var(--t-body);
  border-left: 0.5px solid var(--separator);
}
.alert__actions button:first-child { border-left: 0; }
.alert__actions button.is-strong { font-weight: 600; }
.alert__actions button.is-destructive { color: var(--sys-red); }
.alert__actions--stack { flex-direction: column; }
.alert__actions--stack button { border-left: 0; border-top: 0.5px solid var(--separator); }
.alert__actions--stack button:first-child { border-top: 0; }

/* --- Toast ----------------------------------------------------------------------- */
.toasts {
  position: fixed;
  z-index: 120;
  left: 50%;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + var(--sp-4));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  align-items: center;
  pointer-events: none;
  width: max-content;
  max-width: min(92vw, 460px);
}
@media (min-width: 820px) { .toasts { bottom: var(--sp-5); } }

.toast {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 10px var(--sp-4);
  border-radius: var(--r-full);
  background: var(--glass-bar-solid);
  -webkit-backdrop-filter: var(--glass-blur-strong);
  backdrop-filter: var(--glass-blur-strong);
  box-shadow: var(--shadow-lg), inset 0 0 0 0.5px var(--border-glass);
  font: var(--t-subhead);
  color: var(--label);
  animation: toast-in var(--dur-base) var(--ease-ios);
  pointer-events: auto;
}
.toast.is-out { animation: toast-out var(--dur-base) var(--ease-ios) forwards; }
.toast svg { width: 17px; height: 17px; flex: none; }
.toast--success svg { color: var(--sys-green); }
.toast--error svg { color: var(--sys-red); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(14px) scale(0.94); }
  to { opacity: 1; transform: none; }
}
@keyframes toast-out {
  to { opacity: 0; transform: translateY(10px) scale(0.96); }
}

/* --- Empty state ------------------------------------------------------------------- */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--sp-3);
  padding: var(--sp-9) var(--sp-4);
  text-align: center;
  color: var(--label-secondary);
}
.empty__icon {
  width: 64px;
  height: 64px;
  display: grid;
  place-items: center;
  border-radius: var(--r-lg);
  background: var(--bg-inset);
  color: var(--label-tertiary);
}
.empty__icon svg { width: 34px; height: 34px; }
.empty__title { font: var(--t-title3); color: var(--label); }
.empty__text { font: var(--t-callout); max-width: 34ch; }

/* --- Skeleton ---------------------------------------------------------------------- */
.skeleton {
  background: linear-gradient(90deg, var(--bg-inset) 25%, var(--bg-inset-strong) 37%, var(--bg-inset) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.4s ease infinite;
  border-radius: var(--r-sm);
}
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

/* --- Misc ------------------------------------------------------------------------- */
.muted { color: var(--label-secondary); }
.tabular { font-variant-numeric: tabular-nums; }
.stack { display: flex; flex-direction: column; gap: var(--sp-3); }
.stack--tight { gap: var(--sp-2); }
.hstack { display: flex; align-items: center; gap: var(--sp-2); }
.hstack--wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.text-center { text-align: center; }
.mt-4 { margin-top: var(--sp-4); }
.mt-6 { margin-top: var(--sp-6); }
.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;
}

.view-enter { animation: view-in var(--dur-base) var(--ease-ios); }
@keyframes view-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
