:root {
  color-scheme: light dark;
  --tg-bg: #eef2f5;
  --tg-surface: #ffffff;
  --tg-surface-muted: #f5f7fa;
  --tg-text: #17212b;
  --tg-muted: #708499;
  --tg-line: rgba(15, 35, 53, 0.08);
  --tg-accent: #2aabee;
  --tg-accent-pressed: #2297d4;
  --tg-accent-soft: rgba(42, 171, 238, 0.12);
  --tg-button-text: #ffffff;
  --tg-success: #31b545;
  --tg-shadow: 0 12px 32px rgba(16, 38, 59, 0.10);
  --app-bg:
    radial-gradient(circle at top, rgba(42, 171, 238, 0.18), transparent 38%),
    linear-gradient(180deg, #edf3f8 0%, var(--tg-bg) 48%, #e8eef4 100%);
  --app-frame-bg: rgba(255, 255, 255, 0.48);
  --app-frame-border: rgba(255, 255, 255, 0.8);
  --app-frame-shadow: 0 24px 72px rgba(13, 37, 62, 0.12);
  --empty-bg: linear-gradient(180deg, #f8fbfe 0%, #f2f6fa 100%);
  --empty-border: rgba(112, 132, 153, 0.22);
  --empty-icon-bg: #ffffff;
  --receipt-item-border: rgba(24, 44, 63, 0.05);
  --skeleton-bg:
    linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.85) 50%, rgba(255, 255, 255, 0) 100%),
    linear-gradient(180deg, #edf3f8 0%, #e7eef5 100%);
  --quantity-bg: #ffffff;
  --quantity-border: rgba(42, 171, 238, 0.18);
  --summary-bg: rgba(255, 255, 255, 0.96);
  --summary-border: rgba(24, 44, 63, 0.08);
  --summary-shadow: 0 10px 28px rgba(15, 35, 53, 0.16);
  --disabled-bg: #eef2f5;
  --disabled-text: #9aabbb;
  --sheet-backdrop: rgba(23, 33, 43, 0.34);
  --sheet-shadow: 0 -18px 42px rgba(15, 35, 53, 0.18);
  --handle-bg: rgba(112, 132, 153, 0.28);
  --radius-xl: 24px;
  --radius-lg: 18px;
  --radius-md: 14px;
}

:root[data-theme="dark"] {
  --tg-bg: #17212b;
  --tg-surface: #1f2a36;
  --tg-surface-muted: #263241;
  --tg-text: #f5f7fa;
  --tg-muted: #9aa8b5;
  --tg-line: rgba(255, 255, 255, 0.08);
  --tg-accent: #5288c1;
  --tg-accent-pressed: #4577ac;
  --tg-accent-soft: rgba(82, 136, 193, 0.18);
  --tg-button-text: #ffffff;
  --tg-shadow: 0 16px 42px rgba(0, 0, 0, 0.28);
  --app-bg:
    radial-gradient(circle at top, rgba(82, 136, 193, 0.24), transparent 38%),
    linear-gradient(180deg, #111922 0%, var(--tg-bg) 50%, #0f1720 100%);
  --app-frame-bg: rgba(31, 42, 54, 0.72);
  --app-frame-border: rgba(255, 255, 255, 0.07);
  --app-frame-shadow: 0 24px 72px rgba(0, 0, 0, 0.34);
  --empty-bg: linear-gradient(180deg, #202c39 0%, #1b2531 100%);
  --empty-border: rgba(154, 168, 181, 0.22);
  --empty-icon-bg: #263241;
  --receipt-item-border: rgba(255, 255, 255, 0.05);
  --skeleton-bg:
    linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.10) 50%, rgba(255, 255, 255, 0) 100%),
    linear-gradient(180deg, #263241 0%, #202b38 100%);
  --quantity-bg: #1a2430;
  --quantity-border: rgba(82, 136, 193, 0.28);
  --summary-bg: rgba(31, 42, 54, 0.94);
  --summary-border: rgba(255, 255, 255, 0.08);
  --summary-shadow: 0 10px 30px rgba(0, 0, 0, 0.32);
  --disabled-bg: #263241;
  --disabled-text: #6f7d8a;
  --sheet-backdrop: rgba(0, 0, 0, 0.52);
  --sheet-shadow: 0 -18px 42px rgba(0, 0, 0, 0.34);
  --handle-bg: rgba(154, 168, 181, 0.30);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  background: var(--app-bg);
  color: var(--tg-text);
}

.page-shell {
  width: min(720px, calc(100vw - 20px));
  margin: 0 auto;
  padding: 14px 0 28px;
}

.app-frame {
  background: var(--app-frame-bg);
  border: 1px solid var(--app-frame-border);
  border-radius: 30px;
  box-shadow: var(--app-frame-shadow);
  overflow: hidden;
}

.hero {
  padding: 20px 20px 8px;
}

.hero h1 {
  margin: 0;
  text-align: center;
  font-size: clamp(2rem, 7vw, 2.8rem);
  letter-spacing: -0.045em;
}

.scan-status {
  display: block;
  margin-top: 12px;
  font-size: 0.9rem;
  line-height: 1.45;
  color: var(--tg-muted);
}

.scan-status[data-mode="error"] {
  color: var(--tg-text);
  font-weight: 600;
}

.content-stack {
  display: grid;
  gap: 12px;
  padding: 8px 12px 24px;
}

.app-frame.has-receipt .content-stack {
  padding: 8px 12px calc(148px + env(safe-area-inset-bottom, 0px));
}

.surface {
  background: var(--tg-surface);
  border: 1px solid var(--tg-line);
  border-radius: var(--radius-xl);
  box-shadow: var(--tg-shadow);
  padding: 18px;
}

.section-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
}

.section-head h2 {
  margin: 0;
  letter-spacing: -0.03em;
  font-size: 1.28rem;
}

.scanner-actions {
  margin: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

button,
.upload-button {
  border: 0;
  cursor: pointer;
  font: inherit;
  border-radius: 16px;
  padding: 14px 16px;
  transition: transform 140ms ease, opacity 140ms ease, background 140ms ease, border-color 140ms ease;
  font-weight: 600;
}

button:active {
  transform: translateY(1px) scale(0.995);
}

.primary-button {
  background: var(--tg-accent);
  color: var(--tg-button-text);
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.08);
}

.primary-button:hover {
  background: var(--tg-accent-pressed);
}

.ghost-button {
  background: transparent;
  border: 1px solid var(--tg-line);
  color: var(--tg-text);
}

.secondary-button {
  background: var(--tg-surface-muted);
  border: 1px solid transparent;
  color: var(--tg-text);
}

.upload-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#scanner-video {
  width: 100%;
  border-radius: var(--radius-lg);
  background: #0b1116;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  margin-top: 14px;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.empty-state {
  margin-top: 14px;
  padding: 28px 20px;
  border-radius: var(--radius-lg);
  background: var(--empty-bg);
  color: var(--tg-muted);
  border: 1px dashed var(--empty-border);
  text-align: center;
}

.empty-state__icon {
  width: 56px;
  height: 56px;
  margin: 0 auto 12px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  background: var(--empty-icon-bg);
  color: var(--tg-accent);
  font-size: 1.8rem;
  box-shadow: 0 10px 24px rgba(30, 76, 117, 0.08);
}

.empty-state__title {
  font-weight: 700;
  color: var(--tg-text);
  margin-bottom: 6px;
}

.empty-state__text {
  font-size: 0.95rem;
  line-height: 1.45;
}

.items-list {
  margin-top: 14px;
  display: grid;
  gap: 10px;
}

.items-list.is-loading {
  gap: 12px;
}

.receipt-skeleton {
  height: 96px;
  border-radius: var(--radius-lg);
  background: var(--skeleton-bg);
  background-size: 220px 100%, 100% 100%;
  background-repeat: no-repeat;
  animation: skeleton-shimmer 1.2s linear infinite;
}

.receipt-item {
  padding: 14px;
  border-radius: var(--radius-lg);
  background: var(--tg-surface-muted);
  border: 1px solid var(--receipt-item-border);
  display: grid;
  gap: 10px;
  opacity: 0;
  transform: translateY(8px);
  animation: card-enter 220ms ease forwards;
}

.receipt-item__top {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: flex-start;
}

.receipt-item__name {
  font-weight: 700;
  line-height: 1.35;
}

.receipt-item__meta {
  color: var(--tg-muted);
  font-size: 0.88rem;
  margin-top: 4px;
}

.receipt-item__total {
  min-width: max-content;
  max-width: 118px;
  text-align: right;
  white-space: nowrap;
  font-size: clamp(0.92rem, 3.4vw, 1rem);
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
}

.item-quantity {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.quantity-controls {
  display: grid;
  grid-template-columns: 34px clamp(40px, 8vw, 64px) 34px;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  margin-left: auto;
}

.quantity-button {
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 12px;
  background: var(--quantity-bg);
  color: var(--tg-accent);
  border: 1px solid var(--quantity-border);
}

.quantity-value {
  min-width: 0;
  white-space: nowrap;
  text-align: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.split-link {
  width: auto;
  flex: 0 0 auto;
  padding: 0 12px;
  font-size: 0.88rem;
  font-weight: 600;
  text-align: center;
}

.summary-bar {
  position: fixed;
  left: 50%;
  bottom: 10px;
  z-index: 20;
  width: min(720px, calc(100vw - 20px));
  margin: 0;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px));
  border-radius: 16px;
  background: var(--summary-bg);
  backdrop-filter: blur(20px);
  border: 1px solid var(--summary-border);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  box-shadow: var(--summary-shadow);
  transform: translateX(-50%);
}

.summary-label {
  color: var(--tg-muted);
  font-size: 0.88rem;
}

.summary-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
}

.summary-meta strong {
  font-size: clamp(1.36rem, 5.3vw, 1.95rem);
  letter-spacing: 0;
  line-height: 1;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.summary-actions {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  flex: 0 0 auto;
}

.bank-action-button,
.secondary-action-button {
  min-width: 88px;
  padding: 11px 12px;
  border-radius: 14px;
  font-size: 0.9rem;
}

.bank-action-button {
  background: var(--tg-surface-muted);
  color: var(--tg-text);
  border: 1px solid rgba(24, 44, 63, 0.08);
}

.secondary-action-button {
  background: var(--tg-accent);
  color: var(--tg-button-text);
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.08);
}

.secondary-action-button[disabled] {
  background: var(--disabled-bg);
  color: var(--disabled-text);
  border-color: var(--receipt-item-border);
  cursor: default;
}

.bank-sheet {
  position: fixed;
  inset: 0;
  z-index: 40;
}

.split-sheet {
  position: fixed;
  inset: 0;
  z-index: 45;
}

.bank-sheet__backdrop,
.split-sheet__backdrop {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  padding: 0;
  border-radius: 0;
  background: var(--sheet-backdrop);
  opacity: 0;
  transition: opacity 180ms ease;
}

.bank-sheet__panel,
.split-sheet__panel {
  position: absolute;
  left: 50%;
  bottom: 0;
  width: min(720px, 100vw);
  padding: 10px 14px calc(18px + env(safe-area-inset-bottom, 0px));
  border-radius: 22px 22px 0 0;
  background: var(--tg-surface);
  box-shadow: var(--sheet-shadow);
  transform: translate(-50%, 100%);
  transition: transform 180ms ease;
}

.bank-sheet.is-open .bank-sheet__backdrop,
.split-sheet.is-open .split-sheet__backdrop {
  opacity: 1;
}

.bank-sheet.is-open .bank-sheet__panel,
.split-sheet.is-open .split-sheet__panel {
  transform: translate(-50%, 0);
}

.bank-sheet__handle,
.split-sheet__handle {
  width: 42px;
  height: 4px;
  margin: 0 auto 12px;
  border-radius: 999px;
  background: var(--handle-bg);
}

.bank-sheet__head,
.split-sheet__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.bank-sheet__head h2,
.split-sheet__head h2 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: 0;
}

.bank-sheet__close,
.split-sheet__close {
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 12px;
  background: var(--tg-surface-muted);
  color: var(--tg-muted);
}

.split-sheet__item {
  margin: 4px 0 0;
  color: var(--tg-muted);
  font-size: 0.88rem;
  line-height: 1.35;
}

.split-selector {
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr) 54px;
  align-items: center;
  gap: 12px;
  margin: 14px 0;
}

.split-selector__button {
  width: 54px;
  height: 54px;
  padding: 0;
  border-radius: 18px;
  background: var(--tg-surface-muted);
  color: var(--tg-accent);
  border: 1px solid var(--tg-line);
  font-size: 1.35rem;
}

.split-selector__button[disabled] {
  color: var(--disabled-text);
  cursor: default;
}

.split-selector__value {
  display: grid;
  place-items: center;
  gap: 3px;
  min-height: 68px;
  border-radius: 20px;
  background: var(--tg-surface-muted);
  border: 1px solid var(--tg-line);
}

.split-selector__value strong {
  font-size: 2rem;
  line-height: 1;
}

.split-selector__value span {
  color: var(--tg-muted);
  font-size: 0.86rem;
}

.split-sheet__apply {
  width: 100%;
  margin-top: 12px;
}

.bank-list {
  display: grid;
  gap: 8px;
}

.bank-option {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 12px;
  border-radius: 14px;
  background: var(--tg-surface-muted);
  color: var(--tg-text);
  text-align: left;
}

.bank-option__icon {
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  border-radius: 10px;
}

.bank-sheet__hint,
.split-sheet__hint {
  margin: 12px 2px 0;
  color: var(--tg-muted);
  font-size: 0.86rem;
  line-height: 1.4;
}

.debug-panel {
  position: fixed;
  left: 8px;
  right: 8px;
  top: 8px;
  z-index: 80;
  max-height: 42vh;
  margin: 0;
  padding: 10px;
  overflow: auto;
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.92);
  color: #d7fbe8;
  font: 11px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
}

@keyframes skeleton-shimmer {
  from {
    background-position: -220px 0, 0 0;
  }
  to {
    background-position: calc(100% + 220px) 0, 0 0;
  }
}

@keyframes card-enter {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 620px) {
  .page-shell {
    width: 100%;
    padding: 0;
  }

  .app-frame {
    min-height: 100vh;
    border-radius: 0;
    box-shadow: none;
    border: 0;
  }

  .hero {
    padding: 18px 16px 8px;
  }

  .content-stack {
    padding: 8px 10px 24px;
  }

  .app-frame.has-receipt .content-stack {
    padding: 8px 10px calc(156px + env(safe-area-inset-bottom, 0px));
  }

  .surface {
    padding: 16px;
  }

  .scanner-actions > * {
    flex: 1 1 calc(50% - 5px);
  }

  .section-head {
    align-items: flex-start;
  }

  .summary-bar {
    bottom: 8px;
    width: calc(100vw - 16px);
  }

  .summary-actions {
    width: auto;
  }

  .secondary-action-button {
    width: auto;
  }

  .bank-action-button,
  .secondary-action-button {
    min-width: 0;
    padding-inline: 10px;
  }
}


.receipt-loading {
  padding: 28px 16px;
  text-align: center;
}

.receipt-loading p {
  margin: 16px 0 0;
  color: var(--tg-muted);
  font-size: 0.95rem;
}

.loading-dots {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  height: 24px;
}

.loading-dots span {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--tg-accent);
  animation: loading-bounce 1s ease-in-out infinite;
}

.loading-dots span:nth-child(2) { animation-delay: 0.15s; }
.loading-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes loading-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-8px); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .loading-dots span { animation: none; }
}


/* Keep both sharing actions in one row below the total. */
.summary-actions > button { flex: 1 1 0; }
#send-self-button {
  background: var(--quantity-bg);
  color: var(--tg-accent);
  border: 1px solid var(--quantity-border);
}
