:root {
    --bg: #f6f7f5;
    --surface: #ffffff;
    --text: #171a18;
    --muted: #68706a;
    --line: #dde2dc;
    --line-strong: #c7cfc8;
    --accent: #1f6f5b;
    --zero-bg: #f2f4f1;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    color: var(--text);
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
    background: var(--bg);
}

.page {
    width: 100%;
    padding: 42px 36px 72px;
}

.workspace {
    max-width: 1380px;
    margin: 0 auto;
}

.page-header {
    display: grid;
    grid-template-columns: minmax(280px, 1fr) auto auto auto;
    gap: 14px;
    align-items: end;
    margin-bottom: 22px;
}

h1,
h2,
p {
    margin: 0;
}

h1 {
    font-size: 24px;
    line-height: 1.2;
    font-weight: 700;
    letter-spacing: 0;
}

.page-header p {
    margin-top: 5px;
    color: var(--muted);
    font-size: 13px;
}

.page-header .refresh-meta {
    color: var(--accent);
}

.date-control,
.select-control {
    display: grid;
    gap: 6px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

input[type="date"],
input[type="password"],
input[type="text"],
select,
button {
    height: 38px;
    border: 1px solid var(--line-strong);
    border-radius: 6px;
    font: inherit;
    background: var(--surface);
}

input[type="date"] {
    min-width: 160px;
    padding: 0 10px;
    color: var(--text);
}

input[type="password"],
input[type="text"] {
    min-width: 180px;
    padding: 0 10px;
    color: var(--text);
}

select {
    min-width: 112px;
    padding: 0 34px 0 10px;
    color: var(--text);
}

button {
    padding: 0 16px;
    color: #fff;
    border-color: var(--accent);
    background: var(--accent);
    cursor: pointer;
}

button:disabled {
    cursor: progress;
    opacity: 0.72;
}

button:hover,
input[type="date"]:focus,
input[type="password"]:focus,
input[type="text"]:focus,
select:focus {
    outline: 2px solid rgba(31, 111, 91, 0.22);
    outline-offset: 2px;
}

.header-actions {
    display: flex;
    gap: 8px;
    align-items: end;
}

.icon-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    padding: 0;
    border: 1px solid var(--accent);
    border-radius: 6px;
    background: var(--accent);
}

.icon-button img {
    display: block;
    width: 20px;
    height: 20px;
    filter: invert(1);
}

.refresh-status {
    display: none;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 14px;
    align-items: center;
    margin: -8px 0 20px;
    padding: 12px 14px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.refresh-status.is-visible {
    display: grid;
}

.refresh-status strong,
.refresh-status span {
    display: block;
}

.refresh-status strong {
    margin-bottom: 3px;
}

.refresh-status span {
    color: var(--muted);
}

.refresh-status .refresh-error {
    margin-top: 4px;
    color: #a33a2b;
}

.refresh-spinner {
    display: none;
    width: 22px;
    height: 22px;
    border: 3px solid rgba(31, 111, 91, 0.18);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: refresh-spin 800ms linear infinite;
}

.refresh-status.is-running .refresh-spinner {
    display: block;
}

.refresh-status.is-failed {
    border-color: #d69b91;
    background: #fff8f6;
}

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

.matrix-wrap {
    overflow-x: auto;
    overflow-y: hidden;
    padding: 6px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
}

table {
    border-collapse: separate;
    border-spacing: 0;
    width: 100%;
}

.matrix {
    min-width: 1120px;
}

.matrix th,
.matrix td,
.reconciliation th,
.reconciliation td {
    height: 42px;
    padding: 0 12px;
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
}

.matrix thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-align: right;
    background: #fbfcfb;
    border-bottom: 1px solid var(--line-strong);
}

.matrix thead .row-heading {
    text-align: left;
}

.matrix .row-heading,
.matrix .row-label {
    position: sticky;
    left: 0;
    z-index: 2;
    width: 180px;
    min-width: 180px;
    max-width: 180px;
    text-align: left;
    background: var(--surface);
}

.matrix thead .row-heading {
    z-index: 3;
    background: #fbfcfb;
}

.matrix .row-label {
    color: var(--text);
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
}

.matrix td {
    min-width: 150px;
    color: var(--muted);
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.matrix tr:last-child th,
.matrix tr:last-child td,
.reconciliation tr:last-child th,
.reconciliation tr:last-child td {
    border-bottom: 0;
}

.matrix td.filled,
.reconciliation td {
    font-weight: 700;
}

.matrix td.zero {
    color: var(--muted);
    background: var(--zero-bg);
}

.matrix td.relationship,
.reconciliation td.relationship {
    position: relative;
    padding-left: 40px;
    color: var(--relationship-text);
    background: var(--relationship-bg);
    cursor: pointer;
}

.cell-value {
    display: block;
}

.relationship-detail-link {
    position: absolute;
    top: 50%;
    left: 10px;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    visibility: hidden;
    background: transparent;
    border: 0;
    cursor: pointer;
    opacity: 0;
    transform: translateY(-50%);
}

.relationship-detail-link img {
    display: block;
    width: 20px;
    height: 20px;
    opacity: 0.9;
}

.relationship-hover .relationship-detail-link {
    visibility: visible;
    opacity: 1;
}

.relationship-detail-link:hover,
.relationship-detail-link:focus {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

.matrix td.relationship-hover,
.reconciliation td.relationship-hover {
    z-index: 4;
}

.matrix td.relationship-hover::after,
.reconciliation td.relationship-hover::after {
    position: absolute;
    inset: 0;
    border: 2px solid var(--accent);
    content: "";
    pointer-events: none;
}

.summary {
    margin-top: 28px;
    max-width: 760px;
}

h2 {
    margin-bottom: 10px;
    font-size: 16px;
    line-height: 1.3;
    font-weight: 700;
}

.reconciliation {
    width: 100%;
    overflow: hidden;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.reconciliation th {
    width: 46%;
    color: var(--text);
    text-align: left;
    font-weight: 600;
    line-height: 1.35;
    white-space: normal;
}

.reconciliation th,
.reconciliation td {
    height: auto;
    min-height: 42px;
    padding-top: 10px;
    padding-bottom: 10px;
    vertical-align: top;
}

.reconciliation td {
    text-align: right;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.detail-page-header {
    grid-template-columns: minmax(280px, 1fr) auto auto;
}

.back-link {
    display: inline-block;
    margin-bottom: 8px;
    color: var(--accent);
    font-size: 13px;
    font-weight: 700;
    text-decoration: none;
}

.back-link:hover,
.back-link:focus {
    text-decoration: underline;
}

.relationship-overview {
    margin-bottom: 28px;
    overflow: hidden;
    color: var(--relationship-text, var(--text));
    background: var(--relationship-bg, var(--surface));
    border: 1px solid var(--line);
    border-radius: 8px;
}

.relationship-overview summary {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(220px, 0.8fr) minmax(0, 1fr);
    cursor: pointer;
    list-style: none;
}

.relationship-overview summary::-webkit-details-marker {
    display: none;
}

.relationship-overview summary > div {
    padding: 14px 16px;
    border-right: 1px solid rgba(23, 26, 24, 0.12);
}

.relationship-overview summary > div:last-child {
    border-right: 0;
}

.relationship-overview .relationship-total {
    text-align: center;
}

.relationship-overview summary > div:last-child {
    text-align: right;
}

.relationship-overview span,
.relationship-overview strong,
.relationship-overview small {
    display: block;
}

.relationship-overview span {
    margin-bottom: 5px;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

.relationship-overview strong {
    margin-bottom: 5px;
    font-size: 20px;
    line-height: 1.2;
}

.relationship-overview small {
    color: var(--relationship-text, var(--muted));
    font-size: 12px;
    font-weight: 700;
    line-height: 1.35;
}

.currency-breakdown {
    border-top: 1px solid rgba(23, 26, 24, 0.16);
    background: rgba(255, 255, 255, 0.42);
}

.currency-breakdown-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(220px, 0.8fr) minmax(0, 1fr);
    align-items: stretch;
    border-bottom: 1px solid rgba(23, 26, 24, 0.1);
}

.currency-breakdown-row:last-child {
    border-bottom: 0;
}

.currency-breakdown-row > div {
    padding: 14px 16px;
    border-right: 1px solid rgba(23, 26, 24, 0.12);
}

.currency-breakdown-row > div:last-child {
    border-right: 0;
}

.currency-breakdown-row .relationship-total {
    text-align: center;
}

.currency-breakdown-row > div:last-child {
    text-align: right;
}

.detail-section {
    margin-top: 28px;
}

.section-heading {
    display: flex;
    gap: 12px;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

.section-heading h2 {
    margin-bottom: 0;
}

.secondary-button {
    height: 30px;
    padding: 0 12px;
    color: var(--accent);
    font-size: 13px;
    font-weight: 700;
    border-color: var(--line-strong);
    background: var(--surface);
}

.secondary-button:hover,
.secondary-button:focus {
    border-color: var(--accent);
}

.date-entry-group {
    margin-bottom: 10px;
    overflow: hidden;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.hide-zero-days .date-entry-group.zero-day {
    display: none;
}

.date-entry-group summary {
    display: grid;
    grid-template-columns: minmax(140px, 1fr) minmax(150px, auto) minmax(150px, auto) minmax(110px, auto);
    gap: 16px;
    align-items: center;
    padding: 13px 16px;
    cursor: pointer;
    list-style: none;
}

.date-entry-group summary::-webkit-details-marker {
    display: none;
}

.date-entry-group summary:hover,
.date-entry-group summary:focus {
    background: #fbfcfb;
}

.date-entry-group .date-total,
.date-entry-group summary span:last-child {
    text-align: right;
}

.date-entry-group summary strong {
    display: block;
    font-size: 16px;
}

.date-entry-group summary small {
    display: block;
    margin-bottom: 3px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
}

.detail-table-wrap {
    overflow-x: auto;
    background: var(--surface);
    border-top: 1px solid var(--line);
}

.detail-table {
    min-width: 1080px;
}

.detail-table th,
.detail-table td {
    padding: 10px 12px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: top;
}

.detail-table thead th {
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    background: #fbfcfb;
    border-bottom: 1px solid var(--line-strong);
}

.detail-table .customer-entry-row td {
    background: #f4f5f4;
}

.detail-table .unmatched-entry-row {
    color: var(--relationship-text, var(--text));
    background: var(--relationship-bg, #eef4f1);
}

.detail-table .unmatched-entry-row td {
    color: inherit;
    background: inherit;
}

.detail-table tr:last-child td {
    border-bottom: 0;
}

.detail-table .amount-cell {
    text-align: right;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.empty-detail {
    color: var(--muted);
    text-align: center;
    padding: 18px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.auth-page {
    display: grid;
    place-items: center;
    min-height: 100vh;
}

.auth-panel {
    width: min(100%, 380px);
    padding: 24px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.auth-panel h1 {
    margin-bottom: 18px;
}

.stacked-form,
.stacked-form label,
.admin-add-form label {
    display: grid;
    gap: 6px;
}

.stacked-form,
.admin-add-form {
    gap: 12px;
}

.stacked-form span,
.admin-add-form span {
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

.messages {
    display: grid;
    gap: 8px;
    margin-bottom: 16px;
}

.message {
    padding: 10px 12px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 6px;
}

.message.error {
    color: #8a2f24;
    background: #fff8f6;
    border-color: #d69b91;
}

.message.success {
    color: #1f6f5b;
    background: #f3fbf8;
    border-color: #a7d4c7;
}

.admin-workspace {
    max-width: 1120px;
}

.admin-page-header {
    grid-template-columns: minmax(280px, 1fr) auto;
}

.admin-section {
    margin-top: 22px;
}

.admin-add-form {
    display: grid;
    grid-template-columns: minmax(180px, 1fr) minmax(120px, 160px) minmax(180px, 1fr) auto;
    align-items: end;
    padding: 16px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.admin-setup-form {
    grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) auto;
}

.admin-table-wrap {
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.admin-table {
    min-width: 820px;
}

.admin-table th,
.admin-table td {
    padding: 12px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: middle;
}

.admin-table thead th {
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    background: #fbfcfb;
    border-bottom: 1px solid var(--line-strong);
}

.admin-table tr:last-child td {
    border-bottom: 0;
}

.inline-admin-form {
    display: flex;
    gap: 8px;
    align-items: center;
}

.danger-button {
    color: #fff;
    border-color: #9e3b30;
    background: #9e3b30;
}

@media (max-width: 800px) {
    .page {
        padding: 24px 14px 48px;
    }

    .page-header {
        grid-template-columns: 1fr;
        align-items: stretch;
    }

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

    .relationship-overview .relationship-total,
    .relationship-overview summary > div:last-child {
        text-align: left;
    }

    .relationship-overview summary > div {
        border-right: 0;
        border-bottom: 1px solid rgba(23, 26, 24, 0.12);
    }

    .relationship-overview summary > div:last-child {
        border-bottom: 0;
    }

    .currency-breakdown-row {
        grid-template-columns: 1fr;
    }

    .currency-breakdown-row > div {
        border-right: 0;
        border-bottom: 1px solid rgba(23, 26, 24, 0.08);
    }

    .currency-breakdown-row > div:last-child {
        border-bottom: 0;
    }

    .currency-breakdown-row .relationship-total,
    .currency-breakdown-row > div:last-child {
        text-align: left;
    }

    .date-entry-group summary {
        grid-template-columns: 1fr;
        gap: 5px;
    }

    .date-entry-group .date-total,
    .date-entry-group summary span:last-child {
        text-align: left;
    }

    .section-heading {
        align-items: stretch;
        flex-direction: column;
    }

    button,
    input[type="password"],
    input[type="text"],
    input[type="date"],
    select {
        width: 100%;
    }

    .header-actions,
    .inline-admin-form {
        align-items: stretch;
        flex-direction: column;
    }

    .icon-button {
        width: 100%;
    }

    .admin-page-header,
    .admin-add-form {
        grid-template-columns: 1fr;
    }

    .matrix .row-heading,
    .matrix .row-label {
        width: 150px;
        min-width: 150px;
        max-width: 150px;
    }
}
