/* =========================================================
   Global
========================================================= */

* {
    box-sizing: border-box;
}

body {
    margin: 0;

    min-height: 100vh;

    font-family:
        Arial,
        "Microsoft JhengHei",
        sans-serif;

    background: #f2f4f7;

    display: flex;

    align-items: center;

    justify-content: center;
}


/* =========================================================
   Main Container
========================================================= */

.container {
    width: 100%;

    max-width: 650px;

    padding: 20px;
}


/* =========================================================
   SpeedTest Card
========================================================= */

.speedtest-card {
    width: 100%;

    background: #ffffff;

    border-radius: 20px;

    padding: 45px 35px;

    text-align: center;

    box-shadow:
        0 10px 40px rgba(0, 0, 0, 0.08);
}


/* =========================================================
   Title
========================================================= */

h1 {
    margin: 0;

    font-size: 42px;
}

.subtitle {
    color: #777777;

    margin-top: 10px;

    margin-bottom: 35px;
}


/* =========================================================
   Speed Display
========================================================= */

.speed-display {
    margin-bottom: 15px;
}

.speed-hidden {
    display: none;
}

.speed-display #speed {
    font-size: 70px;

    font-weight: 700;

    line-height: 1;
}

.speed-display span {
    font-size: 18px;

    color: #777777;
}


/* =========================================================
   Status
========================================================= */

.status {
    margin: 20px 0;

    color: #666666;
}


/* =========================================================
   Server Selector
========================================================= */

.server-selector {
    width: 100%;

    margin: 15px auto 12px;

    text-align: left;
}

.server-selector label {
    display: block;

    margin-bottom: 8px;

    color: #666666;

    font-size: 14px;

    font-weight: 600;
}

.server-selector select {
    width: 100%;

    height: 46px;

    padding: 0 14px;

    border: 1px solid #dddddd;

    border-radius: 10px;

    background: #ffffff;

    color: #222222;

    font-size: 15px;

    font-family: inherit;

    outline: none;

    cursor: pointer;
}

.server-selector select:focus {
    border-color: #999999;
}

.server-selector select:disabled {
    background: #f5f5f5;

    color: #999999;

    cursor: not-allowed;
}


/* =========================================================
   Server Info
========================================================= */

.server-info {
    margin: 12px auto 25px;

    padding: 11px 16px;

    width: fit-content;

    max-width: 100%;

    min-width: 220px;

    background: #f7f7f7;

    border-radius: 12px;

    display: flex;

    align-items: center;

    justify-content: center;

    gap: 8px;

    flex-wrap: wrap;

    font-size: 14px;
}

.server-label {
    color: #888888;
}

.server-info strong {
    color: #222222;

    font-size: 15px;
}

.server-location {
    color: #888888;
}

.server-location::before {
    content: "所在地";

    margin-right: 8px;

    color: #aaaaaa;
}


/* =========================================================
   Progress
========================================================= */

.progress {
    width: 100%;

    height: 8px;

    background: #e5e7eb;

    border-radius: 10px;

    overflow: hidden;

    margin-bottom: 30px;
}

.progress-bar {
    width: 0%;

    height: 100%;

    background: #333333;

    transition: width 0.15s;
}


/* =========================================================
   Button
========================================================= */

button {
    width: 100%;

    padding: 16px;

    border: 0;

    border-radius: 12px;

    background: #111111;

    color: #ffffff;

    font-size: 18px;

    font-weight: 600;

    font-family: inherit;

    cursor: pointer;

    transition:
        opacity 0.2s ease,
        transform 0.15s ease;
}

button:hover {
    opacity: 0.85;
}

button:active {
    transform: scale(0.99);
}

button:disabled {
    opacity: 0.5;

    cursor: not-allowed;
}


/* =========================================================
   Results
========================================================= */

.results {
    display: grid;

    grid-template-columns:
        repeat(3, 1fr);

    gap: 15px;

    margin-top: 35px;
}

.result-box {
    background: #f7f7f7;

    border-radius: 12px;

    padding: 20px 10px;
}

.result-box span {
    display: block;

    color: #777777;

    font-size: 14px;

    margin-bottom: 10px;
}

.result-box strong {
    font-size: 24px;
}

.result-box small {
    color: #777777;
}


/* =========================================================
   Speed Chart
========================================================= */

.speed-chart-container {
    width: 100%;

    margin-top: 30px;

    padding: 20px;

    background: #ffffff;

    border-radius: 16px;

    border: 1px solid #eeeeee;

    box-shadow:
        0 4px 15px rgba(0, 0, 0, 0.05);
}

.chart-title {
    margin-bottom: 15px;

    font-size: 17px;

    font-weight: 700;

    color: #222222;

    text-align: left;
}

.chart-wrapper {
    position: relative;

    width: 100%;

    height: 300px;
}

.chart-wrapper canvas {
    width: 100% !important;

    height: 100% !important;
}


/* =========================================================
   History Container
========================================================= */

.history-container {
    width: 100%;

    margin-top: 30px;

    padding: 20px;

    background: #ffffff;

    border-radius: 16px;

    border: 1px solid #eeeeee;

    box-shadow:
        0 4px 15px rgba(0, 0, 0, 0.05);
}


/* =========================================================
   History Header
========================================================= */

.history-header {
    display: flex;

    align-items: center;

    justify-content: space-between;

    gap: 15px;

    margin-bottom: 15px;
}

.history-title {
    font-size: 17px;

    font-weight: 700;

    color: #222222;
}

.history-ip {
    margin-top: 5px;

    font-size: 13px;

    color: #777777;
}

.history-ip strong {
    color: #333333;

    font-weight: 600;
}


/* =========================================================
   History Refresh Button
========================================================= */

.history-refresh {
    width: auto !important;

    min-width: 0;

    flex-shrink: 0;

    padding: 8px 14px;

    border: none;

    border-radius: 8px;

    background: #f3f3f3;

    color: #333333;

    font-size: 13px;

    font-weight: 500;

    font-family: inherit;

    cursor: pointer;

    white-space: nowrap;
}

.history-refresh:hover {
    background: #e7e7e7;

    opacity: 1;
}

.history-refresh:active {
    transform: scale(0.97);
}


/* =========================================================
   History Table Wrapper
========================================================= */

.history-table-wrapper {
    width: 100%;

    overflow-x: auto;

    -webkit-overflow-scrolling: touch;
}


/* =========================================================
   History Table - Desktop
========================================================= */

.history-table {
    width: 100%;

    border-collapse: collapse;

    font-size: 13px;

    table-layout: auto;
}

.history-table thead {
    background: #f7f7f7;
}

.history-table th {
    padding: 12px 8px;

    text-align: center;

    white-space: nowrap;

    font-weight: 600;

    color: #555555;

    border-bottom: 1px solid #e5e5e5;
}

.history-table td {
    padding: 12px 8px;

    text-align: center;

    white-space: nowrap;

    color: #333333;

    border-bottom: 1px solid #eeeeee;

    vertical-align: middle;
}

.history-table tbody tr {
    transition: background 0.15s ease;
}

.history-table tbody tr:hover {
    background: #fafafa;
}


/* =========================================================
   History Column Width
   ---------------------------------------------------------
   不使用 table-layout: fixed
   避免桌面版文字被硬壓在一起
========================================================= */

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

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

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

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

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

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

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


/* =========================================================
   History Speed
========================================================= */

.history-download {
    color: #2196F3 !important;

    font-weight: 700;
}

.history-upload {
    color: #FF5722 !important;

    font-weight: 700;
}

.history-ping {
    font-weight: 600;
}


/* =========================================================
   History Server
========================================================= */

.history-table td:nth-child(2) {
    white-space: normal;

    line-height: 1.4;
}

.history-table td small {
    color: #999999;

    font-size: 11px;
}


/* =========================================================
   Loading / Empty / Error
========================================================= */

.history-loading,
.history-empty {
    padding: 25px !important;

    color: #999999 !important;

    text-align: center !important;
}

.history-error {
    padding: 25px !important;

    color: #d32f2f !important;

    text-align: center !important;
}


/* =========================================================
   Mobile
========================================================= */

@media (max-width: 600px) {

    /* -----------------------------------------------------
       Container
    ----------------------------------------------------- */

    .container {
        width: 100%;

        padding: 12px;
    }


    /* -----------------------------------------------------
       SpeedTest Card
    ----------------------------------------------------- */

    .speedtest-card {
        width: 100%;

        padding: 30px 20px;

        border-radius: 16px;
    }


    /* -----------------------------------------------------
       Title
    ----------------------------------------------------- */

    h1 {
        font-size: 34px;
    }

    .subtitle {
        margin-bottom: 25px;

        font-size: 14px;
    }


    /* -----------------------------------------------------
       Speed
    ----------------------------------------------------- */

    .speed-display #speed {
        font-size: 55px;
    }

    .speed-display span {
        font-size: 16px;
    }


    /* -----------------------------------------------------
       Server Selector
    ----------------------------------------------------- */

    .server-selector {
        margin-top: 10px;
    }

    .server-selector select {
        height: 44px;

        font-size: 14px;
    }


    /* -----------------------------------------------------
       Server Info
    ----------------------------------------------------- */

    .server-info {
        width: 100%;

        min-width: 0;

        margin-bottom: 20px;

        padding: 10px 12px;

        font-size: 13px;
    }

    .server-info strong {
        font-size: 14px;
    }


    /* -----------------------------------------------------
       Results
    ----------------------------------------------------- */

    .results {
        grid-template-columns: 1fr;

        gap: 10px;

        margin-top: 25px;
    }

    .result-box {
        padding: 16px 10px;
    }

    .result-box strong {
        font-size: 22px;
    }


    /* -----------------------------------------------------
       Speed Chart
    ----------------------------------------------------- */

    .speed-chart-container {
        margin-top: 20px;

        padding: 15px;

        border-radius: 12px;
    }

    .chart-title {
        margin-bottom: 12px;

        font-size: 16px;
    }

    .chart-wrapper {
        height: 240px;
    }


    /* =====================================================
       History
    ===================================================== */

    .history-container {
        width: 100%;

        margin-top: 20px;

        padding: 15px;

        border-radius: 12px;
    }


    /* -----------------------------------------------------
       History Header
    ----------------------------------------------------- */

    .history-header {
        display: flex;

        align-items: flex-start;

        justify-content: space-between;

        gap: 8px;

        margin-bottom: 15px;
    }

    .history-title {
        font-size: 16px;
    }

    .history-ip {
        max-width: 100%;

        margin-top: 4px;

        font-size: 12px;

        line-height: 1.4;

        word-break: break-all;
    }


    /* -----------------------------------------------------
       Refresh Button
    ----------------------------------------------------- */

    .history-refresh {
        width: auto !important;

        flex-shrink: 0;

        padding: 7px 11px;

        font-size: 12px;
    }


    /* -----------------------------------------------------
       Table Wrapper
    ----------------------------------------------------- */

    .history-table-wrapper {
        width: 100%;

        overflow: visible;
    }


    /* -----------------------------------------------------
       Table → Mobile Card
    ----------------------------------------------------- */

    .history-table {
        display: block;

        width: 100%;

        border-collapse: separate;

        font-size: 12px;
    }


    .history-table thead {
        display: none;
    }


    .history-table tbody {
        display: block;

        width: 100%;
    }


    /* -----------------------------------------------------
       每一筆紀錄
    ----------------------------------------------------- */

    .history-table tbody tr {
        display: block;

        width: 100%;

        margin: 0 0 12px 0;

        padding: 9px 12px;

        background: #f7f7f7;

        border: 1px solid #eeeeee;

        border-radius: 12px;

        overflow: hidden;
    }

    .history-table tbody tr:last-child {
        margin-bottom: 0;
    }

    .history-table tbody tr:hover {
        background: #f7f7f7;
    }


    /* -----------------------------------------------------
       清除 Desktop 欄位寬度
    ----------------------------------------------------- */

    .history-table th:nth-child(1),
    .history-table td:nth-child(1),

    .history-table th:nth-child(2),
    .history-table td:nth-child(2),

    .history-table th:nth-child(3),
    .history-table td:nth-child(3),

    .history-table th:nth-child(4),
    .history-table td:nth-child(4),

    .history-table th:nth-child(5),
    .history-table td:nth-child(5),

    .history-table th:nth-child(6),
    .history-table td:nth-child(6),

    .history-table th:nth-child(7),
    .history-table td:nth-child(7) {

        width: 100%;
    }


    /* -----------------------------------------------------
       每一個欄位
    ----------------------------------------------------- */

    .history-table tbody td {
        display: flex;

        align-items: flex-start;

        justify-content: space-between;

        width: 100%;

        min-width: 0;

        gap: 12px;

        padding: 8px 0;

        border-bottom: 1px solid #e5e5e5;

        text-align: right;

        white-space: normal;

        line-height: 1.4;

        overflow-wrap: anywhere;
    }


    .history-table tbody td:last-child {
        border-bottom: none;
    }


    /* -----------------------------------------------------
       欄位名稱
    ----------------------------------------------------- */

    .history-table tbody td::before {
        flex-shrink: 0;

        color: #888888;

        font-size: 12px;

        font-weight: 600;

        text-align: left;

        white-space: nowrap;
    }


    /* -----------------------------------------------------
       欄位名稱
    ----------------------------------------------------- */

    .history-table tbody td:nth-child(1)::before {
        content: "測試時間";
    }

    .history-table tbody td:nth-child(2)::before {
        content: "測速伺服器";
    }

    .history-table tbody td:nth-child(3)::before {
        content: "Ping (延遲)";
    }

    .history-table tbody td:nth-child(4)::before {
        content: "Jitter (抖動)";
    }

    .history-table tbody td:nth-child(5)::before {
        content: "Download (下載速度)";
    }

    .history-table tbody td:nth-child(6)::before {
        content: "Upload (上傳速度)";
    }

    .history-table tbody td:nth-child(7)::before {
        content: "測試耗時";
    }


    /* -----------------------------------------------------
       日期
    ----------------------------------------------------- */

    .history-table tbody td:nth-child(1) {
        word-break: break-word;
    }


    /* -----------------------------------------------------
       Server
    ----------------------------------------------------- */

    .history-table tbody td:nth-child(2) {
        word-break: break-word;

        white-space: normal;
    }


    .history-table tbody td:nth-child(2) small {
        font-size: 11px;

        color: #999999;
    }


    /* -----------------------------------------------------
       速度
    ----------------------------------------------------- */

    .history-table tbody td.history-download,
    .history-table tbody td.history-upload {

        white-space: nowrap;

        font-size: 12px;
    }


    /* -----------------------------------------------------
       Loading / Empty / Error
    ----------------------------------------------------- */

    .history-table tbody td.history-loading,
    .history-table tbody td.history-empty,
    .history-table tbody td.history-error {

        display: block;

        width: 100%;

        padding: 20px 10px !important;

        text-align: center !important;

        border-bottom: none;
    }

    .history-table tbody td.history-loading::before,
    .history-table tbody td.history-empty::before,
    .history-table tbody td.history-error::before {

        display: none;
    }

}


/* =========================================================
   Very Small Mobile
========================================================= */

@media (max-width: 380px) {

    .container {
        padding: 8px;
    }

    .speedtest-card {
        padding: 25px 15px;
    }

    h1 {
        font-size: 30px;
    }

    .speed-display #speed {
        font-size: 48px;
    }


    /* -----------------------------------------------------
       History
    ----------------------------------------------------- */

    .history-container {
        padding: 12px;
    }

    .history-header {
        gap: 6px;
    }

    .history-title {
        font-size: 15px;
    }

    .history-ip {
        font-size: 11px;
    }

    .history-refresh {
        padding: 6px 9px;

        font-size: 11px;
    }

    .history-table tbody tr {
        padding: 9px 11px;
    }

    .history-table tbody td {
        gap: 8px;

        padding: 7px 0;

        font-size: 11px;
    }

    .history-table tbody td::before {
        font-size: 11px;
    }

    .history-table tbody td.history-download,
    .history-table tbody td.history-upload {
        font-size: 11px;
    }

}