:root {
    --bg: #f4f6fa;
    --surface: #ffffff;
    --surface-alt: #f9fafc;
    --border: #e3e8ef;
    --text: #1a2233;
    --muted: #64708a;
    --primary: #1f5eff;
    --primary-hover: #1749cc;
    --error-bg: #fdecec;
    --error-fg: #a32020;
    --success-bg: #e6f6ec;
    --success-fg: #1d6b3a;
    --warning-bg: #fff4e0;
    --warning-fg: #8a5a00;
    --radius: 10px;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

a:hover {
    text-decoration: underline;
}

code {
    font: .8rem/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
    color: var(--muted);
}

main {
    max-width: 1280px;
    margin: 0 auto;
    padding: 24px 16px 48px;
}

nav {
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 0 24px;
    height: 56px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

nav .brand {
    font-weight: 700;
    color: var(--text);
}

nav .brand span {
    font-weight: 400;
    color: var(--muted);
}

nav .links {
    display: flex;
    gap: 4px;
}

nav .links a {
    padding: 6px 12px;
    border-radius: 6px;
    color: var(--muted);
    font-weight: 500;
}

nav .links a:hover {
    background: var(--surface-alt);
    text-decoration: none;
}

nav .links a.active {
    background: #eaf0ff;
    color: var(--primary);
}

nav .user {
    margin-left: auto;
    font-size: .85rem;
    color: var(--muted);
}

form.login {
    display: flex;
    flex-direction: column;
    gap: 16px;
    max-width: 360px;
    margin: 15vh auto 0;
}

form.login h1 {
    margin: 0;
    font-size: 1.5rem;
}

form.login .error {
    margin: 0;
}

.page-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 20px;
}

.page-head h1 {
    margin: 0;
    font-size: 1.5rem;
    letter-spacing: -0.01em;
    display: flex;
    align-items: center;
    gap: 12px;
}

.page-head p {
    margin: 4px 0 0;
    color: var(--muted);
}

.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
}

.page-head > .toolbar {
    flex-wrap: nowrap;
    flex-shrink: 0;
}

.fields .wide {
    grid-column: span 2;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 1px 2px rgba(16, 24, 40, .04);
}

.card + .card, .card + section, section + .card, section + section {
    margin-top: 20px;
}

.card.padded, form.card {
    padding: 24px;
}

fieldset {
    border: 0;
    margin: 0;
    padding: 0;
    min-width: 0;
}

.section-title {
    margin: 0 0 12px;
    font-size: .75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--muted);
}

.section-title span {
    text-transform: none;
    letter-spacing: 0;
    font-weight: 400;
}

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

.card-head .section-title {
    margin: 0;
}

.fields {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 16px;
}

.fields + .section-title, .limits + .section-title, .fees + .section-title {
    margin-top: 24px;
}

label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: .85rem;
    font-weight: 500;
}

label small {
    font-weight: 400;
    color: var(--muted);
}

input, select, textarea {
    font: inherit;
    height: 40px;
    padding: 0 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    width: 100%;
}

textarea {
    height: auto;
    padding: 8px 12px;
    resize: vertical;
}

input:focus, select:focus, textarea:focus {
    outline: 2px solid var(--primary);
    outline-offset: -1px;
    border-color: transparent;
}

fieldset:disabled input, fieldset:disabled select, fieldset:disabled textarea {
    background: var(--surface-alt);
    color: var(--text);
}

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

.limits {
    display: grid;
    grid-template-columns: minmax(160px, 1.4fr) repeat(3, minmax(100px, 1fr));
    gap: 10px 16px;
    align-items: center;
}

.limits .head {
    font-size: .75rem;
    color: var(--muted);
    font-weight: 600;
}

.limits .name {
    font-size: .85rem;
    font-weight: 500;
}

.fees .fee-row {
    display: grid;
    grid-template-columns: 2fr 1fr 1fr auto;
    gap: 10px 16px;
    align-items: center;
    margin-bottom: 10px;
}

.fees .fee-row code {
    grid-column: 1 / -1;
    margin-top: -6px;
}

button, .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font: inherit;
    font-weight: 600;
    height: 40px;
    padding: 0 20px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    white-space: nowrap;
}

button:hover, .button:hover {
    background: var(--primary-hover);
    text-decoration: none;
}

button:disabled {
    opacity: .6;
    cursor: progress;
}

button.secondary, .button.secondary {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}

button.secondary:hover, .button.secondary:hover {
    background: var(--surface-alt);
}

button.danger {
    background: var(--surface);
    color: var(--error-fg);
    border-color: #f3c4c4;
}

button.danger:hover {
    background: var(--error-bg);
}

button.small, .button.small {
    height: 30px;
    padding: 0 12px;
    font-size: .8rem;
}

.error, .flash {
    padding: 12px 16px;
    border-radius: 8px;
    background: var(--error-bg);
    color: var(--error-fg);
}

form .error {
    margin-top: 16px;
}

.flash {
    margin-bottom: 16px;
    white-space: pre-wrap;
}

.flash.success {
    background: var(--success-bg);
    color: var(--success-fg);
}

.empty {
    padding: 32px;
    text-align: center;
    color: var(--muted);
    margin: 0;
}

.summary {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 16px;
    margin: 20px 0 16px;
}

.stat {
    padding: 16px 20px;
}

.stat .label {
    font-size: .8rem;
    color: var(--muted);
}

.stat .value {
    margin-top: 4px;
    font-size: 1.3rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.stat .value small {
    font-size: .8rem;
    font-weight: 500;
    color: var(--muted);
    margin-left: 4px;
}

.facts {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px 24px;
    margin: 0;
}

.facts dt {
    font-size: .8rem;
    color: var(--muted);
}

.facts dd {
    margin: 2px 0 0;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
    overflow-wrap: anywhere;
}

.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-variant-numeric: tabular-nums;
}

th, td {
    padding: 10px 14px;
    white-space: nowrap;
}

th {
    font-size: .72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--muted);
    background: var(--surface-alt);
    border-bottom: 1px solid var(--border);
}

td {
    border-bottom: 1px solid var(--border);
}

tbody tr:hover td {
    background: #f9fbff;
}

tbody tr.link {
    cursor: pointer;
}

tbody tr.reversed td {
    color: var(--muted);
    text-decoration: line-through;
}

tbody tr.reversed td:last-child, tbody tr.reversed td .badge {
    text-decoration: none;
}

tfoot td {
    font-weight: 600;
    border-bottom: 0;
    background: var(--surface-alt);
}

.num {
    text-align: right;
}

.text {
    text-align: left;
}

.center {
    text-align: center;
}

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

.badge {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 999px;
    font-size: .75rem;
    font-weight: 600;
    background: #eef1f6;
    color: var(--muted);
}

.badge.partially-paid, .badge.approved, .badge.pending-approval, .badge.pending-disbursement {
    background: var(--warning-bg);
    color: var(--warning-fg);
}

.badge.paid, .badge.active, .badge.applied {
    background: var(--success-bg);
    color: var(--success-fg);
}

.badge.late, .badge.active-in-arrears, .badge.rejected {
    background: var(--error-bg);
    color: var(--error-fg);
}

.badge.draft {
    background: #eaf0ff;
    color: var(--primary);
}

.chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 12px;
    font-size: .85rem;
}

.chips a {
    padding: 2px 10px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
}

.inline-result {
    font-size: .85rem;
    color: var(--muted);
}

.inline-result b {
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

#apiLog {
    margin-top: 32px;
}

#apiLog > summary {
    cursor: pointer;
    font-weight: 600;
    color: var(--muted);
    padding: 8px 0;
}

#apiLog .entry {
    border-top: 1px solid var(--border);
}

#apiLog .entry summary {
    cursor: pointer;
    padding: 8px 12px;
    font: .8rem/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
}

#apiLog .entry summary .status-ok {
    color: var(--success-fg);
}

#apiLog .entry summary .status-error {
    color: var(--error-fg);
}

#apiLog pre {
    margin: 0 12px 12px;
    padding: 12px;
    max-height: 360px;
    overflow: auto;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: .75rem;
}
