/*
 * CAMS — django-unfold overrides
 *
 * Loaded after unfold's own CSS (see core/theme.py: get_unfold_config
 * returns STYLES=["css/unfold-overrides.css"]).
 *
 * Two roles:
 *  1. CAMS-specific polish on top of unfold's Tailwind-based styles.
 *  2. Fallback CSS for the legacy custom sidebar (.sidebar-category*)
 *     in templates/admin/base_site.html, which has no rules from
 *     django-unfold's own stylesheet.
 *
 * Note on Unfold's actual form markup (inspected from a live form):
 *   <form>
 *     <div class="... form-rows">            ← fieldset wrapper card
 *       <div class="field-row form-row">     ← per-field row
 *         <div class="field-line flex-col flex"> ← label + input as siblings
 *           <div><label>Formulation:</label></div>
 *           <div><select>...</select></div>
 *         </div>
 *       </div>
 *     </div>
 *   </form>
 * Unfold sets vertical (flex-col) by default. We override to flex-row
 * with a fixed-width label column to get the classic aligned form look.
 */

:root {
    --cams-font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
    --cams-sidebar-bg: #ffffff;
    --cams-sidebar-border: #e5e7eb;
    --cams-sidebar-text: #374151;
    --cams-sidebar-muted: #6b7280;
    --cams-sidebar-hover: #f3f4f6;
    --cams-sidebar-active: #eef2ff;
    --cams-form-label-col: 12rem;
}

body {
    font-family: var(--cams-font-family);
}

/* Tighter form spacing in changelist */
.module table {
    border-collapse: collapse;
}

/* Subtle brand accent on the login page submit button */
.submit-row input[type="submit"] {
    font-weight: 600;
}

/* Mobile-friendly toolbar */
@media (max-width: 768px) {
    #header { padding: 0.5rem 0.75rem; }
    .breadcrumbs { font-size: 0.85rem; }
}

/* ===================================================================
 * Legacy custom sidebar (templates/admin/base_site.html)
 * ----------------------------------------------------------------
 * django-unfold's own CSS does not style these classes — they're a
 * bespoke markup produced by our base_site.html. Without these rules
 * the sidebar collapses to a flat bulleted list.
 * =================================================================== */

/* Reset <ul> bullets that the browser injects by default */
#nav-sidebar,
#nav-sidebar ul,
#nav-sidebar li {
    list-style: none !important;
    margin: 0;
    padding: 0;
}

#nav-sidebar ul ul { padding: 0; }

/* The whole sidebar panel */
#nav-sidebar {
    background: var(--cams-sidebar-bg) !important;
    color: var(--cams-sidebar-text);
    font-size: 0.875rem;
    line-height: 1.4;
    width: 260px !important;
    min-width: 260px;
    border-right: 1px solid var(--cams-sidebar-border);
    flex-shrink: 0;
}

/* One section: a category header + its collapsible list */
.sidebar-category {
    margin: 0;
    padding: 0;
    border-bottom: 1px solid var(--cams-sidebar-border);
}

.sidebar-category:last-child {
    border-bottom: none;
}

/* Category header (clickable, with the down-arrow ▼ indicator) */
.sidebar-category-header {
    display: flex !important;
    align-items: center;
    gap: 0.5rem;
    padding: 0.7rem 1rem;
    margin-top: 0.75rem;
    font-weight: 700 !important;
    font-size: 0.75rem !important;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--cams-sidebar-text) !important;
    cursor: pointer;
    user-select: none;
    background: var(--cams-sidebar-hover);
    border-top: 1px solid var(--cams-sidebar-border);
    transition: background-color 0.15s ease;
}

.sidebar-category:first-of-type .sidebar-category-header { margin-top: 0; border-top: none; }

.sidebar-category-header:hover {
    background: #e5e7eb;
}

.sidebar-category-icon {
    display: inline-block !important;
    width: 1rem;
    font-size: 0.7rem;
    color: var(--cams-sidebar-muted);
    transition: transform 0.15s ease;
    flex-shrink: 0;
}

.sidebar-category-header.collapsed .sidebar-category-icon {
    transform: rotate(-90deg);
}

/* The list of links under each category */
.sidebar-category-items {
    overflow: hidden;
    max-height: 1500px;
    transition: max-height 0.2s ease;
}

.sidebar-category-items.collapsed {
    max-height: 0;
}

.sidebar-category-items > ul {
    padding: 0.35rem 0 0.5rem 0 !important;
}

.sidebar-category-items li {
    margin: 0;
    list-style: none !important;
}

.sidebar-category-items a {
    display: block !important;
    padding: 0.4rem 1rem 0.4rem 2rem !important;
    color: var(--cams-sidebar-muted) !important;
    text-decoration: none !important;
    font-size: 0.85rem !important;
    transition: background-color 0.1s ease, color 0.1s ease;
    border-left: 3px solid transparent;
}

.sidebar-category-items a:hover {
    background: var(--cams-sidebar-hover) !important;
    color: var(--cams-sidebar-text) !important;
}

/* Active link — Django marks the current page's link with a class; we
   approximate it with a left accent + bg tint */
.sidebar-category-items a.active,
.sidebar-category-items a[aria-current="page"] {
    background: var(--cams-sidebar-active) !important;
    color: var(--cams-sidebar-text) !important;
    font-weight: 600;
    border-left-color: var(--cams-sidebar-text);
}

/* Mobile hamburger trigger */
.mobile-menu-toggle {
    display: none;
}

@media (max-width: 1024px) {
    #nav-sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        width: 280px;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        z-index: 40;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    }
    #nav-sidebar.show { transform: translateX(0); }
    .mobile-menu-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        position: fixed;
        top: 0.75rem;
        left: 0.75rem;
        z-index: 50;
        background: var(--cams-sidebar-bg);
        border: 1px solid var(--cams-sidebar-border);
        border-radius: 6px;
        width: 2.25rem;
        height: 2.25rem;
        cursor: pointer;
    }
}

#sidebarOverlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 35;
}
#sidebarOverlay.show { display: block; }

/* ===================================================================
 * Form layout — refit to Unfold's actual emitted classes.
 *
 * Unfold's change_form wraps each field in:
 *   <div class="field-row form-row">           ← row container
 *     <div class="field-line flex flex-col">   ← vertical by default
 *       <div><label>...</label></div>          ← label
 *       <div>...input...</div>                 ← input
 *     </div>
 *   </div>
 *
 * We override .field-line to flex-row with a 12rem label column.
 * =================================================================== */

.form-rows,
.aligned {
    border-color: var(--cams-sidebar-border) !important;
}

/* Per-row container — give it horizontal layout so label-column + input-column
   sit side by side. Without this, Unfold's flex-col on .field-line collapses
   everything into a single vertical stack. */
.field-row.form-row {
    border-bottom: 1px solid #f3f4f6;
    display: flex;
    flex-direction: row;
    align-items: flex-start;
}

.field-row.form-row:last-child {
    border-bottom: none;
}

/* The field-line: switch from flex-col to flex-row with a label column.
   Unfold sets `flex flex-col` on this element; we override with !important
   so the aligned label/input layout wins. */
.field-line,
div[class*="field-line"] {
    display: flex !important;
    flex-direction: row !important;
    align-items: flex-start !important;
    gap: 1rem !important;
}

/* The label's wrapping div: fixed-width right-aligned label column.
   Unfold wraps <label> in a generic <div> (no class); we target it
   via the first-child div inside .field-line. */
.field-line > div:first-child,
div[class*="field-line"] > div:first-child {
    flex: 0 0 var(--cams-form-label-col) !important;
    flex-basis: var(--cams-form-label-col) !important;
    width: var(--cams-form-label-col) !important;
    max-width: var(--cams-form-label-col) !important;
    padding: 0.5rem 0.75rem 0.5rem 0 !important;
    text-align: right !important;
    color: var(--cams-sidebar-text);
    font-weight: 600;
    font-size: 0.8125rem;
    line-height: 1.4;
}

.field-line > div:first-child label,
div[class*="field-line"] > div:first-child label {
    font-weight: 600 !important;
    color: var(--cams-sidebar-text);
}

.field-line > div:last-child,
div[class*="field-line"] > div:last-child {
    flex: 1 1 auto !important;
    min-width: 0;
    padding: 0.4rem 0 !important;
    width: auto !important;
}

/* Help text — Unfold puts it inside the input div with .help class */
.field-line .help,
.field-line p.help {
    margin: 0.25rem 0 0;
    color: var(--cams-sidebar-muted);
    font-size: 0.75rem;
}

/* Errors */
.field-row.errors {
    background: #fef2f2;
}

.field-row.errors .field-line > div:first-child label {
    color: #b91c1c;
}

/* Inputs & selects — Unfold adds its own Tailwind classes; we set a
   sensible max-width so the form doesn't stretch ridiculously wide. */
.field-line input[type="text"],
.field-line input[type="email"],
.field-line input[type="url"],
.field-line input[type="number"],
.field-line input[type="password"],
.field-line input[type="date"],
.field-line input[type="datetime-local"],
.field-line input[type="time"],
.field-line input[type="tel"],
.field-line input[type="file"],
.field-line textarea,
.field-line select,
.field-line .vTextField,
.field-line .vURLField,
.field-line .vIntegerField,
.field-line .vBigIntegerField,
.field-line .vFloatField,
.field-line .vDecimalField,
.field-line .vDateField,
.field-line .vTimeField {
    width: 100%;
    max-width: 28rem;
    padding: 0.45rem 0.65rem;
    font-size: 0.875rem;
    line-height: 1.4;
    color: #111827;
    background: #ffffff;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
    box-sizing: border-box;
}

.field-line input:focus,
.field-line textarea:focus,
.field-line select:focus {
    outline: none;
    border-color: #6366f1;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.18);
}

.field-line textarea {
    min-height: 5rem;
    resize: vertical;
}

/* Checkbox rows */
.field-line input[type="checkbox"] {
    width: 1rem;
    height: 1rem;
    margin-top: 0.4rem;
    accent-color: #4f46e5;
}

/* Related-widget icons (the pencil/plus/eye buttons next to FK selects).
   The wrapper holds <select> + 3 <a> icons and must stay on a single row
   beside the input, otherwise the icons fall onto their own line below. */
.related-widget-wrapper {
    display: inline-flex !important;
    align-items: center;
    gap: 0.35rem;
    max-width: 28rem;
    width: auto;
    flex-wrap: nowrap;
    vertical-align: middle;
}

.related-widget-wrapper select,
.related-widget-wrapper > select {
    flex: 1 1 auto;
    min-width: 0;
    width: auto;
    max-width: 100%;
}

.related-widget-wrapper-link {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    width: 1.65rem;
    height: 1.65rem;
    flex: 0 0 auto;
    text-decoration: none;
    line-height: 1;
    vertical-align: middle;
    border-radius: 4px;
    transition: background-color 0.15s ease, opacity 0.15s ease;
}

.related-widget-wrapper-link img {
    width: 16px;
    height: 16px;
    margin: 0;
    opacity: 0.7;
    transition: opacity 0.15s ease;
}
.related-widget-wrapper-link:hover {
    background: var(--cams-sidebar-hover);
}
.related-widget-wrapper-link:hover img { opacity: 1; }

/* Fieldset module card */
fieldset.module {
    border: 1px solid var(--cams-sidebar-border);
    border-radius: 8px;
    padding: 1.25rem 1.5rem;
    margin: 1.25rem 0;
    background: #ffffff;
}

fieldset.module h2,
.aligned > h2 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--cams-sidebar-text);
    margin: -1.85rem 0 1rem;
    padding: 0 0.5rem;
    background: #ffffff;
    display: inline-block;
}

/* Submit row — Unfold renders this as #submit-row > button group */
#submit-row,
.submit-row {
    display: flex !important;
    justify-content: flex-end !important;
    gap: 0.5rem;
    margin: 1.5rem 0 0;
    padding: 1rem 0 0;
    border-top: 1px solid var(--cams-sidebar-border);
}

#submit-row input[type="submit"],
#submit-row a.button,
#submit-row button,
.submit-row input[type="submit"],
.submit-row a.button,
.submit-row button {
    padding: 0.5rem 1.1rem !important;
    font-size: 0.875rem !important;
    font-weight: 600 !important;
    border-radius: 6px !important;
    border: 1px solid transparent !important;
    cursor: pointer !important;
    text-decoration: none !important;
    transition: background-color 0.15s ease;
}

#submit-row input[name="_save"],
#submit-row input[value="Save"],
.submit-row input[name="_save"],
.submit-row input[value="Save"] {
    background: #4f46e5 !important;
    color: #ffffff !important;
    border-color: #4f46e5 !important;
}
#submit-row input[name="_save"]:hover,
#submit-row input[value="Save"]:hover,
.submit-row input[name="_save"]:hover { background: #4338ca !important; }

#submit-row input[name="_continue"],
#submit-row input[name="_addanother"],
.submit-row input[name="_continue"],
.submit-row input[name="_addanother"] {
    background: #ffffff !important;
    color: #374151 !important;
    border-color: #d1d5db !important;
}
#submit-row input[name="_continue"]:hover,
#submit-row input[name="_addanother"]:hover,
.submit-row input[name="_continue"]:hover,
.submit-row input[name="_addanother"]:hover { background: #f9fafb !important; }

/* ===================================================================
 * Dark mode — gated on data-theme on <html>, set by the theme toggle
 * in templates/admin/base_site.html and templates/dashboard/index.html.
 * =================================================================== */
:root[data-theme="dark"] {
    --cams-sidebar-bg: #0f172a;
    --cams-sidebar-border: #1e293b;
    --cams-sidebar-text: #e2e8f0;
    --cams-sidebar-muted: #94a3b8;
    --cams-sidebar-hover: #1e293b;
    --cams-sidebar-active: #312e81;
}
:root[data-theme="dark"] fieldset.module,
:root[data-theme="dark"] .aligned { background: #1e293b; }
:root[data-theme="dark"] fieldset.module h2,
:root[data-theme="dark"] .aligned > h2 { background: #1e293b; color: #e2e8f0; }
:root[data-theme="dark"] .field-line input,
:root[data-theme="dark"] .field-line textarea,
:root[data-theme="dark"] .field-line select {
    background: #0f172a; color: #e2e8f0; border-color: #334155;
}
:root[data-theme="dark"] #submit-row input[name="_continue"],
:root[data-theme="dark"] #submit-row input[name="_addanother"] {
    background: #1e293b !important; color: #e2e8f0 !important; border-color: #334155 !important;
}
:root[data-theme="dark"] .field-row.form-row { border-color: #1e293b; }