/* ============ ระบบจัดการข้อมูลผู้สูงอายุ - UI ============ */
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Thai:wght@300;400;500;600;700&display=swap');

:root {
    --primary: #0ea5e9;
    --primary-dark: #0369a1;
    --primary-light: #e0f2fe;
    --secondary: #8b5cf6;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --info: #06b6d4;
    --dark: #0f172a;
    --gray-50: #f8fafc;
    --gray-100: #f1f5f9;
    --gray-200: #e2e8f0;
    --gray-300: #cbd5e1;
    --gray-400: #94a3b8;
    --gray-500: #64748b;
    --gray-600: #475569;
    --gray-700: #334155;
    --gray-800: #1e293b;
    --shadow-sm: 0 1px 2px rgba(15,23,42,.06);
    --shadow: 0 4px 12px rgba(15,23,42,.08);
    --shadow-lg: 0 12px 32px rgba(15,23,42,.12);
    --radius: 14px;
    --radius-sm: 8px;
    --sidebar-w: 270px;
}

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

html, body {
    font-family: 'Noto Sans Thai', 'Sarabun', 'Segoe UI', sans-serif;
    background: #f1f5f9;
    color: var(--gray-800);
    font-size: 16px;
    line-height: 1.65;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--primary-dark); text-decoration: none; }

/* ============ Layout ============ */
.app {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: var(--sidebar-w);
    background: linear-gradient(180deg, #0c4a6e 0%, #075985 50%, #0369a1 100%);
    color: #fff;
    position: fixed;
    top: 0; left: 0; bottom: 0;
    overflow-y: auto;
    z-index: 100;
    box-shadow: 4px 0 24px rgba(15,23,42,.15);
}

.sidebar-brand {
    padding: 28px 22px 22px;
    border-bottom: 1px solid rgba(255,255,255,.12);
    text-align: center;
}
.sidebar-brand .logo {
    width: 64px; height: 64px;
    background: linear-gradient(135deg,#fff,#e0f2fe);
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    margin-bottom: 12px;
    box-shadow: 0 8px 24px rgba(0,0,0,.2);
}
.sidebar-brand h1 {
    font-size: 17px;
    font-weight: 600;
    line-height: 1.35;
    color: #fff;
}
.sidebar-brand p {
    font-size: 13px;
    color: #bae6fd;
    margin-top: 6px;
    line-height: 1.5;
}

.sidebar-menu {
    list-style: none;
    padding: 18px 12px;
}
.sidebar-menu li { margin-bottom: 4px; }
.sidebar-menu a {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    color: #e0f2fe;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 400;
    transition: all .2s;
}
.sidebar-menu a:hover {
    background: rgba(255,255,255,.1);
    color: #fff;
    transform: translateX(2px);
}
.sidebar-menu a.active {
    background: linear-gradient(90deg, rgba(255,255,255,.2), rgba(255,255,255,.05));
    color: #fff;
    font-weight: 500;
    box-shadow: inset 3px 0 0 #fff;
}
.sidebar-menu .menu-icon {
    width: 22px; text-align: center; font-size: 17px;
}
.sidebar-menu .menu-title {
    padding: 18px 16px 8px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #7dd3fc;
    font-weight: 600;
}

/* ============ Main ============ */
.main {
    margin-left: var(--sidebar-w);
    flex: 1;
    min-height: 100vh;
}

.topbar {
    background: #fff;
    padding: 18px 32px;
    box-shadow: var(--shadow-sm);
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 50;
    border-bottom: 1px solid var(--gray-200);
}
.topbar .page-title {
    font-size: 20px;
    font-weight: 600;
    color: var(--gray-800);
}
.topbar .page-title small {
    font-size: 14px;
    color: var(--gray-500);
    font-weight: 400;
    display: block;
    margin-top: 3px;
}
.topbar .user-info {
    display: flex; align-items: center; gap: 12px;
    font-size: 14px; color: var(--gray-600);
}
.topbar .avatar {
    width: 42px; height: 42px;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    border-radius: 50%;
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 600;
}

.content {
    padding: 32px;
    max-width: 100%;
}

/* ============ Cards ============ */
.card {
    background: #fff;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
    margin-bottom: 24px;
}
.card-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
}
.card-header h2 {
    font-size: 18px;
    font-weight: 600;
    color: var(--gray-800);
    display: flex; align-items: center; gap: 10px;
}
.card-body { padding: 24px; }

/* ============ Stats ============ */
.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 20px;
    margin-bottom: 28px;
}
.stat-card {
    background: #fff;
    border-radius: var(--radius);
    padding: 22px;
    box-shadow: var(--shadow);
    position: relative;
    overflow: hidden;
    transition: transform .25s, box-shadow .25s;
}
.stat-card:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-lg);
}
.stat-card::before {
    content: '';
    position: absolute;
    top: 0; right: 0;
    width: 110px; height: 110px;
    border-radius: 50%;
    opacity: .1;
    transform: translate(35%, -35%);
}
.stat-card .stat-icon {
    width: 56px; height: 56px;
    border-radius: 14px;
    display: flex; align-items: center; justify-content: center;
    font-size: 26px;
    color: #fff;
    margin-bottom: 14px;
    box-shadow: 0 8px 20px rgba(0,0,0,.12);
}
.stat-card .stat-label {
    font-size: 15px;
    color: var(--gray-500);
    margin-bottom: 6px;
}
.stat-card .stat-value {
    font-size: 32px;
    font-weight: 700;
    color: var(--gray-800);
}
.stat-card.c1::before { background: var(--primary); }
.stat-card.c1 .stat-icon { background: linear-gradient(135deg, #0ea5e9, #0369a1); }
.stat-card.c2::before { background: var(--success); }
.stat-card.c2 .stat-icon { background: linear-gradient(135deg, #10b981, #047857); }
.stat-card.c3::before { background: var(--warning); }
.stat-card.c3 .stat-icon { background: linear-gradient(135deg, #f59e0b, #b45309); }
.stat-card.c4::before { background: var(--danger); }
.stat-card.c4 .stat-icon { background: linear-gradient(135deg, #ef4444, #b91c1c); }
.stat-card.c5 .stat-icon { background: linear-gradient(135deg, #8b5cf6, #6d28d9); }

/* ============ Buttons ============ */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 11px 20px;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    border: none;
    transition: all .2s;
    text-decoration: none;
    line-height: 1.2;
}
.btn:hover { transform: translateY(-1px); }
.btn-primary {
    background: linear-gradient(135deg, #0ea5e9, #0369a1);
    color: #fff;
    box-shadow: 0 4px 12px rgba(14,165,233,.35);
}
.btn-primary:hover { box-shadow: 0 8px 18px rgba(14,165,233,.45); }
.btn-success {
    background: linear-gradient(135deg, #10b981, #047857);
    color: #fff;
    box-shadow: 0 4px 12px rgba(16,185,129,.35);
}
.btn-warning {
    background: linear-gradient(135deg, #f59e0b, #b45309);
    color: #fff;
}
.btn-danger {
    background: linear-gradient(135deg, #ef4444, #b91c1c);
    color: #fff;
}
.btn-secondary {
    background: var(--gray-200);
    color: var(--gray-700);
}
.btn-secondary:hover { background: var(--gray-300); }
.btn-sm { padding: 8px 14px; font-size: 14px; }
.btn-icon { padding: 8px 10px; }

/* ============ Forms ============ */
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 18px;
}
.form-grid .full { grid-column: 1 / -1; }
@media(max-width: 768px) { .form-grid { grid-template-columns: 1fr; } }

.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label {
    font-size: 15px;
    font-weight: 500;
    color: var(--gray-700);
}
.form-group label .req { color: var(--danger); }
.form-control {
    padding: 12px 16px;
    border: 1.5px solid var(--gray-200);
    border-radius: 10px;
    font-size: 15px;
    font-family: inherit;
    background: #fff;
    color: var(--gray-800);
    transition: all .2s;
}
.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(14,165,233,.12);
}
textarea.form-control {
    resize: vertical;
    min-height: 90px;
}
select.form-control { cursor: pointer; }

.checkbox-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 10px;
}
.checkbox-item {
    display: flex; align-items: center; gap: 8px;
    padding: 10px 14px;
    background: var(--gray-50);
    border: 1.5px solid var(--gray-200);
    border-radius: 10px;
    cursor: pointer;
    transition: all .2s;
}
.checkbox-item:hover { border-color: var(--primary); }
.checkbox-item input { width: 17px; height: 17px; accent-color: var(--primary); }

/* ============ Table ============ */
.table-wrap {
    overflow-x: auto;
    border-radius: var(--radius);
}
.table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
}
.table thead th {
    background: linear-gradient(135deg, #f8fafc, #f1f5f9);
    color: var(--gray-700);
    text-align: left;
    padding: 15px 18px;
    font-weight: 600;
    font-size: 15px;
    border-bottom: 2px solid var(--gray-200);
    white-space: nowrap;
}
.table tbody td {
    padding: 15px 18px;
    border-bottom: 1px solid var(--gray-100);
    font-size: 15px;
    color: var(--gray-700);
    vertical-align: middle;
}
.table tbody tr:hover { background: var(--gray-50); }
.table .actions {
    display: flex; gap: 6px;
}

/* ============ Badge ============ */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    border-radius: 999px;
    font-size: 14px;
    font-weight: 500;
    background: var(--gray-100);
    color: var(--gray-700);
}
.badge.dot::before {
    content: '';
    width: 7px; height: 7px;
    border-radius: 50%;
    background: currentColor;
}

/* ============ Alert ============ */
.alert {
    padding: 15px 20px;
    border-radius: 12px;
    margin-bottom: 20px;
    border-left: 4px solid;
    font-size: 15px;
}
.alert-success {
    background: #ecfdf5;
    color: #065f46;
    border-color: var(--success);
}
.alert-danger {
    background: #fef2f2;
    color: #991b1b;
    border-color: var(--danger);
}
.alert-info {
    background: #ecfeff;
    color: #155e75;
    border-color: var(--info);
}

/* ============ Search bar ============ */
.toolbar {
    display: flex;
    gap: 12px;
    margin-bottom: 18px;
    flex-wrap: wrap;
    align-items: center;
}
.search-box {
    flex: 1;
    min-width: 240px;
    position: relative;
}
.search-box input {
    width: 100%;
    padding: 11px 14px 11px 40px;
    border: 1.5px solid var(--gray-200);
    border-radius: 10px;
    font-family: inherit;
    font-size: 14px;
}
.search-box::before {
    content: '🔍';
    position: absolute;
    top: 50%; left: 14px;
    transform: translateY(-50%);
    font-size: 14px;
}

/* ============ Image gallery ============ */
.image-preview-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
}
.image-preview-grid .img-slot {
    aspect-ratio: 1;
    border: 2px dashed var(--gray-300);
    border-radius: 12px;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    background: var(--gray-50);
    overflow: hidden;
    position: relative;
    cursor: pointer;
    transition: all .2s;
}
.image-preview-grid .img-slot:hover { border-color: var(--primary); }
.image-preview-grid .img-slot img {
    width: 100%; height: 100%;
    object-fit: cover;
}
.image-preview-grid .img-slot input[type=file] {
    position: absolute; opacity: 0;
    width: 100%; height: 100%; cursor: pointer;
}
.image-preview-grid .img-slot .placeholder {
    color: var(--gray-400);
    font-size: 13px;
    text-align: center;
    padding: 10px;
}

/* ============ Detail view ============ */
.detail-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
}
@media(max-width: 768px) { .detail-grid { grid-template-columns: 1fr; } }
.detail-item {
    padding: 14px 0;
    border-bottom: 1px solid var(--gray-100);
}
.detail-item .label {
    font-size: 13px;
    color: var(--gray-500);
    margin-bottom: 5px;
    letter-spacing: .2px;
}
.detail-item .value {
    font-size: 16px;
    color: var(--gray-800);
    font-weight: 500;
}

/* ============ Modal ============ */
.modal-bg {
    position: fixed;
    inset: 0;
    background: rgba(15,23,42,.6);
    z-index: 200;
    display: none;
    align-items: center; justify-content: center;
    padding: 20px;
}
.modal-bg.active { display: flex; }
.modal {
    background: #fff;
    border-radius: 16px;
    width: 100%;
    max-width: 480px;
    box-shadow: 0 25px 60px rgba(0,0,0,.3);
    overflow: hidden;
}
.modal-header {
    padding: 18px 22px;
    border-bottom: 1px solid var(--gray-200);
    display: flex; justify-content: space-between; align-items: center;
}
.modal-header h3 { font-size: 17px; font-weight: 600; color: var(--gray-800); }
.modal-body { padding: 22px; }
.modal-footer {
    padding: 16px 22px;
    border-top: 1px solid var(--gray-200);
    display: flex; gap: 10px; justify-content: flex-end;
    background: var(--gray-50);
}
.close-btn {
    background: none; border: none;
    font-size: 22px; cursor: pointer;
    color: var(--gray-400);
}

/* ============ Map ============ */
#map {
    height: 70vh;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.map-legend {
    display: flex; gap: 18px; flex-wrap: wrap;
    margin-bottom: 16px;
}
.legend-item {
    display: flex; align-items: center; gap: 8px;
    background: #fff;
    padding: 8px 14px;
    border-radius: 999px;
    font-size: 13px;
    box-shadow: var(--shadow-sm);
}
.legend-item .dot {
    width: 12px; height: 12px; border-radius: 50%;
}

/* ============ Mobile Menu Toggle ============ */
.menu-toggle {
    display: none;
    background: linear-gradient(135deg, #0ea5e9, #0369a1);
    color: #fff;
    border: none;
    width: 44px; height: 44px;
    border-radius: 10px;
    font-size: 22px;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(14,165,233,.35);
    align-items: center; justify-content: center;
}
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15,23,42,.6);
    z-index: 99;
}
.sidebar-overlay.active { display: block; }

/* ============ Mobile Responsive ============ */
@media(max-width: 900px) {
    :root { --sidebar-w: 260px; }
    html, body { font-size: 15px; }

    .sidebar {
        transform: translateX(-100%);
        transition: transform .3s ease;
        width: 260px;
    }
    .sidebar.open { transform: translateX(0); }
    .main { margin-left: 0; }

    .menu-toggle { display: inline-flex; }

    .topbar {
        padding: 14px 16px;
        gap: 12px;
    }
    .topbar .page-title { font-size: 17px; flex: 1; min-width: 0; }
    .topbar .page-title small { font-size: 12.5px; }
    .topbar .user-info { display: none; }

    .content { padding: 16px; }

    /* Cards */
    .card { border-radius: 12px; margin-bottom: 16px; }
    .card-header { padding: 14px 16px; }
    .card-header h2 { font-size: 16px; }
    .card-body { padding: 16px; }

    /* Stats - 2 columns on tablet, 1 on phone */
    .stats { grid-template-columns: repeat(2, 1fr); gap: 12px; }
    .stat-card { padding: 16px; }
    .stat-card .stat-icon { width: 44px; height: 44px; font-size: 20px; border-radius: 10px; margin-bottom: 10px; }
    .stat-card .stat-label { font-size: 13px; }
    .stat-card .stat-value { font-size: 22px; }

    /* Form */
    .form-grid { grid-template-columns: 1fr; gap: 14px; }
    .form-control {
        padding: 14px 16px;
        font-size: 16px; /* ป้องกัน iOS auto-zoom */
    }
    .form-group label { font-size: 15px; }

    /* Buttons - touch-friendly */
    .btn {
        padding: 13px 18px;
        font-size: 15px;
        min-height: 46px;
    }
    .btn-sm { padding: 10px 14px; font-size: 14px; min-height: 40px; }

    /* Toolbar */
    .toolbar { flex-direction: column; align-items: stretch; gap: 10px; }
    .toolbar .search-box { min-width: 0; }
    .toolbar .form-control { max-width: 100% !important; }

    /* Tables → scroll แนวนอน + แถวกระชับ */
    .table-wrap { -webkit-overflow-scrolling: touch; }
    .table thead th, .table tbody td {
        padding: 12px 12px;
        font-size: 14px;
    }
    .table .actions { flex-wrap: wrap; }

    /* Alert */
    .alert { padding: 12px 14px; font-size: 14px; }

    /* Detail view */
    .detail-grid { grid-template-columns: 1fr; gap: 0; }

    /* Image grid */
    .image-preview-grid { grid-template-columns: 1fr 1fr; gap: 10px; }

    /* Modal */
    .modal { max-width: calc(100vw - 24px); border-radius: 14px; }
    .modal-body { padding: 18px; }

    /* Map */
    #map { height: 65vh; }
    .map-legend { gap: 8px; }
    .legend-item { font-size: 12px; padding: 6px 10px; }

    /* Login */
    .login-wrap { padding: 32px 24px; border-radius: 16px; }
    .login-wrap h1 { font-size: 19px; }
    .login-logo { width: 76px; height: 76px; font-size: 38px; }
}

@media(max-width: 560px) {
    .content { padding: 12px; }
    .stats { grid-template-columns: 1fr; }
    .image-preview-grid { grid-template-columns: 1fr; }

    /* เปลี่ยนตารางเป็นการ์ด */
    .table.mobile-cards thead { display: none; }
    .table.mobile-cards, .table.mobile-cards tbody,
    .table.mobile-cards tr, .table.mobile-cards td { display: block; width: 100%; }
    .table.mobile-cards tr {
        background: #fff;
        border: 1px solid var(--gray-200);
        border-radius: 12px;
        padding: 12px 14px;
        margin-bottom: 12px;
        box-shadow: var(--shadow-sm);
    }
    .table.mobile-cards td {
        padding: 6px 0;
        border: none;
        font-size: 14px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 10px;
    }
    .table.mobile-cards td::before {
        content: attr(data-label);
        font-weight: 500;
        color: var(--gray-500);
        font-size: 13px;
        flex-shrink: 0;
    }
    .table.mobile-cards td.actions {
        padding-top: 10px;
        margin-top: 6px;
        border-top: 1px solid var(--gray-100);
        justify-content: flex-start;
        gap: 8px;
    }
    .table.mobile-cards td.actions::before { display: none; }
    .table.mobile-cards td[data-label="#"] { display: none; }
}

/* safe area for iOS notch */
@supports(padding: max(0px)) {
    .topbar { padding-top: max(14px, env(safe-area-inset-top)); }
}
