:root {
    --bg: #f0f4f8;
    --sidebar: #1e293b;
    --sidebar-text: #cbd5e1;
    --sidebar-active: #3b82f6;
    --card: #ffffff;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e2e8f0;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --success: #059669;
    --danger: #dc2626;
    --warning: #d97706;
    --radius: 12px;
    --shadow: 0 4px 24px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: 'Segoe UI', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.app { display: flex; min-height: 100vh; }

.sidebar {
    width: 240px;
    background: var(--sidebar);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    padding: 20px 0;
    position: fixed;
    top: 0; left: 0; bottom: 0;
}

.brand {
    font-size: 1.25rem;
    font-weight: 700;
    padding: 0 20px 24px;
    color: #fff;
}

.sidebar nav { flex: 1; display: flex; flex-direction: column; gap: 4px; }

.sidebar nav a {
    color: var(--sidebar-text);
    padding: 10px 20px;
    text-decoration: none;
    border-left: 3px solid transparent;
}

.sidebar nav a:hover,
.sidebar nav a.active {
    background: rgba(255,255,255,.06);
    color: #fff;
    text-decoration: none;
}

.sidebar nav a.active { border-left-color: var(--sidebar-active); }

.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid rgba(255,255,255,.1);
    display: flex;
    justify-content: space-between;
    font-size: .875rem;
}

.content {
    margin-left: 240px;
    flex: 1;
    padding: 28px 32px 80px;
    max-width: 1200px;
}

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    gap: 16px;
    flex-wrap: wrap;
}

.page-header h1 { margin: 0; font-size: 1.75rem; }

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
    margin-bottom: 28px;
}

.stat-card {
    background: var(--card);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
}

.stat-card .label { color: var(--muted); font-size: .875rem; margin-bottom: 8px; }
.stat-card .value { font-size: 1.75rem; font-weight: 700; }
.stat-card.success .value { color: var(--success); }
.stat-card.warning .value { color: var(--warning); }
.stat-card.primary .value { color: var(--primary); }

.card {
    background: var(--card);
    border-radius: var(--radius);
    padding: 24px;
    box-shadow: var(--shadow);
    margin-bottom: 24px;
}

.card h2 { margin: 0 0 16px; font-size: 1.125rem; }

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
}

.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: .875rem; font-weight: 600; color: var(--muted); }
.form-group.full { grid-column: 1 / -1; }

input, select, textarea {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    font: inherit;
    background: #fff;
}

input:focus, select:focus, textarea:focus {
    outline: 2px solid rgba(37, 99, 235, .25);
    border-color: var(--primary);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border: none;
    border-radius: 8px;
    padding: 10px 16px;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); text-decoration: none; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-secondary { background: #e2e8f0; color: var(--text); }
.btn-email { background: #0ea5e9; color: #fff; }
.btn-email:hover { background: #0284c7; text-decoration: none; color: #fff; }
.btn-whatsapp { background: #25d366; color: #fff; }
.btn-whatsapp:hover { background: #1da851; text-decoration: none; color: #fff; }
.btn-sm { padding: 6px 10px; font-size: .8125rem; }

.table-wrap { overflow-x: auto; }

table {
    width: 100%;
    border-collapse: collapse;
    font-size: .9375rem;
}

th, td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

th { color: var(--muted); font-size: .8125rem; text-transform: uppercase; letter-spacing: .03em; }

.badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 999px;
    font-size: .75rem;
    font-weight: 600;
}

.badge-pending { background: #fef3c7; color: #92400e; }
.badge-paid { background: #d1fae5; color: #065f46; }
.badge-partial { background: #dbeafe; color: #1e40af; }

.alert {
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 16px;
}

.alert-success { background: #d1fae5; color: #065f46; }
.alert-error { background: #fee2e2; color: #991b1b; }
.alert-warning { background: #fef3c7; color: #92400e; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #1e293b, #334155);
}

.login-card {
    background: #fff;
    padding: 32px;
    border-radius: var(--radius);
    width: 100%;
    max-width: 400px;
    box-shadow: var(--shadow);
}

.login-card h1 { margin: 0 0 8px; }
.login-card p { color: var(--muted); margin: 0 0 24px; }

.danger-zone {
    border: 1px solid #fecaca;
    background: #fff5f5;
    border-radius: var(--radius);
    padding: 20px;
}

.danger-zone h3 { color: var(--danger); margin-top: 0; }

.ai-fab {
    position: fixed;
    right: 24px;
    bottom: 24px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    background: var(--primary);
    color: #fff;
    font-size: 1.5rem;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(37, 99, 235, .4);
    z-index: 1000;
}

.ai-fab:hover { transform: scale(1.05); }

.ai-panel {
    position: fixed;
    right: 24px;
    bottom: 92px;
    width: 380px;
    max-width: calc(100vw - 48px);
    height: 480px;
    background: #fff;
    border-radius: var(--radius);
    box-shadow: 0 12px 40px rgba(0,0,0,.18);
    display: flex;
    flex-direction: column;
    z-index: 1000;
}

.ai-panel[hidden] { display: none !important; }

.ai-panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}

.ai-panel-header button {
    border: none;
    background: none;
    font-size: 1.125rem;
    cursor: pointer;
}

.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.ai-msg {
    padding: 10px 12px;
    border-radius: 10px;
    font-size: .9rem;
    line-height: 1.45;
    max-width: 92%;
}

.ai-msg-user {
    background: var(--primary);
    color: #fff;
    align-self: flex-end;
}

.ai-msg-bot {
    background: #f1f5f9;
    color: var(--text);
    align-self: flex-start;
}

.ai-form {
    display: flex;
    gap: 8px;
    padding: 12px;
    border-top: 1px solid var(--border);
}

.ai-form textarea {
    flex: 1;
    resize: none;
}

.ai-form button {
    align-self: flex-end;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 10px 14px;
    cursor: pointer;
    font-weight: 600;
}

@media (max-width: 768px) {
    .sidebar { position: static; width: 100%; }
    .app { flex-direction: column; }
    .content { margin-left: 0; padding: 16px; }
}

.status-badge {
    display: inline-block;
    padding: 6px 12px;
    border-radius: 999px;
    font-weight: 600;
    font-size: .875rem;
}

.status-ok { background: #d1fae5; color: #065f46; }
.status-error { background: #fee2e2; color: #991b1b; }

.text-muted { color: var(--muted); font-size: .9rem; }
.text-success { color: var(--success); font-weight: 600; }
.text-danger { color: var(--danger); font-weight: 600; }

.chart-container {
    position: relative;
    height: 360px;
    width: 100%;
}

.filter-form { display: flex; flex-direction: column; gap: 16px; }

.filter-tabs {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
}

.filter-tabs label {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    font-weight: 600;
}

.cliente-search-wrap {
    position: relative;
}

.cliente-search-dropdown {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: var(--shadow);
    max-height: 260px;
    overflow-y: auto;
    z-index: 50;
}

.cliente-search-item {
    display: block;
    width: 100%;
    text-align: left;
    border: none;
    background: none;
    padding: 10px 12px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
}

.cliente-search-item:hover {
    background: #f1f5f9;
}

.cliente-search-item strong {
    display: block;
    margin-bottom: 4px;
}

.cliente-search-item span {
    display: block;
    font-size: .8125rem;
    color: var(--muted);
}

.cliente-search-empty {
    padding: 12px;
    color: var(--muted);
    font-size: .875rem;
}

.cliente-details summary {
    list-style: none;
}

.cliente-details summary::-webkit-details-marker {
    display: none;
}

.sub-table {
    font-size: .875rem;
}

.sub-table th {
    font-size: .75rem;
}

.email-preview {
    background: #f8fafc;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 16px;
}

.email-preview pre {
    white-space: pre-wrap;
    font-family: inherit;
    margin: 0;
    line-height: 1.5;
}

.email-preview hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 12px 0;
}

.import-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding: 10px 12px;
    background: #f8fafc;
    border-radius: 8px;
}

.import-toolbar label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    cursor: pointer;
}

.import-select {
    width: 100%;
    min-width: 180px;
    font-size: .8125rem;
    padding: 6px 8px;
}

.row-duplicado {
    background: #fffbeb;
}
