:root {
    color-scheme: light;
    --ink: #22211f;
    --muted: #67615a;
    --line: #d8d2c8;
    --panel: #ffffff;
    --wash: #f4f1eb;
    --field: #ffffff;
    --raised: #fbfaf7;
    --soft: #eef6f4;
    --soft-strong: #f7fbfa;
    --warning: #fff6df;
    --notice: #fff1ed;
    --notice-line: #c68271;
    --hover: #ebe5da;
    --text-on-accent: #ffffff;
    --accent: #2f6f73;
    --accent-strong: #1f4f54;
    --good: #24794f;
    --bad: #a3403f;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --ink: #f2efe8;
    --muted: #b9b2a8;
    --line: #3f4846;
    --panel: #171d1b;
    --wash: #0d1110;
    --field: #101513;
    --raised: #1f2623;
    --soft: #17302f;
    --soft-strong: #112321;
    --warning: #332811;
    --notice: #321d19;
    --notice-line: #a96e60;
    --hover: #26312e;
    --text-on-accent: #f8fffd;
    --accent: #54a7a4;
    --accent-strong: #89ceca;
    --good: #74c69d;
    --bad: #e07a76;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--ink);
    background: var(--wash);
}

button,
input {
    font: inherit;
}

button {
    border: 0;
    border-radius: 6px;
    padding: 0.75rem 1rem;
    color: var(--text-on-accent);
    background: var(--accent);
    cursor: pointer;
}

a {
    color: inherit;
}

button:hover {
    background: var(--accent-strong);
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.48;
}

.button-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 43px;
    border-radius: 6px;
    padding: 0.7rem 1rem;
    color: var(--text-on-accent);
    background: var(--accent);
    text-decoration: none;
}

.icon-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 43px;
    height: 43px;
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 0;
    color: var(--ink);
    background: transparent;
    font-size: 1.2rem;
    line-height: 1;
    text-decoration: none;
}

.icon-button:hover {
    background: var(--hover);
}

.secondary-link {
    color: var(--ink);
    border: 1px solid var(--line);
    background: transparent;
}

button.secondary {
    color: var(--ink);
    background: transparent;
    border: 1px solid var(--line);
}

button.secondary:hover {
    background: var(--hover);
}

input {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 0.75rem;
    color: var(--ink);
    background: var(--field);
}

select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 0.65rem;
    color: var(--ink);
    background: var(--field);
    font: inherit;
}

label {
    display: grid;
    gap: 0.4rem;
    font-weight: 700;
}

h1,
h2,
p {
    margin-top: 0;
}

h1 {
    max-width: 14ch;
    font-size: clamp(2.2rem, 7vw, 5rem);
    line-height: 0.95;
}

h2 {
    font-size: 1rem;
}

.app-shell {
    width: min(1360px, calc(100% - 32px));
    margin: 0 auto;
    padding: 20px 0;
}

.notice {
    margin-bottom: 16px;
    border: 1px solid var(--notice-line);
    border-radius: 6px;
    padding: 0.8rem 1rem;
    background: var(--notice);
}

.auth-grid {
    min-height: calc(100vh - 64px);
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(320px, 420px);
    gap: 32px;
    align-items: center;
}

.identity-panel p {
    max-width: 56ch;
    color: var(--muted);
    font-size: 1.1rem;
}

.eyebrow {
    margin-bottom: 0.75rem;
    color: var(--accent-strong);
    font-size: 0.78rem;
    font-weight: 800;
    text-transform: uppercase;
}

.form-stack,
.single-column {
    display: grid;
    gap: 16px;
}

.panel,
.stats-panel,
.feed-panel,
.influence-panel,
.threads-panel {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 16px;
    background: var(--panel);
}

.panel {
    display: grid;
    gap: 16px;
}

.topbar {
    display: flex;
    gap: 24px;
    align-items: flex-start;
    justify-content: space-between;
}

.topbar-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.topbar-actions-left {
    justify-content: flex-start;
}

.single-column {
    max-width: 720px;
    margin: 10vh auto 0;
}

.summary-page,
.detail-page {
    width: min(1120px, 100%);
    max-width: none;
    margin: 24px auto 0;
    grid-template-columns: minmax(0, 1fr);
}

.detail-page {
    gap: 18px;
}

.summary-page > *,
.detail-page > * {
    grid-area: auto;
    width: 100%;
}

.detail-page > .topbar,
.summary-page > .topbar {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 18px;
    background: var(--panel);
}

.detail-page > .topbar {
    display: grid;
    grid-template-columns: 1fr;
}

.detail-page > .topbar .button-link,
.summary-page > .topbar .button-link {
    width: fit-content;
}

.detail-page h1,
.summary-page h1 {
    max-width: 24ch;
    font-size: clamp(2rem, 4.5vw, 4rem);
    line-height: 1;
}

.detail-page .topbar p:not(.eyebrow),
.summary-page .topbar p:not(.eyebrow) {
    max-width: 72ch;
    color: var(--muted);
    line-height: 1.5;
}

.detail-page > .feed-panel {
    padding: 20px;
}

.creation-form {
    max-width: 520px;
}

.dashboard {
    display: grid;
    grid-template-columns: 235px minmax(0, 1fr) minmax(360px, 440px);
    grid-template-areas:
        "top top top"
        "collapse collapse collapse"
        "away away away"
        "stats feed side";
    gap: 16px;
    align-items: start;
}

.since-away-panel {
    grid-area: away;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
    background: var(--soft-strong);
}

.since-away-lines {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.since-away-lines span {
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 0.25rem 0.55rem;
    color: var(--muted);
    background: var(--panel);
    font-size: 0.82rem;
}

.collapse-banner {
    grid-area: collapse;
    display: flex;
    gap: 12px;
    align-items: center;
    justify-content: space-between;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
    background: var(--soft);
}

.collapse-banner.warning {
    border-color: #d5b171;
    background: var(--warning);
}

.collapse-banner span {
    color: var(--muted);
}

.collapse-banner a {
    color: var(--accent-strong);
    font-weight: 800;
    text-decoration: none;
    white-space: nowrap;
}

.dashboard-topbar {
    grid-area: top;
    align-items: center;
    min-width: 0;
}

.dashboard-titleline {
    display: flex;
    min-width: 0;
    gap: 12px;
    align-items: baseline;
    flex-wrap: wrap;
}

.dashboard-titleline strong {
    max-width: min(34ch, 48vw);
    overflow: hidden;
    font-size: 1.15rem;
    line-height: 1.2;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.dashboard-titleline span {
    color: var(--muted);
    font-size: 0.88rem;
    white-space: nowrap;
}

.stats-panel {
    grid-area: stats;
    display: flex;
    flex-direction: column;
}

.feed-panel {
    grid-area: feed;
}

.side-panel {
    grid-area: side;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 0.9fr);
    gap: 16px;
    min-width: 0;
}

.single-column.summary-page,
.single-column.detail-page {
    grid-template-columns: minmax(0, 1fr);
}

.single-column.summary-page > *,
.single-column.detail-page > * {
    grid-area: auto;
    grid-column: 1 / -1;
    width: 100%;
}

.single-column.detail-page > .topbar,
.single-column.summary-page > .topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
}

.influence-panel,
.threads-panel,
.spotlight-panel,
.feed-panel,
.stats-panel {
    min-width: 0;
}

.spotlight-panel {
    grid-column: 1 / -1;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 16px;
    background: var(--panel);
}

.spotlight-card {
    background: var(--soft);
}

.stat-list,
.event-list {
    display: grid;
    gap: 12px;
}

.stats-visual {
    display: grid;
    gap: 12px;
}

.stats-radar-card {
    position: relative;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px;
    background: var(--raised);
}

.stats-radar-shell {
    position: relative;
}

.stats-radar-shell.expanded {
    position: fixed;
    z-index: 30;
    top: var(--radar-expanded-top, 140px);
    left: var(--radar-expanded-left, 24px);
    width: min(400px, calc(100vw - 32px));
    max-width: none;
}

.stats-radar-shell.expanded .stats-radar-card {
    width: 100%;
    padding: 10px;
    box-shadow: 0 18px 50px rgba(0, 0, 0, 0.18);
}

.stats-radar-shell.expanded .stats-radar {
    width: 100%;
}

.stats-radar {
    display: block;
    width: 100%;
    height: auto;
    overflow: visible;
}

.stats-radar-ring,
.stats-radar-axis {
    fill: none;
    stroke: var(--line);
    stroke-width: 1;
}

.stats-radar-ring.outer {
    stroke: color-mix(in srgb, var(--accent) 28%, var(--line));
}

.stats-radar-area {
    fill: color-mix(in srgb, var(--accent) 24%, transparent);
    stroke: none;
}

.stats-radar-outline {
    fill: none;
    stroke: var(--accent);
    stroke-width: 2;
}

.stats-radar-point {
    fill: var(--accent);
    stroke: var(--panel);
    stroke-width: 1.5;
}

.stats-radar-point.up {
    fill: var(--good);
}

.stats-radar-point.down {
    fill: var(--bad);
}

.stats-radar-point.flat {
    fill: var(--accent);
}

.stats-radar-label {
    font-size: 8.5px;
    font-weight: 700;
}

.stats-legend {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.stats-legend-item {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 0.35rem 0.55rem;
    background: var(--panel);
    font-size: 0.78rem;
}

.stats-legend-item.up {
    color: var(--good);
}

.stats-legend-item.down {
    color: var(--bad);
}

.stats-legend-swatch {
    width: 9px;
    height: 9px;
    border-radius: 999px;
    flex: 0 0 auto;
    background: currentColor;
}

.stats-legend-label {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stats-radar-expand {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 2;
    width: 30px;
    height: 30px;
    font-size: 1rem;
    font-weight: 700;
}

.scroll-region {
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-right: 6px;
    scrollbar-gutter: stable;
}

.event-scroll {
    max-height: clamp(360px, calc(100vh - 190px), 680px);
}

.stats-scroll {
    max-height: clamp(360px, calc(100vh - 190px), 680px);
}

.influence-scroll {
    max-height: clamp(280px, calc(100vh - 250px), 600px);
}

.threads-scroll {
    max-height: clamp(280px, calc(100vh - 205px), 600px);
}

.stat-row {
    display: grid;
    gap: 8px;
}

.stat-row div {
    display: flex;
    justify-content: space-between;
}

.stat-row strong {
    font-size: 1.1rem;
}

.stat-row meter {
    width: 100%;
}

.stat-row small {
    color: var(--muted);
}

.stat-row small.up {
    color: var(--good);
}

.stat-row small.down {
    color: var(--bad);
}

.section-heading,
.event-meta {
    display: flex;
    gap: 12px;
    justify-content: space-between;
}

.section-heading span,
.event-meta {
    color: var(--muted);
    font-size: 0.85rem;
}

.stat-changes-tooltip {
    position: relative;
    display: inline-flex;
    align-items: center;
}

.stat-changes-trigger {
    border-bottom: 1px dotted var(--muted);
    cursor: help;
}

.stat-changes-popover {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    display: none;
    min-width: 180px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0.55rem 0.65rem;
    background: var(--panel);
    color: var(--ink);
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.16);
    white-space: nowrap;
}

.stat-changes-popover span {
    display: block;
    line-height: 1.45;
}

.stat-changes-tooltip:hover .stat-changes-popover,
.stat-changes-tooltip:focus-within .stat-changes-popover {
    display: block;
}

.heading-link {
    color: inherit;
    text-decoration: none;
}

.inline-link {
    color: var(--accent-strong);
    text-decoration: none;
}

.inline-link:hover,
.heading-link:hover {
    color: var(--accent-strong);
    text-decoration: underline;
}

.feed-heading {
    align-items: center;
}

.feed-title-actions {
    display: flex;
    gap: 1ch;
    align-items: center;
    min-width: 0;
}

.feed-title-actions h2 {
    margin: 0;
}

.advance-form {
    margin: 0;
}

.advance-button {
    width: 36px;
    height: 36px;
    font-size: 0.95rem;
}

.event-item {
    border-top: 1px solid var(--line);
    padding-top: 14px;
}

.event-item p {
    margin: 0.35rem 0 0;
    line-height: 1.55;
}

.thread-tag {
    display: inline-flex;
    width: fit-content;
    margin-top: 8px;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 0.25rem 0.55rem;
    color: var(--accent-strong);
    background: var(--soft);
    font-size: 0.78rem;
    font-weight: 800;
    text-decoration: none;
}

.template-meta {
    margin-top: 7px;
    color: var(--muted);
    font-size: 0.78rem;
}

.thread-list {
    display: grid;
    gap: 10px;
}

.stats-factions {
    margin-top: 18px;
    border-top: 1px solid var(--line);
    padding-top: 14px;
}

.stats-factions .section-heading a {
    color: var(--accent-strong);
    font-size: 0.85rem;
    font-weight: 800;
    text-decoration: none;
}

.faction-list,
.faction-list-large {
    display: grid;
    gap: 10px;
}

.compact-faction-list {
    max-height: 190px;
    overflow-y: auto;
    padding-right: 4px;
}

.faction-chip,
.faction-row {
    display: grid;
    gap: 4px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px;
    background: var(--raised);
    text-decoration: none;
}

.faction-chip span,
.faction-row strong {
    font-weight: 800;
}

.faction-chip small,
.faction-row span,
.faction-metrics {
    color: var(--muted);
    font-size: 0.82rem;
}

.faction-row {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
}

.faction-metrics {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.faction-detail-grid {
    display: grid;
    grid-template-columns: minmax(0, 0.8fr) minmax(0, 1.2fr);
    gap: 16px;
}

.detail-page .faction-detail-grid {
    grid-template-columns: minmax(0, 1fr);
}

.detail-page .faction-power-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(360px, 0.9fr);
    gap: 16px;
    align-items: start;
}

.detail-page .faction-power-grid > * {
    grid-area: auto;
    grid-column: auto;
}

.historical-figure-summary-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    justify-items: start;
}

.historical-figure-summary-grid > .feed-panel {
    grid-column: 1;
    width: min(100%, 520px);
}

.faction-metric-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.faction-metric-grid div {
    display: grid;
    gap: 3px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px;
    background: var(--raised);
}

.faction-metric-grid span,
.ideology-row span {
    color: var(--muted);
    font-size: 0.82rem;
}

.ideology-list {
    display: grid;
    gap: 10px;
}

.ideology-row {
    display: grid;
    grid-template-columns: minmax(90px, 1fr) minmax(120px, 2fr) 34px;
    gap: 10px;
    align-items: center;
}

.ideology-row meter {
    width: 100%;
}

.institution-links {
    display: grid;
    gap: 10px;
}

.institution-links div {
    display: grid;
    gap: 4px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px;
    background: var(--raised);
}

.institution-links span {
    color: var(--muted);
    font-size: 0.82rem;
}

.chronicle-list,
.chronicle-mini-list {
    display: grid;
    gap: 10px;
}

.chronicle-card,
.chronicle-mini,
.figure-card {
    display: grid;
    gap: 6px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
    background: var(--raised);
    text-decoration: none;
}

.chronicle-card p {
    margin: 0;
    color: var(--muted);
    line-height: 1.5;
}

.figure-list,
.figure-links {
    display: grid;
    gap: 10px;
}

.figure-card p {
    margin: 0;
    color: var(--muted);
    line-height: 1.5;
}

.figure-timeline-item {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 14px;
    background: var(--raised);
}

.chronicle-mini {
    padding: 10px;
}

.chronicle-mini span {
    overflow: hidden;
    font-weight: 800;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chronicle-mini small {
    color: var(--muted);
    font-size: 0.82rem;
}

.chronicle-body {
    max-width: 78ch;
    color: var(--ink);
    font-size: 1.05rem;
    line-height: 1.65;
}

.compact-empty {
    font-size: 0.85rem;
}

.influence-meter {
    width: 100%;
    margin-bottom: 14px;
}

.action-list {
    display: grid;
    gap: 12px;
}

.action-form {
    display: grid;
    gap: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
    background: var(--raised);
}

.action-copy {
    display: grid;
    gap: 3px;
}

.action-copy span {
    color: var(--muted);
    font-size: 0.85rem;
}

.action-controls {
    display: grid;
    gap: 8px;
}

.preference-form {
    display: grid;
    gap: 10px;
}

.preference-form label {
    font-size: 0.82rem;
}

.thread-card {
    display: grid;
    gap: 7px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
    background: var(--raised);
    text-decoration: none;
}

.thread-card span {
    font-weight: 800;
}

.thread-card small {
    color: var(--muted);
}

.thread-card meter {
    width: 100%;
}

.thread-view h1 {
    max-width: 18ch;
    font-size: clamp(2rem, 5vw, 4rem);
}

.detail-page.thread-view h1 {
    max-width: 24ch;
}

.thread-view .event-list {
    grid-template-columns: minmax(0, 1fr);
    align-items: start;
}

.thread-view .event-item {
    height: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 14px;
    background: var(--raised);
}

.summary-page .chronicle-list {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}

.summary-page .faction-list-large {
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}

.settings-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
}

.settings-grid > .feed-panel {
    grid-area: auto;
}

.settings-grid > .feed-panel:last-child {
    grid-column: 1 / -1;
}

.settings-form {
    display: grid;
    gap: 12px;
}

.logout-form {
    margin-top: 16px;
    border-top: 1px solid var(--line);
    padding-top: 16px;
}

.empty-state {
    color: var(--muted);
    line-height: 1.45;
}

.empty-state strong {
    color: var(--ink);
}

@media (max-width: 900px) {
    .auth-grid,
    .dashboard {
        grid-template-columns: 1fr;
    }

    .dashboard {
        grid-template-areas:
            "top"
            "collapse"
            "away"
            "stats"
            "side"
            "feed";
    }

    .side-panel {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    }

    .event-scroll {
        max-height: 34vh;
    }

    .stats-scroll {
        max-height: 34vh;
    }

    .influence-scroll,
    .threads-scroll {
        max-height: 28vh;
    }
}

@media (max-width: 560px) {
    .app-shell {
        width: min(100% - 20px, 1180px);
        padding: 16px 0;
    }

    .dashboard {
        gap: 10px;
    }

    .collapse-banner {
        display: grid;
    }

    .dashboard-topbar {
        display: flex;
        align-items: center;
    }

    .dashboard-titleline {
        gap: 6px 10px;
    }

    .dashboard-titleline strong {
        max-width: 100%;
        flex-basis: 100%;
        font-size: 1rem;
    }

    .stats-panel,
    .feed-panel,
    .influence-panel,
    .threads-panel {
        padding: 12px;
    }

    .faction-detail-grid,
    .detail-page .faction-power-grid,
    .settings-grid,
    .faction-row {
        grid-template-columns: 1fr;
    }

    .settings-grid > .feed-panel:last-child {
        grid-column: auto;
    }

    .faction-metrics {
        justify-content: flex-start;
    }

    .stats-legend {
        grid-template-columns: 1fr;
    }

    .stats-radar-label {
        font-size: 7px;
    }

    .side-panel {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .event-scroll {
        max-height: 28vh;
    }

    .stats-scroll {
        max-height: 28vh;
    }

    .influence-scroll {
        max-height: 25vh;
    }

    .threads-scroll {
        max-height: 22vh;
    }

    .topbar,
    .section-heading,
    .event-meta {
        display: grid;
    }

    .topbar-actions {
        justify-content: flex-start;
    }
}
