:root {
    --bg: #f6f8fc;
    --text: #0f172a;
    --muted: #5f6b7c;
    --border: #d6deeb;
    --card-bg: #ffffff;
    --panel-w: 420px;
    --topbar-h: 56px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
    color: var(--text);
    background: var(--bg);
    overflow: hidden;
    height: 100dvh;
}

#map {
    position: fixed;
    inset: 0;
    z-index: 0;
}

.topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--topbar-h);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0 0.8rem;
    z-index: 500;
    border-bottom: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(6px);
}

.topbar-logo {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    text-decoration: none;
    color: var(--text);
    font-weight: 700;
    white-space: nowrap;
}
.topbar-logo-img { height: 26px; width: auto; }
.topbar-logo-text { font-size: 0.92rem; }

.search-wrap {
    position: relative;
    flex: 1;
    max-width: 680px;
}
.search-wrap input {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.5rem 0.7rem;
    font-size: 0.88rem;
    background: #fff;
}
.search-wrap input:focus {
    outline: none;
    border-color: #0ea5e9;
    box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.18);
}

.search-results {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 10px;
    max-height: 260px;
    overflow-y: auto;
    box-shadow: 0 8px 22px rgba(0, 0, 0, 0.12);
    display: none;
}
.search-results.visible { display: block; }
.search-item {
    display: block;
    width: 100%;
    border: none;
    text-align: left;
    padding: 0.6rem 0.7rem;
    background: #fff;
    cursor: pointer;
}
.search-item:hover { background: #eff6ff; }
.search-code {
    font-size: 0.74rem;
    color: #0369a1;
    font-weight: 700;
}
.search-name {
    font-size: 0.86rem;
    color: #0f172a;
}

.topbar-actions { display: flex; align-items: center; }
.topbar-btn {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.35rem 0.62rem;
    background: #fff;
    color: var(--text);
    cursor: pointer;
}

.panel {
    position: fixed;
    top: var(--topbar-h);
    right: 0;
    width: var(--panel-w);
    height: calc(100dvh - var(--topbar-h));
    background: var(--card-bg);
    border-left: 1px solid var(--border);
    z-index: 400;
    display: flex;
    flex-direction: column;
    transition: transform 0.28s ease;
}
.panel.hidden { transform: translateX(100%); }

.panel-header {
    display: flex;
    align-items: center;
    border-bottom: 1px solid var(--border);
    padding-right: 0.3rem;
}
.panel-tabs {
    display: flex;
    gap: 0.45rem;
    overflow-x: auto;
    flex: 1;
    padding: 0.35rem;
    scrollbar-width: none;
}
.panel-tabs::-webkit-scrollbar { display: none; }

.ptab-group {
    display: flex;
    gap: 0.25rem;
    border-radius: 10px;
    padding: 0.2rem;
}
.ptab-group-selected {
    background: #ecfeff;
    border: 1px solid #a5f3fc;
}
.ptab-group-all {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
}

.ptab {
    border: none;
    border-radius: 8px;
    padding: 0.42rem 0.62rem;
    min-width: 110px;
    background: transparent;
    color: var(--muted);
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 0.15rem;
    cursor: pointer;
}
.ptab.active {
    color: var(--text);
    background: #fff;
    box-shadow: 0 1px 0 rgba(0,0,0,0.04);
}
.ptab-title {
    font-size: 0.81rem;
    font-weight: 600;
    line-height: 1.05;
}
.ptab-tag {
    font-size: 0.61rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    border-radius: 999px;
    background: #e2e8f0;
    color: #475569;
    padding: 0.03rem 0.34rem;
}
.ptab.active .ptab-tag {
    background: #0f172a;
    color: #fff;
}

.panel-close {
    border: none;
    background: transparent;
    color: #64748b;
    font-size: 1rem;
    padding: 0.5rem;
    cursor: pointer;
}

.panel-body {
    flex: 1;
    overflow-y: auto;
}
.tab-pane { display: none; padding: 0.9rem; }
.tab-pane.active { display: block; }

.selected-stop-label {
    font-size: 0.9rem;
    color: #334155;
    margin-bottom: 0.55rem;
    font-weight: 600;
}
.arrivals-actions { margin-bottom: 0.65rem; }
.open-arrivals-link {
    display: inline-flex;
    align-items: center;
    border-radius: 8px;
    border: 1px solid #bae6fd;
    color: #0369a1;
    background: #f0f9ff;
    text-decoration: none;
    font-size: 0.78rem;
    font-weight: 600;
    padding: 0.35rem 0.55rem;
}

.pane-loading {
    color: var(--muted);
    font-size: 0.86rem;
    padding: 0.7rem 0.1rem;
}

.arrivals-list, .history-content, .volume-list {
    display: flex;
    flex-direction: column;
    gap: 0.42rem;
}
.arrival-row, .history-row, .volume-row {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.52rem 0.62rem;
    background: #fff;
}
.arrival-head {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.35rem;
}
.svc-badge {
    min-width: 44px;
    text-align: center;
    border-radius: 6px;
    background: #0284c7;
    color: #fff;
    font-size: 0.76rem;
    font-weight: 800;
    padding: 0.18rem 0.35rem;
}
.svc-operator { color: #64748b; font-size: 0.74rem; }
.arrival-times {
    display: flex;
    gap: 0.35rem;
    flex-wrap: wrap;
}
.arrival-pill {
    font-size: 0.72rem;
    border-radius: 999px;
    border: 1px solid #cbd5e1;
    padding: 0.14rem 0.44rem;
    background: #f8fafc;
}

.history-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
    margin-bottom: 0.65rem;
}
.history-select {
    flex: 1 1 11rem;
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.42rem 0.58rem;
    background: #fff;
    font-size: 0.83rem;
}
#historyLoadBtn { white-space: nowrap; }
#historyClearBtn { white-space: nowrap; }

.inline-loader {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    margin-bottom: 0.5rem;
    color: #475569;
    font-size: 0.8rem;
    font-weight: 600;
}
.inline-loader::before {
    content: "";
    width: 12px;
    height: 12px;
    border: 2px solid #bfdbfe;
    border-top-color: #0284c7;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin {
    to { transform: rotate(360deg); }
}

.history-title {
    font-size: 0.78rem;
    color: #64748b;
    margin-top: 0.3rem;
}
.history-row {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.45rem;
    align-items: center;
}
.history-row.clickable {
    cursor: pointer;
}
.history-row.clickable:hover {
    background: #f0f9ff;
    border-color: #7dd3fc;
}
.history-name {
    font-size: 0.8rem;
    color: #0f172a;
}
.history-vol {
    font-size: 0.76rem;
    color: #475569;
}

.volume-row {
    display: grid;
    grid-template-columns: 2.5rem 1fr auto;
    align-items: center;
    gap: 0.5rem;
}
.volume-rank {
    font-size: 0.74rem;
    color: #64748b;
}
.volume-name {
    font-size: 0.82rem;
    color: #0f172a;
}
.volume-total {
    font-size: 0.76rem;
    color: #475569;
}

.bus-stop-tooltip {
    font-size: 11px !important;
    line-height: 1.15 !important;
    padding: 1px 5px !important;
    border-radius: 4px;
    border: 1px solid #cbd5e1;
    background: rgba(255, 255, 255, 0.9);
}

.panel-open-btn {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    z-index: 401;
    display: none;
    border: none;
    border-radius: 999px;
    padding: 0.58rem 1rem;
    background: #0f172a;
    color: #fff;
    font-weight: 600;
    cursor: pointer;
}
.panel-open-btn.visible { display: block; }

@media (max-width: 880px) {
    :root { --panel-w: 100vw; }
    .topbar-logo-text { display: none; }
    .panel {
        width: 100%;
        right: 0;
        left: 0;
        top: auto;
        bottom: 0;
        height: 63dvh;
        border-left: none;
        border-top: 1px solid var(--border);
    }
    .panel.hidden { transform: translateY(100%); }
    .search-wrap { max-width: none; }
    .history-select, #historyLoadBtn { flex: 1 1 100%; }
}
