* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #e8e8ed;
  --ink: #1c1c1e;
  --ink-2: #3a3a3c;
  --fill: rgba(255, 255, 255, 0.5);
  --fill-solid: #ffffff;
  --inset: rgba(120, 120, 128, 0.16);
  --line: rgba(255, 255, 255, 0.72);
  --line-strong: rgba(60, 60, 67, 0.18);
  --hairline: rgba(60, 60, 67, 0.12);
  --blue: #007aff;
  --blue-press: #0066d6;
  --red: #ff3b30;
  --green: #248a3d;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
  --radius: 28px;
  --spring: cubic-bezier(0.32, 0.72, 0, 1);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --ink: #f5f5f7;
    --ink-2: #aeaeb2;
    --fill: rgba(28, 28, 30, 0.48);
    --fill-solid: #1c1c1e;
    --inset: rgba(118, 118, 128, 0.24);
    --line: rgba(255, 255, 255, 0.14);
    --line-strong: rgba(255, 255, 255, 0.22);
    --hairline: rgba(255, 255, 255, 0.1);
    --blue: #0a84ff;
    --blue-press: #409cff;
    --red: #ff453a;
    --green: #30d158;
    --shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

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

body {
  min-height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Helvetica Neue", sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }
::selection { background: color-mix(in srgb, var(--blue) 28%, transparent); }

button, input { font-family: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.wash {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
  background:
    radial-gradient(80% 50% at 100% 0%, rgba(120, 170, 255, 0.45), transparent 60%),
    radial-gradient(70% 45% at 0% 100%, rgba(255, 186, 170, 0.38), transparent 55%),
    var(--bg);
}

.blob {
  position: absolute;
  width: 62vmax;
  height: 62vmax;
  border-radius: 50%;
  filter: blur(40px);
  opacity: 0.85;
}
.blob-a {
  top: -22vmax;
  right: -18vmax;
  background: #9ec0ff;
  animation: drift 28s var(--spring) infinite alternate;
}
.blob-b {
  bottom: -24vmax;
  left: -16vmax;
  background: #ffd0c4;
  animation: drift 34s var(--spring) -10s infinite alternate-reverse;
}

.bg-image {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.wash.has-photo .blob { display: none; }
.wash.has-photo::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.08);
}
body.has-photo {
  --fill: rgba(255, 255, 255, 0.4);
}

@media (prefers-color-scheme: dark) {
  .wash {
    background:
      radial-gradient(80% 50% at 100% 0%, rgba(40, 80, 180, 0.45), transparent 60%),
      radial-gradient(70% 45% at 0% 80%, rgba(120, 40, 70, 0.28), transparent 55%),
      var(--bg);
  }
  .blob-a { background: #1d4ed8; opacity: 0.55; }
  .blob-b { background: #9f1239; opacity: 0.35; }
  .wash.has-photo::after { background: rgba(0, 0, 0, 0.22); }
  body.has-photo { --fill: rgba(22, 22, 24, 0.42); }
}

@keyframes drift {
  from { transform: translate3d(0, 0, 0) scale(1); }
  to { transform: translate3d(4vmax, 3vmax, 0) scale(1.08); }
}

.app {
  position: relative;
  z-index: 1;
  max-width: 440px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top)) 16px max(28px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.bar {
  position: sticky;
  top: max(10px, env(safe-area-inset-top));
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 4px;
  min-height: 52px;
  padding: 6px 8px 6px 18px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--fill-solid) 36%, transparent);
  border: 1px solid var(--line);
  box-shadow: var(--shadow), inset 0 1px 0 var(--line);
  backdrop-filter: blur(40px) saturate(1.85);
  -webkit-backdrop-filter: blur(40px) saturate(1.85);
}

h1 {
  font-size: 1.0625rem;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.bar-actions, #barActions { display: flex; align-items: center; }
#mainActions { margin-left: auto; }

.text-btn {
  min-height: 40px;
  min-width: 44px;
  padding: 0 8px;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--blue);
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s var(--spring), transform 0.2s var(--spring);
}
.text-btn:hover { background: color-mix(in srgb, var(--blue) 10%, transparent); }
.text-btn:active { transform: scale(0.96); }

main { display: flex; flex-direction: column; gap: 14px; }
.stack { display: flex; flex-direction: column; gap: 14px; }

.panel {
  background: var(--fill);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow), inset 0 1px 0 var(--line);
  backdrop-filter: blur(40px) saturate(1.85);
  -webkit-backdrop-filter: blur(40px) saturate(1.85);
  padding: 22px 20px 20px;
}

.eyebrow {
  color: var(--ink-2);
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.setup { animation: rise 0.45s var(--spring) both; }

.field {
  margin-top: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 64px;
  padding: 0 16px;
  border-radius: 18px;
  background: var(--inset);
  border: 1px solid transparent;
  transition: border-color 0.2s ease, background 0.2s ease;
}
.field:focus-within {
  border-color: color-mix(in srgb, var(--blue) 55%, transparent);
  background: color-mix(in srgb, var(--blue) 8%, var(--inset));
}
.field-unit {
  color: var(--ink-2);
  font-size: 1.35rem;
  font-weight: 600;
}
.field input {
  flex: 1;
  min-width: 0;
  border: none;
  outline: none;
  background: transparent;
  color: var(--ink);
  font-size: 2rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
}
.field input::placeholder { color: color-mix(in srgb, var(--ink-2) 55%, transparent); font-weight: 500; }
.field.compact { margin-top: 0; min-height: 48px; flex: 1; }
.field.compact input { font-size: 1.125rem; }

.note {
  flex: 1.15;
  min-width: 0;
  min-height: 48px;
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: 18px;
  background: var(--inset);
  color: var(--ink);
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s ease, background 0.2s ease;
}
.note:focus {
  border-color: color-mix(in srgb, var(--blue) 55%, transparent);
  background: color-mix(in srgb, var(--blue) 8%, var(--inset));
}
.note::placeholder { color: color-mix(in srgb, var(--ink-2) 70%, transparent); }

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type="number"] { -moz-appearance: textfield; appearance: textfield; }

.chips {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}
#quickChips { grid-template-columns: repeat(5, 1fr); }
#categoryChips { grid-template-columns: repeat(3, 1fr); }
.chip {
  min-height: 36px;
  padding: 0 8px;
  text-align: center;
  border-radius: 999px;
  border: 1px solid var(--hairline);
  background: color-mix(in srgb, var(--fill-solid) 35%, transparent);
  color: var(--ink);
  font-size: 0.9375rem;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: background 0.2s var(--spring), color 0.2s ease, transform 0.2s var(--spring), border-color 0.2s ease;
}
.chip:hover { background: color-mix(in srgb, var(--blue) 10%, transparent); }
.chip:active { transform: scale(0.96); }
.chip.active {
  background: var(--blue);
  border-color: transparent;
  color: #fff;
}

.error {
  min-height: 1.25em;
  margin: 10px 2px 0;
  color: var(--red);
  font-size: 0.8125rem;
}
.error.shake { animation: shake 0.36s var(--spring); }

.primary, .secondary {
  width: 100%;
  min-height: 50px;
  margin-top: 8px;
  border: none;
  border-radius: 16px;
  font-size: 1.0625rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.18s var(--spring), background 0.18s ease, filter 0.18s ease;
}
.primary {
  background: var(--blue);
  color: #fff;
}
.primary:hover { background: var(--blue-press); }
.primary:active, .secondary:active { transform: scale(0.97); }
.secondary {
  background: var(--inset);
  color: var(--ink);
}

.hero { animation: rise 0.5s var(--spring) both; }
.hero-top { display: flex; align-items: center; justify-content: space-between; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 26px;
  padding: 0 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--inset);
  color: var(--ink-2);
}
.pill::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}
.pill.ok { color: var(--green); background: color-mix(in srgb, var(--green) 14%, transparent); }
.pill.bad { color: var(--red); background: color-mix(in srgb, var(--red) 14%, transparent); }

.balance {
  margin-top: 8px;
  font-weight: 650;
  font-size: clamp(2.75rem, 13vw, 3.75rem);
  letter-spacing: -0.045em;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}
.balance .dec {
  font-size: 0.46em;
  font-weight: 550;
  color: var(--ink-2);
  letter-spacing: -0.03em;
}
.balance.negative { color: var(--red); }
.balance.negative .dec { color: color-mix(in srgb, var(--red) 70%, var(--ink-2)); }
.balance.bump { animation: bump 0.42s var(--spring); }

.rate {
  margin-top: 6px;
  color: var(--ink-2);
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
}

.track {
  margin-top: 18px;
  height: 6px;
  border-radius: 999px;
  background: var(--inset);
  overflow: hidden;
}
.track-fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: var(--blue);
  transition: width 0.25s linear;
}

.meta {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  color: var(--ink-2);
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
}

.pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--hairline);
}
.pair div { display: flex; flex-direction: column; gap: 2px; }
.pair span { color: var(--ink-2); font-size: 0.8125rem; }
.pair strong {
  font-size: 0.9375rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.pair .out { color: var(--red); }

.spend { animation: rise 0.55s var(--spring) 0.05s both; }
.spend-row { display: flex; gap: 8px; }

.list { list-style: none; margin-top: 8px; }
.item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 56px;
  padding: 8px 0;
  border-top: 1px solid var(--hairline);
}
.item.is-new { animation: rise 0.35s var(--spring) both; }
.tag {
  flex-shrink: 0;
  min-width: 40px;
  text-align: center;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--inset);
  color: var(--ink-2);
  font-size: 0.75rem;
  font-weight: 600;
}
.item-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.item-name {
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.item-time { color: var(--ink-2); font-size: 0.75rem; }
.item-amt {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--red);
  letter-spacing: -0.02em;
}
.del {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-2);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  transition: background 0.18s ease, color 0.18s ease, transform 0.18s var(--spring);
}
.del:hover { background: color-mix(in srgb, var(--red) 12%, transparent); color: var(--red); }
.del:active { transform: scale(0.92); }

.empty {
  text-align: center;
  color: var(--ink-2);
  font-size: 0.9375rem;
  padding: 18px 0 4px;
}

.sheet-mask {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fade 0.25s ease;
}
.sheet {
  width: min(100%, 440px);
  border-radius: 28px;
  animation: sheet 0.42s var(--spring);
}
.sheet h2 {
  font-size: 1.25rem;
  font-weight: 650;
  letter-spacing: -0.02em;
}
.sheet .field { margin-top: 16px; }
.bg-frame {
  margin-top: 16px;
  height: 168px;
  border-radius: 18px;
  background: var(--inset) center / cover no-repeat;
  display: grid;
  place-items: center;
  color: var(--ink-2);
  font-size: 0.9375rem;
  overflow: hidden;
}
.bg-frame.has-image span { display: none; }
.sheet-actions { display: flex; gap: 10px; margin-top: 8px; }
.sheet-actions .primary,
.sheet-actions .secondary { margin-top: 0; }

:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(10px) scale(0.985); }
  to { opacity: 1; transform: none; }
}
@keyframes bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.025); }
  100% { transform: scale(1); }
}
@keyframes sheet {
  from { opacity: 0; transform: translateY(24px) scale(0.98); }
  to { opacity: 1; transform: none; }
}
@keyframes fade { from { opacity: 0; } }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  30% { transform: translateX(-4px); }
  60% { transform: translateX(4px); }
}

@media (max-width: 420px) {
  .spend-row { flex-direction: column; }
  .note { min-height: 48px; }
}

.back { margin-left: -6px; }

.bill-total {
  margin-top: 8px;
  font-weight: 650;
  font-size: clamp(2.5rem, 12vw, 3.25rem);
  letter-spacing: -0.045em;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}

.pie-box {
  display: grid;
  place-items: center;
  margin: 8px 0 4px;
}
#billPie { width: 196px; height: 196px; overflow: visible; outline: none; }
#billPie .slice {
  cursor: pointer;
  transform-box: view-box;
  transform-origin: center;
  outline: none;
  transition: transform 0.55s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.45s ease;
}
#billPie .slice:focus,
#billPie .slice:focus-visible { outline: none; }
#billPie.has-hot .slice { opacity: 0.38; }
#billPie.has-hot .slice.is-hot {
  opacity: 1;
  transform: scale(1.12);
}

.legend { list-style: none; }
.legend li {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  margin: 0 -8px;
  padding: 0 8px;
  border-radius: 12px;
  font-variant-numeric: tabular-nums;
  transition: background 0.45s ease;
}
.legend li.is-hot {
  background: color-mix(in srgb, var(--ink) 7%, transparent);
}
.list .item.is-hot {
  margin: 0 -8px;
  padding-left: 8px;
  padding-right: 8px;
  border-radius: 14px;
  background: color-mix(in srgb, var(--blue) 10%, transparent);
}
.swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.legend .name { flex: 1; min-width: 0; }
.legend .pct {
  color: var(--ink-2);
  font-size: 0.8125rem;
  min-width: 3.2em;
  text-align: right;
}
.legend .amt { font-weight: 600; letter-spacing: -0.02em; }

#billView { padding-bottom: 92px; }
#billView .panel { animation: rise 0.45s var(--spring) both; }
#billView .panel:nth-child(2) { animation-delay: 0.06s; }

.share-dock {
  position: fixed;
  z-index: 6;
  left: 50%;
  bottom: max(14px, env(safe-area-inset-bottom));
  width: min(408px, calc(100% - 32px));
  transform: translateX(-50%);
  padding: 10px;
  border-radius: 22px;
  background: var(--fill);
  border: 1px solid var(--line);
  box-shadow: var(--shadow), inset 0 1px 0 var(--line);
  backdrop-filter: blur(40px) saturate(1.85);
  -webkit-backdrop-filter: blur(40px) saturate(1.85);
  animation: dock-in 0.5s var(--spring) 0.18s both;
}
.share-dock .primary { margin-top: 0; }

@keyframes dock-in {
  from { opacity: 0; transform: translateX(-50%) translateY(28px) scale(0.98); }
  to { opacity: 1; transform: translateX(-50%); }
}

@media (prefers-reduced-transparency: reduce) {
  .bar, .panel, .sheet, .share-dock {
    background: var(--fill-solid);
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  .sheet-mask { backdrop-filter: none; -webkit-backdrop-filter: none; }
}

@media (prefers-contrast: more) {
  .bar, .panel, .chip, .field, .note {
    border-color: var(--line-strong);
  }
  .pair { border-top-color: var(--line-strong); }
}

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