:root {
    color-scheme: light dark;
    --app-bg: var(--tg-theme-secondary-bg-color, #e7ebf0);
    --surface: var(--tg-theme-bg-color, #ffffff);
    --text: var(--tg-theme-text-color, #17212b);
    --muted: var(--tg-theme-hint-color, #708499);
    --link: var(--tg-theme-link-color, #2481cc);
    --button: var(--tg-theme-button-color, #2481cc);
    --button-text: var(--tg-theme-button-text-color, #ffffff);
    --surface-muted: var(--surface);
    --secondary-text: var(--text);
    --border: var(--muted);
    --header-bg: var(--surface);
    --focus-border: var(--button);
    --focus-ring: transparent;
    --error-border: #d94b4b;
    --reaction-bg: transparent;
    --service-bg: #52697f;
    --shadow: 0 1px 2px rgba(15, 23, 42, 0.08), 0 8px 24px rgba(15, 23, 42, 0.05);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@supports (color: color-mix(in srgb, black, white)) {
    :root {
        --surface-muted: color-mix(in srgb, var(--surface) 94%, #2481cc 6%);
        --secondary-text: color-mix(in srgb, var(--text) 76%, var(--muted) 24%);
        --border: color-mix(in srgb, var(--muted) 24%, transparent);
        --header-bg: color-mix(in srgb, var(--surface) 96%, transparent);
        --focus-border: color-mix(in srgb, var(--button) 68%, transparent);
        --focus-ring: color-mix(in srgb, var(--button) 14%, transparent);
        --error-border: color-mix(in srgb, #d94b4b 38%, var(--border));
        --reaction-bg: color-mix(in srgb, var(--muted) 10%, transparent);
    }
}

* {
    box-sizing: border-box;
}

html {
    min-height: 100%;
    background: var(--app-bg);
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    min-width: 320px;
    min-height: 100vh;
    min-height: 100dvh;
    min-height: var(--tg-viewport-stable-height, 100dvh);
    margin: 0;
    background: var(--app-bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

button,
input {
    font: inherit;
}

button,
a {
    -webkit-tap-highlight-color: transparent;
}

button {
    color: inherit;
}

.app-shell {
    width: min(100%, 820px);
    min-height: 100vh;
    min-height: 100dvh;
    min-height: var(--tg-viewport-stable-height, 100dvh);
    margin: 0 auto;
}

.app-header {
    position: sticky;
    top: 0;
    z-index: 10;
    padding: calc(12px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right)) 12px calc(14px + env(safe-area-inset-left));
    background: var(--header-bg);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
}

.channel-row {
    display: flex;
    align-items: center;
    gap: 11px;
    margin-bottom: 12px;
}

.channel-avatar {
    width: 44px;
    height: 44px;
    flex: 0 0 auto;
    border-radius: 50%;
    object-fit: cover;
}

.channel-copy {
    min-width: 0;
    flex: 1;
}

.channel-copy h1,
.channel-copy p {
    overflow: hidden;
    margin: 0;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.channel-copy h1 {
    font-size: 16px;
    line-height: 1.25;
}

.channel-copy p {
    margin-top: 2px;
    color: var(--muted);
    font-size: 13px;
}

.search-form {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
}

.search-form input {
    min-width: 0;
    height: 46px;
    padding: 0 15px;
    border: 1px solid transparent;
    border-radius: 13px;
    outline: none;
    background: var(--surface-muted);
    color: var(--text);
    font-size: 16px;
}

.search-form input::placeholder {
    color: var(--muted);
}

.search-form input:focus {
    border-color: var(--focus-border);
    box-shadow: 0 0 0 3px var(--focus-ring);
}

.search-button,
.retry-button,
.load-more-button,
.feed-retry-button,
.examples button {
    min-height: 44px;
    border: 0;
    cursor: pointer;
}

.search-button {
    min-width: 76px;
    height: 46px;
    padding: 0 16px;
    border-radius: 13px;
    background: var(--button);
    color: var(--button-text);
    font-weight: 700;
}

.search-button:active,
.retry-button:active,
.load-more-button:active,
.feed-retry-button:active,
.examples button:active {
    transform: translateY(1px);
}

.search-button:disabled,
.retry-button:disabled,
.load-more-button:disabled,
.feed-retry-button:disabled {
    cursor: default;
    opacity: 0.45;
    transform: none;
}

.examples {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 6px;
    overflow-x: auto;
    color: var(--muted);
    font-size: 12px;
    scrollbar-width: none;
}

.examples::-webkit-scrollbar {
    display: none;
}

.examples > span,
.examples button {
    flex: 0 0 auto;
}

.examples button {
    padding: 0 10px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: transparent;
    color: var(--link);
}

.search-content {
    min-height: calc(100vh - 190px);
    min-height: calc(100dvh - 190px);
    min-height: calc(var(--tg-viewport-stable-height, 100dvh) - 190px);
    scroll-margin-top: calc(196px + env(safe-area-inset-top));
    padding: 18px calc(14px + env(safe-area-inset-right)) 30px calc(14px + env(safe-area-inset-left));
}

.welcome-state,
.results-state,
.loading-state,
.error-state {
    width: min(100%, 720px);
    margin: 0 auto;
}

.service-message {
    width: fit-content;
    max-width: 92%;
    margin: 0 auto 16px;
    padding: 7px 11px;
    border-radius: 999px;
    background: var(--service-bg);
    color: #ffffff;
    font-size: 12px;
    line-height: 1.35;
    text-align: center;
}

.loading-message {
    display: flex;
    align-items: center;
    gap: 7px;
}

.loading-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: currentColor;
    animation: loading-pulse 1.1s ease-in-out infinite;
}

@keyframes loading-pulse {
    0%, 100% { opacity: 0.35; transform: scale(0.8); }
    50% { opacity: 1; transform: scale(1); }
}

.message-card {
    width: min(100%, 650px);
    margin: 0 0 10px;
    padding: 12px 13px 8px;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.message-card--intro,
.message-card--error {
    margin-right: auto;
    margin-left: auto;
    padding-bottom: 13px;
}

.message-card--error {
    border-color: var(--error-border);
}

.message-card p {
    margin: 0;
}

.message-author {
    color: var(--link);
    font-size: 13px;
    font-weight: 750;
}

.message-card--intro > p:nth-child(2),
.message-card--error > p:nth-child(2) {
    margin-top: 6px;
    font-size: 15px;
    line-height: 1.45;
}

.retry-button {
    margin-top: 12px;
    padding: 0 16px;
    border-radius: 12px;
    background: var(--button);
    color: var(--button-text);
    font-weight: 700;
}

.results-heading {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 16px;
    margin: 0 2px 13px;
}

.eyebrow,
.results-heading h2,
.results-range {
    margin: 0;
}

.eyebrow {
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.results-heading h2 {
    margin-top: 3px;
    overflow-wrap: anywhere;
    font-size: 18px;
    line-height: 1.25;
    outline: none;
}

.results-range {
    flex: 0 0 auto;
    color: var(--muted);
    font-size: 12px;
}

.empty-state {
    width: min(100%, 650px);
    margin: 0 auto;
    padding: 22px 18px;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--surface);
    color: var(--muted);
    text-align: center;
    box-shadow: var(--shadow);
}

.empty-state p {
    margin: 4px 0 0;
    font-size: 14px;
    line-height: 1.45;
}

.empty-state .empty-state__title {
    margin: 0;
    color: var(--text);
    font-size: 16px;
    font-weight: 700;
}

.results-list {
    display: flex;
    flex-direction: column;
}

.message-topline,
.message-footer,
.message-meta {
    display: flex;
    align-items: center;
}

.message-topline,
.message-footer {
    justify-content: space-between;
    gap: 12px;
}

.relevance-label {
    color: var(--secondary-text);
    font-size: 12px;
    white-space: nowrap;
}

.message-text {
    margin-top: 6px !important;
    overflow-wrap: anywhere;
    font-size: 15px;
    line-height: 1.45;
    white-space: pre-wrap;
}

.excerpt-note {
    margin-top: 7px !important;
    color: var(--secondary-text);
    font-size: 12px;
}

.message-footer {
    align-items: end;
    margin-top: 3px;
}

.message-meta {
    min-width: 0;
    flex-wrap: wrap;
    gap: 3px 8px;
    color: var(--secondary-text);
    font-size: 12px;
}

.reaction-count {
    padding: 2px 6px;
    border-radius: 999px;
    background: var(--reaction-bg);
    white-space: nowrap;
}

.message-link {
    display: inline-flex;
    min-height: 44px;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    padding-left: 12px;
    color: var(--link);
    font-size: 12px;
    font-weight: 650;
    text-decoration: none;
}

.message-link:focus-visible,
.message-link:hover {
    text-decoration: underline;
}

.feed-state {
    display: flex;
    width: min(100%, 650px);
    flex-direction: column;
    align-items: center;
    gap: 8px;
    margin-right: auto;
    margin-top: 18px;
    margin-left: auto;
}

.load-more-button,
.feed-retry-button {
    min-height: 44px;
    padding: 0 14px;
    border: 0;
    border-radius: 12px;
    background: var(--surface);
    color: var(--link);
    font-weight: 700;
    box-shadow: var(--shadow);
}

.load-more-button {
    width: min(100%, 260px);
}

.feed-loading {
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 7px 11px;
    border-radius: 999px;
    background: var(--service-bg);
    color: #ffffff;
    font-size: 12px;
    line-height: 1.35;
    outline: none;
}

.feed-error {
    width: min(100%, 520px);
    padding: 13px;
    border: 1px solid var(--error-border);
    border-radius: 14px;
    background: var(--surface);
    text-align: center;
    box-shadow: var(--shadow);
}

.feed-error p,
.feed-end {
    margin: 0;
    font-size: 13px;
    line-height: 1.4;
}

.feed-retry-button {
    margin-top: 10px;
}

.feed-end {
    width: fit-content;
    max-width: 92%;
    padding: 7px 11px;
    border-radius: 999px;
    background: var(--service-bg);
    color: #ffffff;
    text-align: center;
    outline: none;
}

.scroll-sentinel {
    width: 100%;
    height: 1px;
    margin-top: 1px;
}

.app-footer {
    padding: 0 calc(16px + env(safe-area-inset-right)) calc(18px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
    color: var(--muted);
    font-size: 11px;
    line-height: 1.4;
    text-align: center;
}

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

[hidden] {
    display: none !important;
}

@media (max-width: 420px) {
    .message-footer {
        gap: 5px;
    }

    .message-meta {
        padding-bottom: 7px;
    }
}

@media (min-width: 720px) {
    .app-header {
        padding-right: 22px;
        padding-left: 22px;
        border-right: 1px solid var(--border);
        border-left: 1px solid var(--border);
        border-radius: 0 0 18px 18px;
    }

    .search-content {
        padding-top: 28px;
    }
}

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