:root {
    color-scheme: dark;
    --bg: #0f1115;
    --surface: #171a21;
    --border: #262b36;
    --text: #e6e8eb;
    --muted: #9aa3b2;
    --primary: #23d887;
    --link: #3898ec;
    --on-primary: #04130c;
    --danger: #f7546a;
}

:root[data-theme="light"] {
    color-scheme: light;
    --bg: #f4f6f9;
    --surface: #ffffff;
    --border: #e2e6ec;
    --text: #1a1d23;
    --muted: #5c6573;
    --primary: #16a368;
    --link: #2563eb;
    --on-primary: #ffffff;
    --danger: #d83452;
}

* { box-sizing: border-box; }

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

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

/* ── App shell: fixed left sidebar + scrollable content column ───────── */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    position: sticky;
    top: 0;
    align-self: flex-start;
    flex: 0 0 240px;
    height: 100vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    padding: 1.25rem 0.85rem;
    background: var(--surface);
    border-right: 1px solid var(--border);
}

.sidebar .brand {
    font-weight: 700;
    color: var(--primary);
    font-size: 1.1rem;
    padding: 0.25rem 0.65rem;
}
.sidebar .brand:hover { text-decoration: none; }

.sidebar-nav { display: flex; flex-direction: column; gap: 1.25rem; }
.nav-group { display: flex; flex-direction: column; gap: 0.15rem; }
.nav-group-label {
    color: var(--muted);
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    padding: 0 0.65rem 0.25rem;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.5rem 0.65rem;
    border-radius: 8px;
    color: var(--text);
    font-size: 0.92rem;
    font-weight: 500;
    border-left: 3px solid transparent;
}
.nav-link:hover { background: var(--bg); text-decoration: none; }
.nav-link.is-active {
    background: var(--bg);
    color: var(--primary);
    border-left-color: var(--primary);
}
.nav-icon { flex: 0 0 auto; opacity: 0.85; }
.nav-link.is-active .nav-icon { opacity: 1; }

/* ── Content column: sticky topbar + scrolling main ──────────────────── */
.app-content { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.topbar-title { margin: 0; font-size: 1.15rem; font-weight: 600; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 0.85rem; }

/* Theme toggle — sun shown in dark mode, moon in light mode. */
.theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.4rem;
    border-radius: 8px;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    cursor: pointer;
}
.theme-toggle:hover { background: var(--surface); }
.theme-toggle .nav-icon { opacity: 0.85; }
.theme-toggle .icon-light { display: none; }
:root[data-theme="light"] .theme-toggle .icon-dark { display: none; }
:root[data-theme="light"] .theme-toggle .icon-light { display: inline-flex; }

/* User menu — native <details> dropdown, no JS. */
.user-menu { position: relative; }
.user-menu > summary {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.3rem 0.5rem 0.3rem 0.35rem;
    border-radius: 8px;
    cursor: pointer;
    list-style: none;
    color: var(--text);
    font-size: 0.9rem;
}
.user-menu > summary::-webkit-details-marker { display: none; }
.user-menu > summary:hover { background: var(--bg); }
.user-menu[open] > summary { background: var(--bg); }
.avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--primary);
    color: var(--on-primary);
    font-weight: 700;
    font-size: 0.8rem;
}
.user-name { max-width: 14rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-menu[open] > summary .nav-icon { transform: rotate(180deg); }

.user-menu-pop {
    position: absolute;
    right: 0;
    top: calc(100% + 0.4rem);
    min-width: 200px;
    display: flex;
    flex-direction: column;
    padding: 0.35rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}
.user-menu-head {
    padding: 0.4rem 0.6rem;
    color: var(--muted);
    font-size: 0.8rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 0.25rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.user-menu-item {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.5rem 0.6rem;
    border-radius: 7px;
    color: var(--text);
    font-size: 0.9rem;
}
.user-menu-item:hover { background: var(--bg); text-decoration: none; }

.app-main { flex: 1 1 auto; max-width: 1100px; width: 100%; margin: 0 auto; padding: 2rem 1.5rem; }

/* ── Mobile: sidebar collapses to a top strip ────────────────────────── */
@media (max-width: 768px) {
    .app-shell { flex-direction: column; }
    .sidebar {
        position: static;
        flex-basis: auto;
        height: auto;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        gap: 0.5rem 1rem;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }
    .sidebar-nav { flex-direction: row; flex-wrap: wrap; gap: 0.25rem 0.75rem; }
    .nav-group { flex-direction: row; flex-wrap: wrap; align-items: center; gap: 0.25rem; }
    .nav-group-label { display: none; }
    .nav-link { border-left: none; padding: 0.4rem 0.6rem; }
    .nav-link.is-active { border-left: none; }
    .user-name { display: none; }
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1.25rem 1.5rem;
}

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.6rem 0.5rem; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.03em; }

.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    background: var(--primary);
    color: var(--on-primary);
    font-weight: 600;
    border: none;
    border-radius: 8px;
    padding: 0.5rem 0.9rem;
    cursor: pointer;
}
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }

/* Modal dialog — backdrop fades in, dialog pops up (Bootstrap/Metronic style). */
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: rgba(0, 0, 0, 0.55);
    animation: modal-backdrop-in 0.18s ease-out;
}
.modal-dialog {
    width: 100%;
    max-width: 440px;
    animation: modal-dialog-in 0.22s cubic-bezier(0.2, 0.8, 0.25, 1);
}
@keyframes modal-backdrop-in {
    from { opacity: 0; }
    to { opacity: 1; }
}
@keyframes modal-dialog-in {
    from { opacity: 0; transform: translateY(-14px) scale(0.96); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}
@media (prefers-reduced-motion: reduce) {
    .modal-backdrop, .modal-dialog { animation: none; }
}

input, textarea, .field {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.55rem 0.7rem;
    color: var(--text);
}

/* All multi-line text areas share one height; only vertical resize allowed. */
textarea, textarea.field {
    min-height: 11rem;
    font: inherit;
    line-height: 1.5;
    resize: vertical;
}

label { display: block; margin-bottom: 0.35rem; color: var(--muted); font-size: 0.9rem; }
.form-row { margin-bottom: 1rem; }
.search { max-width: 320px; margin-bottom: 1rem; }

/* Two-column responsive field grid. Short fields sit side by side; fields that
   need the full width opt in with .span-2. Collapses to one column on narrow
   viewports. */
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0 1.5rem;
}
.form-grid .form-row { margin-bottom: 1.1rem; }
.form-grid .span-2 { grid-column: 1 / -1; }

.form-section {
    grid-column: 1 / -1;
    margin: 0.5rem 0 0.25rem;
    padding-top: 0.85rem;
    border-top: 1px solid var(--border);
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}
.form-section:first-child { margin-top: 0; padding-top: 0; border-top: none; }

@media (max-width: 560px) {
    .form-grid { grid-template-columns: 1fr; }
}

/* ---- Data grid: total count, per-page selector, pager ---- */
.datagrid-toolbar {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-bottom: 0.6rem;
    color: var(--muted);
    font-size: 0.85rem;
}

.datagrid-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-top: 1rem;
    color: var(--muted);
    font-size: 0.85rem;
}
.datagrid-footer .datagrid-info { display: flex; align-items: center; gap: 0.6rem; }

/* Narrow, auto-width version of the .field select — sits inline in the footer. */
.datagrid-perpage {
    width: auto;
    padding: 0.3rem 1.6rem 0.3rem 0.6rem;
    font-size: 0.85rem;
    cursor: pointer;
}

.datagrid-pager { display: inline-flex; gap: 0.3rem; }
.btn-page {
    min-width: 2rem;
    height: 2rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 0.5rem;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.9rem;
    line-height: 1;
    cursor: pointer;
    transition: border-color 0.12s ease, background 0.12s ease, color 0.12s ease;
}
.btn-page:hover:not(:disabled):not(.is-active) {
    border-color: var(--primary);
    color: var(--primary);
}
.btn-page.is-active {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--on-primary);
    font-weight: 600;
    cursor: default;
}
.btn-page:disabled { opacity: 0.4; cursor: not-allowed; }
.datagrid-ellipsis { padding: 0 0.15rem; color: var(--muted); align-self: center; }

/* ---- Tom Select (ux-autocomplete) theming ----
   The vendor tom-select.default.css is a light theme; on this dark UI it clashes.
   These overrides drive Tom Select off the same CSS variables as .field so the
   widget matches the native inputs and follows light/dark automatically.
   The select keeps its `field` class, which Tom Select copies onto .ts-wrapper —
   we neutralise that outer box and style the inner .ts-control instead. */
.ts-wrapper.field {
    padding: 0;
    border: 0;
    background: none;
    border-radius: 0;
    width: 100%;
}
/* Doubled .ts-wrapper raises specificity above vendor's .ts-wrapper.single
   .ts-control; background-image:none kills the vendor white gradient. */
.ts-wrapper.ts-wrapper.field .ts-control {
    background: var(--bg);
    background-image: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.55rem 2.2rem 0.55rem 0.7rem;
    color: var(--text);
    box-shadow: none;
    min-height: 0;
    font-size: inherit;
    line-height: 1.4;
}
.ts-wrapper.ts-wrapper.field .ts-control input,
.ts-wrapper.ts-wrapper.field .ts-control .item { color: var(--text); }
.ts-wrapper.ts-wrapper.field .ts-control input::placeholder { color: var(--muted); }
/* Open / focused: primary ring like a focused field, keep all corners rounded. */
.ts-wrapper.ts-wrapper.field.focus .ts-control,
.ts-wrapper.ts-wrapper.field.dropdown-active .ts-control {
    border-color: var(--primary);
    border-radius: 8px;
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 25%, transparent);
}
/* Caret + clear button. */
.ts-wrapper .ts-control:after { border-color: var(--muted) transparent transparent; }
.ts-wrapper .clear-button { color: var(--muted); }
.ts-wrapper .clear-button:hover { color: var(--text); }
/* Dropdown panel. Doubled class (.ts-dropdown.ts-dropdown) raises specificity
   above vendor tom-select.default.css, which loads after app.css. The panel is
   appended to <body>, not inside .ts-wrapper, so descendant selectors can't reach it. */
.ts-dropdown.ts-dropdown {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-top: 0.3rem;
    color: var(--text);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}
.ts-dropdown.ts-dropdown .option { color: var(--text); padding: 0.45rem 0.7rem; background: none; }
.ts-dropdown.ts-dropdown .option.active,
.ts-dropdown.ts-dropdown .option:hover {
    background: color-mix(in srgb, var(--primary) 22%, transparent);
    color: var(--text);
}
.ts-dropdown.ts-dropdown .option[aria-selected="true"] { color: var(--primary); }
.ts-dropdown.ts-dropdown .no-results,
.ts-dropdown.ts-dropdown .optgroup-header { color: var(--muted); background: none; }

/* Flash toasts — top-right, auto-dismissing notifications. */
.toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-width: min(360px, calc(100vw - 2rem));
}

.toast {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-left: 4px solid var(--primary);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
    animation: toast-in 0.2s ease-out;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.is-hiding {
    opacity: 0;
    transform: translateX(0.5rem);
}

.toast--success { border-left-color: var(--primary); }
.toast--error { border-left-color: var(--danger); }

.toast__message { flex: 1; line-height: 1.4; }

.toast__close {
    flex-shrink: 0;
    padding: 0;
    background: none;
    border: 0;
    color: var(--muted);
    font-size: 1.25rem;
    line-height: 1;
    cursor: pointer;
}
.toast__close:hover { color: var(--text); }

@keyframes toast-in {
    from { opacity: 0; transform: translateX(1rem); }
    to { opacity: 1; transform: translateX(0); }
}
