/* 基礎重置 */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Microsoft JhengHei', 'Segoe UI', sans-serif;
    line-height: 1.6;
    background: #f8f9fa;
    color: #333;
}

.container {
    max-width: 1200px;
    margin: 2rem auto;
    padding: 0 1rem;
}

/* 搜尋框樣式 */
.search-box {
    margin: 2rem 0;
}

.search-box form {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
}

.input-wrapper {
    position: relative;
    width: 60%;
}

.search-box input[type="text"] {
    width: 100%;
    padding: 0.8rem;
    padding-right: 2.5rem; /* 為清除按鈕留出空間 */
    border: 2px solid #6c757d;
    border-radius: 25px;
    font-size: 1.1rem;
    transition: all 0.3s ease;
}

.clear-btn {
    position: absolute;
    top: 50%;
    right: 0.5rem;
    transform: translateY(-50%);
    background: none;
    border: none;
    font-size: 1.5rem;
    line-height: 1;
    color: #6c757d;
    cursor: pointer;
    padding: 0 0.5rem;
    display: none; /* 預設隱藏 */
}

.search-box input[type="text"]:focus {
    outline: none;
    border-color: #6f42c1;
    box-shadow: 0 0 8px rgba(111, 66, 193, 0.3);
}

.search-box button[type="submit"] {
    padding: 0.8rem 2rem;
    background: #6f42c1;
    color: white;
    border: none;
    border-radius: 25px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.search-box button[type="submit"]:hover {
    background: #5a32a3;
    transform: translateY(-2px);
}

/* 控制項容器 (每頁筆數選擇) */
.controls-container {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 1rem;
}

.per-page-selector {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
    color: #495057;
}

.per-page-selector select {
    padding: 0.4rem 0.6rem;
    border: 1px solid #ced4da;
    border-radius: 5px;
    background-color: #fff;
    font-size: 0.9rem;
    cursor: pointer;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.per-page-selector select:focus {
    outline: none;
    border-color: #6f42c1;
    box-shadow: 0 0 0 0.2rem rgba(111, 66, 193, 0.25);
}

/* 表格容器 */
.table-container {
    background: white;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    overflow-x: auto;
}

/* 表格樣式 */
table {
    width: 100%;
    border-collapse: collapse;
    background: white;
    min-width: 600px;
}

thead {
    position: sticky;
    top: 0;
    z-index: 1;
}

th, td {
    padding: 1rem;
    text-align: left;
    border-bottom: 1px solid #dee2e6;
}

th {
    background: #6f42c1;
    color: white;
    font-weight: 600;
    position: sticky;
    top: 0;
}

/* 移除可排序欄位的 padding，交給按鈕處理，以擴大點擊範圍 */
th[data-sort] {
    padding: 0;
    transition: background-color 0.2s ease;
}

th[data-sort]:hover {
    background-color: #5a32a3;
}

.sort-btn {
    background: none;
    border: none;
    color: inherit;
    font: inherit;
    cursor: pointer;
    width: 100%;
    height: 100%;
    padding: 1rem; /* 恢復原有的 padding */
    text-align: left;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

/* 排序指示器 (使用 Unicode 箭頭) */
.sort-btn::after {
    content: ' \2195'; /* Up-Down Arrow */
    opacity: 0.6;
    font-weight: normal;
    transition: opacity 0.2s ease;
}

th[data-sort]:hover .sort-btn::after,
th.sort-asc .sort-btn::after,
th.sort-desc .sort-btn::after {
    opacity: 1;
}

th.sort-asc .sort-btn::after {
    content: ' \25B2'; /* Up Arrow */
}

th.sort-desc .sort-btn::after {
    content: ' \25BC'; /* Down Arrow */
}

tbody tr:nth-child(even) {
    background-color: #f8f9fa;
}

tbody tr:hover {
    background-color: rgba(111, 66, 193, 0.05);
}

/* 搜尋結果高亮樣式 */
mark {
    background-color: #fff3cd; /* 使用柔和的黃色，比預設更不刺眼 */
    color: #664d03;
    padding: 0.1em 0.2em;
    border-radius: 3px;
}

/* 載入中動畫 (Spinner) */
#loading-indicator td {
    text-align: center;
    padding: 4rem 1rem;
    border: none;
    background-color: #fff !important; /* 覆蓋 tr:nth-child(even) 的樣式 */
}

.spinner {
    width: 36px;
    height: 36px;
    border: 4px solid rgba(111, 66, 193, 0.2);
    border-top-color: #6f42c1;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 1rem; /* 水平置中並與文字間隔 */
}

.loading-text {
    display: block;
    color: #6c757d;
    font-size: 1.1rem;
    font-weight: 500;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* 分頁樣式 */
.pagination {
    display: flex;
    justify-content: center;
    margin: 2rem 0;
    gap: 0.5rem;
}

.pagination a {
    padding: 0.5rem 1rem;
    text-decoration: none;
    color: #6f42c1;
    border: 2px solid #6f42c1;
    border-radius: 25px;
    transition: all 0.3s ease;
}

.pagination a.active {
    background: #6f42c1;
    color: white;
    transform: scale(1.05);
    box-shadow: 0 2px 6px rgba(111, 66, 193, 0.3);
}

.pagination a:hover:not(.active) {
    background: rgba(111, 66, 193, 0.1);
    transform: translateY(-2px);
}

.pagination a.prev-next {
    font-weight: 500;
}

/* 禁用狀態的分頁按鈕樣式 */
.pagination a.disabled {
    color: #adb5bd;
    border-color: #dee2e6;
    background-color: #f8f9fa;
    pointer-events: none; /* 讓按鈕無法被點擊 */
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
}

/* 省略符號樣式 */
.pagination .ellipsis {
    padding: 0.5rem 0.8rem;
    color: #6c757d;
    user-select: none; /* 讓文字無法被選取 */
    align-self: center; /* 確保垂直居中對齊 */
}

/* 響應式設計 */
@media (max-width: 768px) {
    .container {
        padding: 0 0.5rem;
    }

    .search-box form {
        flex-direction: column;
    }

    .input-wrapper {
        width: 100%;
    }

    .search-box button[type="submit"] {
        width: 100%;
    }
    .controls-container {
        justify-content: center;
        margin-top: 1rem;
        margin-bottom: 1.5rem;
    }

    /* --- 響應式表格：堆疊式卡片佈局 --- */

    .table-container {
        overflow-x: hidden; /* 禁用橫向捲動 */
        background: transparent; /* 卡片有自己的背景，容器不需要 */
        box-shadow: none;
        border-radius: 0;
    }

    table {
        min-width: unset; /* 允許表格縮小 */
        font-size: 1rem;  /* 在卡片視圖中重設字體大小以提高可讀性 */
        background: transparent;
    }

    /* 隱藏原始的表格標頭 */
    table thead {
        display: none;
    }

    /* 將表格元素設為區塊級元素 */
    table, tbody, tr, td {
        display: block;
        width: 100%;
    }

    /* 將每一列樣式化為卡片 */
    tbody tr {
        margin-bottom: 1.5rem;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        background: white; /* 覆蓋 tr:nth-child(even) 的樣式 */
    }

    /* 儲存格樣式 */
    td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.8rem 1rem;
        text-align: right;
        border-bottom: 1px solid #f1f1f1;
        white-space: normal; /* 允許文字換行 */
    }

    td:last-child {
        border-bottom: none;
    }

    /* 使用 ::before 偽元素顯示標籤 */
    td::before {
        content: attr(data-label); /* 從 data-label 屬性獲取內容 */
        font-weight: 600;
        color: #6f42c1;
        text-align: left;
        margin-right: 1rem;
        white-space: nowrap;
    }

    /* 響應式下的載入中樣式 */
    #loading-indicator {
        border: none; /* 在卡片模式下，載入中不需要邊框 */
        box-shadow: none;
        background: transparent;
        margin-bottom: 0;
    }
    #loading-indicator td {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        min-height: 250px; /* 給予足夠高度 */
        border-bottom: none;
        text-align: center;
    }

    .pagination {
        flex-wrap: wrap;
    }
}

@media (max-width: 480px) {
    .pagination a {
        padding: 0.5rem;
        font-size: 0.9rem;
    }
}
