@import url('https://fonts.googleapis.com/css2?family=Audiowide&family=Inter:wght@400;600;900&family=Noto+Sans+TC:wght@500;700&display=swap');

:root { 
    --primary: #6366f1; 
    --primary-dark: #4338ca; 
    --primary-glow: rgba(99, 102, 241, 0.5); 
    --bg-grad: #f3f4f6; 
    --bg-color: #f3f4f6; 
    --card-bg: rgba(255, 255, 255, 0.7); 
    --card-border: rgba(255, 255, 255, 0.85); 
    --text-main: #0f172a; 
    --text-muted: #64748b; 
    --shadow: 0 25px 60px -12px rgba(0,0,0,0.15); 
    --input-bg: rgba(255,255,255,0.7); 
    --radius: 32px; 
    --c-dl: #10b981; 
    --c-ul: #8b5cf6; 
    --error-color: #ef4444; 
}
[data-theme="dark"] { 
    --primary: #818cf8; 
    --primary-dark: #6366f1; 
    --primary-glow: rgba(129, 140, 248, 0.4); 
    --bg-grad: #020617; 
    --bg-color: #020617; 
    --card-bg: rgba(30, 41, 59, 0.65); 
    --card-border: rgba(255, 255, 255, 0.08); 
    --text-main: #f8fafc; 
    --text-muted: #94a3b8; 
    --shadow: 0 25px 60px -12px rgba(0,0,0,0.5); 
    --input-bg: rgba(15, 23, 42, 0.6); 
    --c-dl: #34d399; 
    --c-ul: #a78bfa; 
    --error-color: #f87171;
}

* { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Inter', system-ui, sans-serif; -webkit-tap-highlight-color: transparent; }

body, button, select, input {
    font-family: 'Inter', 'Noto Sans TC', system-ui, -apple-system, sans-serif;
}

html { 
    background-color: var(--bg-color);
    background-image: none;
    height: 100%; 
    width: 100%;
}

body { 
    min-height: 100dvh; 
    height: 100%;
    width: 100%;
    background-color: var(--bg-color);
    color: var(--text-main); 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    padding: 20px; 
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    overflow: auto; 
}

.app-container { 
    width: 100%; 
    max-width: 680px; 
    background: var(--card-bg); 
    backdrop-filter: blur(40px) saturate(160%); 
    -webkit-backdrop-filter: blur(40px) saturate(160%); 
    border: 1px solid var(--card-border); 
    border-radius: var(--radius); 
    box-shadow: var(--shadow); 
    display: flex; flex-direction: column; 
    justify-content: center;
    gap: 20px;
    
    animation: fadeIn 0.8s cubic-bezier(0.16, 1, 0.3, 1); 
    padding: 30px 40px;
    position: relative; 
    transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
    will-change: transform, opacity;
    
    height: auto;
    min-height: 580px;
    max-height: 95vh;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(30px); } to { opacity: 1; transform: translateY(0); } }

header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 5px; flex-shrink: 0; }
h1 { font-family: 'Audiowide', cursive; font-size: 2rem; background: linear-gradient(135deg, var(--text-main), var(--primary)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin: 0; letter-spacing: 1px; filter: drop-shadow(0 2px 8px var(--primary-glow)); }
.theme-btn { background: var(--input-bg); border: 1px solid var(--card-border); width: 40px; height: 40px; border-radius: 12px; cursor: pointer; font-size: 1.2rem; display: flex; align-items: center; justify-content: center; transition: all 0.3s ease; }
.theme-btn:hover { background: var(--card-border); transform: rotate(15deg) scale(1.05); }

.status-bar { 
    display: flex; justify-content: space-between; margin-bottom: 5px; 
    font-size: 0.95rem; color: var(--text-muted); background: var(--input-bg); 
    padding: 15px 24px; border-radius: 20px; border: 1px solid var(--card-border); align-items: center; 
    box-shadow: inset 0 2px 8px rgba(0,0,0,0.02);
    flex-shrink: 0;
}
.status-group { display: flex; flex-direction: column; }
.status-group.start { align-items: flex-start; text-align: left; }
.status-group.end { align-items: flex-end; text-align: right; }

.status-label { font-size: 0.7em; text-transform: uppercase; margin-bottom: 4px; opacity: 0.7; display: block; font-weight: 800; letter-spacing: 0.5px; }
.status-val { font-weight: 700; color: var(--text-main); font-size: 1.1em; }

.val-provider { 
    max-width: 250px; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
}

.gauge-wrapper { 
    position: relative; 
    width: 100%; 
    max-width: 400px;
    margin: 5px auto; 
    transition: all 0.6s cubic-bezier(0.4, 0, 0.2, 1); 
    opacity: 1; 
    transform: scale(1); 
    transform-origin: center top; 
    overflow: visible; 
    will-change: height, margin, opacity;
    
    flex: 1; 
    min-height: 160px; 
    max-height: 35vh;
    
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.gauge-wrapper.hidden { 
    height: 0; 
    margin-top: 0; 
    margin-bottom: 0; 
    opacity: 0; 
    transform: scale(0.95);
    min-height: 0; 
    flex: 0;
}

.gauge-bg { fill: none; stroke: var(--text-muted); stroke-width: 12; stroke-linecap: round; opacity: 0.1; }
.gauge-progress { 
    fill: none; stroke: url(#gradient); stroke-width: 12; stroke-linecap: round; 
    stroke-dasharray: 503; 
    stroke-dashoffset: 503; 
    transition: stroke-dashoffset 0.1s linear; 
    filter: drop-shadow(0 0 15px var(--primary-glow)); 
    will-change: stroke-dashoffset;
}
.gauge-tick-label { fill: var(--text-muted); font-size: 10px; font-weight: 700; font-family: 'Inter', sans-serif; opacity: 0.7; }

.speed-display { 
    position: absolute; 
    bottom: 20px; 
    left: 0; 
    width: 100%; 
    text-align: center; 
    pointer-events: none; 
    will-change: transform;
}
.speed-value { 
    font-size: clamp(2rem, 6vmin, 4.5rem);
    font-weight: 900; 
    line-height: 1; 
    color: var(--text-main); 
    text-shadow: 0 0 35px var(--primary-glow); 
    letter-spacing: -2px;
    font-variant-numeric: tabular-nums;
    transform: translateY(0); 
    transition: color 0.3s ease;
}
.speed-unit { font-size: 1rem; color: var(--primary); font-weight: 800; letter-spacing: 1px; opacity: 0.9; display: block; margin-top: 5px; text-transform: none; }

.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; margin: 10px 0 15px 0; transition: margin 0.6s cubic-bezier(0.4, 0, 0.2, 1); flex-shrink: 0; }
.grid.result-mode { margin: 20px 0 30px 0; }

.info-item { 
    background: var(--input-bg); padding: 18px 10px; border-radius: 20px; 
    border: 1px solid var(--card-border); text-align: center; transition: all 0.3s ease; 
    display: flex; flex-direction: column; justify-content: center; position: relative; overflow: hidden;
}
.info-item:hover { transform: translateY(-4px); box-shadow: 0 12px 25px -5px rgba(0,0,0,0.1); border-color: var(--primary-glow); }
.info-item.active { border-color: var(--primary); box-shadow: 0 0 25px var(--primary-glow); transform: translateY(-2px); }
.info-label { font-size: 0.75rem; text-transform: uppercase; color: var(--text-muted); margin-bottom: 4px; font-weight: 800; letter-spacing: 0.5px; }
.info-val { 
    font-size: clamp(1.3rem, 3vw, 1.6rem);
    font-weight: 800; color: var(--text-main); 
}
.info-sub { font-size: 0.8rem; color: var(--text-muted); margin-top: 4px; font-weight: 600; }
.jitter-tag { font-size: 0.7rem; color: var(--text-muted); background: rgba(125,125,125,0.08); padding: 3px 8px; border-radius: 10px; margin-top: 6px; display: inline-block; font-weight: 700; }
#dlText { color: var(--c-dl); }
#ulText { color: var(--c-ul); }

.btn-start { 
    width: 100%; padding: 20px; border: none; border-radius: 24px; 
    background: linear-gradient(135deg, var(--primary), var(--primary-dark)); 
    color: #fff; font-family: 'Audiowide', cursive; 
    font-size: clamp(1.2rem, 3vw, 1.5rem);
    cursor: pointer; transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1); letter-spacing: 2px; 
    box-shadow: 0 12px 35px var(--primary-glow); position: relative; overflow: hidden; flex-shrink: 0;
    margin-top: 10px; 
}
.btn-start::after { content:''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: linear-gradient(rgba(255,255,255,0.2), transparent); opacity: 0; transition: opacity 0.3s; }
.btn-start:hover:not(:disabled) { transform: translateY(-3px) scale(1.01); box-shadow: 0 18px 45px var(--primary-glow); }
.btn-start:hover::after { opacity: 1; }
.btn-start:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; filter: grayscale(1); }

.progress-container { width: 100%; height: 6px; background: rgba(0,0,0,0.05); margin-top: 25px; border-radius: 10px; overflow: hidden; flex-shrink: 0; }
.progress-bar { height: 100%; background: var(--primary); width: 0%; transition: width 0.3s linear; box-shadow: 0 0 15px var(--primary); will-change: width; }

.fab-settings, .fab-info {
    position: fixed; bottom: 35px; width: 52px; height: 52px; 
    background: var(--input-bg); border: 1px solid var(--card-border); border-radius: 50%; 
    display: flex; justify-content: center; align-items: center; 
    color: var(--text-muted); cursor: pointer; box-shadow: 0 2px 5px rgba(0,0,0,0.05); z-index: 100; 
    transition: all 0.2s ease; 
}
.fab-settings { right: 35px; }
.fab-info { left: 35px; display: none; }
.fab-settings svg, .fab-info svg { width: 24px; height: 24px; stroke-width: 2px; }
.fab-settings:hover, .fab-info:hover { transform: scale(1.05); color: var(--primary); border-color: var(--primary); background: var(--card-bg); }

.modal-backdrop {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.3); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); z-index: 999; display: flex; justify-content: center; align-items: center; opacity: 0; pointer-events: none; transition: opacity 0.3s ease;
}
.modal-backdrop.open { opacity: 1; pointer-events: auto; }

.settings-panel {
    background: var(--card-bg); border: 1px solid var(--card-border); padding: 35px; border-radius: 32px; width: 90%; max-width: 380px; box-shadow: 0 25px 70px rgba(0,0,0,0.25); transform: scale(0.95) translateY(20px); transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}
.modal-backdrop.open .settings-panel { transform: scale(1) translateY(0); }
.settings-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px; font-weight: 800; font-size: 1.4rem; letter-spacing: -0.5px; }
.close-modal { background: none; border: none; font-size: 1.6rem; cursor: pointer; color: var(--text-muted); transition: 0.2s; }
.close-modal:hover { color: var(--text-main); transform: scale(1.1); }

.toggle-group { display: flex; background: var(--input-bg); border: 1px solid var(--card-border); border-radius: 18px; padding: 6px; width: 100%; }
.toggle-opt { flex: 1; padding: 12px; border-radius: 14px; font-size: 0.95rem; font-weight: 700; cursor: pointer; color: var(--text-muted); transition: 0.2s; border: none; background: transparent; }
.toggle-opt.selected { background: var(--card-bg); color: var(--primary); box-shadow: 0 4px 15px rgba(0,0,0,0.08); }
.full-select { width: 100%; padding: 15px; border-radius: 18px; border: 1px solid var(--card-border); background: var(--input-bg); color: var(--text-main); font-weight: 700; outline: none; transition: 0.2s; -webkit-appearance: none; cursor: pointer; font-size: 0.95rem; }
.full-select:hover { border-color: var(--primary); }

.info-row { display: flex; justify-content: space-between; margin-bottom: 20px; align-items: center; }
.info-key { font-weight: 700; color: var(--text-muted); font-size: 0.9rem; text-transform: uppercase; }
.info-value { font-weight: 700; color: var(--text-main); font-size: 1.1rem; text-align: right; max-width: 60%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

@media (max-height: 800px) {
    .app-container { padding: 15px 20px; min-height: auto; gap: 10px; max-height: 100vh; justify-content: center; }
    .gauge-wrapper { min-height: 100px; flex: 0 1 auto; margin: 0 auto; }
    .speed-value { font-size: 2.8rem; }
    .grid { margin: 10px 0; }
    h1 { font-size: 1.5rem; }
    .btn-start { padding: 12px; margin-top: 5px; font-size: 1.1rem; }
    .status-bar { padding: 10px 15px; }
    .progress-container { margin-top: 10px; }
}

@media (max-width: 768px) {
    html { overflow-y: hidden; overflow-x: hidden; position: fixed; width: 100%; height: 100%; }
    body { padding: 10px; display: flex; width: 100%; height: 100%; min-height: 0; overflow: hidden; }
    
    .app-container { 
        padding: 24px 20px 85px 20px;
        width: 100%;
        height: 100%;
        max-width: none; 
        border-radius: 36px;
        border: 1px solid var(--card-border);
        box-shadow: 0 0 40px -10px rgba(0,0,0,0.2);
        margin: 0 auto;
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 15px; 
        overflow: hidden;
    } 

    header { margin-bottom: 5px; flex-shrink: 0; }

    .gauge-wrapper { 
        width: 100%; 
        height: auto;
        flex: 1;
        min-height: 0;
        margin: 10px auto; 
    }
    
    .speed-value { font-size: clamp(2rem, 12vmin, 4rem); }
    .speed-display { bottom: 0; } 
    .speed-unit { margin-top: 2px; font-size: 0.9rem; text-transform: none; letter-spacing: 0.5px; }
    
    .status-bar { display: none !important; }
    
    .fab-info { display: flex; left: 20px; bottom: 20px; }
    .fab-settings { right: 20px; bottom: 20px; }

    .grid { 
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 12px; 
        margin: 10px 0 10px 0;
        flex-shrink: 0;
    }
    
    .grid.result-mode { margin: 15px 0 25px 0; }

    #card-ping {
        grid-column: span 2; 
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        padding: 14px 20px;
        text-align: left;
    }

    .ping-group { display: flex; flex-direction: column; align-items: flex-start; }
    #card-ping .info-label { margin-bottom: 0; text-transform: none; }
    #card-ping .info-val { font-size: 1.4rem; }
    #card-ping .jitter-tag { margin-top: 0; margin-left: 0; font-size: 0.8rem; background: none; padding: 0; }
    .jitter-wrapper { text-align: right; }

    #card-download, #card-upload {
        padding: 12px;
        aspect-ratio: auto;
        height: 90px;
        justify-content: center;
    }
    
    .info-val { font-size: 1.35rem; }

    .btn-start { 
        padding: 18px; 
        font-size: 1.3rem; 
        border-radius: 24px;
        box-shadow: 0 15px 40px -5px var(--primary-glow);
        margin-top: 10px;
        flex-shrink: 0;
    }
    
    .progress-container { margin-top: 15px; flex-shrink: 0; }

    .info-value { white-space: normal; word-break: break-word; }
}