:root {
  --bg: #f7f8fa; --card: #ffffff; --primary: #1a2744; --accent: #3b82f6;
  --accent2: #10b981; --text: #1e293b; --muted: #64748b; --border: #e2e8f0;
  --red: #ef4444; --orange: #f59e0b;
}
* { margin:0; padding:0; box-sizing:border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: var(--bg); color: var(--text); overflow-x: hidden; }

/* Login screen */
.login-screen {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: var(--primary);
}
.login-card {
  background: var(--card); border-radius: 14px; padding: 36px; width: 360px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.login-card h2 { font-size: 20px; font-weight: 700; color: var(--primary); }
.login-card input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 6px; font-size: 14px; margin-top: 4px;
}
.login-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(59,130,246,.1); }

/* User badge */
.user-badge {
  background: #334155; color: #e2e8f0; padding: 4px 12px; border-radius: 16px;
  font-size: 12px; cursor: pointer; font-weight: 500;
}
.user-badge:hover { background: #475569; }

/* Header */
.header { background: var(--primary); color: #fff; padding: 20px 24px; }
.header h1 { font-size: 20px; font-weight: 600; }
.header p { color: #94a3b8; font-size: 12px; margin-top: 4px; }
.header a { color: #94a3b8; text-decoration: underline; }

/* KPIs */
.kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; padding: 16px 24px; }
.kpi { background: var(--card); border-radius: 10px; padding: 16px; box-shadow: 0 1px 3px rgba(0,0,0,.06); }
.kpi .label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 4px; }
.kpi .sub { font-size: 11px; color: var(--muted); margin-top: 2px; }

/* Global filter */
.global-filter { background: var(--card); margin: 0 24px; padding: 12px 16px; border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,.06); display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.global-filter label { font-size: 11px; color: var(--muted); font-weight: 500; }
.global-filter select, .global-filter input { padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; background: #fff; }
.active-filters { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 24px 0; min-height: 4px; }
.filter-tag { display: inline-flex; align-items: center; gap: 4px; background: #eef2ff; color: var(--accent); padding: 3px 10px; border-radius: 12px; font-size: 11px; font-weight: 500; }
.filter-tag .remove { cursor: pointer; font-size: 14px; margin-left: 2px; opacity: .7; }
.filter-tag .remove:hover { opacity: 1; }
.clear-all { font-size: 11px; color: var(--red); cursor: pointer; padding: 3px 10px; }
.clear-all:hover { text-decoration: underline; }

/* Main grid */
.main { padding: 16px 24px 24px; display: grid; grid-template-columns: 1fr 1fr; gap: 16px; max-width: 100%; }
.card { background: var(--card); border-radius: 10px; padding: 16px; box-shadow: 0 1px 3px rgba(0,0,0,.06); overflow: hidden; min-width: 0; }
.card.full { grid-column: 1 / -1; }
.card h2 { font-size: 14px; font-weight: 600; margin-bottom: 12px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; }
.card h2 .badge { font-size: 10px; background: #eef2ff; color: var(--accent); padding: 2px 7px; border-radius: 5px; font-weight: 500; }
.chart-container { position: relative; width: 100%; }
.chart-container.tall { height: 320px; }
.chart-container.short { height: 260px; }
.chart-container.med { height: 300px; }

/* Toggles & buttons */
.toggle-group { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; font-size: 11px; }
.toggle-btn { padding: 4px 10px; cursor: pointer; background: #fff; border: none; color: var(--muted); }
.toggle-btn.active { background: var(--accent); color: #fff; }
.btn { padding: 5px 12px; border-radius: 6px; border: 1px solid var(--border); background: #fff; cursor: pointer; font-size: 11px; transition: all .15s; }
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: #2563eb; }
.btn-icon { padding: 3px 6px; font-size: 13px; line-height: 1; border: none; background: transparent; cursor: pointer; border-radius: 4px; }
.btn-icon:hover { background: #f1f5f9; }
.btn-icon.del:hover { background: #fef2f2; color: var(--red); }

/* Table */
table { width: 100%; border-collapse: collapse; font-size: 12px; }
th { text-align: left; padding: 6px 10px; border-bottom: 2px solid var(--border); color: var(--muted); font-weight: 500; font-size: 10px; text-transform: uppercase; letter-spacing: .3px; }
td { padding: 6px 10px; border-bottom: 1px solid var(--border); }
tr:hover { background: #f8fafc; }
tr.clickable { cursor: pointer; }
.amount { text-align: right; font-variant-numeric: tabular-nums; font-weight: 500; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--accent); }
th.sortable::after { content: ' ↕'; font-size: 9px; }

/* Source tags */
.source-tag { display: inline-block; font-size: 9px; padding: 1px 5px; border-radius: 3px; font-weight: 600; }
.source-tag.invoice { background: #dbeafe; color: #1d4ed8; }
.source-tag.mastercard { background: #fef3c7; color: #b45309; }

/* VAT quarterly table */
.quarter-table { width: 100%; }
.quarter-table th, .quarter-table td { padding: 8px 10px; font-size: 12px; }
.quarter-table th { background: #f8fafc; }
.quarter-table .total-row { font-weight: 700; background: #f0f4ff; }
.quarter-table .status { font-size: 10px; padding: 2px 6px; border-radius: 4px; }
.quarter-table .status.reimbursed { background: #d1fae5; color: #065f46; }
.quarter-table .status.final { background: #dbeafe; color: #1e40af; }
.quarter-table .status.ongoing { background: #fef3c7; color: #92400e; }
.quarter-table tr.clickable:hover { background: #f0f4ff; }

/* Modal */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 100; align-items: center; justify-content: center; }
.modal-overlay.active { display: flex; }
.modal { background: var(--card); border-radius: 14px; padding: 24px; max-width: 700px; width: 92%; max-height: 85vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.2); }
.modal h3 { font-size: 16px; margin-bottom: 12px; }
.modal .close-btn { float: right; background: none; border: none; font-size: 20px; cursor: pointer; color: var(--muted); }
.modal .close-btn:hover { color: var(--text); }

/* Form */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-group.full { grid-column: 1 / -1; }
.form-group label { font-size: 11px; color: var(--muted); font-weight: 500; }
.form-group input, .form-group select { padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; background: #fff; }

/* Bulk realloc */
.realloc-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.realloc-row .inv-info { flex: 1; min-width: 0; }
.realloc-row .inv-vendor { font-weight: 500; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.realloc-row .inv-desc { font-size: 11px; color: var(--muted); }
.realloc-row .inv-amount { font-weight: 600; min-width: 70px; text-align: right; font-size: 13px; }
.realloc-row select { padding: 3px 6px; border: 1px solid var(--border); border-radius: 5px; font-size: 11px; background: #fff; cursor: pointer; max-width: 170px; }
.realloc-row select.changed { border-color: var(--accent); background: #eef2ff; }

/* KPI interactive controls */
.kpi-range { display: flex; gap: 4px; margin-top: 6px; align-items: center; }
.kpi-range select { padding: 2px 4px; border: 1px solid var(--border); border-radius: 4px; font-size: 10px; background: #fff; cursor: pointer; }
.kpi-range label { font-size: 10px; color: var(--muted); }

/* CAC calculator card */
.cac-card { border-top: 3px solid var(--orange); }
.cac-form { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; margin-top: 12px; }
.cac-form .form-group { flex: 1; min-width: 120px; }
.cac-form .form-group label { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; }
.cac-form .form-group input, .cac-form .form-group select { padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; width: 100%; }
.cac-result { margin-top: 16px; padding: 16px; background: #fffbeb; border-radius: 8px; border: 1px solid #fde68a; }
.cac-result .cac-value { font-size: 28px; font-weight: 700; color: var(--orange); }
.cac-result .cac-detail { font-size: 12px; color: var(--muted); margin-top: 4px; }
.cac-hidden { display: none; }

/* Toast */
.toast { position: fixed; bottom: 20px; right: 20px; background: var(--primary); color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 12px; z-index: 200; display: none; box-shadow: 0 4px 12px rgba(0,0,0,.15); }
.toast.show { display: block; animation: slideIn .3s ease; }
@keyframes slideIn { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* ── Tablet ───────────────────────────────────────────────────── */
@media (max-width: 900px) {
  .main { grid-template-columns: 1fr; }
  .kpi-row { grid-template-columns: repeat(2, 1fr); }
  .form-grid { grid-template-columns: 1fr; }
}

/* ── Smartphone ──────────────────────────────────────────────── */
@media (max-width: 600px) {
  /* Prevent any horizontal overflow at the root */
  html, body { overflow-x: hidden; }
  #appScreen { overflow-x: hidden; max-width: 100vw; width: 100%; }

  /* Header — stack vertically */
  .header { padding: 14px 16px; }
  .header h1 { font-size: 16px; }
  .header p { font-size: 10px; line-height: 1.4; }
  .header > div { flex-direction: column !important; align-items: flex-start !important; gap: 10px; }
  .header > div > div:last-child { width: 100%; display: flex; justify-content: flex-start; gap: 8px; flex-wrap: wrap; }

  /* KPIs — SINGLE column, no exceptions */
  .kpi-row {
    grid-template-columns: 1fr !important;
    gap: 8px;
    padding: 12px 12px;
    max-width: 100%;
    overflow: hidden;
  }
  .kpi { padding: 12px; overflow: hidden; max-width: 100%; box-sizing: border-box; }
  .kpi .value { font-size: 20px; word-break: break-word; }
  .kpi .label { font-size: 10px; }
  .kpi .sub { font-size: 10px; word-break: break-word; }
  .kpi-range { flex-wrap: wrap; gap: 6px; width: 100%; }
  .kpi-range select { flex: 1; min-width: 0; max-width: none; font-size: 12px; padding: 6px 8px; }
  .kpi-range label { white-space: nowrap; }
  .kpi-range span { white-space: nowrap; }

  /* Filter bar — stack vertically */
  .global-filter { margin: 0 12px; padding: 10px 12px; gap: 8px; flex-direction: column; align-items: stretch; }
  .global-filter > label { display: none; }
  .global-filter select, .global-filter input { width: 100%; padding: 8px 10px; font-size: 13px; }
  .global-filter .toggle-group { margin-left: 0; align-self: flex-start; }
  .active-filters { margin: 6px 12px 0; }

  /* Main grid — single column, tighter padding */
  .main { padding: 12px 12px 20px; gap: 12px; grid-template-columns: 1fr !important; max-width: 100%; overflow: hidden; }
  .card { padding: 12px; min-width: 0; max-width: 100%; box-sizing: border-box; overflow: visible; }
  .card h2 { font-size: 13px; }

  /* Charts — shorter on mobile */
  .chart-container.tall { height: 240px; }
  .chart-container.short { height: 200px; }
  .chart-container.med { height: 240px; }

  /* ── Invoice table: hide low-value columns on mobile ── */
  .card.full { padding: 12px 0; overflow: visible; }
  .card.full h2 { padding: 0 12px; }
  .card.full > div { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table:not(.quarter-table) { font-size: 11px; width: 100%; min-width: 0; }
  table:not(.quarter-table) th,
  table:not(.quarter-table) td { padding: 6px 6px; }
  /* Hide: col 3=Description, 6=VAT amt, 7=Incl.VAT, 8=Source */
  table:not(.quarter-table) th:nth-child(3),
  table:not(.quarter-table) td:nth-child(3),
  table:not(.quarter-table) th:nth-child(6),
  table:not(.quarter-table) td:nth-child(6),
  table:not(.quarter-table) th:nth-child(7),
  table:not(.quarter-table) td:nth-child(7),
  table:not(.quarter-table) th:nth-child(8),
  table:not(.quarter-table) td:nth-child(8) { display: none; }
  /* Remaining visible: Period, Vendor, Category, Excl.VAT, Actions */
  table:not(.quarter-table) th:nth-child(1),
  table:not(.quarter-table) td:nth-child(1) { max-width: 62px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 10px; }
  table:not(.quarter-table) th:nth-child(2),
  table:not(.quarter-table) td:nth-child(2) { max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  table:not(.quarter-table) th:nth-child(4),
  table:not(.quarter-table) td:nth-child(4) { max-width: 70px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 10px; }
  table:not(.quarter-table) th:nth-child(5),
  table:not(.quarter-table) td:nth-child(5) { white-space: nowrap; font-size: 11px; }

  /* ── Quarter/VAT tables — scrollable wrapper ── */
  #vatQuarterly { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .quarter-table { font-size: 11px; min-width: 480px; }
  .quarter-table th, .quarter-table td { padding: 6px 8px; font-size: 10px; white-space: nowrap; }

  /* CAC form — stack */
  .cac-form { flex-direction: column; }
  .cac-form .form-group { min-width: 100%; }
  .cac-result .cac-value { font-size: 22px; }

  /* Modals — full width on mobile */
  .modal { width: 96%; max-width: none; padding: 18px; border-radius: 10px; max-height: 90vh; }
  .modal h3 { font-size: 15px; }

  /* Realloc rows — stack on mobile */
  .realloc-row { flex-wrap: wrap; gap: 6px; }
  .realloc-row select { max-width: none; width: 100%; }
  .realloc-row .inv-amount { min-width: auto; }

  /* Login card */
  .login-card { width: 90%; max-width: 360px; padding: 24px; }

  /* Toast — centered at bottom */
  .toast { left: 12px; right: 12px; bottom: 12px; text-align: center; }

  /* Buttons — larger touch targets */
  .btn { padding: 8px 14px; font-size: 12px; }
  .btn-icon { padding: 6px 10px; font-size: 16px; }
  .toggle-btn { padding: 6px 12px; }
}
