/* ============================================================
   TrackBill — Global + PWA Scan Stylesheet
   ============================================================ */

/* ── Tokens ── */
:root {
  --bg: #f4f7fb;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #dbe5ef;
  --blue: #2563eb;
  --green: #059669;
  --red: #dc2626;
  --amber: #d97706;
  --shadow: 0 10px 28px rgba(15, 23, 42, 0.08);
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

/* ── Shared components ── */
.container {
  width: min(1120px, calc(100% - 24px));
  margin: 0 auto;
  padding: 20px 0 32px;
}

.nav {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
}

.nav a {
  text-decoration: none;
  color: #fff;
  background: var(--blue);
  padding: 10px 14px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 14px;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

h1, h2, h3 { margin: 0 0 10px; }
p { margin: 0 0 10px; }

.muted { color: var(--muted); }
.ok { color: var(--green); font-weight: 700; }
.error { color: var(--red); font-weight: 700; }
.warning { color: var(--amber); font-weight: 700; }

.notice {
  background: #fff7ed;
  border: 1px solid #fed7aa;
  padding: 12px 14px;
  border-radius: 12px;
  color: #9a3412;
}

.notice.info {
  background: #eff6ff;
  border-color: #bfdbfe;
  color: #1d4ed8;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.form-grid.one { grid-template-columns: 1fr; }
.form-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }

label {
  display: block;
  font-weight: 700;
  margin-bottom: 6px;
}

input, select, button, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 15px;
  background: #fff;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid #bfdbfe;
  border-color: #60a5fa;
}

button {
  border: 0;
  font-weight: 700;
  cursor: pointer;
  background: #111827;
  color: #fff;
}

button.primary { background: var(--blue); }
button.success { background: var(--green); }
button.danger { background: var(--red); }
button.secondary { background: #334155; }
button.light {
  background: #e2e8f0;
  color: #0f172a;
}
button.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: #0f172a;
}

.button-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.button-row > * {
  flex: 1 1 160px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
  gap: 16px;
}

.table-wrap { overflow: auto; }

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  border-bottom: 1px solid #e5e7eb;
  padding: 10px;
  text-align: left;
  vertical-align: top;
  font-size: 14px;
}

th {
  background: #f8fafc;
  position: sticky;
  top: 0;
}

.code {
  font-family: Consolas, Monaco, monospace;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 4px 8px;
  display: inline-block;
  word-break: break-all;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 13px;
  font-weight: 700;
}

.pill.success { background: #dcfce7; color: #166534; }
.pill.danger  { background: #fee2e2; color: #991b1b; }
.pill.info    { background: #dbeafe; color: #1d4ed8; }
.pill.gray    { background: #e2e8f0; color: #334155; }

.history-item {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 10px;
  background: #fff;
}

.history-time {
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 6px;
}

.hidden { display: none !important; }

.device-card {
  border: 1px solid #e5e7eb;
  border-radius: 14px;
  padding: 12px;
  background: #fff;
}

.device-card small {
  color: var(--muted);
  display: block;
  margin-top: 6px;
}

.actions-inline {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.actions-inline button,
.actions-inline a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 120px;
  text-decoration: none;
}

.footer-note {
  color: #64748b;
  font-size: 13px;
  margin-top: 8px;
}

/* ── Section header (title + inline button) ── */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.section-header h2 { margin: 0; }

/* ── Collapsible ── */
.collapsible {
  overflow: hidden;
  transition: max-height 0.3s ease;
}

/* ── Small button variant ── */
.small-btn {
  padding: 6px 14px !important;
  font-size: 13px !important;
  min-width: auto !important;
  width: auto !important;
  border-radius: 10px !important;
}

/* ── Disabled input ── */
.disabled-input {
  background: #f1f5f9 !important;
  color: #94a3b8 !important;
  cursor: not-allowed;
}

/* ── Checkbox row ── */
.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--blue);
  cursor: pointer;
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
}

.modal-overlay.hidden { display: none; }

.modal-box {
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.2);
  width: min(480px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  animation: modalIn 0.2s ease;
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(20px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #e5e7eb;
}

.modal-header h3 { margin: 0; }

.modal-body {
  padding: 20px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid #e5e7eb;
}

.modal-footer button {
  width: auto;
  min-width: 100px;
}

.lookup-result {
  padding: 14px;
  border-radius: 14px;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
}

.kiosk-banner {
  border-radius: 18px;
  padding: 18px;
  background: #eef6ff;
  border: 1px solid #bfdbfe;
  margin-bottom: 12px;
}

.kiosk-banner .title {
  font-size: clamp(22px, 4.4vw, 34px);
  font-weight: 800;
  color: #1d4ed8;
  margin-bottom: 8px;
}

.kiosk-banner .desc {
  color: #334155;
  font-size: 15px;
}

/* ============================================================
   SCAN PAGE — PWA Mobile-Optimized
   ============================================================ */

.scan-shell {
  min-height: 100vh;
  min-height: 100dvh;
  background: linear-gradient(180deg, #0f172a 0px, #0f172a 120px, #eef2f7 120px, #eef2f7 100%);
  padding-bottom: var(--safe-bottom);
}

.scan-container {
  width: min(540px, 100%);
  margin: 0 auto;
  padding: 0 0 16px;
}

/* ── Header ── */
.scan-header {
  color: #fff;
  padding: 12px 16px 14px;
}

.scan-header-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}

.scan-location {
  font-size: clamp(20px, 5vw, 30px);
  font-weight: 800;
  line-height: 1.15;
}

.scan-subtext {
  color: #94a3b8;
  font-size: 13px;
  margin-top: 2px;
}

.scan-header-badges {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.counter-pill {
  background: rgba(37, 99, 235, 0.25);
  color: #93c5fd;
  font-variant-numeric: tabular-nums;
  min-width: 56px;
  justify-content: center;
  transition: transform 0.2s ease;
}

.counter-bump {
  transform: scale(1.15);
}

/* ── Main card ── */
.scan-main {
  background: #fff;
  border-radius: 20px 20px 16px 16px;
  box-shadow: 0 8px 32px rgba(15, 23, 42, 0.1);
  margin: 0 8px;
  padding: 12px;
  overflow: hidden;
}

/* ── Status Panel ── */
.status-panel {
  border-radius: 16px;
  padding: 16px;
  color: #fff;
  margin-bottom: 12px;
  transition: background 0.3s ease, transform 0.2s ease;
  position: relative;
  overflow: hidden;
}

.status-panel.idle    { background: #1e293b; }
.status-panel.ready   { background: var(--blue); }
.status-panel.success { background: var(--green); }
.status-panel.error   { background: var(--red); }
.status-panel.warning { background: var(--amber); }

.status-panel.pulse {
  animation: statusPulse 0.4s ease;
}

@keyframes statusPulse {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.02); }
  100% { transform: scale(1); }
}

.status-panel .big {
  font-size: clamp(24px, 5vw, 36px);
  font-weight: 800;
  line-height: 1.1;
  margin-bottom: 4px;
}

.status-panel .medium {
  font-size: clamp(14px, 2.5vw, 18px);
  font-weight: 600;
  opacity: 0.95;
}

.status-panel .small {
  font-size: 12px;
  opacity: 0.8;
  margin-top: 4px;
}

/* ── Camera / Reader ── */
.reader-shell {
  position: relative;
  overflow: hidden;
  border-radius: 16px;
  border: 2px solid #cbd5e1;
  background: #000;
  margin-bottom: 10px;
}

#reader {
  min-height: 260px;
  background: #000;
}

#reader video {
  border-radius: 14px;
}

.scan-overlay {
  display: none;
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
  z-index: 5;
}

.scan-overlay.active {
  display: flex;
  align-items: center;
  justify-content: center;
}

.scan-frame {
  width: 88%;
  height: 40%;
  border: 2px solid rgba(37, 99, 235, 0.7);
  border-radius: 8px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.25);
  animation: framePulse 2s ease-in-out infinite;
}

@keyframes framePulse {
  0%, 100% { border-color: rgba(37, 99, 235, 0.7); }
  50%      { border-color: rgba(37, 99, 235, 0.3); }
}

/* ── Scan action buttons ── */
.scan-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.scan-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: 12px;
  padding: 14px 10px;
  font-size: 14px;
  font-weight: 700;
  border: none;
  cursor: pointer;
  transition: transform 0.15s ease, opacity 0.15s ease;
  min-height: 48px;
}

.scan-btn:active {
  transform: scale(0.97);
  opacity: 0.9;
}

.primary-btn {
  background: var(--blue);
  color: #fff;
}

.ghost-btn {
  background: #f1f5f9;
  color: #334155;
  border: 1px solid #e2e8f0;
}

.btn-icon {
  font-size: 16px;
  line-height: 1;
}

/* ── Last bill card ── */
.last-bill-card {
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  padding: 12px 14px;
  background: #f8fafc;
  margin-bottom: 10px;
  transition: background 0.5s ease, border-color 0.5s ease;
}

.last-bill-card.highlight {
  background: #ecfdf5;
  border-color: #86efac;
}

.last-bill-label {
  font-size: 12px;
  font-weight: 700;
  color: #64748b;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}

.last-bill-code {
  font-size: clamp(22px, 5vw, 34px);
  font-weight: 800;
  line-height: 1.1;
  word-break: break-all;
  font-family: 'SF Mono', Consolas, Monaco, monospace;
  color: #0f172a;
}

.last-bill-meta {
  font-size: 13px;
  color: #64748b;
  margin-top: 4px;
}

.last-bill-meta b {
  color: #0f172a;
}

/* ── Manual panel ── */
.manual-panel {
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  background: #fff;
  margin-bottom: 10px;
  overflow: hidden;
}

.manual-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: #f8fafc;
  font-weight: 700;
  font-size: 14px;
  border-bottom: 1px solid #e2e8f0;
}

.btn-close {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  color: #64748b;
  width: auto;
}

.btn-close:active { background: #e2e8f0; }

.manual-body {
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.manual-body input {
  font-size: 17px;
  font-family: 'SF Mono', Consolas, Monaco, monospace;
  letter-spacing: 0.02em;
  padding: 14px;
  border-radius: 12px;
}

.full-width { width: 100%; }

/* ── Keyboard trap ── */
.keyboard-trap {
  position: absolute;
  left: -9999px;
  opacity: 0;
  width: 1px;
  height: 1px;
}

/* ── Session history ── */
.session-history {
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  overflow: hidden;
  margin-top: 2px;
}

.session-history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: #f8fafc;
  font-weight: 700;
  font-size: 13px;
  color: #334155;
  border-bottom: 1px solid #e2e8f0;
}

.session-history-list {
  max-height: 200px;
  overflow-y: auto;
}

.history-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  font-size: 13px;
  border-bottom: 1px solid #f1f5f9;
  transition: background 0.3s ease;
}

.history-row.latest {
  background: #ecfdf5;
}

.history-code {
  font-family: 'SF Mono', Consolas, Monaco, monospace;
  font-weight: 700;
  color: #0f172a;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-loc {
  color: var(--blue);
  font-weight: 600;
  font-size: 12px;
  flex-shrink: 0;
}

.history-time {
  color: #94a3b8;
  font-size: 11px;
  flex-shrink: 0;
}

/* ── Footer ── */
.scan-footer {
  text-align: center;
  padding: 10px 16px;
  color: #94a3b8;
  font-size: 12px;
}

/* ── Keyboard trap (USB/BT scanner focus sink) ── */
.keyboard-trap {
  position: absolute;
  left: -9999px;
  top: -9999px;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

/* ── PWA standalone: no header padding needed ── */
@media all and (display-mode: standalone) {
  .scan-header {
    padding-top: max(12px, env(safe-area-inset-top, 12px));
  }
}

/* ── Mobile responsive ── */
@media (max-width: 600px) {
  .scan-main {
    margin: 0 6px;
    padding: 10px;
    border-radius: 18px 18px 14px 14px;
  }

  .status-panel {
    padding: 14px;
    border-radius: 14px;
  }

  #reader {
    min-height: 220px;
  }

  .scan-actions {
    gap: 6px;
  }

  .scan-btn {
    padding: 12px 8px;
    font-size: 13px;
  }
}

/* ── Desktop (admin, lookup, simulate) ── */
@media (max-width: 860px) {
  .form-grid,
  .form-grid.three,
  .grid-2 {
    grid-template-columns: 1fr;
  }

  .container {
    width: calc(100% - 14px);
  }
}

/* ── Login Screen ── */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
  padding: 16px;
}

.login-screen.hidden { display: none; }

.login-card {
  background: #fff;
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  width: min(400px, 100%);
}

.login-card h1 {
  text-align: center;
  margin-bottom: 4px;
}

.login-card .muted {
  text-align: center;
  margin-bottom: 18px;
}

/* ── Nav user badge ── */
.nav-user {
  display: inline-flex;
  align-items: center;
  background: rgba(255,255,255,0.15);
  color: #fff;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  margin-left: auto;
}

.nav-logout {
  color: #fff !important;
  border-color: rgba(255,255,255,0.3) !important;
  padding: 8px 14px !important;
  font-size: 13px !important;
}

/* ── Stale alert card ── */
.stale-card {
  background: #fef2f2;
  border: 2px solid #fca5a5;
}

.stale-card h2 { color: #991b1b; }

/* ── Textarea ── */
textarea {
  font-family: 'SF Mono', Consolas, Monaco, monospace;
  font-size: 14px;
  line-height: 1.5;
  resize: vertical;
}
