:root {
    --bg: #f0f4f8;
    --bg-accent: #e8eef6;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --surface-3: #f1f5f9;
    --border: #e2e8f0;
    --border-focus: #6366f1;
    --text: #0f172a;
    --text-muted: #64748b;
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --primary-soft: #eef2ff;
    --green: #059669;
    --green-dark: #047857;
    --green-bg: #ecfdf5;
    --amber: #d97706;
    --amber-bg: #fffbeb;
    --red: #dc2626;
    --red-dark: #b91c1c;
    --red-bg: #fef2f2;
    --purple: #7c3aed;
    --purple-bg: #f5f3ff;
    --row-hover: #f8fafc;
    --input-bg: #ffffff;
    --radius: 16px;
    --radius-sm: 10px;
    --radius-xs: 8px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
    --shadow-lg: 0 20px 40px rgba(15, 23, 42, 0.1);
    --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="dark"] {
    color-scheme: dark;
    --bg: #0b1220;
    --bg-accent: #111827;
    --surface: #1e293b;
    --surface-2: #334155;
    --border: #475569;
    --text: #f1f5f9;
    --text-muted: #94a3b8;
    --primary-soft: #312e81;
    --green-bg: #064e3b;
    --green-dark: #6ee7b7;
    --amber-bg: #78350f;
    --amber: #fcd34d;
    --red-bg: #7f1d1d;
    --red-dark: #fca5a5;
    --row-hover: #334155;
    --input-bg: #0f172a;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.45);
}

[data-theme="dark"] .topbar {
    background: rgba(15, 23, 42, 0.92);
}

[data-theme="dark"] input:focus,
[data-theme="dark"] select:focus,
[data-theme="dark"] textarea:focus,
[data-theme="dark"] .flatpickr-input:focus {
    background: var(--input-bg);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: "Plus Jakarta Sans", "Segoe UI", system-ui, -apple-system, sans-serif;
    background:
        radial-gradient(ellipse 80% 50% at 50% -20%, rgba(99, 102, 241, 0.12), transparent),
        radial-gradient(ellipse 60% 40% at 100% 0%, rgba(16, 185, 129, 0.08), transparent),
        var(--bg);
    color: var(--text);
    line-height: 1.55;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-dark); }

/* ── Topbar ── */
.topbar {
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    padding: 10px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    min-height: 64px;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}

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

.topbar-right {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.topbar .brand {
    font-weight: 800;
    font-size: 17px;
    letter-spacing: -0.02em;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
}

.topbar .brand:hover { color: var(--text); text-decoration: none; }

.topbar .brand:not(:has(.brand-logo))::before {
    content: "📋";
    font-size: 20px;
}

.brand-logo {
    max-height: 38px;
    max-width: 180px;
    object-fit: contain;
    display: block;
}

.global-search {
    position: relative;
    display: flex;
    align-items: center;
    flex: 1 1 280px;
    min-width: 180px;
    max-width: 420px;
    margin: 0 auto;
}

.global-search-icon {
    position: absolute;
    left: 14px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    pointer-events: none;
    display: flex;
}

.global-search input[type=search] {
    width: 100%;
    padding: 10px 44px 10px 42px;
    border: 1px solid var(--border);
    border-radius: 999px;
    font-size: 14px;
    background: var(--input-bg);
    color: var(--text);
    transition: border-color var(--transition), box-shadow var(--transition);
}

.global-search input[type=search]:focus {
    outline: none;
    border-color: var(--border-focus);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.global-search-submit {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    width: 34px;
    height: 34px;
    border: none;
    border-radius: 999px;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background var(--transition), transform var(--transition);
}

.global-search-submit:hover {
    background: var(--primary-dark);
}

.theme-toggle-btn {
    width: 40px;
    height: 40px;
    min-width: 40px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: var(--text);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
}

.theme-toggle-btn:hover {
    color: var(--primary);
    border-color: var(--primary);
    background: var(--primary-soft);
}

.theme-toggle-btn svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    stroke: currentColor;
    fill: none;
    pointer-events: none;
}

.theme-toggle-btn .theme-icon-sun {
    display: none;
}

html[data-theme="dark"] .theme-toggle-btn .theme-icon-moon {
    display: none;
}

html[data-theme="dark"] .theme-toggle-btn .theme-icon-sun {
    display: block;
}

.theme-toggle-btn .theme-icon-moon {
    display: block;
}

.alert-badge {
    display: inline-flex;
    align-items: center;
    padding: 5px 12px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    text-decoration: none;
}

.alert-badge-danger {
    background: var(--red-bg);
    color: var(--red-dark);
    border: 1px solid #fecaca;
}

.alert-badge-warning {
    background: var(--amber-bg);
    color: var(--amber);
    border: 1px solid #fde68a;
}

.topbar nav {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px;
}

.topbar nav a {
    margin: 0;
    padding: 8px 14px;
    border-radius: 999px;
    color: var(--text-muted);
    font-weight: 600;
    font-size: 14px;
    transition: background var(--transition), color var(--transition);
    text-decoration: none;
}

.topbar nav a:hover {
    background: var(--surface-2);
    color: var(--text);
    text-decoration: none;
}

.topbar nav a.active {
    background: var(--primary-soft);
    color: var(--primary-dark);
}

.topbar .user-info {
    font-size: 13px;
    color: var(--text-muted);
    background: var(--surface-2);
    border: 1px solid var(--border);
    padding: 6px 12px;
    border-radius: 999px;
    white-space: nowrap;
}

.container {
    max-width: 1180px;
    margin: 0 auto;
    padding: 32px 24px 56px;
}

.container-narrow {
    max-width: 1080px;
}

/* ── Page header ── */
.page-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 24px;
    flex-wrap: wrap;
}

.page-header h1 { margin: 0 0 4px; }
.page-header .subtitle { margin: 0; }

h1 { font-size: 26px; font-weight: 800; letter-spacing: -0.02em; }
h2 { font-size: 17px; font-weight: 700; margin: 0 0 16px; }
p.subtitle { color: var(--text-muted); font-size: 15px; }

/* ── Cards ── */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: var(--shadow-sm);
}

.card-flat {
    background: transparent;
    border: none;
    box-shadow: none;
    padding: 0;
}

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

.card-header h2 { margin: 0; }

.filter-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px 20px;
    margin-bottom: 20px;
    box-shadow: var(--shadow-sm);
}

/* ── Stats ── */
.grid-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 18px;
    margin-bottom: 24px;
}

.stat {
    position: relative;
    overflow: hidden;
    padding: 22px 22px 22px 26px;
    margin-bottom: 0;
    transition: transform var(--transition), box-shadow var(--transition);
}

.stat:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.stat::before {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 5px;
    border-radius: var(--radius) 0 0 var(--radius);
}

.stat.payable::before { background: var(--red); }
.stat.receivable::before { background: var(--green); }
.stat.pending::before { background: var(--amber); }

.stat .label {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 8px;
    font-weight: 500;
}

.stat .value {
    font-size: 28px;
    font-weight: 800;
    letter-spacing: -0.03em;
}

.stat.payable .value { color: var(--red); }
.stat.receivable .value { color: var(--green); }
.stat.pending .value { color: var(--amber); }

/* ── Tables ── */
.table-wrap {
    overflow-x: auto;
    margin: -4px -4px 0;
    padding: 4px;
}

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

th, td {
    text-align: left;
    padding: 12px 10px;
    border-bottom: 1px solid var(--border);
}

th {
    color: var(--text-muted);
    font-weight: 700;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    background: var(--surface-2);
}

th:first-child { border-radius: var(--radius-sm) 0 0 var(--radius-sm); }
th:last-child { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }

tbody tr { transition: background var(--transition); }
tbody tr:hover td { background: var(--row-hover); }
tbody tr:last-child td { border-bottom: none; }

/* ── Badges ── */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 11px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    white-space: nowrap;
}

.badge-paid { background: var(--green-bg); color: var(--green-dark); }
.badge-pending { background: var(--amber-bg); color: var(--amber); }
.badge-overdue { background: var(--red-bg); color: var(--red-dark); }
.badge-type-payable { background: var(--red-bg); color: var(--red-dark); }
.badge-type-receivable { background: var(--green-bg); color: var(--green-dark); }

/* ── Buttons ── */
.btn,
button.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    padding: 10px 18px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    line-height: 1.2;
    text-decoration: none;
    white-space: nowrap;
    transition: background var(--transition), border-color var(--transition),
                color var(--transition), box-shadow var(--transition), transform var(--transition);
    box-shadow: var(--shadow-sm);
}

.btn:hover,
button.btn:hover {
    text-decoration: none;
}

.btn:not(.btn-primary):not(.btn-success):not(.btn-danger):not(.btn-ai):not(.btn-ghost):hover,
button.btn:not(.btn-primary):not(.btn-success):not(.btn-danger):not(.btn-ai):not(.btn-ghost):hover {
    background: var(--surface-2);
    border-color: #c4cdd8;
}

.btn:active,
button.btn:active {
    transform: translateY(1px);
}

.btn:focus-visible,
button.btn:focus-visible {
    outline: 2px solid var(--border-focus);
    outline-offset: 2px;
}

.btn-primary {
    background: linear-gradient(180deg, #6366f1 0%, var(--primary) 100%);
    color: #fff;
    border-color: var(--primary);
}

.btn-primary:hover {
    background: linear-gradient(180deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: #fff;
    border-color: var(--primary-dark);
    box-shadow: 0 4px 14px rgba(79, 70, 229, 0.35);
}

.btn-success,
button.btn-success {
    background: linear-gradient(180deg, #10b981 0%, var(--green) 100%);
    color: #fff !important;
    border-color: var(--green);
}

.btn-success:hover,
button.btn-success:hover {
    background: linear-gradient(180deg, var(--green) 0%, var(--green-dark) 100%);
    color: #fff !important;
    border-color: var(--green-dark);
    box-shadow: 0 4px 12px rgba(5, 150, 105, 0.3);
}

[data-theme="dark"] .btn-success,
[data-theme="dark"] button.btn-success {
    background: linear-gradient(180deg, #34d399 0%, #059669 100%);
    border-color: #059669;
}

[data-theme="dark"] .btn-success:hover,
[data-theme="dark"] button.btn-success:hover {
    background: linear-gradient(180deg, #059669 0%, #047857 100%);
    border-color: #047857;
}

.btn-danger {
    color: var(--red-dark);
    border-color: #fecaca;
    background: #fff5f5;
}

.btn-danger:hover {
    background: var(--red-bg);
    border-color: #fca5a5;
    color: var(--red-dark);
}

.btn-ghost {
    background: transparent;
    border-color: transparent;
    box-shadow: none;
    color: var(--text-muted);
}

.btn-ghost:hover {
    background: var(--surface-2);
    color: var(--text);
}

.btn-ai {
    background: linear-gradient(135deg, #ede9fe 0%, #dbeafe 100%);
    border-color: #c4b5fd;
    color: #5b21b6;
}

.btn-ai:hover {
    background: linear-gradient(135deg, #ddd6fe 0%, #bfdbfe 100%);
    border-color: #a78bfa;
    box-shadow: 0 4px 14px rgba(124, 58, 237, 0.2);
}

.btn-sm {
    padding: 6px 12px;
    font-size: 12.5px;
    border-radius: 7px;
}

.btn-lg {
    padding: 13px 22px;
    font-size: 15px;
}

.btn-block { width: 100%; }

.btn-group {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
}

.actions-row {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    flex-wrap: wrap;
    align-items: center;
}

.form-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: center;
    padding-top: 8px;
    margin-top: 8px;
    border-top: 1px solid var(--border);
}

.form-actions-danger {
    margin-left: auto;
}

/* ── Forms ── */
.filters {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
    align-items: end;
}

.filters .field { display: flex; flex-direction: column; gap: 6px; min-width: 140px; }
.filters label { font-size: 12px; color: var(--text-muted); font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; }

input[type=text],
input[type=number],
input[type=date],
input[type=month],
input[type=email],
input[type=tel],
input[type=password],
input[type=search],
select,
textarea,
.flatpickr-input {
    width: 100%;
    padding: 12px 14px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    background: var(--input-bg);
    color: var(--text);
    font-family: inherit;
    transition: border-color var(--transition), box-shadow var(--transition), background var(--transition);
}

input::placeholder,
textarea::placeholder {
    color: #94a3b8;
}

input:hover,
select:hover,
textarea:hover,
.flatpickr-input:hover {
    border-color: #b8c4d4;
}

input:focus,
select:focus,
textarea:focus,
.flatpickr-input:focus {
    outline: none;
    border-color: var(--border-focus);
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.12);
    background: #fff;
}

input[type=date],
input[type=month],
.date-input {
    cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='4' width='18' height='18' rx='2' ry='2'/%3E%3Cline x1='16' y1='2' x2='16' y2='6'/%3E%3Cline x1='8' y1='2' x2='8' y2='6'/%3E%3Cline x1='3' y1='10' x2='21' y2='10'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 42px;
}

input[type=date]::-webkit-calendar-picker-indicator,
input[type=month]::-webkit-calendar-picker-indicator {
    opacity: 0;
    cursor: pointer;
    width: 24px;
    height: 24px;
}

select {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 38px;
}

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

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
}

.form-section {
    margin-bottom: 0;
    padding-bottom: 0;
}

.form-section-title {
    display: none;
}

.form-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 22px 22px 8px;
    margin-bottom: 16px;
    box-shadow: var(--shadow-sm);
}

.form-panel-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
    padding-bottom: 14px;
    border-bottom: 1px solid var(--border);
}

.form-panel-icon {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    background: var(--primary-soft);
    color: var(--primary);
}

.form-panel-icon svg {
    width: 20px !important;
    height: 20px !important;
    max-width: 20px !important;
    max-height: 20px !important;
    display: block;
}

.form-panel-icon-green {
    background: var(--green-bg);
    color: var(--green);
}

.form-panel-icon-purple {
    background: var(--purple-bg);
    color: var(--purple);
}

.form-panel-icon-amber {
    background: var(--amber-bg);
    color: var(--amber);
}

.form-panel-title {
    margin: 0;
    font-size: 15px;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.form-panel-subtitle {
    margin: 2px 0 0;
    font-size: 12.5px;
    color: var(--text-muted);
}

form .field {
    margin-bottom: 16px;
}

label {
    display: block;
    font-size: 12.5px;
    font-weight: 600;
    margin-bottom: 7px;
    color: var(--text-muted);
}

.invoice-form-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 320px;
    gap: 20px;
    align-items: start;
}

.invoice-form-aside {
    position: sticky;
    top: 84px;
}

.form-actions-bar {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    align-items: center;
    margin-top: 8px;
    padding: 18px 22px;
    background: rgba(255, 255, 255, 0.88);
    backdrop-filter: blur(12px);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    position: sticky;
    bottom: 16px;
    z-index: 10;
}

[data-theme="dark"] .form-actions-bar {
    background: rgba(30, 41, 59, 0.92);
}

.page-header-modern {
    margin-bottom: 28px;
}

.page-header-modern h1 {
    font-size: 28px;
    font-weight: 800;
    letter-spacing: -0.03em;
}

.page-back {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 10px;
    text-decoration: none;
}

.page-back:hover {
    color: var(--primary);
    text-decoration: none;
}

.checkbox-inline {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
    font-size: 14px;
    cursor: pointer;
}

.checkbox-inline input[type=checkbox] {
    width: 18px;
    height: 18px;
    accent-color: var(--primary);
    cursor: pointer;
}

/* ── File upload ── */
.file-upload {
    position: relative;
    border: 1.5px dashed #cbd5e1;
    border-radius: var(--radius-sm);
    padding: 0;
    background: var(--surface-2);
    transition: border-color var(--transition), background var(--transition), box-shadow var(--transition);
    overflow: hidden;
    cursor: pointer;
}

.file-upload:hover,
.file-upload.is-dragover {
    border-color: var(--primary);
    background: var(--primary-soft);
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}

.file-upload-inner {
    position: relative;
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px;
    text-align: left;
    z-index: 1;
    pointer-events: none;
}

.file-upload-icon {
    width: 44px;
    height: 44px;
    min-width: 44px;
    border-radius: 12px;
    background: var(--surface);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
}

.file-upload-icon svg {
    width: 22px !important;
    height: 22px !important;
    max-width: 22px !important;
    max-height: 22px !important;
    display: block;
    flex-shrink: 0;
}

.file-upload-copy {
    flex: 1;
    min-width: 0;
}

.file-upload-title {
    font-size: 14px;
    font-weight: 700;
    color: var(--text);
    margin: 0 0 2px;
}

.file-upload-text {
    font-size: 12px;
    color: var(--text-muted);
    margin: 0;
    line-height: 1.4;
}

.file-upload-trigger {
    pointer-events: auto;
    margin-top: 10px;
    width: 100%;
}

.file-upload input[type="file"] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
    z-index: 2;
    padding: 0;
    border: none;
    margin: 0;
    font-size: 0;
}

.file-upload-name {
    font-size: 12.5px;
    color: var(--green-dark);
    font-weight: 600;
    margin: 0;
    padding: 0 16px 14px;
    min-height: 0;
    text-align: left;
    word-break: break-all;
}

.file-upload-name:empty {
    display: none;
    padding: 0;
}

.ai-panel {
    margin-top: 14px;
    padding: 14px;
    border-radius: var(--radius-sm);
    background: linear-gradient(135deg, var(--purple-bg) 0%, var(--primary-soft) 100%);
    border: 1px solid #ddd6fe;
}

.ai-panel .btn-ai {
    width: 100%;
}

.ai-panel .help-text {
    margin-top: 10px;
    margin-bottom: 0;
}

.counterparty-hint {
    font-size: 13px;
    margin-top: 8px;
    min-height: 18px;
}

.counterparty-hint-found {
    color: var(--green-dark);
    font-weight: 600;
}

.counterparty-hint-loading {
    color: var(--text-muted);
}

.counterparty-suggestions {
    position: absolute;
    z-index: 20;
    left: 0;
    right: 0;
    top: calc(100% + 6px);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    max-height: 220px;
    overflow-y: auto;
    display: none;
}

.counterparty-suggestions.is-open {
    display: block;
}

.counterparty-suggestion {
    width: 100%;
    border: none;
    background: transparent;
    text-align: left;
    padding: 10px 12px;
    cursor: pointer;
    font: inherit;
    color: var(--text);
    border-bottom: 1px solid var(--border);
}

.counterparty-suggestion:last-child {
    border-bottom: none;
}

.counterparty-suggestion:hover,
.counterparty-suggestion:focus-visible {
    background: var(--row-hover);
    outline: none;
}

.counterparty-suggestion-meta {
    display: block;
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 2px;
}

.field-with-suggestions {
    position: relative;
}

/* ── Flatpickr overrides ── */
.flatpickr-calendar {
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    font-family: inherit;
}

.flatpickr-day.selected,
.flatpickr-day.startRange,
.flatpickr-day.endRange {
    background: var(--primary);
    border-color: var(--primary);
}

.flatpickr-day:hover {
    background: var(--primary-soft);
    border-color: var(--primary-soft);
}

.flatpickr-months .flatpickr-month {
    border-radius: var(--radius) var(--radius) 0 0;
}

.flatpickr-current-month .flatpickr-monthDropdown-months,
.flatpickr-current-month input.cur-year {
    font-weight: 700;
}

/* ── Flash messages ── */
.flash {
    padding: 14px 16px;
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    font-size: 14px;
    font-weight: 500;
    border: 1px solid transparent;
}

.flash-success { background: var(--green-bg); color: var(--green-dark); border-color: #a7f3d0; }
.flash-error { background: var(--red-bg); color: var(--red-dark); border-color: #fecaca; }
.flash-info { background: var(--primary-soft); color: var(--primary-dark); border-color: #bfdbfe; }

/* ── Login ── */
/* ── Seguridad formularios (honeypot anti-bots) ── */
.hp-field {
    position: absolute;
    left: -9999px;
    width: 1px;
    height: 1px;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
}

.cf-turnstile {
    margin: 12px 0 4px;
}

.login-wrap {
    max-width: 420px;
    margin: 72px auto;
    padding: 0 20px;
}

.login-wrap .card {
    padding: 36px 32px;
    box-shadow: var(--shadow-lg);
}

.login-wrap h1 {
    text-align: center;
    font-size: 24px;
}

.login-wrap .subtitle { text-align: center; }

.login-brand {
    text-align: center;
    font-size: 48px;
    margin-bottom: 8px;
}

/* ── Registro / auth compacto ── */
.auth-register .login-wrap {
    max-width: 560px;
    margin: 32px auto 40px;
}

.auth-register .auth-card {
    padding: 28px 28px 24px;
}

.auth-register-header {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    margin-bottom: 20px;
}

.auth-register-header .login-brand {
    font-size: 36px;
    margin: 0;
    line-height: 1;
    flex-shrink: 0;
}

.auth-register-intro h1 {
    text-align: left;
    font-size: 22px;
    margin: 0 0 4px;
    line-height: 1.2;
}

.auth-register-intro .subtitle {
    text-align: left;
    margin: 0 0 10px;
    font-size: 13.5px;
    line-height: 1.45;
}

.auth-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.auth-badge {
    display: inline-flex;
    align-items: center;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    background: var(--primary-soft);
    color: var(--primary-dark);
    border: 1px solid rgba(99, 102, 241, 0.15);
}

[data-theme="dark"] .auth-badge {
    color: #c7d2fe;
}

.auth-form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px 14px;
}

.auth-form-grid .field {
    margin-bottom: 0;
}

.auth-form-grid .field-span-2 {
    grid-column: 1 / -1;
}

.label-optional {
    font-weight: 500;
    color: var(--text-muted);
    font-size: 11px;
}

.phone-input {
    display: flex;
    align-items: stretch;
    width: 100%;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--input-bg);
    overflow: hidden;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.phone-input:hover {
    border-color: #b8c4d4;
}

.phone-input:focus-within {
    border-color: var(--border-focus);
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.12);
}

[data-theme="dark"] .phone-input:focus-within {
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.2);
}

.phone-prefix {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 0 12px;
    background: var(--surface-2);
    border-right: 1.5px solid var(--border);
    font-weight: 700;
    font-size: 14px;
    color: var(--text-muted);
    flex-shrink: 0;
    white-space: nowrap;
}

.phone-prefix svg {
    width: 16px;
    height: 16px;
    opacity: 0.7;
}

.phone-input input[type="tel"] {
    flex: 1;
    min-width: 0;
    border: none;
    border-radius: 0;
    padding: 12px 14px;
    font-size: 15px;
    background: transparent;
    box-shadow: none;
}

.phone-input input[type="tel"]:focus {
    outline: none;
    box-shadow: none;
    background: transparent;
}

.auth-captcha-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.auth-captcha-prompt {
    flex: 1;
    padding: 11px 14px;
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    border: 1.5px solid var(--border);
    font-size: 14px;
    font-weight: 700;
    color: var(--text);
    white-space: nowrap;
}

.auth-captcha-row input[type="number"] {
    width: 88px;
    flex-shrink: 0;
    text-align: center;
    font-size: 16px;
    font-weight: 700;
    padding: 11px 10px;
}

.auth-form-actions {
    grid-column: 1 / -1;
    margin-top: 4px;
}

.auth-form-grid .cf-turnstile {
    grid-column: 1 / -1;
    margin: 0;
}

.auth-form-footer {
    grid-column: 1 / -1;
    text-align: center;
    margin: 8px 0 0;
    font-size: 13px;
}

@media (max-width: 600px) {
    .auth-register .login-wrap {
        margin: 20px auto 32px;
        padding: 0 14px;
    }

    .auth-register .auth-card {
        padding: 22px 18px 20px;
    }

    .auth-form-grid {
        grid-template-columns: 1fr;
    }

    .auth-form-grid .field-span-2 {
        grid-column: auto;
    }

    .auth-captcha-row {
        flex-direction: column;
        align-items: stretch;
    }

    .auth-captcha-row input[type="number"] {
        width: 100%;
    }
}

/* ── Misc ── */
.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--text-muted);
}

.ai-status { font-size: 13px; }
.ai-status-loading { color: var(--text-muted); }
.ai-status-success { color: var(--green-dark); font-weight: 600; }
.ai-status-error { color: var(--red-dark); font-weight: 600; }

.text-muted { color: var(--text-muted); }

.doc-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--primary);
    transition: background var(--transition), border-color var(--transition), color var(--transition);
}

.doc-link:hover {
    background: var(--primary-soft);
    border-color: #93c5fd;
    color: var(--primary-dark);
    text-decoration: none;
}

.doc-icon {
    width: 18px;
    height: 18px;
    display: block;
}

.help-text {
    font-size: 12.5px;
    color: var(--text-muted);
    margin-top: 8px;
    line-height: 1.45;
}

@media (max-width: 768px) {
    .grid-3 { grid-template-columns: 1fr; }
    .form-row { grid-template-columns: 1fr; }
    .invoice-form-grid { grid-template-columns: 1fr; }
    .invoice-form-aside { position: static; }
    .form-actions-bar { position: static; }
    .topbar {
        flex-direction: column;
        align-items: stretch;
        padding: 14px 16px;
        min-height: auto;
    }
    .topbar nav { justify-content: flex-start; }
    .topbar .user-info { align-self: flex-start; }
    .container { padding: 20px 16px 40px; }
    .page-header { flex-direction: column; }
    .form-actions-danger { margin-left: 0; width: 100%; }
    .form-actions-danger .btn { width: 100%; }
    .filters .field { min-width: 100%; flex: 1 1 100%; }
    .global-search { max-width: none; order: 3; width: 100%; }
}

/* ── Charts, timeline, settings ── */
.chart-wrap {
    position: relative;
    min-height: 280px;
}

.card-danger {
    border-color: #fecaca;
    background: #fffafa;
}

[data-theme="dark"] .card-danger {
    background: #450a0a;
    border-color: #991b1b;
}

.text-danger { color: var(--red-dark); }

.alert-banner {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.money-equiv { font-size: 12px; font-weight: 500; }

.timeline {
    list-style: none;
    margin: 0;
    padding: 0;
}

.timeline-item {
    display: flex;
    gap: 14px;
    padding-bottom: 18px;
    position: relative;
}

.timeline-item:not(:last-child)::before {
    content: "";
    position: absolute;
    left: 7px;
    top: 18px;
    bottom: 0;
    width: 2px;
    background: var(--border);
}

.timeline-dot {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--primary);
    border: 3px solid var(--primary-soft);
    flex-shrink: 0;
    margin-top: 2px;
}

.timeline-content { flex: 1; }

.settings-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 20px;
}

.logo-preview {
    margin-top: 12px;
    padding: 16px;
    background: var(--surface-2);
    border-radius: var(--radius-sm);
    text-align: center;
}

.logo-preview img {
    max-height: 60px;
    max-width: 100%;
}

.help-box {
    margin-top: 18px;
    padding: 14px;
    background: var(--surface-2);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    font-size: 13px;
}

.help-box code {
    display: block;
    margin-top: 8px;
    padding: 10px;
    background: var(--input-bg);
    border-radius: 6px;
    font-size: 12px;
    word-break: break-all;
}

input[type=search] {
    width: 100%;
}

.view-tabs {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}

.view-tab {
    padding: 8px 16px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-muted);
    font-size: 13px;
    font-weight: 600;
    text-decoration: none;
    transition: background var(--transition), color var(--transition);
}

.view-tab:hover {
    background: var(--surface-2);
    color: var(--text);
    text-decoration: none;
}

.view-tab.active {
    background: var(--primary-soft);
    border-color: #93c5fd;
    color: var(--primary-dark);
}

.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    flex-wrap: wrap;
    margin: 16px 0;
}

.pagination-info {
    font-size: 13px;
    color: var(--text-muted);
    font-weight: 600;
}

.form-readonly input:not([type=hidden]),
.form-readonly select,
.form-readonly textarea {
    opacity: 0.85;
    pointer-events: none;
}

.cash-box { border-left: 5px solid var(--primary); }
.cash-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    margin: 16px 0;
}
.cash-item {
    padding: 18px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface-2);
}
.cash-label { display: block; font-size: 12px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); margin-bottom: 6px; }
.cash-value { display: block; font-size: 22px; font-weight: 800; }
.cash-meta { display: block; font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.cash-out .cash-value { color: var(--red-dark); }
.cash-in .cash-value { color: var(--green-dark); }
.cash-positive .cash-value { color: var(--green-dark); }
.cash-negative .cash-value { color: var(--red-dark); }
.cash-global {
    font-size: 13px;
    color: var(--text-muted);
    padding-top: 12px;
    border-top: 1px dashed var(--border);
}

.timeline-dot-audit { background: var(--purple, #7c3aed); border-color: var(--purple-bg, #ede9fe); }

.modal[hidden] { display: none !important; }
.modal {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.5);
}
.modal-card {
    position: relative;
    background: var(--surface);
    border-radius: var(--radius);
    padding: 24px;
    max-width: 440px;
    width: 100%;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
}
.modal-card h2 { margin-top: 0; }

.trial-banner {
    background: linear-gradient(90deg, #eef2ff 0%, #ecfdf5 100%);
    border: 1px solid #c7d2fe;
    border-radius: var(--radius-sm);
    padding: 12px 16px;
    margin-bottom: 16px;
    font-size: 13px;
    color: #334155;
}

.trial-banner a {
    font-weight: 700;
}

[data-theme="dark"] .trial-banner {
    background: linear-gradient(90deg, #1e1b4b 0%, #064e3b 100%);
    border-color: #475569;
    color: #e2e8f0;
}

@media (max-width: 768px) {
    .cash-grid { grid-template-columns: 1fr; }
}
