:root {
    --primary: #004098;
    --primary-light: #e6efff;
    --accent: #d32f2f;
    --bg-body: #f4f7fa;
    --text-dark: #1e293b;
    --text-light: #64748b;
    --border: #e2e8f0;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --card-bg: #fff;
}

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

body {
    background: var(--bg-body);
    font-family: 'Noto Sans TC', sans-serif;
    color: var(--text-dark);
    line-height: 1.5;
}

.app-wrapper {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

/* Header */
.app-header {
    text-align: center;
    margin-bottom: 30px;
}

.logo {
    font-family: 'Outfit', sans-serif;
    font-size: 2rem;
    font-weight: 600;
    color: var(--primary);
}

.logo span {
    color: var(--accent);
}

.app-header p {
    color: var(--text-light);
    font-size: 0.9rem;
}

/* Tabs */
.invoice-tabs {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-bottom: 25px;
}

.tab-btn {
    padding: 12px 30px;
    border: 2px solid var(--border);
    background: var(--card-bg);
    border-radius: 12px;
    cursor: pointer;
    font-size: 1.1rem;
    font-weight: 500;
    color: var(--text-light);
    transition: all 0.3s;
    text-align: center;
}

.tab-btn span {
    display: block;
    font-size: 0.75rem;
    font-weight: 400;
}

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

/* Main Layout */
.main-layout {
    display: grid;
    grid-template-columns: 1fr; /* 改為單欄，實現上下排列 */
    gap: 30px;
}

.preview-section {
    width: 100%;
}

.preview-sticky {
    position: relative; /* 移除 sticky，改為隨頁面滾動 */
    top: 0;
}

.card {
    background: var(--card-bg);
    border-radius: 20px;
    padding: 30px;
    box-shadow: var(--shadow);
}

.section-title {
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: 20px;
    padding-left: 12px;
    border-left: 4px solid var(--primary);
}

/* Form Styles */
.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}

.field {
    margin-bottom: 20px;
}

.field label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-light);
    margin-bottom: 8px;
}

input[type="text"], input[type="number"] {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 1rem;
    transition: border-color 0.2s;
}

input:focus {
    outline: none;
    border-color: var(--primary);
}

.amount-input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.currency {
    position: absolute;
    left: 15px;
    color: var(--text-light);
}

.amount-input-wrapper input {
    padding-left: 35px;
}

.divider {
    height: 1px;
    background: var(--border);
    margin: 25px 0;
}

/* Radio Control */
.tax-controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 30px;
    background: var(--primary-light);
    padding: 20px;
    border-radius: 12px;
}

.radios {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 5px;
}

.radios label {
    font-size: 0.95rem;
    color: var(--text-dark);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
}

.calc-grid {
    display: grid;
    grid-template-columns: 1.5fr 1fr 1fr;
    gap: 15px;
}

.field.readonly .val {
    padding: 12px;
    background: #f8fafc;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-weight: 500;
}

/* Total Display */
.total-display {
    margin-top: 30px;
    background: #1e293b;
    padding: 30px;
    border-radius: 16px;
    color: #fff;
    text-align: center;
}

.total-label {
    font-size: 0.9rem;
    opacity: 0.7;
}

.total-val {
    font-size: 2.5rem;
    font-weight: 700;
    margin: 10px 0;
    color: #ffd740;
}

.total-chinese {
    font-size: 1.1rem;
    border-top: 1px solid rgba(255,255,255,0.1);
    padding-top: 15px;
}

/* Preview Section */
.preview-sticky {
    position: sticky;
    top: 20px;
}

.invoice-paper {
    background: #fff;
    border: 3px solid #60a5fa; /* 淺藍色粗外框 */
    border-radius: 4px;
    padding: 25px;
    position: relative;
    box-shadow: 0 10px 25px -5px rgba(0,0,0,0.1);
    color: #1e3a8a; /* 深藍色文字 */
    width: 100%;
    max-width: 800px; /* 增加寬度限制以在上下佈局中更醒目 */
    margin: 0 auto;
}

.upper-meta {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 10px;
}

.invoice-no-main {
    font-size: 1.5rem;
    font-weight: 700;
}

.invoice-title-wrapper {
    text-align: center;
    flex-grow: 1;
}

.paper-title {
    font-size: 1.4rem;
    font-weight: 700;
    letter-spacing: 4px;
}

.paper-year-month {
    font-size: 0.9rem;
    margin-top: 5px;
}

.invoice-date-line {
    margin: 10px 0;
    font-size: 1rem;
    border-bottom: 1px solid #94a3b8;
    padding-bottom: 2px;
}

.p-value {
    color: #2563eb;
    font-weight: 500;
}

.invoice-meta-grid {
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    margin-bottom: 8px;
}

.char-box-container {
    display: inline-flex;
    gap: 2px;
    vertical-align: middle;
}

.char-box {
    width: 20px;
    height: 20px;
    border: 1px solid #3b82f6;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
}

.address-row {
    font-size: 0.8rem;
    border-bottom: 1px solid #94a3b8;
    padding-bottom: 2px;
    margin-bottom: 15px;
}

.preview-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    border: 1px solid #3b82f6;
}

.preview-table th, .preview-table td {
    border: 1px solid #3b82f6;
    padding: 8px;
    text-align: center;
    height: 35px;
}

.preview-table th {
    font-weight: 600;
}

.preview-table td:first-child {
    text-align: left;
}

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

.tax-rate-row {
    display: flex;
    gap: 15px;
    align-items: center;
    font-size: 0.8rem;
}

.tax-option {
    display: flex;
    align-items: center;
    gap: 5px;
}

.tax-option span {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 1px solid #3b82f6;
    position: relative;
}

.tax-option span.check::before {
    content: "✓";
    position: absolute;
    top: -4px;
    left: 1px;
    color: #2563eb;
    font-weight: 700;
}

.total-big {
    font-size: 1.1rem;
    font-weight: 700;
    color: #2563eb;
}

.stamp-cell {
    position: relative;
    vertical-align: top;
    padding-top: 10px !important;
}

.stamp-box {
    width: 120px;
    height: 120px;
    border: 2px solid rgba(37, 99, 235, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-size: 0.9rem;
    color: rgba(37, 99, 235, 0.4);
    margin: 0 auto 5px;
    line-height: 1.4;
    position: relative;
    z-index: 1;
}

.stamp-hint {
    font-size: 0.65rem;
    color: #64748b;
    position: relative;
    z-index: 2;
}

.chinese-total-row {
    display: flex;
    border: 1px solid #3b82f6;
    border-top: none;
    align-items: center;
}

.chinese-label {
    width: 100px;
    padding: 5px;
    font-size: 0.75rem;
    text-align: center;
    border-right: 1px solid #3b82f6;
}

.chinese-values {
    flex-grow: 1;
    display: flex;
    padding: 0 10px;
    justify-content: space-around;
    font-size: 0.9rem;
}

.digit-box {
    position: relative;
    padding: 0 5px;
}

.val-char {
    color: #2563eb;
    font-weight: 700;
    min-width: 1em;
    display: inline-block;
    text-decoration: underline;
    text-align: center;
}

.paper-footer {
    margin-top: 10px;
}

.footer-note {
    font-size: 0.65rem;
    color: #64748b;
}

.footer-copy-info {
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    font-weight: 700;
    font-size: 1rem;
}

.copy-count { color: #1e3a8a; }
.hint-label { color: #2563eb; }

/* Guide */
.guide-box {
    margin-top: 20px;
    background: #fff8e1;
    border: 1px solid #ffe082;
    padding: 15px;
    border-radius: 12px;
}

.guide-title {
    font-weight: 700;
    font-size: 0.9rem;
    margin-bottom: 8px;
    color: #f57c00;
}

.guide-box ul {
    list-style: none;
    font-size: 0.85rem;
}

.guide-box li {
    margin-bottom: 4px;
    padding-left: 15px;
    position: relative;
}

.guide-box li::before {
    content: "•";
    position: absolute;
    left: 0;
    color: #f57c00;
}

.print-btn {
    width: 100%;
    margin-top: 15px;
    padding: 12px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
}

@media print {
    .form-section, .invoice-tabs, .app-header, .app-footer { display: none; }
    .main-layout { display: block; }
    .invoice-paper { border: none; box-shadow: none; }
}

.tax-type-row {
    display: flex;
    border: 1px solid #3b82f6;
    border-top: none;
    align-items: center;
    font-size: 0.85rem;
}

.tax-type-label {
    width: 100px;
    padding: 8px;
    text-align: center;
    border-right: 1px solid #3b82f6;
    letter-spacing: 2px;
}

.tax-type-options {
    flex-grow: 1;
    display: flex;
    padding: 0 15px;
    justify-content: space-around;
}

.tax-type-item {
    display: flex;
    align-items: center;
    gap: 10px;
}

.tax-type-item span {
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 1px solid #3b82f6;
    position: relative;
}

.tax-type-item span.check::before {
    content: "✓";
    position: absolute;
    top: -4px;
    left: 2px;
    color: #2563eb;
    font-weight: 700;
}

/* RWD 支援與發票縮放 */
@media (max-width: 850px) {
    .invoice-paper {
        transform: scale(0.9);
        transform-origin: top center;
        margin-bottom: -50px; /* 補償縮放後的空白 */
    }
}

@media (max-width: 650px) {
    .app-wrapper {
        padding: 10px;
    }
    
    .card {
        padding: 20px;
    }

    .preview-section {
        overflow-x: auto; /* 手機版若發票太寬則允許左右捲動 */
        padding-bottom: 20px;
    }

    .invoice-paper {
        transform: scale(0.7);
        transform-origin: top left;
        width: 600px; /* 保持固定寬度以維持比例，依靠 overflow-x 捲動 */
        margin-bottom: -150px;
    }
    
    .grid-2, .calc-grid {
        grid-template-columns: 1fr;
    }
}

/* Footer */
.app-footer {
    text-align: center;
    margin-top: 40px;
    padding: 20px 0;
    border-top: 1px solid var(--border);
    color: var(--text-light);
    font-size: 0.9rem;
}

.app-footer a {
    color: var(--primary);
    text-decoration: none;
    font-weight: 500;
}

.app-footer a:hover {
    text-decoration: underline;
}
