:root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --text: #111827;
    --muted: #64748b;
    --line: #e2e8f0;
    --primary: #111827;
    --primary-2: #334155;
    --borrow: #dc2626;
    --borrow-bg: #fef2f2;
    --lend: #2563eb;
    --lend-bg: #eff6ff;
    --ok: #15803d;
    --ok-bg: #f0fdf4;
    --warn: #b45309;
    --warn-bg: #fffbeb;
    --shadow: 0 10px 30px rgba(15, 23, 42, .06);
    --radius: 18px;
}

* {
    box-sizing: border-box
}

html {
    background: var(--bg)
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
   font-family: "Helvetica Neue", Arial, "Yu Gothic Medium", "Yu Gothic", "游ゴシック体", "YuGothic", "游ゴシック", "Yu Gothic", "ヒラギノ角ゴ Pro W3", "Hiragino Kaku Gothic Pro", "メイリオ", "Meiryo", sans-serif !important;
    line-height: 1.6
}

a {
    color: inherit;
    text-decoration: none
}

button,
input,
textarea,
select {
    font: inherit
}

button {
    cursor: pointer
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    padding: 14px 28px;
    background: rgba(255, 255, 255, .94);
    backdrop-filter: blur(16px);
    border-bottom: 1px solid var(--line)
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 800;
    letter-spacing: .02em
}

.brand-mark {
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border-radius: 10px;
    background: var(--primary);
    color: #fff;
    font-weight: 900
}

.nav {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-wrap: wrap
}

.nav a {
    padding: 9px 12px;
    border-radius: 10px;
    color: var(--muted);
    font-size: 14px;
    font-weight: 700
}

.nav a:hover,
.nav a.active {
    background: #f1f5f9;
    color: var(--text)
}

.shell {
    width: min(1180px, calc(100% - 32px));
    margin: 28px auto 60px;
    max-width: 1100px;
    margin: auto;
}

.auth-shell {
    width: min(480px, calc(100% - 32px));
    margin-top: 9vh
}

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

.page-title {
    margin: 0;
    font-size: clamp(24px, 3vw, 34px);
    line-height: 1.25;
    letter-spacing: -.03em
}

.page-sub {
    margin: 6px 0 0;
    color: var(--muted);
    font-size: 14px
}

.actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 42px;
    padding: 9px 15px;
    border: 1px solid transparent;
    border-radius: 12px;
    background: var(--primary);
    color: #fff;
    font-weight: 800;
    font-size: 14px;
    transition: .15s ease
}

.btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 8px 20px rgba(15, 23, 42, .12)
}

.btn.secondary {
    background: #fff;
    color: var(--text);
    border-color: var(--line)
}

.btn.danger {
    background: #fff;
    color: var(--borrow);
    border-color: #fecaca
}

.btn.small {
    min-height: 34px;
    padding: 6px 10px;
    border-radius: 9px;
    font-size: 13px
}

.cards {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 14px;
    margin-bottom: 22px
}

.summary-card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow)
}

.summary-label {
    color: var(--muted);
    font-weight: 700;
    font-size: 13px;
    margin-bottom: 7px
}

.summary-value {
    font-size: clamp(24px, 3vw, 34px);
    font-weight: 600;
    letter-spacing: -.04em;
    line-height: 1.15
}

.summary-card.borrow .summary-value {
    color: var(--borrow)
}

.summary-card.lend .summary-value {
    color: var(--lend)
}

.summary-card.net .summary-value.positive {
    color: var(--lend)
}

.summary-card.net .summary-value.negative {
    color: var(--borrow)
}

.panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden
}

.panel+.panel {
    margin-top: 18px
}

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

.panel-title {
    font-size: 18px;
    font-weight: 600;
    margin: 0
}

.panel-body {
    padding: 20px
}

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

.search {
    position: relative;
    min-width: 230px;
    flex: 1
}

.search input {
    width: 100%;
    padding: 10px 12px 10px 38px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fff;
    color: var(--text);
    outline: none
}

.search:before {
    content: "⌕";
    position: absolute;
    left: 13px;
    top: 50%;
    transform: translateY(-52%);
    color: var(--muted);
    font-size: 20px
}

.search input:focus,
.field input:focus,
.field textarea:focus,
.field select:focus {
    border-color: #94a3b8;
    box-shadow: 0 0 0 3px rgba(148, 163, 184, .15)
}

.table-wrap {
    overflow: auto
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 860px
}

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

th {
    background: var(--surface-2);
    color: var(--muted);
    font-size: 12px;
    letter-spacing: .02em;
    white-space: nowrap
}

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

tbody tr.clickable {
    cursor: pointer;
    transition: .12s ease
}

tbody tr.clickable:hover {
    background: #f8fafc
}

.person-name {
    font-weight: 600;
    font-size: 16px
}

.person-note {
    margin-top: 3px;
    color: var(--muted);
    font-size: 12px;
    max-width: 250px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis
}

.amount {
    font-weight: 600;
    white-space: nowrap
}

.amount.borrow {
    color: var(--borrow)
}

.amount.lend {
    color: var(--lend)
}

.amount.zero {
    color: #94a3b8
}

.condition-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-width: 360px
}

.condition-item {
    display: flex;
    gap: 7px;
    align-items: flex-start;
    font-size: 13px
}

.condition-text {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden
}

.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    white-space: nowrap;
    padding: 3px 8px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 900
}

.badge.borrow {
    background: var(--borrow-bg);
    color: var(--borrow)
}

.badge.lend {
    background: var(--lend-bg);
    color: var(--lend)
}

.badge.paid {
    background: var(--ok-bg);
    color: var(--ok)
}

.badge.open {
    background: var(--warn-bg);
    color: var(--warn)
}

.empty {
    padding: 50px 20px;
    text-align: center;
    color: var(--muted)
}

.empty strong {
    display: block;
    color: var(--text);
    font-size: 18px;
    margin-bottom: 5px
}

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

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

.form-card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 22px;
    box-shadow: var(--shadow)
}

.form-card h2 {
    margin: 0 0 18px;
    font-size: 20px
}

.field {
    display: flex;
    flex-direction: column;
    gap: 7px;
    margin-bottom: 16px
}

.field:last-child {
    margin-bottom: 0
}

.field label {
    font-size: 13px;
    font-weight: 800
}

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

.field input,
.field textarea,
.field select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fff;
    color: var(--text);
    padding: 11px 12px;
    outline: none
}

.field textarea {
    min-height: 112px;
    resize: vertical
}

.segmented {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px
}

.segmented input {
    position: absolute;
    opacity: 0;
    pointer-events: none
}

.segmented label {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 48px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fff;
    font-weight: 600;
    cursor: pointer
}

.segmented input[value="borrowed"]:checked+label {
    background: var(--borrow-bg);
    border-color: #fecaca;
    color: var(--borrow)
}

.segmented input[value="lent"]:checked+label {
    background: var(--lend-bg);
    border-color: #bfdbfe;
    color: var(--lend)
}

.form-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    flex-wrap: wrap;
    margin-top: 22px
}

.error-box {
    margin-bottom: 18px;
    padding: 12px 14px;
    border: 1px solid #fecaca;
    border-radius: 12px;
    background: var(--borrow-bg);
    color: #991b1b;
    font-size: 14px
}

.flash {
    padding: 12px 14px;
    margin-bottom: 16px;
    border-radius: 12px;
    border: 1px solid var(--line);
    background: #fff;
    font-weight: 700;
    font-size: 14px
}

.flash.success {
    background: var(--ok-bg);
    border-color: #bbf7d0;
    color: #166534
}

.flash.error {
    background: var(--borrow-bg);
    border-color: #fecaca;
    color: #991b1b
}

.flash.info {
    background: var(--lend-bg);
    border-color: #bfdbfe;
    color: #1d4ed8
}

.auth-card {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 24px;
    padding: 28px;
    box-shadow: var(--shadow)
}

.auth-logo {
    display: flex;
    align-items: center;
    gap: 11px;
    font-size: 21px;
    font-weight: 600;
    margin-bottom: 22px
}

.auth-card h1 {
    margin: 0 0 6px;
    font-size: 28px;
    letter-spacing: -.03em
}

.auth-card p {
    margin: 0 0 22px;
    color: var(--muted);
    font-size: 14px
}

.loan-list {
    display: flex;
    flex-direction: column;
    gap: 14px
}

.loan-card {
    border: 1px solid var(--line);
    border-radius: 16px;
    overflow: hidden
}

.loan-card-head {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: flex-start;
    padding: 16px 18px;
    background: var(--surface-2);
    border-bottom: 1px solid var(--line)
}

.loan-card-title {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    font-weight: 900
}

.loan-card-meta {
    margin-top: 4px;
    color: var(--muted);
    font-size: 12px
}

.loan-stats {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0;
    border-bottom: 1px solid var(--line)
}

.loan-stat {
    padding: 14px 18px;
    border-right: 1px solid var(--line)
}

.loan-stat:last-child {
    border-right: 0
}

.loan-stat span {
    display: block;
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    margin-bottom: 2px
}

.loan-stat strong {
    font-size: 17px
}

.loan-details {
    padding: 16px 18px
}

.kv {
    display: grid;
    grid-template-columns: 110px 1fr;
    gap: 8px 16px;
    margin: 0
}

.kv dt {
    color: var(--muted);
    font-size: 12px;
    font-weight: 800
}

.kv dd {
    margin: 0;
    font-size: 14px;
    white-space: pre-wrap;
    overflow-wrap: anywhere
}

.payment-table {
    margin-top: 14px;
    border: 1px solid var(--line);
    border-radius: 12px;
    overflow: hidden
}

.payment-table table {
    min-width: 0
}

.payment-table th,
.payment-table td {
    padding: 10px 12px
}

.payment-table th {
    font-size: 11px
}

.loan-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    padding: 14px 18px;
    border-top: 1px solid var(--line);
    background: #fff
}

.inline-form {
    display: inline
}

.section-label {
    margin: 28px 0 10px;
    font-size: 14px;
    color: var(--muted);
    font-weight: 600;
    letter-spacing: .02em
}

.person-header {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: flex-start;
    margin-bottom: 18px
}

.person-header h1 {
    margin: 0;
    font-size: 30px;
    letter-spacing: -.03em
}

.person-header .note {
    margin-top: 5px;
    color: var(--muted);
    white-space: pre-wrap
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 18px;
    z-index: 100
}

.modal-backdrop.open {
    display: flex
}

.modal {
    width: min(520px, 100%);
    background: #fff;
    border-radius: 20px;
    border: 1px solid var(--line);
    box-shadow: 0 30px 80px rgba(15, 23, 42, .25);
    padding: 22px
}

.modal h3 {
    margin: 0 0 8px
}

.modal p {
    margin: 0 0 20px;
    color: var(--muted);
    font-size: 14px
}

.text-right {
    text-align: right
}

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

.nowrap {
    white-space: nowrap
}

@media(max-width:820px) {
    .topbar {
        padding: 12px 14px;
        align-items: flex-start;
        flex-direction: column;
        gap: 9px
    }

    .nav {
        width: 100%;
        overflow-x: auto;
        flex-wrap: nowrap;
        padding-bottom: 2px
    }

    .nav a {
        white-space: nowrap
    }

    .shell {
        width: min(100% - 20px, 1180px);
        margin-top: 18px
    }

    .cards,
    .grid-2,
    .grid-3 {
        grid-template-columns: 1fr
    }

    .page-head,
    .person-header {
        align-items: stretch;
        flex-direction: column
    }

    .page-head .actions,
    .person-header .actions {
        width: 100%
    }

    .page-head .actions .btn,
    .person-header .actions .btn {
        flex: 1
    }

    .loan-card-head {
        flex-direction: column
    }

    .loan-stats {
        grid-template-columns: 1fr 1fr
    }

    .loan-stat:nth-child(2) {
        border-right: 0
    }

    .loan-stat:nth-child(3) {
        grid-column: 1 / -1;
        border-right: 0;
        border-top: 1px solid var(--line)
    }

    .kv {
        grid-template-columns: 82px 1fr
    }

    .auth-shell {
        margin-top: 5vh
    }

    .auth-card {
        padding: 22px
    }
}

@media(max-width:520px) {
    .summary-card {
        padding: 17px
    }

    .btn {
        width: 100%
    }

    .btn.small {
        width: auto
    }

    .form-actions .btn {
        width: 100%
    }

    .segmented {
        grid-template-columns: 1fr
    }

    .loan-stats {
        grid-template-columns: 1fr
    }

    .loan-stat,
    .loan-stat:nth-child(2),
    .loan-stat:nth-child(3) {
        grid-column: auto;
        border-right: 0;
        border-top: 1px solid var(--line)
    }

    .loan-stat:first-child {
        border-top: 0
    }

    .payment-table {
        overflow: auto
    }

    .payment-table table {
        min-width: 520px
    }
}

/* ===== Compact mobile UI v2 ===== */
.mobile-label,
.nav-short {
    display: none
}

@media(max-width:700px) {
    :root {
        --radius: 14px;
        --shadow: 0 4px 16px rgba(15, 23, 42, .045)
    }

    body {
        font-size: 14px;
        line-height: 1.45;
        padding-bottom: 66px
    }

    /* Header: one slim bar + fixed bottom navigation */
    .topbar {
        height: 48px;
        padding: 7px 12px;
        display: flex;
        align-items: center;
        justify-content: flex-start;
        flex-direction: row;
        gap: 0
    }

    .brand {
        gap: 8px;
        font-size: 14px
    }

    .brand-mark {
        width: 30px;
        height: 30px;
        border-radius: 8px;
        font-size: 14px
    }

    .brand-text {
        white-space: nowrap
    }

    .nav {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 50;
        width: 100%;
        height: 60px;
        display: grid;
        grid-template-columns: repeat(5, 1fr);
        gap: 0;
        padding: 0 max(4px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-left));
        background: rgba(255, 255, 255, .97);
        border-top: 1px solid var(--line);
        box-shadow: 0 -6px 20px rgba(15, 23, 42, .06);
        overflow: visible
    }

    .nav a {
        min-width: 0;
        padding: 8px 2px 6px;
        border-radius: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 11px;
        line-height: 1;
        font-weight: 800;
        color: #64748b
    }

    .nav a.active {
        background: transparent;
        color: var(--text);
        box-shadow: inset 0 2px 0 var(--text)
    }

    .nav-full {
        display: none
    }

    .nav-short {
        display: inline
    }

    .shell {
        width: calc(100% - 20px);
        margin: 14px auto 24px;

    }

    .auth-shell {
        width: calc(100% - 24px);
        margin-top: 5vh;
        padding-bottom: 0
    }

    /* Page headings */
    .page-head {
        margin-bottom: 12px;
        gap: 10px;
        flex-direction: row;
        align-items: center
    }

    .page-head>div:first-child {
        min-width: 0;
        flex: 1
    }

    .page-title {
        font-size: 21px;
        line-height: 1.2
    }

    .page-sub {
        font-size: 11px;
        margin-top: 3px;
        line-height: 1.35
    }

    .page-head .actions {
        width: auto;
        flex: 0 0 auto
    }

    .page-head .actions .btn {
        width: auto;
        flex: none;
        min-height: 38px;
        padding: 8px 11px;
        font-size: 12px
    }

    .person-header {
        margin-bottom: 12px;
        gap: 9px;
        flex-direction: column
    }

    .person-header h1 {
        font-size: 22px
    }

    .person-header .note {
        font-size: 12px;
        margin-top: 2px
    }

    .person-header .actions {
        display: grid;
        grid-template-columns: 1fr 1.25fr;
        gap: 7px;
        width: 100%
    }

    .person-header .actions .btn {
        min-width: 0;
        width: 100%;
        min-height: 38px;
        padding: 7px 8px;
        font-size: 11px;
        white-space: nowrap
    }

    /* Three summary figures stay in one row */
    .cards {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 6px;
        margin-bottom: 12px
    }

    .summary-card {
        padding: 10px 8px;
        border-radius: 12px;
        min-width: 0;
        box-shadow: none
    }

    .summary-label {
        font-size: 10px;
        margin-bottom: 3px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis
    }

    .summary-value {
        font-size: clamp(14px, 4.6vw, 19px);
        letter-spacing: -.035em;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis
    }

    .desktop-label {
        display: none
    }

    .mobile-label {
        display: inline
    }

    /* Panels and search */
    .panel {
        border-radius: 13px;
        box-shadow: none
    }

    .panel-head {
        padding: 10px 11px;
        gap: 8px
    }

    .panel-title {
        font-size: 14px
    }

    .toolbar {
        flex: 1;
        justify-content: flex-end
    }

    .search {
        min-width: 0;
        max-width: 190px
    }

    .search input {
        height: 36px;
        padding: 7px 9px 7px 31px;
        border-radius: 9px;
        font-size: 13px
    }

    .search:before {
        left: 10px;
        font-size: 17px
    }

    .panel-body {
        padding: 12px
    }

    /* Dashboard: table becomes compact person cards */
    .table-wrap {
        overflow: visible
    }

    .dashboard-table {
        display: block;
        min-width: 0;
        width: 100%
    }

    .dashboard-table thead {
        display: none
    }

    .dashboard-table tbody {
        display: grid;
        gap: 7px;
        padding: 7px;
        background: #f8fafc
    }

    .dashboard-table tr {
        position: relative;
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 7px 10px;
        padding: 10px 32px 9px 10px;
        background: #fff;
        border: 1px solid var(--line);
        border-radius: 11px
    }

    .dashboard-table td {
        display: block;
        padding: 0;
        border: 0;
        min-width: 0
    }

    .dashboard-table td[data-label]::before {
        content: attr(data-label);
        display: block;
        margin-bottom: 1px;
        color: var(--muted);
        font-size: 9px;
        font-weight: 800;
        line-height: 1.2
    }

    .dashboard-table td:nth-child(1) {
        grid-column: 1/-1;
        padding-right: 2px
    }

    .dashboard-table td:nth-child(1)::before {
        display: none
    }

    .dashboard-table td:nth-child(4) {
        grid-column: 1/-1;
        padding-top: 2px;
        border-top: 1px solid #f1f5f9
    }

    .dashboard-table td:nth-child(5) {
        grid-column: 1/-1;
        display: flex;
        gap: 6px;
        align-items: center;
        color: var(--muted);
        font-size: 10px
    }

    .dashboard-table td:nth-child(5)::before {
        display: inline;
        margin: 0
    }

    .dashboard-table .row-chevron {
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
        font-size: 22px;
        color: #94a3b8
    }

    .person-name {
        font-size: 15px;
        line-height: 1.25
    }

    .person-note {
        font-size: 10px;
        margin-top: 2px;
        max-width: 100%
    }

    .dashboard-table .amount {
        font-size: 15px;
        line-height: 1.25
    }

    .condition-list {
        gap: 3px;
        max-width: none
    }

    .condition-item {
        font-size: 11px;
        gap: 5px
    }

    .condition-text {
        -webkit-line-clamp: 1
    }

    .badge {
        padding: 2px 6px;
        font-size: 9px
    }

    .empty {
        padding: 34px 14px;
        font-size: 12px
    }

    .empty strong {
        font-size: 15px
    }

    /* Loan detail cards */
    .section-label {
        margin: 16px 2px 7px;
        font-size: 11px
    }

    .loan-list {
        gap: 8px
    }

    .loan-card {
        border-radius: 12px
    }

    .loan-card-head {
        padding: 10px 11px;
        display: grid;
        grid-template-columns: 1fr auto;
        align-items: start;
        gap: 7px;
        flex-direction: unset
    }

    .loan-card-title {
        gap: 5px;
        font-size: 12px;
        line-height: 1.25
    }

    .loan-card-meta {
        display: none
    }

    .loan-card-head>.amount {
        font-size: 13px;
        align-self: center
    }

    .loan-stats {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        border-bottom: 1px solid var(--line)
    }

    .loan-stat,
    .loan-stat:nth-child(2),
    .loan-stat:nth-child(3) {
        grid-column: auto;
        padding: 8px 8px;
        border-top: 0;
        border-right: 1px solid var(--line)
    }

    .loan-stat:nth-child(3) {
        border-right: 0
    }

    .loan-stat span {
        font-size: 9px;
        margin-bottom: 1px;
        white-space: nowrap
    }

    .loan-stat strong {
        font-size: 13px;
        white-space: nowrap
    }

    .loan-details {
        padding: 10px 11px
    }

    .kv {
        grid-template-columns: 52px 1fr;
        gap: 5px 8px
    }

    .kv dt {
        font-size: 10px
    }

    .kv dd {
        font-size: 12px
    }

    .loan-details .section-label {
        margin: 12px 0 6px
    }

    .loan-actions {
        padding: 8px 10px;
        gap: 6px
    }

    .loan-actions .btn.small {
        min-height: 32px;
        padding: 5px 8px;
        font-size: 10px
    }

    /* Payment history: no horizontal scroll */
    .payment-table {
        margin-top: 7px;
        overflow: visible;
        border: 0;
        border-radius: 0
    }

    .payment-history-table {
        display: block;
        min-width: 0 !important
    }

    .payment-history-table thead {
        display: none
    }

    .payment-history-table tbody {
        display: grid;
        gap: 6px
    }

    .payment-history-table tr {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 4px 8px;
        padding: 8px;
        background: #f8fafc;
        border: 1px solid var(--line);
        border-radius: 9px
    }

    .payment-history-table td {
        display: block;
        padding: 0;
        border: 0;
        min-width: 0;
        font-size: 11px;
        white-space: normal
    }

    .payment-history-table td[data-label]::before {
        content: attr(data-label);
        display: block;
        color: var(--muted);
        font-size: 9px;
        font-weight: 800;
        margin-bottom: 1px
    }

    .payment-history-table td:nth-child(3) {
        grid-column: 1/-1
    }

    .payment-history-table .payment-actions-cell {
        grid-column: 1/-1;
        display: flex;
        justify-content: flex-end;
        gap: 5px;
        padding-top: 4px;
        border-top: 1px solid var(--line)
    }

    .payment-history-table .payment-actions-cell .btn.small {
        min-height: 29px;
        padding: 4px 8px;
        font-size: 10px
    }

    /* Forms */
    .form-card {
        padding: 13px;
        border-radius: 13px;
        box-shadow: none
    }

    .form-card h2 {
        font-size: 16px;
        margin-bottom: 12px
    }

    .grid-2,
    .grid-3 {
        grid-template-columns: 1fr;
        gap: 0
    }

    .field {
        gap: 5px;
        margin-bottom: 11px
    }

    .field label {
        font-size: 11px
    }

    .field .hint {
        font-size: 10px
    }

    .field input,
    .field textarea,
    .field select {
        padding: 9px 10px;
        border-radius: 9px;
        font-size: 16px;
        line-height: 1.25
    }

    .field textarea {
        min-height: 82px
    }

    .segmented {
        grid-template-columns: 1fr 1fr;
        gap: 6px
    }

    .segmented label {
        min-height: 40px;
        border-radius: 9px;
        font-size: 12px
    }

    .form-actions {
        display: grid;
        grid-template-columns: 1fr 1.25fr;
        gap: 7px;
        margin-top: 13px
    }

    .form-actions .btn {
        width: 100%;
        min-height: 40px;
        padding: 8px 10px;
        font-size: 12px
    }

    .form-actions[style*="space-between"] {
        display: flex;
        flex-direction: column;
        gap: 7px
    }

    .form-actions[style*="space-between"] .btn {
        width: 100%
    }

    .error-box,
    .flash {
        padding: 9px 10px;
        margin-bottom: 10px;
        border-radius: 9px;
        font-size: 12px
    }

    .btn {
        min-height: 38px;
        padding: 8px 11px;
        border-radius: 9px;
        font-size: 12px
    }

    .btn.small {
        width: auto
    }

    .auth-card {
        padding: 20px;
        border-radius: 16px
    }

    .auth-card h1 {
        font-size: 23px
    }

    .auth-card p {
        font-size: 12px;
        margin-bottom: 16px
    }
}

@media(max-width:360px) {
    .shell {
        width: calc(100% - 14px)
    }

    .summary-card {
        padding: 9px 5px
    }

    .summary-label {
        font-size: 9px
    }

    .summary-value {
        font-size: 14px
    }

    .person-header .actions {
        grid-template-columns: 1fr
    }

    .dashboard-table tbody {
        padding: 5px
    }

    .dashboard-table tr {
        padding-left: 8px
    }
}

/* ===== Compact spreadsheet desktop UI v3 ===== */
@media(min-width:701px) {
    :root {
        --shadow: none;
        --radius: 4px;
    }

    body {
        font-size: 13px;
        line-height: 1.4;
        background: #eef1f4;
    }

    .topbar {
        min-height: 48px;
        padding: 0 16px;
        gap: 16px;
        background: #fff;
        backdrop-filter: none;
        border-bottom: 1px solid #cfd6de;
        max-width: 1100px;
        margin: auto;
    }

    .brand {
        gap: 8px;
        font-size: 14px
    }

    .brand-mark {
        width: 28px;
        height: 28px;
        border-radius: 3px;
        font-size: 13px
    }

    .nav {
        gap: 0;
        align-self: stretch
    }

    .nav a {
        display: flex;
        align-items: center;
        min-height: 47px;
        padding: 0 12px;
        border-radius: 0;
        border-left: 1px solid transparent;
        border-right: 1px solid transparent;
        font-size: 12px;
    }

    .nav a:hover,
    .nav a.active {
        background: #f3f5f7;
        border-left-color: #e1e5ea;
        border-right-color: #e1e5ea;
    }

    .nav a.active {
        box-shadow: inset 0 -2px 0 #334155
    }

    .shell {
        width: min(1480px, calc(100% - 24px));
        margin: 12px auto 34px;
    }

    .auth-shell {
        width: min(430px, calc(100% - 24px));
        margin-top: 7vh
    }

    .page-head {
        align-items: center;
        margin-bottom: 10px;
        gap: 12px;
    }

    .page-title {
        font-size: 20px;
        line-height: 1.2;
        letter-spacing: -.015em
    }

    .page-sub {
        margin-top: 3px;
        font-size: 11px
    }

    .actions {
        gap: 6px
    }

    .btn {
        min-height: 34px;
        padding: 6px 11px;
        border-radius: 3px;
        font-size: 12px;
    }

    .btn:hover {
        transform: none;
        box-shadow: none;
        filter: brightness(.98)
    }

    .btn.small {
        min-height: 28px;
        padding: 4px 8px;
        border-radius: 3px;
        font-size: 11px
    }

    .cards {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 0;
        margin-bottom: 10px;
        border: 1px solid #cfd6de;
        background: #fff;
    }

    .summary-card {
        min-width: 0;
        padding: 9px 12px;
        border: 0;
        border-right: 1px solid #dbe0e6;
        border-radius: 0;
        box-shadow: none;
    }

    .summary-card:last-child {
        border-right: 0
    }

    .summary-label {
        margin-bottom: 2px;
        font-size: 10px
    }

    .summary-value {
        font-size: 20px;
        line-height: 1.1
    }

    .panel {
        border-color: #cfd6de;
        border-radius: 3px;
        box-shadow: none;
    }

    .panel+.panel {
        margin-top: 10px
    }

    .panel-head {
        min-height: 42px;
        padding: 6px 8px 6px 10px;
        gap: 10px;
        background: #f7f8fa;
    }

    .panel-title {
        font-size: 13px
    }

    .panel-body {
        padding: 10px
    }

    .toolbar {
        gap: 6px
    }

    .search {
        min-width: 220px;
        max-width: 340px
    }

    .search input {
        height: 31px;
        padding: 5px 8px 5px 30px;
        border-radius: 2px;
        font-size: 12px;
    }

    .search:before {
        left: 9px;
        font-size: 16px
    }

    .table-wrap {
        overflow: auto
    }

    table {
        min-width: 0;
        border-collapse: collapse
    }

    th,
    td {
        padding: 7px 8px;
        border-right: 1px solid #dde2e8;
        border-bottom: 1px solid #dde2e8;
        line-height: 1.3;
    }

    th:last-child,
    td:last-child {
        border-right: 0
    }

    th {
        background: #eef1f4;
        color: #475569;
        font-size: 10px;
        font-weight: 800;
    }

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

    tbody tr.clickable:hover {
        background: #f3f7fb
    }

    .person-name {
        font-size: 13px
    }

    .person-note {
        margin-top: 1px;
        font-size: 10px;
        max-width: 290px
    }

    .amount {
        font-size: 13px
    }

    .condition-list {
        gap: 2px;
        max-width: 440px
    }

    .condition-item {
        gap: 5px;
        font-size: 11px
    }

    .condition-text {
        -webkit-line-clamp: 1
    }

    .badge {
        padding: 2px 6px;
        border-radius: 3px;
        font-size: 9px
    }

    .empty {
        padding: 32px 16px;
        font-size: 12px
    }

    .empty strong {
        font-size: 14px
    }

    .person-header {
        align-items: center;
        margin-bottom: 10px;
    }

    .person-header h1 {
        font-size: 20px
    }

    .person-header .note {
        margin-top: 2px;
        font-size: 11px
    }

    .section-label {
        margin: 14px 0 5px;
        font-size: 11px
    }

    .loan-list {
        gap: 7px
    }

    .loan-card {
        border-radius: 3px;
        border-color: #cfd6de
    }

    .loan-card-head {
        padding: 7px 9px;
        background: #f4f6f8;
        border-bottom-color: #d8dee5;
    }

    .loan-card-title {
        gap: 5px;
        font-size: 11px
    }

    .loan-card-meta {
        margin-top: 2px;
        font-size: 9px
    }

    .loan-card-head>.amount {
        font-size: 14px
    }

    .loan-stats {
        border-bottom-color: #d8dee5
    }

    .loan-stat {
        padding: 7px 9px;
        border-right-color: #d8dee5
    }

    .loan-stat span {
        font-size: 9px
    }

    .loan-stat strong {
        font-size: 13px
    }

    .loan-details {
        padding: 8px 9px
    }

    .kv {
        grid-template-columns: 92px 1fr;
        gap: 4px 10px
    }

    .kv dt {
        font-size: 10px
    }

    .kv dd {
        font-size: 11px
    }

    .payment-table {
        margin-top: 8px;
        border-radius: 2px
    }

    .payment-table th,
    .payment-table td {
        padding: 5px 7px
    }

    .payment-table th {
        font-size: 9px
    }

    .loan-actions {
        gap: 5px;
        padding: 6px 8px
    }

    .form-card {
        padding: 14px;
        border-radius: 3px;
        border-color: #cfd6de;
        box-shadow: none;
    }

    .form-card h2 {
        margin-bottom: 12px;
        font-size: 15px
    }

    .grid-2,
    .grid-3 {
        gap: 10px
    }

    .field {
        gap: 4px;
        margin-bottom: 10px
    }

    .field label {
        font-size: 11px
    }

    .field .hint {
        font-size: 10px
    }

    .field input,
    .field textarea,
    .field select {
        min-height: 34px;
        padding: 6px 8px;
        border-radius: 2px;
        font-size: 13px;
    }

    .field textarea {
        min-height: 72px
    }

    .segmented {
        gap: 6px
    }

    .segmented label {
        min-height: 36px;
        border-radius: 2px;
        font-size: 12px
    }

    .form-actions {
        gap: 6px;
        margin-top: 12px
    }

    .error-box,
    .flash {
        padding: 8px 10px;
        margin-bottom: 9px;
        border-radius: 3px;
        font-size: 12px
    }

    .auth-card {
        padding: 20px;
        border-radius: 4px;
        box-shadow: none
    }

    .auth-logo {
        margin-bottom: 14px;
        font-size: 17px
    }

    .auth-card h1 {
        font-size: 22px
    }

    .auth-card p {
        margin-bottom: 16px;
        font-size: 12px
    }
}

/* Date fields: make the entire input feel clickable */
input[type="date"] {
    cursor: pointer
}

/* Desktop/tablet correction for the legacy 820px breakpoint */
@media(min-width:701px) {
    .topbar {
        flex-direction: row;
        align-items: center
    }

    .nav {
        width: auto;
        overflow: visible;
        flex-wrap: wrap;
        padding-bottom: 0
    }

    .page-head,
    .person-header {
        flex-direction: row;
        align-items: center
    }

    .page-head .actions,
    .person-header .actions {
        width: auto
    }

    .page-head .actions .btn,
    .person-header .actions .btn {
        flex: none
    }

    .loan-card-head {
        flex-direction: row
    }

    .loan-stats {
        grid-template-columns: repeat(3, minmax(0, 1fr))
    }

    .loan-stat:nth-child(2) {
        border-right: 1px solid #d8dee5
    }

    .loan-stat:nth-child(3) {
        grid-column: auto;
        border-right: 0;
        border-top: 0
    }
}