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

  :root {
    --ink: #1a1a18;
    --paper: #f5f2ec;
    --cream: #ede9e0;
    --rule: #c8c0b0;
    --accent: #2d5a3d;
    --accent-light: #e8f0ea;
    --warn: #8b3a2a;
    --warn-light: #f5ebe8;
    --gold: #b8860b;
    --gold-light: #fdf6e3;
    --sidebar: #1e2820;
    --sidebar-text: #c8d4ca;
    --sidebar-active: #3d7a52;
    --card: #ffffff;
    --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 4px 16px rgba(0,0,0,0.06);
  }

  * { box-sizing: border-box; margin: 0; padding: 0; }

  body {
    font-family: 'DM Sans', sans-serif;
    background: var(--paper);
    color: var(--ink);
    min-height: 100vh;
    font-size: 14px;
  }

  #app-body {
    display: flex;
    flex-direction: row;
    width: 100%;
    min-height: 100vh;
  }

  /* ── SIDEBAR ─────────────────────────────── */
  #sidebar {
    width: 220px;
    min-height: 100vh;
    background: var(--sidebar);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; left: 0;
    z-index: 100;
  }

  .sidebar-brand {
    padding: 28px 20px 20px;
    border-bottom: 1px solid rgba(255,255,255,0.07);
  }

  .sidebar-brand h1 {
    font-family: 'DM Serif Display', serif;
    font-size: 18px;
    color: #ffffff;
    line-height: 1.2;
  }

  .sidebar-brand p {
    font-size: 10px;
    color: var(--sidebar-text);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    margin-top: 4px;
  }

  .sidebar-nav {
    padding: 16px 0;
    flex: 1;
  }

  .nav-section {
    padding: 0 12px;
    margin-bottom: 4px;
  }

  .nav-label {
    font-size: 9px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: rgba(200,212,202,0.4);
    padding: 8px 8px 4px;
    display: block;
  }

  .nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 10px;
    border-radius: 6px;
    cursor: pointer;
    color: var(--sidebar-text);
    font-size: 13px;
    font-weight: 400;
    transition: all 0.15s;
    margin-bottom: 1px;
    text-decoration: none;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
  }

  .nav-item:hover { background: rgba(255,255,255,0.07); color: #fff; }

  .nav-item.active {
    background: var(--sidebar-active);
    color: #fff;
    font-weight: 500;
  }

  .nav-item .icon { font-size: 15px; width: 18px; text-align: center; flex-shrink: 0; }

  /* ── MAIN ─────────────────────────────── */
  #main {
    margin-left: 220px;
    width: calc(100% - 220px);
    min-height: 100vh;
    overflow-x: hidden;
  }

  .topbar {
    background: var(--card);
    border-bottom: 1px solid var(--rule);
    padding: 0 32px;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 50;
  }

  .topbar h2 {
    font-family: 'DM Serif Display', serif;
    font-size: 20px;
    font-weight: 400;
  }

  .topbar-actions { display: flex; gap: 10px; align-items: center; }

  .content { padding: 28px 32px; }

  /* ── BUTTONS ─────────────────────────────── */
  .btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    font-family: 'DM Sans', sans-serif;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.15s;
    white-space: nowrap;
  }

  .btn-primary { background: var(--accent); color: #fff; }
  .btn-primary:hover { background: #235030; }
  .btn-secondary { background: var(--cream); color: var(--ink); border: 1px solid var(--rule); }
  .btn-secondary:hover { background: var(--rule); }
  .btn-danger { background: var(--warn-light); color: var(--warn); border: 1px solid #e8c4bc; }
  .btn-danger:hover { background: #f0d0ca; }
  .btn-gold { background: var(--gold-light); color: var(--gold); border: 1px solid #e0cc90; }
  .btn-gold:hover { background: #f8edc0; }
  .btn-sm { padding: 5px 10px; font-size: 12px; }

  /* ── CARDS ─────────────────────────────── */
  .card {
    background: var(--card);
    border: 1px solid var(--rule);
    border-radius: 10px;
    box-shadow: var(--shadow);
  }

  .card-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--cream);
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .card-header h3 {
    font-family: 'DM Serif Display', serif;
    font-size: 15px;
    font-weight: 400;
  }

  /* ── STATS GRID ─────────────────────────── */
  .stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px; }

  .stat-card {
    background: var(--card);
    border: 1px solid var(--rule);
    border-radius: 10px;
    padding: 18px 20px;
    box-shadow: var(--shadow);
  }

  .stat-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: #888; margin-bottom: 8px; }

  .stat-value {
    font-family: 'DM Mono', monospace;
    font-size: 24px;
    font-weight: 500;
    color: var(--ink);
  }

  .stat-value.green { color: var(--accent); }
  .stat-value.red { color: var(--warn); }
  .stat-value.gold { color: var(--gold); }

  /* ── TABLES ─────────────────────────────── */
  .table-wrap { overflow-x: auto; }

  table { width: 100%; border-collapse: collapse; font-size: 13px; }

  thead th {
    text-align: left;
    padding: 10px 14px;
    font-size: 10px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: #999;
    border-bottom: 1px solid var(--cream);
    background: #fafaf8;
  }

  tbody tr { border-bottom: 1px solid #f0ede7; }
  tbody tr:last-child { border-bottom: none; }

  tbody td { padding: 10px 14px; }

  tbody tr:hover { background: #fafaf8; }

  .mono { font-family: 'DM Mono', monospace; font-size: 12px; }

  /* ── FORMS ─────────────────────────────── */
  .form-grid { display: grid; gap: 16px; }
  .form-grid-2 { grid-template-columns: 1fr 1fr; }
  .form-grid-3 { grid-template-columns: 1fr 1fr 1fr; }

  .form-group { display: flex; flex-direction: column; gap: 5px; }

  label { font-size: 12px; font-weight: 500; color: #555; }

  input, select, textarea {
    padding: 8px 11px;
    border: 1px solid var(--rule);
    border-radius: 6px;
    font-family: 'DM Sans', sans-serif;
    font-size: 13px;
    background: var(--card);
    color: var(--ink);
    transition: border-color 0.15s;
    width: 100%;
  }

  input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(45,90,61,0.1);
  }

  textarea { resize: vertical; min-height: 70px; }

  /* ── MODAL ─────────────────────────────── */
  .modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.45);
    display: flex; align-items: center; justify-content: center;
    z-index: 200;
    backdrop-filter: blur(2px);
  }

  .modal {
    background: var(--card);
    border-radius: 12px;
    padding: 28px;
    width: 560px;
    max-width: 95vw;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  }

  .modal-invoice-shell {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 0;
  }

  .modal-invoice-header {
    flex-shrink: 0;
    padding: 28px 28px 0;
  }

  .modal-invoice-content {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 0 28px;
  }

  .modal h2 {
    font-family: 'DM Serif Display', serif;
    font-size: 20px;
    font-weight: 400;
    margin-bottom: 20px;
  }

  .modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--cream); }

  /* ── BADGES ─────────────────────────────── */
  .badge {
    display: inline-block;
    padding: 3px 8px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 500;
  }

  .badge-green { background: var(--accent-light); color: var(--accent); }
  .badge-red { background: var(--warn-light); color: var(--warn); }
  .badge-gold { background: var(--gold-light); color: var(--gold); }
  .badge-gray { background: var(--cream); color: #888; }
  .badge-blue { background: #e8f0f8; color: #2a5080; }

  /* ── PAGE VIEWS ─────────────────────────── */
  .view { display: none; }
  .view.active { display: block; }

  .section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  .section-header h3 {
    font-family: 'DM Serif Display', serif;
    font-size: 18px;
    font-weight: 400;
  }

  /* ── PROPERTY CARDS ─────────────────────── */
  .property-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 16px; }

  .property-card {
    background: var(--card);
    border: 1px solid var(--rule);
    border-radius: 10px;
    padding: 20px;
    box-shadow: var(--shadow);
    transition: box-shadow 0.15s;
    cursor: pointer;
  }

  .property-card:hover { box-shadow: 0 4px 20px rgba(0,0,0,0.1); }

  .property-card-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 12px; }

  .property-card-name {
    font-family: 'DM Serif Display', serif;
    font-size: 17px;
    font-weight: 400;
  }

  .property-card-owner { font-size: 12px; color: #888; margin-top: 2px; }
  .property-card-address { font-size: 12px; color: #666; margin-top: 6px; }

  .property-card-fee { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--cream); font-size: 12px; }

  /* ── SUMMARY SECTION ─────────────────────── */
  .summary-controls { display: flex; gap: 12px; align-items: center; margin-bottom: 20px; }

  .summary-breakdown {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 20px;
  }

  .breakdown-item { padding: 14px 18px; background: var(--cream); border-radius: 8px; }
  .breakdown-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: #888; margin-bottom: 4px; }
  .breakdown-value { font-family: 'DM Mono', monospace; font-size: 18px; font-weight: 500; }

  .fee-box {
    background: var(--accent);
    color: white;
    border-radius: 8px;
    padding: 16px 20px;
    margin-bottom: 20px;
  }

  .fee-box h4 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; opacity: 0.7; margin-bottom: 6px; }
  .fee-box .fee-amount { font-family: 'DM Mono', monospace; font-size: 28px; font-weight: 500; }
  .fee-box .fee-detail { font-size: 12px; opacity: 0.75; margin-top: 4px; }

  /* ── EMPTY STATE ─────────────────────────── */
  .empty-state { text-align: center; padding: 60px 20px; color: #aaa; }
  .empty-state .empty-icon { font-size: 48px; margin-bottom: 16px; }
  .empty-state p { font-size: 14px; }

  /* ── INVOICE PREVIEW ─────────────────────── */
  .invoice-preview {
    background: white;
    border: 1px solid var(--rule);
    border-radius: 8px;
    padding: 40px;
    font-family: 'DM Sans', sans-serif;
    max-width: 700px;
    margin: 0 auto;
  }

  .inv-header { display: flex; justify-content: space-between; margin-bottom: 36px; }
  .inv-company { font-size: 12px; line-height: 1.6; color: #555; }
  .inv-company strong { font-size: 16px; font-family: 'DM Serif Display', serif; color: var(--ink); display: block; margin-bottom: 4px; }
  .inv-meta { text-align: right; }
  .inv-meta h1 { font-family: 'DM Serif Display', serif; font-size: 28px; font-weight: 400; color: var(--accent); margin-bottom: 8px; }
  .inv-meta p { font-size: 12px; color: #666; line-height: 1.6; }
  .inv-meta .inv-num { font-family: 'DM Mono', monospace; font-size: 14px; font-weight: 500; }

  .inv-divider { border: none; border-top: 2px solid var(--cream); margin: 20px 0; }

  .inv-section-title { font-size: 10px; text-transform: uppercase; letter-spacing: 0.1em; color: #999; margin-bottom: 10px; margin-top: 20px; }

  .inv-client { font-size: 13px; line-height: 1.7; color: #444; margin-bottom: 8px; }
  .inv-client strong { color: var(--ink); }

  .inv-table { width: 100%; border-collapse: collapse; font-size: 12px; margin-bottom: 12px; }
  .inv-table th { text-align: left; padding: 6px 10px; font-size: 10px; text-transform: uppercase; letter-spacing: 0.05em; color: #aaa; border-bottom: 1px solid #f0ede7; }
  .inv-table td { padding: 7px 10px; border-bottom: 1px solid #f8f6f2; }
  .inv-table tr:last-child td { border-bottom: none; }
  .inv-table .right { text-align: right; }
  .inv-table .mono { font-family: 'DM Mono', monospace; }

  .inv-subtotal { display: flex; justify-content: space-between; padding: 8px 10px; background: #f8f6f2; border-radius: 4px; font-size: 12px; font-weight: 600; }
  .inv-subtotal-muted {
    background: transparent;
    border-top: 1px solid #ece6db;
    border-bottom: 1px solid #ece6db;
    border-radius: 0;
    padding: 10px 2px;
    color: #5f5a52;
  }

  .inv-summary-box {
    margin-top: 16px;
    padding: 14px 16px;
    background: #f5f3ee;
    border: 1px solid #e7e0d3;
    border-radius: 6px;
    font-size: 12px;
  }

  .inv-summary-row {
    display: flex;
    justify-content: space-between;
    color: #7e776d;
  }

  .inv-summary-row + .inv-summary-row {
    margin-top: 6px;
    padding-top: 6px;
    border-top: 1px solid #e6dfd2;
  }

  .inv-total-box {
    background: linear-gradient(180deg, #2d5a3d 0%, #234834 100%);
    color: white;
    border-radius: 4px;
    padding: 18px 22px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 18px;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.08);
  }

  .inv-total-box .label {
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    opacity: 0.95;
  }
  .inv-total-box .amount { font-family: 'DM Mono', monospace; font-size: 26px; font-weight: 500; }

  .modal-preview-actions {
    flex-shrink: 0;
    margin-top: 0;
    padding: 16px 28px 20px;
    border-top: 1px solid var(--cream);
    background: linear-gradient(180deg, rgba(255,255,255,0.96) 0%, rgba(245,242,236,1) 100%);
  }

  .inv-save-btn {
    min-width: 190px;
    justify-content: center;
    padding: 12px 18px;
    font-size: 14px;
    box-shadow: 0 8px 18px rgba(35, 72, 52, 0.18);
  }

  .inv-save-btn[disabled] {
    cursor: not-allowed;
    opacity: 0.65;
    box-shadow: none;
  }

  .inv-footer { margin-top: 32px; font-size: 11px; color: #aaa; text-align: center; border-top: 1px solid #f0ede7; padding-top: 16px; }

  /* ── MOBILE NAV TOGGLE ───────────────────── */
  #mobile-topbar {
    display: none;
    background: var(--sidebar);
    height: 52px;
    align-items: center;
    gap: 12px;
    padding: 0 16px;
    position: sticky;
    top: 0;
    z-index: 150;
  }

  #mobile-topbar .mobile-brand {
    font-family: 'DM Serif Display', serif;
    font-size: 16px;
    color: #fff;
  }

  #hamburger {
    background: none;
    border: none;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 5px;
    padding: 4px;
  }

  #hamburger span {
    display: block;
    width: 22px;
    height: 2px;
    background: #fff;
    border-radius: 2px;
    transition: all 0.2s;
  }

  #sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 98;
  }

  /* ── RESPONSIVE BREAKPOINTS ─────────────── */
  @media (max-width: 768px) {

    /* Stack app vertically on mobile */
    #app-body { flex-direction: column; }

    /* Show mobile topbar, hide sidebar by default */
    #mobile-topbar { display: flex; width: 100%; flex-shrink: 0; }

    /* Push sidebar content below the fixed mobile topbar */
    #sidebar { padding-top: 52px; }

    #sidebar {
      transform: translateX(-100%);
      transition: transform 0.25s ease;
      z-index: 99;
    }

    #sidebar.mobile-open { transform: translateX(0); }
    #sidebar-overlay.visible { display: block; }

    /* Main takes full width, stacks below mobile topbar */
    #main { margin-left: 0; width: 100%; min-height: calc(100vh - 52px); }

    /* On mobile, hide the topbar title (shown in mobile topbar instead) */
    .topbar h2 { display: none; }
    .topbar {
      position: relative; /* don't sticky on mobile — mobile topbar handles global nav */
      top: auto;
      display: flex;
      height: auto;
      min-height: 0;
      padding: 8px 16px;
      justify-content: flex-end;
    }
    /* Hide topbar when it has no action buttons */
    .topbar:not(:has(.topbar-actions > *)) { display: none; }
    .topbar-actions { flex-wrap: wrap; gap: 8px; }

    /* Touch targets: minimum 44px for comfortable tapping */
    .btn { min-height: 44px; }
    .btn-sm { min-height: 36px; padding: 4px 8px; font-size: 11px; }
    .nav-item { min-height: 44px; padding: 10px; }

    /* iOS Safari: prevent auto-zoom on input focus */
    input, select, textarea { font-size: 16px; }

    /* Tighten content padding */
    .content { padding: 16px; }

    /* Stats: 2 cols on mobile */
    .stats-grid { grid-template-columns: 1fr 1fr; gap: 10px; }

    .stat-value { font-size: 18px; }

    /* Dashboard 2-col → 1-col */
    #view-dashboard .content > div[style*="grid-template-columns:1fr 1fr"] {
      grid-template-columns: 1fr !important;
    }

    /* Property grid: single col */
    .property-grid { grid-template-columns: 1fr; }

    /* Form grids: collapse to 1 col */
    .form-grid-2, .form-grid-3 { grid-template-columns: 1fr; }

    /* Summary controls: stack vertically */
    .summary-controls { flex-direction: column; align-items: stretch; }
    .summary-controls .form-group { flex-direction: column !important; }
    .summary-controls select { width: 100% !important; }
    .summary-controls .btn { width: 100%; justify-content: center; }

    /* Summary breakdown: 2 col on mobile is fine, but tighten */
    .summary-breakdown { grid-template-columns: 1fr 1fr; gap: 10px; }
    .breakdown-value { font-size: 15px; }

    /* Invoice detail summary stats: 2 col */
    #inv-detail-content > div[style*="grid-template-columns:repeat(3"] {
      grid-template-columns: 1fr 1fr !important;
    }

    /* 2-col expense/revenue panels → 1 col */
    #summary-content > div[style*="grid-template-columns:1fr 1fr"],
    #inv-detail-content > div[style*="grid-template-columns:1fr 1fr"] {
      grid-template-columns: 1fr !important;
    }

    /* Modals: full screen bottom sheet */
    .modal-overlay { align-items: flex-end; }

    .modal {
      width: 100% !important;
      max-width: 100% !important;
      border-radius: 16px 16px 0 0;
      max-height: 90vh;
      padding: 20px 16px;
    }

    .modal-invoice-shell {
      padding: 0 !important;
    }

    .modal-invoice-header {
      padding: 20px 16px 0;
    }

    .modal-invoice-content {
      padding: 0 16px;
    }

    .modal-preview-actions {
      padding: 14px 16px 18px;
    }

    /* Modal header rows: wrap buttons */
    .modal > div[style*="display:flex"] {
      flex-wrap: wrap;
      gap: 8px;
    }
    /* Inner button groups in modal headers */
    .modal > div[style*="display:flex"] > div[style*="display:flex"] {
      flex-wrap: wrap;
      gap: 8px;
    }

    /* Invoice preview: less padding */
    .invoice-preview { padding: 20px 16px; }
    .inv-header { flex-direction: column; gap: 16px; }
    .inv-meta { text-align: left; }

    /* Filter row: wrap */
    .filter-row { flex-wrap: wrap; gap: 8px; }
    .filter-row select.filter-sel { flex: 1; min-width: 120px; }

    /* Topbar actions on invoice detail modal */
    #modal-inv-detail .modal > div:first-child {
      flex-wrap: wrap;
    }

    /* Toast: bottom center */
    #toast { right: 16px; left: 16px; bottom: 16px; text-align: center; }

    /* Table: allow horizontal scroll, reduce font */
    table { font-size: 12px; }
    tbody td { padding: 8px 10px; }
    thead th { padding: 8px 10px; }

    /* Fee box: smaller font */
    .fee-box .fee-amount { font-size: 22px; }

    /* Generate invoice card padding */
    #view-invoices .card > div[style*="padding:16px 20px"] {
      padding: 12px !important;
    }
  }

  @media (max-width: 480px) {
    /* Stats: 1 col on very small screens */
    .stats-grid { grid-template-columns: 1fr 1fr; }

    /* Summary breakdown: stack */
    .summary-breakdown { grid-template-columns: 1fr; }

    /* Hide less-important table columns via a helper class */
    .hide-xs { display: none !important; }
  }

  #toast {
    position: fixed;
    bottom: 28px; right: 28px;
    background: var(--ink);
    color: white;
    padding: 12px 20px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    z-index: 9999;
    transition: all 0.3s;
    transform: translateY(0);
    opacity: 0;
    pointer-events: none;
  }

  #toast.show { opacity: 1; }

  /* ── MISC ─────────────────────────────── */
  .paid-tag { opacity: 0.5; text-decoration: line-through; }
  .pill-row { display: flex; gap: 8px; flex-wrap: wrap; }

  .filter-row { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }
  .filter-row label { font-size: 12px; color: #888; white-space: nowrap; }

  select.filter-sel { width: auto; padding: 6px 10px; font-size: 12px; }

  .alert-box {
    background: var(--gold-light);
    border: 1px solid #e0cc90;
    border-radius: 8px;
    padding: 12px 16px;
    font-size: 13px;
    color: #7a5a00;
    margin-bottom: 16px;
  }

  hr.divider { border: none; border-top: 1px solid var(--cream); margin: 20px 0; }

  /* ── AUTH GATE ───────────────────────────── */
