*{box-sizing:border-box;margin:0;padding:0}
body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;background:#f7f7f9;color:#111;font-size:14px}
.hidden{display:none!important}
/* First-paint auth-driven visibility so the topbar never flashes before JS. */
html[data-auth="out"] .topbar,
html[data-auth="out"] .chat-panel,
html[data-auth="out"] main > .view{display:none!important}
html[data-auth="in"] #login{display:none!important}
.topbar{display:flex;align-items:center;gap:16px;padding:12px 20px;background:#111827;color:#fff;position:sticky;top:0;z-index:5}
.brand{font-weight:700}
.topbar select{background:#1f2937;color:#fff;border:1px solid #374151;padding:6px 10px;border-radius:6px;max-width:220px}
.topbar nav{display:flex;gap:12px;flex:1;margin-left:12px}
.topbar nav a{color:#e5e7eb;text-decoration:none;padding:6px 10px;border-radius:4px;cursor:pointer;font-size:13px}
.topbar nav a.active{background:rgba(255,255,255,.14)}
.chat-btn{background:#f59e0b;color:#111;border:none;font-size:18px;padding:6px 12px;border-radius:6px;cursor:pointer}

/* Topbar user menu */
.user-menu{position:relative}
.user-btn{background:#1f2937;color:#fff;border:1px solid #374151;padding:6px 12px;border-radius:6px;font-size:12px;cursor:pointer;display:inline-flex;align-items:center;gap:6px;font-weight:500}
.user-btn:hover{background:#374151}
.user-btn .chev{font-size:10px;opacity:.7}
.user-dropdown{position:absolute;top:100%;right:0;margin-top:6px;background:#fff;color:#111;border-radius:8px;box-shadow:0 10px 30px rgba(0,0,0,.18);min-width:220px;z-index:40;overflow:hidden}
.user-dropdown-header{padding:10px 14px;font-size:12px;color:#6b7280;background:#f9fafb;border-bottom:1px solid #eee;word-break:break-all}
.user-dropdown button{display:block;width:100%;text-align:left;background:transparent;color:#111;border:none;padding:10px 14px;font-size:13px;font-weight:500;cursor:pointer}
.user-dropdown button:hover{background:#f3f4f6}
.user-dropdown button[data-user-action="logout"]{border-top:1px solid #eee;color:#b91c1c}
main{padding:24px;max-width:1200px;margin:0 auto}
.view{background:#fff;border-radius:8px;padding:20px;box-shadow:0 1px 3px rgba(0,0,0,.05)}
.card{background:#fff;border-radius:8px;padding:24px;max-width:420px;margin:60px auto;box-shadow:0 4px 16px rgba(0,0,0,.06)}
h2{font-size:18px;font-weight:600;margin-bottom:16px}
.cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:12px}
.stat{background:#f9fafb;padding:16px;border-radius:8px;border:1px solid #eee}
.stat label{color:#666;font-size:12px;display:block;margin-bottom:6px}
.stat > div{font-size:22px;font-weight:700}
.row{display:flex;gap:12px;flex-wrap:wrap}
.btn,button{display:inline-block;background:#0f766e;color:#fff;padding:9px 16px;border-radius:6px;text-decoration:none;font-weight:600;font-size:13px;border:none;cursor:pointer}
.muted{color:#666;font-size:12px}
input,select{padding:8px 10px;border:1px solid #d4d4d8;border-radius:6px;font-size:14px}

/* Modals + forms */
.modal{position:fixed;inset:0;background:rgba(0,0,0,.4);display:flex;align-items:center;justify-content:center;z-index:30;padding:20px;overflow-y:auto}
.modal .card{max-width:560px;width:100%;margin:0}
.modal .card.wide{max-width:760px}
.modal h3{font-size:16px;margin-bottom:16px}
.modal form{display:flex;flex-direction:column;gap:10px}
.modal label{display:block;font-size:12px;color:#555;margin-bottom:0}
.modal label input,.modal label select{width:100%;margin-top:3px}
.grid2{display:grid;grid-template-columns:1fr 1fr;gap:10px}
.row.end{justify-content:flex-end;margin-top:6px}
button.ghost{background:transparent;color:#666;border:1px solid #d4d4d8}
button.ghost:hover{background:#f5f5f5}
table.lines{width:100%;border-collapse:collapse;margin:8px 0}
table.lines th{background:#f9fafb;font-weight:600;color:#555;padding:6px 8px;text-align:left;font-size:12px;border-bottom:1px solid #eee}
table.lines td{padding:4px}
table.lines td input,table.lines td select{width:100%;font-size:13px;padding:6px}
table.lines td:last-child{width:32px}
#journal-totals b{color:#111}

/* Reports */
.subnav{display:flex;gap:2px;background:#f3f4f6;border-radius:6px;padding:4px;margin-bottom:16px;flex-wrap:wrap}
.subnav a{padding:6px 12px;border-radius:4px;text-decoration:none;color:#374151;font-size:13px;cursor:pointer;font-weight:500}
.subnav a.active{background:#0f766e;color:#fff}
.subnav a:hover:not(.active){background:#e5e7eb}
.rpt-filters{align-items:end;flex-wrap:wrap;gap:12px;margin-bottom:16px}
.rpt-filters label{display:flex;flex-direction:column;font-size:12px;color:#555;margin:0}
.rpt-filters input{width:auto;min-width:130px;margin-top:2px}
.rpt-head{margin-bottom:12px}
.rpt-head h3{font-size:18px;margin:0 0 4px}
.rpt-grid{display:grid;grid-template-columns:1fr 1fr;gap:16px;margin-bottom:16px}
.rpt-block{background:#fafafa;border:1px solid #eee;border-radius:6px;padding:12px}
.rpt-block h4{font-size:13px;color:#374151;margin-bottom:8px;text-transform:uppercase;letter-spacing:.5px}
.rpt-block table{width:100%;border-collapse:collapse}
.rpt-block th,.rpt-block td{padding:6px 8px;font-size:13px;border-bottom:1px solid #eee;text-align:left}
.rpt-block td.num,.rpt-block th.num{text-align:right;font-variant-numeric:tabular-nums}
.rpt-block tr.total td{font-weight:700;border-top:2px solid #333;background:#fff}
.rpt-net{padding:14px 16px;border-radius:6px;text-align:center;font-size:15px;font-weight:700;margin-top:8px}
.rpt-net.profit{background:#dcfce7;color:#166534}
.rpt-net.loss{background:#fee2e2;color:#991b1b}

/* Chat panel */
.chat-panel{position:fixed;bottom:24px;right:24px;width:360px;height:500px;background:#fff;border-radius:12px;box-shadow:0 10px 30px rgba(0,0,0,.2);display:flex;flex-direction:column;z-index:20}
.chat-panel header{background:#0f766e;color:#fff;padding:12px 14px;border-radius:12px 12px 0 0;font-weight:600;display:flex;justify-content:space-between;align-items:center}
.chat-panel header button{background:none;color:#fff;font-size:20px;padding:0;width:24px;height:24px}
.chat-msgs{flex:1;overflow-y:auto;padding:12px;display:flex;flex-direction:column;gap:8px}
.chat-msgs .u,.chat-msgs .a{padding:8px 12px;border-radius:12px;max-width:85%;white-space:pre-wrap;font-size:13px;line-height:1.4}
.chat-msgs .u{background:#0f766e;color:#fff;align-self:flex-end}
.chat-msgs .a{background:#f3f4f6;color:#111;align-self:flex-start}
.chat-panel form{display:flex;gap:6px;padding:10px;border-top:1px solid #eee}
.chat-panel input{flex:1}

/* Every list view: horizontally scroll wide tables instead of overflowing. */
.view > div{overflow-x:auto;-webkit-overflow-scrolling:touch}
.view > div > table{min-width:560px}

/* Mobile-first responsive rules.
   ≤ 900 : shrink top nav padding, allow horizontal scroll for nav pills
   ≤ 640 : single-column stat cards + form grid, edge-to-edge modals + chat */
@media (max-width: 900px) {
  .topbar { padding: 10px 12px; gap: 10px; flex-wrap: wrap; }
  .topbar select { max-width: 40vw; }
  .topbar nav { order: 10; width: 100%; margin: 8px 0 0; overflow-x: auto; gap: 4px; padding-bottom: 4px; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .topbar nav::-webkit-scrollbar { display: none; }
  .topbar nav a { padding: 6px 10px; font-size: 12px; white-space: nowrap; }
  main { padding: 14px 10px; }
  .view { padding: 14px 12px; }
  .rpt-grid { grid-template-columns: 1fr; }
  .subnav { gap: 0; }
  .subnav a { padding: 8px 10px; font-size: 12px; }
  .rpt-filters label { flex: 1 1 45%; }
  .rpt-filters input { min-width: 0; width: 100%; }
}

@media (max-width: 640px) {
  .brand { font-size: 14px; }
  .chat-btn { padding: 4px 10px; }
  h2 { font-size: 15px; margin-bottom: 12px; }
  h3 { font-size: 14px; }
  .cards { grid-template-columns: 1fr; }
  .stat > div { font-size: 18px; }
  .grid2 { grid-template-columns: 1fr; }
  .row:not(.end) { flex-direction: column; align-items: stretch; }
  .row.end { flex-direction: row; }
  .modal { padding: 0; align-items: stretch; }
  .modal .card { max-width: 100%; border-radius: 0; padding: 20px 16px; min-height: 100vh; overflow-y: auto; margin: 0; }
  .modal .card.wide { max-width: 100%; }
  .chat-panel { width: 100%; height: 100dvh; bottom: 0; right: 0; left: 0; border-radius: 0; }
  .chat-panel header { border-radius: 0; }
  table { font-size: 12px; }
  th, td { padding: 6px 8px !important; }
  .view > div > table { min-width: 480px; }
  .rpt-filters { flex-direction: column; align-items: stretch; }
  .rpt-filters label { flex: none; }
  button, .btn { padding: 10px 14px; font-size: 13px; min-height: 40px; }
}

/* Print — hide chrome, expand reports. */
@media print {
  .topbar, .subnav, .rpt-filters, #btn-print-report, .chat-panel, .chat-btn { display: none !important; }
  body { background: #fff; }
  .view { box-shadow: none; padding: 0; }
  .rpt-grid { grid-template-columns: 1fr 1fr; }
}
