/* CalWhat Calculator Platform - SplitBillCalculator Specific Styles */
/* Mobile-First Design: base = phone, min-width queries for larger screens */

/* ============================================================
   LABELS & INPUT GROUPS
   ============================================================ */
.input-label {
    font-weight: 500;
    color: #4a5568;
    margin-bottom: 8px;
    display: block;
    font-size: 0.95rem;
}

.input-group-custom {
    margin-bottom: 15px;
}

/* ============================================================
   PEOPLE SUMMARY (成員摘要 / 引導文案)
   人員清單由墊付列反向推導，此處顯示目前成員或引導打名。
   ============================================================ */
.people-summary {
    color: #4a5568;
    font-size: 1rem;
    line-height: 1.6;
    margin: 0;
}

/* ============================================================
   PEOPLE ROSTER (顯式成員名單：可加 / 可刪人)
   ============================================================ */
.people-roster {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin: 0 0 12px;
}

/* 空名單時不留多餘空白 */
.people-roster:empty {
    margin: 0;
}

.person-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #f0fff4;
    border: 1px solid #c6f6d5;
    border-radius: 999px;
    padding: 7px 8px 7px 14px;
    transition: all 0.2s ease;
    max-width: 100%;
}

.person-chip:hover {
    border-color: #48bb78;
    box-shadow: 0 2px 8px rgba(72, 187, 120, 0.18);
}

/* 墊付者（已在墊付列出資）用紫色系，與純參與者區隔 */
.person-chip.is-payer {
    background: #eef2ff;
    border-color: #c3dafe;
}

.person-chip-name {
    font-size: 1rem;
    font-weight: 600;
    color: #2d3748;
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.person-chip .payer-tag {
    font-size: 0.75rem;
    color: #667eea;
    font-weight: 600;
}

.person-chip-delete {
    background: transparent;
    border: none;
    color: #a0aec0;
    cursor: pointer;
    width: 26px;
    height: 26px;
    min-width: 26px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.95rem;
    transition: all 0.2s ease;
}

.person-chip-delete:hover {
    color: #fff;
    background: #ef4444;
}

.add-person-row {
    display: flex;
    gap: 8px;
    align-items: stretch;
}

/* 沿用全域 .form-input 外觀，僅讓它在 flex 列裡撐滿剩餘寬度 */
.add-person-input {
    flex: 1;
    min-width: 0;
}

.btn-add-person {
    border: none;
    border-radius: 10px;
    padding: 10px 18px;
    cursor: pointer;
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
    font-size: 1rem;
    font-weight: 600;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 6px;
}

.btn-add-person:hover {
    filter: brightness(1.05);
}

.roster-hint {
    font-size: 0.9rem;
    color: #718096;
    margin: 12px 0 0;
    line-height: 1.6;
}

.roster-hint i {
    color: #a0aec0;
}

/* ============================================================
   DEMO HINT (示範資料邀請接手提示)
   ============================================================ */
.demo-hint {
    background: #eef2ff;
    border: 1px dashed #667eea;
    border-radius: 10px;
    padding: 10px 14px;
    margin-bottom: 12px;
    color: #4c51bf;
    font-size: 0.95rem;
    font-weight: 600;
    line-height: 1.5;
}

/* ============================================================
   ADVANCED OPTIONS (進階選項：總金額 / 自訂人名 / 比例 收合)
   ============================================================ */
.advanced-options {
    margin-top: 12px;
    border-top: 1px solid #e2e8f0;
    padding-top: 12px;
}

.advanced-options-summary {
    cursor: pointer;
    color: #667eea;
    font-weight: 600;
    font-size: 0.95rem;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 0;
    user-select: none;
}

.advanced-options-summary::-webkit-details-marker {
    display: none;
}

.advanced-options-summary::before {
    content: "\f078"; /* fa chevron-down */
    font-family: "Font Awesome 6 Free";
    font-weight: 900;
    font-size: 0.8rem;
    transition: transform 0.2s ease;
}

.advanced-options[open] > .advanced-options-summary::before {
    transform: rotate(180deg);
}

.advanced-options-body {
    margin-top: 12px;
}

/* ============================================================
   PAGE HEADER (mobile-first scaling)
   ============================================================ */
.page-header h1 {
    font-size: 1.5rem;
}

.page-header .subtitle {
    font-size: 0.95rem;
}

/* ============================================================
   GLASS-CARD PADDING OVERRIDE (scoped to this calculator)
   ============================================================ */
.main-container .glass-card {
    padding: 16px;
    border-radius: 16px;
    margin-bottom: 16px;
}

/* ============================================================
   TABLE-RESPONSIVE: disable horizontal scroll on mobile
   (stacked layout doesn't need it)
   ============================================================ */
.table-responsive {
    overflow-x: visible;
    overflow-y: visible;
}

/* ============================================================
   PAYMENT TABLE - BASE (MOBILE: stacked card layout)
   ============================================================ */
.payment-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
}

/* Hide table header on mobile */
.payment-table thead {
    display: none;
}

/* Each row becomes a mini-card */
.payment-table tbody {
    counter-reset: payment-row;
}

.payment-table tbody tr {
    display: block;
    background: #f7fafc;
    border-radius: 12px;
    padding: 12px;
    margin-bottom: 12px;
    position: relative;
    counter-increment: payment-row;
    transition: background 0.3s ease;
}

/* Row number indicator on mobile */
.payment-table tbody tr::before {
    content: "#" counter(payment-row);
    display: block;
    font-size: 0.7rem;
    font-weight: 700;
    color: #667eea;
    margin-bottom: 6px;
    opacity: 0.7;
}

/* All cells become block */
.payment-table tbody td {
    display: block;
    padding: 4px 0;
    border: none;
}

/* Mobile data labels (via data-label attr) */
.payment-table tbody td::before {
    content: attr(data-label);
    display: block;
    font-size: 0.7rem;
    font-weight: 600;
    color: #667eea;
    margin-bottom: 3px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Hide label for delete column */
.payment-table tbody td:last-child::before {
    display: none;
}

/* Payer cell: inline, leave room for delete button */
.payment-table tbody td:first-child {
    display: inline-block;
    width: calc(100% - 44px);
    vertical-align: top;
}

/* Delete button cell: absolute top-right */
.payment-table tbody td:last-child {
    position: absolute;
    top: 12px;
    right: 12px;
    width: auto;
    padding: 0;
}

/* Touch-friendly inputs inside table */
.payment-table select,
.payment-table input {
    width: 100%;
    padding: 12px;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    font-size: 16px; /* Prevents iOS auto-zoom */
    min-height: 44px; /* Apple HIG touch target */
    box-sizing: border-box;
}

.payment-table .form-input {
    background: #f0fff4;
}

.payment-table select:focus,
.payment-table input:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
}

/* ============================================================
   QUICK AMOUNT BUTTONS
   ============================================================ */
.quick-amount-btns {
    display: flex;
    gap: 6px;
    margin-top: 8px;
    flex-wrap: nowrap;
}

.quick-amount-btn {
    flex: 1;
    min-width: 0;
    padding: 8px 4px;
    min-height: 36px;
    background: #f3f4f6;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #374151;
    font-weight: 500;
}

.quick-amount-btn:hover {
    background: #667eea;
    border-color: #667eea;
    color: white;
}

.quick-amount-btn:active {
    transform: scale(0.95);
}

/* ============================================================
   DELETE BUTTON
   ============================================================ */
.btn-remove {
    background: #ef4444;
    color: white;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-remove:hover {
    background: #dc2626;
    transform: scale(1.1);
}

/* ============================================================
   ADD PAYMENT BUTTON (sticky on mobile)
   ============================================================ */
.btn-add {
    width: 100%;
    padding: 14px;
    background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
    color: white;
    border: none;
    border-radius: 12px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.3s ease;
    margin-top: 10px;
    position: sticky;
    bottom: 0;
    z-index: 5;
    box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.1);
}

.btn-add:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(72, 187, 120, 0.4);
}

/* ============================================================
   RESULT SECTION
   ============================================================ */
.result-main {
    text-align: center;
    margin-bottom: 20px;
}

.result-label {
    font-size: 1rem;
    color: #4a5568;
    font-weight: 500;
    margin-bottom: 8px;
}

.result-value {
    font-family: 'Roboto Mono', monospace;
    font-size: 2rem;
    font-weight: 700;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 16px;
    animation: pulse 0.5s ease;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

.result-details {
    background: #f7fafc;
    padding: 12px;
    border-radius: 10px;
}

.result-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid #e2e8f0;
}

.result-item:last-child {
    border-bottom: none;
}

.result-item-label {
    font-size: 0.9rem;
    color: #4a5568;
    display: flex;
    align-items: center;
    gap: 6px;
}

.result-item-value {
    font-family: 'Roboto Mono', monospace;
    font-weight: 600;
    color: #2d3748;
    font-size: 1rem;
}

/* ============================================================
   PERSON DETAILS
   ============================================================ */
.person-detail {
    background: #f7fafc;
    border-radius: 10px;
    padding: 12px;
    margin-bottom: 12px;
    transition: all 0.3s ease;
}

.person-detail:hover {
    background: #edf2f7;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.person-detail-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    flex-wrap: wrap;
    gap: 6px;
}

.person-name {
    font-weight: 600;
    font-size: 1rem;
    color: #2d3748;
}

.person-status {
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 600;
    white-space: nowrap;
}

.status-owe {
    background: #fee;
    color: #c53030;
}

.status-paid {
    background: #e6fffa;
    color: #047857;
}

.status-even {
    background: #e2e8f0;
    color: #4a5568;
}

.person-detail-body {
    font-size: 0.85rem;
    color: #4a5568;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    padding: 4px 0;
}

.items-list {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px dashed #cbd5e0;
}

.items-list ul {
    list-style: none;
    padding-left: 8px;
}

.items-list li {
    padding: 2px 0;
    font-size: 0.8rem;
}

/* ============================================================
   SETTLEMENT / TRANSACTIONS
   ============================================================ */
.settlement-info {
    text-align: center;
    margin-bottom: 12px;
}

.badge {
    display: inline-block;
    padding: 5px 10px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 600;
}

.transaction-item {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    padding: 10px;
    background: white;
    border-radius: 8px;
    margin-bottom: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    gap: 4px;
    font-size: 0.9rem;
}

.transaction-from,
.transaction-to {
    font-weight: 600;
    color: #2d3748;
    flex: 0 0 100%;
}

.transaction-arrow {
    flex: 0 0 100%;
    text-align: center;
    color: #667eea;
    font-size: 1rem;
    margin: 2px 0;
    transform: rotate(90deg);
}

.transaction-amount {
    font-family: 'Roboto Mono', monospace;
    font-weight: 700;
    font-size: 1rem;
    color: #48bb78;
    flex: 1;
}

.btn-copy-amount {
    background: #667eea;
    color: white;
    border: none;
    border-radius: 6px;
    padding: 6px 10px;
    cursor: pointer;
    transition: all 0.3s ease;
    min-height: 36px;
}

.btn-copy-amount:hover {
    background: #5a67d8;
}

.settlement-summary {
    text-align: center;
    margin-top: 16px;
    padding: 12px;
    background: #e6fffa;
    border-radius: 10px;
    color: #047857;
    font-weight: 600;
    font-size: 0.9rem;
}

/* ============================================================
   FORM CHECKBOX
   ============================================================ */
.form-check {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
}

.form-check input[type="checkbox"] {
    width: 20px;
    height: 20px;
    cursor: pointer;
}

.form-check label {
    cursor: pointer;
    user-select: none;
}

/* ============================================================
   NAME & RATIO INPUTS
   ============================================================ */
#nameInputSection {
    margin-top: 15px;
}

.name-ratio-row {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 10px;
}

.name-ratio-row .ratio-person-name {
    flex: 1;
    min-width: 0;
    font-weight: 600;
    color: #2d3748;
    font-size: 1rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ratio-input-wrapper {
    flex: 0 0 90px;
}

.ratio-input {
    width: 100%;
    padding: 10px 6px;
    border: 2px solid #48bb78;
    border-radius: 8px;
    font-size: 0.9rem;
    text-align: center;
}

.ratio-label {
    font-size: 0.75rem;
    color: #666;
    text-align: center;
    margin-bottom: 3px;
}

/* ============================================================
   PARTICIPANTS SELECTOR (inline dropdown for desktop)
   ============================================================ */
.participants-selector {
    position: relative;
    z-index: 1;
}

.participants-btn {
    background: #f3f4f6;
    border: 1px solid #d1d5db;
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 5px;
    width: 100%;
    justify-content: space-between;
    min-height: 44px;
}

.participants-btn:hover {
    background: #e5e7eb;
    border-color: #667eea;
}

.participants-btn.active {
    border-color: #667eea;
    background: #eef2ff;
}

.participants-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    min-width: 220px;
    background: white;
    border: 2px solid #667eea;
    border-radius: 8px;
    margin-top: 5px;
    padding: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
    z-index: 100;
    max-height: 250px;
    overflow-y: auto;
}

.participant-checkbox {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 6px;
    cursor: pointer;
    border-radius: 6px;
    transition: background 0.2s;
}

.participant-checkbox:hover {
    background: #f3f4f6;
}

.participant-checkbox input[type="checkbox"] {
    width: 22px;
    height: 22px;
    cursor: pointer;
    flex-shrink: 0;
}

.participant-checkbox label {
    cursor: pointer;
    margin: 0;
    flex: 1;
    user-select: none;
    font-size: 0.95rem;
}

.participants-count {
    color: #667eea;
    font-weight: 600;
}

/* ============================================================
   DESKTOP SHARED DROPDOWN (appended to body, avoids table z-index)
   ============================================================ */
.desktop-participants-dropdown {
    position: fixed;
    min-width: 200px;
    background: white;
    border: 2px solid #667eea;
    border-radius: 8px;
    padding: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
    z-index: 10000;
    max-height: 250px;
    overflow-y: auto;
}

/* ============================================================
   BOTTOM SHEET (mobile participants selector)
   ============================================================ */
.participants-bottom-sheet {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10000;
    display: none;
}

.participants-bottom-sheet.active {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
}

.bottom-sheet-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    animation: bsFadeIn 0.2s ease;
}

.bottom-sheet-content {
    position: relative;
    background: white;
    border-radius: 20px 20px 0 0;
    padding: 20px;
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
    max-height: 60vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    animation: bsSlideUp 0.3s ease;
}

.bottom-sheet-handle {
    width: 40px;
    height: 4px;
    background: #d1d5db;
    border-radius: 2px;
    margin: 0 auto 16px;
}

.bottom-sheet-title {
    font-size: 1.1rem;
    font-weight: 600;
    color: #2d3748;
    margin-bottom: 16px;
    margin-top: 0;
}

.bottom-sheet-checkboxes .participant-checkbox {
    padding: 12px 8px;
    border-bottom: 1px solid #f3f4f6;
}

.bottom-sheet-checkboxes .participant-checkbox:last-child {
    border-bottom: none;
}

.bottom-sheet-done {
    margin-top: 16px;
    width: 100%;
}

@keyframes bsSlideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

@keyframes bsFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* ============================================================
   TRIP LEDGER (named multi-save chip list)
   ============================================================ */
.trip-ledger .ledger-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin: 4px 0 14px;
}

.ledger-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #f0fff4;
    border: 1px solid #c6f6d5;
    border-radius: 999px;
    padding: 6px 10px;
    transition: all 0.2s ease;
    max-width: 100%;
}

.ledger-chip:hover {
    border-color: #48bb78;
    box-shadow: 0 2px 8px rgba(72, 187, 120, 0.18);
}

.ledger-chip.active {
    background: #eef2ff;
    border-color: #667eea;
    box-shadow: 0 2px 10px rgba(102, 126, 234, 0.22);
}

.ledger-chip-load {
    background: transparent;
    border: none;
    color: #667eea;
    cursor: pointer;
    padding: 2px 4px;
    font-size: 1rem;
    display: flex;
    align-items: center;
    min-height: 32px;
    min-width: 28px;
}

.ledger-chip-load:hover {
    color: #4c51bf;
}

.ledger-chip-name {
    font-size: 1rem;
    font-weight: 600;
    color: #2d3748;
    outline: none;
    border-radius: 4px;
    padding: 2px 4px;
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: text;
}

.ledger-chip-name:focus {
    background: #fff;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.35);
    overflow: visible;
    white-space: normal;
}

.ledger-chip-delete {
    background: transparent;
    border: none;
    color: #a0aec0;
    cursor: pointer;
    padding: 2px 4px;
    font-size: 0.95rem;
    display: flex;
    align-items: center;
    min-height: 32px;
    min-width: 28px;
    border-radius: 50%;
    transition: all 0.2s ease;
}

.ledger-chip-delete:hover {
    color: #fff;
    background: #ef4444;
}

.ledger-save-btn {
    width: 100%;
}

/* ============================================================
   ANIMATIONS
   ============================================================ */
@keyframes fadeInDown {
    from {
        opacity: 0;
        transform: translateY(-30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ============================================================
   RESPONSIVE: min-width: 576px (large phones)
   ============================================================ */
@media (min-width: 576px) {
    .page-header h1 {
        font-size: 1.8rem;
    }

    .main-container .glass-card {
        padding: 20px;
        border-radius: 18px;
    }

    .result-value {
        font-size: 2.3rem;
    }

    .ratio-input-wrapper {
        flex: 0 0 110px;
    }

    /* Transaction items: horizontal on larger phones */
    .transaction-from,
    .transaction-to {
        flex: 1;
    }

    .transaction-arrow {
        flex: 0 0 auto;
        margin: 0 6px;
        transform: none;
    }
}

/* ============================================================
   RESPONSIVE: min-width: 768px (tablets)
   Restore horizontal table layout, inline dropdowns
   ============================================================ */
@media (min-width: 768px) {
    .page-header h1 {
        font-size: 2rem;
    }

    .main-container .glass-card {
        padding: 24px;
        border-radius: 20px;
        margin-bottom: 20px;
    }

    .result-value {
        font-size: 2.5rem;
    }

    .result-details {
        padding: 18px;
        border-radius: 12px;
    }

    .result-item-label {
        font-size: 0.95rem;
    }

    .result-item-value {
        font-size: 1.1rem;
    }

    .person-detail {
        padding: 15px;
        margin-bottom: 15px;
    }

    .person-name {
        font-size: 1.1rem;
    }

    .person-status {
        font-size: 0.9rem;
    }

    .person-detail-body {
        font-size: 0.9rem;
    }

    .items-list li {
        font-size: 0.85rem;
    }

    /* Keep overflow visible so dropdown is not clipped */
    .table-responsive {
        overflow-x: visible;
        overflow-y: visible;
    }

    /* Show table header */
    .payment-table thead {
        display: table-header-group;
    }

    .payment-table thead th {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 12px;
        font-weight: 600;
        text-align: left;
    }

    /* Restore column widths for desktop table layout */
    .payment-table thead th:nth-child(1) { width: 18%; }
    .payment-table thead th:nth-child(2) { width: 25%; }
    .payment-table thead th:nth-child(3) { width: 18%; }
    .payment-table thead th:nth-child(4) { width: 30%; }
    .payment-table thead th:nth-child(5) { width: 9%; }

    .payment-table thead th:first-child {
        border-radius: 10px 0 0 10px;
    }

    .payment-table thead th:last-child {
        border-radius: 0 10px 10px 0;
    }

    /* Restore normal table layout */
    .payment-table {
        border-spacing: 0 10px;
    }

    .payment-table tbody tr {
        display: table-row;
        padding: 0;
        margin-bottom: 0;
        border-radius: 0;
        background: #f7fafc;
    }

    .payment-table tbody tr::before {
        display: none;
    }

    .payment-table tbody tr:hover {
        background: #edf2f7;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    }

    .payment-table tbody td {
        display: table-cell;
        padding: 10px;
    }

    .payment-table tbody td::before {
        display: none;
    }

    .payment-table tbody td:first-child {
        display: table-cell;
        width: auto;
        border-radius: 10px 0 0 10px;
    }

    .payment-table tbody td:last-child {
        position: static;
        width: auto;
        text-align: center;
        border-radius: 0 10px 10px 0;
    }

    /* Table inputs revert to smaller size */
    .payment-table select,
    .payment-table input {
        padding: 8px;
        font-size: 0.95rem;
        min-height: auto;
    }

    /* Quick amount buttons smaller */
    .quick-amount-btns {
        gap: 4px;
        margin-top: 6px;
    }

    .quick-amount-btn {
        padding: 4px 8px;
        min-height: auto;
        font-size: 0.75rem;
        border-radius: 6px;
    }

    /* Delete button smaller */
    .btn-remove {
        width: 32px;
        height: 32px;
        min-width: 32px;
        min-height: 32px;
    }

    /* Add button: no longer sticky */
    .btn-add {
        position: static;
        box-shadow: none;
        padding: 12px;
    }

    /* Participants button smaller padding */
    .participants-btn {
        padding: 6px 10px;
        font-size: 0.85rem;
        min-height: auto;
    }

    .participant-checkbox {
        padding: 6px;
    }

    .participant-checkbox input[type="checkbox"] {
        width: 16px;
        height: 16px;
    }

    .participant-checkbox label {
        font-size: 0.9rem;
    }

    /* Ratio input wrapper wider */
    .ratio-input-wrapper {
        flex: 0 0 120px;
    }

    /* Transaction items horizontal */
    .transaction-item {
        flex-wrap: nowrap;
        padding: 12px;
        font-size: 1rem;
    }

    .transaction-from,
    .transaction-to {
        flex: 1;
    }

    .transaction-arrow {
        flex: 0 0 auto;
        margin: 0 10px;
        font-size: 1.2rem;
    }

    .transaction-amount {
        font-size: 1.1rem;
        margin-right: 10px;
    }

    /* Bottom sheet: force hidden on tablet+ */
    .participants-bottom-sheet {
        display: none !important;
    }

    /* Settlement summary */
    .settlement-summary {
        font-size: 1rem;
        padding: 15px;
    }

    .badge {
        font-size: 0.9rem;
        padding: 6px 12px;
    }
}

/* ============================================================
   RESPONSIVE: min-width: 992px (desktop)
   Full two-column layout via Bootstrap col-lg-7/col-lg-5
   ============================================================ */
@media (min-width: 992px) {
    .page-header h1 {
        font-size: 2.5rem;
    }

    .page-header .subtitle {
        font-size: 1.1rem;
    }

    .main-container .glass-card {
        padding: 30px;
        border-radius: 24px;
        margin-bottom: 25px;
    }

    .result-value {
        font-size: 3rem;
        margin-bottom: 20px;
    }

    .result-main {
        margin-bottom: 30px;
    }

    .result-details {
        padding: 20px;
    }

    .transaction-item:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
    }

    .form-check input[type="checkbox"] {
        width: 18px;
        height: 18px;
    }
}

/* ============================================================
   SHARED RECEIVER CTA（分享連結 → 轉新用戶迴圈）
   持久卡片：使用全域 .glass-card，僅補佈局微調，不重定義全域 class。
   ============================================================ */
.shared-receiver-cta {
    display: flex;
    flex-direction: column;
    gap: 14px;
    align-items: stretch;
    border-left: 4px solid #667eea;
}

.shared-cta-text {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #4a5568;
    font-size: 1rem;
    line-height: 1.5;
}

.shared-cta-text i {
    color: #667eea;
    font-size: 1.2rem;
    flex-shrink: 0;
}

.shared-cta-headline {
    font-weight: 600;
}

.shared-cta-btn {
    align-self: flex-start;
}

@media (min-width: 768px) {
    .shared-receiver-cta {
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
    }

    .shared-cta-btn {
        align-self: auto;
        white-space: nowrap;
    }
}

/* 手機版 sticky 摘要列（桌機由 .d-lg-none 隱藏）：把平均每人/總花費常駐視線 */
.mobile-sticky-bar {
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    justify-content: space-around;
    gap: 8px;
    background: rgba(102, 126, 234, 0.97);
    backdrop-filter: blur(8px);
    border-radius: 0 0 14px 14px;
    padding: 10px 12px;
    margin-bottom: 16px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}

.mobile-sticky-bar .sticky-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 1;
    min-width: 0;
}

.mobile-sticky-bar .sticky-label {
    font-size: 0.78rem;
    color: rgba(255, 255, 255, 0.85);
    white-space: nowrap;
}

.mobile-sticky-bar .sticky-value {
    font-size: 1.1rem;
    font-weight: 800;
    color: white;
    font-family: 'Roboto Mono', monospace;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

/* ============================================================
   MOBILE ANSWER-FIRST REFLOW (<=991px) — pure CSS, no DOM change
   ------------------------------------------------------------
   Real DOM: .main-container > .row > (.col-lg-7 inputs | .col-lg-5 results).
   col-lg-5 holds 總覽(hero)/個人明細/結算方案/action-buttons, and the
   client-injected results ad (.ad-container[data-ad-pos="results"]) is
   appended as col-lg-5's LAST child. We flatten both columns with
   display:contents so every card becomes a direct flex item of .row,
   then assign order to surface the answer first.
   Order: 總覽(1) → 結算方案(2) → results-ad(3) → 參與人員(4)
          → 墊付項目(5) → 個人明細(6) → action-buttons(7).
   (#mobileStickyBar, shared-cta, related-calculators, seo-ad and
    .seo-section live directly under .main-container, outside .row, so
    their natural block order already trails the answer — untouched.)
   ============================================================ */
@media (max-width: 991px) {
    /* DIRECT CHILD only — never .main-container .row (would hit the
       nested .row.g-2 action-button grid and break it to 1x4). */
    .main-container > .row {
        display: flex;
        flex-direction: column;
    }

    /* Flatten columns so child cards participate in the row's flex flow. */
    .main-container > .row > .col-lg-7,
    .main-container > .row > .col-lg-5 {
        display: contents;
    }

    /* --- answer-first ordering (scoped child selectors, no :has()) ---
       col-lg-7 note: JS prepends a hidden #tripLedgerCard.glass-card as the
       FIRST child, which shifts nth-of-type. So col-lg-7 is ordered by a
       lifecycle-stable default + #peopleDatalist sibling anchor, NOT by
       nth-of-type (which would break once the ledger card is injected). */
    /* 總覽 hero result */
    .main-container > .row > .col-lg-5 > div.glass-card:nth-of-type(1) { order: 1; }
    /* 結算方案 settlement */
    .main-container > .row > .col-lg-5 > div.glass-card:nth-of-type(3) { order: 2; }
    /* injected results ad — explicit order so it never floats above answer,
       and kept modest so it never pushes the answer down. */
    .main-container > .row > .col-lg-5 > .ad-container[data-ad-pos="results"] {
        order: 3;
        margin: 12px 0 0;
        min-height: 60px;
    }
    /* 參與人員 participants (+ hidden #tripLedgerCard, which is display:none) */
    .main-container > .row > .col-lg-7 > div.glass-card { order: 4; }
    /* 墊付項目 payment inputs — the only glass-card AFTER the shared datalist,
       so this anchor is stable regardless of the prepended ledger card. */
    .main-container > .row > .col-lg-7 #peopleDatalist ~ div.glass-card { order: 5; }
    /* 個人明細 person details */
    .main-container > .row > .col-lg-5 > div.glass-card:nth-of-type(2) { order: 6; }
    /* action buttons */
    .main-container > .row > .col-lg-5 > div.glass-card:nth-of-type(4) { order: 7; }

    /* --- HERO RESULT CARD: overlap header, enlarge the answer --- */
    .main-container > .row > .col-lg-5 > div.glass-card:nth-of-type(1) {
        position: relative;
        z-index: 10;
        margin-top: -32px;
        box-shadow: 0 8px 32px rgba(102, 126, 234, 0.20);
    }

    /* --- 冷啟動新訪客：輸入優先首屏（.sb-input-first 由 JS 於 init 判定加在 .main-container）---
       兩個 class 的特異度高於上方單 class 規則，故 class 存在時覆寫 order；不存在時維持既有答案優先。
       目標：墊付項目→參與人員 排最前，結果卡與廣告下放，讓搜尋進來的新訪客首屏＝可操作輸入。 */
    .main-container.sb-input-first > .row > .col-lg-7 #peopleDatalist ~ div.glass-card { order: 1; } /* 墊付項目 */
    .main-container.sb-input-first > .row > .col-lg-7 > div.glass-card { order: 2; }               /* 參與人員（+隱藏帳本卡）*/
    /* 總覽：改置中段，並取消「上移蓋頁首」效果（輸入優先時它不在頂端，上移會蓋到上方輸入卡）*/
    .main-container.sb-input-first > .row > .col-lg-5 > div.glass-card:nth-of-type(1) {
        order: 3;
        margin-top: 0;
    }
    .main-container.sb-input-first > .row > .col-lg-5 > div.glass-card:nth-of-type(3) { order: 4; } /* 結算方案 */
    .main-container.sb-input-first > .row > .col-lg-5 > div.glass-card:nth-of-type(2) { order: 5; } /* 個人明細 */
    /* 廣告刻意排到個人明細之後（比 answer-first 更後面）：新訪客首屏聚焦輸入，不讓廣告插進輸入→答案的動線 */
    .main-container.sb-input-first > .row > .col-lg-5 > .ad-container[data-ad-pos="results"] { order: 6; }
    .main-container.sb-input-first > .row > .col-lg-5 > div.glass-card:nth-of-type(4) { order: 7; } /* 操作按鈕 */

    .result-main {
        margin-bottom: 14px;
    }

    .result-label {
        font-size: 0.78rem;
        font-weight: 700;
        color: #667eea;
        text-transform: uppercase;
        letter-spacing: 0.8px;
        margin-bottom: 4px;
    }

    /* Hero number (總花費) — solid color, mono, large & centered. */
    .result-value {
        font-size: 42px;
        line-height: 1.05;
        letter-spacing: -1px;
        color: #5a67d8;
        background: none;
        -webkit-text-fill-color: #5a67d8;
        -webkit-background-clip: border-box;
        background-clip: border-box;
        margin-bottom: 14px;
    }

    /* Per-person metric: bump prominence as the secondary hero figure. */
    #perPersonCost {
        font-size: 1.2rem;
        color: #5a67d8;
    }

    .result-details {
        background: #f7fafc;
        border-radius: 12px;
        padding: 12px 14px;
    }

    /* --- SETTLEMENT: high-value transaction ergonomics --- */
    .transaction-item {
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
        gap: 6px;
        padding: 12px;
        background: #fafafa;
        border-radius: 10px;
        margin-bottom: 8px;
        box-shadow: none;
        font-size: 1rem;
    }

    .transaction-from,
    .transaction-to {
        flex: 1;
        min-width: 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 1rem;
        font-weight: 700;
    }

    .transaction-from { color: #c53030; }

    .transaction-to {
        color: #047857;
        text-align: right;
    }

    /* Arrow back to inline (base CSS rotates it 90deg for tiny phones). */
    .transaction-arrow {
        flex: 0 0 auto;
        margin: 0 2px;
        transform: none;
        color: #667eea;
        font-size: 0.9rem;
    }

    .transaction-amount {
        flex: 0 0 auto;
        font-family: 'Roboto Mono', Consolas, monospace;
        font-weight: 800;
        font-size: 1.05rem;
        color: #5a67d8;
        background: #eef2ff;
        padding: 4px 8px;
        border-radius: 6px;
    }

    /* Per-transaction copy button: full 44px touch target. */
    .btn-copy-amount {
        flex: 0 0 auto;
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: transparent;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        color: #a0aec0;
        padding: 0;
    }

    .btn-copy-amount:hover {
        background: #eef2ff;
        color: #5a67d8;
        border-color: #c7d2fe;
    }

    /* Bulk 複製結算 button: full-width below the rows. */
    .settlement-info {
        flex-wrap: wrap;
    }

    #btnCopySettlement {
        flex: 1 1 100%;
        width: 100%;
        justify-content: center;
        min-height: 44px;
        margin-top: 4px;
    }

    /* --- INPUTS: anti-iOS-zoom + comfortable touch targets --- */
    .form-input,
    .payment-table select,
    .payment-table input,
    #totalAmount {
        font-size: 16px;
        min-height: 48px;
    }

    /* Quick-amount + participants + delete: keep large touch targets. */
    .quick-amount-btn {
        flex: 1;
        min-height: 44px;
        font-size: 0.85rem;
    }

    .participants-btn {
        width: 100%;
        min-height: 48px;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    /* Add-item button full-width gradient; NOT sticky (avoids overlap). */
    .btn-add {
        position: static;
        bottom: auto;
        box-shadow: 0 4px 14px rgba(72, 187, 120, 0.3);
        min-height: 52px;
        font-size: 1rem;
        margin-top: 4px;
    }

    .btn-remove {
        width: 34px;
        height: 34px;
        min-width: 34px;
        min-height: 34px;
    }

    /* --- ACTION BUTTONS: keep the standard 2x2 grid (DO NOT stack). --- */
    /* The nested .row.g-2 is the action-button grid; explicitly keep it a
       horizontal-wrapping row so the > .row column-flip above never makes
       it 1x4. (Belt-and-braces: the > direct-child selector already spares
       it, but Bootstrap's .row is overridden defensively here.) */
    .main-container .glass-card .row.g-2 {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
    }

    .main-container .glass-card .row.g-2 > .col-6 {
        flex: 0 0 50%;
        max-width: 50%;
    }

    .main-container .glass-card .row.g-2 .btn {
        min-height: 52px;
        font-size: 0.9rem;
        border-radius: 12px;
    }
}
