:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-alt: #f1f5f9;
  --text: #0f172a;
  --text-muted: #475569;
  --border: #cbd5e1;
  --border-light: #e2e8f0;
  --primary: #0f766e;
  --primary-hover: #0d9488;
  --primary-soft: #ccfbf1;
  --primary-text: #ffffff;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --warning: #b45309;
  --warning-soft: #fef3c7;
  --success: #15803d;
  --success-soft: #dcfce7;
  --radius: 10px;
  --radius-sm: 6px;
  --nav-height: 64px;
  --header-height: 58px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
}

body {
  padding-top: var(--header-height);
  padding-bottom: calc(var(--nav-height) + 20px);
}

.app-container {
  max-width: 720px;
  margin: 0 auto;
  padding: 14px;
}

/* Header */
.app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-height);
  background: var(--surface);
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 40;
}

.header-brand {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.header-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--primary);
  line-height: 1.15;
}

.header-subtitle {
  font-size: 0.78rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--surface-alt);
  color: var(--text-muted);
  border: 1px solid var(--border-light);
}

.status-pill.online {
  background: var(--success-soft);
  color: var(--success);
  border-color: #bbf7d0;
}

.status-pill.offline {
  background: var(--warning-soft);
  color: var(--warning);
  border-color: #fde68a;
}

/* Bottom Navigation */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--nav-height);
  background: var(--surface);
  border-top: 1px solid var(--border-light);
  display: flex;
  justify-content: space-around;
  align-items: stretch;
  z-index: 40;
}

.nav-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
  padding: 6px 4px;
}

.nav-btn.active {
  color: var(--primary);
  background: #f0fdfa;
  border-top: 3px solid var(--primary);
}

.nav-icon {
  font-size: 1.2rem;
  line-height: 1;
}

/* Screens */
.screen {
  display: none;
}

.screen.active {
  display: block;
}

.screen-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}

.screen-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}

.card-title {
  margin: 0 0 10px 0;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
}

/* Summary Grid */
.summary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 14px;
}

.stat-card.full {
  grid-column: 1 / -1;
}

.stat-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 4px;
}

.stat-value {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--text);
}

.stat-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Primary Actions */
.primary-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}

@media (max-width: 420px) {
  .primary-actions {
    grid-template-columns: 1fr;
  }
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 10px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

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

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--primary-soft);
  border-color: #99f6e4;
  color: var(--primary);
}

.btn-danger {
  background: var(--danger-soft);
  border-color: #fecaca;
  color: var(--danger);
}

.btn-sm {
  min-height: 38px;
  padding: 6px 12px;
  font-size: 0.88rem;
}

.btn-block {
  width: 100%;
}

/* Forms */
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

@media (max-width: 540px) {
  .form-grid {
    grid-template-columns: 1fr;
  }
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 12px;
}

.form-group.full {
  grid-column: 1 / -1;
}

label {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
}

.required::after {
  content: " *";
  color: var(--danger);
}

input, select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
  border-color: var(--primary);
}

input.uncertain, select.uncertain, textarea.uncertain {
  background: var(--warning-soft);
  border-color: var(--warning);
}

.field-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* Product Rows */
.product-row {
  border: 1px solid var(--border-light);
  background: var(--surface-alt);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 10px;
}

.product-row-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-weight: 700;
  font-size: 0.9rem;
}

.product-row-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.product-row-grid .full {
  grid-column: 1 / -1;
}

.product-row-total {
  margin-top: 8px;
  text-align: right;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--primary);
}

/* Totals Summary Box */
.totals-box {
  background: #f0fdfa;
  border: 1px solid #99f6e4;
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}

.total-line {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 0.95rem;
}

.total-line.grand {
  border-top: 2px solid #99f6e4;
  margin-top: 6px;
  padding-top: 8px;
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--primary);
}

/* Lists */
.bill-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bill-item-card {
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 12px 14px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.bill-item-card:active {
  background: var(--surface-alt);
}

.bill-item-main {
  min-width: 0;
}

.bill-item-title {
  font-weight: 700;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bill-item-meta {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.bill-item-right {
  text-align: right;
  flex-shrink: 0;
}

.bill-item-amount {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
}

/* Badges */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
}

.badge-synced { background: var(--success-soft); color: var(--success); }
.badge-pending { background: var(--warning-soft); color: var(--warning); }
.badge-failed { background: var(--danger-soft); color: var(--danger); }
.badge-local { background: var(--surface-alt); color: var(--text-muted); }
.badge-scanned { background: #e0f2fe; color: #0369a1; }
.badge-manual { background: var(--surface-alt); color: var(--text-muted); }

/* Alerts */
.alert {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  font-size: 0.92rem;
  border: 1px solid transparent;
}

.alert-warning {
  background: var(--warning-soft);
  border-color: #fde68a;
  color: #92400e;
}

.alert-danger {
  background: var(--danger-soft);
  border-color: #fecaca;
  color: var(--danger);
}

.alert-info {
  background: #f0fdfa;
  border-color: #99f6e4;
  color: var(--primary);
}

/* Scanner preview */
.image-preview-wrap {
  background: #0f172a;
  border-radius: var(--radius);
  padding: 8px;
  text-align: center;
  margin-bottom: 12px;
}

.image-preview-wrap img,
.image-preview-wrap canvas {
  max-width: 100%;
  max-height: 420px;
  border-radius: var(--radius-sm);
  object-fit: contain;
}

.scanner-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: none;
  align-items: flex-end;
  justify-content: center;
  z-index: 60;
}

.modal-backdrop.open {
  display: flex;
}

.modal-panel {
  background: var(--surface);
  width: 100%;
  max-width: 720px;
  max-height: 90vh;
  overflow-y: auto;
  border-radius: 16px 16px 0 0;
  padding: 18px 16px 24px;
}

@media (min-width: 640px) {
  .modal-backdrop {
    align-items: center;
    padding: 16px;
  }
  .modal-panel {
    border-radius: var(--radius);
  }
}

/* Toast */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + 16px);
  left: 50%;
  transform: translateX(-50%);
  background: #0f172a;
  color: #ffffff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
  z-index: 80;
  display: none;
  max-width: 90vw;
  text-align: center;
}

.toast.show {
  display: block;
}

.hidden {
  display: none !important;
}

.table-scroll {
  overflow-x: auto;
}

table.simple-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

table.simple-table th,
table.simple-table td {
  padding: 8px 6px;
  border-bottom: 1px solid var(--border-light);
  text-align: left;
}

table.simple-table th {
  font-weight: 700;
  color: var(--text-muted);
  font-size: 0.8rem;
}

.text-right {
  text-align: right !important;
}
