/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; }

:root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

body {
    margin: 0;
    padding: 0;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    display: flex;
    flex-direction: column;
    height: 100dvh;
    min-height: 100vh;
}

/* ===== Header ===== */
#header {
    height: calc(48px + var(--safe-top));
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--safe-top) 16px 0 16px;
    background: rgba(255, 255, 255, 0.82);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
    z-index: 20;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    cursor: pointer;
}

.header-left:hover,
.header-left:focus-visible {
    text-decoration: none;
}

.header-title {
    display: inline-flex;
    align-items: baseline;
    font-family: "Avenir Next", "Montserrat", "Helvetica Neue", "Segoe UI", sans-serif;
    font-size: 24px;
    font-weight: 700;
    line-height: 1;
    letter-spacing: -0.045em;
}

.header-title-primary {
    color: #0f2553;
}

.header-title-dot {
    color: #2f7fd3;
}

.header-title-accent {
    color: #3b8bd9;
    letter-spacing: -0.03em;
}

.header-title-product {
    color: #0f2553;
    font-weight: 700;
    letter-spacing: -0.035em;
}

.header-title-separator {
    color: #6b7280;
    margin: 0 0.22em 0 0.12em;
    letter-spacing: 0;
}

@media (max-width: 640px) {
    .header-title {
        font-size: 21px;
    }
}

.header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

#about-link {
    font-size: 13px;
    font-weight: 500;
    color: #6b7280;
    text-decoration: none;
    padding: 4px 2px;
    transition: color 0.15s ease;
}

#about-link:hover {
    color: #1f2937;
}


/* ===== Map ===== */
#map {
    flex: 1;
    min-height: 0;
}

/* ===== Example Banner ===== */
#example-banner {
    position: fixed;
    top: calc(56px + var(--safe-top));
    left: 50%;
    transform: translateX(-50%);
    z-index: 15;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 14px;
    font-size: 13px;
    color: #374151;
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

#example-banner-text {
    white-space: nowrap;
}

#banner-dismiss {
    padding: 2px 10px;
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    color: #6366f1;
    background: transparent;
    border: 1px solid rgba(99, 102, 241, 0.3);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.15s ease;
    white-space: nowrap;
}

#banner-dismiss:hover {
    background: rgba(99, 102, 241, 0.06);
    border-color: rgba(99, 102, 241, 0.5);
}

#banner-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    padding: 0;
    margin-left: 2px;
    font-size: 14px;
    line-height: 1;
    color: #9ca3af;
    background: transparent;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.15s ease;
}

#banner-close:hover {
    color: #374151;
    background: rgba(0, 0, 0, 0.06);
}

@media (max-width: 640px) {
    #example-banner {
        width: calc(100vw - 14px);
        max-width: 420px;
        display: grid;
        grid-template-columns: 1fr auto;
        grid-template-rows: auto auto;
        column-gap: 8px;
        row-gap: 6px;
        align-items: center;
        justify-items: center;
        padding: 8px 10px;
    }

    #example-banner-text {
        grid-column: 1 / -1;
        text-align: center;
        white-space: normal;
        line-height: 1.25;
        font-size: 12px;
    }

    #banner-dismiss {
        justify-self: end;
    }

    #banner-close {
        justify-self: start;
        margin-left: 0;
    }
}

#map-warning {
    position: fixed;
    top: calc(92px + var(--safe-top));
    left: 50%;
    transform: translateX(-50%);
    z-index: 16;
    max-width: min(92vw, 760px);
    padding: 8px 12px;
    font-size: 13px;
    color: #7c2d12;
    background: rgba(255, 247, 237, 0.96);
    border: 1px solid rgba(251, 146, 60, 0.35);
    border-radius: 12px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

/* ===== Hidden ===== */
.hidden { display: none !important; }

/* ===== Trace Modal ===== */
#trace-modal {
    position: fixed;
    inset: 0;
    z-index: 19;
    pointer-events: none;
}

#trace-modal-panel {
    position: fixed;
    width: min(520px, calc(100vw - 16px));
    max-height: min(70vh, 560px);
    background: #ffffff;
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 14px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
    padding: 16px;
    pointer-events: auto;
    opacity: 0;
    transform: translateY(-10px) scale(0.98);
    transform-origin: top right;
    transition: opacity 160ms ease, transform 180ms cubic-bezier(0.22, 1, 0.36, 1);
    will-change: transform, opacity;
}

#trace-modal.is-open #trace-modal-panel {
    opacity: 1;
    transform: translateY(0) scale(1);
}

#trace-modal-panel::before {
    content: '';
    position: absolute;
    top: -12px;
    left: var(--trace-pointer-left, calc(100% - 28px));
    transform: translateX(-50%);
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-bottom: 12px solid rgba(0, 0, 0, 0.16);
}

#trace-modal-panel::after {
    content: '';
    position: absolute;
    top: -10px;
    left: var(--trace-pointer-left, calc(100% - 28px));
    transform: translateX(-50%);
    border-left: 9px solid transparent;
    border-right: 9px solid transparent;
    border-bottom: 11px solid #ffffff;
}

#trace-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

#trace-modal-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: #6b7280;
    cursor: pointer;
    transition: all 0.15s ease;
}

#trace-modal-close:hover {
    color: #111827;
    background: rgba(0, 0, 0, 0.05);
}

/* ===== Bottom Panel ===== */
#panel {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    max-height: 50vh;
    background: #ffffff;
    border-top: 1px solid rgba(0, 0, 0, 0.08);
    z-index: 10;
    position: relative;
}

#panel-drag-handle {
    position: absolute;
    top: -12px;
    left: 0;
    right: 0;
    height: 24px;
    cursor: ns-resize;
    z-index: 11;
    touch-action: none;
}

#panel-drag-handle::after {
    content: '';
    position: absolute;
    top: 11px;
    left: 50%;
    transform: translateX(-50%);
    width: 38px;
    height: 4px;
    border-radius: 2px;
    background: rgba(0, 0, 0, 0.1);
    transition: background 0.15s ease;
}

#panel-drag-handle:hover::after,
#panel.is-resizing #panel-drag-handle::after {
    background: rgba(0, 0, 0, 0.25);
}

#collapse-panel-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: 4px;
    color: #9ca3af;
    cursor: pointer;
    transition: all 0.15s ease;
}

#collapse-panel-btn:hover {
    color: #374151;
    background: rgba(0, 0, 0, 0.05);
}

#panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 16px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
    flex-shrink: 0;
}

.panel-title-text {
    font-size: 13px;
    font-weight: 600;
    color: #374151;
}

#panel-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

#panel-tabs {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    padding: 2px;
    background: #f3f4f6;
    border-radius: 8px;
}

.panel-tab {
    padding: 4px 10px;
    font-size: 12px;
    font-weight: 600;
    font-family: inherit;
    color: #6b7280;
    background: transparent;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.panel-tab:hover {
    color: #374151;
}

.panel-tab.is-active {
    color: #111827;
    background: #ffffff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

#copy-raw-btn,
#copy-hops-btn {
    padding: 4px 10px;
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    color: #4b5563;
    background: #f9fafb;
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s ease;
}

#copy-raw-btn:hover,
#copy-hops-btn:hover {
    background: #f3f4f6;
    color: #111827;
}

#new-trace-btn {
    padding: 4px 12px;
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    color: #6366f1;
    background: transparent;
    border: 1px solid rgba(99, 102, 241, 0.3);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s ease;
}

#new-trace-btn:hover {
    background: rgba(99, 102, 241, 0.06);
    border-color: rgba(99, 102, 241, 0.5);
}

#panel-body {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex: 1;
    min-height: 0;
    padding: 0 16px 8px 16px;
}

/* ===== Map Button ===== */
.map-btn {
    position: fixed;
    z-index: 10;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    background: #fff;
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 10px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    cursor: pointer;
    color: #374151;
    transition: all 0.15s ease;
}

.map-btn:hover {
    background: #f9fafb;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

#overview-btn {
    top: calc(60px + var(--safe-top));
    right: 10px;
}

/* ===== Restore Button ===== */
#restore-btn {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 6px 0;
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    color: #6b7280;
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: none;
    border-top: 1px solid rgba(0, 0, 0, 0.06);
    cursor: pointer;
    transition: all 0.15s ease;
}

#restore-btn:hover {
    color: #374151;
    background: rgba(255, 255, 255, 0.98);
}

.input-row {
    display: flex;
    gap: 12px;
    align-items: stretch;
    flex: 1;
    min-height: 0;
}

#trace-input {
    flex: 1;
    padding: 10px 12px;
    font-size: 12px;
    font-family: "SF Mono", "Fira Code", "Fira Mono", Menlo, monospace;
    color: #1a1a2e;
    background: rgba(255, 255, 255, 0.7);
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    outline: none;
    resize: none;
    line-height: 1.5;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

#trace-input::placeholder { color: #9ca3af; }

#trace-input:focus {
    border-color: rgba(99, 102, 241, 0.4);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.08);
}

.input-buttons {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex-shrink: 0;
}

#example-btn {
    padding: 8px 16px;
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    color: #6366f1;
    background: transparent;
    border: 1px solid rgba(99, 102, 241, 0.3);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
}

#example-btn:hover {
    background: rgba(99, 102, 241, 0.06);
    border-color: rgba(99, 102, 241, 0.5);
}

#visualize-btn {
    padding: 8px 24px;
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    color: #ffffff;
    background: #6366f1;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s ease;
}

#visualize-btn:hover { background: #4f46e5; }
#visualize-btn:disabled { opacity: 0.5; cursor: not-allowed; }

#trace-modal-panel #trace-input {
    min-height: 220px;
}

#trace-modal-panel .input-row {
    flex-direction: column;
    gap: 10px;
}

#trace-modal-panel .input-buttons {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: flex-end;
}

@media (max-width: 720px) {
    #trace-modal-panel .input-buttons {
        flex-direction: row;
    }

    #trace-modal-panel #example-btn,
    #trace-modal-panel #visualize-btn {
        flex: 1;
    }
}

@media (prefers-reduced-motion: reduce) {
    #trace-modal-panel {
        transition: none;
    }
}

/* ===== Results View ===== */
#results-view {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

/* ===== Hop Table ===== */
#hop-table-wrap {
    margin: 0 -16px -12px -16px;
    flex: 1;
    min-height: 0;
    overflow: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

#raw-trace-wrap {
    margin: 8px 0 0 0;
    flex: 1;
    min-height: 0;
    border: 1px solid rgba(0, 0, 0, 0.06);
    border-radius: 10px;
    background: #fbfbfc;
    overflow: auto;
}

#raw-trace-pre {
    margin: 0;
    padding: 12px;
    font-size: 12px;
    line-height: 1.45;
    color: #111827;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
    white-space: pre-wrap;
    word-break: break-word;
}

#hop-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}

#hop-table thead th {
    text-align: left;
    padding: 5px 12px;
    font-size: 10px;
    font-weight: 600;
    color: #9ca3af;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
    white-space: nowrap;
    position: sticky;
    top: 0;
    z-index: 2;
    background: rgba(255, 255, 255, 0.98);
}

#hop-table tbody tr {
    cursor: pointer;
    transition: background 0.1s ease;
}

#hop-table tbody tr:hover {
    background: rgba(99, 102, 241, 0.05);
}

#hop-table tbody tr.hop-highlighted {
    background: rgba(99, 102, 241, 0.12);
}

#hop-table tbody tr.hop-timeout {
    opacity: 0.35;
    cursor: default;
}

#hop-table tbody td {
    padding: 5px 12px;
    color: #374151;
    border-bottom: 1px solid rgba(0, 0, 0, 0.03);
    white-space: nowrap;
}

#hop-table tbody td:first-child {
    font-weight: 600;
    color: #6b7280;
}

#hop-table .hop-color-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 6px;
    vertical-align: middle;
}

#hop-table .hop-ip,
#hop-table .hop-host {
    font-family: "SF Mono", "Fira Code", "Fira Mono", Menlo, monospace;
    font-size: 11px;
}

/* ===== Finish Dot (Checkered) ===== */
.finish-dot {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 2px solid #111827;
    background:
        repeating-conic-gradient(#111827 0% 25%, #ffffff 0% 50%) 50% / 5px 5px;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.3);
    pointer-events: none;
}

/* ===== MapLibre Popup Overrides ===== */
.maplibregl-popup-content {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 14px;
    border-radius: 12px;
    padding: 14px 16px;
    background: rgba(30, 30, 40, 0.92) !important;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    color: #f3f4f6 !important;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25), 0 1px 4px rgba(0, 0, 0, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.08);
    max-width: 320px;
}

.maplibregl-popup-anchor-bottom .maplibregl-popup-tip {
    border-top-color: rgba(30, 30, 40, 0.92) !important;
}
.maplibregl-popup-anchor-top .maplibregl-popup-tip {
    border-bottom-color: rgba(30, 30, 40, 0.92) !important;
}
.maplibregl-popup-anchor-left .maplibregl-popup-tip {
    border-right-color: rgba(30, 30, 40, 0.92) !important;
}
.maplibregl-popup-anchor-right .maplibregl-popup-tip {
    border-left-color: rgba(30, 30, 40, 0.92) !important;
}

.maplibregl-popup-close-button {
    font-size: 16px;
    padding: 4px 8px;
    color: rgba(255, 255, 255, 0.5);
    transition: color 0.15s ease;
}

.maplibregl-popup-close-button:hover {
    color: #ffffff;
    background: transparent;
}

.popup-title {
    font-size: 15px;
    font-weight: 600;
    color: #ffffff;
    margin: 0 0 6px 0;
    letter-spacing: -0.01em;
}

.popup-detail {
    font-size: 13px;
    color: rgba(255, 255, 255, 0.7);
    line-height: 1.5;
    margin: 0;
}

.popup-detail span { display: block; }

/* ===== MapLibre Controls Override ===== */
.maplibregl-ctrl-group {
    border-radius: 10px !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06) !important;
    border: 1px solid rgba(0, 0, 0, 0.08) !important;
    overflow: hidden;
}

.maplibregl-ctrl-group button {
    width: 36px !important;
    height: 36px !important;
}

body.is-panel-collapsed .maplibregl-ctrl-bottom-right,
body.is-panel-collapsed .maplibregl-ctrl-bottom-left {
    bottom: calc(58px + var(--safe-bottom));
}

@media (max-width: 768px) {
    #header {
        height: calc(52px + var(--safe-top));
        padding: var(--safe-top) 10px 0 10px;
    }

    .header-left {
        min-width: 0;
        gap: 6px;
    }

    .header-title {
        font-size: 18px;
        line-height: 1;
    }

    .header-title-separator,
    .header-title-product {
        display: none;
    }

    .header-right {
        gap: 6px;
    }

    #new-trace-btn,
    #banner-dismiss,
    #example-btn,
    #visualize-btn,
    #copy-raw-btn,
    #copy-hops-btn,
    .panel-tab {
        min-height: 36px;
    }

    #new-trace-btn {
        padding: 0 10px;
    }

    #map-warning {
        top: calc(62px + var(--safe-top));
        width: calc(100vw - 12px);
        max-width: none;
        font-size: 12px;
    }

    #overview-btn {
        top: calc(66px + var(--safe-top));
        right: 8px;
        width: 42px;
        height: 42px;
    }

    #trace-modal-panel {
        max-height: min(74vh, 560px);
    }

    #trace-modal-panel::before,
    #trace-modal-panel::after {
        display: none;
    }

    #trace-modal-panel #trace-input {
        min-height: 170px;
        font-size: 13px;
    }

    #panel {
        max-height: 72vh;
    }

    #panel-header {
        padding: 8px 10px;
        gap: 8px;
    }

    #panel-tabs {
        min-width: 0;
    }

    #panel-body {
        padding: 0 10px 8px 10px;
    }

    #hop-table {
        min-width: 760px;
        font-size: 11px;
    }

    #hop-table thead th,
    #hop-table tbody td {
        padding: 8px 10px;
    }

    #raw-trace-pre {
        font-size: 11px;
    }

    #restore-btn {
        min-height: 44px;
        padding: 8px 0 calc(8px + var(--safe-bottom));
    }

    body.is-panel-collapsed .maplibregl-ctrl-bottom-right,
    body.is-panel-collapsed .maplibregl-ctrl-bottom-left {
        bottom: calc(68px + var(--safe-bottom));
    }
}

@media (max-width: 768px) {
    #example-banner {
        display: none !important;
    }
}

@media (pointer: coarse) {
    #collapse-panel-btn,
    #trace-modal-close,
    #banner-close {
        min-width: 32px;
        min-height: 32px;
    }
}
