@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;600;700&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Libre+Baskerville:wght@400;700&display=swap');

@font-face {
  font-family: 'Twemoji Country Flags';
  /* Regional Indicator Symbols (national flags) + Black Flag + TAG characters (subdivision flags) */
  unicode-range: U+1F1E6-1F1FF, U+1F3F4, U+E0000-E007F;
  src: url('https://cdn.jsdelivr.net/npm/country-flag-emoji-polyfill@0.1/dist/TwemojiCountryFlags.woff2') format('woff2');
  font-display: swap;
}

:root {
            /* Surfaces */
            --bg-app: #121212;
            --bg-surface: #1E1E1E;
            --bg-surface-elevated: #2D2D2D;
            --border-color: #383838;

            /* Brand Colors */
            --primary: #90CAF9;
            --primary-hover: #42A5F5;
            --secondary: #80CBC4;
            --accent: #BB86FC;

            /* Functional */
            --status-breaking: #F48FB1;
            --status-opinion: #FFCC80;
            --status-trending: #A5D6A7;

            /* Text */
            --text-high: #E1E1E1;
            --text-medium: #B0B0B0;
            --text-low: #757575;
            --user-defined-font: "Libre Baskerville", Georgia, "Times New Roman", Times, serif;
            /* Display font for headlines/nav/footers */
            --display-font: "IBM Plex Sans", ui-sans-serif, system-ui, "Segoe UI", Roboto, Arial, sans-serif;
        }

        /* Fallback/correct root variables: ensure variables are defined regardless of UA parsing */
        :root {
            /* Surfaces */
            --bg-app: #121212;
            --bg-surface: #1E1E1E;
            --bg-surface-elevated: #2D2D2D;
            --border-color: #383838;

            /* Brand Colors */
            --primary: #90CAF9;
            --primary-hover: #42A5F5;
            --secondary: #80CBC4;
            --accent: #BB86FC;

            /* Functional */
            --status-breaking: #F48FB1;
            --status-opinion: #FFCC80;
            --status-trending: #A5D6A7;

            /* Text */
            --text-high: #E1E1E1;
            --text-medium: #B0B0B0;
            --text-low: #757575;
            --user-defined-font: "Libre Baskerville", Georgia, "Times New Roman", Times, serif;
            /* Display font for headlines/nav/footers */
            --display-font: "IBM Plex Sans", ui-sans-serif, system-ui, "Segoe UI", Roboto, Arial, sans-serif;
        }

        /* Universal link styling using semantic color tokens */
        a {
            color: var(--primary);
            text-decoration-thickness: 0.08em;
            text-underline-offset: 2px;
        }
        a:visited { color: var(--primary); }
        a:hover,
        a:focus-visible {
            color: var(--primary-hover);
            text-decoration: underline;
        }
        a:active { color: var(--primary-hover); }

        /* Optional light color scheme using the same variable tokens */
        @media (prefers-color-scheme: light) {
            :root {
                --bg-app: #f7f7f8;
                --bg-surface: #ffffff;
                --bg-surface-elevated: #f2f4f7;
                --border-color: #e3e8ef;

                --primary: #1565C0;
                --primary-hover: #0D47A1;
                --secondary: #00897B;
                --accent: #7E57C2;

                --text-high: #111316;
                --text-medium: #475569;
                --text-low: #64748B;
            }
        }

        body {
            background-color: var(--bg-app);
            color: var(--text-high);
            font-family: var(--user-defined-font);
            margin: 0;
            font-size: 16px;
        }
        

        /* Headline / Navigation / Footer display font wiring */
        h1, h2, h3,
        header .logo,
        nav, nav a,
        /* Ensure news-card footer uses display font */
        .news-card-footer,
        .news-card .footer, .time-info,
        .news-detail-container h2,
        .sidebar-section h3,
        .source-list ul,
        #sources-container > div > ul > li > span
        {
            font-family: var(--display-font);
        }

        /* Ensure published/fuzzy time line uses display font */
        .published-line,
        .published-line .local-time,
        .published-line .fuzzy-time {
            font-family: var(--display-font);
        }

        /* Sidebar: make the opened article headline use secondary color */
        .sidebar-section h1 {
            color: var(--secondary);
        }

        /* Windows flag emoji rendering fix */
        /* Ensure Twemoji Country Flags wins over high-specificity selectors applying display font */
        .flag-emoji,
        .country-flag,
        .win,
        .flag-emoji.win,
        #sources-container > div > ul > li > span.win {
            font-family: "Twemoji Country Flags", var(--user-defined-font) !important;
            line-height: 1; /* align emoji box nicely */
            font-variant-emoji: emoji;
        }

        /* Extra-robust targeting on Windows: use UA-derived body.win (set in index.html) */
        body.win .flag-emoji,
        body.win .country-flag,
        body.win .news-card-footer .flag-emoji,
        body.win #sources-container .flag-emoji {
            font-family: "Twemoji Country Flags", var(--user-defined-font) !important;
            line-height: 1;
            font-variant-emoji: emoji;
        }

        /* Scrollbar Styles */
        ::-webkit-scrollbar {
            width: 12px;
        }

        ::-webkit-scrollbar-track {
            background: var(--bg-surface);
        }

        ::-webkit-scrollbar-thumb {
            background-color: var(--bg-surface-elevated);
            border-radius: 6px;
            border: 3px solid var(--bg-surface);
        }

        ::-webkit-scrollbar-thumb:hover {
            background-color: var(--primary-hover);
        }

        /* Sidebar source list: ensure source name uses display font explicitly */
        .source-list .source-name {
            font-family: var(--display-font);
            font-weight: 600;
            margin-right: 0.35rem;
        }

        /* Flag emoji sizing */
        .country-flag {
            font-size: 1.2em; /* slightly larger for visibility on cards */
            line-height: 1;
            margin-left: 0.15rem;
        }
        /* Flags next to source names in sidebar */
        .source-list .flag-emoji {
            font-size: 1.1em;
            margin: 0 0.25rem 0 0.1rem;
        }

        .container {
            display: grid;
            /* Flexible space, Nav (fixed), Main (fixed), Aside (fixed), Flexible space */
            grid-template-columns: 1fr 344px 900px 556px 1fr; /* Golden ratio! */
            grid-template-rows: auto 1fr;
            grid-template-areas:
                "header header header header header" /* Header spans all columns */
                ". nav main aside ."; /* Nav, Main, Aside are centered with flexible space on sides */
            height: 100vh;
            gap: 1rem; /* Re-introduce gap for overall spacing */
            padding: 0; /* Remove container padding, spacing handled by 1fr columns */
        }

        header {
            grid-area: header;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 1rem;
            border-bottom: 1px solid var(--border-color);
            background-color: var(--bg-surface-elevated);
        }

        /* Hamburger (hidden by default, shown on mobile) */
        .hamburger {
            display: none;
            width: 40px;
            height: 40px;
            border: 1px solid var(--primary);
            background: transparent;
            border-radius: 6px;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            color: var(--primary);
            transition: color 0.15s ease, border-color 0.15s ease;
        }

        /* Ensure Material icon follows the button color */
        .hamburger .material-icons-round {
            color: inherit;
        }

        /* Hover/active states use primary-hover */
        .hamburger:hover,
        .hamburger:active {
            color: var(--primary-hover);
            border-color: var(--primary-hover);
        }
        .hamburger:focus-visible {
            outline: none;
            color: var(--primary-hover);
            border-color: var(--primary-hover);
            box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary-hover) 40%, transparent);
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            display: flex;
            align-items: center;
        }
        
        .logo img {
            height: 1.5rem;
            margin-right: 0.5rem;
        }

        .search-bar input {
            background-color: var(--bg-surface);
            border: 1px solid var(--border-color);
            border-radius: 4px;
            color: var(--text-high);
            padding: 0.5rem;
            width: 300px;
        }

        nav {
            grid-area: nav;
            padding-top: 1rem;
        }

        nav ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        nav ul li a {
            display: block;
            padding: 0.75rem 1rem;
            text-decoration: none;
            color: var(--text-medium);
            border-radius: 4px;
            margin-bottom: 0.5rem;
        }

        nav ul li a:hover {
            background-color: var(--bg-surface);
            color: var(--text-high);
        }
        
        nav ul li a.active {
            background-color: var(--bg-surface);
            color: var(--primary);
            font-weight: bold;
        }

        main {
            grid-area: main;
            overflow-y: auto;
            padding-top: 1rem;
            /* Removed padding-left here, gap will handle it */
        }

        .news-grid {
            display: flex; /* Use flexbox for a single column layout */
            flex-direction: column;
            gap: 1rem; /* Add gap between news cards */
            /* Removed max-width and margin: auto as main column now has fixed width */
        }

        p {
            font-family: var(--user-defined-font); /* Use site body font variable */
            line-height: 1.3; /* Added line-height for better spacing */
        }

        aside {
            grid-area: aside;
            padding-top: 1rem;
            /* Removed padding-left here, gap will handle it */
        }

        .sidebar-section {
            background-color: var(--bg-surface);
            border-radius: 8px;
            padding: 1rem;
            margin-bottom: 1rem;
        }

        .sidebar-section h3 {
            margin-top: 0;
            border-bottom: 1px solid var(--border-color);
            padding-bottom: 0.5rem;
            font-size: 1rem;
            color: var(--text-high);
        }

        .news-detail-container {
            background-color: var(--bg-surface);
            border-radius: 8px;
            padding: 2rem;
            border: 1px solid var(--border-color);
        }

        .news-detail-container h2 {
            margin-top: 0;
            color: var(--text-high);
        }

        .news-detail-container p {
            color: var(--text-medium);
        }

        .article-list {
            list-style: none;
            padding: 0;
            margin-top: 1rem;
        }

        .article-list li {
            margin-bottom: 1rem;
            border-bottom: 1px solid var(--border-color);
            padding-bottom: 1rem;
        }

        /* Styling for grouped sources in the sidebar */
        .source-list {
            list-style: none;
            padding: 0;
            margin-top: 1rem;
        }

        .source-list > li { /* Direct children of source-list (the source groups) */
            margin-bottom: 1rem;
            padding-bottom: 0.5rem;
        }

        .source-list > li:last-child {
            border-bottom: none;
        }

        .source-list > li > .source-label { /* Source name and flag on one line, no wrapping */
            display: inline-flex;
            align-items: baseline;
            white-space: nowrap;
            font-weight: bold;
            gap: 0.35rem;
            margin-bottom: 0.5rem;
        }

        .source-list > li > .source-label .flag-emoji { /* Adjust flag emoji spacing */
            margin-right: 0;
        }

        .source-list ul { /* Nested list for articles under each source */
            list-style: none;
            padding-left: 1rem; /* Indent articles */
            margin: 0;
        }

        .source-list ul li { /* Individual articles under a source */
            margin-bottom: 0.25rem;
        }

        .source-list ul li a { /* Article links */
            text-decoration: none;
            color: var(--primary);
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            display: block;
        }

        .source-list ul li a:hover {
            color: var(--primary-hover);
        }

        /* General link styling for article-list and source-list */
        .article-list a {
            text-decoration: none;
            color: var(--primary);
        }

        .article-list a:hover {
            color: var(--primary-hover);
        }

        .article-source {
            font-size: 0.9rem;
            color: var(--text-low);
            margin-left: 0.5rem;
        }

        .news-card {
            background-color: var(--bg-surface);
            border-radius: 8px;
            padding: 1rem;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            border: 1px solid var(--border-color);
            transition: background-color 0.2s ease-in-out;
            margin-bottom: 0; /* Gap is handled by news-grid now */
        }

        .news-card:hover {
            background-color: var(--bg-surface-elevated);
        }

        .news-card h3 {
            margin: 0 0 0.5rem 0;
            font-size: 1.5rem;
            color: var(--primary);
        }

        .news-card h3 a {
            text-decoration: none;
        }

        .news-card h3 a:hover {
            color: var(--primary-hover);
        }

        .news-card p {
            font-size: 0.9rem;
            color: var(--text-medium);
            font-family: var(--user-defined-font);
        }

        .news-card-footer {
            font-size: 0.8rem;
            color: var(--text-low);
            margin-top: 1rem;
            display: flex;
            flex-direction: column; /* flags row at the bottom */
            gap: 0.5rem;
            align-items: stretch;
        }

        .news-card-footer .footer-top {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .news-card-footer .footer-flags {
            display: flex;
            flex-wrap: wrap;
            gap: 0.25rem 0.35rem;
        }

        .news-card-footer .footer-flags .country-flag {
            font-size: 1.5em; /* larger flags on their own row */
            line-height: 1;
            margin-left: 0;
        }

        .time-info {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
        }

        @media (max-width: 900px) {
            .container {
                grid-template-columns: 1fr;
                grid-template-rows: auto auto 1fr auto; /* header, nav, main, aside stacked */
                grid-template-areas:
                    "header"
                    "nav"
                    "main"
                    "aside";
                height: auto;
                padding: 0;
            }

            /* Hide the right-side placeholder in header so the menu button can align right */
            header > div:last-child { display: none; }

            /* Show hamburger on mobile */
            .hamburger { display: inline-flex; }

            /* Mobile nav dropdown */
            nav {
                display: none; /* hidden by default */
                padding: 0.5rem 1rem 1rem 1rem;
                background: var(--bg-surface-elevated);
                border-bottom: 1px solid var(--border-color);
            }

            body.nav-open nav { display: block; }

            nav ul li a {
                margin-bottom: 0.35rem;
            }

            main {
                padding: 1rem;
            }
        }
