/* ==========================================================================
   Main Styles - CSS Reset, Variables, Typography, Components, Utilities
   ========================================================================== */

/* --- CSS Reset / Normalize --- */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: var(--font-size-base, 16px);
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    line-height: 1.15;
}

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
    font-size: 0.9375rem;
    font-weight: 400;
    line-height: 1.6;
    color: var(--text);
    background-color: var(--bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

article, aside, details, figcaption, figure, footer, header, main, nav, section, summary {
    display: block;
}

img {
    max-width: 100%;
    height: auto;
    border-style: none;
}

a {
    color: var(--primary);
    text-decoration: none;
    background-color: transparent;
}

a:hover {
    color: var(--primary-dark);
    text-decoration: underline;
}

table {
    border-collapse: collapse;
    border-spacing: 0;
}

button, input, optgroup, select, textarea {
    font-family: inherit;
    font-size: inherit;
    line-height: inherit;
    margin: 0;
}

button {
    cursor: pointer;
    border: none;
    background: none;
}

ul, ol {
    list-style: none;
}

h1, h2, h3, h4, h5, h6 {
    margin-bottom: 0.5rem;
    font-weight: 600;
    line-height: 1.3;
}

h1 { font-size: 1.75rem; }
h2 { font-size: 1.5rem; }
h3 { font-size: 1.25rem; }
h4 { font-size: 1.1rem; }
h5 { font-size: 1rem; }
h6 { font-size: 0.875rem; }

p {
    margin-bottom: 1rem;
}

/* --- CSS Custom Properties --- */
:root {
    --font-size-base: 16px;
    --primary: #3b82f6;
    --primary-dark: #2563eb;
    --primary-light: #93bbfd;
    --secondary: #64748b;
    --secondary-dark: #475569;
    --success: #22c55e;
    --success-dark: #16a34a;
    --danger: #ef4444;
    --danger-dark: #dc2626;
    --warning: #f59e0b;
    --warning-dark: #d97706;
    --info: #06b6d4;

    --bg: #f1f5f9;
    --bg-white: #ffffff;
    --text: #1e293b;
    --text-secondary: #64748b;
    --text-light: #94a3b8;
    --border: #e2e8f0;
    --border-dark: #cbd5e1;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.07), 0 2px 4px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1), 0 4px 6px rgba(0, 0, 0, 0.05);

    --radius-sm: 4px;
    --radius: 6px;
    --radius-lg: 8px;
    --radius-xl: 12px;

    --transition: 0.2s ease;
    --header-height: 56px;
    --sidebar-width: 240px;
    --sidebar-collapsed-width: 60px;
    --header-bg: #1e293b;
    --header-text: #f1f5f9;
    --sidebar-bg: #ffffff;
    --sidebar-text: #64748b;
    --sidebar-hover-bg: #f1f5f9;
    --sidebar-section-text: #94a3b8;
    --sidebar-active-bg: #eff6ff;
    --sidebar-active-text: #3b82f6;
    --sidebar-border: #e2e8f0;
    --sidebar-tab-border: #cbd5e1;
}

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.5;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background-color: var(--bg-white);
    color: var(--text);
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
    user-select: none;
    text-decoration: none;
}

.btn:hover {
    background-color: var(--bg);
    border-color: var(--border-dark);
    text-decoration: none;
}

.btn:focus {
    outline: 2px solid var(--primary-light);
    outline-offset: 1px;
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    pointer-events: none;
}

.btn-primary {
    background-color: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background-color: var(--primary-dark);
    border-color: var(--primary-dark);
    color: #fff;
}

.btn-secondary {
    background-color: var(--secondary);
    border-color: var(--secondary);
    color: #fff;
}

.btn-secondary:hover {
    background-color: var(--secondary-dark);
    border-color: var(--secondary-dark);
    color: #fff;
}

.btn-success {
    background-color: var(--success);
    border-color: var(--success);
    color: #fff;
}

.btn-success:hover {
    background-color: var(--success-dark);
    border-color: var(--success-dark);
    color: #fff;
}

.btn-danger {
    background-color: var(--danger);
    border-color: var(--danger);
    color: #fff;
}

.btn-danger:hover {
    background-color: var(--danger-dark);
    border-color: var(--danger-dark);
    color: #fff;
}

.btn-warning {
    background-color: var(--warning);
    border-color: var(--warning);
    color: #fff;
}

.btn-warning:hover {
    background-color: var(--warning-dark);
    border-color: var(--warning-dark);
    color: #fff;
}

.btn-sm {
    padding: 0.3rem 0.6rem;
    font-size: 0.8125rem;
}

.btn-lg {
    padding: 0.65rem 1.25rem;
    font-size: 1rem;
}

.btn-block {
    display: flex;
    width: 100%;
}

/* --- Alerts --- */
.alert {
    padding: 0.75rem 1rem;
    margin-bottom: 1rem;
    border: 1px solid transparent;
    border-radius: var(--radius);
    font-size: 0.875rem;
    line-height: 1.5;
}

.alert-success {
    background-color: #f0fdf4;
    border-color: #bbf7d0;
    color: #166534;
}

.alert-danger {
    background-color: #fef2f2;
    border-color: #fecaca;
    color: #991b1b;
}

.alert-warning {
    background-color: #fffbeb;
    border-color: #fde68a;
    color: #92400e;
}

.alert-info {
    background-color: #ecfeff;
    border-color: #a5f3fc;
    color: #155e75;
}

/* --- Cards --- */
.card {
    background-color: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
}

.card-header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
}

.card-body {
    padding: 1.25rem;
}

.card-footer {
    padding: 0.75rem 1.25rem;
    border-top: 1px solid var(--border);
    background-color: #f8fafc;
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

/* --- Loading spinner --- */
.spinner {
    display: inline-block;
    width: 1.25rem;
    height: 1.25rem;
    border: 2px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

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

.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

/* --- Utility Classes --- */
.hidden {
    display: none !important;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.text-center { text-align: center; }
.text-right  { text-align: right; }
.text-left   { text-align: left; }

.text-primary   { color: var(--primary); }
.text-secondary { color: var(--text-secondary); }
.text-success   { color: var(--success); }
.text-danger    { color: var(--danger); }
.text-warning   { color: var(--warning); }
.text-muted     { color: var(--text-light); }

.fw-bold   { font-weight: 700; }
.fw-medium { font-weight: 500; }
.fw-normal { font-weight: 400; }

.fs-sm { font-size: 0.8125rem; }
.fs-lg { font-size: 1.125rem; }

/* Margin top */
.mt-0 { margin-top: 0; }
.mt-1 { margin-top: 0.25rem; }
.mt-2 { margin-top: 0.5rem; }
.mt-3 { margin-top: 1rem; }
.mt-4 { margin-top: 1.5rem; }

/* Margin bottom */
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: 0.25rem; }
.mb-2 { margin-bottom: 0.5rem; }
.mb-3 { margin-bottom: 1rem; }
.mb-4 { margin-bottom: 1.5rem; }

/* Margin right */
.mr-1 { margin-right: 0.25rem; }
.mr-2 { margin-right: 0.5rem; }
.mr-3 { margin-right: 1rem; }

/* Margin left */
.ml-1 { margin-left: 0.25rem; }
.ml-2 { margin-left: 0.5rem; }
.ml-3 { margin-left: 1rem; }
.ml-auto { margin-left: auto; }

/* Padding */
.p-0 { padding: 0; }
.p-1 { padding: 0.25rem; }
.p-2 { padding: 0.5rem; }
.p-3 { padding: 1rem; }
.p-4 { padding: 1.5rem; }

/* Flexbox */
.d-flex { display: flex; }
.d-inline-flex { display: inline-flex; }
.flex-column { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.align-center { align-items: center; }
.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.justify-end { justify-content: flex-end; }
.gap-1 { gap: 0.25rem; }
.gap-2 { gap: 0.5rem; }
.gap-3 { gap: 1rem; }

/* Width */
.w-100 { width: 100%; }
.w-auto { width: auto; }

/* ==========================================================================
   Tabs — 統一頁籤元件
   ========================================================================== */

/* --- 底線型頁籤（用於主要導航，如單元設定、權限切換） --- */
.tabs-underline {
    display: flex;
    gap: 0;
    border-bottom: 2px solid var(--border);
    margin-bottom: 1.25rem;
}

.tab-underline {
    padding: 0.625rem 1.25rem;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary);
    cursor: pointer;
    white-space: nowrap;
    transition: color 0.15s, border-color 0.15s;
}

.tab-underline:hover {
    color: var(--text);
}

.tab-underline.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

/* 標記未保存狀態 */
.tab-underline.tab-dirty::after {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    background: var(--danger);
    border-radius: 50%;
    margin-left: 5px;
    vertical-align: middle;
}

/* --- 藥丸型頁籤（用於分類篩選、子項切換） --- */
.tabs-pill {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    margin-bottom: 1rem;
}

.tab-pill {
    padding: 0.35rem 0.85rem;
    font-size: 0.8125rem;
    font-weight: 500;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--bg-card, #fff);
    color: var(--text-secondary);
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.15s;
}

.tab-pill:hover {
    background: var(--bg);
    color: var(--text);
}

.tab-pill.active {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

/* 藥丸型變體：段落切換器（無邊框、有陰影） */
.tabs-pill.tabs-segment {
    background: var(--bg);
    border-radius: var(--radius);
    padding: 0.25rem;
    gap: 0;
    overflow-x: auto;
}

.tabs-segment .tab-pill {
    border: none;
    border-radius: var(--radius);
    background: none;
    padding: 0.5rem 1rem;
}

.tabs-segment .tab-pill:hover {
    color: var(--text);
    background: var(--bg-white, #fff);
}

.tabs-segment .tab-pill.active {
    background: var(--bg-white, #fff);
    color: var(--primary);
    font-weight: 600;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

/* --- 頁籤內容區域 --- */
.tab-content {
    display: block;
}

.tab-content.hidden {
    display: none;
}

/* Toast 通知 */
.toast-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9999;
    display: flex;
    flex-direction: column-reverse;
    gap: 8px;
    pointer-events: none;
}
.toast-item {
    padding: 10px 16px;
    border-radius: 4px;
    color: #fff;
    box-shadow: 0 2px 8px rgba(0,0,0,.25);
    font-size: 14px;
    pointer-events: auto;
    animation: toast-in .3s ease;
    transition: opacity .4s;
    max-width: 320px;
    word-break: break-word;
}
.toast-item.toast-success { background: #28a745; }
.toast-item.toast-warning { background: #f39c12; }
.toast-item.toast-error   { background: #e53935; }
.toast-item.toast-info    { background: #1976d2; }
.toast-item.toast-hide    { opacity: 0; }
@keyframes toast-in {
    from { opacity: 0; transform: translateY(16px); }
    to   { opacity: 1; transform: translateY(0); }
}
