:root {
    color-scheme: dark;
    --bg: #0f1419;
    --surface: #171e26;
    --surface-2: #202a34;
    --surface-3: #10161c;
    --border: #303b46;
    --text: #e7edf2;
    --muted: #8f9ba7;
    --link: #7fd0dc;
    --link-hover: #a8e8f0;
    --ok: #14261d;
    --ok-border: #2f7d4d;
    --warn: #3b2f13;
    --warn-border: #c9972f;
    --bad: #3a1b1c;
    --bad-border: #db5b57;
    --focus: #48b8c2;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
    font-size: 15px;
    line-height: 1.45;
}

a {
    color: var(--link);
    text-decoration: none;
}

a:hover {
    color: var(--link-hover);
    text-decoration: underline;
}

code {
    font-family: "Cascadia Mono", Consolas, monospace;
    font-size: 0.92em;
}

h1,
h2 {
    margin: 0;
    letter-spacing: 0;
}

h1 {
    font-size: 2rem;
    line-height: 1.15;
}

h2 {
    font-size: 1.1rem;
}

button,
summary {
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #24424b;
    color: var(--text);
    cursor: pointer;
    font: inherit;
    min-height: 36px;
    padding: 0.45rem 0.8rem;
}

button:hover,
summary:hover {
    background: #2d5661;
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

button:disabled:hover {
    background: #24424b;
}

input,
select,
textarea {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface-3);
    color: var(--text);
    font: inherit;
    min-height: 36px;
    padding: 0.42rem 0.55rem;
}

textarea {
    resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--focus);
    outline: 2px solid rgba(42, 157, 143, 0.18);
}

label {
    color: var(--muted);
    display: grid;
    gap: 0.3rem;
    font-size: 0.9rem;
    font-weight: 600;
}

table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
}

th,
td {
    border-bottom: 1px solid var(--border);
    padding: 0.65rem;
    text-align: left;
    vertical-align: middle;
}

th {
    background: var(--surface-2);
    color: var(--muted);
    font-size: 0.78rem;
    text-transform: uppercase;
}

.topbar {
    align-items: center;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    gap: 1rem;
    justify-content: space-between;
    min-height: 60px;
    padding: 0.7rem clamp(1rem, 4vw, 2rem);
    position: sticky;
    top: 0;
    z-index: 10;
}

.brand {
    color: var(--text);
    font-size: 1.1rem;
    font-weight: 700;
}

.top-search {
    display: grid;
    gap: 0.5rem;
    grid-template-columns: minmax(160px, 240px) auto auto;
}

.topbar-actions {
    align-items: center;
    display: flex;
    gap: 0.65rem;
}

.user-label {
    color: var(--muted);
    font-weight: 700;
}

.page {
    margin: 0 auto;
    max-width: 1180px;
    padding: 1.5rem clamp(1rem, 4vw, 2rem) 3rem;
}

.scanner-open {
    overflow: hidden;
}

.scanner-overlay[hidden] {
    display: none;
}

.scanner-overlay {
    align-items: center;
    background: rgba(6, 10, 14, 0.78);
    display: grid;
    inset: 0;
    padding: 1rem;
    position: fixed;
    z-index: 100;
}

.scanner-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
    display: grid;
    gap: 0.8rem;
    margin: 0 auto;
    max-width: 560px;
    padding: 1rem;
    width: min(100%, 560px);
}

.scanner-header {
    align-items: center;
    display: flex;
    justify-content: space-between;
    gap: 1rem;
}

.scanner-viewport {
    background: var(--surface-3);
    border: 1px solid var(--border);
    border-radius: 8px;
    min-height: 260px;
    overflow: hidden;
    position: relative;
}

.scanner-viewport video,
.scanner-viewport canvas {
    display: block;
    height: 100%;
    inset: 0;
    object-fit: cover;
    position: absolute;
    width: 100%;
}

.scanner-message {
    color: var(--muted);
    margin: 0;
}

.scanner-message-error {
    color: #ffaaa5;
}

.section-header {
    align-items: end;
    display: flex;
    justify-content: space-between;
    margin-bottom: 1rem;
}

.section-header.compact {
    align-items: center;
}

.muted,
.back-link {
    color: var(--muted);
}

.back-link {
    display: inline-block;
    margin-bottom: 0.4rem;
}

.content-section,
.road-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
}

.content-section {
    margin-bottom: 1rem;
    padding: 1rem;
}

.road-list {
    display: grid;
    gap: 0.65rem;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    margin-bottom: 0.9rem;
}

.switchbox-card-row {
    display: grid;
    gap: 0.65rem;
    grid-template-columns: repeat(auto-fit, minmax(220px, 280px));
    margin-bottom: 1.5rem;
    padding-top: 0.75rem;
}

.road-item {
    color: var(--text);
    display: grid;
    gap: 0.35rem;
    min-height: 96px;
    padding: 1rem;
}

.road-item:hover {
    border-color: var(--focus);
    text-decoration: none;
}

.road-name {
    font-size: 1.2rem;
    font-weight: 700;
}

.road-meta {
    color: var(--muted);
}

.filter-tabs {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.filter-chip {
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--muted);
    min-height: 34px;
    padding: 0.4rem 0.7rem;
}

.filter-chip:hover,
.filter-chip.active {
    border-color: var(--focus);
    color: var(--text);
    text-decoration: none;
}

.button-link {
    align-items: center;
    background: #24424b;
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    display: inline-flex;
    font-weight: 700;
    min-height: 36px;
    padding: 0.45rem 0.8rem;
}

.button-link:hover {
    background: #2d5661;
    color: var(--text);
    text-decoration: none;
}

.sort-link {
    align-items: center;
    color: var(--muted);
    display: inline-flex;
    gap: 0.3rem;
}

.sort-link:hover,
.sort-link.active {
    color: var(--text);
    text-decoration: none;
}

.sort-mark {
    color: var(--link);
    font-size: 0.78rem;
}

.table-wrap {
    overflow-x: auto;
}

.rail-table th:nth-child(1),
.rail-table td:nth-child(1) {
    width: 118px;
}

.rail-table td {
    padding: 0.34rem 0.5rem;
}

.rail-table td.rail-cell {
    padding: 0.22rem;
}

.rail-cell {
    font-weight: 700;
}

.rail-link {
    align-content: center;
    border: 1px solid transparent;
    border-radius: 6px;
    color: var(--text);
    display: grid;
    min-height: 70px;
    padding: 0.28rem 0.5rem;
}

.rail-link:hover {
    color: var(--link-hover);
    text-decoration: none;
    border-color: var(--focus);
}

.rail-position-value {
    font-size: 1.05rem;
    line-height: 1.1;
}

.rail-serial {
    color: var(--muted);
    font-size: 0.86rem;
    font-weight: 600;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.unit-slots {
    display: grid;
    gap: 0.22rem;
}

.unit-slot,
.inline-unit {
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    gap: 0.65rem;
    min-height: 32px;
    padding: 0.28rem 0.5rem;
}

.unit-slot {
    align-items: center;
    display: grid;
    grid-template-columns: minmax(230px, auto) minmax(0, 1fr);
}

.inline-unit {
    display: flex;
}

.unit-slot:hover,
.inline-unit:hover {
    border-color: var(--focus);
    text-decoration: none;
}

.unit-slot.empty,
.inline-unit.empty {
    align-content: center;
    background: repeating-linear-gradient(135deg, #121920, #121920 8px, #18222c 8px, #18222c 16px);
    color: var(--muted);
}

.unit-slot.empty::before {
    content: "";
    color: var(--muted);
}

.slot-label {
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 700;
    text-transform: uppercase;
}

.unit-serial {
    font-weight: 700;
}

.unit-main {
    align-items: baseline;
    display: flex;
    gap: 0.65rem;
    min-width: 0;
    white-space: nowrap;
}

.unit-last-event {
    color: var(--muted);
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.inventory-note {
    color: var(--muted);
    display: block;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.history-layouts {
    align-items: flex-start;
    display: flex;
    gap: 0.85rem;
    overflow-x: auto;
    padding-bottom: 0.4rem;
}

.history-layout {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    flex: 0 0 min(82vw, 380px);
    min-width: 340px;
    padding: 0.75rem;
}

.history-layout-header {
    align-items: center;
    display: flex;
    gap: 0.6rem;
    justify-content: space-between;
    margin-bottom: 0.55rem;
}

.history-header-actions {
    align-items: center;
    display: flex;
    gap: 0.45rem;
}

.history-time {
    color: var(--muted);
    display: block;
    font-size: 0.88rem;
    font-weight: 700;
    margin-top: 0.15rem;
}

.history-force {
    color: var(--warn-border);
    font-size: 0.8rem;
    font-weight: 800;
    text-transform: uppercase;
}

.history-note-link {
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--muted);
    font-size: 0.82rem;
    font-weight: 800;
    min-height: 30px;
    padding: 0.3rem 0.55rem;
}

.history-note-link:hover,
.history-note-link.has-note {
    border-color: var(--focus);
    color: var(--text);
    text-decoration: none;
}

.history-note-dot {
    background: #ffd982;
    border: 2px solid var(--warn-border);
    border-radius: 50%;
    cursor: help;
    display: inline-block;
    height: 9px;
    width: 9px;
}

.history-table th:nth-child(1),
.history-table td:nth-child(1) {
    width: 48px;
}

.history-table th,
.history-table td {
    padding: 0.28rem;
}

.history-rail {
    align-items: center;
    border: 1px solid var(--border);
    border-radius: 6px;
    display: flex;
    font-weight: 700;
    justify-content: center;
    min-height: 54px;
    padding: 0.28rem;
}

.history-slots {
    display: grid;
    gap: 0.2rem;
}

.history-slot {
    align-items: center;
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    display: flex;
    min-height: 28px;
    min-width: 0;
    padding: 0.22rem 1rem 0.22rem 0.42rem;
    position: relative;
}

.history-slot:hover {
    border-color: var(--focus);
    text-decoration: none;
}

.history-slot.empty {
    background: repeating-linear-gradient(135deg, #121920, #121920 8px, #18222c 8px, #18222c 16px);
    color: var(--muted);
    justify-content: center;
}

.history-unit-main {
    align-items: baseline;
    display: flex;
    gap: 0.5rem;
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
}

.history-unit-main code,
.history-unit-main .unit-serial,
.history-unit-main .unknown-serial {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-change-dot {
    border: 2px solid var(--link-hover);
    border-radius: 50%;
    height: 9px;
    position: absolute;
    right: 0.35rem;
    top: 50%;
    transform: translateY(-50%);
    width: 9px;
}

.history-change-dot.history-diff-added {
    border-color: var(--ok-border);
    background: #8ee0aa;
}

.history-change-dot.history-diff-removed {
    border-color: var(--bad-border);
    background: #ffaaa5;
}

.history-change-dot.history-diff-replaced {
    border-color: var(--warn-border);
    background: #ffd982;
}

.unknown-serial {
    color: #ff817a;
    font-weight: 800;
}

.status-bg-0 {
    background: var(--surface-3);
}

.status-bg-1 {
    background: var(--warn);
    border-color: var(--warn-border);
}

.status-bg-2 {
    background: var(--bad);
    border-color: var(--bad-border);
}

.status-label {
    display: inline;
    font-size: 0.9rem;
    font-weight: 800;
}

.status-0 {
    color: #8ee0aa;
}

.status-1 {
    color: #ffd982;
}

.status-2 {
    color: #ffaaa5;
}

.detail-grid {
    display: grid;
    gap: 1rem;
    grid-template-columns: minmax(0, 1fr) minmax(320px, 0.9fr);
}

.detail-list {
    display: grid;
    gap: 0;
    grid-template-columns: 140px minmax(0, 1fr);
    margin: 1rem 0;
}

.detail-list dt,
.detail-list dd {
    border-bottom: 1px solid var(--border);
    margin: 0;
    min-height: 38px;
    padding: 0.55rem 0;
}

.detail-list dt {
    color: var(--muted);
    font-weight: 700;
}

.stacked-links {
    display: grid;
    gap: 0.4rem;
}

.event-form,
.form-grid,
.scan-note-form {
    display: grid;
    gap: 0.75rem;
}

.event-form,
.form-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-top: 1rem;
}

.scan-note-form {
    max-width: 760px;
}

.event-form button,
.form-grid button,
.full-span {
    grid-column: 1 / -1;
}

.edit-panel {
    margin-top: 1rem;
}

.add-switchbox-panel {
    margin: 0 0 1rem;
}

.edit-panel summary {
    display: inline-flex;
    list-style: none;
}

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

.event-list {
    display: grid;
    gap: 0.7rem;
    margin-top: 1rem;
}

.event-item {
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-3);
    padding: 0.8rem;
}

.event-item-scan {
    align-items: center;
    background: #182b31;
    border-color: #2f6f79;
    color: var(--text);
    display: flex;
    gap: 1rem;
    justify-content: space-between;
    min-height: 38px;
    padding: 0.45rem 0.65rem;
}

.event-item-scan time {
    color: var(--muted);
    flex: 0 0 auto;
}

.event-topline,
.event-meta {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: 0.6rem;
}

.event-topline {
    justify-content: space-between;
}

.event-actions {
    align-items: center;
    display: flex;
    gap: 0.5rem;
}

.event-delete-form {
    margin: 0;
}

.event-delete-button {
    background: var(--bad);
    border-color: var(--bad-border);
    color: #ffcfcc;
    font-size: 0.78rem;
    font-weight: 800;
    min-height: 28px;
    padding: 0.25rem 0.55rem;
}

.event-delete-button:hover {
    background: #512122;
}

.event-type {
    font-weight: 800;
    text-transform: capitalize;
}

.event-meta {
    color: var(--muted);
    font-size: 0.88rem;
    margin-top: 0.25rem;
}

.event-item p {
    margin: 0.7rem 0 0;
    white-space: pre-wrap;
}

.activity-list {
    display: grid;
    gap: 0.7rem;
}

.activity-item {
    background: var(--surface-3);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.75rem;
}

.activity-topline,
.activity-target,
.activity-meta {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: 0.55rem;
}

.activity-topline {
    justify-content: space-between;
}

.activity-target {
    min-width: 0;
}

.activity-kind {
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 800;
    text-transform: uppercase;
}

.activity-meta {
    color: var(--muted);
    font-size: 0.88rem;
    margin-top: 0.25rem;
}

.activity-item time {
    color: var(--muted);
    flex: 0 0 auto;
}

.activity-item p {
    margin: 0.65rem 0 0;
    white-space: pre-wrap;
}

.empty-state {
    color: var(--muted);
    margin: 1rem 0 0;
}

.login-page {
    align-items: center;
    display: grid;
    min-height: 100vh;
    padding: 1rem;
}

.login-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    display: grid;
    gap: 1rem;
    margin: 0 auto;
    max-width: 380px;
    padding: 1.2rem;
    width: 100%;
}

.login-form {
    display: grid;
    gap: 0.8rem;
}

.form-error {
    color: #ffaaa5;
    margin: 0;
}

.switchbox-table th:nth-child(1),
.switchbox-table td:nth-child(1) {
    width: 150px;
}

.switchbox-table th:nth-child(2),
.switchbox-table td:nth-child(2) {
    width: 140px;
}

.switchbox-table th:nth-child(3),
.switchbox-table td:nth-child(3) {
    width: 92px;
}

.switchbox-table th:nth-child(4),
.switchbox-table td:nth-child(4),
.switchbox-table th:nth-child(5),
.switchbox-table td:nth-child(5) {
    width: 130px;
}

.search-table code {
    overflow-wrap: anywhere;
}

@media (max-width: 760px) {
    .section-header {
        align-items: stretch;
        flex-direction: column;
    }

    .topbar {
        align-items: center;
        gap: 0.45rem;
        min-height: 48px;
        padding: 0.45rem 0.6rem;
    }

    .brand {
        flex: 0 0 auto;
        font-size: 0;
        min-width: 2rem;
        text-align: center;
    }

    .brand::after {
        content: attr(data-short-label);
        font-size: 1rem;
        font-weight: 800;
    }

    .topbar-actions {
        flex: 1 1 auto;
        gap: 0.35rem;
        justify-content: flex-end;
        min-width: 0;
    }

    .user-label {
        display: none;
    }

    .top-search {
        flex: 1 1 auto;
        gap: 0.3rem;
        grid-template-columns: minmax(0, 1fr) auto auto;
        min-width: 0;
    }

    .top-search input,
    .top-search button,
    .logout-form button {
        font-size: 0.88rem;
        min-height: 32px;
        padding: 0.34rem 0.5rem;
    }

    .logout-form {
        flex: 0 0 auto;
    }

    .scanner-overlay {
        align-items: end;
        padding: 0.6rem;
    }

    .scanner-panel {
        max-width: none;
        padding: 0.75rem;
        width: 100%;
    }

    .scanner-viewport {
        min-height: min(64vh, 420px);
    }

    .detail-grid {
        grid-template-columns: 1fr;
    }

    .event-form,
    .form-grid {
        grid-template-columns: 1fr;
    }

    .detail-list {
        grid-template-columns: 1fr;
    }

    .detail-list dt {
        border-bottom: 0;
        padding-bottom: 0.1rem;
    }

    .rail-table th:nth-child(1),
    .rail-table td:nth-child(1) {
        width: 108px;
    }

    .unit-slot {
        grid-template-columns: 1fr;
    }

    .search-table,
    .search-table thead,
    .search-table tbody,
    .search-table tr,
    .search-table th,
    .search-table td {
        display: block;
        width: 100%;
    }

    .search-table thead {
        display: none;
    }

    .search-table tr {
        background: var(--surface-3);
        border: 1px solid var(--border);
        border-radius: 8px;
        margin-bottom: 0.75rem;
        overflow: hidden;
    }

    .search-table td {
        align-items: start;
        border-bottom: 1px solid var(--border);
        display: grid;
        gap: 0.65rem;
        grid-template-columns: 92px minmax(0, 1fr);
        min-width: 0;
        padding: 0.5rem 0.65rem;
    }

    .search-table td::before {
        color: var(--muted);
        content: attr(data-label);
        font-size: 0.72rem;
        font-weight: 800;
        text-transform: uppercase;
    }

    .search-table td:last-child {
        border-bottom: 0;
    }
}
