/* ===== Base Styles ===== */
:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --success-color: #10b981;
    --secondary-color: #6b7280;
    --danger-color: #ef4444;
    --border-color: #e5e7eb;
    --bg-color: #f9fafb;
    --text-color: #1f2937;
    --text-muted: #6b7280;
}

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

body {
    font-family: 'Helvetica Neue', Arial, 'Hiragino Kaku Gothic ProN', 'Hiragino Sans', Meiryo, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
}

/* ===== Header ===== */
header {
    text-align: center;
    padding: 30px 0;
    border-bottom: 2px solid var(--border-color);
    margin-bottom: 30px;
}

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

.subtitle {
    color: var(--text-muted);
    font-size: 0.95rem;
}

/* ===== Main Content ===== */
.main-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
}

@media (max-width: 1024px) {
    .main-content {
        grid-template-columns: 1fr;
    }
}

/* ===== Form Section ===== */
.form-section {
    background: white;
    border-radius: 12px;
    padding: 25px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.form-section h2 {
    font-size: 1.25rem;
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--primary-color);
}

.form-group {
    margin-bottom: 25px;
}

.form-group h3 {
    font-size: 1rem;
    color: var(--text-muted);
    margin-bottom: 12px;
    font-weight: 600;
}

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

.form-row {
    display: flex;
    gap: 15px;
    margin-bottom: 12px;
}

.form-row.item-row {
    margin-bottom: 8px;
}

.form-field {
    flex: 1;
}

.form-field.full-width {
    flex: 100%;
}

.form-field.item-name {
    flex: 3;
}

.form-field.item-qty {
    flex: 0.8;
}

.form-field.item-unit {
    flex: 1;
}

.form-field.item-price {
    flex: 1.5;
}

.form-field.item-action {
    flex: 0.5;
    display: flex;
    align-items: flex-end;
}

label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 5px;
    color: var(--text-muted);
}

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

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

input:invalid:not(:placeholder-shown) {
    border-color: var(--danger-color);
}

textarea {
    resize: vertical;
    min-height: 80px;
}

/* ===== Template Selector ===== */
.template-selector {
    margin-bottom: 25px;
}

.template-selector label {
    margin-bottom: 10px;
    font-size: 0.95rem;
}

.template-options {
    display: flex;
    gap: 10px;
}

.template-btn {
    flex: 1;
    padding: 15px 10px;
    border: 2px solid var(--border-color);
    background: white;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

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

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

.preview-icon {
    width: 40px;
    height: 30px;
    border-radius: 4px;
}

.simple-icon {
    background: white;
    border: 2px solid #ddd;
}

.formal-icon {
    background: linear-gradient(to bottom, #1a1a1a 20%, white 20%);
}

.modern-icon {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.template-btn.active .simple-icon {
    border-color: white;
}

/* ===== Line Items ===== */
.line-item {
    margin-bottom: 8px;
}

.remove-item {
    width: 36px;
    height: 38px;
    border: 1px solid var(--border-color);
    background: #fff0f0;
    border-radius: 6px;
    color: var(--danger-color);
    cursor: pointer;
    font-size: 1.2rem;
    transition: all 0.2s;
}

.remove-item:hover:not(:disabled) {
    background: var(--danger-color);
    color: white;
}

.remove-item:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.add-item-btn {
    padding: 10px 20px;
    border: 2px dashed var(--border-color);
    background: transparent;
    border-radius: 8px;
    color: var(--primary-color);
    cursor: pointer;
    font-weight: 500;
    transition: all 0.2s;
    margin-top: 10px;
}

.add-item-btn:hover {
    border-color: var(--primary-color);
    background: #f0f7ff;
}

/* ===== Action Buttons ===== */
.action-buttons {
    display: flex;
    gap: 12px;
    margin-top: 30px;
    flex-wrap: wrap;
}

.btn {
    flex: 1;
    min-width: 140px;
    padding: 14px 20px;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

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

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
    transform: translateY(-1px);
}

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

.btn-success:hover:not(:disabled) {
    background: #059669;
    transform: translateY(-1px);
}

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

.btn-secondary:hover:not(:disabled) {
    background: #4b5563;
    transform: translateY(-1px);
}

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

.preview-section h2 {
    font-size: 1.25rem;
    margin-bottom: 15px;
}

.invoice-preview {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    overflow: hidden;
}

/* ===== Invoice Templates ===== */
.invoice-template {
    padding: 30px;
    min-height: 800px;
}

/* Simple Template */
.template-simple .invoice-template {
    background: white;
}

.template-simple .invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding-bottom: 15px;
    border-bottom: 2px solid var(--primary-color);
}

.template-simple .invoice-title {
    font-size: 2rem;
    font-weight: bold;
    color: var(--primary-color);
}

.template-simple .invoice-meta {
    text-align: right;
}

.template-simple .meta-row {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-bottom: 5px;
}

.template-simple .meta-row .label {
    color: var(--text-muted);
}

/* ===== Parties ===== */
.invoice-parties {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    margin-bottom: 30px;
}

.party {
    padding: 15px;
    border-radius: 8px;
}

.party-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    margin-bottom: 5px;
}

.party-name {
    font-size: 1.1rem;
    font-weight: bold;
    margin-bottom: 5px;
}

.party-address {
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* ===== Invoice Items Table ===== */
.invoice-items table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 20px;
}

.invoice-items thead {
    background: #f3f4f6;
}

.invoice-items th {
    padding: 12px;
    text-align: left;
    font-weight: 600;
    font-size: 0.85rem;
    border-bottom: 2px solid var(--border-color);
}

.invoice-items th:last-child {
    text-align: right;
}

.invoice-items td {
    padding: 12px;
    border-bottom: 1px solid var(--border-color);
}

.invoice-items td:last-child {
    text-align: right;
}

.empty-row td {
    text-align: center;
    color: var(--text-muted);
    padding: 30px;
}

/* ===== Totals ===== */
.invoice-totals {
    max-width: 300px;
    margin-left: auto;
}

.total-row {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid var(--border-color);
}

.total-row.grand-total {
    font-size: 1.3rem;
    font-weight: bold;
    border-bottom: none;
    background: #f3f4f6;
    padding: 15px;
    border-radius: 8px;
    margin-top: 10px;
}

.template-formal .total-row.grand-total {
    background: var(--primary-color);
    color: white;
}

.template-modern .total-row.grand-total {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

/* ===== Notes ===== */
.invoice-notes {
    margin-top: 30px;
    padding: 15px;
    background: #f9fafb;
    border-radius: 6px;
    font-size: 0.85rem;
    white-space: pre-wrap;
}

/* ===== Formal Template ===== */
.template-formal .invoice-template {
    background: white;
}

.template-formal .invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding-bottom: 15px;
    border-bottom: 3px double #1a1a1a;
}

.template-formal .invoice-title {
    font-size: 2.2rem;
    font-weight: bold;
    color: #1a1a1a;
    letter-spacing: 0.3em;
}

.template-formal .invoice-meta .meta-row {
    font-family: 'Times New Roman', serif;
}

.template-formal .party {
    background: #fafafa;
    border: 1px solid #e5e5e5;
}

.template-formal .party-name {
    font-family: 'Times New Roman', serif;
}

.template-formal .invoice-items thead {
    background: #1a1a1a;
    color: white;
}

.template-formal .invoice-items th {
    font-family: 'Times New Roman', serif;
}

.template-formal .invoice-items th:last-child,
.template-formal .invoice-items td:last-child {
    text-align: right;
}

/* ===== Modern Template ===== */
.template-modern .invoice-template {
    background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
    position: relative;
}

.template-modern .invoice-template::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 8px;
    background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
}

.template-modern .invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding-bottom: 20px;
}

.template-modern .invoice-title {
    font-size: 2.5rem;
    font-weight: 300;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.template-modern .party {
    background: white;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.15);
    border-radius: 12px;
}

.template-modern .party-name {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    font-weight: 700;
}

.template-modern .invoice-items thead {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.template-modern .invoice-items th {
    font-weight: 500;
}

.template-modern .invoice-notes {
    background: white;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.1);
}

/* ===== Footer ===== */
footer {
    text-align: center;
    padding: 30px 0;
    margin-top: 40px;
    color: var(--text-muted);
    font-size: 0.85rem;
    border-top: 1px solid var(--border-color);
}

/* ===== Print Styles ===== */
@media print {
    body {
        background: white;
    }

    .container {
        max-width: 100%;
        padding: 0;
    }

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

    .main-content {
        display: block;
    }

    .preview-section {
        position: static;
    }

    .invoice-preview {
        box-shadow: none;
    }

    .invoice-template {
        padding: 0;
        min-height: auto;
    }

    @page {
        size: A4 portrait;
        margin: 15mm;
    }
}

/* ===== Mobile Responsive ===== */
@media (max-width: 768px) {
    .container {
        padding: 15px;
    }

    header h1 {
        font-size: 1.5rem;
    }

    .form-section,
    .invoice-template {
        padding: 15px;
    }

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

    .form-row.item-row {
        flex-wrap: wrap;
    }

    .form-field.item-name,
    .form-field.item-qty,
    .form-field.item-unit,
    .form-field.item-price,
    .form-field.item-action {
        flex: 1 1 45%;
    }

    .form-field.item-name {
        flex: 1 1 100%;
    }

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

    .action-buttons {
        flex-direction: column;
    }

    .btn {
        width: 100%;
    }

    .template-options {
        flex-direction: column;
    }

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

    .invoice-totals {
        max-width: 100%;
    }
}
