/* ============================================
   eMerchan Financial Portal — fin.css
   Single CSS file for the entire portal.
   Dark mode default, light theme toggle.
   ============================================ */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

/* ------------------------------------------
   1. CSS Variables
   ------------------------------------------ */

:root,
[data-theme="dark"] {
    --bg-primary: #1a1a2e;
    --bg-card: #111827;
    --bg-sidebar: #0f172a;
    --bg-sidebar-hover: #1e293b;
    --bg-sidebar-active: #1e293b;
    --bg-input: #1f2937;
    --border: #1f2937;
    --border-light: #374151;
    --text-primary: #e5e7eb;
    --text-secondary: #9ca3af;
    --text-muted: #6b7280;
    --accent: #4ade80;
    --accent-hover: #22c55e;
    --accent-bg: rgba(74, 222, 128, 0.1);
    --danger: #ef4444;
    --danger-bg: rgba(239, 68, 68, 0.1);
    --warning: #f59e0b;
    --warning-bg: rgba(245, 158, 11, 0.1);
    --info: #60a5fa;
    --info-bg: rgba(96, 165, 250, 0.1);
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --radius: 8px;
    --radius-lg: 12px;
    --sidebar-width: 240px;
    --sidebar-collapsed: 64px;
    --transition: 0.2s ease;
}

[data-theme="light"] {
    --bg-primary: #f1f5f9;
    --bg-card: #ffffff;
    --bg-input: #f8fafc;
    --border: #e2e8f0;
    --border-light: #cbd5e1;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --accent: #22c55e;
    --accent-hover: #16a34a;
    --accent-bg: rgba(34, 197, 94, 0.1);
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
    /* sidebar stays dark in light mode */
}

/* ------------------------------------------
   2. Reset + Base
   ------------------------------------------ */

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

html {
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transition: background var(--transition), color var(--transition);
}

a {
    color: var(--accent);
    text-decoration: none;
}

a:hover {
    color: var(--accent-hover);
}

img {
    max-width: 100%;
    display: block;
}

ul, ol {
    list-style: none;
}

/* Smooth theme transitions on key elements */
.stat-card,
.form-card,
.data-table,
.modal,
.login-card,
.main-content,
.page-header,
.tabs {
    transition: background var(--transition), color var(--transition), border-color var(--transition), box-shadow var(--transition);
}

/* ------------------------------------------
   3. Layout
   ------------------------------------------ */

.app-layout {
    display: flex;
    min-height: 100vh;
}

/* Sidebar */
.sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: var(--sidebar-width);
    height: 100vh;
    background: var(--bg-sidebar);
    overflow-y: auto;
    overflow-x: hidden;
    z-index: 100;
    display: flex;
    flex-direction: column;
    transition: width var(--transition);
}

.sidebar.collapsed {
    width: var(--sidebar-collapsed);
}

.sidebar .logo {
    padding: 20px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid #1e293b;
}

.sidebar .logo img {
    width: 32px;
    height: 32px;
    flex-shrink: 0;
}

.sidebar .logo span {
    font-size: 16px;
    font-weight: 700;
    color: #e5e7eb;
    white-space: nowrap;
    overflow: hidden;
    transition: opacity var(--transition), width var(--transition);
}

.sidebar.collapsed .logo span {
    opacity: 0;
    width: 0;
    overflow: hidden;
}

/* Sidebar navigation */
.sidebar .nav-section {
    padding: 16px 0 8px;
}

.sidebar .nav-section-title {
    padding: 0 16px;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #475569;
    margin-bottom: 8px;
    white-space: nowrap;
    overflow: hidden;
}

.sidebar.collapsed .nav-section-title {
    opacity: 0;
    height: 0;
    margin: 0;
    padding: 0;
}

.sidebar .nav-item {
    padding: 10px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    color: #94a3b8;
    cursor: pointer;
    border-left: 3px solid transparent;
    transition: all var(--transition);
    white-space: nowrap;
    font-size: 13px;
}

.sidebar .nav-item:hover {
    background: var(--bg-sidebar-hover);
}

.sidebar .nav-item.active {
    border-left-color: var(--accent);
    background: var(--bg-sidebar-active);
    color: var(--accent);
}

.sidebar .nav-item .icon {
    width: 20px;
    text-align: center;
    flex-shrink: 0;
    font-size: 16px;
}

.sidebar .nav-item .label {
    overflow: hidden;
    transition: opacity var(--transition), width var(--transition);
}

.sidebar.collapsed .nav-item .label {
    opacity: 0;
    width: 0;
    overflow: hidden;
}

.sidebar .badge {
    background: var(--danger);
    color: white;
    font-size: 10px;
    font-weight: 700;
    padding: 1px 6px;
    border-radius: 10px;
    margin-left: auto;
    flex-shrink: 0;
}

.sidebar.collapsed .badge {
    display: none;
}

/* User info at bottom */
.sidebar .user-info {
    margin-top: auto;
    border-top: 1px solid #1e293b;
    padding: 16px;
    font-size: 12px;
    color: #6b7280;
    display: flex;
    align-items: center;
    gap: 10px;
    white-space: nowrap;
    overflow: hidden;
}

.sidebar .user-info .user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #1e293b;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: var(--accent);
    flex-shrink: 0;
}

.sidebar .user-info .user-details {
    overflow: hidden;
    transition: opacity var(--transition);
}

.sidebar.collapsed .user-info .user-details {
    opacity: 0;
    width: 0;
}

.sidebar .user-info .user-name {
    font-weight: 600;
    color: #e5e7eb;
    font-size: 12px;
}

.sidebar .user-info .user-role {
    font-size: 11px;
    color: #6b7280;
}

/* Sidebar toggle */
.sidebar-toggle {
    position: absolute;
    top: 20px;
    right: -12px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--bg-sidebar);
    border: 1px solid #1e293b;
    color: #94a3b8;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    z-index: 101;
    transition: all var(--transition);
}

.sidebar-toggle:hover {
    background: var(--bg-sidebar-hover);
    color: #e5e7eb;
}

/* Main content */
.main-content {
    margin-left: var(--sidebar-width);
    padding: 24px;
    min-height: 100vh;
    flex: 1;
    transition: margin-left var(--transition);
}

.main-content.expanded {
    margin-left: var(--sidebar-collapsed);
}

/* ------------------------------------------
   4. Mobile (< 768px)
   ------------------------------------------ */

.mobile-header {
    display: none;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--bg-sidebar);
    position: sticky;
    top: 0;
    z-index: 50;
}

.mobile-header .hamburger {
    background: none;
    border: none;
    color: #e5e7eb;
    font-size: 20px;
    cursor: pointer;
    padding: 4px;
}

.mobile-header .logo-mobile {
    display: flex;
    align-items: center;
    gap: 8px;
}

.mobile-header .logo-mobile img {
    width: 28px;
    height: 28px;
}

.mobile-header .logo-mobile span {
    font-size: 14px;
    font-weight: 700;
    color: #e5e7eb;
}

.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 99;
    display: none;
}

.sidebar-overlay.active {
    display: block;
}

@media (max-width: 767px) {
    .sidebar {
        transform: translateX(-100%);
        position: fixed;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar.collapsed {
        width: var(--sidebar-width);
    }

    .sidebar-toggle {
        display: none;
    }

    .main-content,
    .main-content.expanded {
        margin-left: 0;
        padding: 16px;
    }

    .mobile-header {
        display: flex;
    }

    .stat-cards {
        grid-template-columns: 1fr !important;
    }

    .form-row {
        grid-template-columns: 1fr !important;
    }

    .page-header {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 12px;
    }

    .data-table {
        font-size: 12px;
    }

    .data-table th,
    .data-table td {
        padding: 8px 10px;
    }

    .modal {
        padding: 24px !important;
        max-width: 95% !important;
    }
}

/* ------------------------------------------
   5. Components
   ------------------------------------------ */

/* --- Page Header --- */

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

.page-title {
    font-size: 20px;
    font-weight: 700;
}

.page-subtitle {
    font-size: 13px;
    color: var(--text-muted);
    margin-top: 4px;
}

.page-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

/* --- Stat Cards --- */

.stat-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: 20px;
    box-shadow: var(--shadow);
}

.stat-card .stat-icon {
    width: 40px;
    height: 40px;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    margin-bottom: 12px;
}

.stat-card .stat-label {
    font-size: 12px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    font-weight: 500;
}

.stat-card .stat-value {
    font-size: 24px;
    font-weight: 700;
    margin-top: 4px;
    line-height: 1.2;
}

.stat-card .stat-change {
    font-size: 12px;
    margin-top: 8px;
    display: flex;
    align-items: center;
    gap: 4px;
}

.stat-card .stat-change.positive {
    color: var(--accent);
}

.stat-card .stat-change.negative {
    color: var(--danger);
}

/* --- Data Tables --- */

.table-container {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    overflow: hidden;
    margin-bottom: 24px;
}

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

.table-header h3 {
    font-size: 14px;
    font-weight: 600;
}

.table-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.table-search {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 6px 12px;
    color: var(--text-primary);
    font-size: 12px;
    width: 200px;
    transition: border-color var(--transition);
}

.table-search:focus {
    border-color: var(--accent);
    outline: none;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow);
}

.table-container .data-table {
    border-radius: 0;
    box-shadow: none;
}

.data-table th {
    background: var(--bg-input);
    padding: 12px 16px;
    text-align: left;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    border-bottom: 1px solid var(--border);
}

.data-table td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
}

.data-table tr:last-child td {
    border-bottom: none;
}

.data-table tr:hover {
    background: var(--bg-input);
}

.table-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    border-top: 1px solid var(--border);
    font-size: 12px;
    color: var(--text-muted);
}

.pagination {
    display: flex;
    gap: 4px;
}

.pagination button {
    padding: 4px 10px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--bg-input);
    color: var(--text-secondary);
    font-size: 12px;
    cursor: pointer;
    transition: all var(--transition);
}

.pagination button:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.pagination button.active {
    background: var(--accent);
    color: #0f172a;
    border-color: var(--accent);
}

/* --- Forms --- */

.form-card {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: 24px;
    box-shadow: var(--shadow);
    margin-bottom: 24px;
}

.form-card h3 {
    font-size: 15px;
    font-weight: 600;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border);
}

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

.form-row.cols-3 {
    grid-template-columns: 1fr 1fr 1fr;
}

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

.form-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.form-field label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
}

.form-field input,
.form-field select,
.form-field textarea {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 12px;
    color: var(--text-primary);
    font-size: 13px;
    font-family: inherit;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.form-field input:focus,
.form-field select:focus,
.form-field textarea:focus {
    border-color: var(--accent);
    outline: none;
    box-shadow: 0 0 0 2px var(--accent-bg);
}

.form-field input::placeholder,
.form-field textarea::placeholder {
    color: var(--text-muted);
}

.form-field textarea {
    resize: vertical;
    min-height: 80px;
}

.form-field select {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236b7280' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 32px;
}

.form-field .hint {
    font-size: 11px;
    color: var(--text-muted);
}

.form-field .error-msg {
    font-size: 11px;
    color: var(--danger);
}

.form-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

/* --- Buttons --- */

.btn {
    padding: 10px 20px;
    border-radius: var(--radius);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition);
    border: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-family: inherit;
    line-height: 1;
}

.btn-primary {
    background: var(--accent);
    color: #0f172a;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    border-color: var(--border-light);
    background: var(--border);
}

.btn-danger {
    background: var(--danger);
    color: white;
}

.btn-danger:hover {
    opacity: 0.9;
}

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

.btn-ghost:hover {
    color: var(--text-primary);
    background: var(--bg-input);
}

.btn-sm {
    padding: 6px 12px;
    font-size: 12px;
}

.btn-lg {
    padding: 12px 28px;
    font-size: 14px;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

/* --- Status Badges --- */

.status-badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
}

.status-finalizado {
    background: var(--accent-bg);
    color: var(--accent);
}

.status-em_processamento {
    background: var(--info-bg);
    color: var(--info);
}

.status-pendente_aprovacao {
    background: var(--warning-bg);
    color: var(--warning);
}

.status-rejeitado,
.status-erro {
    background: var(--danger-bg);
    color: var(--danger);
}

.status-rascunho {
    background: rgba(107, 114, 128, 0.1);
    color: var(--text-muted);
}

/* --- Tabs --- */

.tabs {
    display: flex;
    gap: 0;
    border-bottom: 2px solid var(--border);
    margin-bottom: 24px;
}

.tab {
    padding: 10px 20px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: all var(--transition);
    background: none;
    border-top: none;
    border-left: none;
    border-right: none;
    font-family: inherit;
}

.tab:hover {
    color: var(--text-secondary);
}

.tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* --- Modal --- */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 200;
    display: none;
    align-items: center;
    justify-content: center;
}

.modal-overlay.active {
    display: flex;
}

.modal {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: 32px;
    max-width: 480px;
    width: 90%;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    animation: fadeIn 0.2s ease;
}

.modal h3 {
    margin-bottom: 16px;
    font-size: 16px;
    font-weight: 700;
}

.modal p {
    color: var(--text-secondary);
    font-size: 13px;
    line-height: 1.6;
}

.modal-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    margin-top: 24px;
}

.modal-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 18px;
}

/* --- Login --- */

.login-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-primary);
}

.login-card {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: 40px;
    max-width: 400px;
    width: 90%;
    box-shadow: var(--shadow);
    text-align: center;
}

.login-card .logo {
    margin-bottom: 24px;
    display: flex;
    justify-content: center;
}

.login-card .logo img {
    height: 64px;
}

.login-card h1 {
    font-size: 20px;
    margin-bottom: 8px;
}

.login-card .subtitle {
    color: var(--text-muted);
    font-size: 13px;
    margin-bottom: 32px;
}

.login-card .form-field {
    text-align: left;
    margin-bottom: 16px;
}

.login-card .btn-primary {
    width: 100%;
    justify-content: center;
    padding: 12px;
    margin-top: 8px;
}

.login-card .login-footer {
    margin-top: 24px;
    font-size: 12px;
    color: var(--text-muted);
}

/* --- Toast --- */

.toast {
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 12px 20px;
    border-radius: var(--radius);
    font-size: 13px;
    font-weight: 500;
    z-index: 300;
    animation: slideIn 0.3s ease;
    display: flex;
    align-items: center;
    gap: 8px;
    max-width: 400px;
}

.toast-success {
    background: var(--accent);
    color: #0f172a;
}

.toast-error {
    background: var(--danger);
    color: white;
}

.toast-warning {
    background: var(--warning);
    color: #0f172a;
}

/* --- Empty State --- */

.empty-state {
    text-align: center;
    padding: 48px;
    color: var(--text-muted);
}

.empty-state .empty-icon {
    font-size: 48px;
    margin-bottom: 16px;
    opacity: 0.5;
}

.empty-state h3 {
    font-size: 16px;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.empty-state p {
    font-size: 13px;
    max-width: 320px;
    margin: 0 auto;
}

/* --- QR Code / 2FA --- */

.qr-container {
    text-align: center;
    margin: 20px 0;
}

.qr-container img {
    max-width: 200px;
    border-radius: var(--radius);
    margin: 0 auto;
}

.secret-code {
    font-family: 'Courier New', Courier, monospace;
    background: var(--bg-input);
    padding: 8px 16px;
    border-radius: var(--radius);
    user-select: all;
    font-size: 14px;
    letter-spacing: 2px;
    display: inline-block;
    margin-top: 12px;
    border: 1px solid var(--border);
}

/* --- Theme Toggle --- */

.theme-toggle {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 6px;
    border-radius: var(--radius);
    font-size: 18px;
    transition: all var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.theme-toggle:hover {
    color: var(--text-primary);
    background: var(--bg-input);
}

/* --- Loading / Spinner --- */

.spinner {
    width: 20px;
    height: 20px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

.loading-overlay {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 48px;
}

/* --- Divider --- */

.divider {
    height: 1px;
    background: var(--border);
    margin: 24px 0;
}

/* --- Scrollbar --- */

::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-light);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* --- Utility --- */

.text-accent { color: var(--accent); }
.text-danger { color: var(--danger); }
.text-warning { color: var(--warning); }
.text-info { color: var(--info); }
.text-muted { color: var(--text-muted); }
.text-right { text-align: right; }
.text-center { text-align: center; }
.font-mono { font-family: 'Courier New', Courier, monospace; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mt-24 { margin-top: 24px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.mb-24 { margin-bottom: 24px; }
.gap-8 { gap: 8px; }
.gap-16 { gap: 16px; }
.flex { display: flex; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.flex-center { display: flex; align-items: center; justify-content: center; }
.hidden { display: none !important; }

/* ------------------------------------------
   6. Animations
   ------------------------------------------ */

@keyframes slideIn {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}
