@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Thai:wght@400;500;600;700&display=swap');

:root {
    --background: #f8fafc;
    --foreground: #0f172a;
    --card-bg: #ffffff;
    --card-border: #e2e8f0;
}
@media (prefers-color-scheme: dark) {
    :root { --background: #0b1220; --foreground: #e2e8f0; --card-bg: #0f172a; --card-border: #1e293b; }
}
html[data-theme="dark"] { --background: #0b1220; --foreground: #e2e8f0; --card-bg: #0f172a; --card-border: #1e293b; }
html[data-theme="light"] { --background: #f8fafc; --foreground: #0f172a; --card-bg: #ffffff; --card-border: #e2e8f0; }

/* ขนาดฟอนต์พื้นฐานปรับใหญ่ขึ้นจากต้นฉบับ (18px -> 21px) เพื่อให้ผู้สูงอายุอ่านง่ายขึ้นโดยไม่ต้องซูมหน้าจอ */
* { box-sizing: border-box; }
html { font-size: 21px; }
body {
    margin: 0;
    font-family: "Noto Sans Thai", "Prompt", ui-sans-serif, system-ui, sans-serif;
    background: var(--background);
    color: var(--foreground);
    line-height: 1.7;
}
input, select, textarea, button { font-size: 1rem; font-family: inherit; }
a { color: #1d4ed8; text-decoration: none; transition: color 0.15s ease; }
@media (prefers-color-scheme: dark) { a { color: #60a5fa; } }
html[data-theme="dark"] a { color: #60a5fa; }
button, .btn-primary, .btn-secondary, .btn-logout, .nav-link, .card, .village-row, .dashboard-tile {
    transition: background-color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease, color 0.15s ease;
}

/* ---------- หน้า Login ---------- */
.login-page {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 32px 16px;
    max-width: 440px;
    margin: 0 auto;
    gap: 24px;
}
.login-header { display: flex; flex-direction: column; align-items: center; gap: 12px; text-align: center; }
.login-header img { height: 68px; width: auto; }
@media (min-width: 640px) { .login-header img { height: 84px; } }
.login-header .eyebrow { font-size: 1.05rem; font-weight: 600; color: #1d4ed8; margin: 0; }
.login-header h1 { font-size: 1.8rem; font-weight: 700; margin: 0; color: var(--foreground); }
.login-header .lede { font-size: 1.05rem; color: #475569; margin: 0; }

.login-card {
    width: 100%;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 16px;
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.form-field { margin-bottom: 16px; }
.form-field label { display: block; font-size: 1rem; font-weight: 600; margin-bottom: 6px; color: var(--foreground); }
.form-field input, .form-field select {
    width: 100%;
    min-height: 52px;
    padding: 10px 14px;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    font-size: 1.1rem;
    background: var(--card-bg);
    color: var(--foreground);
}
html[data-theme="dark"] .form-field input, html[data-theme="dark"] .form-field select { border-color: #334155; }
.form-field input:disabled, .form-field select:disabled { background: #f1f5f9; color: #64748b; }
.form-field input:focus, .form-field select:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.18);
}
a:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible {
    outline: 2px solid #2563eb;
    outline-offset: 2px;
}

.btn-primary {
    width: 100%;
    min-height: 56px;
    background: linear-gradient(135deg, #2563eb, #1d4ed8);
    color: #fff;
    border: none;
    border-radius: 12px;
    font-weight: 700;
    font-size: 1.15rem;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(29, 78, 216, 0.25);
}
.btn-primary:hover { background: linear-gradient(135deg, #1d4ed8, #1e40af); box-shadow: 0 6px 16px rgba(29, 78, 216, 0.32); transform: translateY(-1px); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; box-shadow: none; transform: none; }

.btn-secondary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 52px;
    padding: 0 20px;
    background: #f1f5f9;
    color: #1e293b;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    font-weight: 600;
    font-size: 1.05rem;
    cursor: pointer;
}
html[data-theme="dark"] .btn-secondary { background: #1e293b; color: #e2e8f0; border-color: #334155; }
.btn-secondary:hover { border-color: #1d4ed8; color: #1d4ed8; }

.alert-error { background: #fef2f2; border: 1px solid #fecaca; color: #dc2626; border-radius: 8px; padding: 12px 14px; font-size: 1rem; font-weight: 500; }
.forgot-link { display: block; text-align: center; font-size: 1rem; font-weight: 600; padding: 6px 0; }

/* ---------- โครงร่างหลัก: Sidebar (desktop) + TopNav (mobile) ---------- */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    width: 280px;
    flex-shrink: 0;
    display: none;
    flex-direction: column;
    background: var(--card-bg);
    border-right: 1px solid var(--card-border);
}
@media (min-width: 768px) { .sidebar { display: flex; } }

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 18px 20px;
    border-bottom: 1px solid var(--card-border);
}
.sidebar-brand { display: flex; align-items: center; gap: 10px; font-size: 1.1rem; font-weight: 700; color: #1e40af; }
html[data-theme="dark"] .sidebar-brand { color: #60a5fa; }
.sidebar-brand img { height: 36px; width: 36px; border-radius: 999px; object-fit: cover; }

.sidebar-nav { flex: 1; overflow-y: auto; padding: 16px 10px; display: flex; flex-direction: column; gap: 3px; }
.nav-link {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 52px;
    padding: 6px 14px;
    border-radius: 12px;
    font-size: 1.02rem;
    font-weight: 600;
    color: #334155;
    border-left: 3px solid transparent;
}
html[data-theme="dark"] .nav-link { color: #cbd5e1; }
.nav-link:hover { background: #f1f5f9; }
html[data-theme="dark"] .nav-link:hover { background: #1e293b; }
.nav-link.active { background: #eff6ff; color: #1e40af; border-left-color: #1d4ed8; }
html[data-theme="dark"] .nav-link.active { background: #172554; color: #93c5fd; border-left-color: #60a5fa; }

.nav-text { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.nav-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-sublabel { font-size: 0.78rem; font-weight: 500; color: #64748b; }
html[data-theme="dark"] .nav-sublabel { color: #94a3b8; }
.nav-link.active .nav-sublabel { color: #3b82f6; }

.nav-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    font-size: 1.05rem;
    flex-shrink: 0;
}
.nav-icon-purple { border-radius: 999px; background: #ede9fe; }
.nav-icon-green { border-radius: 999px; background: #d1fae5; }
.nav-icon-yellow { border-radius: 999px; background: #fef3c7; }
.nav-icon-brown { border-radius: 999px; background: #f1e0c4; }
html[data-theme="dark"] .nav-icon-purple { background: rgba(76, 29, 149, 0.6); }
html[data-theme="dark"] .nav-icon-green { background: rgba(6, 95, 70, 0.6); }
html[data-theme="dark"] .nav-icon-yellow { background: rgba(120, 53, 15, 0.6); }
html[data-theme="dark"] .nav-icon-brown { background: rgba(90, 68, 38, 0.7); }

.sidebar-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 14px 12px;
    border-top: 1px solid var(--card-border);
}
.sidebar-footer .profile-link { flex: 1; min-width: 0; }

.topnav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 64px;
    padding: 0 16px;
    background: var(--card-bg);
    border-bottom: 1px solid var(--card-border);
    position: sticky;
    top: 0;
    z-index: 30;
}
@media (min-width: 768px) { .topnav { display: none; } }
.topnav-brand { display: flex; align-items: center; gap: 8px; font-size: 1.05rem; font-weight: 700; color: #1e40af; }
html[data-theme="dark"] .topnav-brand { color: #60a5fa; }
.topnav-brand img { height: 32px; width: 32px; border-radius: 999px; object-fit: cover; }
.topnav-actions { display: flex; align-items: center; gap: 4px; }
.icon-btn { border: none; background: none; min-height: 52px; min-width: 52px; font-size: 1.3rem; cursor: pointer; color: var(--foreground); }

.mobile-nav-panel { display: none; border-top: 1px solid var(--card-border); background: var(--card-bg); padding: 12px 16px; }
.mobile-nav-panel.open { display: flex; flex-direction: column; gap: 2px; }
.mobile-nav-panel .nav-link { font-size: 1.1rem; padding: 12px 12px; }
.mobile-nav-footer { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 14px 4px 4px; border-top: 1px solid var(--card-border); margin-top: 8px; font-size: 1rem; }

.profile-link {
    font-size: 0.98rem;
    font-weight: 600;
    color: var(--foreground) !important;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
}
.btn-logout {
    display: inline-flex;
    align-items: center;
    min-height: 48px;
    border: 1px solid #cbd5e1;
    border-radius: 999px;
    padding: 0 16px;
    font-size: 0.95rem;
    font-weight: 600;
    color: #334155 !important;
    background: var(--card-bg);
    cursor: pointer;
}
html[data-theme="dark"] .btn-logout { border-color: #334155; color: #cbd5e1 !important; }
.btn-logout:hover { border-color: #f43f5e; color: #f43f5e !important; }

/* ---------- Page container ---------- */
.page-container { max-width: 1080px; margin: 0 auto; padding: 24px 16px; display: flex; flex-direction: column; gap: 20px; flex: 1; }
.page-back-link { font-size: 1rem; font-weight: 600; color: #1d4ed8; }
html[data-theme="dark"] .page-back-link { color: #60a5fa; }
.page-title { font-size: 1.7rem; font-weight: 700; margin: 4px 0 0; color: var(--foreground); }
.page-subtitle { font-size: 1.02rem; color: #475569; margin: 4px 0 0; }

.card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 20px;
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.04), 0 8px 24px rgba(15, 23, 42, 0.03);
    padding: 24px;
}
.card h2 { font-size: 1.25rem; font-weight: 700; margin: 0 0 6px; color: var(--foreground); }
.card p.desc { margin: 0 0 16px; color: #475569; font-size: 1rem; }

/* ---------- ธีมสีตามเล่มบัญชี (theme.ts) ---------- */
.card-purple { background: #f5f3ff; border-color: #ddd6fe; }
html[data-theme="dark"] .card-purple { background: rgba(46, 16, 101, 0.4); border-color: #5b21b6; }
.card-brown { background: #fbf4ec; border-color: #e4cba3; }
html[data-theme="dark"] .card-brown { background: rgba(58, 44, 30, 0.5); border-color: #6b4f30; }
.card-green { background: #ecfdf5; border-color: #a7f3d0; }
html[data-theme="dark"] .card-green { background: rgba(6, 78, 59, 0.4); border-color: #065f46; }
.card-yellow { background: #fffbeb; border-color: #fde68a; }
html[data-theme="dark"] .card-yellow { background: rgba(69, 39, 4, 0.4); border-color: #92400e; }

.book-heading-purple { color: #4c1d95; } html[data-theme="dark"] .book-heading-purple { color: #ddd6fe; }
.book-heading-brown { color: #5a3a1e; } html[data-theme="dark"] .book-heading-brown { color: #e4cba3; }
.book-heading-green { color: #065f46; } html[data-theme="dark"] .book-heading-green { color: #a7f3d0; }
.book-heading-yellow { color: #78350f; } html[data-theme="dark"] .book-heading-yellow { color: #fde68a; }

.chip-purple { background: #7c3aed; color: #fff; }
.chip-brown { background: #8b5e34; color: #fff; }
.chip-green { background: #059669; color: #fff; }
.chip-yellow { background: #f59e0b; color: #451a03; }

.badge-purple { background: #ede9fe; color: #5b21b6; }
html[data-theme="dark"] .badge-purple { background: rgba(76, 29, 149, 0.6); color: #c4b5fd; }
.badge-brown { background: #f1e0c4; color: #6b4423; }
html[data-theme="dark"] .badge-brown { background: rgba(90, 68, 38, 0.7); color: #d2ad7c; }
.badge-green { background: #d1fae5; color: #065f46; }
html[data-theme="dark"] .badge-green { background: rgba(6, 95, 70, 0.6); color: #6ee7b7; }
.badge-yellow { background: #fef3c7; color: #78350f; }
html[data-theme="dark"] .badge-yellow { background: rgba(120, 53, 15, 0.6); color: #fcd34d; }

/* ---------- ฟอร์มเพิ่มหมู่บ้าน (Hybrid Cascading Dropdown) ---------- */
.address-form {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: flex-end;
    background: var(--background);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    padding: 16px;
}
.address-form .form-field { margin-bottom: 0; width: 220px; }
.address-form .form-field.narrow { width: 120px; }
.address-form .locked-value {
    min-height: 52px;
    display: flex;
    align-items: center;
    padding: 0 12px;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    background: #f1f5f9;
    color: #1e293b;
    font-size: 1.05rem;
}
.address-form button[type="submit"] {
    min-height: 52px;
    padding: 0 20px;
    background: #059669;
    color: #fff;
    border: none;
    border-radius: 8px;
    font-weight: 700;
    font-size: 1.05rem;
    cursor: pointer;
}
.address-form button[type="submit"]:disabled { opacity: 0.6; cursor: not-allowed; }

.village-list { margin-top: 16px; display: flex; flex-direction: column; gap: 10px; }
.village-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    border: 1px solid var(--card-border);
    border-radius: 14px;
    padding: 14px 18px;
    font-size: 1.05rem;
    background: var(--card-bg);
}
.village-row:hover { border-color: #cbd5e1; box-shadow: 0 2px 8px rgba(15, 23, 42, 0.05); }
html[data-theme="dark"] .village-row:hover { border-color: #334155; }
.village-row .village-meta { color: #64748b; font-size: 0.92rem; }

.badge { display: inline-block; padding: 3px 11px; border-radius: 999px; font-size: 0.85rem; font-weight: 700; }
.badge-readonly { background: #f1f5f9; color: #475569; }
html[data-theme="dark"] .badge-readonly { background: #1e293b; color: #94a3b8; }

.dashboard-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 16px; }
.dashboard-tile {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-left: 4px solid #2563eb;
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.04);
}
.dashboard-tile:hover { box-shadow: 0 6px 18px rgba(15, 23, 42, 0.07); transform: translateY(-2px); }
.dashboard-tile .tile-value { font-size: 1.9rem; font-weight: 700; color: #1d4ed8; }
.dashboard-tile .tile-label { font-size: 0.95rem; color: #475569; margin-top: 4px; }

/* ---------- กระดิ่งแจ้งเตือน ---------- */
.notif-bell { position: relative; }
.notif-bell-btn { position: relative; border: none; background: none; font-size: 1.35rem; cursor: pointer; min-height: 52px; min-width: 52px; }
.notif-badge { position: absolute; top: 2px; right: 2px; background: #f43f5e; color: #fff; border-radius: 999px; font-size: 0.7rem; font-weight: 700; padding: 2px 6px; }
.notif-dropdown {
    position: absolute;
    right: 0;
    left: 0;
    top: 100%;
    margin-top: 6px;
    width: 340px;
    max-height: 420px;
    overflow-y: auto;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(15, 23, 42, 0.15);
    z-index: 50;
}
/* sidebar-footer อยู่ล่างสุดของจอ (height:100vh) — ถ้าเปิด dropdown ลงล่างตามปกติจะทะลุขอบจอ มองไม่เห็นเลย */
.notif-dropdown-up { top: auto; bottom: 100%; margin-top: 0; margin-bottom: 6px; }
.notif-dropdown-header { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--card-border); font-size: 0.92rem; font-weight: 700; }
.notif-dropdown-header a { font-weight: 500; font-size: 0.85rem; }
.notif-item { display: block; padding: 12px 16px; border-bottom: 1px solid var(--card-border); font-size: 0.92rem; color: var(--foreground) !important; }
.notif-item:hover { background: var(--background); }
.notif-message { margin-top: 4px; }
.notif-empty { padding: 18px; text-align: center; color: #64748b; font-size: 0.92rem; }
.notif-badge-alert { background: #ffe4e6; color: #be123c; }
.notif-badge-reminder { background: #fef3c7; color: #b45309; }

/* ---------- ตารางจัดเรียงได้ (table-sort.js) ---------- */
table.sortable thead th.sortable-th { cursor: pointer; user-select: none; position: relative; padding-right: 22px !important; }
table.sortable thead th.sortable-th:hover { background: #e2e8f0; }
html[data-theme="dark"] table.sortable thead th.sortable-th:hover { background: #1e293b; }
table.sortable thead th.sortable-th::after { content: '↕'; position: absolute; right: 6px; opacity: 0.35; font-size: 0.85em; }
table.sortable thead th.sorted-asc::after { content: '▲'; opacity: 1; color: #1d4ed8; }
table.sortable thead th.sorted-desc::after { content: '▼'; opacity: 1; color: #1d4ed8; }

/* ---------- แดชบอร์ดภาวะหนี้สิน (chart.js) ---------- */
.chart-card { min-height: 320px; }
.chart-card canvas { max-height: 320px; }
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 20px; }

/* ---------- ระบบค้นหา + สลับมุมมองตาราง/กล่อง (list-toggle.js) ---------- */
.list-toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.list-search-input {
    flex: 1;
    min-width: 200px;
    min-height: 48px;
    padding: 10px 14px;
    border: 1px solid #cbd5e1;
    border-radius: 10px;
    font-size: 1rem;
    background: var(--card-bg);
    color: var(--foreground);
}
html[data-theme="dark"] .list-search-input { border-color: #334155; }
.list-view-buttons { display: flex; gap: 6px; flex-shrink: 0; }
.list-view-buttons button {
    min-height: 48px;
    padding: 0 16px;
    border: 1px solid #cbd5e1;
    border-radius: 10px;
    background: var(--card-bg);
    color: #475569;
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
}
html[data-theme="dark"] .list-view-buttons button { border-color: #334155; color: #cbd5e1; }
.list-view-buttons button.active { background: #2563eb; border-color: #2563eb; color: #fff; }
.list-empty-notice { display: none; color: #64748b; font-size: 0.95rem; padding: 16px 0; text-align: center; }
