:root {
  --blue: #1d4ed8;
  --blue-dark: #1e40af;
  --blue-light: #eff6ff;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-500: #6b7280;
  --gray-700: #374151;
  --gray-900: #111827;
  --green: #16a34a;
  --red: #dc2626;
  --radius: 14px;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation; /* disable double-tap-to-zoom */
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--gray-100);
  color: var(--gray-900);
}

/* ---------- Login gate ---------- */
.login-overlay[hidden] { display: none; }
/* Animated gradient login gate background */
@keyframes gradient-shift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.login-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  /* Fully opaque animated gradient — solid color stops so nothing behind
     the gate shows through. */
  background-color: #1d4ed8; /* fallback while the gradient paints */
  background-image: linear-gradient(135deg, #1d4ed8, #4f46e5, #7c3aed, #ec4899, #1d4ed8);
  background-size: 300% 300%;
  animation: gradient-shift 15s ease infinite;
  color: #fff;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  padding: calc(env(safe-area-inset-top, 0px) + 32px) 20px
           calc(env(safe-area-inset-bottom, 0px) + 32px);
}
@media (prefers-reduced-motion: reduce) {
  .login-overlay { animation: none; }
}
.login-box {
  width: 100%;
  max-width: 340px;
  text-align: center;
}
.login-logo { display: block; margin: 0 auto 22px; max-width: 60vw; height: auto; }
.login-title { font-size: 1.15rem; font-weight: 600; margin: 0 0 14px; }
.login-error { min-height: 1.2em; margin: 12px 0 4px; color: #fecaca; font-weight: 600; font-size: 0.9rem; }

.pin-dots { display: flex; justify-content: center; gap: 16px; margin: 8px 0; }
.pin-dots span {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.7);
  transition: background 0.15s ease;
}
.pin-dots span.filled { background: #fff; border-color: #fff; }

.pin-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin: 10px auto 18px;
  max-width: 300px;
}
.pin-key {
  aspect-ratio: 1 / 1;
  border: none;
  border-radius: 50%;
  background: linear-gradient(180deg, #4a76ea, #2f5be0);
  color: #fff;
  font-size: 1.6rem;
  font-family: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow:
    0 5px 0 #12275f,
    0 8px 12px rgba(0, 0, 0, 0.3);
  transition: transform 0.07s ease, box-shadow 0.07s ease, filter 0.07s ease;
}
.pin-key:active,
.pin-key.pressed {
  transform: translateY(4px);
  box-shadow: 0 1px 0 #12275f, 0 2px 5px rgba(0, 0, 0, 0.25);
  filter: brightness(1.12);
}
.pin-key-blank {
  background: none;
  box-shadow: none;
  cursor: default;
  pointer-events: none;
}
.pin-key-blank:active, .pin-key-blank.pressed { transform: none; }
.pin-key-back { font-size: 1.3rem; }

/* Gate logo: fade in from the top on load */
.logo-wrap { animation: logo-enter 0.8s cubic-bezier(0.22, 1, 0.36, 1) both; }
@keyframes logo-enter {
  from { opacity: 0; transform: translateY(-36px); }
}

/* Wrong PIN: shake the logo */
.login-logo.shake { animation: logo-shake 0.5s ease; }
@keyframes logo-shake {
  15% { transform: translateX(-10px) rotate(-2deg); }
  30% { transform: translateX(9px) rotate(2deg); }
  45% { transform: translateX(-7px) rotate(-1.2deg); }
  60% { transform: translateX(6px) rotate(1.2deg); }
  80% { transform: translateX(-3px); }
}

/* Sign-up inputs sit on the blue overlay — give them light labels */
#signup-view label { color: #fff; text-align: left; }
#signup-view .req { color: #fecaca; }
.login-box .btn-link { color: #fff; }

/* 3D "pushable" buttons at the bottom of the PIN gate */
.gate-actions {
  display: flex;
  gap: 14px;
  justify-content: center;
  margin-top: 24px;
}
.btn3d {
  display: inline-block;
  padding: 13px 20px;
  border: none;
  border-radius: 14px;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 800;
  letter-spacing: 0.01em;
  cursor: pointer;
  text-decoration: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  color: var(--blue-dark);
  background: linear-gradient(180deg, #ffffff, #dbe4fb);
  box-shadow:
    0 6px 0 #a9bce8,
    0 10px 18px rgba(0, 0, 0, 0.3);
  transition: transform 0.09s ease, box-shadow 0.09s ease;
}
.btn3d:hover {
  transform: translateY(-1px);
  box-shadow: 0 7px 0 #a9bce8, 0 12px 20px rgba(0, 0, 0, 0.32);
}
.btn3d:active {
  transform: translateY(5px);
  box-shadow: 0 1px 0 #a9bce8, 0 3px 8px rgba(0, 0, 0, 0.25);
}
.btn3d-alt {
  color: #fff;
  background: linear-gradient(180deg, #2f61e6, var(--blue-dark));
  box-shadow: 0 6px 0 #12275f, 0 10px 18px rgba(0, 0, 0, 0.3);
}
.btn3d-alt:hover { box-shadow: 0 7px 0 #12275f, 0 12px 20px rgba(0, 0, 0, 0.32); }
.btn3d-alt:active { box-shadow: 0 1px 0 #12275f, 0 3px 8px rgba(0, 0, 0, 0.25); }

/* ---------- Header: blue background, white centered text ---------- */
.app-header {
  background: var(--blue);
  color: #fff;
  text-align: center;
  padding: calc(env(safe-area-inset-top, 0px) + 18px) 16px 14px;
  position: relative;
}
.sign-out {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  right: 12px;
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.78rem;
  font-family: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.sign-out[hidden] { display: none; }
.sign-out:active { background: rgba(255, 255, 255, 0.32); }
.app-header h1 {
  margin: 0;
  line-height: 0;
}
.app-header .logo {
  height: 44px;
  max-height: 44px;
  max-width: 70vw;
  width: auto;
  display: block;
  margin: 0 auto;
}
.app-header p {
  margin: 3px 0 0;
  color: #fff;
  opacity: 0.9;
  font-size: 0.85rem;
}

/* ---------- Tabs ---------- */
.tabs {
  display: flex;
  background: #fff;
  border-bottom: 1px solid var(--gray-200);
  position: sticky;
  top: 0;
  z-index: 10;
}
.tab {
  flex: 1;
  padding: 13px 4px;
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  font-size: 0.95rem;
  font-family: inherit;
  color: var(--gray-500);
  cursor: pointer;
}
.tab.active {
  color: var(--blue);
  border-bottom-color: var(--blue);
  font-weight: 600;
}
.badge {
  display: inline-block;
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--blue);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
}

/* ---------- Layout ---------- */
main {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 32px);
}
.card {
  background: #fff;
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.07);
}
.card h2 {
  margin: 0 0 6px;
  font-size: 1.05rem;
}
.hint { color: var(--gray-500); font-size: 0.85rem; margin: 4px 0 12px; }
.center { text-align: center; }
.req { color: var(--red); }

/* ---------- Buttons ---------- */
.btn {
  display: block;
  width: 100%;
  padding: 13px 16px;
  margin-top: 10px;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn-big { padding: 16px; font-size: 1.05rem; }
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:active { background: var(--blue-dark); }
.btn-secondary { background: var(--blue-light); color: var(--blue); }
.btn-link {
  background: none;
  color: var(--blue);
  font-weight: 500;
  text-decoration: underline;
  padding: 8px;
}
.btn-small {
  display: inline-block;
  width: auto;
  padding: 8px 12px;
  font-size: 0.85rem;
  margin-top: 0;
}

/* ---------- Preview & progress ---------- */
#preview-area { text-align: center; }
#preview-img {
  max-width: 100%;
  max-height: 45vh;
  border-radius: 10px;
  border: 1px solid var(--gray-200);
}
.progress {
  height: 10px;
  background: var(--gray-200);
  border-radius: 999px;
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--blue);
  border-radius: 999px;
  transition: width 0.2s ease;
}
/* Engines without granular progress (Scribe) get an animated sweep instead. */
.progress-bar.indeterminate {
  width: 40%;
  animation: bar-sweep 1.1s ease-in-out infinite alternate;
}
@keyframes bar-sweep {
  from { margin-left: 0; }
  to { margin-left: 60%; }
}
#ocr-status { margin: 0 0 10px; font-size: 0.92rem; color: var(--gray-700); }

/* ---------- Form ---------- */
label {
  display: block;
  margin: 14px 0 5px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--gray-700);
}
input, select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--gray-200);
  border-radius: 10px;
  font-size: 16px; /* prevents iOS zoom-on-focus */
  font-family: inherit;
  background: var(--gray-50);
  color: var(--gray-900);
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--blue);
  outline-offset: 0;
  border-color: var(--blue);
}
.row { display: flex; gap: 12px; }
.row > div { flex: 1; min-width: 0; }

/* ---------- Success ---------- */
.success { text-align: center; }
.checkmark {
  width: 56px;
  height: 56px;
  margin: 6px auto 10px;
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  font-size: 2rem;
  line-height: 56px;
}
.success-text { color: var(--green); font-weight: 600; }

/* ---------- History ---------- */
.history-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}
.toolbar-buttons { display: flex; gap: 8px; }
.receipt-list { list-style: none; margin: 0; padding: 0; }
.receipt-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-top: 1px solid var(--gray-100);
}
.receipt-thumb {
  width: 54px;
  height: 54px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--gray-200);
  flex-shrink: 0;
  cursor: pointer;
}
.receipt-info { flex: 1; min-width: 0; cursor: pointer; }
.receipt-merchant {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.receipt-meta { font-size: 0.8rem; color: var(--gray-500); }
.receipt-amount { font-weight: 700; white-space: nowrap; }
.status-chip {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
}
.status-Pending-upload { background: #fef3c7; color: #92400e; }
.status-Submitted { background: #dbeafe; color: #1e40af; }
.status-Exported { background: #dcfce7; color: #166534; }
.btn-delete {
  background: none;
  border: none;
  color: var(--gray-500);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 8px;
}

/* ---------- Modal ---------- */
.modal-overlay[hidden] { display: none; }
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal {
  background: #fff;
  border-radius: var(--radius);
  max-width: 560px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  padding: 18px;
  position: relative;
}
.modal-close {
  position: absolute;
  top: 8px;
  right: 10px;
  background: none;
  border: none;
  font-size: 1.7rem;
  color: var(--gray-500);
  cursor: pointer;
  line-height: 1;
}
#modal-img {
  max-width: 100%;
  max-height: 50vh;
  display: block;
  margin: 8px auto 12px;
  border-radius: 10px;
  border: 1px solid var(--gray-200);
}
#modal-details p { margin: 4px 0; font-size: 0.9rem; }
#modal-details .k { color: var(--gray-500); }
