:root {
    --theme-body: #fff;
    --theme-text: #212529;
    --theme-surface: #fff;
    --theme-raised: #f8f9fa;
    --theme-border: #dee2e6;
    --theme-muted: #6c757d;
    --theme-link: #007bff;
    --theme-link-hover: #0056b3;
    --theme-primary: #007bff;
    --theme-success: #28a745;
    --theme-secondary: #6c757d;
    --theme-warning: #ffc107;
    --theme-highlight: #ffff00;
    --theme-on-accent: #fff;
    --theme-focus: #0056b3;
}

html.dark {
    color-scheme: dark;
    --theme-body: #121820;
    --theme-text: #e6edf3;
    --theme-surface: #1c2632;
    --theme-raised: #273545;
    --theme-border: #526174;
    --theme-muted: #b6c2d0;
    --theme-link: #8ecbff;
    --theme-link-hover: #c9e6ff;
    --theme-primary: #163f66;
    --theme-success: #20533b;
    --theme-secondary: #364558;
    --theme-warning: #604b18;
    --theme-highlight: #665213;
    --theme-focus: #ffd166;
}

/* Existing light-mode colours remain owned by the site's styles. */
html.dark body {
    background-color: var(--theme-body);
    color: var(--theme-text);
}
html.dark a:not(.btn) { color: var(--theme-link); }
html.dark a:not(.btn):hover,
html.dark a:not(.btn):focus { color: var(--theme-link-hover); }

html.dark .navbar,
html.dark .bg-primary { background-color: var(--theme-primary) !important; }
html.dark .bg-success { background-color: var(--theme-success) !important; }
html.dark .bg-secondary { background-color: var(--theme-secondary) !important; }
html.dark .bg-warning { background-color: var(--theme-warning) !important; }

html.dark .as-book,
html.dark .card,
html.dark .list-group-item,
html.dark .dropdown-menu,
html.dark .modal-content,
html.dark .index-list,
html.dark .site-footer,
html.dark footer,
html.dark .bg-white,
html.dark .bg-light {
    background-color: var(--theme-surface) !important;
    color: var(--theme-text);
    border-color: var(--theme-border);
}
html.dark .card-header,
html.dark .card-footer,
html.dark .title-bar,
html.dark .table .thead-light th,
html.dark .table .thead-dark th {
    background-color: var(--theme-raised);
    color: var(--theme-text);
    border-color: var(--theme-border);
}
html.dark table,
html.dark .table,
html.dark pre { color: var(--theme-text); }
html.dark .table { background-color: var(--theme-surface); }
html.dark .table th,
html.dark .table td,
html.dark .table thead th,
html.dark .table tbody + tbody { border-color: var(--theme-border); }
html.dark .table-striped tbody tr:nth-of-type(odd),
html.dark .table-hover tbody tr:hover {
    background-color: var(--theme-raised);
    color: var(--theme-text);
}

html.dark input:not([type="range"]):not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="button"]),
html.dark select,
html.dark textarea,
html.dark .form-control {
    background-color: var(--theme-surface);
    color: var(--theme-text);
    border-color: var(--theme-border);
}
html.dark input::placeholder,
html.dark textarea::placeholder { color: var(--theme-muted); opacity: 1; }
html.dark .form-control:disabled,
html.dark .form-control[readonly],
html.dark .input-group-text,
html.dark .slider { background-color: var(--theme-raised); color: var(--theme-muted); }
html.dark .text-dark,
html.dark .text-body { color: var(--theme-text) !important; }
html.dark .as-note,
html.dark .text-muted { color: var(--theme-muted) !important; }
html.dark .as-book li { border-color: var(--theme-border); }
html.dark .as-results mark,
html.dark .highlight { background-color: var(--theme-highlight); color: var(--theme-text); }
html.dark .btn-outline-success { color: #9ce0b5; border-color: #9ce0b5; }
html.dark .btn-outline-success:hover,
html.dark .btn-outline-success:active { background-color: var(--theme-success); color: var(--theme-on-accent); }
html.dark :focus-visible { outline: 3px solid var(--theme-focus); outline-offset: 3px; }

#dark-mode-toggle {
    position: fixed;
    left: 16px;
    bottom: 16px;
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    z-index: 1030;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    padding: 10px;
    border: 2px solid var(--theme-border);
    border-radius: 50%;
    background-color: var(--theme-surface);
    color: var(--theme-link-hover);
    box-shadow: 0 2px 8px rgba(0, 0, 0, .25);
    cursor: pointer;
    touch-action: manipulation;
}
#dark-mode-toggle:hover { background-color: var(--theme-raised); }
#dark-mode-toggle:focus { outline: 3px solid var(--theme-focus); outline-offset: 3px; }
#dark-mode-toggle svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#dark-mode-toggle .theme-sun,
html.dark #dark-mode-toggle .theme-moon { display: none; }
html.dark #dark-mode-toggle .theme-sun { display: block; }

/* The last content can scroll clear of both floating controls. */
html.theme-toggle-ready { scroll-padding-bottom: calc(80px + env(safe-area-inset-bottom, 0px)); }
html.theme-toggle-ready body { padding-bottom: calc(80px + env(safe-area-inset-bottom, 0px)); }

@media (max-width: 767.98px) {
    /* A solid bottom strip keeps text from showing behind the controls. */
    html.theme-toggle-ready body::after {
        content: "";
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        height: calc(80px + env(safe-area-inset-bottom, 0px));
        background-color: var(--theme-body);
        border-top: 1px solid var(--theme-border);
        z-index: 1020;
    }
    body { padding-left: 16px; padding-right: 16px; overflow-wrap: anywhere; }
    .navbar, .navbar-collapse, .navbar form { min-width: 0; max-width: 100%; }
    .navbar .form-control { min-width: 0; max-width: 100%; }
    .table { width: 100%; table-layout: fixed; }
    .table th, .table td { overflow-wrap: anywhere; }
    .flag-counter { max-width: 100%; height: auto; }
    .index-list { left: 16px; width: calc(100% - 32px); max-width: calc(100% - 32px); }
}

@media print {
    #dark-mode-toggle, html.theme-toggle-ready body::after { display: none; }
    html.theme-toggle-ready body { padding-bottom: 0; }
}

/* Book pages (book.php): their own inline colours need dark overrides. */
html.dark .arabic-text,
html.dark .arabic-text pre,
html.dark .urdu-text,
html.dark .urdu-text pre,
html.dark .page-text pre,
html.dark .hadith-body .font-hadith { color: var(--theme-text); }
html.dark .hadith-body .heading-color { color: #7fd3a8; }
html.dark .meta-info { background: var(--theme-surface); }
html.dark .meta-info h5 { color: #7fd3a8; }
html.dark .meta-info p { color: var(--theme-text); }
html.dark .kitab-name { color: #b9a7ff; }
html.dark .baab-name { color: var(--theme-muted); }
html.dark .index-list { background-color: var(--theme-surface); color: var(--theme-text); }
html.dark .slider { background: var(--theme-raised); }

/* Footer: keep the flag counter small and keep the footer at the bottom of short pages. */
.site-footer .flag-counter { display: block; width: auto; max-width: 240px; height: auto; margin: 0 auto; }
html.theme-toggle-ready body { min-height: 100vh; display: flex; flex-direction: column; }
html.theme-toggle-ready body > .site-footer { margin-top: auto; }
