:root {
    color-scheme: light;
    --bg: #f5f7fa;
    --surface: #ffffff;
    --surface-muted: #f8fafc;
    --text: #18212f;
    --muted: #647184;
    --border: #dbe1e9;
    --border-strong: #c8d0dc;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-soft: #eaf1ff;
    --success: #178447;
    --success-soft: #e8f7ef;
    --warning: #ad6500;
    --warning-soft: #fff4dd;
    --danger: #c0362c;
    --danger-soft: #fff0ee;
    --shadow: 0 8px 28px rgba(24, 33, 47, 0.08);
    --sidebar-width: 224px;
}

* {
    box-sizing: border-box;
}

html {
    min-height: 100%;
    background: var(--bg);
}

body {
    min-width: 320px;
    min-height: 100vh;
    margin: 0;
    color: var(--text);
    background: var(--bg);
    font-family: Inter, "PingFang SC", "Microsoft YaHei", Arial, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    letter-spacing: 0;
}

button,
input,
select,
textarea {
    font: inherit;
    letter-spacing: 0;
}

button {
    cursor: pointer;
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.55;
}

input,
select,
textarea {
    width: 100%;
    min-height: 42px;
    border: 1px solid var(--border-strong);
    border-radius: 5px;
    outline: none;
    color: var(--text);
    background: var(--surface);
    padding: 9px 11px;
    transition: border-color 120ms ease, box-shadow 120ms ease;
}

textarea {
    min-height: 84px;
    resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.13);
}

label {
    display: grid;
    gap: 6px;
    min-width: 0;
}

label > span,
.field-label {
    color: #445064;
    font-size: 12px;
    font-weight: 700;
}

h1,
h2,
p {
    margin-top: 0;
}

.muted {
    color: var(--muted);
    font-size: 12px;
}

em {
    color: var(--muted);
    font-style: normal;
}

.empty-table {
    color: var(--muted);
    padding: 28px 12px;
    text-align: center;
}

h1,
h2 {
    letter-spacing: 0;
}

h1 {
    margin-bottom: 2px;
    font-size: 22px;
    line-height: 1.25;
}

h2 {
    margin-bottom: 4px;
    font-size: 16px;
    line-height: 1.35;
}

p {
    color: var(--muted);
    margin-bottom: 0;
}

.is-hidden {
    display: none !important;
}

.login-shell {
    display: grid;
    min-height: 100vh;
    place-items: center;
    padding: 24px;
    background: #eef2f7;
}

.login-panel {
    width: min(100%, 380px);
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    box-shadow: var(--shadow);
    padding: 34px;
}

.login-panel .brand-mark {
    margin-bottom: 18px;
}

.login-panel h1 {
    font-size: 24px;
}

.login-panel > p {
    margin-bottom: 24px;
}

.login-panel form {
    display: grid;
    gap: 16px;
}

.brand-mark {
    display: grid;
    width: 46px;
    height: 46px;
    place-items: center;
    border-radius: 6px;
    color: #fff;
    background: var(--primary);
    font-size: 22px;
    font-weight: 800;
}

.brand-mark.small {
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
    font-size: 18px;
}

.primary-button,
.secondary-button,
.danger-button,
.text-button,
.icon-button,
.add-row-button {
    min-height: 40px;
    border-radius: 5px;
    padding: 9px 14px;
    font-weight: 700;
}

.primary-button {
    border: 1px solid var(--primary);
    color: #fff;
    background: var(--primary);
}

.primary-button:hover {
    background: var(--primary-dark);
}

.secondary-button {
    border: 1px solid #b9cae8;
    color: #1f4da5;
    background: #f4f7fd;
}

.secondary-button:hover {
    background: #eaf1ff;
}

.text-button {
    border: 1px solid transparent;
    color: #4e5d73;
    background: transparent;
}

.text-button:hover {
    color: var(--text);
    background: #edf1f6;
}

.danger-button {
    border: 1px solid var(--danger);
    color: #fff;
    background: var(--danger);
}

.icon-button {
    display: grid;
    width: 38px;
    min-height: 38px;
    place-items: center;
    border: 1px solid transparent;
    color: var(--muted);
    background: transparent;
    padding: 0;
    font-size: 24px;
    line-height: 1;
}

.icon-button:hover {
    color: var(--danger);
    background: var(--danger-soft);
}

.full-width {
    width: 100%;
}

.form-error {
    min-height: 20px;
    margin: 0;
    color: var(--danger);
    font-size: 13px;
}

.app-shell {
    min-height: 100vh;
}

.sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    display: flex;
    width: var(--sidebar-width);
    flex-direction: column;
    border-right: 1px solid var(--border);
    color: #263143;
    background: #fff;
    padding: 22px 14px 18px;
}

.brand-row {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 0 8px 24px;
}

.brand-row div:last-child {
    min-width: 0;
}

.brand-row strong,
.brand-row span {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.brand-row strong {
    font-size: 15px;
}

.brand-row span {
    color: var(--muted);
    font-size: 11px;
}

.main-nav {
    display: grid;
    gap: 5px;
}

.nav-button {
    display: flex;
    min-height: 44px;
    align-items: center;
    justify-content: flex-start;
    gap: 11px;
    border: 0;
    border-radius: 5px;
    color: #526076;
    background: transparent;
    padding: 10px 12px;
    font-weight: 700;
}

.nav-button:hover {
    color: var(--text);
    background: #f1f4f8;
}

.nav-button.is-active {
    color: #174ca7;
    background: var(--primary-soft);
}

.nav-icon {
    display: inline-grid;
    width: 22px;
    height: 22px;
    flex: 0 0 22px;
    place-items: center;
    font-size: 17px;
    font-weight: 800;
}

.logout-button {
    margin-top: auto;
    text-align: left;
}

.workspace {
    min-height: 100vh;
    margin-left: var(--sidebar-width);
    padding-bottom: 40px;
}

.topbar {
    display: flex;
    min-height: 82px;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.96);
    padding: 16px clamp(20px, 3vw, 42px);
}

.topbar span {
    color: var(--muted);
    font-size: 12px;
}

.loading-bar {
    position: fixed;
    inset: 0 0 auto var(--sidebar-width);
    z-index: 60;
    height: 3px;
    overflow: hidden;
    pointer-events: none;
}

.loading-bar::after {
    position: absolute;
    width: 38%;
    height: 100%;
    background: var(--primary);
    content: "";
    opacity: 0;
    transform: translateX(-120%);
}

.loading-bar.is-loading::after {
    animation: loading 900ms ease-in-out infinite;
    opacity: 1;
}

@keyframes loading {
    to { transform: translateX(350%); }
}

.view {
    display: none;
    width: min(1380px, 100%);
    margin: 0 auto;
    padding: 28px clamp(20px, 3vw, 42px);
}

.view.is-active {
    display: block;
}

.section-surface {
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    padding: 22px;
}

.entry-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 320px;
    align-items: start;
    gap: 20px;
}

.entry-main {
    min-width: 0;
}

.entry-side {
    position: sticky;
    top: 20px;
    display: grid;
    gap: 16px;
}

.section-heading {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 22px;
}

.section-heading p,
.account-form p,
.summary-filter p {
    font-size: 12px;
}

.form-grid {
    display: grid;
    gap: 14px;
}

.metadata-grid {
    grid-template-columns: repeat(4, minmax(130px, 1fr));
    margin-bottom: 24px;
}

.items-header,
.item-row {
    display: grid;
    grid-template-columns: minmax(180px, 1fr) minmax(120px, 180px) 42px;
    gap: 10px;
    align-items: center;
}

.items-header {
    color: var(--muted);
    padding: 0 2px 7px;
    font-size: 12px;
    font-weight: 700;
}

.items-list {
    display: grid;
    gap: 9px;
}

.item-row .remove-item {
    width: 42px;
    min-height: 42px;
}

.add-row-button {
    width: 100%;
    margin-top: 10px;
    border: 1px dashed #b7c1cf;
    color: #42516a;
    background: #fafbfc;
}

.add-row-button:hover {
    border-color: var(--primary);
    color: var(--primary-dark);
    background: var(--primary-soft);
}

.batch-totals {
    display: grid;
    gap: 10px;
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    padding: 14px 0;
}

.batch-totals > div,
.batch-totals > label {
    display: flex;
    min-height: 38px;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.batch-totals span {
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
}

.batch-totals strong {
    font-size: 17px;
}

.batch-totals .money-input {
    width: 126px;
    min-height: 36px;
    text-align: right;
}

.batch-totals .paid-total {
    margin-top: 3px;
    color: var(--primary-dark);
}

.batch-totals .paid-total strong {
    font-size: 21px;
}

.filters {
    display: grid;
    grid-template-columns: 150px 150px minmax(190px, 1fr) 130px auto auto;
    align-items: end;
    gap: 12px;
    margin-bottom: 18px;
}

.table-wrap {
    width: 100%;
    overflow: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
}

th,
td {
    border-bottom: 1px solid var(--border);
    padding: 13px 11px;
    text-align: left;
    vertical-align: top;
}

th {
    color: #566276;
    background: var(--surface-muted);
    font-size: 12px;
    font-weight: 800;
}

tbody tr:last-child td {
    border-bottom: 0;
}

.number-cell {
    text-align: right;
    white-space: nowrap;
}

.batch-code {
    color: var(--muted);
    font-family: Consolas, "Courier New", monospace;
    font-size: 11px;
}

.account-lines {
    display: grid;
    gap: 4px;
}

.account-lines span {
    display: flex;
    justify-content: space-between;
    gap: 12px;
}

.status-badge {
    display: inline-flex;
    min-height: 24px;
    align-items: center;
    border-radius: 4px;
    padding: 3px 8px;
    font-size: 12px;
    font-weight: 800;
    white-space: nowrap;
}

.status-badge.success {
    color: var(--success);
    background: var(--success-soft);
}

.status-badge.warning {
    color: var(--warning);
    background: var(--warning-soft);
}

.status-badge.danger {
    color: var(--danger);
    background: var(--danger-soft);
}

.table-actions {
    display: flex;
    gap: 2px;
}

.table-actions button {
    min-height: 32px;
    padding: 5px 8px;
    font-size: 12px;
}

.mobile-records {
    display: none;
}

.empty-state {
    border: 1px dashed var(--border-strong);
    border-radius: 6px;
    color: var(--muted);
    background: var(--surface);
    padding: 46px 20px;
    text-align: center;
}

.metric-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 14px;
    margin-bottom: 18px;
}

.metric-card {
    min-height: 126px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    padding: 20px;
}

.metric-card span {
    display: block;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
}

.metric-card strong {
    display: block;
    margin: 12px 0 2px;
    overflow-wrap: anywhere;
    font-size: 25px;
    line-height: 1.2;
}

.metric-card small {
    color: #7a8596;
}

.metric-card.primary {
    border-top: 3px solid var(--primary);
}

.metric-card.success {
    border-top: 3px solid var(--success);
}

.metric-card.warning {
    border-top: 3px solid #d49521;
}

.metric-card.neutral {
    border-top: 3px solid #7b8798;
}

.summary-filter {
    display: grid;
    grid-template-columns: minmax(180px, 1fr) 160px 160px auto;
    align-items: end;
    gap: 12px;
    margin-bottom: 18px;
}

.summary-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 18px;
}

.compact-table th,
.compact-table td {
    padding: 11px 9px;
}

.account-form {
    display: grid;
    grid-template-columns: minmax(220px, 1fr) minmax(220px, 1fr) 180px auto;
    align-items: end;
    gap: 14px;
    margin-bottom: 18px;
}

.account-list-section {
    min-height: 260px;
}

.account-list {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
}

.account-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 10px;
    min-height: 82px;
    border: 1px solid var(--border);
    border-radius: 5px;
    background: var(--surface-muted);
    padding: 13px;
}

.account-item strong,
.account-item span {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.account-item span {
    color: var(--muted);
    font-size: 12px;
}

.bottom-nav {
    display: none;
}

.modal {
    width: min(92vw, 560px);
    max-height: 90vh;
    border: 0;
    border-radius: 6px;
    box-shadow: 0 18px 60px rgba(15, 23, 42, 0.3);
    padding: 0;
}

.modal::backdrop {
    background: rgba(15, 23, 42, 0.46);
}

.small-modal {
    width: min(92vw, 420px);
}

.modal-content {
    display: grid;
    gap: 18px;
    padding: 22px;
}

.modal-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.toast {
    position: fixed;
    right: 22px;
    bottom: 22px;
    z-index: 100;
    max-width: min(380px, calc(100vw - 32px));
    border-radius: 5px;
    color: #fff;
    background: #263143;
    box-shadow: var(--shadow);
    padding: 12px 16px;
    opacity: 0;
    pointer-events: none;
    transform: translateY(12px);
    transition: opacity 160ms ease, transform 160ms ease;
}

.toast.is-visible {
    opacity: 1;
    transform: translateY(0);
}

.toast.is-error {
    background: var(--danger);
}

@media (max-width: 1120px) {
    .metadata-grid {
        grid-template-columns: repeat(2, minmax(150px, 1fr));
    }

    .filters {
        grid-template-columns: repeat(2, minmax(150px, 1fr));
    }

    .filter-search {
        grid-column: span 2;
    }

    .metric-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .account-list {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 840px) {
    .sidebar {
        display: none;
    }

    .workspace {
        margin-left: 0;
        padding-bottom: calc(82px + env(safe-area-inset-bottom));
    }

    .loading-bar {
        left: 0;
    }

    .topbar {
        min-height: 68px;
        padding: 12px 16px;
    }

    .topbar h1 {
        font-size: 19px;
    }

    .view {
        padding: 14px 12px 22px;
    }

    .section-surface {
        padding: 16px;
    }

    .entry-layout {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .entry-side {
        position: static;
    }

    .bottom-nav {
        position: fixed;
        inset: auto 0 0;
        z-index: 50;
        display: grid;
        grid-auto-columns: minmax(0, 1fr);
        grid-auto-flow: column;
        border-top: 1px solid var(--border);
        background: rgba(255, 255, 255, 0.98);
        padding: 5px 6px calc(5px + env(safe-area-inset-bottom));
    }

    .bottom-nav .nav-button {
        min-height: 54px;
        flex-direction: column;
        justify-content: center;
        gap: 1px;
        padding: 3px 2px;
        font-size: 11px;
    }

    .bottom-nav .nav-icon {
        height: 24px;
    }

    .desktop-only,
    .desktop-records {
        display: none !important;
    }

    .mobile-records {
        display: grid;
        gap: 10px;
    }

    .record-card {
        border: 1px solid var(--border);
        border-radius: 6px;
        background: var(--surface);
        padding: 15px;
    }

    .record-card-header,
    .record-card-total,
    .record-card-actions {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
    }

    .record-card-header {
        margin-bottom: 12px;
    }

    .record-card-items {
        display: grid;
        gap: 7px;
        border-top: 1px solid var(--border);
        border-bottom: 1px solid var(--border);
        padding: 11px 0;
    }

    .record-card-items div,
    .record-card-total {
        display: flex;
        justify-content: space-between;
        gap: 12px;
    }

    .record-card-total {
        padding: 12px 0 6px;
    }

    .record-card-actions {
        justify-content: flex-end;
    }

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

    .summary-filter {
        grid-template-columns: repeat(2, 1fr);
    }

    .summary-filter > div {
        grid-column: 1 / -1;
    }

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

    .account-form > div {
        grid-column: 1 / -1;
    }
}

@media (max-width: 560px) {
    body {
        font-size: 13px;
    }

    .login-shell {
        padding: 14px;
    }

    .login-panel {
        padding: 26px 20px;
    }

    .section-heading {
        align-items: center;
        margin-bottom: 18px;
    }

    .metadata-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
    }

    .items-header,
    .item-row {
        grid-template-columns: minmax(0, 1fr) 112px 38px;
        gap: 7px;
    }

    .item-row .remove-item {
        width: 38px;
    }

    .filters,
    .summary-filter,
    .account-form {
        grid-template-columns: 1fr 1fr;
    }

    .filter-search,
    .account-form > div,
    .account-form label:first-of-type,
    .account-form button {
        grid-column: 1 / -1;
    }

    .metric-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }

    .metric-card {
        min-height: 108px;
        padding: 14px;
    }

    .metric-card strong {
        margin-top: 10px;
        font-size: 19px;
    }

    .account-list {
        grid-template-columns: 1fr;
    }

    .modal-content {
        padding: 18px;
    }

    .toast {
        right: 16px;
        bottom: calc(74px + env(safe-area-inset-bottom));
    }
}

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

    .items-header {
        display: none;
    }

    .item-row {
        grid-template-columns: minmax(0, 1fr) 104px 38px;
    }

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

body {
    min-width: 0;
}

.account-lines span > b,
.record-card-items div > span {
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.account-lines span > em,
.record-card-items div > strong {
    flex: 0 0 auto;
}

.compact-table td:first-child {
    overflow-wrap: anywhere;
}

@media (max-width: 1280px) {
    .entry-layout {
        grid-template-columns: 1fr;
    }

    .entry-side {
        position: static;
    }
}

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

    .summary-filter,
    .account-form {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .summary-filter > div,
    .account-form > div {
        grid-column: 1 / -1;
    }
}

#current-user-label {
    color: #315b9c;
    margin-top: 2px;
}

.user-form {
    display: grid;
    grid-template-columns: minmax(180px, 1fr) minmax(160px, 1fr) minmax(180px, 1fr) 140px auto;
    align-items: end;
    gap: 14px;
    margin-bottom: 18px;
}

.user-form-heading p {
    font-size: 12px;
}

.user-form-actions,
.user-actions,
.user-item-title {
    display: flex;
    align-items: center;
    gap: 8px;
}

.user-form-actions {
    min-height: 42px;
}

.user-form-actions button {
    white-space: nowrap;
}

.user-list-section {
    min-height: 260px;
}

.user-list {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.user-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 12px;
    min-height: 88px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface-muted);
    padding: 14px;
}

.user-item.is-disabled {
    background: #f3f4f6;
    opacity: 0.78;
}

.user-item-main {
    min-width: 0;
}

.user-item-title {
    flex-wrap: wrap;
}

.user-item-title strong {
    min-width: 0;
    overflow-wrap: anywhere;
}

.role-badge,
.current-user-badge {
    display: inline-flex;
    min-height: 22px;
    align-items: center;
    border-radius: 4px;
    padding: 2px 7px;
    font-size: 11px;
    font-weight: 800;
}

.role-badge {
    color: #315b9c;
    background: #eaf1ff;
}

.current-user-badge {
    color: #5b6472;
    background: #e7ebf0;
}

.user-item-meta {
    display: block;
    color: var(--muted);
    margin-top: 7px;
    font-size: 12px;
}

.user-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
}

.user-actions button {
    min-height: 34px;
    padding: 6px 9px;
}

@media (max-width: 1120px) {
    .user-form {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .user-form-heading,
    .user-form-actions {
        grid-column: 1 / -1;
    }

    .user-list {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 560px) {
    .user-form {
        grid-template-columns: 1fr;
    }

    .user-form-heading,
    .user-form-actions,
    .user-form label {
        grid-column: 1 / -1;
    }

    .user-item {
        grid-template-columns: 1fr;
    }

    .user-actions {
        justify-content: flex-start;
    }
}
