/* ============================================================
   TradesBill PWA App Shell Styles
   ============================================================ */

/* ── App layout ──────────────────────────────────────────────── */
html, body { height: 100%; overflow: hidden; }

#app {
  display: grid;
  height: 100dvh;
  grid-template-rows: var(--topbar-height) 1fr;
  grid-template-columns: 1fr;
}

@media (min-width: 768px) {
  #app {
    grid-template-columns: var(--sidebar-width) 1fr;
    grid-template-rows: 1fr;
  }
}

/* ── Top bar (mobile only) ───────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--sp-4);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  grid-column: 1;
  grid-row: 1;
  position: sticky;
  top: 0;
  z-index: 50;
}

@media (min-width: 768px) {
  .topbar { display: none; }
}

.topbar-logo {
  font-size: var(--text-lg);
  font-weight: 800;
  letter-spacing: -0.02em;
}

.topbar-logo span { color: var(--accent); }

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

/* ── Sidebar ─────────────────────────────────────────────────── */
.sidebar {
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overscroll-behavior: contain;

  /* Mobile: fixed overlay */
  position: fixed;
  inset: 0;
  z-index: 200;
  transform: translateX(-100%);
  transition: transform var(--transition-slow);
  width: var(--sidebar-width);
}

.sidebar.open {
  transform: translateX(0);
}

@media (min-width: 768px) {
  .sidebar {
    position: static;
    transform: none;
    grid-column: 1;
    grid-row: 1;
    z-index: auto;
    width: auto;
  }
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.5);
  z-index: 199;
}

.sidebar-overlay.active { display: block; }

@media (min-width: 768px) {
  .sidebar-overlay { display: none !important; }
}

/* Sidebar header */
.sidebar-header {
  padding: var(--sp-5) var(--sp-5) var(--sp-4);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-logo {
  font-size: var(--text-xl);
  font-weight: 800;
  letter-spacing: -0.02em;
}
.sidebar-logo span { color: var(--accent); }

/* Sidebar nav */
.sidebar-nav {
  flex: 1;
  padding: var(--sp-3) var(--sp-3);
  overflow-y: auto;
}

.sidebar-section {
  margin-bottom: var(--sp-6);
}

.sidebar-section-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 0 var(--sp-3);
  margin-bottom: var(--sp-2);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-3);
  border-radius: var(--radius);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-secondary);
  transition: background var(--transition), color var(--transition);
  text-decoration: none;
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}

.nav-item:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.nav-item.active {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent);
}

.nav-item svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.nav-item-badge {
  margin-left: auto;
  background: var(--error);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
}

/* Sidebar footer (user + plan) */
.sidebar-footer {
  padding: var(--sp-4);
  border-top: 1px solid var(--border);
}

.user-info {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: background var(--transition);
}

.user-info:hover { background: var(--bg-elevated); }

.user-avatar {
  width: 34px;
  height: 34px;
  background: var(--accent);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-sm);
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}

.user-details { min-width: 0; flex: 1; }
.user-name    { font-size: var(--text-sm); font-weight: 600; }
.user-plan    { font-size: var(--text-xs); color: var(--text-muted); }

/* Sync indicator in sidebar footer */
.sync-status {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--radius);
  font-size: var(--text-xs);
  color: var(--text-muted);
  margin-top: var(--sp-2);
  cursor: default;
}

.sync-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--text-muted);
}

.sync-dot.syncing { background: var(--warning); animation: pulse 1s ease-in-out infinite; }
.sync-dot.synced  { background: var(--success); }
.sync-dot.error   { background: var(--error); }
.sync-dot.offline { background: var(--text-muted); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.4; }
}

/* ── Main content area ───────────────────────────────────────── */
#main {
  overflow-y: auto;
  overscroll-behavior-y: contain;
  display: flex;
  flex-direction: column;
}

@media (min-width: 768px) {
  #main {
    grid-column: 2;
    grid-row: 1;
  }
}

/* Page wrapper */
.page {
  flex: 1;
  padding: var(--sp-6) var(--sp-6);
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
}

@media (max-width: 639px) {
  .page { padding: var(--sp-4) var(--sp-4); }
}

/* Page header */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-4);
  margin-bottom: var(--sp-6);
  flex-wrap: wrap;
}

.page-title {
  font-size: var(--text-2xl);
  font-weight: 800;
  letter-spacing: -0.02em;
}

.page-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

/* ── Dashboard stats ─────────────────────────────────────────── */
.stats-grid {
  display: grid;
  gap: var(--sp-4);
  grid-template-columns: repeat(2, 1fr);
  margin-bottom: var(--sp-6);
}

@media (min-width: 640px) {
  .stats-grid { grid-template-columns: repeat(4, 1fr); }
}

.stat-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--sp-4) var(--sp-5);
}

.stat-label {
  font-size: var(--text-xs);
  color: var(--text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: var(--sp-2);
}

.stat-value {
  font-size: var(--text-2xl);
  font-weight: 800;
  letter-spacing: -0.02em;
  font-family: var(--font-mono);
}

.stat-value.overdue { color: var(--error); }
.stat-value.paid    { color: var(--success); }

/* ── Invoice list ────────────────────────────────────────────── */
.list-toolbar {
  display: flex;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
  flex-wrap: wrap;
}

.list-toolbar .input {
  flex: 1;
  min-width: 200px;
}

.filter-tabs {
  display: flex;
  gap: var(--sp-1);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--sp-1);
}

.filter-tab {
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--radius);
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text-muted);
  transition: background var(--transition), color var(--transition);
  cursor: pointer;
  border: none;
  background: none;
  white-space: nowrap;
}

.filter-tab.active {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

/* Invoice rows */
.invoice-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: var(--sp-4);
  align-items: center;
  padding: var(--sp-4) var(--sp-4);
  border-bottom: 1px solid var(--border-subtle);
  cursor: pointer;
  transition: background var(--transition);
}

.invoice-row:hover { background: var(--bg-surface); }
.invoice-row:last-child { border-bottom: none; }

.invoice-row-main { min-width: 0; }
.invoice-number   { font-size: var(--text-sm); font-weight: 700; color: var(--text-primary); }
.invoice-client   { font-size: var(--text-xs); color: var(--text-secondary); margin-top: 2px; }
.invoice-date     { font-size: var(--text-xs); color: var(--text-muted); }

.invoice-amount {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  font-weight: 700;
  text-align: right;
  white-space: nowrap;
}

/* ── Invoice detail / form ───────────────────────────────────── */
.form-section {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: var(--sp-6);
  margin-bottom: var(--sp-4);
}

.form-section-title {
  font-size: var(--text-base);
  font-weight: 700;
  margin-bottom: var(--sp-5);
  padding-bottom: var(--sp-4);
  border-bottom: 1px solid var(--border);
}

.form-grid-2 {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--sp-4);
}

@media (min-width: 640px) {
  .form-grid-2 { grid-template-columns: 1fr 1fr; }
}

/* Line items table */
.line-items-table {
  width: 100%;
  border-collapse: collapse;
}

.line-items-table th {
  font-size: var(--text-xs);
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.line-items-table td {
  padding: var(--sp-2) var(--sp-2);
  border-bottom: 1px solid var(--border-subtle);
}

.line-item-desc  { width: 40%; }
.line-item-qty   { width: 10%; }
.line-item-unit  { width: 10%; }
.line-item-price { width: 15%; }
.line-item-gst   { width: 10%; }
.line-item-total { width: 12%; }
.line-item-del   { width: 3%; }

/* Invoice totals */
.invoice-totals {
  margin-top: var(--sp-4);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--sp-2);
}

.totals-row {
  display: flex;
  gap: var(--sp-8);
  font-size: var(--text-sm);
}

.totals-label { color: var(--text-secondary); min-width: 120px; text-align: right; }
.totals-value { font-family: var(--font-mono); min-width: 100px; text-align: right; font-weight: 600; }
.totals-row.grand { font-size: var(--text-lg); border-top: 2px solid var(--border); padding-top: var(--sp-2); }
.totals-row.grand .totals-value { color: var(--accent); }

/* ── PDF preview panel ───────────────────────────────────────── */
.pdf-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(100vw, 480px);
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 300;
  transform: translateX(100%);
  transition: transform var(--transition-slow);
  box-shadow: var(--shadow-xl);
}

.pdf-panel.open { transform: translateX(0); }

.pdf-panel-header {
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.pdf-panel-title { font-size: var(--text-base); font-weight: 700; }

.pdf-panel-actions {
  display: flex;
  gap: var(--sp-2);
}

.pdf-preview-frame {
  flex: 1;
  border: none;
  background: #525659;
}

/* ── Help button (floating) ──────────────────────────────────── */
#help-fab {
  position: fixed;
  bottom: var(--sp-6);
  right: var(--sp-6);
  width: 48px;
  height: 48px;
  background: var(--accent);
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-xl);
  font-weight: 800;
  box-shadow: var(--shadow-lg);
  z-index: 90;
  border: none;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition);
}

#help-fab:hover {
  background: var(--accent-hover);
  transform: scale(1.05);
}

/* ── Auth screens ────────────────────────────────────────────── */
.auth-page {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-6);
  background: var(--bg-base);
}

.auth-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: var(--sp-8);
  width: 100%;
  max-width: 400px;
}

.auth-logo {
  font-size: var(--text-3xl);
  font-weight: 900;
  letter-spacing: -0.04em;
  text-align: center;
  margin-bottom: var(--sp-8);
}

.auth-logo span { color: var(--accent); }

.auth-title {
  font-size: var(--text-2xl);
  font-weight: 800;
  text-align: center;
  margin-bottom: var(--sp-2);
}

.auth-sub {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  text-align: center;
  margin-bottom: var(--sp-8);
}

.auth-divider {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  margin: var(--sp-5) 0;
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.auth-divider::before,
.auth-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.btn-google {
  background: #fff;
  color: #1a1a1a;
  border: 1px solid #ddd;
  font-weight: 600;
}

.btn-google:hover { background: #f5f5f5; }

.auth-footer {
  text-align: center;
  font-size: var(--text-sm);
  color: var(--text-muted);
  margin-top: var(--sp-6);
}

.auth-footer a { color: var(--accent); }

/* ── Settings page ───────────────────────────────────────────── */
.settings-nav {
  display: flex;
  gap: var(--sp-1);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--sp-1);
  margin-bottom: var(--sp-6);
  overflow-x: auto;
}

.settings-tab {
  padding: var(--sp-2) var(--sp-4);
  border-radius: var(--radius);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-muted);
  transition: all var(--transition);
  cursor: pointer;
  border: none;
  background: none;
  white-space: nowrap;
}

.settings-tab.active {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

/* ── Pro upgrade prompt ──────────────────────────────────────── */
.pro-gate {
  background: var(--bg-surface);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, var(--border));
  border-radius: var(--radius-xl);
  padding: var(--sp-8);
  text-align: center;
  max-width: 400px;
  margin: var(--sp-8) auto;
}

.pro-gate h3 { font-size: var(--text-xl); font-weight: 800; margin-bottom: var(--sp-3); }
.pro-gate p  { color: var(--text-secondary); font-size: var(--text-sm); margin-bottom: var(--sp-6); line-height: 1.65; }

/* ── Help overlay ────────────────────────────────────────────── */
.help-overlay {
  position: fixed;
  inset: 0;
  z-index: 400;
  display: flex;
  align-items: stretch;
  justify-content: flex-end;
  pointer-events: none;
}

.help-overlay.open { pointer-events: auto; }

.help-backdrop {
  position: absolute;
  inset: 0;
  background: rgb(0 0 0 / 0.5);
  opacity: 0;
  transition: opacity var(--transition-slow);
}

.help-overlay.open .help-backdrop { opacity: 1; }

.help-panel {
  position: relative;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  width: min(100vw, 420px);
  height: 100%;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--transition-slow);
}

.help-overlay.open .help-panel { transform: translateX(0); }

.help-panel-header {
  padding: var(--sp-5);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.help-tabs {
  display: flex;
  gap: var(--sp-1);
  border-bottom: 1px solid var(--border);
  padding: var(--sp-2) var(--sp-4);
  flex-shrink: 0;
}

.help-tab {
  padding: var(--sp-2) var(--sp-4);
  border-radius: var(--radius);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-muted);
  background: none;
  border: none;
  cursor: pointer;
  transition: all var(--transition);
}

.help-tab.active {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent);
}

.help-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-5);
}

.help-search-wrap {
  margin-bottom: var(--sp-4);
}

.help-results { display: flex; flex-direction: column; gap: var(--sp-2); }

.help-result {
  padding: var(--sp-4);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: border-color var(--transition);
}

.help-result:hover { border-color: var(--accent); }

.help-result-title { font-size: var(--text-sm); font-weight: 700; margin-bottom: var(--sp-1); }
.help-result-section { font-size: var(--text-xs); color: var(--text-muted); }

/* AI chat */
.chat-messages {
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
  margin-bottom: var(--sp-4);
}

.chat-msg {
  display: flex;
  gap: var(--sp-3);
  align-items: flex-start;
}

.chat-msg-user { flex-direction: row-reverse; }

.chat-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-xs);
  font-weight: 700;
}

.chat-msg-ai    .chat-avatar { background: var(--accent); color: #fff; }
.chat-msg-user  .chat-avatar { background: var(--bg-elevated); color: var(--text-primary); }

.chat-bubble {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--sp-3) var(--sp-4);
  font-size: var(--text-sm);
  line-height: 1.65;
  max-width: 85%;
}

.chat-msg-user .chat-bubble {
  background: color-mix(in srgb, var(--accent) 15%, var(--bg-elevated));
  border-color: color-mix(in srgb, var(--accent) 30%, var(--border));
}

.chat-input-row {
  display: flex;
  gap: var(--sp-2);
  padding-top: var(--sp-4);
  border-top: 1px solid var(--border);
}

.chat-usage {
  font-size: var(--text-xs);
  color: var(--text-muted);
  text-align: right;
  margin-top: var(--sp-2);
}
