:root {
    --bg: #f4f6f8;
    --panel: #ffffff;
    --ink: #17202a;
    --muted: #627083;
    --line: #d8e0e8;
    --accent: #0f766e;
    --accent-dark: #0b5f59;
    --danger: #b42318;
    --warning: #a15c07;
    --shadow: 0 14px 35px rgba(23, 32, 42, 0.12);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: Arial, Helvetica, sans-serif;
    min-width: 320px;
}

.topbar {
    min-height: 74px;
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: center;
    padding: 16px 22px;
    background: #ffffff;
    border-bottom: 1px solid var(--line);
}

h1,
h2,
p {
    margin: 0;
}

h1 {
    font-size: 23px;
}

h2 {
    font-size: 17px;
}

.topbar p,
.panel-heading span {
    color: var(--muted);
    margin-top: 4px;
    font-size: 14px;
}

.layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(360px, 430px);
    gap: 16px;
    padding: 16px;
    min-height: calc(100dvh - 74px);
    height: calc(100dvh - 74px);
}

.settings-layout {
    padding: 16px;
}

.map-panel {
    position: relative;
    min-height: 520px;
    border: 1px solid var(--line);
    background: #dfe7ef;
    overflow: hidden;
}

#map {
    height: 100%;
    min-height: 520px;
    touch-action: none;
}

.station-map-icon {
    background: transparent;
    border: 0;
}

.station-map-building {
    position: relative;
    display: block;
    width: 46px;
    min-height: 62px;
    background: #ffffff;
    border: 3px solid #0f766e;
    box-shadow: 0 5px 14px rgba(23, 32, 42, 0.28);
}

.station-map-building::before {
    content: "";
    position: absolute;
    left: 12px;
    top: -12px;
    width: 18px;
    height: 18px;
    background: #ffffff;
    border-left: 3px solid #0f766e;
    border-top: 3px solid #0f766e;
    transform: rotate(45deg);
}

.station-map-building::after {
    content: "";
    position: absolute;
    left: 16px;
    bottom: -11px;
    width: 8px;
    height: 8px;
    background: #0f766e;
    transform: rotate(45deg);
}

.station-map-cross {
    position: absolute;
    left: 50%;
    top: 13px;
    width: 18px;
    height: 6px;
    background: #d92d20;
    transform: translate(-50%, -50%);
    z-index: 2;
}

.station-map-cross::after {
    content: "";
    position: absolute;
    left: 6px;
    top: -6px;
    width: 6px;
    height: 18px;
    background: #d92d20;
}

.station-map-vehicles {
    position: relative;
    z-index: 3;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
    padding: 23px 0 3px;
}

.station-map-vehicles span {
    display: block;
    width: 40px;
    padding: 1px 0;
    background: #17202a;
    color: #ffffff;
    font-size: 10px;
    font-weight: 700;
    line-height: 1.1;
    text-align: center;
}

.map-tools {
    position: absolute;
    left: 16px;
    top: 16px;
    z-index: 500;
    display: grid;
    grid-template-columns: minmax(260px, 430px) auto;
    align-items: start;
    gap: 10px;
    padding: 10px;
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid var(--line);
    box-shadow: var(--shadow);
}

.place-search {
    position: relative;
    display: grid;
    grid-template-columns: minmax(170px, 1fr) auto;
    gap: 8px;
}

.place-search input {
    min-width: 0;
}

.place-search-results {
    position: absolute;
    left: 0;
    right: 0;
    top: calc(100% + 6px);
    display: grid;
    gap: 4px;
    max-height: 260px;
    overflow: auto;
    padding: 6px;
    background: #ffffff;
    border: 1px solid var(--line);
    box-shadow: var(--shadow);
}

.place-result {
    width: 100%;
    min-height: 36px;
    padding: 8px 10px;
    background: #ffffff;
    color: var(--ink);
    border: 1px solid transparent;
    text-align: left;
}

.place-result:hover {
    background: #eef3f7;
    color: var(--ink);
    border-color: var(--line);
}

.zensus-popup-table {
    margin-top: 6px;
    border-collapse: collapse;
    font-size: 12px;
}

.zensus-popup-table td {
    padding: 2px 8px 2px 0;
    border-bottom: 1px solid #e5ebf1;
    white-space: nowrap;
}

.zensus-popup-table td:last-child {
    padding-right: 0;
    text-align: right;
    font-weight: 700;
}

.zensus-popup-note {
    max-width: 300px;
    margin: 8px 0 0;
    color: var(--muted);
    font-size: 11px;
    line-height: 1.35;
}

.zensus-legend {
    padding: 9px 10px;
    background: rgba(255, 255, 255, 0.94);
    border: 1px solid var(--line);
    box-shadow: var(--shadow);
    color: var(--ink);
    font-size: 12px;
    line-height: 1.25;
}

.zensus-legend strong {
    display: block;
    margin-bottom: 6px;
}

.zensus-legend-row {
    display: grid;
    grid-template-columns: 22px auto;
    gap: 6px;
    align-items: center;
    margin-top: 3px;
}

.zensus-legend-row span {
    width: 22px;
    height: 12px;
    border: 1px solid rgba(23, 32, 42, 0.24);
}

.zensus-cell-label {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #101828;
    font-size: 10px;
    font-weight: 700;
    text-align: center;
    text-shadow: 0 1px 2px #ffffff, 1px 0 2px #ffffff, -1px 0 2px #ffffff;
    pointer-events: none;
}

.route-tools {
    display: grid;
    grid-template-columns: auto 112px auto minmax(150px, 1fr) auto;
    align-items: center;
    gap: 10px;
}

.block-tools {
    grid-column: 1 / -1;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.side-panel {
    overflow: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding-right: 2px;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    padding: 16px;
    box-shadow: 0 4px 16px rgba(23, 32, 42, 0.06);
}

.panel-heading {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: baseline;
    margin-bottom: 12px;
}

.result-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
}

.result-mode-row {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    margin: 0 0 10px;
}

.result-mode-row label {
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}

.result-mode-row select {
    width: auto;
    min-width: 210px;
}

.rank-table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
    font-size: 14px;
}

.rank-table th,
.rank-table td {
    border-bottom: 1px solid var(--line);
    padding: 10px 8px;
    text-align: left;
    vertical-align: top;
}

.rank-table th:first-child,
.rank-table td:first-child {
    width: 48px;
}

.time-cell,
.delta-cell {
    width: 96px;
    text-align: right;
}

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

.delta-positive {
    color: #b42318;
}

.delta-negative {
    color: #047857;
}

.calculation-progress {
    display: grid;
    gap: 7px;
    color: var(--text);
}

.calculation-progress-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    font-weight: 700;
}

.calculation-progress-top strong {
    color: var(--accent-dark);
    white-space: nowrap;
}

.calculation-progress-bar {
    height: 8px;
    overflow: hidden;
    background: #eef3f7;
    border: 1px solid var(--line);
}

.calculation-progress-bar span {
    display: block;
    height: 100%;
    background: #0f766e;
    transition: width 0.45s ease;
}

.calculation-progress-meta {
    color: var(--muted);
    font-size: 12px;
}

.result-station {
    cursor: pointer;
    color: var(--accent-dark);
    font-weight: 700;
}

.result-station:hover,
.result-station:focus {
    background: #eef7f5;
    outline: 2px solid rgba(15, 118, 110, 0.22);
    outline-offset: -2px;
}

.vehicle-tag {
    display: inline-block;
    margin-left: 6px;
    padding: 2px 5px;
    background: #eef3f7;
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
}

.mediation-badge {
    display: inline-block;
    margin-left: 4px;
    padding: 2px 4px;
    background: #fff4e5;
    color: #a15c07;
    border: 1px solid #f2c078;
    font-size: 11px;
    font-weight: 700;
    line-height: 1;
}

.tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin: 14px 0;
}

.tab,
button,
select,
input {
    font: inherit;
}

button,
.button-link {
    border: 0;
    background: var(--accent);
    color: #fff;
    padding: 10px 13px;
    cursor: pointer;
    min-height: 40px;
}

button:hover,
.button-link:hover {
    background: var(--accent-dark);
}

.button-link {
    display: inline-flex;
    align-items: center;
    text-decoration: none;
}

button:disabled {
    background: #9aabb9;
    cursor: not-allowed;
}

.ghost {
    background: #eef3f7;
    color: var(--ink);
    border: 1px solid var(--line);
}

.ghost:hover {
    background: #e0e8ef;
}

.tab {
    background: #eef3f7;
    color: var(--ink);
    border: 1px solid var(--line);
}

.tab.active {
    background: var(--ink);
    color: #fff;
}

.form-grid {
    display: grid;
    gap: 12px;
}

label {
    display: grid;
    gap: 6px;
    color: var(--muted);
    font-size: 13px;
}

input,
select {
    width: 100%;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
    min-height: 40px;
    padding: 9px 10px;
}

.coordinate-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.checkbox-row {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
}

.checkbox-row label,
.checkbox-inline {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--ink);
}

.checkbox-row input,
.checkbox-inline input {
    width: auto;
    min-height: auto;
}

.station-list {
    display: grid;
    gap: 10px;
}

.station-item {
    border: 1px solid var(--line);
    padding: 12px;
    display: grid;
    gap: 8px;
    background: #fbfcfd;
}

.station-title {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    align-items: start;
    font-weight: 700;
}

.station-meta {
    color: var(--muted);
    font-size: 13px;
    line-height: 1.4;
}

.station-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.admin-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 8px;
}

.matrix-scroll {
    margin-top: 10px;
    overflow: auto;
    max-height: 420px;
    border: 1px solid var(--line);
}

.matrix-scroll-wide {
    max-height: calc(100vh - 190px);
}

.matrix-table {
    width: max-content;
    min-width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}

.matrix-table th,
.matrix-table td {
    border: 1px solid var(--line);
    padding: 6px;
    background: #ffffff;
    vertical-align: middle;
}

.matrix-table th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: #eef3f7;
    text-align: left;
}

.matrix-table th:first-child {
    left: 0;
    z-index: 2;
}

.matrix-table td:first-child {
    position: sticky;
    left: 0;
    background: #fbfcfd;
    font-weight: 700;
    min-width: 150px;
}

.matrix-table input {
    width: 70px;
    min-height: 32px;
    padding: 5px 6px;
    text-align: right;
}

.form-actions {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    align-items: center;
}

.danger {
    color: #fff;
    background: var(--danger);
}

.warning {
    color: var(--warning);
}

.hidden {
    display: none !important;
}

.toast {
    position: fixed;
    right: 16px;
    bottom: 16px;
    max-width: 360px;
    padding: 12px 14px;
    background: var(--ink);
    color: #fff;
    box-shadow: var(--shadow);
    z-index: 1000;
}

.notice-backdrop {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: grid;
    place-items: center;
    padding: 18px;
    background: rgba(23, 32, 42, 0.42);
}

.notice-dialog {
    width: min(460px, 100%);
    display: grid;
    gap: 14px;
    padding: 20px;
    background: #ffffff;
    border: 1px solid var(--line);
    box-shadow: var(--shadow);
}

.notice-dialog p {
    color: var(--muted);
    line-height: 1.45;
}

.notice-dialog button {
    justify-self: end;
}

@media (max-width: 980px) {
    .topbar {
        min-height: auto;
    }

    .layout {
        grid-template-columns: 1fr;
        height: auto;
        min-height: 0;
    }

    .map-panel,
    #map {
        min-height: 62dvh;
    }

    .side-panel {
        overflow: visible;
    }
}

@media (max-width: 720px) {
    .topbar {
        align-items: flex-start;
        flex-direction: column;
        gap: 12px;
        padding: 14px 18px;
    }

    .topbar h1 {
        font-size: 22px;
        line-height: 1.2;
    }

    .account-actions,
    .account-actions .button-link,
    .account-actions button {
        width: 100%;
    }

    .account-actions {
        display: grid;
        grid-template-columns: 1fr;
    }

    .layout {
        gap: 12px;
        padding: 0;
    }

    .map-panel {
        display: flex;
        flex-direction: column;
        min-height: 0;
        border-width: 0 0 1px;
        overflow: visible;
    }

    #map {
        min-height: 58dvh;
        height: 58dvh;
        order: 1;
    }

    .map-tools {
        position: static;
        order: 2;
        z-index: auto;
        width: 100%;
        padding: 12px;
        background: #ffffff;
        border-width: 1px 0 0;
        box-shadow: none;
    }

    .map-tools,
    .coordinate-grid {
        grid-template-columns: 1fr;
    }

    .place-search,
    .route-tools {
        display: grid;
        grid-template-columns: 1fr;
    }

    .place-search-results {
        position: static;
        max-height: 220px;
        margin-top: 8px;
    }

    .block-tools {
        display: grid;
        grid-template-columns: 1fr 1fr;
    }

    .block-tools button {
        width: 100%;
        min-width: 0;
    }

    .side-panel {
        gap: 12px;
        padding: 0 10px 14px;
    }

    .panel {
        padding: 14px;
    }

    .panel-heading,
    .result-actions {
        align-items: stretch;
    }

    .panel-heading {
        flex-direction: column;
    }

    .result-actions {
        display: grid;
        grid-template-columns: 1fr auto;
        width: 100%;
    }

    .rank-table {
        font-size: 13px;
    }

    .rank-table th,
    .rank-table td {
        padding: 9px 7px;
    }

    .rank-table th:first-child,
    .rank-table td:first-child {
        width: 42px;
    }

    .time-cell,
    .delta-cell {
        width: 78px;
    }
}

@media (max-width: 420px) {
    .topbar {
        padding: 12px 14px;
    }

    .topbar h1 {
        font-size: 21px;
    }

    #map {
        min-height: 52dvh;
        height: 52dvh;
    }

    .map-tools {
        padding: 10px;
    }

    .block-tools {
        grid-template-columns: 1fr;
    }

    .rank-table {
        font-size: 12px;
    }

    .vehicle-tag,
    .mediation-badge {
        margin-left: 0;
        margin-top: 3px;
    }
}
