:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --border: #ecdfe2;
  --text: #2b1c22;
  --muted: #8a7178;
  --accent: #d9366f;
  --accent-dark: #b82a5c;
  --accent-light: #f2a3c4;
  --ok: #1f9d63;
  --warn: #b8720a;
  --err: #c73b5d;
  --sidebar-bg: #fbe4ec;
  --sidebar-bg-hover: #f6d2e0;
  --sidebar-text: #a8446f;
  --sidebar-text-active: #7a1f47;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}
* { box-sizing: border-box; }
body { background: var(--bg); color: var(--text); margin: 0; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.badge { background: var(--err); color: #fff; border-radius: 999px; padding: 0 6px; font-size: 0.7rem; margin-left: 4px; }

.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 230px; flex-shrink: 0; background: var(--sidebar-bg); min-height: 100vh;
  padding: 1rem 0; position: sticky; top: 0; align-self: flex-start; height: 100vh; overflow-y: auto;
}
.sidebar-brand {
  font-weight: 700; font-size: 1.15rem; line-height: 1.4; letter-spacing: 0.02em;
  padding: 0.5rem 1.25rem 1.25rem; border-bottom: 1px solid rgba(122,31,71,0.12); margin-bottom: 0.5rem;
}
.sidebar-brand .brand-primary { color: var(--accent); }
.sidebar-brand .brand-secondary { color: var(--sidebar-text-active); font-weight: 500; font-size: 0.85rem; letter-spacing: 0.15em; }
.sidebar-nav { display: flex; flex-direction: column; }
.sidebar-nav a, .nav-group-toggle {
  display: block; width: 100%; text-align: left; background: none; border: none; cursor: pointer;
  color: var(--sidebar-text); text-decoration: none; font-size: 0.88rem;
  padding: 0.55rem 1.25rem; border-left: 3px solid transparent;
}
.sidebar-nav a:hover, .nav-group-toggle:hover { background: var(--sidebar-bg-hover); color: var(--sidebar-text-active); text-decoration: none; }
.nav-item.active { color: var(--sidebar-text-active); background: var(--sidebar-bg-hover); border-left-color: var(--accent); font-weight: 600; }

.nav-group-toggle { display: flex; justify-content: space-between; align-items: center; font-weight: 500; }
.nav-group-toggle .chevron { transition: transform 0.15s; font-size: 0.7rem; }
.nav-group.open .nav-group-toggle .chevron { transform: rotate(180deg); }
.nav-group-items { max-height: 0; overflow: hidden; }
.nav-group.open .nav-group-items { max-height: 500px; }
.nav-group-items a { padding-left: 2.25rem; font-size: 0.85rem; }
.nav-group-items a.active { color: var(--sidebar-text-active); background: var(--sidebar-bg-hover); border-left-color: var(--accent); font-weight: 600; }

.main-area { flex: 1; min-width: 0; }
.container { max-width: 1200px; margin: 0 auto; padding: 1.5rem; }

h1 { font-size: 1.4rem; margin: 0 0 0.25rem; }
h2 { font-size: 1.1rem; margin: 1.5rem 0 0.5rem; color: var(--text); }
.subtitle { color: var(--muted); margin-bottom: 1.25rem; font-size: 0.9rem; }

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 1rem;
  box-shadow: 0 1px 2px rgba(16,24,40,0.04);
}
.card .num { font-size: 1.8rem; font-weight: 700; }
.card .label { color: var(--muted); font-size: 0.8rem; }

table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
th, td { padding: 0.5rem 0.65rem; border-bottom: 1px solid var(--border); text-align: left; font-size: 0.88rem; }
th { color: var(--muted); font-weight: 600; background: #f7eef1; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #fbf3f5; }

.flash { padding: 0.6rem 1rem; border-radius: 6px; margin-bottom: 1rem; font-size: 0.9rem; }
.flash.ok { background: rgba(31,157,99,0.1); border: 1px solid var(--ok); color: var(--ok); }
.flash.error { background: rgba(214,69,69,0.1); border: 1px solid var(--err); color: var(--err); }

.btn {
  display: inline-block; background: var(--accent); color: #fff; border: none; border-radius: 6px;
  padding: 0.45rem 0.9rem; font-size: 0.88rem; cursor: pointer;
}
.btn:hover { background: var(--accent-dark); text-decoration: none; }
.btn.secondary { background: #f3e6ea; color: var(--text); }
.btn.secondary:hover { background: #ecd7de; }
.btn.danger { background: var(--err); }
.btn.danger:hover { background: #a92e4a; }
.btn.small { padding: 0.25rem 0.55rem; font-size: 0.78rem; }

form.inline { display: inline; }

label { display: block; font-size: 0.82rem; color: var(--muted); margin: 0.6rem 0 0.2rem; }
input, select, textarea {
  width: 100%; padding: 0.45rem 0.55rem; background: #ffffff; border: 1px solid var(--border);
  border-radius: 6px; color: var(--text); font-size: 0.9rem;
}
textarea { min-height: 60px; }

.line-items { border: 1px solid var(--border); border-radius: 8px; padding: 0.75rem; margin-top: 0.75rem; background: var(--panel); }
.line-item-row { display: grid; grid-template-columns: repeat(6, 1fr) auto; gap: 0.5rem; align-items: end; margin-bottom: 0.5rem; }
.line-item-row label { margin: 0 0 0.15rem; }

.pill { padding: 0.1rem 0.5rem; border-radius: 999px; font-size: 0.75rem; }
.pill.reconciled { background: rgba(31,157,99,0.12); color: var(--ok); }
.pill.running_estimate { background: rgba(184,114,10,0.12); color: var(--warn); }
.pill.open { background: rgba(214,69,69,0.12); color: var(--err); }
.pill.pending { background: rgba(184,114,10,0.12); color: var(--warn); }
.pill.resolved { background: rgba(31,157,99,0.12); color: var(--ok); }

.actions-bar { display: flex; gap: 0.6rem; align-items: center; margin-bottom: 1rem; flex-wrap: wrap; }
.muted { color: var(--muted); }
.right { text-align: right; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

/* ---------------------------------------------------------------------------
   Sign-in page
   --------------------------------------------------------------------------- */
.login-body {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: var(--sidebar-bg); padding: 1.5rem;
}
.login-panel {
  width: 100%; max-width: 360px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 12px; padding: 1.75rem;
  box-shadow: 0 4px 20px rgba(122,31,71,0.10);
}
.login-brand {
  font-weight: 700; font-size: 1.25rem; line-height: 1.45; letter-spacing: 0.02em;
  margin-bottom: 1.25rem;
}
.login-brand .brand-primary { color: var(--accent); }
.login-brand .brand-secondary {
  color: var(--sidebar-text-active); font-weight: 500; font-size: 0.8rem; letter-spacing: 0.15em;
}
.login-btn { width: 100%; margin-top: 1rem; padding: 0.6rem; font-size: 0.95rem; }
.login-note { color: var(--muted); font-size: 0.78rem; margin: 1.1rem 0 0; text-align: center; }

/* Sign out, pinned under the nav */
.sidebar-footer {
  margin-top: auto; padding: 0.75rem 1.25rem 0.25rem;
  border-top: 1px solid rgba(122,31,71,0.12);
}
.sidebar-footer .sign-out { color: var(--sidebar-text); font-size: 0.82rem; }
.sidebar-footer .sign-out:hover { color: var(--sidebar-text-active); }
.sidebar { display: flex; flex-direction: column; }

/* ---------------------------------------------------------------------------
   Mobile top bar — hidden on desktop, revealed by the media query below.
   --------------------------------------------------------------------------- */
.mobile-bar { display: none; }
.sidebar-backdrop { display: none; }

/* ---------------------------------------------------------------------------
   Small screens.
   The layout was previously desktop-only: a fixed 230px sidebar took ~62% of a
   375px phone screen and pushed the content off the edge. On a phone the nav
   now collapses into a slide-over drawer behind a menu button, and wide tables
   scroll horizontally inside themselves instead of stretching the page.
   --------------------------------------------------------------------------- */
@media (max-width: 900px) {
  .mobile-bar {
    display: flex; align-items: center; gap: 0.75rem;
    position: sticky; top: 0; z-index: 30;
    background: var(--sidebar-bg); border-bottom: 1px solid rgba(122,31,71,0.14);
    padding: 0.55rem 0.9rem;
  }
  .mobile-nav-toggle {
    background: none; border: none; cursor: pointer; padding: 0.2rem 0.4rem;
    font-size: 1.35rem; line-height: 1; color: var(--sidebar-text-active);
  }
  .mobile-brand { font-weight: 700; font-size: 0.95rem; letter-spacing: 0.02em; }
  .mobile-brand .brand-primary { color: var(--accent); }
  .mobile-brand .brand-secondary {
    color: var(--sidebar-text-active); font-weight: 500; font-size: 0.72rem; letter-spacing: 0.12em;
  }

  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 40;
    width: 265px; height: 100%;
    transform: translateX(-100%); transition: transform 0.2s ease-out;
    box-shadow: 2px 0 12px rgba(43,28,34,0.18);
  }
  body.sidebar-open .sidebar { transform: translateX(0); }
  body.sidebar-open .sidebar-backdrop {
    display: block; position: fixed; inset: 0; z-index: 35;
    background: rgba(43,28,34,0.42);
  }

  .container { padding: 1rem; }
  h1 { font-size: 1.2rem; }

  /* Wide data tables become their own horizontal scroll area, so the page
     body itself never scrolls sideways. */
  table { display: block; width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  th, td { white-space: nowrap; }

  /* Multi-column line-item entry (receiving, transfers, manual sale) stacks
     instead of squeezing six inputs into a phone width. */
  .line-item-row { grid-template-columns: 1fr 1fr; }

  .grid { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0.75rem; }
  .card .num { font-size: 1.5rem; }
  .actions-bar { gap: 0.4rem; }
  .btn { padding: 0.5rem 0.85rem; }   /* easier tap target */
}

@media (max-width: 520px) {
  .line-item-row { grid-template-columns: 1fr; }
  .container { padding: 0.85rem; }
}
