/* ============================================
   Telegram Mini App - 定位平台
   樣式表
   ============================================ */

:root {
    --tg-bg-color: var(--tg-theme-bg-color, #ffffff);
    --tg-text-color: var(--tg-theme-text-color, #222222);
    --tg-hint-color: var(--tg-theme-hint-color, #999999);
    --tg-link-color: var(--tg-theme-link-color, #2481cc);
    --tg-button-color: var(--tg-theme-button-color, #2481cc);
    --tg-button-text-color: var(--tg-theme-button-text-color, #ffffff);
    --tg-secondary-bg-color: var(--tg-theme-secondary-bg-color, #f0f0f0);
    --tg-header-bg-color: var(--tg-theme-header-bg-color, #ffffff);
    --tg-section-bg-color: var(--tg-theme-section-bg-color, #ffffff);
    --tg-accent-text-color: var(--tg-theme-accent-text-color, #2481cc);
    --tg-destructive-text-color: var(--tg-theme-destructive-text-color, #e53935);
    --tg-section-separator-color: var(--tg-theme-section-separator-color, #e0e0e0);

    --safe-area-inset-top: env(safe-area-inset-top, 0px);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
    --header-height: 52px;
    --bottom-panel-height: 280px;
    --border-radius: 12px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--tg-bg-color);
    color: var(--tg-text-color);
    user-select: none;
    -webkit-user-select: none;
}

#app {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
}

/* ============================================
   頂部導航欄
   ============================================ */

#header {
    height: var(--header-height);
    padding: 0 12px;
    padding-top: var(--safe-area-inset-top);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--tg-header-bg-color);
    border-bottom: 1px solid var(--tg-section-separator-color);
    z-index: 1000;
    flex-shrink: 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 500;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

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

.btn-icon {
    background: none;
    border: none;
    font-size: 22px;
    padding: 6px;
    cursor: pointer;
    border-radius: 8px;
    transition: background 0.2s;
    line-height: 1;
}

.btn-icon:active {
    background: var(--tg-secondary-bg-color);
}

/* ============================================
   地圖容器
   ============================================ */

#map {
    flex: 1;
    width: 100%;
    z-index: 1;
}

/* Leaflet 自訂 */
.leaflet-control-zoom {
    border: none !important;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15) !important;
    border-radius: 8px !important;
    overflow: hidden;
}

.leaflet-control-zoom a {
    width: 36px !important;
    height: 36px !important;
    line-height: 36px !important;
    font-size: 18px !important;
    background: var(--tg-secondary-bg-color) !important;
    color: var(--tg-text-color) !important;
    border: none !important;
}

.leaflet-control-zoom a:hover {
    background: var(--tg-section-bg-color) !important;
}

.leaflet-popup-content-wrapper {
    border-radius: 10px !important;
    background: var(--tg-section-bg-color) !important;
    color: var(--tg-text-color) !important;
    box-shadow: 0 4px 16px rgba(0,0,0,0.2) !important;
}

.leaflet-popup-tip {
    background: var(--tg-section-bg-color) !important;
}

/* 使用者自訂 marker */
.user-marker {
    width: 32px;
    height: 32px;
    background: var(--tg-button-color);
    border: 3px solid white;
    border-radius: 50%;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: white;
    transition: transform 0.3s ease;
}

.user-marker.pulsing {
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(36, 129, 204, 0.7); }
    70% { box-shadow: 0 0 0 12px rgba(36, 129, 204, 0); }
    100% { box-shadow: 0 0 0 0 rgba(36, 129, 204, 0); }
}

.friend-marker {
    width: 28px;
    height: 28px;
    background: #FF6B6B;
    border: 2px solid white;
    border-radius: 50%;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
}

/* 精準度圓圈 */
.accuracy-circle {
    fill: rgba(36, 129, 204, 0.1);
    stroke: rgba(36, 129, 204, 0.3);
    stroke-width: 1;
}

/* ============================================
   底部面板
   ============================================ */

#bottomPanel {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--bottom-panel-height);
    padding-bottom: var(--safe-area-inset-bottom);
    background: var(--tg-bg-color);
    border-top: 1px solid var(--tg-section-separator-color);
    border-radius: var(--border-radius) var(--border-radius) 0 0;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.1);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
    will-change: transform;
}

#bottomPanel.collapsed {
    transform: translateY(calc(100% - 50px));
}

.panel-tabs {
    display: flex;
    border-bottom: 1px solid var(--tg-section-separator-color);
    flex-shrink: 0;
}

.tab-btn {
    flex: 1;
    padding: 10px 8px;
    background: none;
    border: none;
    font-size: 12px;
    color: var(--tg-hint-color);
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
    white-space: nowrap;
}

.tab-btn.active {
    color: var(--tg-button-color);
    font-weight: 600;
}

.tab-btn.active::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 20%;
    right: 20%;
    height: 3px;
    background: var(--tg-button-color);
    border-radius: 3px 3px 0 0;
}

.tab-content {
    display: none;
    flex: 1;
    overflow-y: auto;
    padding: 12px 16px;
    -webkit-overflow-scrolling: touch;
}

.tab-content.active {
    display: block;
}

/* ============================================
   分享位置 Tab
   ============================================ */

.share-info {
    margin-bottom: 12px;
    padding: 10px 12px;
    background: var(--tg-secondary-bg-color);
    border-radius: 10px;
}

.status-text {
    font-size: 14px;
    margin-bottom: 4px;
    font-weight: 500;
}

.status-text.acquiring { color: var(--tg-hint-color); }
.status-text.active { color: #4CAF50; }
.status-text.error { color: var(--tg-destructive-text-color); }
.status-text.inactive { color: var(--tg-hint-color); }

.coords-text {
    font-size: 12px;
    color: var(--tg-hint-color);
    font-family: 'Courier New', monospace;
    margin-bottom: 2px;
}

.accuracy-text {
    font-size: 12px;
    color: var(--tg-hint-color);
}

.share-actions {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

.btn-primary {
    flex: 1;
    min-width: 120px;
    padding: 12px 16px;
    background: var(--tg-button-color);
    color: var(--tg-button-text-color);
    border: none;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s;
}

.btn-primary:active {
    opacity: 0.8;
}

.btn-secondary {
    flex: 1;
    min-width: 120px;
    padding: 12px 16px;
    background: var(--tg-secondary-bg-color);
    color: var(--tg-text-color);
    border: 1px solid var(--tg-section-separator-color);
    border-radius: 10px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-secondary:active {
    background: var(--tg-section-separator-color);
}

.btn-small {
    padding: 6px 12px;
    background: var(--tg-secondary-bg-color);
    color: var(--tg-destructive-text-color);
    border: none;
    border-radius: 6px;
    font-size: 12px;
    cursor: pointer;
}

.share-tips {
    margin-top: 8px;
}

.share-tips p {
    font-size: 11px;
    color: var(--tg-hint-color);
    margin-bottom: 4px;
    line-height: 1.4;
}

/* ============================================
   好友位置 Tab
   ============================================ */

#friendsList {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.friend-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    background: var(--tg-secondary-bg-color);
    border-radius: 10px;
    transition: background 0.2s;
}

.friend-item:active {
    background: var(--tg-section-separator-color);
}

.friend-item.placeholder {
    opacity: 0.6;
    justify-content: center;
}

.friend-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--tg-section-separator-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    flex-shrink: 0;
}

.friend-info {
    flex: 1;
    min-width: 0;
}

.friend-name {
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 2px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.friend-time {
    font-size: 11px;
    color: var(--tg-hint-color);
}

/* ============================================
   歷史記錄 Tab
   ============================================ */

.history-controls {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 8px;
}

#historyList {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.history-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 10px;
    background: var(--tg-secondary-bg-color);
    border-radius: 8px;
    font-size: 12px;
}

.history-item.placeholder {
    justify-content: center;
    color: var(--tg-hint-color);
    padding: 20px;
}

.history-coords {
    font-family: 'Courier New', monospace;
    color: var(--tg-hint-color);
}

.history-time {
    font-size: 11px;
    color: var(--tg-hint-color);
    flex-shrink: 0;
    margin-left: 8px;
}

/* ============================================
   模態彈窗
   ============================================ */

.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    width: 100%;
    max-width: 500px;
    max-height: 80vh;
    background: var(--tg-bg-color);
    border-radius: var(--border-radius) var(--border-radius) 0 0;
    overflow-y: auto;
    animation: slideUp 0.3s ease;
}

@keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid var(--tg-section-separator-color);
}

.modal-header h3 {
    font-size: 16px;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    color: var(--tg-hint-color);
    cursor: pointer;
    padding: 4px;
    line-height: 1;
}

.modal-body {
    padding: 16px;
}

.setting-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid var(--tg-section-separator-color);
}

.setting-item:last-child {
    border-bottom: none;
}

.setting-item label {
    font-size: 14px;
    font-weight: 500;
}

.setting-item select {
    padding: 6px 10px;
    border: 1px solid var(--tg-section-separator-color);
    border-radius: 8px;
    background: var(--tg-secondary-bg-color);
    color: var(--tg-text-color);
    font-size: 13px;
    outline: none;
}

/* Switch toggle */
.switch {
    position: relative;
    display: inline-block;
    width: 48px;
    height: 26px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #ccc;
    transition: 0.3s;
    border-radius: 26px;
}

.slider:before {
    content: '';
    position: absolute;
    height: 20px;
    width: 20px;
    left: 3px;
    bottom: 3px;
    background: white;
    transition: 0.3s;
    border-radius: 50%;
}

input:checked + .slider {
    background: var(--tg-button-color);
}

input:checked + .slider:before {
    transform: translateX(22px);
}

/* ============================================
   響應式調整
   ============================================ */

@media (max-width: 360px) {
    .tab-btn { font-size: 11px; padding: 8px 4px; }
    .btn-primary, .btn-secondary { font-size: 12px; padding: 10px 12px; }
}

@media (min-width: 768px) {
    #bottomPanel {
        max-width: 400px;
        left: auto;
        right: 12px;
        bottom: 12px;
        border-radius: var(--border-radius);
        height: 320px;
    }
}

/* ============================================
   載入動畫
   ============================================ */

.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--tg-bg-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    transition: opacity 0.3s ease;
}

.loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--tg-section-separator-color);
    border-top: 3px solid var(--tg-button-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 12px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-text {
    font-size: 14px;
    color: var(--tg-hint-color);
}
