:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --text: #1c1e26;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

button, .btn, .btn-icon, input, select, textarea, a {
  touch-action: manipulation;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top)) 20px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.25rem; margin: 0; }
.topbar-actions { display: flex; gap: 10px; align-items: center; }

#searchInput {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  min-width: 160px;
}

main.view { max-width: 900px; margin: 0 auto; padding: 20px; }

.btn {
  padding: 9px 16px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}
.btn:hover { background: #f0f1f5; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: #fff; }
.btn-danger:hover { background: var(--danger-hover); color: #fff; }
.btn-back { margin-bottom: 12px; }
.btn-icon {
  border: none; background: none; cursor: pointer; font-size: 1.05rem;
  padding: 4px 6px; border-radius: 6px;
}
.btn-icon:hover { background: #eceefb; }

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 14px;
}

.card-tile {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  cursor: pointer;
  border-left: 6px solid var(--primary);
  transition: transform 0.1s ease;
}
.card-tile:hover { transform: translateY(-2px); }
.card-tile .tile-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.card-tile .tile-heading { display: flex; align-items: center; gap: 10px; min-width: 0; }
.card-tile .tile-photo {
  width: 40px; height: 40px; border-radius: 8px; object-fit: cover; flex-shrink: 0;
}
.card-tile h3 { margin: 0; font-size: 1.05rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-tile .tile-number { color: var(--text-muted); font-size: 0.85rem; margin-top: 6px; font-variant-numeric: tabular-nums; }
.card-tile .tile-balance { font-size: 1.3rem; font-weight: 700; margin-top: 10px; }
.card-tile .tile-usage {
  font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.03em;
  color: var(--text-muted); background: #f0f1f5; padding: 3px 8px; border-radius: 20px;
}
.card-tile.archived { opacity: 0.55; }

.empty-state { color: var(--text-muted); text-align: center; margin-top: 40px; }
.show-archived { display: block; margin-top: 20px; color: var(--text-muted); font-size: 0.9rem; }

/* Detail view */
.detail-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
}
.detail-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.detail-head h2 { margin: 0; }
.color-dot { width: 16px; height: 16px; border-radius: 50%; background: var(--primary); display: inline-block; }
.usage-badge {
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em;
  color: var(--text-muted); background: #f0f1f5; padding: 3px 10px; border-radius: 20px;
}

.detail-photo {
  display: block; width: 100%; max-height: 280px; object-fit: cover;
  border-radius: var(--radius); margin-top: 16px;
}

.balance-block { margin: 18px 0; }
.balance-label { display: block; color: var(--text-muted); font-size: 0.85rem; }
.balance-amount { font-size: 2.2rem; font-weight: 700; }

.barcode-block {
  background: #fafafa;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin: 18px 0;
}
.barcode-render { display: flex; justify-content: center; background: #fff; padding: 12px; border-radius: 10px; overflow-x: auto; }
.barcode-render svg, .barcode-render img, .barcode-render canvas { max-width: 100%; }
.barcode-render.empty { color: var(--text-muted); padding: 30px; text-align: center; }

.copy-row {
  display: flex; align-items: center; gap: 8px; padding: 10px 4px;
  border-top: 1px solid var(--border);
}
.copy-row:first-of-type { border-top: none; margin-top: 8px; }
.copy-field-label { color: var(--text-muted); font-size: 0.8rem; min-width: 90px; }
.copy-field-value { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 1rem; flex: 1; word-break: break-all; }
.copy-field-value.masked { letter-spacing: 2px; }

.detail-notes { color: var(--text-muted); white-space: pre-wrap; }
.detail-actions { display: flex; gap: 10px; margin: 18px 0; flex-wrap: wrap; }

.tx-section h3 { margin-bottom: 10px; }
.tx-form { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.tx-form input {
  padding: 9px 12px; border: 1px solid var(--border); border-radius: 9px; font-size: 0.9rem;
}
.tx-amount-input { width: 150px; }
.tx-note-input { flex: 1; min-width: 140px; }

.tx-type-toggle {
  display: flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden;
}
.tx-type-btn {
  border: none; background: var(--surface); color: var(--text-muted);
  padding: 9px 16px; font-size: 0.9rem; cursor: pointer;
}
.tx-type-btn.selected:first-child { background: var(--danger); color: #fff; }
.tx-type-btn.selected:last-child { background: #16803c; color: #fff; }

.tx-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.tx-list li {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 10px 12px; background: #fafafa; border-radius: 9px; border: 1px solid var(--border);
}
.tx-amount { font-weight: 700; font-variant-numeric: tabular-nums; }
.tx-amount.positive { color: #16803c; }
.tx-amount.negative { color: var(--danger); }
.tx-meta { color: var(--text-muted); font-size: 0.8rem; }
.tx-del { border: none; background: none; color: var(--text-muted); cursor: pointer; }
.tx-del:hover { color: var(--danger); }

/* Modal */
body.scroll-locked { overflow: hidden; height: 100%; }

.modal-overlay {
  position: fixed; inset: 0; background: rgba(15, 17, 26, 0.5);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 30px 14px; overflow-y: auto; z-index: 100;
  overscroll-behavior: contain;
}
.modal {
  background: var(--surface); border-radius: var(--radius);
  padding: 24px; width: 100%; max-width: 480px; box-shadow: var(--shadow);
}
.modal h2 { margin-top: 0; }
.modal form { display: flex; flex-direction: column; gap: 14px; }
.modal label { display: flex; flex-direction: column; gap: 5px; font-size: 0.85rem; color: var(--text-muted); }
.modal label small { color: #9ca3af; }
.modal input, .modal select, .modal textarea {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px;
  font-size: 0.95rem; color: var(--text); font-family: inherit;
}
.field-row { display: flex; gap: 8px; align-items: flex-end; }
.field-row .grow { flex: 1; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }

.photo-preview {
  display: block; max-width: 100%; max-height: 160px; border-radius: 10px;
  margin-bottom: 8px; object-fit: cover;
}
.photo-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.photo-actions input[type="file"] { flex: 1; min-width: 0; font-size: 0.85rem; }

.color-swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.color-swatch {
  width: 28px; height: 28px; border-radius: 50%; cursor: pointer; border: 2px solid transparent;
}
.color-swatch.selected { border-color: var(--text); }

.scan-video-wrap {
  background: #000; border-radius: 10px; overflow: hidden; margin: 10px 0;
  aspect-ratio: 4 / 3; display: flex; align-items: center; justify-content: center;
}
.scan-video-wrap video { width: 100%; height: 100%; object-fit: cover; }
.scan-status { color: var(--text-muted); font-size: 0.85rem; text-align: center; }
#scanCameraSelect { width: 100%; padding: 8px; border-radius: 9px; border: 1px solid var(--border); }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1c1e26; color: #fff; padding: 10px 18px; border-radius: 30px;
  font-size: 0.9rem; z-index: 200; box-shadow: var(--shadow);
}

/* Auth */
.auth-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.auth-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 30px;
  width: 100%;
  max-width: 380px;
}
.auth-title { text-align: center; font-size: 1.4rem; margin: 0 0 20px; }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form h2 { margin: 0; font-size: 1.1rem; }
.auth-form label { display: flex; flex-direction: column; gap: 5px; font-size: 0.85rem; color: var(--text-muted); }
.auth-form input {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px;
  font-size: 0.95rem; color: var(--text); font-family: inherit;
}
.auth-error { color: var(--danger); font-size: 0.85rem; margin: 0; }
.auth-switch { text-align: center; font-size: 0.85rem; color: var(--text-muted); margin: 0; }
.auth-switch a { color: var(--primary); text-decoration: none; }
.user-badge { color: var(--text-muted); font-size: 0.85rem; }

@media (max-width: 640px) {
  body { font-size: 16px; }

  main.view { padding: 16px 16px calc(32px + env(safe-area-inset-bottom)); }

  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: calc(16px + env(safe-area-inset-top)) 16px 16px;
  }
  .topbar h1 { text-align: center; font-size: 1.3rem; }
  .topbar-actions { flex-wrap: wrap; justify-content: space-between; gap: 10px; }
  #searchInput { flex: 1 1 100%; min-height: 44px; font-size: 1rem; }
  .topbar-actions .btn { min-height: 44px; }
  .user-badge { flex-basis: 100%; text-align: center; }

  .card-grid { grid-template-columns: 1fr; gap: 16px; }
  .card-tile { padding: 20px; }
  .card-tile .tile-balance { font-size: 1.5rem; }

  .btn { min-height: 44px; padding: 11px 18px; font-size: 1rem; }
  .btn-icon { min-height: 44px; min-width: 44px; font-size: 1.2rem; }

  .detail-card { padding: 20px; }
  .balance-amount { font-size: 2.6rem; }
  .balance-block { margin: 22px 0; }
  .barcode-block { padding: 20px; margin: 22px 0; }
  .copy-row { padding: 14px 4px; }
  .copy-field-value { font-size: 1.1rem; }

  .detail-actions { gap: 12px; margin: 22px 0; }
  .detail-actions .btn { flex: 1 1 calc(50% - 6px); }

  .tx-section { margin-top: 8px; }
  .tx-form { flex-direction: column; gap: 10px; margin-bottom: 18px; align-items: stretch; }
  .tx-form input, .tx-form button { width: 100%; min-height: 46px; font-size: 1rem; }
  .tx-type-toggle { width: 100%; }
  .tx-type-btn { flex: 1; min-height: 46px; }
  .tx-list { gap: 10px; }
  .tx-list li { padding: 14px; }

  /* align-items: flex-start (not stretch) so the sheet still grows naturally
     with tall form content instead of being clipped to the overlay's height,
     which left overflowing fields rendering without the white background. */
  .modal-overlay { padding: 0; align-items: flex-start; }
  .modal {
    max-width: none;
    width: 100%;
    min-height: 100%;
    border-radius: 0;
    padding: calc(20px + env(safe-area-inset-top)) 20px calc(24px + env(safe-area-inset-bottom));
  }
  .modal form { gap: 18px; }
  .modal input, .modal select, .modal textarea { min-height: 46px; font-size: 1rem; }
  .field-row { flex-wrap: wrap; }
  .field-row .grow { flex: 1 1 100%; }
  .field-row #btnScan { width: 100%; min-height: 46px; }
  .modal-actions { flex-direction: column-reverse; gap: 10px; }
  .modal-actions .btn { width: 100%; min-height: 46px; }

  .auth-view { padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
  .auth-card { padding: 24px 20px; }
  .auth-form input, .auth-form button { min-height: 46px; font-size: 1rem; }
}
