:root {
  --primary: #0066cc;
  --primary-dark: #004d99;
  --success: #0a7a3e;
  --danger: #c0392b;
  --warning: #d68910;
  --bg: #f4f6f9;
  --card-bg: #ffffff;
  --text: #1a1a2e;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --radius: 14px;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100dvh;
  padding-bottom: calc(24px + var(--safe-bottom));
}

/* ── Header ── */
.header {
  background: var(--primary);
  color: white;
  padding: calc(14px + var(--safe-top)) 16px 14px;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.header-inner {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.logo {
  font-size: 1.4rem;
}

.total-balance {
  font-size: 0.85rem;
  font-weight: 600;
  background: rgba(255, 255, 255, 0.18);
  padding: 3px 10px;
  border-radius: 999px;
  vertical-align: middle;
}

.header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}

.header .btn-ghost {
  color: white;
}

.header .btn-ghost:active {
  background: rgba(255, 255, 255, 0.15);
}

/* ── Progress ── */
.progress {
  background: #e8f0fe;
  border-bottom: 1px solid #c5d8f5;
  padding: 10px 16px;
}

.progress.hidden {
  display: none;
}

.progress-bar {
  height: 6px;
  background: #c5d8f5;
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 6px;
}

.progress-fill {
  height: 100%;
  background: var(--primary);
  width: 0%;
  transition: width 0.3s ease;
  border-radius: 3px;
}

.progress-text {
  font-size: 0.8rem;
  color: var(--primary-dark);
  text-align: center;
}

/* ── Main ── */
.main {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
}

.section {
  margin-bottom: 28px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  gap: 8px;
}

.section-header h2 {
  font-size: 1.05rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

.badge {
  background: var(--primary);
  color: white;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
}

.badge-muted {
  background: #9ca3af;
}

/* ── Cards grid ── */
.cards-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 32px 16px;
  font-size: 0.95rem;
}

/* ── Card ── */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  border: 1px solid var(--border);
  transition: box-shadow 0.2s;
}

.card:active {
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.card-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 14px 14px 8px;
  gap: 10px;
}

.card-balance {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--success);
  line-height: 1.2;
}

.card-balance.zero {
  color: var(--text-muted);
}

.card-balance.error {
  color: var(--danger);
  font-size: 1rem;
}

.card-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.card-actions button {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #f9fafb;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.card-actions button:active {
  background: #e5e7eb;
}

.card-actions button:disabled {
  opacity: 0.45;
  pointer-events: none;
}

.card-actions .btn-refresh.loading {
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.card-meta {
  padding: 0 14px 10px;
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
}

.card-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.card-error {
  margin: 0 14px 10px;
  padding: 8px 10px;
  background: #fef2f2;
  color: var(--danger);
  border-radius: 8px;
  font-size: 0.82rem;
}

.card-barcode {
  background: #ffffff;
  padding: 12px 14px 14px;
  border-top: 1px solid var(--border);
  text-align: center;
}

.card-barcode img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0 auto 6px;
}

.card-number {
  font-family: "SF Mono", "Fira Code", "Cascadia Code", Menlo, monospace;
  font-size: 0.78rem;
  color: #333;
  letter-spacing: 0.5px;
  word-break: break-all;
}

.card-pin {
  font-family: "SF Mono", "Fira Code", "Cascadia Code", Menlo, monospace;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 1px;
  margin-top: 4px;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.5;
  pointer-events: none;
}

.btn-primary {
  background: var(--primary);
  color: white;
}

.btn-primary:active {
  background: var(--primary-dark);
}

.btn-outline {
  background: transparent;
  color: var(--primary);
  border: 1.5px solid var(--primary);
}

.btn-outline:active {
  background: #e8f0fe;
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}

.btn-ghost:active {
  background: #f3f4f6;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 0.8rem;
}

.btn-danger {
  background: var(--danger);
  color: white;
}

/* ── Modal ── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
}

.modal-content {
  position: relative;
  background: white;
  width: 100%;
  max-width: 420px;
  border-radius: 20px 20px 0 0;
  padding: 24px 20px calc(24px + var(--safe-bottom));
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.12);
  animation: slideUp 0.25s ease;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal-content h3 {
  font-size: 1.15rem;
  margin-bottom: 20px;
  text-align: center;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}

.form-group input {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.15s;
}

.form-group input:focus {
  border-color: var(--primary);
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}

.modal-actions .btn {
  flex: 1;
}

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #1a1a2e;
  color: white;
  padding: 12px 20px;
  border-radius: 12px;
  font-size: 0.9rem;
  z-index: 200;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  max-width: 90%;
  text-align: center;
}

.toast.hidden {
  display: none;
}

.toast.success {
  background: var(--success);
}

.toast.error {
  background: var(--danger);
}

/* ── Login page ── */
.login-wrap {
  min-height: calc(100dvh - 64px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-bottom: calc(24px + var(--safe-bottom));
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--card-bg);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  text-align: center;
}

.login-title {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 0 0 24px;
  color: var(--text-muted);
}

.login-form .form-group {
  text-align: left;
}

.login-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin: -8px 0 16px;
  min-height: 1.2em;
}

.login-form button {
  width: 100%;
}

/* ── Desktop tweaks ── */
@media (min-width: 600px) {
  .modal {
    align-items: center;
  }
  .modal-content {
    border-radius: 16px;
    padding-bottom: 24px;
  }
  @keyframes slideUp {
    from { opacity: 0; transform: scale(0.95); }
    to { opacity: 1; transform: scale(1); }
  }
}