/*
 * KM Market — Consolidated UX/UI Polish v0.1
 *
 * Presentation-only overlay. It intentionally does not change:
 * API semantics, market identity, signal semantics, synthesis policy,
 * multilingual routing or research/runtime contracts.
 */

:root {
    --km-radius-xs: 6px;
    --km-radius-sm: 8px;
    --km-radius-md: 10px;
    --km-radius-lg: 12px;

    --km-control-height: 36px;
    --km-tap-target: 40px;

    --km-focus-ring: 0 0 0 3px rgba(41, 191, 201, .16);
    --km-card-shadow:
        inset 0 1px 0 rgba(255, 255, 255, .018),
        0 12px 34px rgba(0, 0, 0, .08);

    --km-accent-soft: rgba(41, 191, 201, .075);
    --km-accent-border: rgba(41, 191, 201, .24);
    --km-hover-soft: rgba(255, 255, 255, .022);
}

/* -----------------------------------------------------------------
 * Global readability / interaction
 * ----------------------------------------------------------------- */

html {
    scrollbar-gutter: stable;
    text-size-adjust: 100%;
}

body {
    font-kerning: normal;
    font-variant-ligatures: contextual common-ligatures;
    text-rendering: optimizeLegibility;
}

::selection {
    background: rgba(41, 191, 201, .22);
    color: var(--km-text);
}

:where(
    a,
    button,
    input,
    select,
    summary,
    [tabindex]:not([tabindex="-1"])
):focus-visible {
    outline: 2px solid var(--km-primary);
    outline-offset: 2px;
    box-shadow: var(--km-focus-ring);
}

:where(button, summary, a) {
    -webkit-tap-highlight-color: transparent;
}

:where(
    .km-card,
    .km-panel,
    .km-metric-group,
    .km-data-card,
    .km-markets-summary-card,
    .km-markets-table-wrap,
    .km-sig-table-wrap,
    .km-research-table-wrap,
    .km-micro-table-wrap
) {
    box-shadow: var(--km-card-shadow);
}

:where(
    .km-overview-market-card,
    .km-market-heat-tile,
    .km-learn-topic-card,
    .km-card-action,
    .km-primary-action,
    .km-text-link
) {
    transition:
        border-color .16s ease,
        background-color .16s ease,
        color .16s ease,
        transform .16s ease;
}

:where(
    .km-overview-market-card,
    .km-market-heat-tile,
    .km-learn-topic-card
):hover {
    border-color: var(--km-accent-border);
}

/* -----------------------------------------------------------------
 * Header, primary navigation, language switcher
 * ----------------------------------------------------------------- */

.km-header__inner {
    gap: 18px;
}

.km-brand {
    flex: 0 0 auto;
}

.km-nav {
    min-width: 0;
    flex: 1 1 auto;
    gap: 2px;
}

.km-nav a {
    position: relative;
    flex: 0 0 auto;
    padding: 8px 8px;
    white-space: nowrap;
    font-size: 12px;
}

.km-nav a[aria-current="page"] {
    background: var(--km-accent-soft);
    color: #a2e3e7;
}

.km-nav a[aria-current="page"]::after {
    position: absolute;
    right: 9px;
    bottom: 3px;
    left: 9px;
    height: 1px;
    border-radius: 999px;
    background: var(--km-primary);
    content: "";
    opacity: .72;
}

.km-header__actions {
    flex: 0 0 auto;
}

.km-menu-button {
    min-width: 38px;
}

.km-mobile-menu {
    background: rgba(11, 17, 24, .985);
    box-shadow: 0 18px 38px rgba(0, 0, 0, .18);
}

.km-mobile-menu a[aria-current="page"] {
    color: #a2e3e7;
}

.km-language-switcher > summary {
    min-height: var(--km-control-height);
}

.km-language-switcher__menu {
    border-color: var(--km-border-strong);
    box-shadow: 0 18px 48px rgba(0, 0, 0, .38);
}

.km-language-switcher__menu a {
    min-height: 39px;
}

/* -----------------------------------------------------------------
 * Shared hero hierarchy
 * ----------------------------------------------------------------- */

:is(
    .km-overview-hero,
    .km-markets-hero,
    .km-signals-hero,
    .km-intelligence-hero,
    .km-research-hero,
    .km-learn-hero,
    .km-micro-hero
) {
    padding-top: clamp(36px, 4vw, 52px);
}

:is(
    .km-overview-hero h1,
    .km-markets-title-row h1,
    .km-signals-hero h1,
    .km-intelligence-hero h1,
    .km-research-hero h1,
    .km-learn-hero h1,
    .km-micro-hero h1
) {
    max-width: 980px;
    text-wrap: balance;
}

:is(
    .km-overview-hero__top,
    .km-markets-title-row,
    .km-signals-hero__row,
    .km-intelligence-hero__row,
    .km-research-hero__row,
    .km-learn-hero__row,
    .km-micro-hero__row
) {
    gap: 24px;
}

:is(
    .km-signals-contract,
    .km-intelligence-contract,
    .km-research-contract,
    .km-learn-contract,
    .km-micro-contract,
    .km-overview-hero__status,
    .km-markets-count
) {
    border-color: var(--km-border-strong);
    background:
        linear-gradient(
            180deg,
            rgba(255, 255, 255, .018),
            transparent 60%
        ),
        rgba(17, 25, 35, .82);
    box-shadow: var(--km-card-shadow);
}

:is(
    .km-signals-contract small,
    .km-intelligence-contract small,
    .km-research-contract small,
    .km-learn-contract small,
    .km-micro-contract small
) {
    font-size: 10px;
    line-height: 1.5;
}

/* -----------------------------------------------------------------
 * Unified product controls
 * ----------------------------------------------------------------- */

:is(
    .km-market-controls,
    .km-signals-global-controls,
    .km-intelligence-controls,
    .km-research-controls,
    .km-micro-controls
) {
    border-color: var(--km-border-strong);
    background:
        linear-gradient(
            180deg,
            rgba(255, 255, 255, .012),
            transparent
        ),
        var(--km-surface);
    box-shadow: var(--km-card-shadow);
}

:is(
    .km-market-controls,
    .km-signals-global-controls,
    .km-research-controls,
    .km-micro-controls
) {
    gap: 12px 16px;
}

:is(
    .km-signals-select-control select,
    .km-research-asset-picker select,
    .km-intelligence-asset-picker select,
    .km-micro-select-control select,
    .km-markets-screener-controls select,
    .km-markets-screener-controls input,
    .km-learn-search input
) {
    min-height: var(--km-control-height);
    border-color: var(--km-border-strong);
    border-radius: var(--km-radius-sm);
    background-color: var(--km-surface-raised);
}

:is(
    .km-signals-select-control select,
    .km-research-asset-picker select,
    .km-intelligence-asset-picker select,
    .km-micro-select-control select,
    .km-markets-screener-controls select,
    .km-markets-screener-controls input,
    .km-learn-search input
):hover {
    border-color: rgba(255, 255, 255, .16);
}

:is(
    .km-control-group button,
    .km-signals-tabs button,
    .km-research-tabs button,
    .km-micro-tabs button,
    .km-micro-choice button,
    .km-learn-tabs button,
    .km-explain-tabs button,
    .km-markets-view-tabs button,
    .km-intelligence-detail-button
) {
    min-height: 34px;
    touch-action: manipulation;
}

:is(
    .km-signals-tabs,
    .km-research-tabs,
    .km-micro-tabs,
    .km-learn-tabs,
    .km-explain-tabs,
    .km-markets-view-tabs
) {
    scrollbar-width: thin;
    overscroll-behavior-inline: contain;
    scroll-snap-type: x proximity;
}

:is(
    .km-signals-tabs,
    .km-research-tabs,
    .km-micro-tabs,
    .km-learn-tabs,
    .km-explain-tabs,
    .km-markets-view-tabs
) button {
    scroll-snap-align: nearest;
    white-space: nowrap;
}

:is(
    .km-signals-tabs button,
    .km-research-tabs button,
    .km-micro-tabs button,
    .km-learn-tabs button
) {
    font-size: 10px;
}

:is(
    .km-signals-statusbar,
    .km-research-status,
    .km-micro-statusbar,
    .km-learn-statusbar
) {
    font-size: 10px;
    line-height: 1.45;
}

/* -----------------------------------------------------------------
 * Cards, grids, tables
 * ----------------------------------------------------------------- */

:is(
    .km-overview-state-grid,
    .km-overview-market-grid,
    .km-markets-summary-grid,
    .km-signals-safety-grid,
    .km-intelligence-tf-grid,
    .km-intelligence-regime-grid,
    .km-intelligence-context-grid,
    .km-intelligence-overlay-grid,
    .km-research-current-grid,
    .km-research-engine-grid,
    .km-micro-metrics-grid,
    .km-micro-safety-grid,
    .km-learn-topic-grid,
    .km-learn-protocol,
    .km-explain-context-grid
) {
    gap: 10px;
}

:is(
    .km-markets-table-wrap,
    .km-sig-table-wrap,
    .km-research-table-wrap,
    .km-micro-table-wrap
) {
    overflow-x: auto;
    border-color: var(--km-border-strong);
    scrollbar-width: thin;
}

:is(
    .km-markets-table,
    .km-sig-table,
    .km-research-table,
    .km-micro-table
) {
    font-variant-numeric: tabular-nums;
}

:is(
    .km-markets-table,
    .km-sig-table,
    .km-research-table,
    .km-micro-table
) th {
    position: sticky;
    top: 0;
    z-index: 2;
    background: #111923;
    color: var(--km-text-subtle);
    font-size: 9px;
    backdrop-filter: blur(10px);
}

:is(
    .km-markets-table,
    .km-sig-table,
    .km-research-table,
    .km-micro-table
) tbody tr {
    transition: background-color .12s ease;
}

:is(
    .km-markets-table,
    .km-sig-table,
    .km-research-table,
    .km-micro-table
) tbody tr:hover {
    background: var(--km-hover-soft);
}

/* -----------------------------------------------------------------
 * Loading / empty / unavailable / error states
 * ----------------------------------------------------------------- */

:is(
    .km-ui-message,
    .km-load-status,
    .km-explain-loading,
    .km-explain-unavailable,
    [data-km-ui-state]
) {
    border-radius: var(--km-radius-md);
}

.km-ui-message {
    min-height: 112px;
}

.km-ui-message strong {
    font-size: 12px;
}

.km-ui-message span {
    font-size: 11px;
}

.km-ui-message small {
    font-size: 10px;
}

.km-skeleton-card span,
.km-skeleton-card strong,
.km-skeleton-card small {
    position: relative;
    overflow: hidden;
}

.km-skeleton-card span::after,
.km-skeleton-card strong::after,
.km-skeleton-card small::after {
    position: absolute;
    inset: 0;
    transform: translateX(-100%);
    background:
        linear-gradient(
            90deg,
            transparent,
            rgba(255, 255, 255, .05),
            transparent
        );
    content: "";
    animation: km-polish-shimmer 1.4s ease-in-out infinite;
}

@keyframes km-polish-shimmer {
    to {
        transform: translateX(100%);
    }
}

/* -----------------------------------------------------------------
 * Explain layer
 * ----------------------------------------------------------------- */

.km-explain-launch {
    border-color: var(--km-accent-border);
    background: rgba(17, 25, 35, .97);
    box-shadow:
        0 14px 38px rgba(0, 0, 0, .34),
        inset 0 1px 0 rgba(255, 255, 255, .025);
}

.km-explain-launch:hover {
    transform: translateY(-1px);
}

.km-explain-drawer {
    height: 100dvh;
}

.km-explain-body {
    scrollbar-width: thin;
}

/* -----------------------------------------------------------------
 * Asset workspace
 * ----------------------------------------------------------------- */

.km-dashboard-shell {
    padding-bottom: 24px;
}

.km-context-bar {
    border-color: var(--km-border-strong);
    box-shadow: var(--km-card-shadow);
}

.km-panel,
.km-metric-group,
.km-data-card {
    border-color: var(--km-border-strong);
}

.km-state-row,
.km-metric-group dl > div {
    min-height: 38px;
}

.km-chart {
    min-height: clamp(300px, 42vw, 560px);
}

/* -----------------------------------------------------------------
 * Localized-copy resilience
 * ----------------------------------------------------------------- */

:where(
    h1,
    h2,
    h3,
    .km-status-pill,
    .km-contract-list,
    .km-signals-contract,
    .km-intelligence-contract,
    .km-research-contract,
    .km-learn-contract,
    .km-micro-contract,
    .km-context-summary,
    .km-markets-context-rail,
    .km-markets-summary-card,
    .km-driver-chip
) {
    overflow-wrap: anywhere;
}

:where(
    html[lang^="de"],
    html[lang^="fr"],
    html[lang^="it"],
    html[lang^="es"],
    html[lang^="ru"]
) :where(h1, h2, h3, p, small, strong) {
    hyphens: auto;
}

html[lang^="zh"] :where(h1, h2, h3, p, small, strong) {
    word-break: normal;
    line-break: strict;
}

/* -----------------------------------------------------------------
 * Mobile bottom navigation
 * ----------------------------------------------------------------- */

.km-bottom-nav a[aria-current="page"] {
    background: var(--km-accent-soft);
    color: #a2e3e7;
}

.km-bottom-nav button[aria-expanded="true"] {
    color: #a2e3e7;
}

.km-bottom-nav a,
.km-bottom-nav button {
    min-width: 0;
    touch-action: manipulation;
}

.km-bottom-nav small {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* -----------------------------------------------------------------
 * Responsive consolidation
 * ----------------------------------------------------------------- */

@media (max-width: 1180px) {
    .km-nav {
        display: none;
    }

    .km-menu-button {
        display: block;
    }

    .km-mobile-menu:not([hidden]) {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 2px 18px;
        max-height: calc(100dvh - 66px);
        overflow-y: auto;
    }

    .km-mobile-menu a {
        min-width: 0;
        overflow-wrap: anywhere;
    }
}

@media (max-width: 900px) {
    :is(
        .km-overview-hero__top,
        .km-markets-title-row,
        .km-signals-hero__row,
        .km-intelligence-hero__row,
        .km-research-hero__row,
        .km-learn-hero__row,
        .km-micro-hero__row
    ) {
        align-items: stretch;
        flex-direction: column;
    }

    :is(
        .km-signals-contract,
        .km-intelligence-contract,
        .km-research-contract,
        .km-learn-contract,
        .km-micro-contract,
        .km-overview-hero__status,
        .km-markets-count
    ) {
        width: 100%;
        min-width: 0;
    }

    .km-mobile-menu:not([hidden]) {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 760px) {
    .km-shell {
        width: min(calc(100% - 28px), var(--km-shell));
    }

    :is(
        .km-market-controls,
        .km-signals-global-controls,
        .km-research-controls,
        .km-micro-controls
    ) {
        display: grid;
        gap: 8px;
    }

    :is(
        .km-signals-context-links,
        .km-micro-context-links,
        .km-learn-live-links
    ) {
        width: 100%;
        margin-left: 0;
    }

    :is(
        .km-signals-select-control,
        .km-micro-select-control,
        .km-micro-choice
    ) {
        justify-content: space-between;
        min-width: 0;
    }

    :is(
        .km-signals-select-control select,
        .km-micro-select-control select
    ) {
        max-width: 60%;
    }

    :is(
        .km-signals-statusbar,
        .km-research-status,
        .km-micro-statusbar,
        .km-learn-statusbar
    ) {
        align-items: flex-start;
        flex-direction: column;
        gap: 4px;
    }

    :is(
        .km-markets-table,
        .km-sig-table,
        .km-research-table,
        .km-micro-table
    ) th,
    :is(
        .km-markets-table,
        .km-sig-table,
        .km-research-table,
        .km-micro-table
    ) td {
        padding-right: 11px;
        padding-left: 11px;
    }

    .km-explain-launch {
        right: 12px;
        bottom: 78px;
    }

    .km-explain-drawer {
        width: 100%;
    }

    .km-footer__inner {
        align-items: flex-start;
        flex-direction: column;
        justify-content: center;
        gap: 10px;
        padding-block: 26px;
    }

    .km-mobile-menu:not([hidden]) {
        grid-template-columns: 1fr;
        max-height: calc(100dvh - 58px);
    }
}

@media (max-width: 560px) {
    :is(
        .km-overview-hero h1,
        .km-markets-title-row h1,
        .km-signals-hero h1,
        .km-intelligence-hero h1,
        .km-research-hero h1,
        .km-learn-hero h1,
        .km-micro-hero h1
    ) {
        font-size: clamp(32px, 10vw, 44px);
        line-height: 1.02;
    }

    .km-section {
        padding-top: 26px;
        padding-bottom: 26px;
    }

    .km-markets-context-rail {
        align-items: flex-start;
        flex-direction: column;
        gap: 10px;
    }

    .km-explain-context-grid {
        grid-template-columns: 1fr;
    }
}

@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }

    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
    }
}
