:root {
  --blue:        #1a6fc4;
  --blue-mid:    #1560ad;
  --blue-light:  #e8f0fb;
  --bg:          #f2f2f7;
  --card:        #ffffff;
  --text:        #1c1c1e;
  --muted:       #6c6c70;
  --border:      #e0e0e5;
  --danger:      #ff3b30;
  --green:       #34c759;
  --radius:      14px;
  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: calc(24px + var(--safe-bottom));
  -webkit-tap-highlight-color: transparent;
  font-size: 16px;
}

/* ── Header ─────────────────────────────── */
.app-header {
  background: var(--blue);
  color: #fff;
  padding: calc(14px + var(--safe-top)) 16px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  gap: 8px;
}

.header-user {
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 8px;
  max-width: 60%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background 0.15s;
  user-select: none;
}
.header-user:active { background: rgba(255,255,255,0.2); }
.header-user .chevron { font-size: 12px; opacity: 0.75; margin-left: 4px; }

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

/* ── Views ───────────────────────────────── */
.view {
  max-width: 600px;
  margin: 0 auto;
  padding: 16px;
}
.hidden { display: none !important; }

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.view-header h2 { font-size: 18px; font-weight: 700; }

/* ── Cards ───────────────────────────────── */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
}
.card h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--muted);
  font-weight: 700;
  margin-bottom: 14px;
}

/* ── Form fields ─────────────────────────── */
.field-group { margin-bottom: 14px; }
.field-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 6px;
}
.field-group input,
.field-group textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 16px;
  font-family: inherit;
  color: var(--text);
  background: #fafafa;
  -webkit-appearance: none;
  transition: border-color 0.15s, background 0.15s;
}
#expense-amount {
  text-align: right;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.field-group input:focus,
.field-group textarea:focus {
  outline: none;
  border-color: var(--blue);
  background: #fff;
}
.field-group textarea { resize: none; }

/* ── Date picker ─────────────────────────── */
.date-picker-wrapper { position: relative; }

.date-display-btn {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 16px;
  font-family: inherit;
  color: var(--text);
  background: #fafafa;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  -webkit-appearance: none;
  transition: border-color 0.15s, background 0.15s;
}
.date-display-btn:focus,
.date-display-btn:active {
  outline: none;
  border-color: var(--blue);
  background: #fff;
}
.cal-icon { color: var(--muted); flex-shrink: 0; }

/* transparent overlay so the native picker opens on tap, no showPicker() needed */
.date-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
  font-size: 16px; /* prevent iOS auto-zoom on focus */
  border: none;
  background: transparent;
  -webkit-appearance: none;
}

/* ── Date toggle ─────────────────────────── */
.date-toggle-row { margin-bottom: 12px; }
.toggle-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  color: var(--blue);
  cursor: pointer;
  user-select: none;
}
.toggle-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--blue);
  cursor: pointer;
}

/* ── Buttons ─────────────────────────────── */
.btn-primary {
  display: block;
  width: 100%;
  padding: 15px;
  background: var(--blue);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 17px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  margin-top: 10px;
  transition: background 0.15s;
}
.btn-primary:active { background: var(--blue-mid); }

.btn-secondary {
  padding: 10px 16px;
  background: var(--blue-light);
  color: var(--blue);
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}
.btn-secondary:active { background: #cde0f8; }

.btn-back {
  background: none;
  border: none;
  color: var(--blue);
  font-size: 17px;
  font-family: inherit;
  cursor: pointer;
  padding: 4px 0;
  font-weight: 500;
}

.btn-link {
  background: none;
  border: none;
  color: var(--blue);
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}

.btn-icon {
  background: none;
  border: none;
  color: #fff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 4px;
  transition: opacity 0.15s;
}
.btn-icon:active { opacity: 0.7; }
.btn-icon.danger { color: var(--danger); font-size: 15px; }

/* ── Expense list (admin) ────────────────── */
.select-all-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.select-all-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--blue);
  flex-shrink: 0;
  cursor: pointer;
}

.expense-row {
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border);
}
.expense-row:last-child { border-bottom: none; }

.expense-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 11px 0;
  cursor: pointer;
  flex: 1;
  min-width: 0;
}

.btn-edit-expense {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 6px 4px;
  flex-shrink: 0;
  opacity: 0.55;
  transition: opacity 0.15s, color 0.15s;
  display: flex;
  align-items: center;
}
.btn-edit-expense:active { opacity: 1; color: var(--blue); }

.expense-edit-form {
  padding: 12px 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}
.edit-row-top {
  display: flex;
  gap: 8px;
}
.edit-field {
  width: 100%;
  padding: 8px 12px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  font-family: inherit;
  background: #fafafa;
  box-sizing: border-box;
  color: var(--text);
}
.edit-field:focus { outline: none; border-color: var(--blue); background: #fff; }
.edit-user  { flex: 1; }
.edit-amount { flex: 0 0 110px; text-align: right; }
.expense-edit-form textarea.edit-field { resize: none; }
.edit-actions { display: flex; gap: 8px; }
.expense-edit-form .date-display-btn { padding: 8px 12px; font-size: 15px; border-radius: 8px; }

.settle-check {
  margin-top: 2px;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  accent-color: var(--blue);
  cursor: pointer;
}

.expense-info { flex: 1; min-width: 0; }
.expense-user {
  font-weight: 600;
  font-size: 15px;
  display: block;
}
.expense-desc {
  font-size: 14px;
  color: var(--text);
  display: block;
  margin: 2px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.expense-meta { font-size: 12px; color: var(--muted); display: block; }
.expense-amount {
  font-weight: 700;
  font-size: 15px;
  color: var(--blue);
  flex-shrink: 0;
  align-self: center;
}

/* ── Settle section ──────────────────────── */
.settle-actions { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.settle-actions textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  font-family: inherit;
  resize: none;
  margin-bottom: 10px;
  background: #fafafa;
  -webkit-appearance: none;
}
.settle-actions textarea:focus {
  outline: none;
  border-color: var(--blue);
  background: #fff;
}

/* ── User management ─────────────────────── */
.user-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 0;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
}
.user-item:last-child { border-bottom: none; }
.user-item span { font-weight: 500; }

.add-user-row {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}
.add-user-row input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  font-family: inherit;
  -webkit-appearance: none;
  background: #fafafa;
}
.add-user-row input:focus {
  outline: none;
  border-color: var(--blue);
  background: #fff;
}

/* ── Dashboard ───────────────────────────── */
.dashboard-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 0;
  border-bottom: 1px solid var(--border);
}
.dashboard-item:last-child { border-bottom: none; }
.dash-user { font-size: 16px; font-weight: 600; }
.dash-right { text-align: right; }
.dash-amount { display: block; font-size: 20px; font-weight: 700; color: var(--blue); }
.dash-count  { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; }

/* ── User picker sheet ───────────────────── */
.user-picker {
  position: fixed;
  inset: 0;
  z-index: 200;
}
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.45);
}
.user-picker-sheet {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--card);
  border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(20px + var(--safe-bottom));
  max-height: 70vh;
  overflow-y: auto;
}
.picker-handle {
  width: 36px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 10px auto 16px;
}
.user-picker-sheet h3 {
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  text-align: center;
  margin-bottom: 14px;
}
.user-pick-btn {
  display: block;
  width: 100%;
  padding: 14px;
  background: var(--blue-light);
  color: var(--blue);
  border: none;
  border-radius: 12px;
  font-size: 17px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  margin-bottom: 10px;
  text-align: left;
  transition: background 0.15s;
}
.user-pick-btn:active, .user-pick-btn.active {
  background: var(--blue);
  color: #fff;
}

/* ── Sync status ─────────────────────────── */
.sync-status {
  font-size: 11px;
  color: rgba(255,255,255,0.75);
  max-width: 110px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: 0.1px;
}
.sync-status.error { color: #ffccc7; }

/* ── Toast ───────────────────────────────── */
.toast {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: rgba(50,50,55,0.92);
  color: #fff;
  padding: 10px 22px;
  border-radius: 100px;
  font-size: 15px;
  font-weight: 500;
  z-index: 300;
  pointer-events: none;
  transition: transform 0.28s cubic-bezier(0.34,1.56,0.64,1);
  white-space: nowrap;
  backdrop-filter: blur(8px);
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ── Version badge ───────────────────────── */
.version-line {
  text-align: right;
  margin-top: 10px;
}
.version-badge {
  font-size: 11px;
  color: var(--muted);
  opacity: 0.5;
  letter-spacing: 0.3px;
}

/* ── Misc ────────────────────────────────── */
.empty-msg, .offline-msg {
  text-align: center;
  padding: 24px 0;
  font-size: 15px;
  color: var(--muted);
}
.offline-msg { color: var(--danger); }

@keyframes shake {
  0%,100% { transform: translateX(0); }
  25%      { transform: translateX(-6px); }
  75%      { transform: translateX(6px); }
}
.shake {
  animation: shake 0.3s ease;
  border-color: var(--danger) !important;
}

/* ── Admin Auth Modal ────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 250;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal-sheet {
  background: var(--card);
  border-radius: 20px 20px 0 0;
  padding: 8px 20px calc(40px + var(--safe-bottom));
  width: 100%;
  max-width: 480px;
}
.modal-sheet h3 {
  font-size: 17px;
  font-weight: 700;
  text-align: center;
  margin-bottom: 16px;
  color: var(--text);
  text-transform: none;
  letter-spacing: 0;
}
.modal-hint {
  font-size: 14px;
  color: var(--muted);
  text-align: center;
  margin-bottom: 16px;
}
.modal-sheet .field-group input { -webkit-appearance: none; }
.modal-cancel {
  display: block;
  width: 100%;
  text-align: center;
  margin-top: 12px;
  padding: 10px 0;
  color: var(--muted);
}

/* ── Gate views (pending / blocked) ─────────── */
.gate-card {
  text-align: center;
  padding: 40px 20px;
  margin-top: 40px;
}
.gate-title {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 10px;
}
.gate-msg { font-size: 15px; color: var(--muted); line-height: 1.5; }

/* ── Device list ─────────────────────────────── */
.device-item {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.device-item:last-child { border-bottom: none; }
.device-header {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 4px;
}
.device-label { font-weight: 600; font-size: 15px; }
.device-badges { display: flex; gap: 4px; flex-wrap: wrap; }
.device-fp {
  font-size: 11px;
  color: var(--muted);
  font-family: monospace;
  margin-bottom: 8px;
}
.device-user-select {
  width: 100%;
  font-size: 13px;
  padding: 6px 10px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: #fafafa;
  font-family: inherit;
  color: var(--text);
  margin-bottom: 8px;
  -webkit-appearance: none;
}
.device-user-select:focus { outline: none; border-color: var(--blue); background: #fff; }
.device-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-device-action {
  font-size: 12px;
  padding: 4px 10px;
  border: 1.5px solid var(--border);
  border-radius: 7px;
  background: var(--card);
  color: var(--blue);
  font-family: inherit;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.12s;
}
.btn-device-action:active { background: var(--blue-light); }
.btn-device-action.danger-action { color: var(--danger); border-color: #ffd6d6; }
.btn-device-action.danger-action:active { background: #fff0f0; }

/* ── Device status badges ────────────────────── */
.device-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 10px;
  letter-spacing: 0.2px;
  text-transform: uppercase;
  white-space: nowrap;
}
.badge-approved { background: #e8f5e9; color: #2e7d32; }
.badge-pending  { background: #fff8e1; color: #f57f17; }
.badge-blocked  { background: #fce4ec; color: #c62828; }
.badge-admin    { background: #e3f2fd; color: #1565c0; }
.badge-self     { background: #f3e5f5; color: #6a1b9a; }
