:root {
    --bg: #f3f4f6;
    --surface: #ffffff;
    --surface-2: #f9fafb;
    --hover: #f1f5f9;
    --border: #e5e7eb;
    --text: #111827;
    --text-2: #4b5563;
    --text-3: #6b7280;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --accent-soft: #dbeafe;
    --accent-text: #1e40af;
    --link: #2563eb;
    --icon-plate: transparent;
    --danger: #dc2626;
    --danger-soft: #fee2e2;
    --star: #f59e0b;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.05);
    --shadow-lg: 0 20px 40px -12px rgb(0 0 0 / 0.25);
    --radius: 10px;
    --sidebar-w: 300px;
    --topbar-h: 56px;
    color-scheme: light;
}

.dark {
    /* Neutral near-black greys (no blue tint). Cards sit one step above the page. */
    --bg: #000000;
    --surface: #121212;
    --surface-2: #0a0a0a;
    --hover: #222222;
    --border: #2e2e2e;
    --text: #f2f2f2;
    --text-2: #c8c8c8;
    --text-3: #9e9e9e;
    --accent: #2563eb;
    --accent-hover: #3b82f6;
    --accent-soft: rgb(59 130 246 / 0.2);
    --accent-text: #bfdbfe;
    --link: #6ea8fe;
    --danger: #ff6b6b;
    --danger-soft: rgb(255 107 107 / 0.12);
    --star: #fbbf24;
    --icon-plate: #e8e8e8;
    --shadow: none;
    --shadow-lg: 0 20px 40px -12px rgb(0 0 0 / 0.8), 0 0 0 1px #2e2e2e;
    color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; }
svg { flex: none; }
[hidden] { display: none !important; }

.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;
}

:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }

/* ---------- Buttons & inputs ---------- */

.icon-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 38px; height: 38px; border: 0; border-radius: 8px;
    background: transparent; color: var(--text-2);
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn.small { width: 30px; height: 30px; }
.icon-btn.small svg { width: 16px; height: 16px; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    border: 1px solid var(--border); border-radius: 8px;
    background: var(--surface); color: var(--text);
    padding: 8px 14px; font-weight: 600; font-size: 14px; white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: 0.6; cursor: default; }
.btn svg { width: 16px; height: 16px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.block { width: 100%; }
.btn.added { background: transparent; color: var(--text-3); border-color: var(--border); }

.input {
    width: 100%; padding: 9px 12px; font-size: 14px;
    border: 1px solid var(--border); border-radius: 8px;
    background: var(--surface); color: var(--text);
}
.input:focus { outline: none; border-color: var(--link); box-shadow: 0 0 0 3px var(--accent-soft); }
.input::placeholder { color: var(--text-3); }

.field-label { display: block; font-size: 13px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.field { margin-bottom: 16px; }
.error-text { color: var(--danger); font-size: 13px; margin: 6px 0 0; }
.muted { color: var(--text-3); }

.spinner {
    width: 16px; height: 16px; border-radius: 50%;
    border: 2px solid currentColor; border-right-color: transparent;
    animation: spin 0.7s linear infinite; display: inline-block; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Top bar ---------- */

.topbar {
    flex: none; height: var(--topbar-h);
    display: flex; align-items: center; gap: 4px; padding: 0 8px;
    background: var(--surface); border-bottom: 1px solid var(--border);
    position: relative; z-index: 30;
}
.topbar-title {
    flex: 1; min-width: 0; text-align: center;
    border: 0; background: none; padding: 6px 8px; border-radius: 8px;
    font-weight: 700; font-size: 17px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar-title:hover { background: var(--hover); }
.topbar-title.pulsing { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.45; } }
.home-link { font-weight: 700; text-decoration: none; padding: 6px 10px; border-radius: 8px; color: var(--text-2); font-size: 14px; }
.home-link:hover { background: var(--hover); color: var(--text); }
.refresh-btn.spinning svg { animation: spin 0.9s linear infinite; }

/* ---------- Layout ---------- */

.layout { flex: 1; display: flex; min-height: 0; position: relative; }

.sidebar {
    width: var(--sidebar-w); flex: none;
    display: flex; flex-direction: column;
    background: var(--surface); border-right: 1px solid var(--border);
    min-height: 0;
}
.sidebar-scroll { flex: 1; overflow-y: auto; padding: 12px; overscroll-behavior: contain; }
.sidebar-footer { flex: none; padding: 12px; border-top: 1px solid var(--border); background: var(--surface-2); }
.sidebar-header { display: none; }

.overlay { display: none; }

@media (max-width: 1023px) {
    .sidebar {
        position: fixed; top: 0; bottom: 0; left: 0; z-index: 60;
        width: min(var(--sidebar-w), 86vw);
        transform: translateX(-100%); transition: transform 0.25s ease;
        box-shadow: var(--shadow-lg); visibility: hidden;
    }
    .sidebar.open { transform: none; visibility: visible; }
    .sidebar-header {
        display: flex; align-items: center; justify-content: space-between;
        padding: 10px 12px; border-bottom: 1px solid var(--border); font-weight: 700;
    }
    .overlay.open { display: block; position: fixed; inset: 0; z-index: 55; background: rgb(0 0 0 / 0.45); }
}
@media (min-width: 1024px) {
    #menu-btn { display: none; }
}

.nav-item {
    display: flex; align-items: center; gap: 10px; width: 100%;
    padding: 9px 10px; border: 0; border-radius: 8px; background: transparent;
    text-align: left; font-weight: 600; color: var(--text-2);
}
.nav-item:hover { background: var(--hover); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent-text); }
.nav-item svg { width: 18px; height: 18px; }
.nav-item .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { font-size: 12px; font-weight: 700; color: var(--text-3); font-variant-numeric: tabular-nums; }
.nav-item.active .count { color: inherit; }

.section-head {
    display: flex; align-items: center; justify-content: space-between;
    margin: 18px 4px 6px; font-size: 11px; font-weight: 700; letter-spacing: 0.06em;
    text-transform: uppercase; color: var(--text-3);
}

.discover-btn { margin-bottom: 10px; }

.feed-row { display: flex; align-items: center; border-radius: 8px; }
.feed-row:hover { background: var(--hover); }
.feed-row.active { background: var(--accent-soft); }
.feed-row .feed-select {
    flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px;
    padding: 8px 6px 8px 10px; border: 0; background: transparent; text-align: left;
}
.feed-row .feed-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.feed-row.active .feed-name { color: var(--accent-text); font-weight: 600; }
.feed-row .feed-actions { display: flex; padding-right: 4px; }
@media (hover: hover) and (min-width: 1024px) {
    .feed-row .feed-actions { opacity: 0; }
    .feed-row:hover .feed-actions, .feed-row:focus-within .feed-actions { opacity: 1; }
}
.feed-error { color: var(--danger); display: inline-flex; }
.feed-error svg { width: 16px; height: 16px; }

.favicon {
    width: 18px; height: 18px; border-radius: 4px; flex: none; object-fit: cover;
    background: var(--hover);
}
/* Dark or transparent icons (e.g. a black logo) vanish on black; give them a light tile. */
.dark img.favicon { background: var(--icon-plate); padding: 1px; }
.dark img.result-icon { background: var(--icon-plate); }
.favicon-fallback {
    width: 18px; height: 18px; border-radius: 4px; flex: none;
    display: inline-flex; align-items: center; justify-content: center;
    font-size: 11px; font-weight: 700; color: #fff; text-transform: uppercase;
}

.empty-feeds { padding: 10px; font-size: 13px; color: var(--text-3); }

/* ---------- Main ---------- */

.main { flex: 1; min-width: 0; overflow-y: auto; overscroll-behavior-y: contain; -webkit-overflow-scrolling: touch; }
.main:focus { outline: none; }
.content { max-width: 780px; margin: 0 auto; padding: 16px 16px 64px; }

.ptr {
    height: 0; overflow: hidden; display: flex; align-items: center; justify-content: center; gap: 8px;
    color: var(--text-3); font-size: 13px; font-weight: 600; transition: height 0.2s ease;
}
.ptr.visible { height: 52px; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.toolbar .search-wrap { position: relative; flex: 1 1 220px; }
.toolbar .search-wrap svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--text-3); }
.toolbar .search-wrap .input { padding-left: 32px; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.segmented button { border: 0; background: transparent; padding: 7px 12px; font-size: 13px; font-weight: 600; color: var(--text-2); }
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent-text); }

.status-line { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-3); margin: 0 2px 12px; min-height: 18px; }

.banner {
    border: 1px solid var(--danger); background: var(--danger-soft); color: var(--danger);
    border-radius: var(--radius); padding: 10px 14px; margin-bottom: 14px; font-size: 14px;
}

.article {
    position: relative;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 16px 18px; margin-bottom: 12px;
}
.article.read { background: var(--surface-2); }
.article.read .article-title { color: var(--text-3); font-weight: 600; }
.article.selected { border-color: var(--link); box-shadow: 0 0 0 1px var(--link); }
.article-meta { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-3); margin-bottom: 6px; min-width: 0; }
.article-source {
    display: inline-flex; align-items: center; gap: 6px; min-width: 0;
    font-weight: 700; color: var(--link); text-transform: uppercase; letter-spacing: 0.04em;
    border: 0; background: none; padding: 0; font-size: 12px;
}
.article-source span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.article-source:hover span { text-decoration: underline; }
.article-meta time { white-space: nowrap; flex: none; }
.article-meta .article-author { min-width: 0; flex: 0 100 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.article-meta .article-source { flex: 0 1 auto; min-width: 5em; }
.unread-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--link); flex: none; }
.article-body { display: flex; gap: 14px; }
.article-text { flex: 1; min-width: 0; }
.article-title { margin: 0 0 6px; font-size: 18px; line-height: 1.35; font-weight: 700; overflow-wrap: anywhere; }
.article-title a { text-decoration: none; }
.article-title a:hover { color: var(--link); }
.article-summary { margin: 0; color: var(--text-2); font-size: 14px; overflow-wrap: anywhere; }
.article-thumb {
    width: 112px; height: 84px; border-radius: 8px; object-fit: cover; flex: none;
    background: var(--hover); align-self: flex-start;
}
@media (max-width: 520px) {
    .article-thumb { width: 84px; height: 64px; }
    .article-title { font-size: 16px; }
}
.article-audio { width: 100%; margin-top: 10px; height: 36px; }
.article-footer { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 10px; }
.article-actions { display: flex; gap: 2px; margin-left: -8px; }
.article-actions .icon-btn.on { color: var(--star); }
.read-link { font-size: 14px; font-weight: 600; color: var(--link); text-decoration: none; white-space: nowrap; }
.read-link:hover { text-decoration: underline; }

.empty-state { text-align: center; padding: 56px 16px; color: var(--text-3); }
.empty-state h2 { color: var(--text); font-size: 18px; margin: 0 0 6px; }
.empty-state p { margin: 0 0 18px; }
.empty-state .actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.show-more { display: flex; justify-content: center; margin: 8px 0 24px; }

.skeleton { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 12px; }
.skeleton div { height: 12px; border-radius: 6px; background: var(--hover); margin-bottom: 10px; animation: pulse 1.4s ease-in-out infinite; }
.skeleton div:nth-child(1) { width: 30%; }
.skeleton div:nth-child(2) { width: 85%; height: 16px; }
.skeleton div:nth-child(3) { width: 95%; }
.skeleton div:nth-child(4) { width: 60%; margin-bottom: 0; }

/* ---------- Dialogs ---------- */

dialog {
    border: 1px solid var(--border); border-radius: 14px; padding: 0;
    background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg);
    width: min(440px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
}
dialog.wide { width: min(640px, calc(100vw - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / 0.55); backdrop-filter: blur(2px); }
dialog[open] { display: flex; flex-direction: column; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 10px 20px; flex: none; }
.dialog-head h2 { margin: 0; font-size: 18px; }
.dialog-body { padding: 4px 20px 20px; overflow-y: auto; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.settings-section + .settings-section { border-top: 1px solid var(--border); margin-top: 18px; padding-top: 18px; }
.settings-section h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-3); }
.settings-section p { font-size: 13px; color: var(--text-3); margin: 6px 0 0; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.shortcuts { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 13px; margin: 0; }
.shortcuts dt { white-space: nowrap; }
.shortcuts dd { margin: 0; color: var(--text-2); }
kbd {
    display: inline-block; min-width: 22px; padding: 1px 6px; text-align: center;
    border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 5px;
    font: 600 12px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2);
}

/* Discover */
.discover-search { position: sticky; top: 0; background: var(--surface); padding: 0 20px 12px; flex: none; }
.discover-search form { display: flex; gap: 8px; }
.discover-hint { font-size: 13px; color: var(--text-3); margin: 8px 0 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.chip {
    border: 1px solid var(--border); background: var(--surface-2); color: var(--text-2);
    border-radius: 999px; padding: 4px 12px; font-size: 13px; font-weight: 600;
}
.chip:hover { border-color: var(--link); color: var(--link); }
.results { list-style: none; margin: 0; padding: 0; }
.result { display: flex; gap: 12px; align-items: flex-start; padding: 12px 0; border-top: 1px solid var(--border); }
.result:first-child { border-top: 0; }
.result-icon { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; flex: none; background: var(--hover); }
.result-icon.favicon-fallback { width: 44px; height: 44px; font-size: 18px; }
.result-text { flex: 1; min-width: 0; }
.result-title { font-weight: 700; margin: 0; overflow-wrap: anywhere; }
.result-title a { text-decoration: none; }
.result-title a:hover { text-decoration: underline; }
.result-desc { font-size: 13px; color: var(--text-2); margin: 2px 0 4px; display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.result-meta { font-size: 12px; color: var(--text-3); display: flex; flex-wrap: wrap; gap: 4px 10px; }
.badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 0 6px; border-radius: 4px; background: var(--accent-soft); color: var(--accent-text); text-transform: uppercase; letter-spacing: 0.04em; }
.results-status { padding: 24px 0; text-align: center; color: var(--text-3); font-size: 14px; display: flex; gap: 8px; justify-content: center; align-items: center; }

/* Toasts */
.toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; width: max-content; max-width: calc(100vw - 32px); }
.toast {
    background: #111827; color: #f9fafb; padding: 10px 16px; border-radius: 10px; font-size: 14px;
    box-shadow: var(--shadow-lg); animation: toast-in 0.2s ease; pointer-events: auto;
    display: flex; align-items: center; gap: 12px;
}
.dark .toast { background: #f2f2f2; color: #0a0a0a; }
.dark .toast.error { background: var(--danger); color: #0a0a0a; }
.toast.error { background: var(--danger); color: #fff; }
.toast button { border: 0; background: none; color: inherit; font-weight: 700; text-decoration: underline; padding: 0; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

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