/* =========================================
   市集店主財務系統 - 高級介面樣式表
   (基於卍物所風格：圓潤、柔和陰影、無邊界)
========================================= */

/* --- 全域變數 --- */
:root {
    --bg: #f7f6f3;
    --bg-elevated: #ffffff;
    --text: #1d1d1f;
    --text-secondary: #6e6e73;
    --text-tertiary: #86868b;
    --accent: #7a6b58;
    --accent-soft: rgba(122, 107, 88, 0.12);
    --border: rgba(0, 0, 0, 0.08);
    --shadow: 0 24px 64px rgba(0, 0, 0, 0.06);
    --shadow-soft: 0 8px 32px rgba(0, 0, 0, 0.04);
    --radius: 18px;
    --radius-lg: 28px;
    --ease: cubic-bezier(0.25, 0.1, 0.25, 1);
    --header-h: 70px;
    --max-w: 1200px;
}

/* --- 基礎重置與排版 --- */
*, *::before, *::after { 
    box-sizing: border-box; 
    margin: 0; 
    padding: 0; 
}

body {
    font-family: "Inter", "Noto Sans TC", -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    padding-top: var(--header-h);
    overflow-x: hidden;
}

/* --- 頂部導航列 (Header & Nav) --- */
.site-header {
    position: fixed; 
    top: 0; 
    left: 0; 
    right: 0; 
    z-index: 100; 
    height: var(--header-h);
    display: flex; 
    align-items: center; 
    justify-content: center;
    background: rgba(247, 246, 243, 0.85); 
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    border-bottom: 1px solid var(--border);
}

.header-inner { 
    width: min(var(--max-w), 100% - 48px); 
    display: flex; 
    align-items: center; 
    justify-content: space-between; 
    gap: 12px;
}

.auth-tabs {
    display: flex; 
    gap: 4px; 
    background: rgba(0, 0, 0, 0.04); 
    border-radius: 16px; 
    padding: 6px;
    overflow-x: auto; 
    max-width: 100%;
}

.auth-tab {
    border: none; 
    background: transparent; 
    padding: 10px 20px; 
    font-size: 0.9rem; 
    font-weight: 500; 
    color: var(--text-secondary); 
    border-radius: 12px; 
    cursor: pointer; 
    transition: all 0.25s var(--ease);
    white-space: nowrap; 
    font-family: inherit;
}

.auth-tab.active { 
    background: var(--bg-elevated); 
    color: var(--text); 
    box-shadow: var(--shadow-soft); 
}

.auth-tab:hover:not(.active) { 
    color: var(--text); 
    background: rgba(0,0,0,0.02); 
}

/* --- 區塊佈局與卡片 (Layout & Cards) --- */
section { 
    padding: 60px 24px; 
    min-height: calc(100vh - var(--header-h)); 
    display: none; 
}

section.active { 
    display: block; 
    animation: fadeUp 0.6s var(--ease) forwards; 
}

.section-inner { 
    width: min(var(--max-w), 100%); 
    margin: 0 auto; 
}

.form-panel { 
    background: var(--bg-elevated); 
    border-radius: var(--radius-lg); 
    padding: 40px; 
    border: 1px solid var(--border); 
    box-shadow: var(--shadow-soft); 
    margin-bottom: 40px;
}

.form-title { 
    font-family: "Noto Serif TC", serif; 
    font-size: 1.5rem; 
    margin-bottom: 8px; 
    color: var(--text); 
}

.form-subtitle { 
    font-size: 0.9rem; 
    color: var(--text-secondary); 
    margin-bottom: 28px; 
}

@keyframes fadeUp { 
    0% { opacity: 0; transform: translateY(15px); } 
    100% { opacity: 1; transform: translateY(0); } 
}

/* --- 表單與基礎輸入框 (Forms) --- */
.form-row { 
    display: grid; 
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); 
    gap: 20px; 
    margin-bottom: 24px; 
}

.form-group label { 
    display: block; 
    font-size: 0.8rem; 
    font-weight: 500; 
    color: var(--text-secondary); 
    margin-bottom: 8px; 
}

.form-group input, .form-group select {
    width: 100%; 
    padding: 14px 16px; 
    font-size: 1rem; 
    font-family: inherit;
    border: 1px solid var(--border); 
    border-radius: 12px; 
    background: var(--bg); 
    color: var(--text);
    transition: all 0.2s;
}

.form-group input:focus, .form-group select:focus { 
    outline: none; 
    border-color: var(--accent); 
    box-shadow: 0 0 0 4px var(--accent-soft); 
}

/* --- 按鈕系統 (Buttons) --- */
.btn { 
    display: inline-flex; 
    align-items: center; 
    justify-content: center; 
    gap: 8px; 
    padding: 12px 24px; 
    font-size: 0.9rem; 
    font-weight: 500; 
    border-radius: 999px; 
    border: none; 
    cursor: pointer; 
    transition: all 0.25s var(--ease); 
    font-family: inherit; 
}

.btn:active { transform: scale(0.97); }

.btn-primary { background: var(--text); color: #fff; }
.btn-primary:hover { background: #333; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15); }

.btn-secondary { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--bg); }

.btn-danger { background: #fee2e2; color: #991b1b; padding: 8px 16px; border-radius: 12px; font-size: 0.8rem; }
.btn-danger:hover { background: #fca5a5; }

.btn-outline-danger { background: transparent; border: 1px solid #fca5a5; color: #dc2626; padding: 8px 16px; border-radius: 999px; font-size: 0.85rem; }
.btn-outline-danger:hover { background: #fee2e2; }

/* --- 數據表格與內聯編輯 (Tables & Inline Input) --- */
.table-wrapper { 
    border-radius: var(--radius); 
    border: 1px solid var(--border); 
    overflow-x: auto; 
    background: var(--bg-elevated); 
}

table { 
    width: 100%; 
    border-collapse: collapse; 
    text-align: left; 
}

th { 
    font-size: 0.85rem; 
    font-weight: 500; 
    color: var(--text-secondary); 
    padding: 16px; 
    border-bottom: 1px solid var(--border); 
    background: rgba(247, 246, 243, 0.5); 
    white-space: nowrap; 
}

td { 
    padding: 8px 16px; 
    border-bottom: 1px solid rgba(0,0,0,0.04); 
    vertical-align: middle; 
}

tr:last-child td { border-bottom: none; }

.cell-input {
    width: 100%; 
    min-width: 80px; 
    padding: 10px 12px; 
    font-size: 0.95rem; 
    font-family: inherit;
    border: 1px solid transparent; 
    border-radius: 8px; 
    background: transparent; 
    color: var(--text);
    transition: all 0.2s;
}

.cell-input:hover { background: var(--bg); }
.cell-input:focus { 
    background: var(--bg-elevated); 
    border-color: var(--accent); 
    box-shadow: 0 0 0 3px var(--accent-soft); 
    outline: none; 
}

select.cell-input { appearance: none; cursor: pointer; }

/* 輔助排版文字 */
.text-right { text-align: right; }
.text-accent { color: var(--accent); font-weight: 600; }
.text-danger { color: #dc2626; font-weight: 600; }

/* --- 主報表摘要 UI (Dashboard Summary) --- */
.dashboard-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 40px; }
.summary-row { display: flex; justify-content: space-between; padding: 12px 0; border-bottom: 1px dashed var(--border); font-size: 0.95rem; }
.summary-row.total { font-weight: 700; font-size: 1.1rem; border-bottom: none; border-top: 2px solid var(--text); margin-top: 8px; padding-top: 16px; }
.summary-row.indent { padding-left: 24px; color: var(--text-secondary); font-size: 0.9rem; }

@media (max-width: 900px) { .dashboard-grid { grid-template-columns: 1fr; } }


/* =========================================
   安全鎖與唯讀模式 UI (修正版)
========================================= */

/* 1. 唯讀狀態下，只隱藏「報表區塊 (section)」以及「頂部導航列」的危險按鈕，排除登入視窗 */
body.readonly-mode section .btn-primary,
body.readonly-mode section .btn-danger,
body.readonly-mode .btn-outline-danger {
    display: none !important;
}

/* 2. 唯讀狀態下，只鎖定「報表區塊 (section)」內的輸入框，讓登入視窗保持正常運作 */
body.readonly-mode section .cell-input,
body.readonly-mode section .form-group input {
    pointer-events: none; /* 禁用滑鼠點擊 */
    border-color: transparent !important;
    background: transparent !important;
    padding-left: 0;
    color: var(--text);
}

/* --- 登入解鎖視窗與按鈕 (Auth Modal) --- */
.lock-btn {
    background: transparent; 
    border: 1px solid var(--border); 
    border-radius: 999px;
    padding: 8px 16px; 
    font-size: 0.85rem; 
    cursor: pointer; 
    transition: 0.2s;
    font-family: inherit;
    color: var(--text);
}

.lock-btn:hover { background: var(--bg); }

.auth-modal {
    position: fixed; 
    inset: 0; 
    background: rgba(0,0,0,0.5); 
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    display: flex; 
    align-items: center; 
    justify-content: center; 
    z-index: 1000;
    opacity: 0; 
    pointer-events: none; 
    transition: 0.3s var(--ease);
}

.auth-modal.show { 
    opacity: 1; 
    pointer-events: auto; 
}

.auth-box {
    background: var(--bg-elevated); 
    padding: 40px 32px; 
    border-radius: var(--radius-lg);
    width: 90%; 
    max-width: 420px; 
    box-shadow: 0 32px 64px rgba(0, 0, 0, 0.08), 0 8px 32px rgba(0, 0, 0, 0.04); 
    border: 1px solid var(--border);
    transform: translateY(20px);
    transition: 0.4s var(--ease-out);
}

.auth-modal.show .auth-box {
    transform: translateY(0);
}

/* =========================================
   自訂懸浮通知框 (Custom Toast)
========================================= */
.welcome-toast { 
    position: fixed; 
    top: 90px; /* 避開頂部導航列 */
    left: 50%; 
    transform: translate(-50%, -150%); 
    background: rgba(255, 255, 255, 0.85); 
    backdrop-filter: blur(12px); 
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--border); 
    padding: 12px 28px; 
    border-radius: 999px; 
    box-shadow: var(--shadow-soft); 
    z-index: 100000; 
    display: flex; 
    align-items: center; 
    gap: 8px; 
    font-family: "Noto Serif TC", serif; 
    font-size: 0.95rem; 
    font-weight: 500; 
    color: var(--text); 
    transition: transform 0.6s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.4s var(--ease); 
    opacity: 0; 
    pointer-events: none; 
}

.welcome-toast.show { 
    transform: translate(-50%, 0); 
    opacity: 1; 
}

/* 錯誤狀態的紅色風格 */
.welcome-toast.error {
    color: #dc2626;
    border-color: #fca5a5;
    background: rgba(254, 242, 242, 0.95);
}

/* =========================================
   📱 手機版響應式設計 (Mobile Responsive Design)
========================================= */

@media (max-width: 768px) {
    /* --- 1. 頂部導航列 (自動適應高度與堆疊) --- */
    :root {
        --header-h: 110px; /* 手機版導航列需要更多高度 */
    }

    .site-header {
        height: auto;
        padding: 12px 0;
    }

    .header-inner {
        flex-direction: column; /* 將標籤同按鈕改為上下排列 */
        gap: 12px;
        padding: 0 16px;
    }

    .auth-tabs {
        width: 100%;
        padding: 4px;
        justify-content: flex-start;
        /* 加入流暢的橫向滑動效果 */
        overflow-x: auto;
        scroll-snap-type: x mandatory;
        -webkit-overflow-scrolling: touch;
        border-radius: 12px;
    }

    .auth-tab {
        scroll-snap-align: start;
        padding: 8px 14px;
        font-size: 0.85rem;
    }

    /* 鎖頭與清除按鈕排版 */
    .header-inner > button {
        width: 100%;
        text-align: center;
        justify-content: center;
        padding: 10px;
    }

    /* --- 2. 區塊佈局縮減留白 --- */
    section {
        padding: 30px 16px; /* 減少左右空白 */
    }

    .form-panel {
        padding: 24px 16px; 
        border-radius: var(--radius); 
        margin-bottom: 24px;
    }

    .form-title {
        font-size: 1.3rem;
    }

    /* --- 3. 表單與按鈕手機優化 --- */
    .form-row {
        grid-template-columns: 1fr; /* 輸入框改為垂直單行排列 */
        gap: 16px;
    }

    /* 將「新增產品/紀錄」按鈕拉闊至 100%，方便手指點擊 */
    .form-panel > div[style*="margin-bottom: 24px"] {
        display: flex;
        flex-direction: column;
    }
    .form-panel > div[style*="margin-bottom: 24px"] .btn {
        width: 100%;
    }

    /* --- 4. 數據表格 (無邊界延伸設計) --- */
    .table-wrapper {
        margin-inline: -16px; /* 讓表格貼齊手機螢幕左右邊緣 */
        border-radius: 0;
        border-left: none;
        border-right: none;
        -webkit-overflow-scrolling: touch; /* iOS 彈性滑動 */
    }

    th, td {
        padding: 12px 10px; /* 縮小儲存格內距 */
        font-size: 0.85rem;
    }

    .cell-input {
        padding: 8px;
        font-size: 0.9rem;
    }

    /* --- 5. 登入解鎖視窗手機優化 --- */
    .auth-box {
        padding: 32px 20px;
        width: 95%;
    }

    .auth-box h2 {
        font-size: 1.25rem;
    }

    .auth-box p {
        font-size: 0.8rem;
        margin-bottom: 20px !important;
    }

    /* 將登入視窗內的「取消」與「解鎖」按鈕改為上下堆疊的大按鈕 */
    .auth-box > div:last-child {
        flex-direction: column; 
        gap: 10px !important;
    }
    
    .auth-box .btn {
        width: 100%;
        padding: 14px;
        font-size: 0.95rem;
    }
}