/* =========================================================
   Accessibility Utilities
   ========================================================= */

/* Screen-reader only — visually hidden but readable by assistive tech */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Skip to main content link — visible only on keyboard focus */
#skip-to-content {
    position: absolute;
    top: -100%;
    left: 16px;
    background: var(--uvic-color);
    color: var(--text-main);
    padding: 8px 16px;
    border-radius: 0 0 6px 6px;
    font-weight: 600;
    font-size: 14px;
    z-index: 9999;
    text-decoration: none;
    transition: top 0.2s;
}

#skip-to-content:focus {
    top: 0;
}

/* Consistent focus ring using UVic yellow — applies to all keyboard-focused elements */
:focus-visible {
    outline: 2px solid var(--uvic-color);
    outline-offset: 3px;
    border-radius: 4px;
}

/* Remove default focus outline for mouse users (browsers that support :focus-visible) */
:focus:not(:focus-visible) {
    outline: none;
}

:root {
    /* UVic Light Brand Theme */
    --bg-color: #f2f4f8;
    /* Light grey/blue background */
    --land-color: #e5e9f0;
    /* Slightly darker land */
    --border-color: #d8dee9;
    /* Land borders */

    /* UVic Brand Colors */
    --uvic-color: #FDB813;
    /* UVic Yellow Accent */
    --collab-color: #0073BC;
    /* UVic Primary Blue for arcs/nodes */
    --text-main: #002754;
    /* UVic Dark Blue for main text */
    --text-muted: #57B7E7;
    /* UVic Light Blue/Grey for muted */
    --modal-bg: #ffffff;
    /* White modal */
    --btn-primary: #0073BC;
    /* UVic Blue button */
    --btn-hover: #002754;
    /* UVic Dark Blue hover */
}

body,
html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background-color: var(--bg-color);
    font-family: 'Inter', sans-serif;
    color: var(--text-main);
    overflow: hidden;
}

/* =========================================================
   Title Band — full-width top bar
   ========================================================= */

#title-band {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 56px;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 28px;
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(0, 115, 188, 0.18);
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
    pointer-events: auto;
}

#title-band-text {
    display: flex;
    align-items: baseline;
    gap: 12px;
}

#title-uvic {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-main);
    letter-spacing: 0.3px;
    white-space: nowrap;
}

#title-sub {
    font-size: 13px;
    font-weight: 300;
    color: var(--text-muted);
    white-space: nowrap;
}

/* UVic yellow accent bar on left edge of title band */
#title-band::before {
    content: '';
    position: absolute;
    left: 0;
    top: 8px;
    bottom: 8px;
    width: 4px;
    background: var(--uvic-color);
    border-radius: 0 3px 3px 0;
}

.hidden {
    display: none !important;
}

#ui-container {
    position: fixed;
    bottom: 30px;
    left: 40px;
    top: auto;
    z-index: 10;
    pointer-events: auto;
    background: rgba(255, 255, 255, 0.88);
    padding: 16px 20px;
    border-radius: 12px;
    backdrop-filter: blur(8px);
    border: 1px solid rgba(0, 115, 188, 0.2);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
    max-width: 280px;
    width: 280px;
    box-sizing: border-box;
}

#view-toggles {
    pointer-events: auto;
}

/* h1/h2 now live in #title-band; these rules handle modal headings */
h1 {
    margin: 0;
    font-size: 24px;
    font-weight: 600;
    letter-spacing: 0.5px;
}

h2 {
    margin: 0 0 20px 0;
    font-size: 14px;
    font-weight: 300;
    color: var(--text-muted);
}

#view-toggles {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
    pointer-events: auto;
}

.legend {
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 13px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
}

.dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    display: inline-block;
}

.dot.uvic {
    background-color: var(--uvic-color);
    box-shadow: 0 0 8px var(--uvic-color);
}

.dot.collab {
    background-color: var(--collab-color);
    box-shadow: 0 0 8px var(--collab-color);
}

#back-btn {
    margin-top: 15px;
    background: transparent;
    color: var(--text-main);
    border: 1px solid var(--collab-color);
    padding: 8px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-family: 'Inter', sans-serif;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s ease;
    display: inline-block;
    pointer-events: auto;
}

#back-btn:hover {
    background: var(--collab-color);
    color: #fff;
}

#back-btn.hidden {
    display: none;
}

#tooltip {
    position: absolute;
    margin-top: 30px;
    /* Offset to prevent blocking cursor */
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid var(--border-color);
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 13px;
    pointer-events: none;
    transition: opacity 0.2s ease;
    box-shadow: 0 5px 15px rgba(0, 39, 84, 0.15);
    backdrop-filter: blur(4px);
    transform: translateY(20px);
    width: max-content;
    max-width: 450px;
    /* Much wider tooltip */
}

#tooltip.hidden {
    opacity: 0;
}

#tooltip strong {
    color: var(--collab-color);
    font-size: 14px;
}

.tooltip-action-btn {
    display: inline-block;
    margin-top: 10px;
    background: var(--collab-color);
    color: var(--bg-color);
    padding: 6px 10px;
    border: none;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.2s ease;
}

.tooltip-action-btn:hover {
    background: #fff;
    box-shadow: 0 0 10px var(--collab-color);
}

.tooltip-action-btn.secondary {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--text-muted);
}

.tooltip-action-btn.secondary:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    box-shadow: none;
}

.modal-btn {
    display: inline-block;
    background: var(--btn-primary);
    color: #fff;
    padding: 10px 16px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.2s ease;
    text-align: center;
}

.modal-btn:hover {
    background: var(--btn-hover);
    box-shadow: 0 4px 10px rgba(0, 115, 188, 0.3);
}

.modal-btn.secondary {
    background: transparent;
    color: var(--btn-primary);
    border: 1px solid var(--btn-primary);
}

.modal-btn.secondary:hover {
    background: var(--btn-primary);
    color: #fff;
    box-shadow: none;
}

#map-container {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* SVG Styles */
.land {
    fill: var(--land-color);
    stroke: var(--border-color);
    stroke-width: 0.5px;
}

.graticule {
    fill: none;
    stroke: rgba(0, 39, 84, 0.05);
    /* very faint dark blue */
    stroke-width: 0.5px;
}

.arc {
    fill: none;
    stroke: var(--collab-color);
    stroke-linecap: round;
}

.node {
    fill: var(--collab-color);
    opacity: 0.8;
    cursor: pointer;
    transition: r 0.2s ease, fill 0.2s ease;
}

.node:hover {
    fill: var(--btn-hover);
    opacity: 1;
}

.uvic-node {
    fill: var(--uvic-color);
    filter: drop-shadow(0px 0px 6px rgba(253, 184, 19, 0.6));
}

/* ── Victoria, BC map label ── */
.uvic-label-halo {
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    font-weight: 600;
    fill: none;
    stroke: rgba(255, 255, 255, 0.95);
    stroke-width: 3px;
    stroke-linejoin: round;
    pointer-events: none;
}

.uvic-label {
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    font-weight: 600;
    fill: var(--text-main);
    pointer-events: none;
}

/* Modal Styling */
#modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 39, 84, 0.6);
    /* Translucent dark blue */
    backdrop-filter: blur(5px);
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 1;
    transition: opacity 0.3s ease;
}

#modal-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

#modal-content {
    background: var(--modal-bg);
    border: 1px solid rgba(0, 115, 188, 0.2);
    border-radius: 12px;
    width: 90%;
    max-width: 600px;
    max-height: 85vh;
    /* overall modal height constraint */
    display: flex;
    flex-direction: column;
    box-shadow: 0 15px 40px rgba(0, 39, 84, 0.2);
    padding: 24px;
    position: relative;
    color: var(--text-main);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

#modal-close {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 24px;
    cursor: pointer;
    line-height: 1;
    padding: 0;
    transition: color 0.2s;
}

#modal-close:hover {
    color: var(--text-main);
}

#modal-list-container {
    margin-top: 15px;
    overflow-y: auto;
    /* MUST FIX SCROLLING OVERFLOW */
    max-height: 55vh;
    /* PREVENTS MODAL FROM BLOWING OUT CSS HEIGHT */
    flex-grow: 1;
    border-top: 1px solid rgba(0, 39, 84, 0.1);
    padding-top: 10px;
}

#modal-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

#modal-table th {
    text-align: left;
    color: var(--btn-primary);
    font-weight: 600;
    padding: 8px;
    border-bottom: 2px solid rgba(0, 39, 84, 0.1);
    position: sticky;
    top: 0;
    background: var(--modal-bg);
}

#modal-table td {
    padding: 10px 8px;
    border-bottom: 1px solid rgba(0, 39, 84, 0.05);
}

#modal-table tr:hover td {
    background: rgba(0, 115, 188, 0.05);
}

.row-action-btn {
    background: transparent;
    border: 1px solid var(--btn-primary);
    color: var(--btn-primary);
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 11px;
    cursor: pointer;
    text-decoration: none;
    display: inline-block;
    transition: all 0.2s;
}

.row-action-btn:hover {
    background: var(--btn-primary);
    color: #fff;
    box-shadow: 0 2px 5px rgba(0, 115, 188, 0.2);
}

/* =========================================================
   Institution Deep-Dive Panel
   ========================================================= */

#inst-panel {
    margin-top: 12px;
}

/* Loading skeleton */
.inst-panel-loading {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 8px 0;
}

.skeleton-line {
    height: 14px;
    border-radius: 6px;
    background: linear-gradient(
        90deg,
        rgba(0, 115, 188, 0.08) 25%,
        rgba(0, 115, 188, 0.16) 50%,
        rgba(0, 115, 188, 0.08) 75%
    );
    background-size: 200% 100%;
    animation: shimmer 1.4s infinite ease-in-out;
}

.skeleton-line.wide  { width: 85%; }
.skeleton-line.medium { width: 60%; }
.skeleton-line.narrow { width: 40%; }
.skeleton-line.heading { height: 20px; width: 70%; margin-bottom: 4px; }

@keyframes shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* Institution metadata header inside panel */
.inst-meta-header {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 14px;
    flex-wrap: wrap;
}

.inst-meta-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 12px;
}

.inst-meta-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: rgba(0, 115, 188, 0.08);
    color: var(--text-main);
    border: 1px solid rgba(0, 115, 188, 0.2);
    border-radius: 20px;
    padding: 3px 10px;
    font-size: 11px;
    font-weight: 500;
}

.inst-stats-row {
    display: flex;
    gap: 20px;
    margin-bottom: 14px;
    flex-wrap: wrap;
}

.inst-stat {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.inst-stat-value {
    font-size: 18px;
    font-weight: 600;
    color: var(--btn-primary);
    line-height: 1;
}

.inst-stat-label {
    font-size: 10px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Sort toggle button group */
.sort-toggle-group {
    display: flex;
    gap: 0;
    border: 1px solid var(--btn-primary);
    border-radius: 6px;
    overflow: hidden;
    margin-bottom: 10px;
    width: fit-content;
}

.sort-toggle-btn {
    background: transparent;
    color: var(--btn-primary);
    border: none;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s ease;
    font-family: 'Inter', sans-serif;
}

.sort-toggle-btn + .sort-toggle-btn {
    border-left: 1px solid var(--btn-primary);
}

.sort-toggle-btn.active {
    background: var(--btn-primary);
    color: #fff;
}

.sort-toggle-btn:hover:not(.active) {
    background: rgba(0, 115, 188, 0.08);
}

.pub-table-section {
    border-top: 1px solid rgba(0, 39, 84, 0.1);
    padding-top: 12px;
    margin-top: 4px;
}

.pub-table-section h4 {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-main);
    margin: 0 0 10px 0;
}

.pub-table-scroll {
    max-height: 280px;
    overflow-y: auto;
    /* horizontal scroll fallback for very narrow viewports */
    overflow-x: auto;
}

/* Publication table */
.pub-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}

.pub-table th {
    text-align: left;
    padding: 6px 8px;
    border-bottom: 2px solid rgba(0, 39, 84, 0.1);
    color: var(--btn-primary);
    font-weight: 600;
    position: sticky;
    top: 0;
    background: var(--modal-bg);
    white-space: nowrap;
}

.pub-table th:last-child,
.pub-table td:last-child {
    text-align: right;
}

.pub-table td {
    padding: 8px 8px;
    border-bottom: 1px solid rgba(0, 39, 84, 0.05);
    vertical-align: top;
}

.pub-table tr:hover td {
    background: rgba(0, 115, 188, 0.04);
}

.pub-title {
    font-size: 12px;
    color: var(--text-main);
    text-decoration: none;
    display: block;
    line-height: 1.4;
}

.pub-title:hover {
    color: var(--btn-primary);
    text-decoration: underline;
}

.pub-year {
    color: var(--text-muted);
    font-size: 11px;
    white-space: nowrap;
}

/* FWCI badge */
.fwci-badge {
    display: inline-block;
    padding: 2px 7px;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
}

.fwci-badge.high   { background: rgba(16, 163, 127, 0.12); color: #0d7a60; }
.fwci-badge.mid    { background: rgba(251, 191, 36, 0.18); color: #92620a; }
.fwci-badge.low    { background: rgba(0, 39, 84, 0.06);   color: var(--text-muted); }
.fwci-badge.null   { background: transparent; color: var(--text-muted); font-weight: 400; }

.pub-error {
    padding: 16px 0;
    font-size: 13px;
    color: var(--text-muted);
    text-align: center;
}

/* Map Zoom Controls */
.zoom-controls {
    position: fixed;
    bottom: 40px;
    right: 40px;
    z-index: 10;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.zoom-btn {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid rgba(0, 115, 188, 0.2);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    color: var(--btn-primary);
    font-size: 20px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    backdrop-filter: blur(4px);
    padding: 0;
}

.zoom-btn:hover {
    background: var(--btn-primary);
    color: white;
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 115, 188, 0.2);
}

/* =========================================================
   Mobile panel toggle button (hidden on desktop)
   ========================================================= */
#mobile-panel-toggle {
    display: none;
}

/* =========================================================
   Responsive — Tablet (≤ 900px)
   ========================================================= */
@media (max-width: 900px) {
    #title-uvic { font-size: 16px; }
    #title-sub  { display: none; } /* hide subtitle on tablet to save space */

    #ui-container {
        bottom: 20px;
        left: 20px;
        max-width: 260px;
        width: 260px;
    }

    .zoom-controls {
        bottom: 24px;
        right: 16px;
    }
}

/* =========================================================
   Responsive — Mobile (≤ 600px)
   ========================================================= */
@media (max-width: 600px) {

    /* Title band: tighter on small screens */
    #title-band {
        height: 48px;
        padding: 0 16px;
    }

    #title-uvic { font-size: 15px; }
    #title-sub  { display: none; }

    /* Control panel: full-width bottom sheet */
    #ui-container {
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-width: none;
        border-radius: 14px 14px 0 0;
        padding: 0 16px 16px;
        box-sizing: border-box;
    }

    /* Mobile toggle: full-width handle at top of bottom sheet */
    #mobile-panel-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 6px;
        width: 100%;
        background: none;
        border: none;
        border-bottom: 1px solid rgba(0, 115, 188, 0.12);
        color: var(--btn-primary);
        font-size: 13px;
        font-weight: 600;
        font-family: 'Inter', sans-serif;
        padding: 12px 0;
        cursor: pointer;
    }

    /* Collapsible body — hidden by default on mobile */
    #ui-panel-body {
        overflow: hidden;
        max-height: 0;
        transition: max-height 0.3s ease, opacity 0.25s ease;
        opacity: 0;
        pointer-events: none;
    }

    #ui-panel-body.open {
        max-height: 350px;
        opacity: 1;
        pointer-events: auto;
        padding-top: 12px;
    }

    /* View toggles: fill row */
    #view-toggles {
        gap: 8px !important;
        margin-bottom: 10px !important;
    }

    #view-toggles .modal-btn {
        flex: 1;
        text-align: center;
    }

    /* Zoom controls: bigger tap targets, above the panel */
    .zoom-controls {
        bottom: 70px;
        right: 12px;
        gap: 10px;
    }

    .zoom-btn {
        width: 44px;
        height: 44px;
        font-size: 22px;
    }

    /* Tooltip: disabled on touch */
    #tooltip {
        display: none !important;
    }

    /* Modal: bottom sheet on mobile */
    #modal-content {
        width: 100% !important;
        max-width: 100% !important;
        max-height: 90vh;
        border-radius: 16px 16px 0 0;
        padding: 20px 16px 16px;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        margin: 0;
        box-sizing: border-box;
    }

    /* Make the modal body scrollable so nothing is clipped */
    #modal-body-overview {
        overflow-y: auto;
        max-height: calc(90vh - 80px); /* subtract header height */
        padding-right: 2px; /* prevent scrollbar overlap */
    }

    /* Stats row: 2x2 grid on mobile instead of 4-in-a-row */
    .inst-stats-row {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 12px;
    }

    /* Stat values slightly smaller */
    .inst-stat-value { font-size: 15px; }

    /* Pub table: uncap height since modal body itself scrolls */
    .pub-table-scroll {
        max-height: none;
        overflow-x: auto;
    }

    /* Hide the # rank column to save space */
    .pub-table th:first-child,
    .pub-table td:first-child { display: none; }

    /* Chips wrap naturally — no change needed, already flex-wrap */
    .inst-meta-chips { gap: 6px; }

    #modal-overlay { align-items: flex-end; }
    #modal-list-container { max-height: 45vh; }

    /* Table view: account for title band + full width */
    .table-container-inner {
        margin: 60px 8px 8px 8px !important;
        padding: 16px !important;
        height: calc(100vh - 80px) !important;
        border-radius: 8px !important;
    }

    /* Legend: horizontal on mobile */
    .legend { flex-direction: row; gap: 16px; }

    #full-country-table,
    #country-inst-table { font-size: 13px; }

    #info-modal-overlay > div {
        width: 92% !important;
        padding: 20px 16px !important;
    }
}
