:root {
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --bg-color: #f1f5f9;
    --surface: rgba(255, 255, 255, 0.85);
    --border: rgba(200, 200, 200, 0.5);
    --text-main: #1e293b;
    --text-muted: #64748b;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --radius-md: 0.5rem;
    --radius-lg: 1rem;
}

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

body {
    font-family: 'Inter', 'Noto Sans JP', sans-serif;
    background: linear-gradient(135deg, #e0f2fe 0%, #ede9fe 100%);
    color: var(--text-main);
    min-height: 100vh;
    overflow-x: hidden;
}

.app-container {
    display: flex;
    height: 100vh;
    padding: 1rem;
    gap: 1.5rem;
}

/* Sidebar (Glassmorphism) */
.sidebar {
    width: 400px;
    background: var(--surface);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    flex-shrink: 0;
}

.sidebar-header {
    padding: 2rem 1.5rem 1rem;
    border-bottom: 1px solid var(--border);
}

.sidebar-header h1 {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 0.5rem;
}

.sidebar-header p {
    font-size: 0.875rem;
    color: var(--text-muted);
}

.form-container {
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.form-section h2 {
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text-main);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.form-section h2::before {
    content: '';
    display: block;
    width: 4px;
    height: 1rem;
    background-color: var(--primary);
    border-radius: 2px;
}

.form-group {
    margin-bottom: 1rem;
}

label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 0.5rem;
    color: var(--text-main);
}

input[type="text"],
input[type="date"],
input[type="number"],
select {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: var(--radius-md);
    font-family: inherit;
    font-size: 0.875rem;
    transition: all 0.2s ease;
    background: white;
}

input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.2);
}

textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: var(--radius-md);
    font-family: inherit;
    font-size: 0.875rem;
    transition: all 0.2s ease;
    background: white;
    resize: vertical;
}

/* --- Validation Styles --- */
.required-badge {
    font-size: 0.70rem;
    color: #ef4444;
    background: #fee2e2;
    padding: 0.1rem 0.4rem;
    border-radius: var(--radius-sm);
    margin-left: 0.5rem;
    vertical-align: middle;
    font-weight: 600;
}

.recommended-badge {
    font-size: 0.70rem;
    color: #0284c7;
    /* Sky 600 */
    background: #e0f2fe;
    /* Sky 100 */
    padding: 0.1rem 0.4rem;
    border-radius: var(--radius-sm);
    margin-left: 0.5rem;
    vertical-align: middle;
    font-weight: 600;
}

textarea:required:placeholder-shown {
    background-color: #fff0f0;
    border-color: #fca5a5;
}

.checkbox-group {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0;
}

.checkbox-group input[type="checkbox"] {
    width: auto;
    accent-color: var(--primary);
    transform: scale(1.2);
    cursor: pointer;
}

.checkbox-group label {
    margin-bottom: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.btn-secondary {
    width: 100%;
    padding: 0.75rem;
    background: transparent;
    border: 1px dashed var(--primary);
    color: var(--primary);
    font-weight: 500;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s;
}

.btn-secondary:hover {
    background: rgba(79, 70, 229, 0.05);
}

.btn-danger {
    background: transparent;
    border: none;
    color: #ef4444;
    cursor: pointer;
    font-size: 1.25rem;
    padding: 0.25rem;
    line-height: 1;
}

.btn-danger:hover {
    color: #b91c1c;
}

/* Item Entry */
.item-entry {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: var(--radius-md);
    padding: 1rem;
    margin-bottom: 1rem;
    position: relative;
    animation: slideDown 0.3s ease-out forwards;
}

.item-entry-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.5rem;
}

.item-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
}

/* Preview Area */
.preview-area {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    overflow-y: auto;
    padding: 1rem;
}

.invoice-paper {
    background: white;
    width: 100%;
    max-width: 800px;
    min-height: 1131px;
    /* A4 aspect ratio approx */
    padding: 4rem;
    box-shadow: var(--shadow-lg);
    position: relative;
    border-radius: 4px;
    overflow: hidden;
}

.invoice-paper::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 9999;
    background-image: var(--watermark-bg);
    background-repeat: repeat;
    background-size: 500px 200px;
}

.invoice-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 3rem;
    border-bottom: 2px solid #1e293b;
    padding-bottom: 1.5rem;
}

.client-info h2 {
    font-size: 1.5rem;
    font-weight: 600;
    border-bottom: 1px solid #cbd5e1;
    padding-bottom: 0.5rem;
    min-width: 250px;
}

.meta-info {
    text-align: right;
    font-size: 0.875rem;
}

.meta-info h3 {
    margin: 1rem 0 0.25rem;
    font-size: 1.125rem;
    font-weight: 600;
}

.my-name-block {
    position: relative;
    display: inline-block;
}

.company-details {
    margin-top: 0.5rem;
    color: var(--text-muted);
}

.company-details p {
    margin-bottom: 0.2rem;
    font-size: 0.8rem;
}

.sample-stamp {
    position: absolute;
    top: -15px;
    right: -40px;
    width: 60px;
    height: 60px;
    border: 3px solid rgba(220, 38, 38, 0.4);
    border-radius: 50%;
    /* Circle */
    color: rgba(220, 38, 38, 0.6);
    font-weight: 700;
    font-size: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    transform: rotate(-10deg);
    pointer-events: none;
    line-height: 1.1;
    text-align: center;
    word-break: break-all;
    padding: 2px;
}

.invoice-title {
    text-align: center;
    margin-bottom: 3rem;
}

.invoice-title h1 {
    letter-spacing: 0.5em;
    margin-bottom: 0.5rem;
}

.subject-text {
    font-size: 1.125rem;
    font-weight: 600;
    border-bottom: 1px solid #cbd5e1;
    display: inline-block;
    padding-bottom: 0.25rem;
    color: var(--text-main);
    min-width: 200px;
}

.invoice-summary {
    margin-bottom: 2rem;
}

.total-block {
    display: inline-flex;
    flex-direction: column;
    border-bottom: 3px double #1e293b;
    padding: 0.5rem 2rem 0.5rem 0;
}

.total-label {
    font-size: 0.875rem;
    color: var(--text-muted);
}

.total-amount {
    font-size: 2rem;
    font-weight: 700;
}

.items-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 2rem;
    font-size: 0.875rem;
}

.items-table th,
.items-table td {
    border: 1px solid #cbd5e1;
    padding: 0.75rem;
    text-align: right;
}

.items-table th {
    background-color: #f8fafc;
    font-weight: 600;
    text-align: center;
}

.col-desc {
    text-align: left !important;
    width: 40%;
}

.col-qty {
    width: 10%;
}

.col-price {
    width: 20%;
}

.col-taxrate {
    width: 10%;
}

.col-amount {
    width: 20%;
}

.tax-summary-container {
    display: flex;
    justify-content: flex-end;
}

.tax-summary-box {
    width: 60%;
}

.tax-summary-box h4 {
    font-size: 0.875rem;
    margin-bottom: 0.5rem;
}

.tax-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

.tax-table th,
.tax-table td {
    border: 1px solid #cbd5e1;
    padding: 0.5rem;
    text-align: right;
}

.tax-table th {
    background: #f8fafc;
    text-align: center;
}

/* --- Withholding Tax Breakdown Bubble --- */
.withholding-breakdown {
    padding: 1rem;
}

.breakdown-bubble {
    background-color: rgba(254, 242, 242, 0.7);
    /* Translucent fee2e2 (red-50) */
    border: 1px solid rgba(239, 68, 68, 0.3);
    border-radius: 8px;
    padding: 0.75rem 1rem;
    color: #b91c1c;
    font-size: 0.8rem;
    text-align: left;
    margin-bottom: 0.5rem;
}

.breakdown-title {
    font-weight: 700;
    margin-bottom: 0.5rem;
    border-bottom: 1px solid rgba(239, 68, 68, 0.2);
    padding-bottom: 0.25rem;
}

.breakdown-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.25rem;
}

.breakdown-formula {
    text-align: center;
    font-family: 'Courier New', Courier, monospace;
    font-weight: 600;
    margin: 0.5rem 0;
}

.breakdown-total {
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    border-top: 1px dashed rgba(239, 68, 68, 0.3);
    text-align: right;
    font-weight: 700;
    font-size: 0.9rem;
}

/* --- Exempt Logic --- */
body.is-exempt .col-taxrate {
    display: none;
}

body.is-exempt #preview-subtotal-row,
body.is-exempt #preview-tax-row {
    display: none;
}

/* Payment Details */
.payment-details {
    margin-top: 2rem;
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    border-top: 1px dashed #cbd5e1;
    padding-top: 1.5rem;
}

.payment-box {
    flex: 1 1 calc(50% - 1rem);
    min-width: 250px;
}

.remarks-box {
    flex: 1 1 100%;
    border: 1px solid #cbd5e1;
    border-radius: var(--radius-md);
    padding: 1.25rem;
    background: #f8fafc;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.03);
}

.payment-box h4 {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

.payment-box p,
.payment-box pre {
    font-size: 0.875rem;
    line-height: 1.6;
    font-family: inherit;
    white-space: pre-wrap;
    color: var(--text-main);
    margin: 0;
}

/* Animations */
@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.slide-in-left {
    animation: slideInLeft 0.5s ease-out forwards;
}

@keyframes slideInLeft {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }

    to {
        opacity: 1;
        transform: translateX(0);
    }
}

.fade-in {
    animation: fadeIn 0.8s ease-out forwards;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

/* Responsive */
@media (max-width: 1024px) {
    .app-container {
        flex-direction: column;
        height: auto;
    }

    .sidebar {
        width: 100%;
        max-height: 50vh;
    }

    .preview-area {
        overflow: visible;
    }
}