:root {
    --base-hue: 200;
    --surface-saturation: 15%;
    --brand-saturation: 75%;

    --surface-1: hsl(var(--base-hue), var(--surface-saturation), 10%);
    --surface-2: hsl(var(--base-hue), var(--surface-saturation), 12%);
    --surface-3: hsl(var(--base-hue), var(--surface-saturation), 14%);
    --surface-4: hsl(var(--base-hue), var(--surface-saturation), 17%);
    --surface-5: hsl(var(--base-hue), var(--surface-saturation), 25%);
    --surface-6: hsl(var(--base-hue), var(--surface-saturation), 40%);
    --surface-7: hsl(var(--base-hue), var(--surface-saturation), 52%);
    --surface-8: hsl(var(--base-hue), var(--surface-saturation), 63%);
    --surface-9: hsl(var(--base-hue), var(--surface-saturation), 88%);

    --brand-1: hsl(var(--base-hue), var(--brand-saturation), 33%);
    --brand-2: hsl(var(--base-hue), var(--brand-saturation), 50%);
    --brand-3: hsl(var(--base-hue), var(--brand-saturation), 67%);

    /* Semantic — approval/success. Fixed green, decoupled from --base-hue
       so the success hue stays stable regardless of brand tinting. */
    --approval: #34b25e;

    /* Semantic — approval panel (mirrors --error-* but in green).
       Used for soft-tint UI affordances like the Approve button. */
    --approval-bg:     #1a4524;
    --approval-text:   #86efac;
    --approval-border: #15803d;

    /* Semantic — error panel (dark-theme defaults; light overrides below). */
    --error-bg:            #451a1a;
    --error-text:          #fca5a5;
    --error-border:        #b91c1c;
    /* Inline error text — softer red used in flow (no panel background). */
    --error-inline:        #fca5a5;
    /* Strong inline error — theme-agnostic, used where a single value
       must read as "error" in both light and dark schemes. */
    --error-inline-strong: #f87171;

    --modal-overlay: rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: light) {
    :root {
        --surface-1: hsl(var(--base-hue), var(--surface-saturation), 98%);
        --surface-2: hsl(var(--base-hue), var(--surface-saturation), 95%);
        --surface-3: hsl(var(--base-hue), var(--surface-saturation), 92%);
        --surface-4: hsl(var(--base-hue), var(--surface-saturation), 89%);
        --surface-5: hsl(var(--base-hue), var(--surface-saturation), 78%);
        --surface-6: hsl(var(--base-hue), var(--surface-saturation), 60%);
        --surface-7: hsl(var(--base-hue), var(--surface-saturation), 48%);
        --surface-8: hsl(var(--base-hue), var(--surface-saturation), 35%);
        --surface-9: hsl(var(--base-hue), var(--surface-saturation), 10%);

        --brand-1: hsl(var(--base-hue), var(--brand-saturation), 25%);
        --brand-2: hsl(var(--base-hue), var(--brand-saturation), 35%);
        --brand-3: hsl(var(--base-hue), var(--brand-saturation), 50%);

        --approval: #2d864a;
        --approval-bg:     #dcfce7;
        --approval-text:   #166534;
        --approval-border: #4ade80;

        --error-bg:     #fee2e2;
        --error-text:   #991b1b;
        --error-border: #f87171;
        --error-inline: #991b1b;
    }
}

/* Base link styling. Element-level specificity stays below .link-btn and
   .auth-header-slot a, so those keep their explicit colors; this only
   reaches otherwise-unstyled anchors (e.g. the auth-footer switch links).
   Underline is hover-only. */
a {
    color: var(--brand-2);
    text-decoration: none;
}
a:hover {
    color: var(--brand-3);
    text-decoration: underline;
}

body {
    background-color: var(--surface-1);
    color: var(--surface-9);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    line-height: 1.6;
    margin: 0;
    padding: 20px;
    transition: background-color 0.3s, color 0.3s;
}

/* Header & Controls */
header {
    max-width: 1200px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: .5rem 1.25rem;
    margin: 0 auto 1rem;
}

h1 {
    color: var(--brand-2);
    font-size: 1.8rem;
    margin: 0;
}

.app-subtitle {
    color: var(--surface-8);
    font-size: 0.8rem;
}

.search-box {
    max-width: 400px;
    position: relative;
    flex-grow: 1;
}

.search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--surface-8);
    pointer-events: none;
}

.search-box input {
    width: stretch;
    padding-left: 2.2rem;
}

input {
    background: var(--surface-5);
    color: var(--surface-8);
    border: 1px solid var(--surface-6);
    border-radius: .5rem;
    padding: .67rem 1rem;
}

button {
    cursor: pointer;
    background: var(--brand-2);
    color: var(--surface-9);
    font-weight: bold;
    border: none;
    border-radius: .5rem;
    padding: .67rem 1rem;
}

button:hover {
    background: var(--brand-1);
}

#loginHeaderLink i,
#logoutBtn i,
#googleLoginBtn i,
#googleRegisterBtn i {
    padding-left: .5rem;
}

/* DATA TABLE */
.table-container {
    max-width: 1200px;
    margin: 0 auto;
    background: var(--surface-4);
    border-radius: 1rem;
    overflow: hidden; /* Rounds corners */
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.table-wrapper {
    overflow-x: auto; /* Horizontal scroll on mobile */
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 600px; /* Forces scroll on very small screens */
}

th, td {
    padding: 15px;
    text-align: left;
    border-bottom: 1px solid var(--surface-6);
}

th {
    background-color: var(--surface-5);
    font-weight: 600;
    color: var(--brand-2);
    position: sticky;
    top: 0;
    white-space: nowrap;
    cursor: pointer;
}

th:hover { opacity: 0.9; }

tr:hover {
    background-color: var(--surface-2);
    cursor: pointer;
}
/* STOP - DATA TABLE */

/* VIRTUAL TABLE */
.virtual-table {
    width: 100%;
}

.virtual-header,
.virtual-row {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr;
}

.virtual-header {
    font-weight: bold;
    border-bottom: 1px solid var(--surface-6);
}

.virtual-header .virtual-cell {
    cursor: pointer;
    padding: 12px 8px;
    border-radius: .75rem;
}

.virtual-header .virtual-cell:hover {
    background: var(--surface-2);
}

.virtual-row {
    height: 40px; /* Must match rowHeight option */
    border-radius: .67rem;
}

.virtual-row .virtual-cell {
    padding: 8px;
    line-height: 24px; /* 40px row - 16px padding = 24px */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.virtual-row.species-row:hover {
    background: var(--surface-2);
    cursor: pointer;
}

.virtual-row.empty-state {
    position: relative;
    justify-content: center;
}

.virtual-viewport { overflow-y: auto; }
.virtual-content { position: relative; }

.virtual-row.species-row { position: absolute; width: 100%; }

.virtual-species-cell { display: flex; align-items: center; gap: 6px; }
.virtual-capsule-img {
    height: 23px;
    aspect-ratio: 1;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    flex-shrink: 0;
}

.virtual-row.empty-state .virtual-cell { grid-column: 1 / -1; }
/* STOP - VIRTUAL TABLE */

/* POPUP */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: var(--modal-overlay);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    padding: 20px;
    opacity: 0;
    transition: opacity 0.2s ease;
    backdrop-filter: blur(2px);
}

.modal-overlay.open {
    display: flex;
    opacity: 1;
}

.modal-content {
    background: var(--surface-4);
    width: 100%;
    max-width: 600px;
    border-radius: 12px;
    position: relative;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    /* Hide scrollbar */
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE/Edge */
}

.modal-content::-webkit-scrollbar {
    display: none; /* Chrome/Safari */
}

.modal-header {
    padding: 20px;
    border-bottom: 1px solid var(--surface-6);
}

.modal-header h2 {
    margin: 0;
    color: var(--brand-2);
    font-size: 1.8rem;
}

.modal-header .subtitle {
    color: var(--surface-8);
    font-size: 1.1rem;
    margin-top: 5px;
    font-style: italic;
}

.modal-header-actions {
    position: absolute;
    top: 15px;
    right: 15px;
    display: flex;
    gap: 8px;
    align-items: center;
}

.modal-header-actions button {
    background: none;
    color: var(--surface-8);
    font-size: 2rem;
    padding: .5rem;
}

.modal-header-actions button:hover {
    color: var(--surface-9);
}

.capsule-img-detail {
    display: block;
    height: calc(23px * 5);
    width: auto;
    margin: 0 auto 8px;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

.links-label {
    display: block;
}

.modal-body {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

#species-edit-form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.section-header {
    color: var(--brand-2);
    font-weight: 600;
    font-size: 1.2rem;
    margin-bottom: 10px;
    border-bottom: 1px solid var(--surface-6);
    padding-bottom: 5px;
}

.detail-group {
    background: var(--surface-3);
    padding: 15px;
    border-radius: 8px;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    border-bottom: 1px solid var(--surface-5);
    padding-bottom: 8px;
}

.detail-row:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.label { color: var(--surface-8); font-size: 0.9rem; }
.value { font-weight: 500; text-align: right; }

/* LOADING SPINNERS */
/* Inline variant: list-view first-mount.
   Block-level so it sits in the empty space the table would occupy. */
.spinner-inline {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 4rem 0;
}

.spinner-inline > i {
    font-size: 2rem;
    color: var(--brand-2);
}

/* Overlay variant: shown during the detail-view species fetch.
   Reuses --modal-overlay and z-index 1000 so the spinner-to-modal hand-off
   has no visual jump — the spinner card is effectively a placeholder for
   the .modal-content that replaces it. */
.spinner-overlay {
    position: fixed;
    inset: 0;
    background: var(--modal-overlay);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    backdrop-filter: blur(2px);
}

.spinner-card {
    width: 80px;
    height: 80px;
    background: var(--surface-4);
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    display: flex;
    justify-content: center;
    align-items: center;
}

.spinner-card > i {
    font-size: 2rem;
    color: var(--brand-2);
}
/* STOP - LOADING SPINNERS */

/* Grid Layouts for Stats & Links */
.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 10px;
    text-align: center;
    margin-top: 10px;
}
.stat-box {
    background: var(--surface-2);
    padding: 10px;
    border-radius: 6px;
}

.stat-label {
    font-size: 0.8rem;
    color: var(--surface-8);
    display: block;
    margin-bottom: 4px;
}

.stat-value {
    font-weight: 600;
    font-size: 1rem;
}

.links-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
    margin-top: 10px;
}

.link-btn {
    display: block;
    text-align: center;
    background: var(--surface-5);
    color: var(--surface-9);
    text-decoration: none;
    padding: 10px;
    border-radius: 6px;
    font-size: 0.9rem;
    transition: background 0.2s;
}

.link-btn:hover {
    background: var(--brand-2);
    color: white;
}

.link-btn.disabled {
    opacity: 0.5;
    pointer-events: none;
}

/* ERROR MESSAGES */
.error-message {
    max-width: fit-content;
    margin: 20px auto;
    padding: 20px;
    border-radius: 4px;
    background: var(--error-bg);
    color: var(--error-text);
    border: 1px solid var(--error-border);
}

/* ACCESSIBILITY */
/* Focus indicators for keyboard navigation */
input:focus,
.close-btn:focus,
.links-grid a:focus,
.link-btn:focus {
    outline: 2px solid var(--brand-2);
    outline-offset: 2px;
}

/* Table row focus - matches hover style plus visible outline */
tr:focus,
.virtual-row.species-row:focus {
    /*background-color: var(--surface-2);*/
    outline: 2px solid var(--brand-2);
    outline-offset: -2px;
}

/* Sortable header focus */
th:focus,
.virtual-header .virtual-cell:focus {
    outline: 2px solid var(--brand-2);
    outline-offset: -2px;
}

/* Sort indicator: faint two-headed icon at rest, brand color when the
   column is the active sort. Both vars are theme-aware (light/dark). */
.sort-indicator {
    color: var(--surface-7);
    margin-left: 0.35rem;
    font-size: 0.85em;
}
.sort-indicator--active {
    color: var(--brand-2);
}

/* Respect reduced motion preferences */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* High contrast mode support */
@media (forced-colors: active) {
    tr:focus,
    .virtual-row.species-row:focus,
    .close-btn:focus,
    .link-btn:focus {
        outline: 2px solid Highlight;
    }
}
/* STOP - ACCESSIBILITY */

.table-footer {
    max-width: 1200px;
    margin: 0.5rem auto 0;
    display: flex;
    justify-content: right;
    gap: .5rem;
}

.footer-add-btn { margin: .25rem; }

.footer-inline {
    display: flex;
    align-items: center;
    gap: .25rem;
    padding: .25rem;
    flex-wrap: wrap;
    /* Definite width so .inline-error's flex-basis:100% resolves and the
       message WRAPS within the column. Without this the error's max-content
       widens this right-pinned cluster and shoves the input row left. */
    width: 100%;
    justify-content: flex-end; /* keep input+buttons hugging the right */
}

.table-footer button, .changelog-btn {
    background: none;
    color: var(--brand-2);
    font-size: 1.33rem;
    padding: .75rem;
}

.auth-header-slot {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.inline-error {
    color: var(--error-inline);
    padding: 0 .5rem;
    flex-basis: 100%; /* own line below the input row */
    text-align: right; /* sit under the right-aligned controls */
    /* Leave a gap below the message when scrollIntoView brings it
       on-screen, instead of landing flush against the viewport edge. */
    scroll-margin-bottom: 1.5rem;
    opacity: 0;
    transform: translateY(-0.5rem);
    transition: opacity .35s ease-out, transform .35s ease-out;
}

.inline-error.is-visible {
    opacity: 1;
    transform: translateY(0);
}

/* EDIT FORM TOOLTIPS */
.edit-tip {
    font-style: italic;
    color: var(--surface-8);
    font-size: 0.85rem;
    margin: 0 0 8px;
}

[data-tooltip],
.has-tooltip {
    position: relative;
    cursor: help;
}

[data-tooltip]::before,
.has-tooltip::before {
    content: attr(data-tooltip);
    position: absolute;
    bottom: calc(100% + 8px);
    left: -3px;
    background: var(--surface-5);
    color: var(--surface-9);
    border: 1px solid var(--surface-6);
    border-radius: 6px;
    padding: 4px 10px;
    font-size: 0.8rem;
    font-weight: normal;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s ease, visibility 0.15s ease;
    z-index: 1100;
}

[data-tooltip]::after,
.has-tooltip::after {
    content: '';
    position: absolute;
    /* Negate `[data-tooltip]::before` `border-width` */
    bottom: calc(100% - 1px);
    left: 12px;
    border: 5px solid transparent;
    border-top-color: var(--surface-6);
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s ease, visibility 0.15s ease;
    z-index: 1100;
}

[data-tooltip]:hover::before,
[data-tooltip]:hover::after,
.has-tooltip:hover::before,
.has-tooltip:hover::after,
[data-tooltip].tooltip-active::before,
[data-tooltip].tooltip-active::after,
.has-tooltip.tooltip-active::before,
.has-tooltip.tooltip-active::after {
    opacity: 1;
    visibility: visible;
}

@media (prefers-reduced-motion: reduce) {
    [data-tooltip]::before,
    [data-tooltip]::after,
    .has-tooltip::before,
    .has-tooltip::after {
        transition: none;
    }
}
/* STOP - EDIT FORM TOOLTIPS */

/* EDIT FORM */
.detail-row > input.value,
.detail-row > select.value {
    min-width: 0;
    flex: 1;
    background: transparent;
    color: var(--surface-9);
    font-size: 0.9rem;
    font-weight: 500;
    text-align: right;
    padding: 0 0 2px;
    border: none;
    border-bottom: 1px solid var(--surface-6);
    border-radius: 0;
}

.detail-row > input.value:focus,
.detail-row > select.value:focus {
    outline: none;
    border-bottom-color: var(--brand-2);
}

.edit-multirow {
    display: flex;
    gap: 8px;
    align-items: center;
    padding: 6px 0;
    border-bottom: 1px solid var(--surface-2);
}

.edit-multirow select,
.edit-multirow input[type="text"] {
    flex: 1;
    min-width: 0;
    background: var(--surface-4);
    border: 1px solid var(--surface-6);
    border-radius: 4px;
    color: var(--surface-9);
    font-size: 0.85rem;
    padding: 4px 6px;
}

.edit-remove-btn {
    background: none;
    border: none;
    color: var(--surface-8);
    cursor: pointer;
    font-size: 0.8rem;
    padding: 2px 4px;
    flex-shrink: 0;
}
.edit-remove-btn:hover { color: var(--error-inline-strong); }

.edit-add-btn {
    display: block;
    width: 100%;
    margin-top: 8px;
    padding: 6px;
    background: none;
    border: 1px dashed var(--surface-6);
    border-radius: 4px;
    color: var(--brand-2);
    cursor: pointer;
    font-size: 0.85rem;
    text-align: center;
}
.edit-add-btn:hover { background: var(--surface-2); }

.edit-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 16px;
}

.edit-actions button {
    padding: 8px 16px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    font-size: 0.9rem;
}

.edit-cancel {
    background: var(--surface-5);
    color: var(--surface-9);
}
.edit-cancel:hover { background: var(--surface-6); }

.edit-save {
    background: var(--brand-2);
    color: white;
}
.edit-save:hover { background: var(--brand-1); }
/* STOP - EDIT FORM */

/* RESPONSIVE */
@media (max-width: 600px) {
    th, td {
        padding: 10px;
        font-size: 0.9rem;
    }

    h1 { font-size: 1.4rem; }
    .stats-grid { grid-template-columns: 1fr; }
    .links-grid { grid-template-columns: 1fr; }
}
/* STOP - RESPONSIVE */
