/* 基礎設定 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #2563eb;
    --secondary-color: #64748b;
    --success-color: #059669;
    --danger-color: #dc2626;
    --warning-color: #d97706;
    --border-color: #e2e8f0;
    --gray-50: #f8fafc;
    --gray-100: #f1f5f9;
    --gray-200: #e2e8f0;
    --gray-300: #cbd5e1;
    --gray-400: #94a3b8;
    --gray-500: #64748b;
    --gray-600: #475569;
    --gray-700: #334155;
    --gray-800: #1e293b;
    --gray-900: #0f172a;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --border-radius: 8px;
    --border-radius-lg: 12px;
}

body {
    font-family: 'Noto Sans TC', 'Microsoft JhengHei', sans-serif;
    line-height: 1.6;
    color: var(--gray-800);
    background-color: var(--gray-50);
}

/* 容器和佈局 */
.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
    min-height: 100vh;
}

header {
    text-align: center;
    margin-bottom: 30px;
    padding: 20px 0;
    background: white;
    border-radius: var(--border-radius-lg);
    box-shadow: var(--shadow-sm);
}

header h1 {
    color: var(--primary-color);
    font-size: 2.5rem;
    margin-bottom: 8px;
    font-weight: 700;
}

header p {
    color: var(--gray-600);
    font-size: 1.1rem;
}

.main-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    align-items: start;
}

/* 表單樣式 */
.form-section {
    background: white;
    border-radius: var(--border-radius-lg);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.form-group {
    padding: 25px;
    border-bottom: 1px solid var(--border-color);
}

.form-group:last-child {
    border-bottom: none;
}

.form-group h2 {
    color: var(--gray-800);
    font-size: 1.4rem;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.input-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}

.input-field {
    display: flex;
    flex-direction: column;
}

.input-field.full-width {
    grid-column: 1 / -1;
}

.input-field label {
    font-weight: 600;
    margin-bottom: 6px;
    color: var(--gray-700);
    font-size: 0.9rem;
}

.input-field input,
.input-field textarea {
    padding: 12px 16px;
    border: 2px solid var(--border-color);
    border-radius: var(--border-radius);
    font-size: 1rem;
    transition: all 0.2s ease;
    background: white;
}

.input-field input:focus,
.input-field textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgb(37 99 235 / 0.1);
}

.input-field input::placeholder,
.input-field textarea::placeholder {
    color: var(--gray-400);
}

/* 項目表格樣式 */
.items-section {
    padding: 0;
}

.items-header {
    padding: 20px 25px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.items-table {
    overflow-x: auto;
}

.table-header {
    display: grid;
    grid-template-columns: 80px 1fr 80px 120px 120px 60px;
    gap: 12px;
    padding: 15px 25px;
    background: var(--gray-50);
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--gray-700);
    border-bottom: 1px solid var(--border-color);
}

.item-row {
    display: grid;
    grid-template-columns: 80px 1fr 80px 120px 120px 60px;
    gap: 12px;
    padding: 15px 25px;
    border-bottom: 1px solid var(--border-color);
    align-items: center;
}

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

.item-row input {
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    font-size: 0.9rem;
    width: 100%;
}

.item-row input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgb(37 99 235 / 0.1);
}

.item-subtotal {
    font-weight: 600;
    color: var(--gray-700);
    text-align: right;
}

.remove-item-btn {
    background: var(--danger-color);
    color: white;
    border: none;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    cursor: pointer;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.remove-item-btn:hover {
    background: #b91c1c;
    transform: scale(1.1);
}

.amounts-summary {
    padding: 20px 25px;
    background: var(--gray-50);
    border-top: 1px solid var(--border-color);
}

.amount-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    font-size: 1.1rem;
}

.amount-row.total {
    font-weight: 700;
    font-size: 1.3rem;
    color: var(--primary-color);
    border-top: 2px solid var(--border-color);
    padding-top: 12px;
    margin-top: 12px;
    margin-bottom: 0;
}

/* 按鈕樣式 */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 24px;
    border: none;
    border-radius: var(--border-radius);
    font-size: 1rem;
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background: #1d4ed8;
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-secondary {
    background: var(--secondary-color);
    color: white;
}

.btn-secondary:hover {
    background: #475569;
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-outline {
    background: white;
    color: var(--gray-700);
    border: 2px solid var(--border-color);
}

.btn-outline:hover {
    background: var(--gray-50);
    border-color: var(--gray-300);
    transform: translateY(-1px);
}

.form-actions {
    padding: 25px;
    display: flex;
    gap: 15px;
    justify-content: flex-end;
    background: var(--gray-50);
}

/* 預覽區域 */
.preview-section {
    background: white;
    border-radius: var(--border-radius-lg);
    box-shadow: var(--shadow);
    overflow: hidden;
    position: sticky;
    top: 20px;
    max-height: calc(100vh - 40px);
    display: flex;
    flex-direction: column;
}

.preview-header {
    padding: 20px 25px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--gray-50);
}

.preview-header h2 {
    color: var(--gray-800);
    font-size: 1.3rem;
    display: flex;
    align-items: center;
    gap: 8px;
}

.preview-content {
    flex: 1;
    overflow-y: auto;
    padding: 25px;
}

.invoice-preview {
    font-size: 0.95rem;
    line-height: 1.6;
}

.invoice-preview h1 {
    text-align: center;
    color: var(--primary-color);
    font-size: 2rem;
    margin-bottom: 30px;
    font-weight: 700;
}

.preview-section-block {
    margin-bottom: 25px;
    padding-bottom: 20px;
    border-bottom: 1px solid var(--border-color);
}

.preview-section-block:last-child {
    border-bottom: none;
    margin-bottom: 0;
}

.preview-section-block h3 {
    color: var(--gray-800);
    font-size: 1.2rem;
    margin-bottom: 15px;
    font-weight: 600;
}

.preview-section-block p {
    margin-bottom: 8px;
    display: flex;
    align-items: flex-start;
    gap: 8px;
}

.preview-section-block strong {
    min-width: 80px;
    color: var(--gray-700);
}

.preview-table {
    overflow-x: auto;
    margin-bottom: 15px;
}

.preview-table table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.preview-table th,
.preview-table td {
    padding: 12px 8px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

.preview-table th {
    background: var(--gray-50);
    font-weight: 600;
    color: var(--gray-700);
}

.preview-table .no-items {
    text-align: center;
    color: var(--gray-400);
    font-style: italic;
}

.preview-amounts {
    text-align: right;
    padding: 15px 0;
    border-top: 2px solid var(--border-color);
}

.preview-amounts p {
    justify-content: flex-end;
    margin-bottom: 5px;
}

.preview-amounts .total-amount {
    font-weight: 700;
    font-size: 1.2rem;
    color: var(--primary-color);
}

.preview-notes {
    font-style: italic;
    color: var(--gray-600);
}

/* 載入指示器 */
.loading-indicator {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    color: white;
}

.loading-indicator.hidden {
    display: none;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top: 4px solid white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 20px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* 響應式設計 */
@media (max-width: 1024px) {
    .main-content {
        grid-template-columns: 1fr;
        gap: 20px;
    }

    .preview-section {
        position: static;
        max-height: none;
    }

    header h1 {
        font-size: 2rem;
    }
}

@media (max-width: 768px) {
    .container {
        padding: 15px;
    }

    .input-grid {
        grid-template-columns: 1fr;
        gap: 15px;
    }

    .form-actions {
        flex-direction: column;
        gap: 10px;
    }

    .btn {
        width: 100%;
        justify-content: center;
    }

    .table-header,
    .item-row {
        grid-template-columns: 1fr;
        gap: 10px;
        padding: 15px;
    }

    .table-header {
        display: none;
    }

    .item-row {
        display: flex;
        flex-direction: column;
        background: var(--gray-50);
        border-radius: var(--border-radius);
        margin-bottom: 10px;
        border: 1px solid var(--border-color);
    }

    .item-row > * {
        display: flex;
        align-items: center;
        gap: 10px;
        margin-bottom: 8px;
    }

    .item-row > *:last-child {
        margin-bottom: 0;
    }

    .item-row > *::before {
        content: attr(data-label);
        font-weight: 600;
        min-width: 80px;
        color: var(--gray-700);
        font-size: 0.9rem;
    }

    .preview-header {
        flex-direction: column;
        gap: 10px;
        align-items: stretch;
    }

    .preview-table {
        font-size: 0.8rem;
    }

    header h1 {
        font-size: 1.8rem;
    }

    header p {
        font-size: 1rem;
    }
}

@media (max-width: 480px) {
    .container {
        padding: 10px;
    }

    .form-group {
        padding: 20px 15px;
    }

    .preview-content {
        padding: 20px 15px;
    }

    .invoice-preview h1 {
        font-size: 1.6rem;
    }

    header h1 {
        font-size: 1.6rem;
    }

    header p {
        font-size: 0.9rem;
    }
}

/* 輔助工具類 */
.hidden {
    display: none !important;
}

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

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

.font-weight-bold {
    font-weight: 700;
}

.text-muted {
    color: var(--gray-500);
}

.text-primary {
    color: var(--primary-color);
}

.text-danger {
    color: var(--danger-color);
}

.text-success {
    color: var(--success-color);
}

/* 動畫效果 */
.fade-in {
    animation: fadeIn 0.3s ease-in;
}

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

.slide-down {
    animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* 列印樣式 */
@media print {
    body {
        background: white;
    }

    .container {
        max-width: none;
        padding: 0;
    }

    .form-section,
    .form-actions,
    .preview-header {
        display: none;
    }

    .main-content {
        grid-template-columns: 1fr;
    }

    .preview-section {
        box-shadow: none;
        border: none;
        position: static;
        max-height: none;
    }

    .preview-content {
        padding: 0;
    }

    .invoice-preview {
        font-size: 12pt;
    }
}