:root {
  font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
  --bg: #0f1115;
  --card: #171a21;
  --ink: #f3f4f6;
  --accent: #14b8a6;
  --accent-dark: #0f766e;
  --muted: #9ca3af;
  --border: #2b2f3a;
}

body[data-theme="light"] {
  --bg: #f4f1ea;
  --card: #ffffff;
  --ink: #1f1f1f;
  --accent: #0f766e;
  --accent-dark: #0b5e57;
  --muted: #6b6b6b;
  --border: #e2ddd2;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: radial-gradient(circle at top left, #1f2937 0%, var(--bg) 45%, #0b0d12 100%);
  color: var(--ink);
}

body[data-theme="light"] {
  background: radial-gradient(circle at top left, #fff6e6 0%, #f4f1ea 40%, #ece7dd 100%);
}

.page {
  max-width: 860px;
  margin: 0 auto;
  padding: 32px 20px 60px;
}

.header {
  margin-bottom: 24px;
}

.header h1 {
  margin: 0 0 8px;
  font-size: 36px;
  letter-spacing: -0.02em;
}

.header p {
  margin: 0;
  color: var(--muted);
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.06);
  margin-bottom: 24px;
}

.view {
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

label {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 14px;
  margin-bottom: 16px;
}

input[type="text"],
input[type="file"] {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 15px;
  background: var(--card);
  color: var(--ink);
}

select,
input[type="number"] {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 15px;
  background: var(--card);
  color: var(--ink);
}

input::placeholder {
  color: var(--muted);
}

body[data-theme="dark"] input,
body[data-theme="dark"] select {
  background: #11151c;
}

button {
  background: var(--accent);
  border: none;
  color: white;
  padding: 12px 18px;
  border-radius: 999px;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  min-height: 44px;
}

.crop-actions button:first-child {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--border);
}

button:hover {
  background: var(--accent-dark);
}

.items {
  display: grid;
  gap: 10px;
  margin-bottom: 16px;
}

.item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 1px solid var(--border);
  padding: 14px 16px;
  border-radius: 12px;
  background: var(--card);
}

.item-row:not(.edit-row) {
  cursor: pointer;
}

.item-row:not(.edit-row).paid {
  cursor: default;
}

.item-row label {
  flex: 1;
  margin: 0;
  font-size: 15px;
}

.item-row input[type="checkbox"] {
  transform: scale(1.4);
  margin-right: 12px;
  flex-shrink: 0;
}

.translated-label {
  color: var(--muted);
  font-size: 13px;
  margin-left: 4px;
}

/* --- Edit row: collapsed / expanded states --- */
.edit-row {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 8px;
  align-items: center;
}

.edit-row .edit-field {
  display: none !important;
}

.edit-row.editing .edit-field {
  display: block !important;
}

.edit-row.editing .edit-field.edit-label-cell {
  display: flex !important;
}

.edit-row .edit-display {
  display: inline;
}

.edit-row.editing .edit-display {
  display: none !important;
}

/* Expanded layout: full-width label row, then amount + stepper + remove + done */
.edit-row.editing {
  grid-template-columns: 1fr 100px auto auto;
  grid-template-rows: auto auto;
}

.edit-row.editing .edit-label-cell {
  grid-column: 1 / -1;
  grid-row: 1;
}

.edit-row.editing input[inputmode="decimal"] {
  grid-column: 1;
  grid-row: 2;
}

.edit-row.editing .stepper-wrap {
  grid-column: 2;
  grid-row: 2;
}

.edit-row.editing .edit-remove-btn {
  grid-column: 3;
  grid-row: 2;
}

.edit-row.editing .edit-done-btn {
  grid-column: 4;
  grid-row: 2;
}

.edit-display-label {
  flex: 1;
  font-size: 15px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.edit-display-amount {
  font-size: 14px;
  color: var(--muted);
  flex-shrink: 0;
}

.edit-pencil-btn {
  width: 44px;
  height: 44px;
  min-height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  font-size: 18px;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
  display: grid;
  place-items: center;
}

.edit-pencil-btn:hover {
  background: var(--border);
}

.edit-done-btn {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  width: auto;
  min-height: 44px;
}

.edit-done-btn:hover {
  background: var(--accent-dark);
}

.stepper-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stepper-label {
  font-size: 11px;
  color: var(--muted);
  text-align: center;
}

.item-amount-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  flex-shrink: 0;
}

.item-each {
  font-size: 12px;
  color: var(--muted);
  opacity: 0.7;
}

.guest-split-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

.guest-split-toggle {
  width: auto;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 999px;
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  min-height: 32px;
  cursor: pointer;
}

.guest-split-toggle:hover {
  background: var(--accent);
  color: white;
}

.guest-split-wrap .guest-split-stepper {
  display: none;
}

.guest-split-wrap.open .guest-split-toggle {
  display: none;
}

.guest-split-wrap.open .guest-split-stepper {
  display: flex;
  align-items: center;
  gap: 2px;
}

.guest-split-stepper {
  display: flex;
  align-items: center;
  gap: 2px;
}

.guest-split-btn {
  width: 32px;
  height: 32px;
  min-height: 32px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  font-size: 16px;
  font-weight: 600;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  line-height: 1;
}

.guest-split-btn:hover {
  background: var(--border);
}

.guest-split-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.guest-split-value {
  min-width: 22px;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
}

body[data-theme="dark"] .guest-split-btn {
  background: #11151c;
}

.edit-label-cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.edit-label-cell input {
  width: 100%;
}

.edit-row input {
  margin: 0;
}

.edit-row button.edit-remove-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--ink);
  width: auto;
  min-height: 44px;
}

.stepper {
  display: flex;
  align-items: center;
  gap: 4px;
}

.stepper-btn {
  width: 44px;
  height: 44px;
  min-height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  font-size: 20px;
  font-weight: 600;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  line-height: 1;
}

.stepper-btn:hover {
  background: var(--border);
}

.stepper-value {
  min-width: 28px;
  text-align: center;
  font-size: 16px;
  font-weight: 600;
}

.item-row.paid {
  opacity: 0.5;
  border-color: var(--accent);
}

.item-row.paid label {
  text-decoration: line-through;
}

.item-paid-badge {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  flex-shrink: 0;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--accent);
}

body[data-theme="dark"] .item-row {
  background: #10141b;
}

body[data-theme="dark"] .edit-row button.edit-remove-btn {
  background: #11151c;
  color: var(--ink);
}

body[data-theme="dark"] .stepper-btn {
  background: #11151c;
}

body[data-theme="dark"] .edit-pencil-btn {
  background: #11151c;
}

.total {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-top: 1px solid var(--border);
  margin-bottom: 16px;
}

.result {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: 12px;
  padding: 12px;
  color: #166534;
}

.result.error {
  background: #fef2f2;
  border-color: #fecaca;
  color: #991b1b;
}

body[data-theme="dark"] .result {
  background: rgba(20, 184, 166, 0.12);
  border-color: rgba(20, 184, 166, 0.35);
  color: #5eead4;
}

body[data-theme="dark"] .result.error {
  background: rgba(248, 113, 113, 0.12);
  border-color: rgba(248, 113, 113, 0.35);
  color: #fecaca;
}

.hidden {
  display: none;
}

.qr {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-top: 12px;
}

.qr img {
  width: 140px;
  height: 140px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: white;
}

.copy-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 10px;
}

.copy-row input {
  flex: 1;
}

.copy-row button {
  width: auto;
  padding: 10px 16px;
}

.summary {
  margin-bottom: 16px;
  color: var(--muted);
}

#view-crop .summary {
  margin-top: 8px;
}

.cropper {
  position: relative;
  width: 100%;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #000;
  min-height: 320px;
  touch-action: none;
}

.cropper img {
  display: block;
  width: 100%;
  height: auto;
  user-select: none;
  -webkit-user-drag: none;
}

.crop-overlay {
  position: absolute;
  border: 2px solid #00b3a4;
  background: rgba(0, 179, 164, 0.15);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.35);
  border-radius: 8px;
  pointer-events: none;
}

.crop-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 16px;
}

#share-panel {
  margin-top: 8px;
}

#share-items-list {
  margin-top: 16px;
  display: grid;
  gap: 8px;
}

.share-item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid var(--border);
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--card);
  gap: 12px;
  transition: border-color 0.2s, opacity 0.2s;
}

.share-item-row.claimed {
  border-color: var(--accent);
}

.share-item-label {
  flex: 1;
  font-size: 15px;
}

.share-item-amount {
  flex-shrink: 0;
  color: var(--muted);
  font-size: 14px;
}

.share-item-status {
  flex-shrink: 0;
  font-size: 13px;
  color: var(--muted);
  min-width: 70px;
  text-align: right;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.share-item-row.claimed .share-item-status {
  color: var(--accent);
  font-weight: 600;
}

body[data-theme="dark"] .share-item-row {
  background: #10141b;
}

.share-totals {
  margin-top: 16px;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.total-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
  font-size: 14px;
  color: var(--muted);
}

.total-row-grand {
  font-weight: 600;
  font-size: 16px;
  color: var(--ink);
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}

.share-totals-disclaimer {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--muted);
  opacity: 0.7;
}

.share-selections-detail {
  margin-top: 16px;
}

.share-selections-detail strong {
  display: block;
  margin-bottom: 8px;
  font-size: 15px;
}

.selection-entry {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 8px;
  background: var(--card);
}

body[data-theme="dark"] .selection-entry {
  background: #10141b;
}

.selection-entry-header {
  display: flex;
  align-items: center;
  font-size: 14px;
  font-weight: 600;
  gap: 8px;
}

.selection-entry-header > span:first-child {
  flex: 1;
}

.invoice-copy-btn {
  all: unset;
  cursor: pointer;
  font-size: 11px;
  font-weight: 400;
  color: var(--muted);
  opacity: 0.6;
  padding: 2px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  white-space: nowrap;
  width: auto;
}

.invoice-copy-btn:hover {
  opacity: 1;
  background: var(--border);
}

.host-url-hint {
  margin: 12px 0 0;
  font-size: 13px;
  color: var(--muted);
}

#new-upload {
  margin-top: 16px;
}

.review {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 16px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
}

.review img {
  width: 100%;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.review pre {
  margin: 0;
  white-space: pre-wrap;
  font-size: 13px;
  max-height: 320px;
  overflow: auto;
  background: rgba(0, 0, 0, 0.3);
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.theme-toggle {
  position: fixed;
  top: 16px;
  right: 16px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--border);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.25);
  z-index: 10;
}

.theme-toggle:hover {
  background: var(--card);
}

@media (max-width: 640px) {
  .page {
    padding: 20px 16px 40px;
  }

  .qr {
    flex-direction: column;
    align-items: flex-start;
  }

  .review {
    grid-template-columns: 1fr;
  }

  .card {
    padding: 18px;
  }

  .edit-row:not(.editing) {
    grid-template-columns: auto 1fr auto;
    grid-template-rows: auto auto;
  }

  .edit-row:not(.editing) .edit-display-label {
    grid-column: 1 / -1;
    grid-row: 1;
  }

  .edit-row:not(.editing) .edit-display-amount {
    grid-column: 1;
    grid-row: 2;
  }

  .edit-row:not(.editing) .stepper-wrap {
    grid-column: 2;
    grid-row: 2;
    justify-self: center;
  }

  .edit-row:not(.editing) .edit-pencil-btn {
    grid-column: 3;
    grid-row: 2;
    justify-self: end;
  }

  .edit-row.editing {
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto auto;
  }

  .edit-row.editing .edit-label-cell {
    grid-column: 1 / -1;
    grid-row: 1;
  }

  .edit-row.editing input[inputmode="decimal"] {
    grid-column: 1;
    grid-row: 2;
  }

  .edit-row.editing .stepper-wrap {
    grid-column: 2;
    grid-row: 2;
    justify-self: end;
  }

  .edit-row.editing .edit-remove-btn {
    grid-column: 1;
    grid-row: 3;
  }

  .edit-row.editing .edit-done-btn {
    grid-column: 2;
    grid-row: 3;
  }

  .crop-actions {
    grid-template-columns: 1fr;
  }
}
