/* ==========================================================================
   FCT Dashboard - Custom Styles
   Works on top of Pico CSS v2
   ========================================================================== */

/* ---------- Root variables ---------- */
:root {
    --fct-primary: #1e40af;
    --fct-primary-light: #3b82f6;
    --fct-primary-dark: #1e3a8a;
    --fct-bg: #f8fafc;
    --fct-sidebar-bg: #1e293b;
    --fct-sidebar-text: #cbd5e1;
    --fct-sidebar-active: #3b82f6;
    --fct-header-bg: #ffffff;
    --fct-card-bg: #ffffff;
    --fct-border: #e2e8f0;
    --fct-text: #334155;
    --fct-text-muted: #94a3b8;

    /* Status colors */
    --badge-pending: #f59e0b;
    --badge-pending-bg: #fef3c7;
    --badge-matched: #10b981;
    --badge-matched-bg: #d1fae5;
    --badge-declared: #3b82f6;
    --badge-declared-bg: #dbeafe;
    --badge-skipped: #6b7280;
    --badge-skipped-bg: #f3f4f6;
    --badge-success: #10b981;
    --badge-success-bg: #d1fae5;
    --badge-error: #ef4444;
    --badge-error-bg: #fee2e2;

    /* Sidebar dimensions */
    --sidebar-width: 240px;
    --header-height: 60px;

    /* Override Pico primary color */
    --pico-primary: var(--fct-primary);
}

/* ---------- Font stack ---------- */
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 "Noto Sans", "Helvetica Neue", Arial, sans-serif,
                 "Apple Color Emoji", "Segoe UI Emoji";
    background: var(--fct-bg);
    color: var(--fct-text);
    margin: 0;
    padding: 0;
}

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

/* ---------- Sidebar ---------- */
.sidebar {
    width: var(--sidebar-width);
    background: var(--fct-sidebar-bg);
    color: var(--fct-sidebar-text);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    transition: transform 0.2s ease;
}

.sidebar-header {
    padding: 1.25rem 1rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-header h3 {
    color: #ffffff;
    margin: 0 0 0.25rem 0;
    font-size: 1.1rem;
}

.sidebar-header small {
    color: var(--fct-sidebar-text);
    font-size: 0.75rem;
}

.sidebar-nav {
    flex: 1;
    padding: 0.5rem 0;
}

.sidebar-nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.sidebar-nav li {
    margin: 0;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    color: var(--fct-sidebar-text);
    text-decoration: none;
    font-size: 0.9rem;
    border-left: 3px solid transparent;
    transition: background 0.15s ease, border-color 0.15s ease;
}

.sidebar-nav a:hover {
    background: rgba(255, 255, 255, 0.05);
    color: #ffffff;
}

.sidebar-nav a.active {
    background: rgba(59, 130, 246, 0.15);
    border-left-color: var(--fct-sidebar-active);
    color: #ffffff;
}

.nav-icon {
    font-size: 1rem;
    width: 1.25rem;
    text-align: center;
}

.sidebar-footer {
    padding: 1rem;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-footer small {
    color: var(--fct-text-muted);
    font-size: 0.7rem;
}

/* ---------- Main wrapper ---------- */
.main-wrapper {
    flex: 1;
    margin-left: var(--sidebar-width);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* ---------- Header ---------- */
.app-header {
    height: var(--header-height);
    background: var(--fct-header-bg);
    border-bottom: 1px solid var(--fct-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    position: sticky;
    top: 0;
    z-index: 50;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.header-left h4 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--fct-text);
}

.sidebar-toggle {
    display: none;
    background: none;
    border: none;
    font-size: 1.25rem;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    color: var(--fct-text);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.month-label {
    font-size: 0.85rem;
    color: var(--fct-text-muted);
    margin: 0;
}

.month-picker {
    width: auto;
    margin: 0;
    padding: 0.35rem 0.5rem;
    font-size: 0.85rem;
    border: 1px solid var(--fct-border);
    border-radius: 4px;
}

/* ---------- Main content ---------- */
.main-content {
    flex: 1;
    padding: 1.5rem;
    max-width: 1400px;
}

/* ---------- Footer ---------- */
.app-footer {
    padding: 0.75rem 1.5rem;
    border-top: 1px solid var(--fct-border);
    text-align: center;
    color: var(--fct-text-muted);
}

/* ---------- Summary cards ---------- */
.summary-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.summary-card {
    background: var(--fct-card-bg);
    border: 1px solid var(--fct-border);
    border-radius: 8px;
    padding: 1.25rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    margin: 0;
}

.card-icon {
    font-size: 2rem;
    width: 3rem;
    height: 3rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    flex-shrink: 0;
}

.card-invoices .card-icon {
    background: #dbeafe;
    color: var(--fct-primary);
}

.card-matched .card-icon {
    background: var(--badge-matched-bg);
    color: var(--badge-matched);
}

.card-unmatched .card-icon {
    background: var(--badge-pending-bg);
    color: var(--badge-pending);
}

.card-tax .card-icon {
    background: #fce7f3;
    color: #be185d;
}

.card-body h5 {
    margin: 0;
    font-size: 0.8rem;
    color: var(--fct-text-muted);
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.025em;
}

.card-value {
    margin: 0.25rem 0 0;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--fct-text);
}

/* ---------- Quick actions ---------- */
.quick-actions {
    margin-bottom: 1.5rem;
}

.quick-actions h5 {
    margin-bottom: 0.75rem;
}

.action-buttons {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.btn-action {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    border: 1px solid transparent;
    text-decoration: none;
    transition: all 0.15s ease;
}

.btn-scraper {
    background: var(--fct-primary);
    color: #ffffff;
}

.btn-scraper:hover {
    background: var(--fct-primary-dark);
}

.btn-upload {
    background: var(--badge-matched);
    color: #ffffff;
}

.btn-upload:hover {
    background: #059669;
}

.btn-auto {
    background: var(--fct-primary);
    color: #ffffff;
}

.btn-manual {
    border-color: var(--fct-primary);
    color: var(--fct-primary);
}

.btn-manual:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* ---------- Status badges ---------- */
.badge {
    display: inline-block;
    padding: 0.2rem 0.6rem;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 500;
    white-space: nowrap;
}

.badge-pending {
    background: var(--badge-pending-bg);
    color: #92400e;
}

.badge-matched {
    background: var(--badge-matched-bg);
    color: #065f46;
}

.badge-declared {
    background: var(--badge-declared-bg);
    color: #1e40af;
}

.badge-skipped {
    background: var(--badge-skipped-bg);
    color: #374151;
}

.badge-success {
    background: var(--badge-success-bg);
    color: #065f46;
}

.badge-error {
    background: var(--badge-error-bg);
    color: #991b1b;
}

/* ---------- Tables ---------- */
.table-wrapper {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

table th {
    font-weight: 600;
    text-transform: none;
    font-size: 0.8rem;
    color: var(--fct-text-muted);
    border-bottom: 2px solid var(--fct-border);
    padding: 0.6rem 0.75rem;
    white-space: nowrap;
}

table td {
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--fct-border);
    vertical-align: middle;
}

table.compact th,
table.compact td {
    padding: 0.4rem 0.5rem;
    font-size: 0.8rem;
}

table.striped tbody tr:nth-child(even) {
    background: rgba(0, 0, 0, 0.02);
}

table.striped tbody tr:hover {
    background: rgba(59, 130, 246, 0.04);
}

.total-row {
    background: #f1f5f9 !important;
    font-weight: 600;
}

.total-row td {
    border-top: 2px solid var(--fct-border);
}

.text-right {
    text-align: right;
}

.text-center {
    text-align: center;
}

.text-bold {
    font-weight: 700;
}

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

.clickable-row {
    cursor: pointer;
}

.clickable-row:hover {
    background: rgba(59, 130, 246, 0.06) !important;
}

/* ---------- Action links in tables ---------- */
.action-link {
    background: none;
    border: none;
    color: var(--fct-primary-light);
    cursor: pointer;
    font-size: 0.8rem;
    padding: 0.15rem 0.35rem;
    text-decoration: none;
    border-radius: 3px;
    transition: background 0.15s;
}

.action-link:hover {
    background: rgba(59, 130, 246, 0.1);
    text-decoration: none;
}

.btn-skip, .btn-unmatch, .btn-cancel {
    color: #ef4444;
}

.btn-skip:hover, .btn-unmatch:hover, .btn-cancel:hover {
    background: rgba(239, 68, 68, 0.1);
}

.btn-confirm, .btn-save {
    color: var(--badge-matched);
}

.btn-confirm:hover, .btn-save:hover {
    background: var(--badge-matched-bg);
}

.action-cell {
    white-space: nowrap;
}

/* ---------- Filters ---------- */
.filters-section {
    margin-bottom: 1.5rem;
}

.filters-section h5 {
    margin-bottom: 0.75rem;
}

.filter-form {
    margin: 0;
}

.filter-row {
    display: flex;
    gap: 1rem;
    align-items: flex-end;
    flex-wrap: wrap;
}

.filter-item {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.filter-item label {
    font-size: 0.8rem;
    color: var(--fct-text-muted);
    margin: 0;
}

.filter-item select,
.filter-item input {
    margin: 0;
    padding: 0.4rem 0.6rem;
    font-size: 0.85rem;
    min-width: 150px;
}

.filter-action button {
    margin: 0;
    padding: 0.4rem 1rem;
}

/* ---------- Inline form ---------- */
.inline-form .form-row {
    display: flex;
    gap: 1rem;
    align-items: flex-end;
    flex-wrap: wrap;
}

.form-item {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.form-item label {
    font-size: 0.8rem;
    color: var(--fct-text-muted);
    margin: 0;
}

.form-item select,
.form-item input[type="file"],
.form-item input[type="text"],
.form-item input[type="number"],
.form-item input[type="month"],
.form-item input[type="date"] {
    margin: 0;
    padding: 0.4rem 0.6rem;
    font-size: 0.85rem;
}

.form-action {
    align-self: flex-end;
}

/* ---------- Form grid (vendors) ---------- */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 0.75rem;
    align-items: end;
}

.form-grid .form-item input,
.form-grid .form-item select {
    width: 100%;
}

/* ---------- Upload form ---------- */
.upload-section {
    margin-bottom: 1.5rem;
    background: var(--fct-card-bg);
    border: 1px solid var(--fct-border);
    border-radius: 8px;
    padding: 1.25rem;
}

.upload-section h5 {
    margin: 0 0 0.75rem;
}

/* ---------- Matching page: split layout ---------- */
.matching-page .matching-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.matching-actions .action-left {
    display: flex;
    gap: 0.5rem;
}

.matching-actions .action-right {
    margin-left: auto;
}

.split-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.panel {
    background: var(--fct-card-bg);
    border: 1px solid var(--fct-border);
    border-radius: 8px;
    overflow: hidden;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    background: #f8fafc;
    border-bottom: 1px solid var(--fct-border);
}

.panel-header h5 {
    margin: 0;
    font-size: 0.9rem;
}

.panel-count {
    font-size: 0.75rem;
    color: var(--fct-text-muted);
    background: var(--fct-border);
    padding: 0.15rem 0.5rem;
    border-radius: 10px;
}

.panel-body {
    max-height: 400px;
    overflow-y: auto;
}

/* ---------- Row selection (matching) ---------- */
.selectable-row {
    cursor: pointer;
    transition: background 0.1s ease;
}

.selectable-row:hover {
    background: rgba(59, 130, 246, 0.06) !important;
}

.selectable-row.selected {
    background: rgba(59, 130, 246, 0.12) !important;
    border-left: 3px solid var(--fct-primary);
}

.col-select {
    width: 24px;
    text-align: center;
}

.select-indicator {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid var(--fct-border);
    border-radius: 50%;
    vertical-align: middle;
    transition: all 0.15s ease;
}

.selected .select-indicator {
    border-color: var(--fct-primary);
    background: var(--fct-primary);
    box-shadow: inset 0 0 0 2px #ffffff;
}

.row-matched {
    opacity: 0.5;
}

/* ---------- Inline edit (vendors) ---------- */
.hidden {
    display: none !important;
}

.compact-input {
    padding: 0.25rem 0.4rem !important;
    font-size: 0.8rem !important;
    margin: 0 !important;
}

/* ---------- Sections ---------- */
.table-section {
    margin-bottom: 1.5rem;
}

.table-section h5,
.matched-section h5,
.recent-invoices h5,
.import-history h5,
.downloads-section h5,
.add-vendor-section h5 {
    margin: 0 0 0.75rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--fct-border);
    font-size: 1rem;
}

.add-vendor-section {
    background: var(--fct-card-bg);
    border: 1px solid var(--fct-border);
    border-radius: 8px;
    padding: 1.25rem;
}

/* ---------- Description truncation ---------- */
.txn-description {
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ---------- Pagination ---------- */
.pagination-nav {
    margin-top: 1rem;
    text-align: center;
}

.pagination-nav ul {
    display: inline-flex;
    gap: 0.25rem;
    list-style: none;
    padding: 0;
    margin: 0;
}

.pagination-nav a {
    display: inline-block;
    padding: 0.3rem 0.6rem;
    border-radius: 4px;
    font-size: 0.85rem;
    text-decoration: none;
    color: var(--fct-text);
    border: 1px solid var(--fct-border);
}

.pagination-nav a.active {
    background: var(--fct-primary);
    color: #ffffff;
    border-color: var(--fct-primary);
}

.pagination-nav a:hover:not(.active) {
    background: rgba(59, 130, 246, 0.06);
}

/* ---------- Toast notifications ---------- */
#toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-width: 380px;
}

.toast {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    animation: toast-in 0.3s ease;
    font-size: 0.875rem;
}

.toast-success {
    background: var(--badge-matched-bg);
    border: 1px solid var(--badge-matched);
    color: #065f46;
}

.toast-error {
    background: var(--badge-error-bg);
    border: 1px solid var(--badge-error);
    color: #991b1b;
}

.toast-warning {
    background: var(--badge-pending-bg);
    border: 1px solid var(--badge-pending);
    color: #92400e;
}

.toast-info {
    background: var(--badge-declared-bg);
    border: 1px solid var(--badge-declared);
    color: #1e40af;
}

.toast-content {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.toast-icon {
    font-size: 1.1rem;
}

.toast-close {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.2rem;
    padding: 0 0.25rem;
    opacity: 0.6;
    color: inherit;
}

.toast-close:hover {
    opacity: 1;
}

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

/* ---------- htmx loading indicator ---------- */
.htmx-indicator {
    display: none;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--fct-text-muted);
    font-size: 0.85rem;
}

.global-loader {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.7);
    z-index: 9998;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    gap: 0.75rem;
}

.loader-spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--fct-border);
    border-top-color: var(--fct-primary);
    border-radius: 50%;
    animation: spinner 0.6s linear infinite;
}

.global-loader .loader-spinner {
    width: 36px;
    height: 36px;
}

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

/* ---------- Dialog / Modal ---------- */
dialog {
    border: 1px solid var(--fct-border);
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    max-width: 450px;
    width: 90%;
}

dialog article {
    margin: 0;
}

dialog header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--fct-border);
}

dialog header h5 {
    margin: 0;
}

dialog footer {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    padding: 0.75rem 1.25rem;
    border-top: 1px solid var(--fct-border);
}

/* ---------- Responsive ---------- */
@media (max-width: 1024px) {
    .summary-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .split-layout {
        grid-template-columns: 1fr;
    }
}

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

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

    .sidebar-toggle {
        display: block;
    }

    .main-wrapper {
        margin-left: 0;
    }

    .summary-grid {
        grid-template-columns: 1fr 1fr;
    }

    .filter-row {
        flex-direction: column;
    }

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

    .app-header {
        padding: 0 1rem;
    }

    .main-content {
        padding: 1rem;
    }
}

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

/* ---------- Sidebar collapsed state ---------- */
.sidebar-collapsed .sidebar {
    transform: translateX(-100%);
}

.sidebar-collapsed .main-wrapper {
    margin-left: 0;
}

.sidebar-collapsed .sidebar-toggle {
    display: block;
}

/* ---------- Scraper section ---------- */
.scraper-section {
    background: var(--fct-card-bg);
    border: 1px solid var(--fct-border);
    border-radius: 8px;
    padding: 1.25rem;
    margin-bottom: 1.5rem;
}

.scraper-section h5 {
    margin: 0 0 0.75rem;
}

/* ---------- Import history ---------- */
.import-history {
    margin-top: 2rem;
}

/* ---------- Tax reports ---------- */
.downloads-section {
    margin-top: 2rem;
}

/* ---------- Number formatting helper ---------- */
.num-vnd::after {
    content: " VND";
    font-size: 0.75em;
    color: var(--fct-text-muted);
}

.num-usd::after {
    content: " USD";
    font-size: 0.75em;
    color: var(--fct-text-muted);
}

/* ---------- Print styles ---------- */
@media print {
    .sidebar, .app-header, .app-footer,
    .quick-actions, .filters-section,
    .scraper-section, .upload-section,
    .action-cell, .btn-action,
    #toast-container, .global-loader {
        display: none !important;
    }

    .main-wrapper {
        margin-left: 0;
    }

    .main-content {
        padding: 0;
    }

    table {
        font-size: 0.75rem;
    }
}
