/* XPenses — Phase A brand skeleton. Mobile-first. iOS-safe. */

:root {
  --bg:           #0f172a;
  --bg-soft:      #111827;
  --surface:      #ffffff;
  --surface-2:    #f8fafc;
  --border:       #e5e7eb;
  --text:         #0f172a;
  --text-muted:   #64748b;
  --primary:      #2563eb;
  --primary-700:  #1d4ed8;
  --success:      #16a34a;
  --warn:         #d97706;
  --error:        #dc2626;
  --radius:       12px;
  --radius-sm:    8px;
  --shadow:       0 1px 2px rgba(15,23,42,0.04), 0 4px 12px rgba(15,23,42,0.06);

  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--surface-2);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
body { min-height: 100dvh; }

input, select, textarea, button { font: inherit; }
input, select, textarea { font-size: 16px; } /* no iOS zoom */
button { cursor: pointer; touch-action: manipulation; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.muted { color: var(--text-muted); }
.small { font-size: 0.875rem; }

/* ---------------- App shell ---------------- */
.app-shell {
  padding-top:    var(--safe-top);
  padding-left:   var(--safe-left);
  padding-right:  var(--safe-right);
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg);
  color: #fff;
  position: sticky; top: 0; z-index: 10;
  padding-top: calc(12px + var(--safe-top));
}
.topbar .brand { color: #fff; font-weight: 700; font-size: 1.1rem; }
.topbar .nav-main { display: flex; flex-wrap: wrap; gap: 4px; flex: 1; }
.topbar .nav-link { color: #cbd5e1; padding: 6px 10px; border-radius: var(--radius-sm); font-size: 0.9rem; }
.topbar .nav-link:hover { background: rgba(255,255,255,0.08); text-decoration: none; }
.topbar .nav-link-primary { background: var(--primary); color: #fff; }
.topbar .nav-link-primary:hover { background: var(--primary-700); }
.topbar .nav-user { display: flex; align-items: center; gap: 8px; }
.topbar .nav-user-name { color: #fff; font-weight: 500; }
.logout-form { margin: 0; }

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: calc(96px + var(--safe-bottom));
}

/* ---------------- Auth ---------------- */
.auth-shell {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
  padding: 24px;
  padding-top:    calc(24px + var(--safe-top));
  padding-bottom: calc(24px + var(--safe-bottom));
}
.auth-container { width: 100%; max-width: 380px; }
.auth-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px 24px;
  box-shadow: var(--shadow);
}
.auth-title { margin: 0 0 4px; font-size: 1.6rem; }
.auth-sub { margin: 0 0 24px; color: var(--text-muted); }

/* ---------------- Forms ---------------- */
.form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 0.875rem; font-weight: 500; color: #334155; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--text);
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: 0;
  border-color: var(--primary);
}

/* ---------------- Buttons ---------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  font-weight: 500;
  min-height: 44px; /* iOS touch target */
}
.btn:hover { background: #f1f5f9; text-decoration: none; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-700); border-color: var(--primary-700); }
.btn-ghost { background: transparent; color: #fff; border-color: rgba(255,255,255,0.3); }
.btn-ghost:hover { background: rgba(255,255,255,0.1); }
.btn-block { width: 100%; }
.btn-lg { padding: 14px 22px; font-size: 1.05rem; }

/* ---------------- Flash ---------------- */
.flash {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  font-size: 0.95rem;
}
.flash-success { background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0; }
.flash-error   { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
.flash-info    { background: #eff6ff; color: #1e3a8a; border: 1px solid #bfdbfe; }
.flash-warn    { background: #fffbeb; color: #92400e; border: 1px solid #fde68a; }
.flash ul { margin: 6px 0 0 18px; padding: 0; }

/* ---------------- Dashboard ---------------- */
.dashboard-hero {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 20px;
}
.dashboard-hero h1 { margin: 0; }
.kpi-grid {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  margin-bottom: 20px;
}
.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex; flex-direction: column; gap: 6px;
}
.kpi-label { font-size: 0.85rem; color: var(--text-muted); }
.kpi-value { font-size: 1.4rem; font-weight: 600; }

/* ---------------- Quick actions (sticky bottom) ---------------- */
.quick-actions {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex; gap: 6px;
  padding: 8px 12px;
  padding-bottom: calc(8px + var(--safe-bottom));
  padding-left:  calc(12px + var(--safe-left));
  padding-right: calc(12px + var(--safe-right));
  background: rgba(255,255,255,0.96);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
  z-index: 20;
}
.qa {
  flex: 1;
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px;
  padding: 8px 6px;
  border-radius: var(--radius-sm);
  background: #f1f5f9;
  color: var(--text);
  font-weight: 500;
  font-size: 0.875rem;
  text-align: center;
}
.qa:hover { background: #e2e8f0; text-decoration: none; }
.qa-primary { background: var(--primary); color: #fff; }
.qa-primary:hover { background: var(--primary-700); }

/* ---------------- Pages ---------------- */
.page h1, .page h2 { margin-top: 0; }
.meta { display: grid; grid-template-columns: 120px 1fr; gap: 8px 16px; margin-bottom: 24px; }
.meta dt { color: var(--text-muted); }
.meta dd { margin: 0; font-weight: 500; }

/* ---------------- Errors ---------------- */
.error-page {
  text-align: center;
  padding: 60px 20px;
}
.error-page h1 { font-size: 4rem; margin: 0 0 12px; color: var(--text-muted); }
.error-detail { color: var(--text-muted); font-size: 0.9rem; }

/* ---------------- Tables (skeleton, used in later phases) ---------------- */
.table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.table th, .table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.table th { background: var(--surface-2); font-weight: 600; font-size: 0.85rem; color: #334155; }
.table tr:last-child td { border-bottom: none; }

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  background: var(--surface);
}
.table-wrap .table {
  box-shadow: none;
  border-radius: 0;
  margin-bottom: 0;
}

.grid-2 {
  display: grid;
  gap: 16px;
  grid-template-columns: 1fr;
}
@media (min-width: 769px) {
  .grid-2 { grid-template-columns: 1fr 1fr; }
}

/* ---------------- Phase B additions ---------------- */
.page-head {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 16px;
}
.page-head h1 { margin: 0; font-size: 1.4rem; }
.page-head .row-actions { display: inline-flex; flex-wrap: wrap; gap: 6px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 12px; font-size: 1.05rem; }

.row-2 {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--text);
}
.input:focus { outline: 2px solid var(--primary); border-color: var(--primary); }
.req { color: var(--error); }
.err { color: var(--error); font-size: 0.85rem; }

.form-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }

.filter-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.filter-bar .input { flex: 1 1 180px; min-width: 140px; }
.filter-bar form.inline { display: inline; }
form.inline { display: inline; }

.btn-link { background: transparent; border-color: transparent; color: var(--primary); min-height: auto; padding: 6px 8px; }
.btn-link:hover { background: transparent; text-decoration: underline; }
.btn-link.danger { color: var(--error); }

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

.table .r { text-align: right; }
.table th.r { text-align: right; }
.table tfoot td { padding: 10px 12px; background: var(--surface-2); }
.table tfoot tr:last-child td { font-weight: 600; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.78rem; background: var(--surface-2); color: #334155; text-transform: capitalize; }
.badge.ok { background: #dcfce7; color: #166534; }
.badge.muted { background: #f1f5f9; color: #64748b; }
.badge.status-draft     { background: #e2e8f0; color: #475569; }
.badge.status-sent      { background: #dbeafe; color: #1d4ed8; }
.badge.status-paid      { background: #dcfce7; color: #166534; }
.badge.status-overdue   { background: #fee2e2; color: #b91c1c; }
.badge.status-cancelled { background: #f3f4f6; color: #6b7280; }

.empty-state { padding: 24px; text-align: center; color: var(--text-muted); background: var(--surface); border-radius: var(--radius); border: 1px dashed var(--border); }

.swatch { display: inline-block; width: 14px; height: 14px; border-radius: 4px; vertical-align: middle; border: 1px solid rgba(0,0,0,0.06); }

.kpi-strip { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 16px; }
.kpi-strip .kpi-card { flex: 1 1 180px; }

.pager { display: flex; gap: 6px; margin-top: 12px; flex-wrap: wrap; }
.pager a, .pager .pager-cur { padding: 6px 10px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: #fff; }
.pager .pager-cur { background: var(--primary); color: #fff; border-color: var(--primary); }

.invoice-show .table tfoot td { font-size: 0.95rem; }

.ios-install-hint {
  position: fixed;
  left: calc(12px + var(--safe-left));
  right: calc(12px + var(--safe-right));
  bottom: calc(80px + var(--safe-bottom));
  z-index: 90;
  background: #0f172a;
  color: #f8fafc;
  border-radius: var(--radius);
  padding: 14px 40px 14px 16px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.25);
  font-size: 0.95rem;
  line-height: 1.4;
}
.ios-install-hint strong { display: block; margin-bottom: 4px; }
.ios-install-hint p { margin: 0; color: rgba(248,250,252,0.85); }
.ios-install-hint .ios-icon { display: inline-block; font-size: 1.05em; vertical-align: middle; }
.ios-install-close {
  position: absolute;
  top: 6px;
  right: 8px;
  background: transparent;
  color: rgba(248,250,252,0.85);
  border: 0;
  font-size: 22px;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
}

/* ---------------- Mobile nav (hamburger) ---------------- */
.nav-toggle { display: none; }

@media (max-width: 768px) {
  .topbar { gap: 8px; }
  .topbar .brand { flex: 1; }

  .nav-toggle {
    display: inline-flex;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 44px;
    height: 44px;
    background: transparent;
    border: 1px solid rgba(255,255,255,0.18);
    border-radius: var(--radius-sm);
    padding: 0;
    cursor: pointer;
  }
  .nav-toggle:hover { background: rgba(255,255,255,0.06); }
  .nav-toggle:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
  .nav-toggle-bar {
    display: block;
    width: 22px;
    height: 2px;
    background: #fff;
    border-radius: 1px;
    margin: 0 auto;
    transition: transform .18s ease, opacity .15s ease;
  }
  .topbar.is-open .nav-toggle-bar:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
  }
  .topbar.is-open .nav-toggle-bar:nth-child(2) { opacity: 0; }
  .topbar.is-open .nav-toggle-bar:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
  }

  /* Drawer: nav-main + nav-user become full-width column under brand row */
  .topbar .nav-main,
  .topbar .nav-user {
    display: none;
    flex-basis: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }
  .topbar.is-open .nav-main,
  .topbar.is-open .nav-user {
    display: flex;
    animation: nav-slide-down .18s ease-out;
  }
  .topbar .nav-link {
    padding: 12px 14px;
    font-size: 1rem;
    border-radius: var(--radius-sm);
  }
  .topbar .nav-user {
    border-top: 1px solid rgba(255,255,255,0.08);
    padding-top: 8px;
    margin-top: 4px;
    gap: 8px;
  }
  .topbar .nav-user-name { padding: 8px 14px; }
  .topbar .logout-form .btn { width: 100%; justify-content: flex-start; }

  @keyframes nav-slide-down {
    from { transform: translateY(-6px); opacity: 0; }
    to   { transform: none; opacity: 1; }
  }

  /* Scrim main content while drawer open */
  body.nav-open main.container {
    filter: brightness(0.55);
    pointer-events: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .nav-toggle-bar,
  .topbar.is-open .nav-main,
  .topbar.is-open .nav-user {
    animation: none;
    transition: none;
  }
}

/* Invoice bulk actions */
.is-hidden { display: none !important; }
.bulk-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  background: var(--surface, #f8fafc);
  border: 1px solid var(--border, #e2e8f0);
  border-radius: 8px;
}
.bulk-bar .bulk-count { margin-right: auto; }
.bulk-col { width: 36px; text-align: center; }
.bulk-col input[type="checkbox"] { width: 18px; height: 18px; }
