/* =========================================
   TALLYWA UNIFIED STYLESHEET
   ========================================= */

/* =========================================
   OFFLINE LOCAL FONT FALLBACKS
   ========================================= */

@font-face {
    font-family: 'Outfit';
    font-style: normal;
    font-weight: 100 900;
    font-display: swap;
    src: local('Outfit'),
         url('fonts/outfit.ttf') format('truetype');
}

@font-face {
    font-family: 'Roboto';
    font-style: normal;
    font-weight: 100 900;
    font-display: swap;
    src: local('Roboto'),
         url('fonts/roboto.ttf') format('truetype');
}

/* Replace the existing @font-face and .material-symbols-rounded rules */
@font-face {
    font-family: 'Material Symbols Rounded Fallback';
    font-style: normal;
    font-weight: 100 700;
    font-display: block;
    src: url('fonts/materialsymbolsrounded.ttf') format('truetype');
}

.material-symbols-rounded {
    font-family: 'Material Symbols Rounded', 'Material Symbols Rounded Fallback' !important;
    font-weight: normal;
    font-style: normal;
    font-size: 24px;
    line-height: 1;
    letter-spacing: normal;
    text-transform: none;
    display: inline-block;
    white-space: nowrap;
    word-wrap: normal;
    direction: ltr;
    -webkit-font-feature-settings: 'liga';
    font-feature-settings: 'liga';
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
    font-variation-settings: 'FILL' 1, 'wght' 400, 'GRAD' 0, 'opsz' 24;
}

:root {
    /* M3 Expressive Surface Tones - Timber Green (Light) */
    --md-sys-color-background: #EAF0EC; 
    --md-sys-color-surface: #EAF0EC;
    --md-sys-color-surface-container-lowest: #E2EAE7;
    --md-sys-color-surface-container-low: #DCE5E0;
    --md-sys-color-surface-container: #D0DDD6;
    --md-sys-color-surface-container-high: #C4D3CB;
    --md-sys-color-surface-container-highest: #B6C8BF;
    --md-sys-color-on-surface: #1B2921;
    --md-sys-color-on-surface-variant: #3A4C41;
    --md-sys-color-outline: #687B70;
    --md-sys-color-outline-variant: #B0C6BB;

    /* Frosted Translucent Surfaces (Light) */
    --surface-translucent: rgba(234, 240, 236, 0.62);
    --surface-translucent-card: rgba(226, 234, 231, 0.70);
    --glass-blur: blur(16px) saturate(180%);
    --glass-border: 1px solid rgba(255, 255, 255, 0.65);
    --glass-sheen: inset 0 1px 1px rgba(255, 255, 255, 0.5);

    /* Expressive Election Colors */
    --color-primary: #1A73E8;      
    --color-secondary: #EA4335;    
    --color-tertiary: #1E8E3E;     
    --color-uncalled: #D5D8DC;     
    --color-warning: #F59E0B;      

    /* M3 Shape System */
    --radius-sm: 12px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --radius-full: 9999px;
    --radius-card: 28px;       
    --radius-button: 100px;    
    --radius-input: 16px;      
    
    --m3-ease-standard: cubic-bezier(0.2, 0.0, 0, 1.0);
    --m3-ease-emphasized: cubic-bezier(0.2, 0.0, 0, 1.0);

    /* Elevation */
    --shadow-level-1: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-level-2: 0 4px 14px rgba(0,0,0,0.06), 0 1px 4px rgba(0,0,0,0.03);
    --shadow-level-3: 0 8px 24px rgba(0,0,0,0.10), 0 2px 8px rgba(0,0,0,0.05);
    --elevation-1: 0px 1px 2px 0px rgba(0,0,0,0.15), 0px 1px 3px 1px rgba(0,0,0,0.05);
    --elevation-2: 0px 1px 2px 0px rgba(0,0,0,0.15), 0px 2px 6px 2px rgba(0,0,0,0.05);

    --font-family: 'Outfit', 'Roboto', sans-serif;
}

body.dark-mode {
    /* Timber Green (Dark) */
    --md-sys-color-background: #0E1511;
    --md-sys-color-surface: #0E1511;
    --md-sys-color-surface-container-lowest: #050806;
    --md-sys-color-surface-container-low: #151F19;
    --md-sys-color-surface-container: #1B2821;
    --md-sys-color-surface-container-high: #24352B;
    --md-sys-color-surface-container-highest: #2E4237;
    --md-sys-color-on-surface: #D8E6DE;
    --md-sys-color-on-surface-variant: #B0C4B8;
    --md-sys-color-outline: #8CA395;
    --md-sys-color-outline-variant: #3F5247;

    --color-primary: #3B82F6;      
    --color-secondary: #EF4444;    
    --color-warning: #FBBF24; 
    --color-uncalled: #1F2B24;

    /* Frosted Translucent Surfaces (Dark) */
    --surface-translucent: rgba(14, 21, 17, 0.58);
    --surface-translucent-card: rgba(21, 31, 25, 0.65);
    --glass-blur: blur(16px) saturate(180%);
    --glass-border: 1px solid rgba(255, 255, 255, 0.12);
    --glass-sheen: inset 0 1px 1px rgba(255, 255, 255, 0.15);
    
    --shadow-level-1: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
    --shadow-level-2: 0 4px 14px rgba(0,0,0,0.4), 0 1px 4px rgba(0,0,0,0.2);
    --shadow-level-3: 0 8px 24px rgba(0,0,0,0.5), 0 2px 8px rgba(0,0,0,0.3);
}

/* --- Dynamic Palette Classes --- */
body.theme-grey {
    --md-sys-color-background: #F0F2F5; --md-sys-color-surface: #F0F2F5;
    --md-sys-color-surface-container-lowest: #E5E9EF; --md-sys-color-surface-container-low: #DFE3E9;
    --md-sys-color-surface-container: #D6DCE3; --md-sys-color-surface-container-high: #CBD2D9;
    --md-sys-color-surface-container-highest: #BFC6CE; --md-sys-color-on-surface: #1D2125;
    --md-sys-color-on-surface-variant: #434950; --md-sys-color-outline: #7D858D;
    --md-sys-color-outline-variant: #BFC6CE;
    --surface-translucent: rgba(240, 242, 245, 0.62);
    --surface-translucent-card: rgba(229, 233, 239, 0.70);
    --glass-border: 1px solid rgba(255, 255, 255, 0.65);
}
body.theme-grey.dark-mode {
    --md-sys-color-background: #121212; --md-sys-color-surface: #121212;
    --md-sys-color-surface-container-lowest: #000000; --md-sys-color-surface-container-low: #1E1E1E;
    --md-sys-color-surface-container: #252525; --md-sys-color-surface-container-high: #2E2E2E;
    --md-sys-color-surface-container-highest: #3B3B3B; --md-sys-color-on-surface: #E9ECEF;
    --md-sys-color-on-surface-variant: #ADB5BD; --md-sys-color-outline: #6C757D;
    --md-sys-color-outline-variant: #3B3B3B;
    --color-uncalled: #22222A;
    --surface-translucent: rgba(18, 18, 18, 0.58);
    --surface-translucent-card: rgba(30, 30, 30, 0.65);
    --glass-border: 1px solid rgba(255, 255, 255, 0.12);
}

body.theme-blue {
    --md-sys-color-background: #EAEEF4; --md-sys-color-surface: #EAEEF4;
    --md-sys-color-surface-container-lowest: #DCE3EE; --md-sys-color-surface-container-low: #D2DBE8;
    --md-sys-color-surface-container: #C5D1E1; --md-sys-color-surface-container-high: #B5C4D8;
    --md-sys-color-surface-container-highest: #A5B6CD; --md-sys-color-on-surface: #181F2C;
    --md-sys-color-on-surface-variant: #3A4559; --md-sys-color-outline: #66748A;
    --md-sys-color-outline-variant: #A5B6CD;
    --surface-translucent: rgba(234, 238, 244, 0.62);
    --surface-translucent-card: rgba(220, 227, 238, 0.70);
    --glass-border: 1px solid rgba(255, 255, 255, 0.65);
}
body.theme-blue.dark-mode {
    --md-sys-color-background: #0D1117; --md-sys-color-surface: #0D1117;
    --md-sys-color-surface-container-lowest: #05070A; --md-sys-color-surface-container-low: #141A24;
    --md-sys-color-surface-container: #1A222F; --md-sys-color-surface-container-high: #222D3D;
    --md-sys-color-surface-container-highest: #2C3A4F; --md-sys-color-on-surface: #D8E2F0;
    --md-sys-color-on-surface-variant: #9EAFC9; --md-sys-color-outline: #6B7C99;
    --md-sys-color-outline-variant: #2C3A4F;
    --color-uncalled: #18202C;
    --surface-translucent: rgba(13, 17, 23, 0.58);
    --surface-translucent-card: rgba(20, 26, 36, 0.65);
    --glass-border: 1px solid rgba(255, 255, 255, 0.12);
}

body.theme-red {
    --md-sys-color-background: #F4EBEB; --md-sys-color-surface: #F4EBEB;
    --md-sys-color-surface-container-lowest: #EADCDD; --md-sys-color-surface-container-low: #E2D1D2;
    --md-sys-color-surface-container: #D8C3C4; --md-sys-color-surface-container-high: #CCB4B6;
    --md-sys-color-surface-container-highest: #BFA5A7; --md-sys-color-on-surface: #2E1B1B;
    --md-sys-color-on-surface-variant: #593C3D; --md-sys-color-outline: #886566;
    --md-sys-color-outline-variant: #BFA5A7;
    --surface-translucent: rgba(244, 235, 235, 0.62);
    --surface-translucent-card: rgba(234, 220, 221, 0.70);
    --glass-border: 1px solid rgba(255, 255, 255, 0.65);
}
body.theme-red.dark-mode {
    --md-sys-color-background: #170E0E; --md-sys-color-surface: #170E0E;
    --md-sys-color-surface-container-lowest: #0A0505; --md-sys-color-surface-container-low: #211515;
    --md-sys-color-surface-container: #2B1C1C; --md-sys-color-surface-container-high: #382525;
    --md-sys-color-surface-container-highest: #4A3333; --md-sys-color-on-surface: #F0DCDC;
    --md-sys-color-on-surface-variant: #C9A4A4; --md-sys-color-outline: #997272;
    --md-sys-color-outline-variant: #4A3333;
    --color-uncalled: #281919;
    --surface-translucent: rgba(23, 14, 14, 0.58);
    --surface-translucent-card: rgba(33, 21, 21, 0.65);
    --glass-border: 1px solid rgba(255, 255, 255, 0.12);
}

* { 
    box-sizing: border-box; 
    transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease; 
}

body, header, button, .tally-wrapper, .settings-menu, .sidebar, .map-area, .score, .raw-score, .glass-panel {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

input, textarea {
    -webkit-user-select: text !important;
    -moz-user-select: text !important;
    -ms-user-select: text !important;
    user-select: text !important;
}

body {
    --bg-app: var(--md-sys-color-background);
    --bg-panel: var(--md-sys-color-surface-container-lowest);
    --bg-sidebar: var(--md-sys-color-surface-container-low);
    --bg-card: var(--md-sys-color-surface);
    --text-main: var(--md-sys-color-on-surface);
    --text-muted: var(--md-sys-color-on-surface-variant);

    margin: 0;
    font-family: var(--font-family);
    background-color: var(--bg-app);
    color: var(--text-main);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

h1 { margin: 0; font-size: 1.25rem; font-weight: 600; letter-spacing: -0.2px; }
h3 { font-size: 0.95rem; font-weight: 700; color: var(--text-main); margin-top: 0; margin-bottom: 16px; border-bottom: 1px solid var(--md-sys-color-surface-container-high); padding-bottom: 12px; }
p { margin: 0; }

/* --- Header & Settings --- */

/* =========================================
   HEADER & FROSTED GLASS SURFACES
   ========================================= */

header {
    height: 60px; 
    padding: 0 20px; 
    margin: 12px 16px 0 16px; 
    border-radius: var(--radius-card); 
    border: var(--glass-border);
    background: var(--surface-translucent);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    box-shadow: var(--shadow-level-2), var(--glass-sheen);
    display: flex; 
    justify-content: space-between;
    align-items: center; 
    flex-shrink: 0; 
    position: relative;
    z-index: 30;
}

.brand-container { display: flex; align-items: center; gap: 12px; }

/* =========================================
   PURE VECTOR BRAND LOGO
   ========================================= */

.brand-logo {
    height: 68px;
    width: auto;
    display: block;
    cursor: default;
    color: var(--text-main);
    /* No blend modes or invert filters so backdrop-filter works cleanly */
}

.brand-logo .logo-tally {
    fill: currentcolor;
    transition: fill 0.3s ease;
}

.brand-logo .logo-state {
    fill: #FFD520;
}

.brand-logo .logo-wa {
    fill: #008457;
}

.brand-subtitle {
    font-weight: 700;
    font-size: 1rem;
    letter-spacing: -0.2px;
}

.icon-btn { 
    width: 40px; 
    height: 40px;
    flex-shrink: 0;
    padding: 0;
    background: var(--md-sys-color-surface-container-high); 
    border: none; 
    color: var(--md-sys-color-on-surface-variant); 
    cursor: pointer; 
    border-radius: 50%; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    transition: background 0.2s, color 0.2s, transform 0.15s ease; 
}

.icon-btn:hover { 
    background-color: var(--md-sys-color-surface-container-highest); 
    color: var(--md-sys-color-on-surface); 
}

.icon-btn:active {
    transform: scale(0.94);
}

/* =========================================
   SETTINGS ICON MORPH (GEAR <-> CLOSE)
   ========================================= */

.settings-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.settings-wrapper .icon-btn {
    position: relative;
    overflow: hidden;
}

.settings-wrapper .icon-btn .material-symbols-rounded {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    pointer-events: none;
    transition: transform 0.35s var(--m3-ease-emphasized), opacity 0.25s ease;
}

.settings-wrapper .icon-btn .icon-gear {
    transform: rotate(0deg) scale(1);
    opacity: 1;
}

.settings-wrapper .icon-btn .icon-close {
    transform: rotate(-90deg) scale(0.5);
    opacity: 0;
}

body:has(#settingsMenu.open) .settings-wrapper .icon-btn .icon-gear,
.settings-wrapper.open .icon-btn .icon-gear {
    transform: rotate(90deg) scale(0.5);
    opacity: 0;
}

body:has(#settingsMenu.open) .settings-wrapper .icon-btn .icon-close,
.settings-wrapper.open .icon-btn .icon-close {
    transform: rotate(0deg) scale(1);
    opacity: 1;
}

/* =========================================
   TWO-COLUMN SETTINGS MENU
   ========================================= */

.settings-menu {
    position: fixed;
    top: 80px;
    right: 36px;
    background: var(--surface-translucent-card);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: var(--glass-border);
    border-radius: var(--radius-card);
    padding: 16px 18px;
    display: flex;
    flex-direction: column;
    width: 500px; 
    max-width: calc(100vw - 32px);
    max-height: calc(100vh - 96px);
    overflow-y: auto;
    box-shadow: var(--shadow-level-3), var(--glass-sheen); 
    z-index: 100;
    transition: opacity 0.3s var(--m3-ease-emphasized), visibility 0.3s, transform 0.4s var(--m3-ease-emphasized);
    transform: translateY(-10px) scale(0.98); 
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.settings-menu.open {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateY(0) scale(1); 
}

.menu-two-col {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.menu-col {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.menu-col .control-group {
    margin-bottom: 8px;
}

.menu-col-title {
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--text-muted);
    letter-spacing: 0.5px;
    margin-bottom: 8px;
    padding-left: 4px;
}

.menu-divider {
    height: 1px;
    background: var(--md-sys-color-surface-container-high);
    border: none;
    margin: 12px 0 10px 0;
}

.menu-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px 10px;
}

.menu-item { 
    display: flex; 
    align-items: center; 
    gap: 10px; 
    padding: 10px 14px; 
    font-size: 0.88rem; 
    font-weight: 600; 
    color: var(--text-main); 
    background: transparent; 
    border: none; 
    cursor: pointer; 
    border-radius: var(--radius-button) !important; 
    text-align: left; 
    margin: 0;
    width: 100%;
    box-sizing: border-box;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.menu-item:hover { 
    background-color: var(--md-sys-color-surface-container-high); 
}

.menu-item .material-symbols-rounded {
    font-size: 20px;
    color: var(--text-muted);
    flex-shrink: 0;
}

/* --- Tally Bar Container & Wrapper --- */
.tally-container { 
    margin: 10px 16px 0 16px; 
    background: transparent;  
    border: none;             
    flex-shrink: 0; 
    z-index: 25; 
}

/* Height expanded to 106px to comfortably fit 3 rows of data + avatar */
.tally-wrapper { 
    height: 106px; 
    position: relative; 
    border-radius: var(--radius-card); 
    border: var(--glass-border);
    background: var(--surface-translucent);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    box-shadow: var(--shadow-level-2), var(--glass-sheen);
    overflow: hidden; 
}

.tally-bar-bg { 
    position: absolute; 
    top: 0; bottom: 0; left: 0; right: 0; 
    background: var(--color-uncalled); 
    opacity: 0.45; 
}

.tally-fill { 
    height: 100%; 
    position: absolute; 
    top: 0; 
    transition: width 0.7s cubic-bezier(0.2, 0, 0, 1), left 0.7s cubic-bezier(0.2, 0, 0, 1); 
}
.tally-fill.dem { background-color: var(--color-primary); left: 0; }
.tally-fill.rep { background-color: var(--color-secondary); right: 0; }
.tally-fill.yel { background-color: var(--color-warning); transform: none !important; }

.tally-content { 
    position: relative; 
    height: 100%; 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    padding: 0 16px; 
    z-index: 2; 
}

.center-marker { 
    position: absolute; 
    left: 50%; top: 0; bottom: 0; 
    transform: translateX(-50%); 
    display: flex; 
    flex-direction: column; 
    align-items: center; 
    justify-content: center; 
    z-index: 3; 
    pointer-events: none; 
}

.marker-line { width: 2px; height: 100%; background: var(--text-muted); opacity: 0.3; }
.marker-label { 
    position: absolute; 
    background: var(--surface-translucent-card); 
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    color: var(--text-muted); 
    font-size: 0.72rem; 
    font-weight: 700; 
    padding: 4px 10px; 
    border-radius: var(--radius-full); 
    white-space: nowrap; 
    box-shadow: var(--elevation-1), var(--glass-sheen); 
    border: var(--glass-border);
}

.score-group { 
    display: flex; 
    align-items: center; 
    gap: 12px; 
    z-index: 4;
}

/* =========================================
   TALLY PANELS (AUTHENTIC FROSTED BLUR)
   ========================================= */

.glass-panel {
    display: inline-flex;
    align-items: center;
    gap: 12px;
    min-height: 80px; /* Flexible vertical bounds */
    height: auto;
    padding: 8px 18px;
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border-radius: var(--radius-card); 
    box-shadow: var(--shadow-level-1), var(--glass-sheen); 
    transition: background-color 0.25s ease, border-color 0.25s ease, box-shadow 0.2s ease; 
}

/* Faint Party Glass Tints (Light Mode) */
#groupDem .glass-panel {
    background: rgba(26, 115, 232, 0.15);
    border: 1px solid rgba(26, 115, 232, 0.35);
}

#groupRep .glass-panel {
    background: rgba(234, 67, 53, 0.15);
    border: 1px solid rgba(234, 67, 53, 0.35);
}

/* Faint Party Glass Tints (Dark Mode) */
body.dark-mode #groupDem .glass-panel {
    background: rgba(59, 130, 246, 0.20);
    border: 1px solid rgba(59, 130, 246, 0.40);
}

body.dark-mode #groupRep .glass-panel {
    background: rgba(239, 68, 68, 0.20);
    border: 1px solid rgba(239, 68, 68, 0.40);
}

body.dark-mode #groupYel .glass-panel {
    background: rgba(251, 191, 36, 0.20);
    border: 1px solid rgba(251, 191, 36, 0.40);
}

.pill-info { 
    display: flex; 
    flex-direction: column; 
    justify-content: center; 
}

.pill-title { 
    display: inline-flex; 
    align-items: center; 
    gap: 4px; 
    font-size: 0.78rem; 
    font-weight: 700; 
    color: var(--text-muted); 
    letter-spacing: 0.5px; 
    line-height: 1;
}

/* Proportional Typography overriding inline html overrides */
.score { 
    font-size: 2.2rem !important; 
    font-weight: 900; 
    line-height: 1.05 !important; 
    letter-spacing: -0.5px; 
    margin: 1px 0 !important;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}

.raw-score { 
    font-weight: 700; 
    font-size: 0.85rem !important; 
    line-height: 1.1 !important;
    margin: 0 !important;
}

.score.dem, .raw-score.dem { color: currentcolor; }
.score.rep, .raw-score.rep { color: currentcolor; }
.score.yel, .raw-score.yel { color: currentcolor; }

/* Filled Bar State Overrides */
.score-group.filled .glass-panel { 
    box-shadow: var(--shadow-level-2), var(--glass-sheen); 
}
.score-group.filled .score, 
.score-group.filled .pill-title, 
.score-group.filled .raw-score { 
    color: #ffffff !important; 
}

/* Candidate Portraits */
.candidate-avatar {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-input); 
    object-fit: cover;
    box-shadow: var(--shadow-level-1);
    border: 2px solid transparent;
    flex-shrink: 0;
}
#avatarDem { border-color: var(--color-primary); }
#avatarRep { border-color: var(--color-secondary); }

/* Edit Label Buttons */
.edit-label-btn { 
    background: transparent; 
    border: none; 
    color: var(--text-muted); 
    cursor: pointer; 
    padding: 0; 
    margin: 0 2px;
    display: inline-flex; 
    align-items: center; 
    justify-content: center;
    opacity: 0.7;
    transition: opacity 0.2s, color 0.2s; 
}
.edit-label-btn:hover { opacity: 1; color: var(--text-main); }
.edit-label-btn span { font-size: 14px; }

/* --- Layout & Sidebar Base --- */

.main-layout { 
    display: flex; 
    flex: 1; 
    min-height: 0; 
    min-width: 0;
    padding: 10px 16px 14px 16px; 
    gap: 16px; 
    overflow: visible; 
    position: relative; 
}

.sidebar { 
    width: 320px; 
    background: var(--surface-translucent-card); 
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: var(--glass-border); 
    border-radius: var(--radius-card); 
    box-shadow: var(--shadow-level-2), var(--glass-sheen); 
    padding: 20px; 
    overflow-y: auto; 
    flex-shrink: 0; 
    position: relative;
    z-index: 20;
}

/* Custom Slim Scrollbars */
.sidebar::-webkit-scrollbar,
.settings-menu::-webkit-scrollbar {
    width: 6px;
    background: transparent; 
}

.sidebar::-webkit-scrollbar-track {
    background: transparent;
    margin: 20px 0; 
}

.settings-menu::-webkit-scrollbar-track {
    background: transparent;
    margin: 10px 0; 
}

.sidebar::-webkit-scrollbar-thumb,
.settings-menu::-webkit-scrollbar-thumb {
    background: var(--md-sys-color-surface-container-highest);
    border-radius: var(--radius-full);
}

@supports (-moz-appearance: none) {
    .sidebar, .settings-menu {
        scrollbar-width: thin;
        scrollbar-color: var(--md-sys-color-surface-container-highest) transparent;
    }
}

.district-card {
    background: var(--surface-translucent);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: var(--glass-border);
    border-radius: var(--radius-card);
    padding: 18px;
    box-shadow: var(--shadow-level-1), var(--glass-sheen);
    margin-bottom: 16px;
    transition: box-shadow 0.3s var(--m3-ease-standard);
}

.control-group { margin-bottom: 16px; }
.control-group label { display: block; font-size: 0.8rem; font-weight: 600; color: var(--text-muted); margin-bottom: 6px; }

/* Unified Pill Dropdowns */
.control-group select,
.settings-menu select {
    width: 100%;
    padding: 9px 36px 9px 16px;
    font-family: var(--font-family);
    font-size: 0.88rem;
    font-weight: 600;
    border-radius: var(--radius-button);
    border: 1px solid var(--md-sys-color-outline-variant);
    color: var(--md-sys-color-on-surface);
    background-color: var(--md-sys-color-surface-container-high);
    outline: none;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='20' viewBox='0 -960 960 960' width='20' fill='%23687B70'%3E%3Cpath d='M480-360 280-560h400L480-360Z'/%3E%3C/svg%3E");
    background-repeat: no-repeat !important;
    background-position: right 14px center !important;
    background-size: 16px 16px !important;
    
    transition: border-color 0.2s ease, background-color 0.2s ease, box-shadow 0.2s ease;
}

.control-group select:hover,
.settings-menu select:hover {
    background-color: var(--md-sys-color-surface-container-highest);
    border-color: var(--md-sys-color-outline);
}

.control-group select:focus,
.settings-menu select:focus {
    background-color: var(--md-sys-color-surface-container-lowest);
    border-color: var(--color-primary);
    box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.25);
}

body.dark-mode .control-group select,
body.dark-mode .settings-menu select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='20' viewBox='0 -960 960 960' width='20' fill='%238CA395'%3E%3Cpath d='M480-360 280-560h400L480-360Z'/%3E%3C/svg%3E");
}

.control-group select option,
.settings-menu select option {
    background-color: var(--md-sys-color-surface-container);
    color: var(--md-sys-color-on-surface);
    font-weight: 500;
}

.helper-text { font-size: 0.8rem; color: var(--text-muted); line-height: 1.5; margin-top: 14px; }

/* --- PV Inputs --- */
.pv-input-row { 
    display: flex; 
    align-items: center; 
    justify-content: space-between; 
    background: var(--surface-translucent); 
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    padding: 8px 12px; 
    border-radius: var(--radius-input); 
    border: 1px solid var(--md-sys-color-outline-variant); 
    margin-bottom: 8px; 
}

.pv-input-row input { 
    width: 120px; 
    text-align: right; 
    background: var(--md-sys-color-surface-container-high) !important; 
    border: 1px solid transparent !important; 
    border-radius: var(--radius-input) !important;
    color: var(--md-sys-color-on-surface) !important; 
    font-weight: 600; 
    font-size: 0.95rem; 
    padding: 8px 12px !important;
    outline: none; 
    font-family: var(--font-family); 
    transition: background 0.2s, border-color 0.2s, box-shadow 0.2s;
}

.pv-input-row input:hover { background: var(--md-sys-color-surface-container-highest) !important; }
.pv-input-row input:focus { 
    background: var(--md-sys-color-surface-container-lowest) !important;
    border-color: var(--color-primary) !important; 
    box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.2);
}

/* --- Action / Pill Buttons --- */
.action-btn {
    border: none;
    border-radius: var(--radius-button); 
    padding: 10px 20px;
    font-size: 0.9rem;
    font-weight: 700;
    cursor: pointer;
    font-family: var(--font-family);
    transition: all 0.3s var(--m3-ease-emphasized); 
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-shadow: var(--shadow-level-1);
    background: var(--md-sys-color-surface-container-highest);
    color: var(--text-main);
}
.action-btn:hover { box-shadow: var(--shadow-level-2); transform: translateY(-1px); filter: brightness(1.05); }
.action-btn:active { transform: scale(0.97); box-shadow: var(--shadow-level-1); filter: brightness(0.95); }
.action-btn.dem { background: var(--color-primary); color: #fff; }
.action-btn.rep { background: var(--color-secondary); color: #fff; }

/* --- Map Area & Interaction --- */
.map-area { 
    flex: 1; 
    min-height: 0; 
    min-width: 0;
    height: 100%;
    background: transparent !important; /* Transparent so it does not mask background when panning */
    display: flex; 
    align-items: center; 
    justify-content: center; 
    padding: 20px 28px; 
    position: relative; 
    overflow: visible !important;        /* Allows map to pass under sidebar, header & tally bar */
    z-index: 1;                         /* Stays below sidebar (z:20), tally (z:25), header (z:30) */
    cursor: default;
}

.map-area svg { 
    width: 100%; 
    height: 100%; 
    max-width: 1000px; 
    max-height: 90vh; 
    overflow: visible !important;
    transform-origin: 0 0;
    pointer-events: auto;
    shape-rendering: geometricPrecision;
}

/* Cursor state while dragging across the map */
body.map-panning,
body.map-panning * {
    cursor: grabbing !important;
    user-select: none !important;
    -webkit-user-select: none !important;
}

/* =========================================
   YELLOW FROSTED GLASS RESET VIEW POPUP
   ========================================= */

.reset-view-btn {
    position: absolute;
    bottom: 24px;
    right: 28px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px;
    border-radius: var(--radius-button);
    background: rgba(245, 158, 11, 0.18);
    border: 1px solid rgba(245, 158, 11, 0.45);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    box-shadow: var(--shadow-level-2), var(--glass-sheen);
    color: #854D0E;
    font-family: var(--font-family);
    font-size: 0.88rem;
    font-weight: 700;
    cursor: pointer;
    z-index: 25;
    opacity: 0;
    visibility: hidden;
    transform: translateY(12px) scale(0.95);
    transition: opacity 0.3s var(--m3-ease-emphasized),
                transform 0.3s var(--m3-ease-emphasized),
                visibility 0.3s,
                background-color 0.2s,
                border-color 0.2s,
                box-shadow 0.2s;
    user-select: none;
    -webkit-user-select: none;
}

.reset-view-btn.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

.reset-view-btn:hover {
    background: rgba(245, 158, 11, 0.28);
    border-color: rgba(245, 158, 11, 0.65);
    box-shadow: var(--shadow-level-3), var(--glass-sheen);
    transform: translateY(-1px) scale(1.02);
}

.reset-view-btn:active {
    transform: scale(0.97);
}

.reset-view-btn .material-symbols-rounded {
    font-size: 20px;
    color: currentColor;
}

/* Dark Mode Tint */
body.dark-mode .reset-view-btn {
    background: rgba(251, 191, 36, 0.22);
    border: 1px solid rgba(251, 191, 36, 0.45);
    color: #FDE68A;
}

body.dark-mode .reset-view-btn:hover {
    background: rgba(251, 191, 36, 0.32);
    border-color: rgba(251, 191, 36, 0.70);
    color: #FFFBEB;
}

@media (max-width: 900px) {
    .reset-view-btn {
        bottom: 16px;
        right: 16px;
    }
}

.state {
    fill: var(--color-uncalled);
    stroke: var(--bg-app); 
    stroke-width: 1.5px;
    cursor: pointer; 
    shape-rendering: geometricPrecision;
    transition: fill 0.2s ease, opacity 0.2s ease, stroke 0.2s ease;
}
.state:hover { opacity: 0.8; }

.state.dem { fill: var(--color-primary) !important; }
.state.rep { fill: var(--color-secondary) !important; }
.state.yel { fill: var(--color-warning) !important; }

.state.selected {
    filter: brightness(1.2) drop-shadow(0px 0px 6px rgba(0,0,0,0.3));
    stroke: var(--text-main);
    stroke-width: 2.5px;
}
body.dark-mode .state.selected {
    filter: brightness(1.35) drop-shadow(0px 0px 6px rgba(255,255,255,0.25));
}

/* --- Tooltip (Frosted Glass) --- */
#tooltip { 
    position: absolute; 
    background: var(--surface-translucent-card); 
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    color: var(--text-main); 
    padding: 8px 14px; 
    border-radius: var(--radius-md); 
    font-size: 0.85rem; 
    font-weight: 700; 
    box-shadow: var(--shadow-level-2), var(--glass-sheen); 
    border: var(--glass-border); 
    pointer-events: none;
    opacity: 0; 
    transition: opacity 0.15s ease; 
    z-index: 1000; 
    min-width: 140px; 
    transform: translate(10px, 10px); 
}
#tooltip.visible { opacity: 1; }

/* Responsive adjustments */
@media (max-width: 900px) {
    .main-layout { flex-direction: column; overflow-y: auto; padding: 12px; gap: 12px; }
    .sidebar { width: 100%; padding: 16px; }
    .map-area { min-height: 400px; padding: 16px; }
    header { margin: 8px 12px 0 12px; }
    .tally-container { margin: 8px 12px 0 12px; }
    .settings-menu {
        top: 76px;
        right: 32px;
    }
}

@media (max-width: 560px) {
    .settings-menu {
        top: 76px !important;
        right: 12px !important;
        left: 12px !important;
        width: auto !important;
        max-width: calc(100vw - 24px) !important;
        padding: 14px !important;
    }

    .menu-two-col,
    .menu-grid {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }
}

/* Settings Menu Version & Map Attribution */
.menu-footer-version {
    padding: 10px 16px 8px 16px;
    font-size: 0.72rem;
    font-weight: 500;
    color: var(--text-muted);
    opacity: 0.75;
    text-align: center;
    line-height: 1.5;
    letter-spacing: 0.3px;
}

.menu-footer-version a {
	color: var(--text-muted);
	text-decoration: none;
}

/* --- Center Third Party Pill Vertically --- */
#groupYel {
    position: absolute;
    top: 50% !important;
    transform: translate(-50%, -50%) !important;
    margin: 0 !important;
    z-index: 4;
    transition: left 0.7s cubic-bezier(0.2, 0, 0, 1), opacity 0.3s var(--m3-ease-emphasized);
}

#groupYel .glass-panel {
    flex-direction: column !important;
    align-items: center !important;
    text-align: center !important;
    gap: 2px !important;
    padding: 6px 18px !important;
    min-height: unset !important;
    height: auto !important;
    background: rgba(245, 158, 11, 0.16);
    border: 1px solid rgba(245, 158, 11, 0.35);
}

#groupYel .pill-title {
    justify-content: center;
    font-size: 0.75rem !important;
    margin: 0 !important;
}

#groupYel .score {
    font-size: 1.8rem !important;
    line-height: 1.05 !important;
    margin: 0 !important;
}

#groupYel .raw-score {
    font-size: 0.78rem !important;
    margin: 0 !important;
}

/* --- High-Contrast Third Party Tally Text --- */
#groupYel .score,
#groupYel .raw-score,
#groupYel .pill-title,
#groupYel .edit-label-btn {
    color: var(--text-main) !important;
}

/* Ensure crisp contrast when resting on top of the yellow bar fill */
#groupYel.filled .glass-panel {
    background: rgba(0, 0, 0, 0.12) !important;
    border-color: rgba(0, 0, 0, 0.25) !important;
}

#groupYel.filled .score,
#groupYel.filled .raw-score,
#groupYel.filled .pill-title,
#groupYel.filled .pill-title span,
#groupYel.filled .edit-label-btn {
    color: #000000;
    text-shadow: none !important;
}

/* Allow win label to fade smoothly */
.marker-label {
    transition: opacity 0.25s ease;
}

/* =========================================
   CANDIDATE PORTRAITS MENU ITEMS
   ========================================= */

.menu-item.menu-item-dem {
    background: rgba(26, 115, 232, 0.12);
    border: 1px solid rgba(26, 115, 232, 0.25);
    color: var(--text-main);
    margin-bottom: 6px;
}

.menu-item.menu-item-dem .material-symbols-rounded {
    color: var(--color-primary);
}

.menu-item.menu-item-dem:hover {
    background: rgba(26, 115, 232, 0.22);
}

.menu-item.menu-item-rep {
    background: rgba(234, 67, 53, 0.12);
    border: 1px solid rgba(234, 67, 53, 0.25);
    color: var(--text-main);
}

.menu-item.menu-item-rep .material-symbols-rounded {
    color: var(--color-secondary);
}

.menu-item.menu-item-rep:hover {
    background: rgba(234, 67, 53, 0.22);
}

.menu-item > span {
    pointer-events: none;
}

.clear-portrait-btn {
    margin-left: auto;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: transparent;
    border: none;
    padding: 0;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    opacity: 0.75;
    pointer-events: auto !important;
    z-index: 2;
    transition: background-color 0.2s ease, opacity 0.2s ease, color 0.2s ease, transform 0.15s ease;
}

.clear-portrait-btn.visible {
    display: inline-flex !important;
}

.clear-portrait-btn:hover {
    opacity: 1;
    transform: scale(1.1);
}

.clear-portrait-btn span {
    font-size: 16px !important;
    pointer-events: none;
}

.menu-item-dem .clear-portrait-btn:hover {
    color: var(--color-primary);
    background: rgba(26, 115, 232, 0.2);
}

.menu-item-rep .clear-portrait-btn:hover {
    color: var(--color-secondary);
    background: rgba(234, 67, 53, 0.2);
}

body.dark-mode .menu-item.menu-item-dem {
    background: rgba(59, 130, 246, 0.18);
    border-color: rgba(59, 130, 246, 0.32);
}

body.dark-mode .menu-item.menu-item-rep {
    background: rgba(239, 68, 68, 0.18);
    border-color: rgba(239, 68, 68, 0.32);
}

body.dark-mode .menu-item.menu-item-dem .clear-portrait-btn:hover {
    background: rgba(59, 130, 246, 0.28);
}

body.dark-mode .menu-item.menu-item-rep .clear-portrait-btn:hover {
    background: rgba(239, 68, 68, 0.28);
}

/* =========================================
   POLITICAL ENVIRONMENT SHIFT TOOL
   ========================================= */

.nat-env-tool {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 40px;
    padding: 3px 6px;
    background: var(--md-sys-color-surface-container-high);
    border: 1px solid var(--md-sys-color-outline-variant);
    border-radius: var(--radius-button);
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.06);
    color: var(--text-main);
    transition: background-color 0.25s ease, border-color 0.25s ease, opacity 0.25s ease, color 0.25s ease;
}

.nat-env-tool .nat-env-display {
    flex: 1;
    text-align: center;
    font-weight: 800;
    font-size: 0.95rem;
    letter-spacing: 0.5px;
    color: inherit;
    user-select: none;
    -webkit-user-select: none;
}

.nat-env-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: inherit;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    padding: 0;
    transition: background-color 0.2s ease, transform 0.15s ease;
}

.nat-env-btn:hover {
    background-color: rgba(0, 0, 0, 0.08);
}

body.dark-mode .nat-env-btn:hover {
    background-color: rgba(255, 255, 255, 0.12);
}

.nat-env-btn:active {
    transform: scale(0.92);
}

.nat-env-btn .material-symbols-rounded {
    font-size: 20px;
    line-height: 1;
}

.nat-env-tool.dem-lead {
    background: rgba(26, 115, 232, 0.12);
    border-color: rgba(26, 115, 232, 0.3);
    color: var(--color-primary);
}

body.dark-mode .nat-env-tool.dem-lead {
    background: rgba(59, 130, 246, 0.18);
    border-color: rgba(59, 130, 246, 0.35);
    color: var(--color-primary);
}

.nat-env-tool.rep-lead {
    background: rgba(234, 67, 53, 0.12);
    border-color: rgba(234, 67, 53, 0.3);
    color: var(--color-secondary);
}

body.dark-mode .nat-env-tool.rep-lead {
    background: rgba(239, 68, 68, 0.18);
    border-color: rgba(239, 68, 68, 0.35);
    color: var(--color-secondary);
}

.nat-env-tool.yel-lead {
    background: rgba(245, 158, 11, 0.14);
    border-color: rgba(245, 158, 11, 0.32);
    color: var(--color-warning);
}

body.dark-mode .nat-env-tool.yel-lead {
    background: rgba(251, 191, 36, 0.18);
    border-color: rgba(251, 191, 36, 0.35);
    color: var(--color-warning);
}

.nat-env-tool.disabled {
    opacity: 0.35;
    pointer-events: none !important;
    cursor: not-allowed;
    background: var(--md-sys-color-surface-container-high) !important;
    border-color: var(--md-sys-color-outline-variant) !important;
    color: var(--text-muted) !important;
}

.nat-env-tool.disabled .nat-env-btn {
    pointer-events: none !important;
}

/* =========================================
   COUNTY ENVIRONMENT SHIFT COLLAPSE (M3 EXPRESSIVE)
   ========================================= */

.county-env-collapse {
    display: grid;
    grid-template-rows: 0fr;
    opacity: 0;
    transform: translateY(-8px) scale(0.96);
    pointer-events: none;
    transition: grid-template-rows 0.4s var(--m3-ease-emphasized),
                opacity 0.25s var(--m3-ease-standard),
                transform 0.4s var(--m3-ease-emphasized);
}

.county-env-collapse.open {
    grid-template-rows: 1fr;
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

.county-env-inner {
    overflow: hidden;
    min-height: 0;
}

/* =========================================
   MOBILE & TABLET RESPONSIVE OPTIMIZATION
   ========================================= */

/* Prevent browser pull-to-refresh / rubber-banding during SVG pan & pinch */
.map-area,
.map-area svg {
    touch-action: none;
    -webkit-touch-callout: none;
}

/* -----------------------------------------
   1. TABLET & MOBILE PORTRAIT (<= 900px)
   ----------------------------------------- */
@media (max-width: 900px) {
    body {
        height: 100dvh;
        min-height: -webkit-fill-available;
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
        overflow: hidden;
    }

    /* Compact Header */
    header {
        height: 52px;
        margin: 8px 10px 0 10px;
        padding: 0 14px;
        border-radius: 22px;
    }

    .brand-logo {
        height: 52px;
    }

    .brand-subtitle {
        font-size: 0.92rem;
    }

    /* Compact Tally Bar */
    .tally-container {
        margin: 8px 10px 0 10px;
    }

    .tally-wrapper {
        height: 86px;
        border-radius: 22px;
    }

    .tally-content {
        padding: 0 10px;
    }

    .glass-panel {
        min-height: 64px;
        padding: 6px 12px;
        gap: 8px;
        border-radius: 20px;
    }

    .score {
        font-size: 1.65rem !important;
        letter-spacing: -0.5px;
    }

    .raw-score {
        font-size: 0.75rem !important;
    }

    .pill-title {
        font-size: 0.7rem;
    }

    .candidate-avatar {
        width: 38px;
        height: 38px;
        border-radius: 12px;
    }

    /* Third-Party Center Pill */
    #groupYel .glass-panel {
        padding: 4px 12px !important;
        border-radius: 18px;
    }

    #groupYel .score {
        font-size: 1.35rem !important;
    }

    #groupYel .raw-score {
        font-size: 0.68rem !important;
    }

    /* Stacked Scrollable Workspace (Portrait) */
    .main-layout {
        flex-direction: column;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        padding: 8px 10px calc(16px + env(safe-area-inset-bottom)) 10px;
        gap: 10px;
    }

    /* Map First in Portrait for Immediate Visual Feedback */
    .map-area {
        order: 1;
        flex: none;
        width: 100%;
        height: 44dvh;
        min-height: 270px;
        max-height: 420px;
        padding: 8px;
        overflow: hidden !important; /* Prevents panned map from overlapping scrolled controls */
        border-radius: var(--radius-card);
    }

    .sidebar {
        order: 2;
        width: 100%;
        flex: none;
        overflow-y: visible;
        padding: 16px;
        border-radius: 22px;
    }

    .sidebar h3 {
        margin-bottom: 12px;
        padding-bottom: 8px;
        font-size: 0.9rem;
    }

    .district-card {
        padding: 14px;
        margin-bottom: 0;
        border-radius: 20px;
    }

    /* Prevent iOS Auto-Zoom on Focus (16px minimum) & Enlarge Touch Targets */
    .control-group select,
    .settings-menu select,
    .pv-input-row input {
        font-size: 16px !important;
        min-height: 42px;
    }

    .pv-input-row {
        padding: 6px 12px;
    }

    .pv-input-row input {
        width: 130px;
    }

    .nat-env-tool {
        height: 44px;
    }

    .nat-env-btn {
        width: 38px;
        height: 38px;
    }

    /* Reset View Floating Button */
    .reset-view-btn {
        bottom: 12px;
        right: 12px;
        padding: 8px 14px;
        font-size: 0.82rem;
    }

    /* Hide hover tooltip on touch devices (selection updates sidebar directly) */
    #tooltip {
        display: none !important;
    }

    /* Settings Drawer */
    .settings-menu {
        top: 68px;
        right: 10px;
        left: 10px;
        width: auto;
        max-width: none;
        max-height: calc(100dvh - 82px);
        padding: 16px;
        border-radius: 24px;
    }
}

/* -----------------------------------------
   2. SMALL PHONES PORTRAIT (<= 560px)
   ----------------------------------------- */
@media (max-width: 560px) {
    .brand-logo {
        height: 44px;
    }

    .brand-subtitle {
        font-size: 0.85rem;
        max-width: 135px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .tally-wrapper {
        height: 78px;
    }

    .glass-panel {
        min-height: 56px;
        padding: 5px 10px;
        gap: 6px;
    }

    .score {
        font-size: 1.4rem !important;
    }

    .raw-score {
        font-size: 0.7rem !important;
    }

    .candidate-avatar {
        width: 32px;
        height: 32px;
        border-radius: 10px;
    }

    .marker-label {
        font-size: 0.62rem;
        padding: 3px 7px;
    }

    /* Single-column Settings Menu on narrow screens */
    .menu-two-col,
    .menu-grid {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    .menu-item {
        min-height: 44px;
        padding: 10px 14px;
    }
}

/* -----------------------------------------
   3. MOBILE LANDSCAPE (Short Viewports)
   ----------------------------------------- */
@media (orientation: landscape) and (max-height: 540px) {
    body {
        height: 100dvh;
        overflow: hidden;
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }

    /* Ultra-compact Top Bars to Maximize Map & Input Height */
    header {
        height: 42px;
        margin: 6px 10px 0 10px;
        padding: 0 12px;
        border-radius: 18px;
    }

    .brand-logo {
        height: 40px;
    }

    .icon-btn {
        width: 34px;
        height: 34px;
    }

    .tally-container {
        margin: 6px 10px 0 10px;
    }

    .tally-wrapper {
        height: 58px;
        border-radius: 18px;
    }

    .glass-panel {
        min-height: 44px;
        padding: 4px 12px;
        gap: 8px;
        border-radius: 14px;
    }

    /* Inline score + vote count in landscape to save vertical space */
    .pill-info {
        flex-direction: row;
        align-items: baseline;
        gap: 8px;
    }

    #groupRep .pill-info {
        flex-direction: row-reverse;
        align-items: baseline !important;
    }

    .score {
        font-size: 1.35rem !important;
    }

    .raw-score {
        font-size: 0.72rem !important;
    }

    .candidate-avatar {
        width: 30px;
        height: 30px;
        border-radius: 8px;
    }

    /* Side-by-Side Workspace in Landscape */
    .main-layout {
        flex-direction: row;
        overflow: hidden;
        padding: 6px 10px 8px 10px;
        gap: 10px;
    }

    .sidebar {
        order: 1;
        width: 280px;
        height: 100%;
        overflow-y: auto;
        padding: 12px;
        border-radius: 18px;
    }

    .sidebar h3 {
        font-size: 0.82rem;
        margin-bottom: 8px;
        padding-bottom: 6px;
    }

    .district-card {
        padding: 10px;
        border-radius: 16px;
    }

    .helper-text {
        display: none; /* Hide instructional copy in tight landscape viewports */
    }

    .map-area {
        order: 2;
        flex: 1;
        height: 100%;
        min-height: 0;
        max-height: none;
        padding: 6px 12px;
        overflow: hidden !important;
    }

    /* Two-column Settings Menu fits cleanly in Landscape */
    .settings-menu {
        top: 54px;
        right: 12px;
        left: auto;
        width: min(520px, calc(100vw - 24px));
        max-height: calc(100dvh - 64px);
        padding: 12px 14px;
    }

    .menu-two-col,
    .menu-grid {
        grid-template-columns: 1fr 1fr !important;
    }
}