:root {
    --bg: #f3f4f8;
    --surface: #ffffff;
    --border: #e6e8ee;
    --text: #1c1f2a;
    --text-muted: #767c8c;
    --primary: #5b5ff2;
    --primary-dark: #4548d6;
    --primary-soft: #edeeff;
    --success: #0f9d58;
    --success-soft: #e7f7ee;
    --danger: #e0334f;
    --danger-soft: #fdecef;
    --warning: #b4780a;
    --warning-soft: #fdf3e2;
    --sidebar-bg: #171a26;
    --sidebar-text: #b7bbcc;
    --sidebar-text-active: #ffffff;
    --radius: 10px;
    --shadow: 0 1px 3px rgba(20, 22, 40, .06), 0 1px 2px rgba(20, 22, 40, .04);
}

* { box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    margin: 0;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

h1 { font-size: 22px; font-weight: 700; margin: 0 0 18px; }
.section-title { font-size: 14px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em; margin: 0 0 10px; }
.rate-history { max-width: 320px; }
.month-notes-table th:first-child, .month-notes-table td:first-child { width: 160px; white-space: nowrap; }

/* ---------- chart ---------- */

.chart-legend {
    display: flex;
    gap: 18px;
    margin-bottom: 12px;
    font-size: 13px;
    color: var(--text-muted);
}
.chart-legend .dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    display: inline-block;
    margin-right: 6px;
    vertical-align: middle;
}
.chart-legend .dot.received { background: var(--success); }
.chart-legend .dot.owed { background: var(--primary); }

.chart {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px 16px 14px;
    margin-bottom: 28px;
    overflow-x: auto;
}
.chart-col {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 1;
    min-width: 30px;
}
.chart-bar {
    width: 100%;
    max-width: 32px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    border-radius: 4px 4px 0 0;
    overflow: hidden;
    background: var(--bg);
}
.chart-seg.owed { background: var(--primary); }
.chart-seg.received { background: var(--success); }
.chart-bar:hover .chart-seg { filter: brightness(1.1); }
.chart-value { font-size: 11px; font-weight: 700; color: var(--text-muted); margin-bottom: 4px; white-space: nowrap; }
.chart-label { font-size: 11px; color: var(--text-muted); margin-top: 8px; white-space: nowrap; }

/* ---------- shell / sidebar ---------- */

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

.sidebar {
    width: 232px;
    flex-shrink: 0;
    background: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    padding: 20px 14px;
    position: sticky;
    top: 0;
    height: 100vh;
}

.sidebar .brand {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #fff;
    font-weight: 700;
    font-size: 15px;
    text-decoration: none;
    padding: 6px 10px 26px;
}

.brand-mark {
    display: inline-block;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    object-fit: cover;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    padding: 10px 12px;
    border-radius: 8px;
    transition: background .12s ease, color .12s ease;
}

.sidebar-nav a:hover { background: rgba(255,255,255,.06); color: #fff; }

.sidebar-nav a.active {
    background: var(--primary);
    color: var(--sidebar-text-active);
}

.nav-icon { font-size: 15px; line-height: 1; width: 18px; text-align: center; }

.sidebar-image {
    width: 100%;
    border-radius: 10px;
    display: block;
    object-fit: cover;
}

.sidebar .logout {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 13px;
    padding: 10px 12px;
    border-radius: 8px;
    margin-top: 10px;
    border-top: 1px solid rgba(255,255,255,.08);
    padding-top: 16px;
}

.sidebar .logout:hover { color: #fff; }

.content {
    flex: 1;
    padding: 32px 40px;
    max-width: 1100px;
}

.content-plain {
    max-width: none;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

/* ---------- summary cards ---------- */

.summary { display: flex; gap: 16px; margin-bottom: 24px; flex-wrap: wrap; }
.summary .card {
    background: var(--surface);
    padding: 16px 20px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    min-width: 170px;
}
.summary .card .label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em; font-weight: 600; }
.summary .card .value { font-size: 22px; font-weight: 700; margin-top: 6px; }

/* ---------- table ---------- */

table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    margin-bottom: 24px;
}
th, td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--border); font-size: 14px; vertical-align: middle; }
th { background: #fafafb; font-weight: 600; color: var(--text-muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: #fafbff; }
tr.inactive { opacity: .55; }

/* ---------- badges ---------- */

.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    background: #eef0f4;
    color: var(--text-muted);
}
.badge.paid { background: var(--success-soft); color: var(--success); }
.badge.unpaid { background: var(--danger-soft); color: var(--danger); }
.badge.pending { background: var(--warning-soft); color: var(--warning); }

/* ---------- buttons ---------- */

.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 9px 16px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    text-decoration: none;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background .12s ease, transform .05s ease;
}
.btn:hover { background: var(--primary-dark); }
.btn:active { transform: translateY(1px); }
.btn.secondary { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn.secondary:hover { background: #f7f7fb; }
.btn.danger { background: var(--danger-soft); color: var(--danger); border: 1px solid rgba(224, 51, 79, .25); }
.btn.danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.small { padding: 6px 10px; font-size: 13px; }

/* ---------- forms ---------- */

form.auth-form {
    width: 100%;
    max-width: 340px;
    margin: 0 16px;
    background: var(--surface);
    padding: 32px;
    border-radius: 14px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
}
form.auth-form h1 { text-align: center; }
form.auth-form label { display: block; margin-bottom: 16px; font-size: 14px; color: var(--text-muted); font-weight: 500; }
form.auth-form input {
    width: 100%; padding: 10px 12px; margin-top: 6px; border: 1px solid var(--border);
    border-radius: 8px; font-size: 14px; background: var(--bg);
}
form.auth-form input:focus { outline: none; border-color: var(--primary); background: #fff; }
form.auth-form button { width: 100%; padding: 11px; margin-top: 8px; justify-content: center; }

.error { color: var(--danger); font-size: 14px; }
.errors { color: var(--danger); margin-bottom: 12px; background: var(--danger-soft); padding: 10px 14px; border-radius: 8px; }
.actions { margin-bottom: 18px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.month-picker {
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    background: var(--surface);
    color: var(--text);
    margin-left: auto;
}
.month-picker:focus { outline: none; border-color: var(--primary); }

.export-form {
    display: flex;
    gap: 14px;
    align-items: flex-end;
    flex-wrap: wrap;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px 20px;
    margin-bottom: 24px;
}
.export-form label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 13px;
    color: var(--text-muted);
    font-weight: 500;
}
.export-form input[type=month] {
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    background: var(--bg);
    color: var(--text);
}
.export-form input[type=month]:focus { outline: none; border-color: var(--primary); }

.note {
    background: var(--primary-soft);
    color: var(--text);
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 14px;
    margin: -6px 0 20px;
    white-space: pre-line;
}

.form-grid { max-width: 420px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow); }
.form-grid label { display: block; margin-bottom: 16px; font-size: 14px; font-weight: 500; color: var(--text-muted); }
.form-grid input[type=text],
.form-grid input[type=date],
.form-grid input[type=number],
.form-grid textarea {
    padding: 9px 11px;
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 100%;
    margin-top: 6px;
    font-size: 14px;
    font-family: inherit;
    background: var(--bg);
}
.form-grid input:focus,
.form-grid textarea:focus { outline: none; border-color: var(--primary); background: #fff; }
.form-grid textarea { resize: vertical; min-height: 64px; }

textarea.month-note {
    width: 100%;
    min-height: 60px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    background: var(--surface);
    resize: vertical;
    margin-bottom: 18px;
}
textarea.month-note:focus { outline: none; border-color: var(--primary); }

.comment-cell input[type=text] {
    width: 100%;
    min-width: 140px;
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    font-family: inherit;
    background: var(--bg);
}
.comment-cell input[type=text]:focus { outline: none; border-color: var(--primary); background: #fff; }
.comment-cell form { display: flex; gap: 6px; align-items: center; }

.row-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.row-actions form { display: inline-block; }

/* ---------- responsive ---------- */

@media (max-width: 780px) {
    .app-shell { flex-direction: column; }
    .sidebar {
        width: 100%;
        height: auto;
        position: sticky;
        top: 0;
        z-index: 10;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        gap: 8px 10px;
        padding: 10px max(14px, env(safe-area-inset-right)) 10px max(14px, env(safe-area-inset-left));
        padding-top: max(10px, env(safe-area-inset-top));
    }
    .sidebar .brand { padding: 0; order: 1; }
    .sidebar .logout {
        order: 2;
        margin-left: auto;
        border-top: none;
        margin-top: 0;
        padding: 6px 8px;
    }
    .sidebar-nav {
        order: 3;
        flex: 1 1 100%;
        flex-direction: row;
        gap: 4px;
    }
    .sidebar-nav a { flex: 1; justify-content: center; padding: 8px 4px; font-size: 13px; white-space: nowrap; }
    .sidebar-image { display: none; }
    .content {
        padding: 20px 16px;
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }
    table { display: block; overflow-x: auto; }
}
