/* style.css */
:root {
    /* Material 3 暗黑色彩權杖 (M3 Dark Tokens) */
    --md-sys-color-background: #121318;
    --md-sys-color-surface: #1a1b20;
    --md-sys-color-surface-container: #24262E;
    --md-sys-color-primary: #7B9DFF;               /* M3 亮藍色 */
    --md-sys-color-on-primary: #002B75;
    --md-sys-color-secondary-container: #333742;   /* 作用中選單容器背景色 */
    --md-sys-color-on-secondary-container: #E2E2E6;
    --md-sys-color-text-main: #E2E2E6;
    --md-sys-color-text-muted: #A9A9B4;
    --md-sys-color-outline: rgba(255, 255, 255, 0.12);
    --md-sys-color-outline-focus: #7B9DFF;

    /* Material 3 圓角與尺寸規格 */
    --md-shape-corner-small: 8px;
    --md-shape-corner-medium: 12px;
    --md-shape-corner-large: 16px;
    --md-shape-corner-extra-large: 24px;
    --md-shape-corner-full: 999px; /* 膠囊狀 ( Capsule ) */
    --spacing-unit: 1rem;
    --nav-height: 70px;
    --sidebar-width: 260px;
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    background-color: var(--md-sys-color-background);
    color: var(--md-sys-color-text-main);
    line-height: 1.6;
    scroll-behavior: smooth;
    
    /* 【行動端修正 1】：全域強制鎖定，禁止網頁產生多餘的左右滑動與溢出 */
    overflow-x: hidden;
    width: 100%;
}

.material-symbols-outlined {
    vertical-align: middle;
    font-size: 1.3em;
}

/* 【行動端修正 2】：確保全站所有文字與超連結在遇到長網址時自動安全折行，絕不撐破卡片 */
p, span, a, div, h1, h2, h3, h4, strong, small {
    overflow-wrap: break-word;
    word-wrap: break-word;
    word-break: break-word;
}

/* 極長純網址特定強制斷行 */
a {
    word-break: break-all;
}

/* ==========================================
   1. M3 載入動畫 (Preloader)
   ========================================== */
#preloader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 9999;
    background-color: var(--md-sys-color-background); 
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.5s ease-out, visibility 0.5s ease-out;
}

#preloader.hidden {
    opacity: 0;
    visibility: hidden;
}

.loader-svg {
    width: 80px;
    height: 80px;
    animation: rotate 1.5s linear infinite;
}

.loader-circle {
    fill: none;
    stroke: var(--md-sys-color-primary); 
    stroke-width: 8;
    stroke-linecap: round;
    stroke-dasharray: 314; 
    stroke-dashoffset: 314;
    animation: draw 1.5s ease-in-out infinite;
}

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

@keyframes draw {
    0% { stroke-dasharray: 1 314; stroke-dashoffset: 0; }
    50% { stroke-dasharray: 150 314; stroke-dashoffset: -40; }
    100% { stroke-dasharray: 1 314; stroke-dashoffset: -314; }
}

/* ==========================================
   2. M3 頂部導覽列 (Navbar) & 漢堡按鈕安全防線
   ========================================== */
.navbar {
    background-color: var(--md-sys-color-surface);
    padding: 0 calc(var(--spacing-unit) * 2);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: var(--nav-height);
    z-index: 1000;
    border-bottom: 1px solid var(--md-sys-color-outline);
    box-sizing: border-box;
}

/* 導覽列左側標頭 */
.navbar-header {
    display: flex;
    align-items: center;
    gap: 1rem;
}

/* 【安全物理防線】：全域預設強制隱藏漢堡按鈕，防止其在首頁、關於我等前台頁面漏出 */
#btn-hamburger {
    background: transparent;
    border: none;
    color: var(--md-sys-color-text-main);
    cursor: pointer;
    font-size: 1.8rem;
    padding: 4px;
    border-radius: 50%;
    display: none !important; /* 電腦版與前台頁面強制隱藏 */
    transition: background-color 0.2s;
}

#btn-hamburger:hover {
    background-color: rgba(255, 255, 255, 0.08);
}

.nav-brand {
    font-size: 1.35rem;
    font-weight: 600;
    color: var(--md-sys-color-text-main);
    letter-spacing: -0.025em;
}

.nav-links {
    display: flex;
    gap: calc(var(--spacing-unit) * 0.5);
}

.nav-link {
    padding: 0.6rem 1.25rem;
    text-decoration: none;
    color: var(--md-sys-color-text-muted);
    border-radius: var(--md-shape-corner-full); /* 膠囊圓角 */
    transition: background-color 0.25s, color 0.25s;
    font-weight: 500;
    font-size: 0.95rem;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.nav-link:hover {
    color: var(--md-sys-color-text-main);
    background-color: rgba(255, 255, 255, 0.08);
}

.nav-link.active {
    background-color: var(--md-sys-color-secondary-container);
    color: var(--md-sys-color-primary);
}

/* 後台選單特定按鈕 */
.nav-link-admin {
    border: 1px solid var(--md-sys-color-outline);
}

.nav-link-admin.active {
    background-color: var(--md-sys-color-primary);
    color: var(--md-sys-color-on-primary);
}

/* ==========================================
   3. M3 側邊欄 (Sidebar - 藥丸導覽選單)
   ========================================== */
.sidebar {
    width: var(--sidebar-width);
    background-color: var(--md-sys-color-surface);
    border-right: 1px solid var(--md-sys-color-outline);
    padding: 2rem 1.25rem;
    position: fixed;
    top: var(--nav-height); /* 避開頂部導覽列 */
    bottom: 0;
    left: 0;
    z-index: 1200; /* 高於內容層級 */
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1); /* M3 標準動畫過渡 */
}

.sidebar-menu {
    list-style: none;
    width: 100%;
}

.sidebar-item {
    width: 100%;
    margin-bottom: 0.5rem;
}

/* 側邊欄按鈕：M3 膠囊藥丸狀 (Navigation Pills) */
.sidebar-btn {
    display: flex;
    align-items: center;
    gap: 1rem;
    width: 100%;
    padding: 0.9rem 1.5rem;
    background-color: transparent;
    color: var(--md-sys-color-text-muted);
    text-decoration: none;
    font-weight: 500;
    font-size: 0.95rem;
    border: none;
    border-radius: var(--md-shape-corner-full); /* Capsule Shape */
    cursor: pointer;
    transition: all 0.2s ease-in-out;
}

.sidebar-btn:hover {
    background-color: rgba(255, 255, 255, 0.08);
    color: var(--md-sys-color-text-main);
}

.sidebar-btn.active {
    background-color: var(--md-sys-color-secondary-container);
    color: var(--md-sys-color-primary);
    font-weight: 600;
}

/* 後台抽屜選單專用半透明遮罩背景 */
#sidebar-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.6);
    z-index: 1100; /* 低於側邊欄，高於主內容 */
    display: none;
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
}

#sidebar-backdrop.show {
    display: block;
    opacity: 1;
}

/* ==========================================
   4. 主要內容排版定位區 ( 採用安全 box-sizing )
   ========================================== */

/* 【前台展示頁】滿版無側邊欄樣式 */
.full-width-content {
    margin-top: var(--nav-height); /* 預留高度避免被固定導覽列覆蓋 */
    padding: calc(var(--spacing-unit) * 4) calc(var(--spacing-unit) * 2);
    max-width: 960px;
    margin-left: auto;
    margin-right: auto;
    box-sizing: border-box;
}

/* 【後台管理頁】左右並排佈局容器 */
.layout-container {
    display: flex;
    min-height: 100vh;
}

/* 【後台管理頁】主要內容區：避開左邊側邊欄、頂部導覽列 */
.main-content {
    flex: 1;
    margin-left: var(--sidebar-width); /* 防側邊欄遮擋 */
    margin-top: var(--nav-height);    /* 防導覽列遮擋 */
    padding: calc(var(--spacing-unit) * 4) calc(var(--spacing-unit) * 4);
    background-color: var(--md-sys-color-background);
    min-height: calc(100vh - var(--nav-height));
    box-sizing: border-box;
}

/* ==========================================
   5. M3 卡片、列表與公告
   ========================================== */
h1 {
    font-size: 2.5rem;
    color: var(--md-sys-color-primary);
    margin-bottom: 0.5rem;
    letter-spacing: -0.025em;
}

h2 {
    font-size: 1.85rem;
    border-bottom: 1px solid var(--md-sys-color-outline);
    padding-bottom: 0.75rem;
    margin-top: 2rem;
    margin-bottom: 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    color: var(--md-sys-color-text-main);
}

p {
    margin-bottom: 1rem;
    max-width: 65ch;
    color: var(--md-sys-color-text-muted);
}

.highlight {
    color: var(--md-sys-color-primary);
    font-weight: 500;
}

/* 響應式網格 */
.portfolio-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1.5rem;
}

/* M3 高圓角卡片 (Cards) */
.card {
    background-color: var(--md-sys-color-surface-container);
    border-radius: var(--md-shape-corner-extra-large); /* M3 規範 24px 高圓角 */
    padding: 1.75rem;
    transition: transform 0.25s, background-color 0.25s;
    border: 1px solid var(--md-sys-color-outline);
    display: flex;
    flex-direction: column;
}

.card:hover {
    transform: translateY(-4px);
    background-color: rgba(255, 255, 255, 0.04);
}

.card h3 {
    margin-bottom: 0.5rem;
    color: var(--md-sys-color-text-main);
    font-size: 1.25rem;
}

.card p {
    flex-grow: 1;
    font-size: 0.95rem;
}

.card a {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 1.25rem;
    text-decoration: none;
    color: var(--md-sys-color-primary);
    font-weight: 600;
    font-size: 0.95rem;
}

/* 聯絡資訊列表 */
.contact-list {
    list-style: none;
    padding-left: 0;
}

.contact-list li {
    margin-bottom: 1rem;
}

.contact-list a {
    color: var(--md-sys-color-text-main);
    text-decoration: none;
    font-size: 1.1rem;
    display: inline-flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.6rem 1.25rem;
    border-radius: var(--md-shape-corner-full); /* 膠囊狀圓角 */
    border: 1px solid var(--md-sys-color-outline);
    transition: all 0.25s;
}

.contact-list a:hover {
    color: var(--md-sys-color-primary);
    background-color: rgba(255, 255, 255, 0.06);
    border-color: var(--md-sys-color-primary);
}

/* 公告區塊結構 */
.announcement-section {
    background-color: var(--md-sys-color-surface);
    border-radius: var(--md-shape-corner-extra-large); /* 24px */
    border: 1px solid var(--md-sys-color-outline);
    padding: 1.75rem;
    margin-top: 2rem;
    margin-bottom: 2rem;
}

.announcement-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--md-sys-color-text-main);
    margin-bottom: 1.25rem;
}

.announcement-list {
    list-style: none;
}

.announcement-item {
    display: flex;
    align-items: flex-start;
    border-bottom: 1px solid var(--md-sys-color-outline);
}

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

.announcement-date {
    font-size: 0.85rem;
    color: var(--md-sys-color-primary);
    font-weight: 600;
}

.announcement-content {
    color: var(--md-sys-color-text-main);
}

/* 統一後台管理列表項目與動作按鈕容器，徹底解決手機端溢出推擠 */
.admin-list-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.15rem;
    background: var(--md-sys-color-background);
    border: 1px solid var(--md-sys-color-outline);
    border-radius: var(--md-shape-corner-medium);
    transition: border-color 0.2s;
    width: 100%;
}

.admin-list-actions {
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0; /* 鎖定大小不允許被擠壓變形 */
}

/* ==========================================
   6. 新增：M3 進階自訂控制項 & 置頂標籤
   ========================================== */

/* 自製紅底白字置頂標籤 */
.m3-badge-pinned {
    background-color: #ff5252; /* M3 經典警示紅 */
    color: #ffffff;
    font-size: 0.75rem;
    font-weight: bold;
    padding: 0.25rem 0.65rem;
    border-radius: var(--md-shape-corner-full);
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    letter-spacing: 0.05em;
    border: none;
    box-shadow: 0 2px 4px rgba(255, 82, 82, 0.2);
}

.m3-badge-pinned .material-symbols-outlined {
    font-size: 13px;
    color: #ffffff;
}

/* M3 文字輸入欄位、下拉選單、日期選擇器 */
.m3-text-field {
    width: 100%;
    background-color: rgba(255, 255, 255, 0.03) !important;
    border: 1px solid var(--md-sys-color-outline) !important;
    border-radius: var(--md-shape-corner-medium) !important; /* 12px 圓角 */
    padding: 0.85rem 1.15rem !important;
    color: var(--md-sys-color-text-main) !important;
    font-family: inherit;
    font-size: 0.95rem;
    transition: all 0.2s ease-in-out;
}

.m3-text-field:focus {
    outline: none;
    border-color: var(--md-sys-color-primary) !important;
    background-color: rgba(255, 255, 255, 0.06) !important;
    box-shadow: 0 0 0 3px rgba(123, 157, 255, 0.15) !important;
}

/* 自訂 M3 複選框 (Checkbox) */
.m3-checkbox-container {
    display: inline-flex;
    align-items: center;
    gap: 0.75rem;
    cursor: pointer;
    user-select: none;
    font-weight: 500;
    color: var(--md-sys-color-text-main);
}

.m3-checkbox-input {
    appearance: none;
    -webkit-appearance: none;
    width: 22px;
    height: 22px;
    border: 2px solid var(--md-sys-color-outline);
    border-radius: 6px;
    outline: none;
    background-color: transparent;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
    display: inline-block;
}

.m3-checkbox-input:checked {
    background-color: var(--md-sys-color-primary);
    border-color: var(--md-sys-color-primary);
}

.m3-checkbox-input:checked::after {
    content: "check";
    font-family: 'Material Symbols Outlined';
    font-size: 15px;
    color: var(--md-sys-color-on-primary);
    font-weight: bold;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

/* 後台大標題面板：標題在左、新增按鈕在右 */
.admin-panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    border-bottom: 1px solid var(--md-sys-color-outline);
    padding-bottom: 1rem;
}

.admin-panel-header h2 {
    border-bottom: none;
    margin: 0;
    padding-bottom: 0;
}

/* 後台視圖漸變動畫 */
.admin-view-fade {
    animation: m3Fade 0.3s ease-in-out;
}

@keyframes m3Fade {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

/* 雙欄表單自適應摺疊 */
.m3-grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}

/* 三欄/技能列表自適應摺疊 */
.m3-grid-skills {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 1rem;
}

/* ==========================================
   10. 響應式佈局 (RWD 安全自適應與極致體驗優化)
   ========================================== */
@media (max-width: 768px) {
    /* 【頂部導覽列雙行結構優化】：第一行品牌與漢堡，第二行橫向滾動按鈕 */
    .navbar {
        flex-direction: column;
        align-items: stretch; /* 撐滿寬度 */
        padding: 0.75rem 1rem;
        height: auto;
        position: relative; /* 手機端取消固定定位，避免與內容重疊 */
        width: 100%;
        box-sizing: border-box;
    }
    
    /* 僅在手機端 (下於 768px) 且為後台管理頁時，才顯示漢堡按鈕 */
    body.admin-page #btn-hamburger {
        display: block !important;
    }

    /* 手機端專屬：網航按鈕「不換行、可橫向滑動」M3 頂級導覽列體驗 */
    .nav-links {
        margin-top: 0.75rem;
        width: 100%;
        display: flex;
        overflow-x: auto; /* 允許橫向滑動滾動 */
        white-space: nowrap; /* 強制不換行 */
        gap: 0.5rem;
        padding-bottom: 4px; /* 預留滑動時的觸覺空間 */
        -webkit-overflow-scrolling: touch; /* 啟用 iOS 流暢慣性滾動 */
    }

    /* 隱藏手機端導覽滾動條，保持極致乾淨視覺 */
    .nav-links::-webkit-scrollbar {
        display: none;
    }
    
    .nav-link {
        flex-shrink: 0; /* 鎖定按鈕不允許在滾動時被擠壓縮小 */
        padding: 0.5rem 0.95rem; 
        font-size: 0.85rem;
    }
    
    /* 縮小手機端前台卡片與容器的左右邊距，釋放螢幕空間並防溢出 */
    .full-width-content {
        margin-top: 0;
        padding: calc(var(--spacing-unit) * 2) var(--spacing-unit);
        width: 100%;
        box-sizing: border-box;
    }

    /* 【手機端抽屜式側邊欄 (Navigation Drawer)】：預設隱藏於螢幕左外側 */
    .sidebar {
        position: fixed;
        width: var(--sidebar-width);
        height: 100vh;
        top: 0;
        bottom: 0;
        left: 0;
        z-index: 1500; /* 高於所有層級，蓋在 Navbar 上 */
        border-right: 1px solid var(--md-sys-color-outline);
        border-bottom: none;
        padding: 2rem 1.25rem;
        box-sizing: border-box;
        transform: translateX(-100%); /* 藏到左邊外側 */
        box-shadow: 16px 0 24px rgba(0, 0, 0, 0.4); /* M3 抽屜陰影 */
    }

    /* 抽屜啟動滑出樣式 */
    .sidebar.open {
        transform: translateX(0); /* 滑入畫面 */
    }

    .main-content {
        margin-left: 0;
        margin-top: 0;
        padding: calc(var(--spacing-unit) * 2) var(--spacing-unit);
        width: 100%;
        box-sizing: border-box;
    }

    /* --------------------------------------
       【後台 RWD 核心堆疊】：解決手機端表格、表單與列表擠壓
       -------------------------------------- */
    .m3-grid-2 {
        grid-template-columns: 1fr; /* 雙欄欄位全自動堆疊為單欄，防擠壓 */
        gap: 1rem;
    }

    .m3-grid-skills {
        grid-template-columns: 1fr; /* 技能管理清單全自動堆疊 */
        gap: 0.75rem;
    }

    .portfolio-grid {
        grid-template-columns: 1fr !important; /* Settings 連線卡片雙欄強制改為單欄堆疊 */
        gap: 1.5rem;
    }

    .layout-container {
        flex-direction: column; 
    }

    /* 解決連線面板高度被 100% 拉伸過長問題 */
    .admin-panel-section .portfolio-grid > div {
        height: auto !important;
    }

    /* 後台標題在手機端自適應垂直排列，防新增按鈕被擠壓 */
    .admin-panel-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
    }

    .admin-panel-header button {
        width: 100% !important; /* 按鈕寬度撐滿手機螢幕，極易點擊 */
        justify-content: center;
    }

    /* 【安全優化】：公告與作品的管理清單，在手機端由左右並排自動轉為「上下堆疊」，防按鈕溢出 */
    .admin-list-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
        width: 100%;
    }

    .admin-list-actions {
        width: 100%;
        justify-content: flex-end; /* 在手機端將編輯、刪除按鈕靠右排列 */
    }
}

/* 【全域物理安全防禦】：除了後台之外，前台所有頁面一律強制隱藏漢堡按鈕 */
body:not(.admin-page) #btn-hamburger {
    display: none !important;
}
