/* ============================================================
   Water Meter System – consolidated styles
   ============================================================ */

:root {
    --bg: #f6f4ef;
    --card: #ffffff;
    --card-bg: #ffffff;
    --text: #17242b;
    --text-muted: #4d626c;
    --line: #dde3e2;
    --input-bg: #ffffff;
    --input-text: #17242b;
    --teal: #0e6e63;
    --teal-dark: #0a534a;
    --amber: #c07a1f;
    --danger: #b3402f;
    --success: #2f6e2f;
    --brand: #0d9488;
    --radius: 10px;
    --ink: #17242b;
    --slate: #4d626c;
    --paper: #f6f4ef;
}

/* Dark theme — high-contrast text and surfaces */
body.dark-mode {
    --bg: #0f172a;
    --card: #1e293b;
    --card-bg: #1e293b;
    --text: #f1f5f9;
    --text-muted: #cbd5e1;
    --line: #334155;
    --input-bg: #0f172a;
    --input-text: #f1f5f9;
    --ink: #f1f5f9;
    --slate: #cbd5e1;
    --paper: #0f172a;
    --teal: #2dd4bf;
    --teal-dark: #14b8a6;
    --danger: #f87171;
    --success: #4ade80;
    --amber: #fbbf24;
    --brand: #2dd4bf;
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
}

body {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    transition: background-color 0.2s ease, color 0.2s ease;
}

/* ── Topbar ─────────────────────────────────────────────── */
.topbar {
    background: var(--teal-dark);
    color: #fff;
    padding: 14px 18px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.topbar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 4px;
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 1.1rem;
    font-weight: 800;
    letter-spacing: 0.3px;
    color: #ffffff;
    background: rgba(0, 0, 0, 0.35);
    border: 1px solid rgba(255, 255, 255, 0.15);
    padding: 8px 14px;
    border-radius: 8px;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Theme toggle — one icon at a time (moon in light, sun in dark) */
.theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    padding: 0;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.15);
    color: #fbbf24;
    border: 1px solid rgba(255, 255, 255, 0.25);
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
}
.theme-toggle:hover {
    background: rgba(255, 255, 255, 0.28);
    transform: translateY(-1px);
}
.theme-toggle .icon-sun,
.theme-toggle .icon-moon {
    display: none !important;
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}
/* Light mode → show moon only (click to go dark) */
body:not(.dark-mode) .theme-toggle .icon-moon {
    display: block !important;
}
/* Dark mode → show sun only (click to go light) */
body.dark-mode .theme-toggle .icon-sun {
    display: block !important;
}
body.dark-mode .theme-toggle {
    color: #fbbf24;
}

/* Logout */
.logout-icon-btn,
a.logout-icon-btn {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 38px !important;
    height: 38px !important;
    padding: 0 !important;
    border-radius: 8px !important;
    background-color: #dc2626 !important;
    color: #ffffff !important;
    border: 1px solid #dc2626 !important;
    text-decoration: none !important;
    transition: all 0.2s ease !important;
}
.logout-icon-btn:hover,
a.logout-icon-btn:hover {
    background-color: #b91c1c !important;
    border-color: #b91c1c !important;
    transform: translateY(-1px);
}

/* Navigation */
.nav {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 6px;
    width: 100%;
}
.nav a {
    text-decoration: none;
    font-size: 0.95rem;
    font-weight: 700;
    padding: 7px 12px;
    border-radius: 6px;
    position: relative;
    transition: all 0.2s ease;
    color: rgba(255, 255, 255, 0.85);
}
.nav a:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.12);
}
.nav a.active {
    background: rgba(255, 255, 255, 0.18);
}
.nav a.active::after {
    content: '';
    position: absolute;
    bottom: -4px;
    left: 10px;
    right: 10px;
    height: 3px;
    border-radius: 2px;
    background-color: currentColor;
    box-shadow: 0 0 8px currentColor;
}
/* Unique tab accent colors */
.nav a[href="index.php"]    { color: #38bdf8; }
.nav a[href="units.php"]    { color: #fbbf24; }
.nav a[href="readings.php"] { color: #34d399; }
.nav a[href="billing.php"]  { color: #c084fc; }
.nav a[href="debts.php"]    { color: #fb7185; }
.nav a[href="messages.php"] { color: #f472b6; }
.nav a[href="settings.php"] { color: #94a3b8; }

/* ── Layout ─────────────────────────────────────────────── */
.container {
    max-width: 1200px;
    width: 100%;
    margin: 0 auto;
    padding: 22px 16px 60px;
    box-sizing: border-box;
}

/* Bills page: full width so the table can fit without horizontal scroll */
body.page-billing .container {
    max-width: 100%;
    padding-left: 8px;
    padding-right: 8px;
}

main, .login-wrap {
    flex: 1 0 auto;
}

h1 { font-size: 1.5rem; margin: 0 0 4px; color: var(--text) !important; }
h2 { font-size: 1.15rem; margin: 28px 0 12px; color: var(--text) !important; }
.subtitle { color: var(--text-muted) !important; margin: 0 0 20px; font-size: 0.95rem; }
.card label,
.field label {
    color: var(--text-muted) !important;
}
.card .subtitle {
    color: var(--text-muted) !important;
}

/* Cards & stats */
.card, .stat {
    background: var(--card);
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 16px;
    width: 100%;
    box-sizing: border-box;
    transition: background-color 0.2s, border-color 0.2s;
}
.card { margin-bottom: 18px; }

.stat-grid, .stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 14px;
    margin-bottom: 24px;
    width: 100%;
}
.stat {
    border-left: 4px solid var(--teal);
    padding: 14px 16px;
}
.stat .value {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text) !important;
}
.stat .label {
    color: var(--text-muted);
    font-size: 0.85rem;
}

/* ── Tables ─────────────────────────────────────────────── */
.table-wrap {
    width: 100%;
    overflow-x: auto; /* fallback only; bills page tries to avoid needing it */
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}
th, td {
    text-align: left;
    padding: 9px 8px;
    border-bottom: 1px solid var(--line);
    vertical-align: middle;
    color: var(--text);
}
th {
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.8rem;
    white-space: nowrap;
}
tr:last-child td { border-bottom: none; }

/* Bills table: compact so everything fits without horizontal scrollbar */
body.page-billing table {
    font-size: 0.78rem;
    table-layout: fixed;
    width: 100%;
}
body.page-billing th,
body.page-billing td {
    padding: 6px 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
/* Column width hints (12 columns) */
body.page-billing th:nth-child(1),
body.page-billing td:nth-child(1) { width: 5%; }   /* Unit */
body.page-billing th:nth-child(2),
body.page-billing td:nth-child(2) { width: 12%; white-space: normal; } /* Tenant */
body.page-billing th:nth-child(3),
body.page-billing td:nth-child(3) { width: 5%; }   /* Units */
body.page-billing th:nth-child(4),
body.page-billing td:nth-child(4) { width: 9%; }   /* Water+Garbage */
body.page-billing th:nth-child(5),
body.page-billing td:nth-child(5) { width: 9%; }   /* Carried in */
body.page-billing th:nth-child(6),
body.page-billing td:nth-child(6) { width: 9%; }   /* Total Due */
body.page-billing th:nth-child(7),
body.page-billing td:nth-child(7) { width: 9%; white-space: normal; } /* Paid */
body.page-billing th:nth-child(8),
body.page-billing td:nth-child(8) { width: 8%; }   /* Date Paid */
body.page-billing th:nth-child(9),
body.page-billing td:nth-child(9) { width: 8%; }   /* Balance */
body.page-billing th:nth-child(10),
body.page-billing td:nth-child(10) { width: 6%; }  /* Message */
body.page-billing th:nth-child(11),
body.page-billing td:nth-child(11) { width: 7%; }  /* Payment */
body.page-billing th:nth-child(12),
body.page-billing td:nth-child(12) { width: 13%; } /* Actions */

body.page-billing .table-wrap {
    overflow-x: hidden; /* no horizontal scrollbar — table is forced to fit */
    width: 100%;
}
/* On very small screens allow scroll as last resort */
@media (max-width: 700px) {
    body.page-billing .table-wrap {
        overflow-x: auto;
    }
    body.page-billing table {
        table-layout: auto;
        min-width: 700px;
    }
}

/* Keep action buttons in a row */
table td:last-child {
    white-space: nowrap;
}
table td:last-child form,
table td:last-child .btn-row {
    display: inline-flex;
    gap: 4px;
    flex-wrap: nowrap;
    align-items: center;
}

/* ── Forms ──────────────────────────────────────────────── */
form .field { margin-bottom: 14px; }
label {
    display: block;
    font-size: 0.88rem;
    font-weight: 600;
    margin-bottom: 5px;
    color: var(--text-muted);
}
input[type="text"],
input[type="number"],
input[type="password"],
input[type="date"],
input[type="month"],
input[type="tel"],
input[type="email"],
select,
textarea {
    width: 100%;
    padding: 10px 11px;
    border: 1px solid var(--line);
    border-radius: 8px;
    font-size: 1rem;
    background: var(--input-bg);
    color: var(--input-text);
    transition: background 0.2s, color 0.2s, border-color 0.2s;
}
input:focus, select:focus, textarea:focus {
    outline: 2px solid var(--teal);
    outline-offset: 1px;
    border-color: var(--teal);
}

/* ── Buttons ────────────────────────────────────────────── */
.btn {
    display: inline-block;
    background: var(--teal);
    color: #fff;
    border: none;
    padding: 10px 16px;
    border-radius: 8px;
    font-size: 0.94rem;
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
    transition: background 0.15s;
}
.btn:hover { background: var(--teal-dark); }
.btn.secondary {
    background: var(--card);
    color: var(--teal);
    border: 1px solid var(--teal);
}
.btn.secondary:hover { background: rgba(13, 148, 136, 0.08); }
body.dark-mode .btn.secondary:hover { background: rgba(20, 184, 166, 0.15); }
.btn.danger { background: var(--danger); color: #fff; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* Smaller action buttons inside tables */
table .btn,
table button.btn {
    padding: 5px 9px;
    font-size: 0.78rem;
}

/* ── Badges & alerts ────────────────────────────────────── */
.badge {
    display: inline-block;
    padding: 2px 9px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
}
.badge.pending { background: #fbe9d2; color: #9a5b12; }
.badge.sent    { background: #d9efe9; color: #0a534a; }
.badge.paid    { background: #dcedd4; color: #2f6e2f; }
.badge.partial { background: #fde0d0; color: #b3402f; }
.badge.unpaid  { background: #f6dcd6; color: #b3402f; }

body.dark-mode .badge.pending { background: #422006; color: #fbbf24; }
body.dark-mode .badge.sent    { background: #134e4a; color: #5eead4; }
body.dark-mode .badge.paid    { background: #14532d; color: #86efac; }
body.dark-mode .badge.partial { background: #431407; color: #fdba74; }
body.dark-mode .badge.unpaid  { background: #450a0a; color: #fca5a5; }

.alert {
    padding: 12px 14px;
    border-radius: 8px;
    margin-bottom: 18px;
    font-size: 0.94rem;
}
.alert.success { background: #dcedd4; color: #2f6e2f; }
.alert.error   { background: #f6dcd6; color: var(--danger); }
body.dark-mode .alert.success { background: #14532d; color: #86efac; }
body.dark-mode .alert.error   { background: #450a0a; color: #fca5a5; }

/* Live calculation preview on Readings */
.calc-preview {
    background: #ecfdf5;
    border: 1px solid #a7f3d0;
    color: #065f46;
}
body.dark-mode .calc-preview {
    background: #14532d;
    border-color: #166534;
    color: #86efac;
}
body.dark-mode .calc-preview strong {
    color: #bbf7d0;
}

.empty-state {
    text-align: center;
    color: var(--text-muted);
    padding: 30px 10px;
}

/* ── Login ──────────────────────────────────────────────── */
.login-wrap {
    max-width: 360px;
    margin: 10vh auto 0;
}

.checkbox-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 0;
    border-bottom: 1px solid var(--line);
}
.checkbox-row input { width: auto; }

/* ── Footer ─────────────────────────────────────────────── */
.footer {
    width: 100%;
    padding: 16px 24px;
    margin-top: auto;
    border-top: 1px solid var(--line);
    background-color: var(--card);
    color: var(--text-muted);
}
.footer-content {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    font-size: 0.85rem;
}
.footer-credit strong { color: var(--brand); }
.footer-install { width: 100%; text-align: center; margin-top: 6px; }

/* ── Install / PWA buttons ──────────────────────────────── */
.btn-install {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #5eead4;
    color: #134e4a;
    border: none;
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    white-space: nowrap;
}
.btn-install:hover { filter: brightness(1.08); }
.btn-install svg { flex-shrink: 0; }

.btn-install-footer {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: rgba(13, 148, 136, 0.12);
    color: #0d9488;
    border: 1px solid rgba(13, 148, 136, 0.35);
    border-radius: 8px;
    padding: 8px 14px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
}
.btn-install-footer:hover { background: rgba(13, 148, 136, 0.2); }
body.dark-mode .btn-install-footer {
    background: rgba(20, 184, 166, 0.15);
    color: #5eead4;
    border-color: rgba(20, 184, 166, 0.35);
}

.install-hint {
    display: block;
    margin-top: 6px;
    font-size: 12px;
    color: var(--text-muted);
}

.btn-install-login {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background: rgba(13, 148, 136, 0.1);
    color: #0d9488;
    border: 1px solid rgba(13, 148, 136, 0.3);
    border-radius: 10px;
    padding: 12px 16px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
}
.btn-install-login:hover { background: rgba(13, 148, 136, 0.18); }

/* ── Message preview (Send Messages) ────────────────────── */
.message-preview-item summary::-webkit-details-marker { display: none; }
.message-preview-item summary::before {
    content: "▸";
    display: inline-block;
    width: 1em;
    color: var(--text-muted);
    transition: transform 0.15s ease;
}
.message-preview-item[open] summary::before {
    transform: rotate(90deg);
}
.message-preview-item .message-body {
    user-select: text;
}

/* ── Dark-mode specific fixes for remaining hard-coded colors ── */
body.dark-mode a:not(.nav a):not(.btn):not(.logout-icon-btn) {
    color: #5eead4;
}
body.dark-mode .subtitle a {
    color: #5eead4;
}
body.dark-mode input[type="month"],
body.dark-mode input[type="date"] {
    color-scheme: dark;
}

/* Ensure all text inherits readable colors */
body.dark-mode,
body.dark-mode p,
body.dark-mode span,
body.dark-mode li,
body.dark-mode td,
body.dark-mode th,
body.dark-mode label,
body.dark-mode small,
body.dark-mode strong {
    color: inherit;
}
body.dark-mode h1,
body.dark-mode h2,
body.dark-mode h3 {
    color: var(--text);
}
body.dark-mode .stat .value {
    color: var(--text) !important;
}
body.dark-mode .stat .label {
    color: var(--text-muted) !important;
}
body.dark-mode label {
    color: var(--text-muted);
}
body.dark-mode th {
    color: var(--text-muted);
}
body.dark-mode td {
    color: var(--text);
}

/* Override common light-only inline alert / preview styles */
body.dark-mode [style*="background:#d1fae5"],
body.dark-mode [style*="background: #d1fae5"],
body.dark-mode [style*="background:#ecfdf5"],
body.dark-mode [style*="background: #ecfdf5"] {
    background: #14532d !important;
    border-color: #166534 !important;
    color: #86efac !important;
}
body.dark-mode [style*="color:#065f46"],
body.dark-mode [style*="color: #065f46"] {
    color: #86efac !important;
}
body.dark-mode [style*="background:#fee2e2"],
body.dark-mode [style*="background: #fee2e2"] {
    background: #450a0a !important;
    border-color: #7f1d1d !important;
    color: #fca5a5 !important;
}
body.dark-mode [style*="color:#991b1b"],
body.dark-mode [style*="color: #991b1b"] {
    color: #fca5a5 !important;
}
body.dark-mode [style*="border: 1px solid #a7f3d0"],
body.dark-mode [style*="border:1px solid #a7f3d0"] {
    border-color: #166534 !important;
}

/* Placeholders, selects, disabled fields */
body.dark-mode ::placeholder {
    color: #94a3b8;
    opacity: 1;
}
body.dark-mode select option {
    background: var(--card);
    color: var(--text);
}
body.dark-mode input[disabled],
body.dark-mode select[disabled],
body.dark-mode textarea[disabled] {
    background: #1a2332 !important;
    color: #94a3b8 !important;
}

/* Message preview / details */
body.dark-mode .message-preview-item {
    background: var(--card) !important;
    border-color: var(--line) !important;
    color: var(--text);
}
body.dark-mode .message-preview-item summary {
    color: var(--text);
}
body.dark-mode .message-body {
    color: var(--text);
}

/* Login page extras */
body.dark-mode .login-wrap .card,
body.dark-mode .login-wrap {
    color: var(--text);
}
body.dark-mode .btn-install-login {
    background: rgba(20, 184, 166, 0.15);
    color: #5eead4;
    border-color: rgba(20, 184, 166, 0.35);
}

/* Fallback for success color used as #0a7 */
body.dark-mode [style*="var(--success, #0a7)"],
body.dark-mode [style*="color:var(--success"] {
    color: var(--success) !important;
}

/* Small utility */
.no-print { }
@media print {
    .no-print, .topbar, .footer, .theme-toggle, .btn-install { display: none !important; }
    body { background: #fff; color: #000; }
    .card { border: 1px solid #ccc; box-shadow: none; }
    table { font-size: 10pt; }
    th { background: #f2f2f2 !important; font-weight: bold; }
    .badge {
        border: 1px solid #000 !important;
        color: #000 !important;
        background: transparent !important;
    }
}

/* Responsive */
@media (max-width: 600px) {
    .topbar { flex-direction: column; align-items: flex-start; }
    .container { padding: 16px 12px 50px; }
    .btn-install .install-label { display: none; }
    .btn-install { padding: 8px; }
    .nav a { font-size: 0.85rem; padding: 6px 8px; }
}
