:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-elev: #ffffff;
  --text: #1a1d21;
  --muted: #5f6b7a;
  --border: #e6e9ee;
  --accent: #3b82f6;
  --accent-dark: #1e60d6;
  --accent-soft: rgba(59, 130, 246, 0.10);
  --success: #16a34a;
  --danger: #dc2626;
  --warn: #d97706;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 1px 1px rgba(15, 23, 42, .02);
  --shadow-md: 0 4px 14px rgba(15, 23, 42, .06);
  --sidebar-w: 240px;
  --sidebar-w-collapsed: 68px;
  --transition: 200ms ease;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

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

h1, h2, h3 { margin: 0 0 .5rem; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; }

.muted { color: var(--muted); }
.right { text-align: right; }
.center { text-align: center; }
.mt { margin-top: 1rem; }
.mb { margin-bottom: 1rem; }
.pos { color: var(--success); }
.neg { color: var(--danger); }

/* Public landing */
.landing {
  max-width: 720px;
  margin: 6rem auto;
  padding: 0 1.5rem;
  text-align: center;
}
.landing h1 { font-size: 3rem; margin-bottom: 0.5rem; letter-spacing: -0.02em; }
.landing p { font-size: 1.2rem; color: var(--muted); }
.landing .cta { margin-top: 2rem; }
.landing .cta a { display: inline-block; margin: 0 .5rem; }

/* ---- App shell: sidebar + main ---- */

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
  transition: grid-template-columns var(--transition);
}
html.sidebar-collapsed .app-shell { grid-template-columns: var(--sidebar-w-collapsed) 1fr; }

.app-sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 50;
}

/* Mobile drawer backdrop (hidden by default; only visible on mobile when open). */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
}

/* Mobile top bar with hamburger (hidden by default; mobile media query shows it). */
.app-mobile-bar {
  display: none;
  align-items: center;
  gap: .5rem;
  padding: .5rem .75rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  height: 52px;
  position: sticky;
  top: 0;
  z-index: 30;
}
.app-mobile-bar-title { font-weight: 700; font-size: 1rem; }
.mobile-menu-btn {
  background: transparent;
  color: var(--text);
  border: none;
  padding: .35rem;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.mobile-menu-btn:hover { background: rgba(0,0,0,.05); }

.sidebar-header {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: 1rem 0.875rem;
  height: 64px;
  border-bottom: 1px solid var(--border);
}
html.sidebar-collapsed .sidebar-header {
  padding: 1rem 0;
  justify-content: center;
}

.app-brand {
  display: flex;
  align-items: center;
  gap: .5rem;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  letter-spacing: -0.01em;
  min-width: 0;
  overflow: hidden;
}
.app-brand:hover { text-decoration: none; }
html.sidebar-collapsed .app-brand { display: none; }

.brand-mark {
  display: inline-flex;
  width: 32px; height: 32px;
  align-items: center; justify-content: center;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), #6366f1);
  color: white;
  font-weight: 700;
  font-size: 1rem;
  flex-shrink: 0;
}
.brand-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-toggle {
  background: transparent;
  color: var(--muted);
  border: none;
  padding: .4rem;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: auto;
  flex-shrink: 0;
}
.sidebar-toggle:hover { background: rgba(0,0,0,.05); color: var(--text); }
html.sidebar-collapsed .sidebar-toggle { margin-left: 0; }

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: .15rem;
  padding: .75rem .5rem;
  flex: 1;
  overflow-y: auto;
}
.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .55rem .75rem;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  transition: background var(--transition), color var(--transition);
}
.sidebar-nav a:hover { background: rgba(0,0,0,.04); color: var(--text); text-decoration: none; }
.sidebar-nav a.active { background: var(--accent-soft); color: var(--accent); }
.sidebar-nav .nav-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}
.sidebar-nav .nav-icon svg { width: 20px; height: 20px; }
.nav-label { overflow: hidden; text-overflow: ellipsis; }
html.sidebar-collapsed .sidebar-nav a { justify-content: center; padding-left: 0; padding-right: 0; }
html.sidebar-collapsed .nav-label { display: none; }

.sidebar-footer {
  padding: .75rem .5rem 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: .25rem;
}
.footer-user {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .4rem .75rem;
  color: var(--muted);
  font-size: .85rem;
  white-space: nowrap;
  overflow: hidden;
}
.footer-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  flex-shrink: 0;
  text-transform: uppercase;
}
.footer-text { overflow: hidden; text-overflow: ellipsis; }
html.sidebar-collapsed .footer-text { display: none; }
html.sidebar-collapsed .footer-user { justify-content: center; padding: .4rem 0; }
html.sidebar-collapsed .sidebar-signout { justify-content: center; padding: .5rem 0; }

.sidebar-signout-form { margin: 0; }
.sidebar-signout {
  background: transparent;
  color: var(--muted);
  border: none;
  padding: .5rem .75rem;
  border-radius: 8px;
  width: 100%;
  display: flex;
  align-items: center;
  gap: .75rem;
  cursor: pointer;
  font: inherit;
  font-weight: 500;
}
.sidebar-signout:hover { background: rgba(0,0,0,.04); color: var(--text); }

/* ---- Main area ---- */

.app-main {
  min-width: 0;
}
.app-main-inner {
  max-width: 1100px;
  padding: 1.75rem 2rem;
  margin: 0 auto;
}

/* ---- Cards & layout ---- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: 1rem;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.25rem;
}
.stat-label { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; font-weight: 500; }
.stat-value { font-size: 1.7rem; font-weight: 600; margin-top: .35rem; letter-spacing: -0.01em; }

table.records {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
}
table.records th, table.records td {
  text-align: left;
  padding: .7rem .75rem;
  border-bottom: 1px solid var(--border);
}
table.records th {
  font-size: .78rem;
  text-transform: uppercase;
  color: var(--muted);
  letter-spacing: .04em;
  background: transparent;
  font-weight: 500;
}
table.records tbody tr:hover { background: rgba(0,0,0,.02); }
table.records tbody tr:last-child td { border-bottom: none; }

form .field { margin-bottom: 1rem; }
form label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .35rem; font-weight: 500; }
form input[type=text],
form input[type=email],
form input[type=password],
form input[type=date],
form input[type=number],
form input[type=file],
form select,
form textarea {
  width: 100%;
  max-width: 480px;
  padding: .6rem .75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  background: var(--surface);
  color: var(--text);
  transition: border-color var(--transition), box-shadow var(--transition);
}
form input:focus, form select:focus, form textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
form textarea { min-height: 80px; }
form .actions { margin-top: 1rem; display: flex; gap: .5rem; align-items: center; }

input[type=submit], button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: white;
  border: none;
  padding: .55rem 1.1rem;
  border-radius: var(--radius-sm);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: background var(--transition), transform 60ms ease;
}
input[type=submit]:hover, button:hover, .btn:hover { background: var(--accent-dark); text-decoration: none; color: white; }
input[type=submit]:active, button:active, .btn:active { transform: translateY(1px); }
.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); box-shadow: var(--shadow); }
.btn-secondary:hover { background: #f5f7fa; color: var(--text); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #b91c1c; }
.btn-sm { padding: .35rem .75rem; font-size: .85rem; }

.flash { padding: .85rem 1rem; border-radius: var(--radius-sm); margin-bottom: 1rem; }
.flash-notice { background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0; }
.flash-alert  { background: #fef2f2; color: #7f1d1d; border: 1px solid #fecaca; }

.swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  vertical-align: middle;
  margin-right: .35rem;
}

.bar-row { display: grid; grid-template-columns: 160px 1fr 90px; gap: .75rem; align-items: center; padding: .4rem 0; }
.bar { height: 10px; background: var(--border); border-radius: 4px; overflow: hidden; }
.bar > span { display: block; height: 100%; }

.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.25rem; gap: 1rem; flex-wrap: wrap; }
.page-header h1 { margin: 0; }
.page-header h2 { margin: 0; }

.line-items { display: flex; flex-direction: column; gap: .5rem; }
.line-item {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: .5rem;
  align-items: end;
}
.line-item .field { margin: 0; }
.line-item.removed { display: none; }
.line-item-remove {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--border);
  padding: .6rem .85rem;
}
.line-item-remove:hover { background: #fef2f2; color: var(--danger); }

.errors { background: #fef2f2; border: 1px solid #fecaca; color: #7f1d1d; padding: .8rem 1rem; border-radius: var(--radius-sm); margin-bottom: 1rem; }
.errors ul { margin: 0; padding-left: 1.25rem; }

.receipt-preview { max-width: 240px; max-height: 240px; border: 1px solid var(--border); border-radius: var(--radius-sm); }

.auth-card { max-width: 420px; margin: 4rem auto; }
.filters { display: flex; gap: .75rem; align-items: end; flex-wrap: wrap; margin-bottom: 1rem; }
.filters .field { margin: 0; }
.filters input, .filters select { max-width: 200px; }

/* ---- Responsive: auto-collapse sidebar on narrow viewports ---- */

/* Tables: wrap in .table-wrap so they scroll horizontally on narrow screens
   rather than squashing their cells. */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

@media (max-width: 720px) {
  /* Single column — main takes the full width; sidebar is an overlay drawer. */
  .app-shell,
  html.sidebar-collapsed .app-shell { grid-template-columns: 1fr; }

  .app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-w);
    height: 100vh;
    height: 100dvh;
    transform: translateX(-100%);
    transition: transform 220ms ease;
    box-shadow: 4px 0 24px rgba(15, 23, 42, .12);
  }
  html.sidebar-open .app-sidebar { transform: translateX(0); }
  .sidebar-backdrop { display: block; }
  html.sidebar-open .sidebar-backdrop { opacity: 1; pointer-events: auto; }

  /* Inside the drawer on mobile, always show full labels (collapsed-state
     was a desktop concept and doesn't apply when the whole sidebar is hidden). */
  .app-brand, .nav-label, .footer-text { display: revert; }
  .sidebar-header { padding: 1rem 0.875rem; justify-content: flex-start; }
  .sidebar-toggle { margin-left: auto; }
  .sidebar-nav a { justify-content: flex-start; padding: .55rem .75rem; }
  .footer-user { justify-content: flex-start; padding: .4rem .75rem; }
  .sidebar-signout { justify-content: flex-start; padding: .5rem .75rem; }

  .app-mobile-bar { display: flex; }
  .app-main-inner { padding: 1rem; }
  .line-item { grid-template-columns: 1fr; }

  /* Force tables to keep readable column widths; the .table-wrap container
     scrolls horizontally instead of crushing each cell. */
  table.records { min-width: 540px; }

  /* Bigger tap targets. */
  .btn, input[type=submit], button { min-height: 40px; }
}
