:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --success: #16a34a;
    --danger: #dc2626;
    --warning: #f59e0b;
    --bg: #f1f5f9;
    --card: #ffffff;
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --income-bg: #ecfdf5;
    --expense-bg: #fef2f2;
    --debt-bg: #fefce8;
    --received-bg: #dcfce7;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Login Page */
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 1rem;
}

.login-box {
    background: var(--card);
    border-radius: 12px;
    padding: 2.5rem;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 4px 24px rgba(0,0,0,0.08);
}

.login-box h1 {
    text-align: center;
    margin-bottom: 0.5rem;
    font-size: 1.75rem;
}

.login-box .subtitle {
    text-align: center;
    color: var(--text-muted);
    margin-bottom: 2rem;
}

.form-group {
    margin-bottom: 1.25rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.4rem;
    font-weight: 600;
    font-size: 0.875rem;
}

.form-group input, .form-group select {
    width: 100%;
    padding: 0.65rem 0.85rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.95rem;
    transition: border-color 0.2s;
}

.form-group input:focus, .form-group select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.6rem 1.2rem;
    border: none;
    border-radius: 8px;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-success {
    background: var(--success);
    color: #fff;
}
.btn-success:hover { background: #15803d; }

.btn-danger {
    background: var(--danger);
    color: #fff;
}
.btn-danger:hover { background: #b91c1c; }

.btn-sm {
    padding: 0.35rem 0.7rem;
    font-size: 0.8rem;
}

.btn-block {
    width: 100%;
    justify-content: center;
    padding: 0.75rem;
    font-size: 1rem;
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
}
.btn-outline:hover {
    background: var(--bg);
}

.error-msg {
    background: #fef2f2;
    color: var(--danger);
    padding: 0.6rem 0.85rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-size: 0.875rem;
}

/* Nav */
.navbar {
    background: var(--card);
    border-bottom: 1px solid var(--border);
    padding: 0 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 60px;
    position: sticky;
    top: 0;
    z-index: 100;
}

.navbar .brand {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--primary);
    text-decoration: none;
}

.navbar nav {
    display: flex;
    gap: 0.25rem;
}

.navbar nav a {
    padding: 0.5rem 1rem;
    text-decoration: none;
    color: var(--text-muted);
    font-weight: 500;
    border-radius: 8px;
    font-size: 0.9rem;
    transition: all 0.2s;
}

.navbar nav a:hover, .navbar nav a.active {
    color: var(--primary);
    background: rgba(37,99,235,0.06);
}

.navbar .user-info {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-size: 0.875rem;
    color: var(--text-muted);
}

/* Layout */
.container {
    max-width: min(1700px, 96vw);
    margin: 0 auto;
    padding: 1.5rem 2.25rem;
}

.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    gap: 1rem;
}

.page-header h2 {
    font-size: 1.5rem;
}

/* Summary Cards */
/* PHASE_POLISH_LAYOUT: lock 4-card grid so KPI boxes don't shift around as numbers change. */
.summary-cards {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
    margin-bottom: 1.5rem;
}
@media (max-width: 900px) {
    .summary-cards { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 540px) {
    .summary-cards { grid-template-columns: 1fr; }
}

.summary-card {
    background: var(--card);
    border-radius: 10px;
    padding: 1.25rem;
    box-shadow: 0 1px 4px rgba(0,0,0,0.05);
}

.summary-card .label {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 0.4rem;
}

.summary-card .value {
    font-size: 1.5rem;
    font-weight: 700;
}

.summary-card .value.positive { color: var(--success); }
.summary-card .value.negative { color: var(--danger); }

/* Tables */
.table-wrap {
    background: var(--card);
    border-radius: 10px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.05);
    overflow-x: auto;
    margin-bottom: 1.5rem;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
    white-space: nowrap;
}

th {
    background: #f8fafc;
    padding: 0.7rem 0.85rem;
    text-align: left;
    font-weight: 600;
    font-size: 0.8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.3px;
    border-bottom: 2px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}

td {
    padding: 0.6rem 0.85rem;
    border-bottom: 1px solid var(--border);
}

tr:hover { background: #f8fafc; }

tr.income-row { background: var(--income-bg); }
tr.income-row:hover { background: #d1fae5; }

tr.separator-row { background: #f1f5f9; height: 4px; }
tr.separator-row td { padding: 0; border: none; }

tr.total-row {
    background: #f8fafc;
    font-weight: 700;
}

.amount-positive { color: var(--success); font-weight: 600; }
.amount-negative { color: var(--danger); font-weight: 600; }

.debt-badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    background: var(--debt-bg);
    color: #a16207;
}

.received-check {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s;
    border: 2px solid var(--border);
    background: #fff;
}

.received-check.checked {
    background: var(--success);
    border-color: var(--success);
    color: #fff;
}

.received-check:hover {
    border-color: var(--success);
}

/* Source link */
.source-link {
    color: var(--text);
    text-decoration: none;
    cursor: pointer;
    border-bottom: 1px dashed var(--border);
}
.source-link:hover {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

/* Context menu */
.context-menu {
    position: fixed;
    background: var(--card);
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.15);
    z-index: 1000;
    min-width: 120px;
    overflow: hidden;
}
.context-menu a {
    display: block;
    padding: 0.6rem 1rem;
    text-decoration: none;
    color: var(--text);
    font-size: 0.875rem;
    font-weight: 500;
    transition: background 0.15s;
}
.context-menu a:hover {
    background: var(--bg);
}
.context-menu a.danger {
    color: var(--danger);
}
.context-menu a.danger:hover {
    background: #fef2f2;
}

tr.bill-paid {
    opacity: 0.45;
}
tr.bill-paid td {
    text-decoration: line-through;
    text-decoration-color: var(--text-muted);
}

/* Month columns */
.month-col {
    text-align: right;
    min-width: 90px;
}

.month-header {
    writing-mode: horizontal-tb;
    font-size: 0.75rem;
}

/* Modal */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal-overlay.active { display: flex; }

.modal {
    background: var(--card);
    border-radius: 12px;
    width: 100%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0,0,0,0.15);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border);
}

.modal-header h3 { font-size: 1.1rem; }

.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-muted);
    line-height: 1;
}

.modal-body { padding: 1.5rem; }

.modal-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 0.75rem;
    justify-content: flex-end;
}

/* Year/Month selector */
.month-selector {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.month-selector select {
    padding: 0.4rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.875rem;
}

/* Tabs */
.tab-bar {
    display: flex;
    gap: 0;
    border-bottom: 2px solid var(--border);
    margin-bottom: 1.5rem;
}

.tab-bar a {
    padding: 0.75rem 1.25rem;
    text-decoration: none;
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.9rem;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: all 0.2s;
}

.tab-bar a:hover { color: var(--text); }
.tab-bar a.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

/* Dashboard Tool Cards */
.tool-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
}

.tool-card {
    background: var(--card);
    border-radius: 12px;
    padding: 2rem;
    box-shadow: 0 1px 4px rgba(0,0,0,0.05);
    text-decoration: none;
    color: var(--text);
    transition: transform 0.2s, box-shadow 0.2s;
    display: block;
}

.tool-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0,0,0,0.1);
}

.tool-card .tool-icon {
    font-size: 2.5rem;
    margin-bottom: 1rem;
}

.tool-card .tool-title {
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
}

.tool-card .tool-desc {
    color: var(--text-muted);
    font-size: 0.9rem;
    line-height: 1.4;
}

/* Bills + Ledger Layout */
.bills-layout {
    display: flex;
    gap: 1.5rem;
    align-items: flex-start;
}

.bills-main {
    flex: 1;
    min-width: 0;
}

.ledger-panel {
    width: 320px;
    flex-shrink: 0;
    background: var(--card);
    border-radius: 10px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.05);
    padding: 1rem;
    position: sticky;
    top: 76px;
    max-height: calc(100vh - 92px);
    overflow-y: auto;
}

.ledger-row {
    display: grid;
    grid-template-columns: 28px 1fr auto auto;
    gap: 0.4rem;
    padding: 0.4rem 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.8rem;
    align-items: center;
}

.ledger-row:last-child {
    border-bottom: none;
}

.ledger-date {
    color: var(--text-muted);
    font-size: 0.75rem;
    text-align: right;
}

.ledger-name {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 500;
}

.ledger-amt {
    text-align: right;
    font-size: 0.75rem;
    white-space: nowrap;
}

.ledger-bal {
    text-align: right;
    font-weight: 700;
    font-size: 0.8rem;
    min-width: 70px;
    white-space: nowrap;
}

.ledger-bal.positive { color: var(--success); }
.ledger-bal.negative { color: var(--danger); }

/* Receipt Cards */
.receipt-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.receipt-card {
    background: var(--card);
    border-radius: 10px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.05);
    display: flex;
    gap: 1rem;
    padding: 1rem;
    align-items: flex-start;
}

.receipt-thumb {
    width: 80px;
    height: 80px;
    border-radius: 8px;
    object-fit: cover;
    flex-shrink: 0;
    cursor: pointer;
}

.receipt-info {
    flex: 1;
    min-width: 0;
}

.image-preview {
    max-width: 100%;
    max-height: 200px;
    border-radius: 8px;
    margin-top: 0.5rem;
}

/* Microsoft SSO Button */
.btn-microsoft {
    background: #2f2f2f;
    color: #fff;
    gap: 0.6rem;
}

.btn-microsoft:hover {
    background: #1a1a1a;
}

.btn-microsoft svg {
    flex-shrink: 0;
}

/* Responsive */
@media (max-width: 768px) {
    .navbar { padding: 0 1rem; }
    .navbar nav { display: none; }
    .container { padding: 1rem; }
    .summary-cards { grid-template-columns: 1fr 1fr; }
    .page-header { flex-direction: column; align-items: flex-start; }
    .tool-grid { grid-template-columns: 1fr; }
    .receipt-grid { grid-template-columns: 1fr; }
    .bills-layout { flex-direction: column; }
    .ledger-panel { width: 100%; position: static; max-height: none; }
}


.policy-footer {
    order: 99;
    margin-top: auto;
    text-align: center;
    font-size: .78em;
    color: #888;
    padding: 10px 0;
    border-top: 1px solid #eee;
    background: var(--card);
}
.policy-footer a { color: #888; text-decoration: none; margin: 0 .25rem; }
.policy-footer a:hover { color: var(--text); text-decoration: underline; }

/* PHASE_BALANCE_STRIP_CSS: bank-balance strip above Bills KPI cards. */
.balance-strip {
    background: var(--card);
    border-radius: 10px;
    padding: 0.85rem 1.15rem;
    margin-bottom: 1rem;
    box-shadow: 0 1px 4px rgba(0,0,0,0.05);
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 1.25rem;
}
.balance-strip .bal-total { display: flex; align-items: baseline; gap: 0.6rem; flex: 0 0 auto; }
.balance-strip .bal-label { font-size: 0.8rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.balance-strip .bal-amount { font-size: 1.4rem; font-weight: 700; color: var(--success); }
.balance-strip .bal-age { font-size: 0.75rem; color: var(--text-muted); }
.balance-strip .bal-accounts { display: flex; flex-wrap: wrap; gap: 0.5rem; flex: 1 1 auto; justify-content: flex-end; }
.balance-strip .bal-acct { font-size: 0.85rem; color: var(--text-muted); padding: 0.15rem 0.6rem; background: var(--background, #f5f5f7); border-radius: 6px; white-space: nowrap; }

/* PHASE_PASTDUE_CSS: past-due banner above bills view. */
.pastdue-banner {
    background: #fff7ed;
    border: 1px solid #fdba74;
    border-radius: 10px;
    margin-bottom: 1rem;
    overflow: hidden;
}
.pastdue-banner > summary {
    list-style: none;
    cursor: pointer;
    padding: 0.85rem 1.15rem;
    display: flex;
    align-items: baseline;
    gap: 0.8rem;
    color: #9a3412;
}
.pastdue-banner > summary::-webkit-details-marker { display: none; }
.pastdue-banner .pd-icon { font-size: 1.1rem; }
.pastdue-banner .pd-label { font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; font-size: 0.85rem; }
.pastdue-banner .pd-amount { font-size: 1.3rem; font-weight: 700; color: var(--danger, #dc2626); }
.pastdue-banner .pd-count { font-size: 0.9rem; color: #9a3412; }
.pastdue-banner .pd-hint { margin-left: auto; font-size: 0.75rem; color: #c2410c; opacity: 0.7; }
.pastdue-banner[open] .pd-hint { display: none; }
.pastdue-banner .pastdue-list {
    width: 100%;
    border-top: 1px solid #fdba74;
    background: #fff;
    border-collapse: collapse;
}
.pastdue-banner .pastdue-list th,
.pastdue-banner .pastdue-list td {
    padding: 0.55rem 1.15rem;
    text-align: left;
    border-bottom: 1px solid #fef3c7;
    font-size: 0.9rem;
}
.pastdue-banner .pastdue-list th { background: #fef3c7; color: #78350f; font-weight: 600; text-transform: uppercase; letter-spacing: 0.3px; font-size: 0.75rem; }
.pastdue-banner .pastdue-list .amt { text-align: right; font-variant-numeric: tabular-nums; color: var(--danger, #dc2626); }
.pastdue-banner .pill.debt { background: #fef3c7; color: #92400e; padding: 1px 6px; border-radius: 4px; font-size: 0.7rem; font-weight: 600; }

