:root {
    --bg: #f4f5f7;
    --panel: #ffffff;
    --border: #e3e5e9;
    --text: #1c1e21;
    --text-muted: #6b7280;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
    --amber: #b45309;
    --amber-bg: #fef3c7;
    --green: #15803d;
    --green-bg: #dcfce7;
    --red: #b91c1c;
    --red-bg: #fee2e2;
    --accent: #2563eb;
    --mono: "JetBrains Mono", "SF Mono", "Menlo", "Consolas", monospace;
    --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
    -webkit-font-smoothing: antialiased;
}

.page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 32px;
    width: 100%;
    max-width: 380px;
}

.panel h1 {
    font-size: 1.25rem;
    margin: 0 0 4px;
}

.panel .subtitle {
    color: var(--text-muted);
    font-size: 0.875rem;
    margin: 0 0 24px;
}

label {
    display: block;
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 6px;
}

input[type="password"] {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: var(--mono);
    font-size: 0.9rem;
    background: var(--bg);
    color: var(--text);
    margin-bottom: 16px;
}

input[type="password"]:focus {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

button {
    width: 100%;
    padding: 10px 12px;
    border: none;
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
}

button:hover {
    opacity: 0.92;
}

.error {
    background: var(--red-bg);
    color: var(--red);
    border-radius: 8px;
    padding: 10px 12px;
    font-size: 0.85rem;
    margin-bottom: 16px;
}

.top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
}

.top-bar .brand {
    font-weight: 600;
    font-family: var(--mono);
}

.top-bar form button {
    width: auto;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    font-weight: 500;
    padding: 6px 14px;
}

.content {
    padding: 32px 24px;
    max-width: 640px;
    margin: 0 auto;
}

.status-pill {
    display: inline-block;
    font-family: var(--mono);
    font-size: 0.75rem;
    padding: 4px 10px;
    border-radius: 999px;
    background: var(--green-bg);
    color: var(--green);
}

.card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 20px;
    margin-top: 20px;
}

.health-head {
    display: flex;
    align-items: center;
    gap: 10px;
}

.pulse-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--text-muted);
    flex-shrink: 0;
}

.health-card[data-state="alive"] .pulse-dot {
    background: var(--green);
    box-shadow: 0 0 0 var(--green);
    animation: pulse 2s infinite;
}

.health-card[data-state="starting"] .pulse-dot,
.health-card[data-state="unknown"] .pulse-dot {
    background: var(--amber);
}

.health-card[data-state="down"] .pulse-dot {
    background: var(--red);
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(21, 128, 61, 0.5); }
    70% { box-shadow: 0 0 0 8px rgba(21, 128, 61, 0); }
    100% { box-shadow: 0 0 0 0 rgba(21, 128, 61, 0); }
}

.health-label {
    font-family: var(--mono);
    font-weight: 700;
    letter-spacing: 0.04em;
    font-size: 0.95rem;
}

.health-card[data-state="alive"] .health-label { color: var(--green); }
.health-card[data-state="starting"] .health-label,
.health-card[data-state="unknown"] .health-label { color: var(--amber); }
.health-card[data-state="down"] .health-label { color: var(--red); }

.health-grid {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 6px 16px;
    margin: 16px 0 0;
    font-size: 0.875rem;
}

.health-grid dt {
    color: var(--text-muted);
}

.health-grid dd {
    margin: 0;
    font-family: var(--mono);
    text-align: right;
}

.health-degraded {
    margin: 12px 0 0;
    padding: 8px 10px;
    background: var(--amber-bg);
    color: var(--amber);
    border-radius: 8px;
    font-size: 0.8rem;
}

.health-error {
    margin: 12px 0 0;
    padding: 8px 10px;
    background: var(--red-bg);
    color: var(--red);
    border-radius: 8px;
    font-size: 0.8rem;
}

.view-history-link {
    display: inline-block;
    margin-top: 20px;
    color: var(--accent);
    font-family: var(--mono);
    font-size: 0.85rem;
    text-decoration: none;
}

.view-history-link:hover {
    text-decoration: underline;
}

.back-link {
    display: inline-block;
    color: var(--text-muted);
    font-family: var(--mono);
    font-size: 0.85rem;
    text-decoration: none;
    margin-bottom: 12px;
}

.back-link:hover {
    color: var(--text);
}

.page-title {
    font-size: 1.1rem;
    margin: 0 0 16px;
}

.filter-pills {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.filter-pill {
    width: auto;
    padding: 6px 14px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--panel);
    color: var(--text-muted);
    font-family: var(--mono);
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
}

.filter-pill[aria-pressed="true"] {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.timeline-card {
    padding: 8px 20px 20px;
}

.timeline {
    list-style: none;
    margin: 0;
    padding: 0;
    max-height: 70vh;
    overflow-y: auto;
}

.timeline-day-header {
    position: sticky;
    top: 0;
    background: var(--panel);
    color: var(--text-muted);
    font-family: var(--mono);
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    padding: 12px 0 6px;
}

.timeline-item {
    display: flex;
    gap: 10px;
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
}

.timeline-item:last-child {
    border-bottom: none;
}

.timeline-marker {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-top: 6px;
    flex-shrink: 0;
    background: var(--text-muted);
}

.timeline-item.timeline-info .timeline-marker { background: var(--green); }
.timeline-item.timeline-warning .timeline-marker { background: var(--amber); }
.timeline-item.timeline-error .timeline-marker { background: var(--red); }

.timeline-body {
    min-width: 0;
}

.timeline-time {
    display: block;
    font-family: var(--mono);
    font-size: 0.75rem;
    color: var(--text-muted);
    cursor: pointer;
}

.timeline-text {
    margin: 2px 0 0;
    font-size: 0.9rem;
    word-wrap: break-word;
}

.timeline-item.timeline-warning .timeline-text { color: var(--amber); }
.timeline-item.timeline-error .timeline-text { color: var(--red); }

.timeline-empty {
    color: var(--text-muted);
    font-size: 0.875rem;
    padding: 12px 0;
}

.section-title {
    font-size: 0.95rem;
    margin: 0 0 12px;
}

.section-subtitle {
    color: var(--text-muted);
    font-weight: 400;
    font-size: 0.8rem;
}

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin: 0;
}

.stat dt {
    color: var(--text-muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.stat dd {
    margin: 4px 0 0;
    font-family: var(--mono);
    font-size: 1.5rem;
    font-weight: 700;
}

.restart-chart {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    height: 120px;
}

.restart-col {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
}

.restart-bar {
    width: 100%;
    max-width: 28px;
    min-height: 1px;
    border-radius: 3px 3px 0 0;
    background: var(--accent);
    opacity: 0.65;
}

.restart-bar-elevated {
    background: var(--red);
    opacity: 0.9;
}

.restart-label {
    margin-top: 6px;
    font-family: var(--mono);
    font-size: 0.65rem;
    color: var(--text-muted);
}

.error-item {
    list-style: none;
    padding: 10px 0 10px 12px;
    margin: 0 0 8px;
    border-left: 3px solid var(--border);
    border-bottom: none;
}

.error-item-needs_attention {
    border-left-color: var(--red);
    background: var(--red-bg);
    border-radius: 0 6px 6px 0;
}

.error-item-needs_attention .timeline-text {
    color: var(--red);
    font-weight: 600;
}

.error-item-routine {
    border-left-color: var(--border);
}

.error-item-routine .timeline-text {
    color: var(--text-muted);
}

/* --- API/data health strip (home page) --- */
.strip-card {
    padding-bottom: 16px;
}

.health-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.strip-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    flex: 1 1 160px;
    min-width: 0;
}

.strip-dot {
    animation: none;
}

.strip-item[data-state="alive"] .strip-dot { background: var(--green); }
.strip-item[data-state="down"] .strip-dot { background: var(--red); }
.strip-item[data-state="unknown"] .strip-dot { background: var(--amber); }

.strip-label {
    font-family: var(--mono);
    font-weight: 600;
    font-size: 0.8rem;
    white-space: nowrap;
}

.strip-detail {
    color: var(--text-muted);
    font-size: 0.75rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.data-gaps {
    margin-top: 14px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}

.gap-row {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    font-size: 0.8rem;
    padding: 4px 0;
}

.gap-label {
    color: var(--text-muted);
}

.gap-value {
    font-family: var(--mono);
    text-align: right;
}

/* --- Rulebook viewer --- */
.rb-section {
    padding: 0;
    overflow: hidden;
}

.rb-summary {
    cursor: pointer;
    list-style: none;
    padding: 16px 20px;
    font-weight: 700;
    font-size: 0.95rem;
    font-family: var(--mono);
}

.rb-summary::-webkit-details-marker {
    display: none;
}

.rb-summary::before {
    content: "▸";
    display: inline-block;
    margin-right: 8px;
    color: var(--text-muted);
    transition: transform 0.1s ease;
}

.rb-section[open] .rb-summary::before {
    transform: rotate(90deg);
}

.rb-body {
    padding: 0 20px 18px;
    font-size: 0.85rem;
}

.rb-dl {
    display: grid;
    grid-template-columns: minmax(120px, auto) 1fr;
    gap: 6px 14px;
    margin: 0;
}

.rb-body > .rb-dl {
    padding-top: 2px;
}

.rb-dl dt {
    color: var(--text-muted);
    align-self: baseline;
}

.rb-dl dd {
    margin: 0;
    align-self: baseline;
}

.rb-object-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.rb-object-item {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    background: var(--bg);
}

.rb-scalar {
    font-family: var(--mono);
}

.rb-scalar-list {
    font-family: var(--mono);
}

.rb-empty {
    color: var(--text-muted);
}

.rb-bool {
    font-family: var(--mono);
    font-weight: 700;
}

.rb-bool-true {
    color: var(--green);
}

.rb-bool-false {
    color: var(--red);
}

.load-more {
    width: auto;
    display: block;
    margin: 16px auto 0;
    background: transparent;
    color: var(--accent);
    border: 1px solid var(--border);
    font-weight: 500;
    padding: 8px 20px;
}

/* --- Universe editor --- */
.banner {
    border-radius: 8px;
    padding: 12px 14px;
    font-size: 0.85rem;
    margin: 0 0 16px;
}

.banner-saved {
    background: var(--green-bg);
    color: var(--green);
}

.banner-error {
    background: var(--red-bg);
    color: var(--red);
}

.universe-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.universe-item {
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}

.universe-item:last-child {
    border-bottom: none;
}

.universe-item-head {
    display: flex;
    align-items: center;
    gap: 8px;
}

.universe-symbol {
    font-family: var(--mono);
    font-weight: 700;
    font-size: 0.95rem;
}

.universe-badge {
    font-family: var(--mono);
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 999px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.universe-badge-core {
    background: var(--amber-bg);
    color: var(--amber);
}

.universe-badge-conditional {
    background: var(--bg);
    color: var(--text-muted);
    border: 1px solid var(--border);
}

.universe-fate {
    margin: 6px 0 0;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.universe-fate-scored {
    color: var(--green);
}

.universe-fate-not_scored {
    color: var(--amber);
}

.universe-gate {
    margin-top: 10px;
    font-size: 0.8rem;
}

.remove-form {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

.confirm-input {
    flex: 1;
    min-width: 0;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: var(--mono);
    font-size: 0.8rem;
    background: var(--bg);
    color: var(--text);
}

.remove-btn {
    width: auto;
    padding: 8px 16px;
    background: var(--red);
    font-size: 0.8rem;
}

.add-form {
    display: flex;
    gap: 8px;
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.add-input {
    flex: 1;
    min-width: 0;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: var(--mono);
    font-size: 0.85rem;
    background: var(--bg);
    color: var(--text);
    text-transform: uppercase;
}

.add-btn {
    width: auto;
    padding: 10px 18px;
    font-size: 0.85rem;
}

/* --- Manual restart button (Step 7) --- */
.restart-btn {
    background: var(--red);
}

.restart-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.restart-status {
    margin: 12px 0 0;
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 0.85rem;
    background: var(--bg);
    color: var(--text-muted);
}

.restart-status-ok {
    background: var(--green-bg);
    color: var(--green);
}

.restart-status-error {
    background: var(--red-bg);
    color: var(--red);
}

/* --- Positions screenshot upload (manual test page) --- */
.upload-form {
    display: flex;
    gap: 8px;
    align-items: center;
}

.upload-form input[type="file"] {
    flex: 1;
    min-width: 0;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.upload-form button {
    width: auto;
    padding: 10px 18px;
    font-size: 0.85rem;
}

.upload-form button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.upload-status {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* `display: flex` above is an author-stylesheet declaration, which beats
   the browser's built-in `[hidden] { display: none }` rule regardless of
   selector specificity (author origin always outranks user-agent origin
   in the cascade) -- so without this override, the `hidden` attribute
   the JS sets after the request completes has no visual effect and the
   spinner never goes away. This higher-specificity rule restores it. */
.upload-status[hidden] {
    display: none;
}

.upload-spinner {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
    border: 2px solid var(--border);
    border-top-color: var(--text-muted);
    border-radius: 50%;
    animation: upload-spin 0.7s linear infinite;
}

@keyframes upload-spin {
    to { transform: rotate(360deg); }
}

.upload-result {
    margin-top: 16px;
    padding: 12px 14px;
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-family: var(--mono);
    font-size: 0.8rem;
    white-space: pre-wrap;
    word-break: break-all;
}

/* --- Positions confirm/edit screen --- */
.position-row {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 14px;
    margin-top: 12px;
    background: var(--bg);
}

.position-row-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.position-row-label {
    font-family: var(--mono);
    font-size: 0.8rem;
    color: var(--text-muted);
}

.position-badge {
    font-family: var(--mono);
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 999px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.position-badge-high {
    background: var(--green-bg);
    color: var(--green);
}

.position-badge-low {
    background: var(--amber-bg);
    color: var(--amber);
}

.position-fields {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 10px;
}

.position-fields label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.position-fields input {
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: var(--mono);
    font-size: 0.85rem;
    background: var(--panel);
    color: var(--text);
}

.field-optional {
    font-style: italic;
    font-weight: 400;
    text-transform: none;
    letter-spacing: normal;
}

.positions-confirm-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 18px;
}

.positions-confirm-actions button,
#positions-new-upload-btn {
    width: auto;
    padding: 10px 18px;
    font-size: 0.85rem;
}

.secondary-btn {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
}

/* --- Positions summary card (home page, read-only) --- */
.positions-summary-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.positions-summary-item {
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}

.positions-summary-item:last-child {
    border-bottom: none;
}

.positions-summary-head {
    display: flex;
    align-items: baseline;
    gap: 8px;
}

.positions-summary-underlying {
    font-family: var(--mono);
    font-weight: 700;
    font-size: 0.95rem;
}

.positions-summary-structure {
    color: var(--text-muted);
    font-size: 0.85rem;
}

.positions-summary-grid {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 4px 16px;
    margin: 8px 0 0;
    font-size: 0.85rem;
}

.positions-summary-grid dt {
    color: var(--text-muted);
}

.positions-summary-grid dd {
    margin: 0;
    font-family: var(--mono);
    text-align: right;
}

.positions-summary-updated {
    margin: 12px 0 0;
    font-size: 0.8rem;
    color: var(--text-muted);
}
