:root {
    --bg: #f4f6f9;
    --card-bg: #ffffff;
    --border: #e2e6ec;
    --text: #1f2532;
    --text-muted: #6b7280;
    --primary: #3757e8;
    --primary-dark: #2842c4;
    --danger: #dc3545;
    --danger-dark: #b02a37;
    --success-bg: #e8f8ee;
    --success-text: #1a7f43;
    --error-bg: #fdeceb;
    --error-text: #b3261e;
    --radius: 10px;
    --shadow: 0 1px 3px rgba(20, 25, 40, 0.06), 0 1px 2px rgba(20, 25, 40, 0.08);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.5;
}

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

.muted { color: var(--text-muted); }
.muted-small { color: var(--text-muted); font-size: 12.5px; }

/* ---------- Top bar ---------- */
.topbar {
    background: #171b2b;
    color: #fff;
    position: sticky;
    top: 0;
    z-index: 50;
}
.topbar-inner {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 0 20px;
    height: 56px;
}
.brand {
    color: #fff;
    font-weight: 700;
    font-size: 17px;
    letter-spacing: 0.2px;
}
.brand:hover { text-decoration: none; }
.topnav { display: flex; gap: 4px; flex: 1; }
.topnav a {
    color: #cbd0e0;
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
}
.topnav a:hover { color: #fff; text-decoration: none; background: rgba(255,255,255,0.06); }
.topnav a.active { color: #fff; background: var(--primary); }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.admin-name { color: #9aa1b8; font-size: 13.5px; }
.mobile-menu-btn { display: none; background: none; border: none; color: #fff; font-size: 22px; padding: 10px 16px; cursor: pointer; }

/* ---------- Layout ---------- */
.main-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px 20px 60px;
}

.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 24px;
    margin-bottom: 20px;
}
.card h2 { margin-top: 0; }
.card-header-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }

/* ---------- Forms ---------- */
label { display: block; font-weight: 600; font-size: 13.5px; margin: 16px 0 6px; }
label:first-of-type { margin-top: 0; }
.input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14.5px;
    background: #fff;
    color: var(--text);
    font-family: inherit;
}
.input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(55,87,232,0.12); }
.input-small { max-width: 160px; }
.checkbox-label { display: flex; align-items: center; gap: 8px; font-weight: 500; font-size: 14px; margin: 16px 0; }
.checkbox-label input { width: auto; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    border-radius: 8px;
    border: 1px solid transparent;
    background: #eef0f5;
    color: var(--text);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-dark); }
.btn-outline { background: transparent; border-color: rgba(255,255,255,0.25); color: #fff; }
.btn-outline:hover { background: rgba(255,255,255,0.1); }
.btn-small { padding: 6px 12px; font-size: 13px; }
.btn-block { width: 100%; margin-top: 20px; }
.btn-link { background: none; color: var(--primary); padding: 8px 4px; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

/* ---------- Alerts ---------- */
.alert { padding: 12px 14px; border-radius: 8px; font-size: 14px; margin: 14px 0; }
.alert-error { background: var(--error-bg); color: var(--error-text); }
.alert-success { background: var(--success-bg); color: var(--success-text); }

/* ---------- Auth pages ---------- */
.auth-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(160deg, #171b2b 0%, #262c47 100%);
    padding: 20px;
}
.auth-card {
    background: #fff;
    border-radius: 14px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.35);
    padding: 36px 32px;
    width: 100%;
    max-width: 400px;
}
.install-card { max-width: 460px; }
.auth-card h1 { margin: 0 0 4px; font-size: 22px; }
.auth-sub { color: var(--text-muted); margin: 0 0 20px; font-size: 13.5px; }
.auth-card h3 { font-size: 14px; margin: 22px 0 4px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.4px; }

/* ---------- Search panel ---------- */
.search-panel {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px;
    margin-bottom: 18px;
}
.search-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}
.search-row .input { width: auto; }
#datasetSelect { min-width: 200px; flex: 0 0 auto; }
.search-input { flex: 1 1 280px; min-width: 200px; }
.result-meta { margin-top: 10px; font-size: 13.5px; color: var(--text-muted); min-height: 18px; }

/* ---------- Results ---------- */
.results-wrap {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px;
}
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13.8px; }
.data-table th, .data-table td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
.data-table th { color: var(--text-muted); font-weight: 600; font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.3px; }
.data-table tbody tr:hover { background: #f7f9fc; }
.clickable-row { cursor: pointer; }
.no-results { text-align: center; color: var(--text-muted); padding: 30px 0 !important; white-space: normal; }

.pagination { display: flex; gap: 6px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.page-btn {
    min-width: 36px;
    padding: 7px 10px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: #fff;
    cursor: pointer;
    font-size: 13.5px;
}
.page-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.page-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-state .btn { margin-top: 12px; }

/* ---------- All-datasets grouped results ---------- */
.dataset-group { margin-bottom: 24px; }
.dataset-group:last-child { margin-bottom: 0; }
.dataset-group-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 8px;
    padding-bottom: 6px;
    border-bottom: 2px solid var(--border);
}
.dataset-group-head span { color: var(--text-muted); font-size: 12.5px; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 18, 30, 0.55); }
.modal-box {
    position: relative;
    background: #fff;
    border-radius: 12px;
    max-width: 640px;
    width: 100%;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-close { background: none; border: none; font-size: 22px; cursor: pointer; color: var(--text-muted); line-height: 1; padding: 4px; }
.modal-body { padding: 20px; overflow-y: auto; }

.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field-item { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; background: #fafbfd; }
.field-label { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px; color: var(--text-muted); margin-bottom: 4px; }
.field-value { font-size: 14px; word-break: break-word; }

/* ---------- Misc ---------- */
.badge {
    display: inline-block;
    background: #eef0f5;
    color: var(--text);
    font-size: 11px;
    font-weight: 700;
    padding: 3px 8px;
    border-radius: 5px;
    letter-spacing: 0.3px;
}
.actions-cell { display: flex; gap: 6px; white-space: nowrap; }

.upload-progress { display: flex; align-items: center; gap: 10px; margin-top: 16px; color: var(--text-muted); font-size: 14px; }
.spinner {
    width: 18px; height: 18px;
    border: 2.5px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
    .mobile-menu-btn { display: block; margin-left: auto; }
    .topnav {
        position: absolute;
        top: 56px;
        left: 0;
        right: 0;
        background: #171b2b;
        flex-direction: column;
        padding: 8px;
        display: none;
        border-top: 1px solid rgba(255,255,255,0.08);
    }
    .topnav.open { display: flex; }
    .topbar-inner { flex-wrap: wrap; }
    .brand { flex: 1; }

    .search-row { flex-direction: column; }
    .search-row .input, #datasetSelect, .input-small { width: 100%; max-width: none; }

    .field-grid { grid-template-columns: 1fr; }

    .card { padding: 18px; }
    .main-container { padding: 16px 12px 50px; }

    .data-table th, .data-table td { padding: 8px 10px; }
}

@media (max-width: 420px) {
    .auth-card { padding: 26px 20px; }
}
