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

:root {
    --color-background: #f8fafc;
    --color-surface: #ffffff;
    --color-surface-muted: #f1f5f9;
    --color-text: #0f172a;
    --color-text-muted: #475569;
    --color-primary: #1e40af;
    --color-primary-hover: #1e3a8a;
    --color-interactive: #2563eb;
    --color-accent: #b45309;
    --color-success: #15803d;
    --color-danger: #b91c1c;
    --color-border: #d9e2ec;
    --color-border-strong: #b8c5d3;
    --color-focus: #2563eb;
    --radius: 8px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
    --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.08);
}

* {
    box-sizing: border-box;
}

html {
    color-scheme: light;
    scroll-behavior: smooth;
}

body {
    min-height: 100vh;
    margin: 0;
    padding: 24px;
    color: var(--color-text);
    background: var(--color-background);
    font-family: 'Manrope', 'Segoe UI', sans-serif;
    font-size: 16px;
    line-height: 1.5;
}

button,
input,
select {
    font: inherit;
}

button,
a,
input[type="checkbox"] {
    touch-action: manipulation;
}

button,
a[href],
select,
input[type="checkbox"] {
    cursor: pointer;
}

button:disabled,
input:disabled,
select:disabled {
    cursor: not-allowed;
}

a {
    color: var(--color-interactive);
}

.skip-link {
    position: fixed;
    top: 8px;
    left: 8px;
    z-index: 100;
    padding: 10px 14px;
    border-radius: 6px;
    color: #ffffff;
    background: var(--color-primary);
    text-decoration: none;
    transform: translateY(-140%);
    transition: transform 160ms ease;
}

.skip-link:focus {
    transform: translateY(0);
}

.app-shell {
    width: min(100%, 1440px);
    margin: 0 auto;
}

.app-header {
    display: grid;
    gap: 20px;
    margin-bottom: 24px;
    padding: 4px 0 20px;
    border-bottom: 1px solid var(--color-border);
}

.page-heading {
    display: grid;
    gap: 6px;
}

.page-heading h1 {
    margin: 0;
    color: var(--color-text);
    font-size: 2rem;
    font-weight: 800;
    line-height: 1.2;
    letter-spacing: 0;
}

.subtitle {
    max-width: 820px;
    margin: 0;
    color: var(--color-text-muted);
}

.top-nav {
    display: flex;
    gap: 6px;
    min-width: 0;
    overflow-x: auto;
    padding-bottom: 2px;
    scrollbar-width: thin;
}

.top-nav a {
    display: inline-flex;
    min-height: 44px;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    padding: 9px 14px;
    border: 1px solid transparent;
    border-radius: 6px;
    color: var(--color-text-muted);
    font-size: 0.92rem;
    font-weight: 700;
    text-decoration: none;
    transition: background-color 160ms ease, border-color 160ms ease, color 160ms ease;
}

.top-nav a:hover {
    border-color: var(--color-border);
    color: var(--color-primary);
    background: var(--color-surface);
}

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

.app-main {
    display: grid;
    gap: 20px;
    min-width: 0;
}

.app-main > * {
    min-width: 0;
}

.instructions,
.data-section,
.summary-section,
.filter-section,
.results-section,
.panel,
.log-section {
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    background: var(--color-surface);
    box-shadow: var(--shadow-sm);
}

.instructions {
    padding: 16px 20px;
    border-left: 4px solid var(--color-interactive);
    background: #eff6ff;
    box-shadow: none;
}

.instructions h2,
.data-section h2,
.summary-section h2,
.filter-section h2,
.results-section h2,
.panel h2,
.log-section h2 {
    margin: 0;
    color: var(--color-text);
    font-size: 1.16rem;
    font-weight: 800;
    line-height: 1.35;
    letter-spacing: 0;
}

.instructions ul,
.instructions ol {
    margin: 8px 0 0;
    padding-left: 22px;
    color: var(--color-text-muted);
}

.instructions li + li {
    margin-top: 4px;
}

.data-section,
.summary-section,
.filter-section,
.results-section,
.panel,
.log-section {
    padding: 20px;
}

.section-heading,
.results-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 16px;
}

.action-buttons,
.results-actions,
.actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.submit-container {
    display: flex;
    min-height: 52px;
    align-items: center;
    gap: 16px;
    padding: 12px 16px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    background: var(--color-surface);
    box-shadow: var(--shadow-sm);
}

.submit-btn,
.btn-primary,
.btn-secondary,
.btn-add,
.btn-delete-selected,
.btn-clear-all,
.btn-search,
.btn-copy-image,
.btn-clear-log {
    display: inline-flex;
    min-height: 44px;
    align-items: center;
    justify-content: center;
    padding: 9px 16px;
    border: 1px solid transparent;
    border-radius: 6px;
    font-weight: 700;
    line-height: 1.2;
    text-decoration: none;
    transition: background-color 160ms ease, border-color 160ms ease, color 160ms ease, box-shadow 160ms ease;
}

.submit-btn,
.btn-primary,
.btn-search {
    color: #ffffff;
    background: var(--color-primary);
}

.submit-btn:hover,
.btn-primary:hover,
.btn-search:hover {
    background: var(--color-primary-hover);
}

.btn-secondary {
    border-color: var(--color-border-strong);
    color: var(--color-text);
    background: var(--color-surface);
}

.btn-secondary:hover {
    border-color: var(--color-primary);
    color: var(--color-primary);
    background: #eff6ff;
}

.btn-add {
    border-color: #86efac;
    color: #14532d;
    background: #f0fdf4;
}

.btn-add:hover {
    border-color: var(--color-success);
    background: #dcfce7;
}

.btn-delete-selected {
    color: #ffffff;
    background: var(--color-danger);
}

.btn-delete-selected:hover {
    background: #991b1b;
}

.btn-clear-all {
    border-color: #fcd34d;
    color: #78350f;
    background: #fffbeb;
}

.btn-clear-all:hover {
    border-color: var(--color-accent);
    background: #fef3c7;
}

.btn-copy-image,
.btn-clear-log {
    border-color: var(--color-border-strong);
    color: var(--color-text);
    background: var(--color-surface-muted);
}

.btn-copy-image:hover,
.btn-clear-log:hover {
    border-color: var(--color-primary);
    color: var(--color-primary);
    background: #eff6ff;
}

button:disabled,
.submit-btn:disabled,
.btn-primary:disabled,
.btn-copy-image:disabled {
    opacity: 0.58;
    box-shadow: none;
}

.status-message {
    display: none;
    min-width: 0;
    flex: 1;
    padding: 10px 12px;
    border: 1px solid var(--color-border);
    border-radius: 6px;
    font-weight: 700;
}

.status-message.success {
    display: block;
    border-color: #86efac;
    color: #14532d;
    background: #f0fdf4;
}

.status-message.error {
    display: block;
    border-color: #fca5a5;
    color: #7f1d1d;
    background: #fef2f2;
}

.status-message.loading {
    display: block;
    border-color: #93c5fd;
    color: #1e3a8a;
    background: #eff6ff;
}

.table-container,
#resultsContainer {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    border: 1px solid var(--color-border);
    border-radius: 6px;
    background: var(--color-surface);
    overscroll-behavior-inline: contain;
    scrollbar-width: thin;
}

.data-section .table-container {
    max-height: 560px;
    overflow-y: auto;
}

.summary-section .table-container {
    max-height: none;
    overflow-y: visible;
}

.data-table,
.summary-table,
.results-table {
    width: 100%;
    border-collapse: collapse;
    color: var(--color-text);
    font-variant-numeric: tabular-nums;
}

.data-table {
    min-width: 760px;
}

#salaryTable {
    min-width: 1040px;
}

#exportTable {
    min-width: 1180px;
}

.summary-table {
    min-width: 520px;
}

.results-table {
    min-width: 640px;
}

.data-table thead,
.summary-table thead {
    position: sticky;
    top: 0;
    z-index: 2;
}

.data-table th,
.summary-table th,
.results-table th {
    padding: 11px 12px;
    border-bottom: 1px solid var(--color-border-strong);
    color: #ffffff;
    background: var(--color-primary);
    font-size: 0.78rem;
    font-weight: 800;
    line-height: 1.3;
    text-align: left;
    text-transform: uppercase;
    white-space: nowrap;
}

.data-table td,
.summary-table td,
.results-table td {
    min-width: 112px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--color-border);
    background: var(--color-surface);
    vertical-align: middle;
}

.data-table tbody tr:last-child td,
.summary-table tbody tr:last-child td,
.results-table tbody tr:last-child td {
    border-bottom: 0;
}

.data-table tbody tr:hover td,
.summary-table tbody tr:hover td,
.results-table tbody tr:hover td {
    background: #f8fafc;
}

.selection-column {
    width: 52px;
    min-width: 52px !important;
    text-align: center !important;
}

.data-table .editable-cell {
    display: block;
    width: 100%;
    min-height: 32px;
    padding: 5px 6px;
    border: 1px solid transparent;
    border-radius: 4px;
    cursor: text;
    outline: none;
}

.data-table .editable-cell:hover {
    border-color: var(--color-border-strong);
    background: var(--color-surface-muted);
}

.data-table .editable-cell:focus {
    border-color: var(--color-interactive);
    background: var(--color-surface);
    box-shadow: 0 0 0 3px var(--color-focus);
}

.data-table .editable-cell:empty::before {
    color: #64748b;
    content: attr(data-placeholder);
}

.readonly-cell {
    color: var(--color-text-muted);
    background: var(--color-surface-muted) !important;
    cursor: not-allowed;
}

.btn-remove {
    display: inline-grid;
    width: 44px;
    height: 44px;
    padding: 0;
    place-items: center;
    border: 1px solid #fecaca;
    border-radius: 6px;
    color: var(--color-danger);
    background: #fef2f2;
    font-size: 1rem;
    font-weight: 800;
}

.btn-remove:hover {
    border-color: var(--color-danger);
    background: #fee2e2;
}

input[type="checkbox"] {
    width: 18px;
    height: 18px;
    margin: 0;
    accent-color: var(--color-primary);
}

.search-row th {
    padding: 8px;
    border-bottom-color: var(--color-border-strong);
    background: #dbeafe;
}

.search-input,
.filter-group input,
.filter-group select,
.field input {
    width: 100%;
    min-height: 44px;
    padding: 9px 11px;
    border: 1px solid var(--color-border-strong);
    border-radius: 6px;
    color: var(--color-text);
    background: var(--color-surface);
}

.search-input {
    min-width: 132px;
    font-size: 0.88rem;
}

.search-input::placeholder,
.filter-group input::placeholder {
    color: #64748b;
}

.search-input:focus,
.filter-group input:focus,
.filter-group select:focus,
.field input:focus {
    border-color: var(--color-interactive);
    box-shadow: 0 0 0 3px var(--color-focus);
    outline: none;
}

.validation-errors {
    display: none;
    margin: 14px 0;
    padding: 12px 14px;
    border: 1px solid #fca5a5;
    border-radius: 6px;
    color: #7f1d1d;
    background: #fef2f2;
}

.validation-errors .error-header {
    margin-bottom: 6px;
    font-weight: 800;
}

.validation-errors ul {
    margin: 0;
    padding-left: 20px;
}

.summary-table td:last-child,
.total-amount {
    color: var(--color-success);
    font-weight: 800;
}

.filter-row {
    display: flex;
    align-items: flex-end;
    flex-wrap: wrap;
    gap: 12px;
    margin-top: 16px;
}

.filter-row:first-of-type {
    margin-top: 12px;
}

.filter-group,
.field {
    display: flex;
    min-width: 180px;
    flex: 1 1 180px;
    flex-direction: column;
    gap: 6px;
}

.filter-group label,
.field label {
    color: var(--color-text);
    font-size: 0.88rem;
    font-weight: 700;
}

.dynamic-filters {
    display: none;
}

.dynamic-filters.active {
    display: flex;
}

.is-hidden {
    display: none;
}

.filter-hint,
.copy-status,
.status {
    color: var(--color-text-muted);
    font-size: 0.88rem;
}

.filter-hint {
    margin-top: 12px;
}

.results-section {
    min-height: 220px;
}

.results-header {
    margin-bottom: 14px;
}

#resultsContainer {
    border: 0;
    border-radius: 0;
}

.no-results,
.loading {
    min-height: 136px;
    display: grid;
    place-items: center;
    padding: 24px;
    border: 1px dashed var(--color-border-strong);
    border-radius: 6px;
    color: var(--color-text-muted);
    background: var(--color-surface-muted);
    text-align: center;
}

.loading {
    border-style: solid;
    color: var(--color-primary);
    font-weight: 700;
}

.summary-box {
    margin-bottom: 14px;
    padding: 16px 18px;
    border-radius: var(--radius);
    color: #ffffff;
    background: var(--color-primary);
}

.summary-box h3 {
    margin: 0 0 4px;
    color: #dbeafe;
    font-size: 0.92rem;
    font-weight: 700;
}

.summary-box .total-amount {
    margin: 0;
    color: #ffffff;
    font-size: 1.65rem;
}

.log-section {
    color: #e5e7eb;
    border-color: #1f2937;
    background: #111827;
    box-shadow: none;
}

.log-section h2 {
    color: #f9fafb;
}

.log-container,
.log-box {
    min-height: 140px;
    max-height: 320px;
    margin-top: 12px;
    overflow-y: auto;
    color: #e5e7eb;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.86rem;
    line-height: 1.65;
}

.log-container:empty::before,
.log-box:empty::before {
    color: #9ca3af;
    content: 'Chưa có nhật ký xử lý.';
}

.log-entry + .log-entry {
    margin-top: 6px;
}

.log-entry.info {
    color: #bfdbfe;
}

.log-entry.success {
    color: #bbf7d0;
}

.log-entry.error {
    color: #fecaca;
}

.log-entry.warning {
    color: #fde68a;
}

.log-timestamp {
    margin-right: 8px;
    color: #9ca3af;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
    margin-top: 14px;
}

.include-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 8px;
    margin-top: 16px;
}

.include-option {
    display: flex;
    min-height: 44px;
    align-items: center;
    gap: 9px;
    padding: 9px 11px;
    border: 1px solid var(--color-border);
    border-radius: 6px;
    color: var(--color-text);
    background: var(--color-surface);
    cursor: pointer;
}

.include-option:hover {
    border-color: var(--color-interactive);
    background: #eff6ff;
}

.actions {
    margin-top: 18px;
}

.actions .status {
    min-width: 180px;
    flex: 1;
}

.result-list {
    display: grid;
    gap: 8px;
    margin-top: 12px;
}

.result-item,
.error-box {
    padding: 11px 12px;
    border-radius: 6px;
}

.result-item {
    border: 1px solid var(--color-border);
    background: var(--color-surface-muted);
}

.result-item a {
    font-weight: 700;
    text-decoration: none;
}

.result-item a:hover {
    text-decoration: underline;
}

.error-box {
    border: 1px solid #fca5a5;
    color: #7f1d1d;
    background: #fef2f2;
    overflow-wrap: anywhere;
}

:where(a, button, input, select, [contenteditable="true"]):focus-visible {
    outline: 3px solid var(--color-focus);
    outline-offset: 2px;
}

@media (max-width: 768px) {
    body {
        padding: 16px 12px 28px;
    }

    .app-header {
        gap: 16px;
        margin-bottom: 18px;
    }

    .page-heading h1 {
        font-size: 1.7rem;
    }

    .top-nav {
        margin-right: -12px;
        padding-right: 12px;
    }

    .app-main {
        gap: 16px;
    }

    .instructions,
    .data-section,
    .summary-section,
    .filter-section,
    .results-section,
    .panel,
    .log-section {
        padding: 16px;
    }

    .section-heading,
    .results-header,
    .submit-container {
        align-items: stretch;
        flex-direction: column;
    }

    .action-buttons,
    .results-actions,
    .actions {
        align-items: stretch;
        flex-direction: column;
    }

    .submit-btn,
    .btn-primary,
    .btn-secondary,
    .btn-add,
    .btn-delete-selected,
    .btn-clear-all,
    .btn-search,
    .btn-copy-image,
    .btn-clear-log {
        width: 100%;
        min-height: 44px;
    }

    .status-message {
        width: 100%;
    }

    .filter-row,
    .dynamic-filters.active {
        align-items: stretch;
        flex-direction: column;
    }

    .filter-group,
    .field {
        width: 100%;
        min-width: 0;
        flex: 0 0 auto;
    }

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

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

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

@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }

    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
    }
}
