:root {
    --blue-700: #1d4ed8;
    --indigo-600: #4f46e5;
    --blue-600: #2563eb;
    --blue-50: #eff6ff;
    --violet-600: #7c3aed;
    --violet-50: #f5f3ff;
    --amber-500: #f59e0b;
    --amber-50: #fffbeb;
    --orange-500: #f59e0b;
    --green-600: #16a34a;
    --green-50: #f0fdf4;
    --red-600: #dc2626;
    --red-50: #fef2f2;
    --sky-600: #0284c7;
    --ink: #1e293b;
    --ink-soft: #475569;
    --ink-muted: #94a3b8;
    --line: #e2e8f0;
    --surface: #ffffff;
    --page: #f4f6fb;
    --radius-lg: 18px;
    --radius-md: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 4px 16px rgba(15, 23, 42, .05);
    --font: "Kantumruy Pro", "Inter", "Khmer OS Battambang", system-ui, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
    margin: 0;
    background: var(--page);
    color: var(--ink);
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.6;
}

h1, h2, p { margin: 0; }
a { color: inherit; }

:focus-visible { outline: 3px solid rgba(37, 99, 235, .45); outline-offset: 2px; }

.visually-hidden {
    position: absolute !important;
    width: 1px; height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

.container { width: min(1380px, 100% - 48px); margin-inline: auto; }

/* Header */
.app-header {
    background: linear-gradient(100deg, var(--blue-700) 0%, var(--indigo-600) 55%, #1e40af 100%);
    color: #fff;
    box-shadow: 0 6px 20px rgba(30, 64, 175, .25);
}

.header-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    flex-wrap: wrap;
    padding: 18px 0;
}

.brand { display: flex; align-items: center; gap: 14px; }

.brand-icon {
    display: grid;
    place-items: center;
    width: 52px; height: 52px;
    border-radius: 14px;
    background: rgba(255, 255, 255, .16);
    font-size: 26px;
}

.brand h1 { font-size: 22px; font-weight: 700; line-height: 1.3; }
.brand p { font-family: "Inter", sans-serif; font-size: 13px; opacity: .85; }

.header-actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    border: 0;
    border-radius: var(--radius-sm);
    font: 600 14px/1.2 var(--font);
    text-decoration: none;
    cursor: pointer;
    transition: filter .15s, transform .15s;
}

.btn:hover { filter: brightness(1.07); }
.btn:active { transform: translateY(1px); }
.btn[disabled], .btn.is-busy { opacity: .7; cursor: progress; }

.btn-export { background: #5b4ff0; color: #fff; }
.btn-import { background: var(--orange-500); color: #fff; position: relative; }
.btn-add { background: #10b981; color: #fff; }
.btn-primary { background: var(--blue-600); color: #fff; padding-inline: 22px; }
.btn-ghost { background: #fff; color: var(--ink-soft); border: 1px solid var(--line); }

/* Page */
.page { padding: 56px 0 64px; display: grid; gap: 28px; }

/* Stat cards */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 24px; }

.stat-card {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 24px 26px;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
}

.stat-label { color: var(--ink-muted); font-size: 13px; font-weight: 600; }
.stat-value { font: 700 32px/1.3 "Inter", sans-serif; color: var(--ink); margin-top: 6px; }
.value-inbound { color: var(--sky-600); }
.value-outbound { color: var(--violet-600); }
.value-pending { color: var(--amber-500); }

.stat-icon {
    display: grid;
    place-items: center;
    width: 52px; height: 52px;
    border-radius: 14px;
    font-size: 22px;
}

.icon-total { background: var(--blue-50); color: var(--blue-600); }
.icon-inbound { background: #f0f9ff; color: var(--sky-600); }
.icon-outbound { background: var(--violet-50); color: var(--violet-600); }
.icon-pending { background: var(--amber-50); color: var(--amber-500); }

/* Toolbar */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    padding: 22px 24px;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
}

.tabs { display: inline-flex; padding: 6px; gap: 4px; background: #f1f5f9; border-radius: var(--radius-md); }

.tab {
    padding: 8px 18px;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--ink-soft);
    font: 500 15px/1.4 var(--font);
    cursor: pointer;
}

.tab.active { background: #fff; color: var(--blue-700); font-weight: 700; box-shadow: 0 1px 3px rgba(15, 23, 42, .12); }

.search {
    display: flex;
    align-items: center;
    gap: 10px;
    width: min(360px, 100%);
    padding: 0 14px;
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    color: var(--ink-muted);
}

.search:focus-within { border-color: var(--blue-600); box-shadow: 0 0 0 3px rgba(37, 99, 235, .12); }

.search input {
    flex: 1;
    min-width: 0;
    padding: 11px 0;
    border: 0;
    outline: 0;
    background: transparent;
    font: 400 14px var(--font);
    color: var(--ink);
}

/* Table */
.table-card { background: var(--surface); border-radius: var(--radius-lg); box-shadow: var(--shadow); overflow: hidden; }
.table-scroll { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; min-width: 1080px; }

thead th {
    padding: 16px 14px;
    background: #f8fafc;
    color: var(--ink-soft);
    font-size: 13px;
    font-weight: 600;
    text-align: left;
    border-bottom: 1px solid var(--line);
}

tbody td { padding: 18px 14px; border-bottom: 1px solid #f1f5f9; vertical-align: middle; font-size: 14px; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: #fafbff; }

.col-no { width: 76px; padding-left: 26px !important; }
.col-ref { width: 150px; }
.col-date { width: 120px; }
.col-type { width: 96px; }
.col-sender { width: 170px; }
.col-officer { width: 150px; }
.col-status { width: 160px; }
.col-actions { width: 150px; text-align: center !important; }

.num { font-weight: 700; color: var(--ink-soft); }
.strong { font-weight: 700; }
.muted { color: var(--ink-soft); font-family: "Inter", sans-serif; }
.officer { color: var(--blue-600); font-weight: 500; }

.subject {
    max-width: 380px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.table-message { padding: 48px 16px !important; text-align: center; color: var(--ink-muted); }

/* Pills and badges */
.type-pill {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
}

.type-inbound { background: var(--blue-50); color: var(--blue-700); }
.type-outbound { background: var(--violet-50); color: var(--violet-600); }

.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    border-radius: 999px;
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
}

.badge .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-pending { background: var(--amber-50); color: #b45309; }
.status-processing { background: var(--blue-50); color: var(--blue-700); }
.status-completed { background: var(--green-50); color: var(--green-600); }

/* Row actions */
.actions { display: inline-flex; gap: 8px; }

.action {
    display: grid;
    place-items: center;
    width: 36px; height: 36px;
    border: 0;
    border-radius: 10px;
    font-size: 15px;
    cursor: pointer;
    text-decoration: none;
}

.action-file { background: #f1f5f9; color: var(--ink-soft); }
.action-file.disabled { color: #cbd5e1; cursor: not-allowed; }
.action-edit { background: var(--blue-50); color: var(--blue-600); }
.action-delete { background: var(--red-50); color: var(--red-600); }
.action:not(.disabled):hover { filter: brightness(.95); }

/* Modal */
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 40;
    background: rgba(30, 41, 59, .45);
    backdrop-filter: blur(4px);
}

.modal {
    position: fixed;
    z-index: 50;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: min(580px, 100% - 32px);
    max-height: calc(100vh - 40px);
    overflow-y: auto;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: 0 24px 60px rgba(15, 23, 42, .25);
    animation: modal-in .18s ease-out;
}

@keyframes modal-in {
    from { opacity: 0; transform: translate(-50%, -46%); }
    to { opacity: 1; transform: translate(-50%, -50%); }
}

@media (prefers-reduced-motion: reduce) {
    .modal { animation: none; }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 26px;
    background: #f8fafc;
    border-bottom: 1px solid var(--line);
}

.modal-header h2 { font-size: 18px; font-weight: 700; }

.icon-button {
    display: grid;
    place-items: center;
    width: 36px; height: 36px;
    border: 0;
    border-radius: 50%;
    background: #e9eef5;
    color: var(--ink-soft);
    cursor: pointer;
}

.modal-body { padding: 24px 26px 8px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px 18px; }
.span-2 { grid-column: span 2; }

.field { display: grid; gap: 6px; }
.field > span { font-size: 13px; font-weight: 600; color: var(--ink-soft); }

.field input, .field select, .field textarea {
    width: 100%;
    padding: 11px 14px;
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    background: #fff;
    color: var(--ink);
    font: 400 15px/1.4 var(--font);
}

.field textarea { resize: vertical; min-height: 88px; }

.field input:focus, .field select:focus, .field textarea:focus {
    outline: 0;
    border-color: var(--blue-600);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

.file-box { padding: 10px 14px; border: 1px solid var(--line); border-radius: var(--radius-md); }
.file-box input { border: 0; padding: 0; }
.file-box input::file-selector-button {
    margin-right: 14px;
    padding: 8px 16px;
    border: 0;
    border-radius: var(--radius-sm);
    background: var(--blue-50);
    color: var(--blue-700);
    font: 600 13px var(--font);
    cursor: pointer;
}

.file-note { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-soft); flex-wrap: wrap; }
.file-note a { color: var(--blue-600); }
.file-note input[type="checkbox"] { width: auto; margin-left: 12px; }
.field-error { font-size: 13px; color: var(--red-600); }

.form-errors {
    display: flex;
    gap: 10px;
    margin-bottom: 18px;
    padding: 12px 16px;
    border-radius: var(--radius-md);
    background: var(--red-50);
    color: #b91c1c;
    font-size: 14px;
}

.form-errors ul { margin: 0; padding-left: 16px; }

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin: 12px 26px 0;
    padding: 18px 0 24px;
    border-top: 1px solid var(--line);
}

/* Toast */
.toast {
    position: fixed;
    right: 24px;
    bottom: 24px;
    z-index: 60;
    display: flex;
    gap: 12px;
    max-width: 440px;
    padding: 14px 18px;
    border-radius: var(--radius-md);
    background: #fff;
    box-shadow: 0 12px 32px rgba(15, 23, 42, .18);
    font-size: 14px;
    cursor: pointer;
}

.toast i { font-size: 18px; }
.toast-success { border-left: 4px solid var(--green-600); border-radius: 0 var(--radius-md) var(--radius-md) 0; }
.toast-success i { color: var(--green-600); }
.toast-error { border-left: 4px solid var(--red-600); border-radius: 0 var(--radius-md) var(--radius-md) 0; }
.toast-error i { color: var(--red-600); }
.toast-detail { font-size: 12px; color: var(--ink-soft); margin-top: 4px; }

/* Spinner */
.spinner {
    width: 14px; height: 14px;
    border: 2px solid rgba(255, 255, 255, .5);
    border-top-color: #fff;
    border-radius: 50%;
    display: inline-block;
    animation: spin .7s linear infinite;
    vertical-align: middle;
}

.spinner.dark { border-color: #cbd5e1; border-top-color: var(--blue-600); margin-right: 6px; }

@keyframes spin { to { transform: rotate(360deg); } }

/* Blazor error UI */
#blazor-error-ui {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 1000;
    padding: 12px 20px;
    background: #fef3c7;
    color: #92400e;
    box-shadow: 0 -1px 4px rgba(0, 0, 0, .15);
}

#blazor-error-ui .reload { margin-left: 8px; font-weight: 700; }
#blazor-error-ui .dismiss { position: absolute; right: 16px; top: 10px; cursor: pointer; }

/* Responsive */
@media (max-width: 1100px) {
    .stats { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 640px) {
    .container { width: calc(100% - 24px); }
    .page { padding-top: 24px; gap: 18px; }
    .stats { grid-template-columns: 1fr 1fr; gap: 12px; }
    .stat-card { padding: 16px; }
    .stat-icon { display: none; }
    .form-grid { grid-template-columns: 1fr; }
    .span-2 { grid-column: auto; }
    .header-actions .btn { flex: 1; justify-content: center; }
}

/* Top navigation */
.top-nav {
    background: #0f1e4a;
}

.top-nav-inner {
    display: flex;
    align-items: center;
    gap: 4px;
    min-height: 48px;
}

.nav-link {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    border-radius: var(--radius-sm);
    color: rgba(255, 255, 255, .72);
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
}

    .nav-link:hover {
        color: #fff;
        background: rgba(255, 255, 255, .08);
    }

    .nav-link.active {
        color: #fff;
        background: rgba(255, 255, 255, .15);
    }

/* Management pages */
.page-title-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    padding: 22px 24px;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
}

    .page-title-bar h1 {
        font-size: 20px;
        font-weight: 700;
    }

    .page-title-bar p {
        color: var(--ink-soft);
        font-size: 14px;
    }

.table-compact {
    min-width: 760px;
}

.status-inactive {
    background: #f1f5f9;
    color: var(--ink-muted);
}

.action[disabled] {
    opacity: .35;
    cursor: not-allowed;
}

.notice {
    padding: 12px 16px;
    border-radius: var(--radius-md);
    font-size: 14px;
}

.notice-success {
    background: var(--green-50);
    color: var(--green-600);
}

.notice-error {
    background: var(--red-50);
    color: #b91c1c;
}

.field-check {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 14px;
    color: var(--ink-soft);
    cursor: pointer;
}

    .field-check input {
        width: 18px;
        height: 18px;
    }

.hint {
    font-size: 12px;
    color: var(--ink-muted);
}

/* Login */
.login-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
    background: linear-gradient(135deg, var(--blue-700) 0%, var(--indigo-600) 60%, #1e3a8a 100%);
}

.login-card {
    display: grid;
    gap: 22px;
    width: min(420px, 100%);
    padding: 32px;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: 0 24px 60px rgba(15, 23, 42, .3);
}

.login-brand {
    display: flex;
    align-items: center;
    gap: 14px;
}

    .login-brand .brand-icon {
        flex-shrink: 0;
        background: var(--blue-50);
        color: var(--blue-600);
    }

    .login-brand h1 {
        font-size: 18px;
        font-weight: 700;
        line-height: 1.4;
    }

    .login-brand p {
        font-family: "Inter", sans-serif;
        font-size: 12px;
        color: var(--ink-muted);
    }

.login-form {
    display: grid;
    gap: 16px;
}

.login-card .form-errors {
    margin: 0;
    align-items: center;
}

.btn-block {
    width: 100%;
    justify-content: center;
    padding-block: 12px;
}

/* Signed-in user in the nav bar */
.user-menu {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 6px;
}

    .user-menu form {
        margin: 0;
    }

.user-name {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: #fff;
    font-size: 14px;
}

.nav-link.logout {
    border: 0;
    background: transparent;
    cursor: pointer;
    font-family: var(--font);
}

/* Users page */
.you-tag {
    margin-left: 6px;
    padding: 1px 8px;
    border-radius: 999px;
    background: var(--blue-50);
    color: var(--blue-700);
    font-size: 12px;
    font-weight: 600;
}

.action-key {
    background: var(--amber-50);
    color: #b45309;
}

/* Created by / updated by in the document form */
.audit-meta {
    display: grid;
    gap: 4px;
    margin-top: 18px;
    padding: 12px 14px;
    border-radius: var(--radius-md);
    background: #f8fafc;
    font-size: 13px;
    color: var(--ink-soft);
}

    .audit-meta i {
        margin-right: 4px;
        color: var(--ink-muted);
    }

/* Activity log page */
.log-filters {
    justify-content: flex-start;
}

    .log-filters .search {
        flex: 1;
        width: auto;
        min-width: 240px;
    }

.filter-input {
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    background: #fff;
    color: var(--ink);
    font: 400 14px var(--font);
}

.table-logs {
    min-width: 980px;
}

.col-time {
    width: 170px;
    white-space: nowrap;
}

.log-user small {
    display: block;
    font-size: 12px;
    color: var(--ink-muted);
}

.log-description {
    max-width: 440px;
    color: var(--ink-soft);
}

.log-danger {
    background: var(--red-50);
    color: var(--red-600);
}

.pager {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 24px;
    border-top: 1px solid var(--line);
    font-size: 14px;
    color: var(--ink-soft);
}

    .pager div {
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .pager .btn {
        padding: 8px 12px;
    }