@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@300;400;500;600&family=DM+Mono:wght@400;500&display=swap');

:root {
  --bg: #f5f5f3;
  --surface: #ffffff;
  --border: #e2e2de;
  --text: #1a1a1a;
  --text-mid: #6b6b6b;
  --text-light: #9b9b9b;
  --accent: #1a1a1a;
  --accent-hover: #333;
  --sidebar-bg: #1a1a1a;
  --sidebar-text: #c8c8c4;
  --sidebar-act: #ffffff;
  --sidebar-w: 220px;
  --green: #2d7d46;
  --red: #c0392b;
  --blue: #1a5fa8;
  --radius: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --topbar-h: 56px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 15px; }
body { font-family: 'DM Sans', sans-serif; background: var(--bg); color: var(--text); line-height: 1.55; min-height: 100vh; }

/* ── Sidebar ── */
.sidebar { position: fixed; top: 0; left: 0; bottom: 0; width: var(--sidebar-w); background: var(--sidebar-bg); display: flex; flex-direction: column; overflow-y: auto; z-index: 200; }
.sidebar-header { padding: 20px 18px 16px; border-bottom: 1px solid rgba(255,255,255,.08); display: flex; align-items: center; gap: 10px; }
.logo-mark { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; background: #fff; color: #1a1a1a; border-radius: 5px; font-size: 11px; font-weight: 700; flex-shrink: 0; }
.logo-text { color: #fff; font-size: 14px; font-weight: 600; }
.sidebar-section { padding: 12px 0; border-bottom: 1px solid rgba(255,255,255,.06); }
.sidebar-label { padding: 4px 18px 6px; font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: rgba(255,255,255,.3); }
.sidebar-link { display: flex; align-items: center; gap: 9px; padding: 8px 18px; color: var(--sidebar-text); text-decoration: none; font-size: 13px; transition: background .15s; }
.sidebar-link svg { width: 16px; height: 16px; fill: currentColor; flex-shrink: 0; opacity: .7; }
.sidebar-link:hover { background: rgba(255,255,255,.06); color: #fff; }
.sidebar-link.active { background: rgba(255,255,255,.1); color: #fff; font-weight: 500; }
.sidebar-link.active svg { opacity: 1; }
.sidebar-footer { margin-top: auto; padding: 14px 18px; border-top: 1px solid rgba(255,255,255,.08); font-size: 12px; color: rgba(255,255,255,.4); display: flex; flex-direction: column; gap: 4px; }
.logout-link { color: rgba(255,255,255,.5); text-decoration: none; font-size: 12px; }
.logout-link:hover { color: #fff; }

/* ── Mobile topbar + drawer — hidden on desktop ── */
.mobile-topbar { display: none; }
.mobile-drawer { display: none; }
.mobile-drawer-overlay { display: none; }

/* ── Main ── */
.main-content { margin-left: var(--sidebar-w); min-height: 100vh; }
.page-inner { padding: 28px 32px; max-width: 1100px; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; gap: 12px; flex-wrap: wrap; }
.page-header h1 { font-size: 20px; font-weight: 600; }
.section-header { margin: 24px 0 12px; }
.section-header h2 { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--text-mid); }

/* ── Buttons ── */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: var(--radius); font-size: 13px; font-family: inherit; font-weight: 500; cursor: pointer; text-decoration: none; transition: background .15s; white-space: nowrap; min-height: 38px; }
.btn:hover { background: #f0f0ee; }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-sm { padding: 5px 10px; font-size: 12px; min-height: 30px; }
.btn-success { background: var(--green); color: #fff; border-color: var(--green); }
.btn-danger { background: var(--red); color: #fff; border-color: var(--red); }
.btn-action { background: var(--blue); color: #fff; border-color: var(--blue); }
.btn-full { width: 100%; }
.btn-group { display: flex; gap: 8px; flex-wrap: wrap; }

/* ── Stats ── */
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 24px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); color: var(--text); display: block; }
a.stat-card:hover { border-color: #999; box-shadow: 0 2px 8px rgba(0,0,0,.12); transform: translateY(-1px); transition: all .15s; }
.stat-label { font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--text-mid); margin-bottom: 6px; }
.stat-value { font-size: 24px; font-weight: 600; }
.stat-overdue .stat-value { color: var(--red); }
.stat-sent .stat-value { color: var(--blue); }
.stat-paid .stat-value { color: var(--green); }
.stat-money .stat-value { font-size: 17px; }

/* ── Tables ── */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; box-shadow: var(--shadow); -webkit-overflow-scrolling: touch; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 500px; }
.data-table th { background: #fafaf8; border-bottom: 1px solid var(--border); padding: 10px 12px; text-align: left; font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--text-mid); white-space: nowrap; }
.data-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: #fafaf9; }
.data-table tfoot td { padding: 8px 12px; border-top: 1px solid var(--border); }
.data-table tfoot tr:last-child td { border-top: 2px solid var(--text); }
.total-row td { font-weight: 600; }
.text-right { text-align: right !important; }
.empty-cell { text-align: center; color: var(--text-light); padding: 24px; }
.actions-cell { white-space: nowrap; }

/* ── Card list ── */
.card-list { display: flex; flex-direction: column; gap: 10px; }
.company-row-card, .invoice-row-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.company-row-main { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; gap: 10px; }
.company-row-identity { display: flex; align-items: center; gap: 12px; }
.company-badge { display: inline-flex; align-items: center; justify-content: center; width: 36px; height: 36px; background: var(--accent); color: #fff; border-radius: 6px; font-family: 'DM Mono', monospace; font-size: 11px; font-weight: 600; flex-shrink: 0; }
.company-row-name { font-size: 14px; font-weight: 600; }
.company-row-meta { font-size: 12px; color: var(--text-light); margin-top: 2px; }
.company-row-tags { display: flex; gap: 6px; flex-shrink: 0; }
.company-row-actions { display: flex; gap: 8px; flex-wrap: wrap; padding-top: 10px; border-top: 1px solid var(--border); }
.company-row-actions .btn { flex: 1; min-width: 70px; }
.invoice-row-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 6px; }
.invoice-row-number { font-size: 15px; font-weight: 700; }
.invoice-row-client { font-size: 13px; color: var(--text-mid); margin-top: 2px; }
.invoice-row-amount { font-size: 16px; font-weight: 700; text-align: right; }
.invoice-row-dates { display: flex; gap: 16px; font-size: 12px; color: var(--text-light); margin-bottom: 10px; }
.invoice-row-actions { display: flex; gap: 8px; flex-wrap: wrap; padding-top: 10px; border-top: 1px solid var(--border); }
.invoice-row-actions .btn { flex: 1; min-width: 70px; }

/* ── Badges ── */
.badge { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; background: #eee; color: var(--text-mid); }
.badge-draft { background: #eee; color: #666; }
.badge-sent { background: #dbeafe; color: #1e40af; }
.badge-paid { background: #dcfce7; color: #166534; }
.badge-overdue { background: #fee2e2; color: #991b1b; }

/* ── Forms ── */
.form-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow); max-width: 900px; }
.form-section-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .07em; color: var(--text-mid); margin: 20px 0 12px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.form-section-title:first-child { margin-top: 0; }
.form-row { display: flex; gap: 12px; flex-wrap: wrap; }
.form-group { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; flex: 1; min-width: 140px; }
.form-col-1 { flex: 1; }
.form-col-2 { flex: 2; }
label { font-size: 11px; font-weight: 600; color: var(--text-mid); text-transform: uppercase; letter-spacing: .04em; }
label small { font-size: 11px; font-weight: 400; text-transform: none; letter-spacing: 0; }
input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="date"], input[type="file"], select, textarea { padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); font-family: inherit; font-size: 14px; color: var(--text); background: #fff; width: 100%; transition: border-color .15s; min-height: 42px; }
input:focus, select:focus, textarea:focus { outline: none; border-color: #888; box-shadow: 0 0 0 3px rgba(0,0,0,.06); }
textarea { resize: vertical; min-height: 80px; }
.checkbox-label { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 500; text-transform: none; letter-spacing: 0; color: var(--text); cursor: pointer; padding: 8px 0; }
.checkbox-label input[type="checkbox"] { width: 18px; height: 18px; flex-shrink: 0; }
.checkbox-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 4px; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.form-actions { display: flex; gap: 10px; margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.form-actions .btn { flex: 1; min-width: 100px; }
.form-note { font-size: 12px; color: var(--text-light); margin: -6px 0 10px; }

/* ── Line items ── */
.line-item-header { display: flex; gap: 8px; padding: 6px 0; font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--text-mid); border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.line-item-row { display: flex; gap: 8px; align-items: center; padding: 6px 0; border-bottom: 1px solid #f0f0ee; }
.line-item-row input, .line-item-row select { margin-bottom: 0; min-height: 38px; }
.invoice-totals { margin-top: 16px; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.totals-row { display: flex; gap: 32px; font-size: 13px; color: var(--text-mid); }
.totals-row span:last-child { min-width: 90px; text-align: right; font-family: 'DM Mono', monospace; }
.total-final { font-size: 15px; font-weight: 700; color: var(--text); padding-top: 6px; border-top: 2px solid var(--text); margin-top: 4px; }

/* ── Invoice view ── */
.invoice-view { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow); }
.invoice-meta-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 20px; padding-bottom: 16px; border-bottom: 1px solid var(--border); }
.meta-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--text-light); margin-bottom: 4px; }
.meta-value { font-size: 14px; font-weight: 500; }
.meta-addr  { font-size: 12px; color: var(--text-mid); line-height: 1.6; }
.invoice-notes { margin-top: 16px; padding: 12px; background: var(--bg); border-radius: var(--radius); font-size: 13px; }
.status-actions { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); display: flex; gap: 8px; flex-wrap: wrap; }
.status-actions .btn { flex: 1; min-width: 120px; }

/* ── Filter bar ── */
.filter-bar { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.filter-bar input, .filter-bar select { flex: 1; min-width: 140px; width: auto; }

/* ── Alerts ── */
.alert { padding: 12px 16px; border-radius: var(--radius); margin-bottom: 16px; font-size: 13px; font-weight: 500; }
.alert-success { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.alert-error { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }

/* ── Login ── */
.login-body { background: var(--bg); display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.login-wrap { width: 100%; max-width: 400px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 32px 28px; box-shadow: 0 4px 20px rgba(0,0,0,.08); }
.login-logo { display: flex; align-items: center; gap: 10px; margin-bottom: 24px; }
.login-logo .logo-mark { width: 36px; height: 36px; background: var(--accent); color: #fff; border-radius: 6px; font-size: 12px; }
.login-logo .logo-text { color: var(--text); font-size: 18px; font-weight: 700; }

/* ── Select company ── */
.select-body { background: var(--bg); display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.select-wrap { width: 100%; max-width: 700px; }
.select-header { text-align: center; margin-bottom: 32px; }
.select-logo { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 8px; }
.select-logo .logo-mark { width: 36px; height: 36px; background: #1a1a1a; color: #fff; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; }
.select-logo .logo-text { color: #1a1a1a; font-size: 18px; font-weight: 700; }
.select-subtitle { color: #6b6b6b; font-size: 14px; }
.company-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 16px; margin-bottom: 32px; }
.company-card { background: #fff; border: 1px solid #e2e2de; border-radius: 8px; padding: 24px 16px; text-align: center; text-decoration: none; color: #1a1a1a; transition: border-color .15s, box-shadow .15s, transform .15s; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.company-card:hover { border-color: #1a1a1a; box-shadow: 0 4px 16px rgba(0,0,0,.1); transform: translateY(-2px); }
.company-card-initials { width: 56px; height: 56px; background: #1a1a1a; color: #fff; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 18px; font-weight: 700; }
.company-card-logo img { max-width: 56px; max-height: 56px; object-fit: contain; }
.company-card-name { font-size: 14px; font-weight: 600; line-height: 1.3; }
.company-card-prefix { font-size: 11px; color: #9b9b9b; font-family: 'DM Mono', monospace; }
.select-footer { text-align: center; font-size: 12px; color: #9b9b9b; }
.select-footer a { color: #6b6b6b; }

/* ── Empty state ── */
.empty-state { text-align: center; padding: 40px 20px; color: var(--text-light); font-size: 14px; }
.empty-state a { color: var(--accent); }
code { font-family: 'DM Mono', monospace; background: var(--bg); padding: 2px 6px; border-radius: 3px; font-size: 12px; }

/* ============================================================
   MOBILE
   ============================================================ */
@media (max-width: 768px) {
  .sidebar { display: none; }

  .mobile-topbar { display: flex; align-items: center; justify-content: space-between; position: fixed; top: 0; left: 0; right: 0; height: var(--topbar-h); background: var(--sidebar-bg); padding: 0 16px; z-index: 300; box-shadow: 0 2px 8px rgba(0,0,0,.2); }
  .mobile-topbar-logo { display: flex; align-items: center; gap: 8px; }
  .mobile-topbar-logo .logo-mark { width: 28px; height: 28px; font-size: 10px; }
  .mobile-topbar-logo .logo-text { font-size: 14px; font-weight: 600; color: #fff; }
  .mobile-menu-btn { background: none; border: none; cursor: pointer; padding: 8px; color: #fff; }
  .mobile-menu-btn svg { width: 22px; height: 22px; fill: currentColor; }

  .mobile-drawer { display: flex; position: fixed; top: 0; right: 0; bottom: 0; width: 260px; background: var(--sidebar-bg); z-index: 400; transform: translateX(100%); transition: transform .25s ease; flex-direction: column; overflow-y: auto; }
  .mobile-drawer.open { transform: translateX(0); }
  .mobile-drawer-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 350; }
  .mobile-drawer-overlay.open { display: block; }
  .mobile-drawer-header { padding: 16px; border-bottom: 1px solid rgba(255,255,255,.08); display: flex; align-items: center; justify-content: space-between; }
  .mobile-drawer-close { background: none; border: none; color: rgba(255,255,255,.6); cursor: pointer; font-size: 20px; padding: 4px 8px; }
  .mobile-drawer .sidebar-section { padding: 8px 0; }
  .mobile-drawer .sidebar-link { padding: 10px 16px; font-size: 14px; }
  .mobile-drawer .sidebar-label { padding: 4px 16px 6px; }
  .mobile-drawer .sidebar-footer { padding: 14px 16px; }

  .main-content { margin-left: 0; padding-top: var(--topbar-h); }
  .page-inner { padding: 14px 14px 24px; }
  .page-header { margin-top: 10px; }
  .page-header h1 { font-size: 18px; }

  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat-value { font-size: 20px; }
  .stat-money .stat-value { font-size: 14px; }

  .form-card { padding: 16px; }
  .form-row { flex-direction: column; gap: 0; }
  .form-col-1, .form-col-2 { flex: 1 1 100%; }

  .invoice-meta-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }

  .line-item-header { display: none; }
  .line-item-row { flex-direction: column; gap: 6px; background: var(--bg); border-radius: var(--radius); margin-bottom: 8px; padding: 10px; border-bottom: none; }
  .line-item-row input, .line-item-row select { flex: none !important; width: 100% !important; }

  .btn-group { flex-direction: column; }
  .btn-group .btn { width: 100%; }

  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar input, .filter-bar select { width: 100%; }

  .status-actions .btn { flex: 1 1 100%; }

  .company-grid { grid-template-columns: repeat(2, 1fr); }
}


/* ── Description autocomplete ── */
.desc-wrap { position: relative; }
.desc-dropdown { position: absolute; top: 100%; left: 0; right: 0; background: var(--surface); border: 1px solid var(--border); border-top: none; border-radius: 0 0 var(--radius) var(--radius); box-shadow: 0 4px 12px rgba(0,0,0,.12); z-index: 500; max-height: 260px; overflow-y: auto; }
.desc-option { padding: 9px 12px; cursor: pointer; font-size: 13px; border-bottom: 1px solid var(--border); line-height: 1.4; }
.desc-option:last-child { border-bottom: none; }
.desc-option:hover { background: var(--bg); }
.desc-option strong { color: var(--text); }
.desc-option small { color: var(--text-mid); }
.desc-option-price { font-family: 'DM Mono', monospace; font-size: 12px; color: var(--green); margin-left: 6px; }
.desc-option-add { color: var(--blue); font-size: 12px; background: #f0f5ff; }
.desc-option-add:hover { background: #e0ecff; }
.desc-toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px); background: #1a1a1a; color: #fff; padding: 10px 20px; border-radius: 20px; font-size: 13px; opacity: 0; transition: opacity .25s, transform .25s; pointer-events: none; z-index: 9999; }
.desc-toast-show { opacity: 1; transform: translateX(-50%) translateY(0); }
.desc-page-intro { font-size: 13px; color: var(--text-mid); margin-bottom: 18px; max-width: 620px; line-height: 1.6; }

/* Logo display fixes */
.sidebar-header img { max-height: 28px; max-width: 140px; width: auto; height: auto; object-fit: contain; }
.company-card-logo { width: 80px; height: 50px; display: flex; align-items: center; justify-content: center; }
.company-card-logo img { max-width: 80px; max-height: 50px; width: auto; height: auto; object-fit: contain; }
