:root {
    /* Header Theme (from index.html) - Light Aesthetic */
    --header-bg: rgba(246, 248, 251, 0.85);
    --header-line: rgba(15, 23, 42, 0.1);
    --header-text: #0f172a;

    /* Premium Dark Theme for Editor */
    --bg-app: #0f172a;
    --bg-panel: #1e293b;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --border-color: #334155;
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --danger: #ef4444;

    /* Invoice Paper Theme - Premium */
    --paper-bg: #ffffff;
    --paper-text: #334155;
    --paper-accent: #0f172a;
    --paper-accent-light: #f1f5f9;
    --paper-border: #e2e8f0;
    --paper-highlight: #3b82f6;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 1000;
    backdrop-filter: blur(10px);
    background: var(--header-bg);
    border-bottom: 1px solid var(--header-line);
    color: var(--header-text);
}

.topbar .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 1.5rem;
}

.topbar .nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.85rem 0;
    gap: 1rem;
}

.topbar .brand {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    font-weight: 800;
    text-decoration: none;
    color: var(--header-text);
}

.topbar .brand img {
    width: 28px;
    height: 28px;
}

.topbar .brand span {
    font-size: 1.125rem;
    font-family: 'Inter', sans-serif;
    letter-spacing: -0.01em;
}

.topbar .navlinks {
    display: flex;
    gap: 1.5rem;
    font-weight: 600;
    font-size: 0.875rem;
}

.topbar .navlinks a {
    text-decoration: none;
    color: #475569;
    transition: color 0.2s, background 0.2s;
    padding: 6px 10px;
    border-radius: 8px;
}

.topbar .navlinks a:hover {
    color: var(--header-text);
    background: rgba(15, 23, 42, 0.05);
}

.topbar .navright {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.topbar .pill {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    border: 1px solid var(--header-line);
    padding: 0.45rem 0.85rem;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.6);
    color: #475569;
    font-weight: 700;
    font-size: 0.8125rem;
}

.topbar .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1rem;
    border-radius: 10px;
    font-weight: 700;
    font-size: 0.875rem;
    text-decoration: none;
    transition: all 0.2s;
}

.topbar .btn-ghost {
    background: transparent;
    border: 1px solid var(--header-line);
    color: #475569;
}

.topbar .btn-ghost:hover {
    background: rgba(15, 23, 42, 0.05);
    color: var(--header-text);
}

.topbar .btn-header-primary {
    background: #16a34a;
    color: white;
}

.topbar .btn-header-primary:hover {
    background: #15803d;
    box-shadow: 0 4px 12px rgba(22, 163, 74, 0.2);
}

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

body {
    font-family: 'Inter', sans-serif;
    color: #1e293b;
    margin: 0;
    padding: 0;
}

body.editor-mode {
    background-color: var(--bg-app);
    color: var(--text-main);
    height: 100vh;
    overflow: hidden;
}


.app-container {
    display: flex;
    height: calc(100vh - 63px);
}

/* --- Editor Panel --- */
.editor-panel {
    width: 400px;
    background-color: var(--bg-panel);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    /* overflow: hidden; Removed to allow absolute panels to show */
    flex-shrink: 0;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.3);
    z-index: 10;
    position: relative;
}

.editor-scroll {
    flex: 1;
    overflow-y: auto;
}

.editor-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
    background: rgba(30, 41, 59, 0.95);
    backdrop-filter: blur(8px);
    position: sticky;
    top: 0;
    z-index: 20;
}

.editor-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.editor-header h1 {
    font-family: 'Outfit', sans-serif;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-main);
    margin: 0;
}

.language-switcher {
    display: none !important;
}

.editor-header p {
    color: var(--text-muted);
    font-size: 0.875rem;
    margin-top: 0.25rem;
}

#invoiceForm {
    padding: 1.5rem;
    flex: 1;
}

.form-section {
    margin-bottom: 2rem;
}

.form-section h2 {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 1rem;
    font-weight: 600;
}

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

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

.form-group label {
    display: block;
    font-size: 0.875rem;
    margin-bottom: 0.5rem;
    color: var(--text-muted);
}

input,
select,
textarea {
    width: 100%;
    background-color: var(--bg-app);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    padding: 0.625rem;
    border-radius: 0.375rem;
    font-family: inherit;
    font-size: 0.875rem;
    transition: border-color 0.2s, box-shadow 0.2s;
}

input:focus,
select:focus,
textarea:focus,
button:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

/* Custom Calendar Icon Color */
input[type="date"]::-webkit-calendar-picker-indicator {
    filter: invert(1) brightness(0.9) sepia(1) saturate(5) hue-rotate(175deg);
    /* Light Blue / Cyan Aesthetic */
    cursor: pointer;
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.item-row {
    background-color: var(--bg-app);
    border: 1px solid var(--border-color);
    padding: 1rem;
    border-radius: 0.5rem;
    margin-bottom: 1rem;
    position: relative;
    animation: slideIn 0.2s ease-out;
}

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

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

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

.btn-remove {
    background: none;
    border: none;
    color: var(--danger);
    cursor: pointer;
    font-size: 0.875rem;
    padding: 0.25rem;
    transition: opacity 0.2s;
}

.btn-remove:hover,
.btn-remove:focus {
    text-decoration: underline;
    opacity: 0.8;
    outline: 2px solid var(--danger);
    outline-offset: 2px;
    border-radius: 2px;
}

.btn-secondary {
    background: none;
    border: 1px dashed var(--border-color);
    color: var(--primary);
    padding: 0.5rem 1rem;
    border-radius: 0.375rem;
    cursor: pointer;
    font-size: 0.875rem;
    transition: all 0.2s;
}

.btn-secondary:hover,
.btn-secondary:focus {
    background-color: rgba(59, 130, 246, 0.1);
    border-color: var(--primary);
}

.btn-secondary:active {
    transform: scale(0.98);
}

.editor-footer {
    padding: 1.5rem;
    border-top: 1px solid var(--border-color);
    background: var(--bg-panel);
    position: sticky;
    bottom: 0;
    overflow: visible;
    /* Important for absolute children */
    z-index: 50;
}

/* Template Selection Styles */
.btn-template-selector {
    width: 100%;
    padding: 0.85rem;
    background: rgba(59, 130, 246, 0.08);
    /* Subtle blue tint */
    border: 1px dashed rgba(59, 130, 246, 0.3);
    border-radius: 12px;
    color: var(--primary);
    text-align: center;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
    margin-bottom: 1.5rem;
    font-size: 0.875rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.btn-template-selector:hover {
    background: rgba(59, 130, 246, 0.15);
    border-style: solid;
    transform: translateY(-1px);
}

.btn-template-selector::before {
    content: '🎨';
    /* Paint palette icon */
    font-size: 1rem;
}

.template-panel {
    position: absolute;
    bottom: 100%;
    /* Sits above footer */
    left: 0;
    right: 0;
    background: var(--bg-panel);
    border-top: 1px solid var(--border-color);
    box-shadow: 0 -15px 40px rgba(0, 0, 0, 0.4);
    z-index: 1000;
    visibility: hidden;
    opacity: 0;
    transform: translateY(20px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    padding-bottom: 2rem;
    height: 550px;
    display: flex;
    flex-direction: column;
}

.template-panel.active {
    visibility: visible;
    opacity: 1;
    transform: translateY(0);
}

.template-panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.template-panel-header span {
    font-size: 0.875rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.btn-close-panel {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.75rem;
    cursor: pointer;
    line-height: 1;
}

.template-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1.25rem;
    padding: 1.5rem;
    overflow-y: auto;
    flex: 1;
    /* Take up remaining space */
}

.template-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
}

.template-option {
    cursor: pointer;
    border-radius: 12px;
    overflow: hidden;
    border: 3px solid rgba(255, 255, 255, 0.05);
    /* Very subtle border */
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    background: #1e293b;
    padding: 0;
    /* No padding inside, image fills it */
    width: 100%;
    aspect-ratio: 3/4;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

.premium-badge {
    position: absolute;
    top: 8px;
    right: 8px;
    background: #a855f7;
    color: white;
    font-size: 8px;
    font-weight: 900;
    padding: 2px 6px;
    border-radius: 4px;
    letter-spacing: 0.5px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    z-index: 5;
}

.template-premium .template-option {
    border-color: rgba(168, 85, 247, 0.2);
}

.template-premium:hover .template-option {
    border-color: #a855f7;
}

.template-thumbnail {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: top;
    background: white;
    display: block;
}

.template-preview-fallback {
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    color: var(--primary);
    font-size: 2rem;
}

.template-item span {
    font-size: 13px;
    font-weight: 700;
    color: #f8fafc;
    /* Bright white/gray for clarity */
    text-transform: capitalize;
}

.template-item:hover .template-option {
    transform: translateY(-4px);
    border-color: rgba(59, 130, 246, 0.5);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.4);
}

.template-item.active .template-option {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.3), 0 10px 20px rgba(0, 0, 0, 0.4);
}

.template-item.active span {
    color: #60a5fa;
}

.footer-buttons-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

.btn-primary {
    width: 100%;
    background-color: var(--primary);
    color: white;
    border: none;
    padding: 0.75rem;
    border-radius: 0.5rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s;
}

.btn-primary:hover,
.btn-primary:focus {
    background-color: var(--primary-hover);
}

.btn-primary:active {
    transform: scale(0.98);
}

/* Keyboard navigation improvements */
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* --- Preview Panel --- */
.preview-panel {
    flex: 1;
    background-color: #525659;
    /* Browser PDF viewer bg color */
    padding: 2rem;
    overflow-y: auto;
    display: block;
    /* Changed from flex to block to allow proper scrolling/expansion */
}

.preview-wrapper {
    width: 210mm;
    /* A4 width */
    min-height: 297mm;
    /* A4 height */
    background: white;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    margin: 0 auto 2rem auto;
    /* Center horizontally */
}

/* --- Invoice Paper Design --- */
.invoice-paper {
    padding: 15mm;
    color: #333;
    font-size: 10pt;
    line-height: 1.4;
    position: relative;
    min-height: 100%;
    /* Ensure it fills the wrapper */
    height: auto;
    /* Allow it to grow */
    display: flex;
    flex-direction: column;
    font-family: 'Inter', sans-serif;
}

/* Orange Bar */
.header-bar {
    width: 100%;
    height: 8px;
    background-color: #ff6600;
    margin-bottom: 15px;
}

.invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 40px;
    margin-top: 20px;
}

.logo {
    max-width: 200px;
    max-height: 80px;
}

.text-logo {
    font-family: 'Outfit', sans-serif;
    font-weight: 700;
    font-size: 1.5rem;
    color: var(--paper-accent, #0f172a);
    text-align: left;
    line-height: 1.2;
    margin-bottom: 10px;
}

.header-right {
    text-align: right;
    width: 60%;
    /* Give it specific width to align bar */
}

.header-date {
    font-size: 24pt;
    font-weight: 800;
    color: #ff6600;
    margin-bottom: 5px;
}

.header-note {
    font-size: 9pt;
    color: #666;
}

.header-meta-row {
    font-size: 10pt;
    font-weight: 600;
    color: #0f172a;
    padding-top: 5px;
    text-transform: uppercase;
}

.meta-separator {
    margin: 0 5px;
    color: #ccc;
}

/* Info Section */
.info-total-section {
    display: flex;
    justify-content: space-between;
    margin-bottom: 40px;
    align-items: flex-start;
}

.info-col-left {
    width: 45%;
}

.company-info-block {
    border-left: 4px solid #0f172a;
    padding-left: 15px;
    font-size: 9pt;
    color: #444;
    line-height: 1.5;
}

.company-name {
    font-weight: 800;
    color: #0f172a;
    text-transform: uppercase;
    margin-bottom: 5px;
}

.info-col-right {
    width: 45%;
    text-align: left;
}

.customer-info-block-right {
    font-size: 10pt;
    color: #333;
}

.sayin-label {
    font-size: 8pt;
    color: #999;
    font-weight: 600;
    text-transform: uppercase;
    margin-bottom: 5px;
    letter-spacing: 1px;
}

.customer-name-right {
    font-weight: 800;
    font-size: 11pt;
    margin-bottom: 5px;
    color: #0f172a;
    text-transform: uppercase;
}

.customer-address-right {
    margin-bottom: 5px;
    color: #555;
    white-space: pre-line;
}

.customer-phone-right {
    color: #555;
    font-weight: 500;
}

/* Items Table */
.items-section {
    margin-bottom: 30px;
}

.table-header-row {
    display: flex;
    border-bottom: 2px solid #0f172a;
    /* Dark blue border */
    padding-bottom: 10px;
    margin-bottom: 15px;
    font-weight: 700;
    color: #0f172a;
    font-size: 9pt;
    text-transform: uppercase;
}

.th-desc {
    width: 50%;
}

.th-price {
    width: 15%;
    text-align: right;
}

.th-qty {
    width: 15%;
    text-align: center;
}

.th-total {
    width: 20%;
    text-align: right;
}

.item-row-display {
    display: flex;
    padding: 15px 0;
    border-bottom: 1px solid #e2e8f0;
    align-items: center;
}

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

.col-desc {
    width: 50%;
    padding-left: 0;
    font-weight: 700;
    color: #444;
}

.col-price {
    width: 15%;
    text-align: right;
    color: #444;
    font-size: 9pt;
}

.col-qty {
    width: 15%;
    text-align: center;
    color: #444;
}

.col-total {
    width: 20%;
    text-align: right;
    padding-right: 0;
    font-weight: 700;
    color: #444;
}

/* Totals Section */
.totals-section {
    display: flex;
    justify-content: space-between;
    margin-bottom: 40px;
    align-items: flex-start;
}

.notes-col {
    width: 55%;
    padding-right: 20px;
}

.notes-content {
    font-size: 9pt;
    color: #555;
    white-space: pre-line;
    line-height: 1.4;
}

.totals-col-right {
    width: 40%;
}

.totals-table {
    width: 100%;
    border-collapse: collapse;
}

.totals-table th {
    text-align: left;
    padding: 8px 0;
    font-size: 8pt;
    color: #444;
    font-weight: 700;
    text-transform: uppercase;
}

.totals-table td {
    text-align: right;
    padding: 8px 0;
    font-weight: 800;
    color: #333;
}

.totals-table tr {
    border-bottom: 1px dotted #ccc;
}

.totals-table tr:last-child {
    border-bottom: none;
}

/* Footer */
.invoice-footer {
    margin-top: auto;
    border-top: 1px solid #999;
    padding-top: 20px;
    display: flex;
    align-items: center;
}

.footer-logo {
    margin-right: 20px;
}

.footer-logo-img {
    height: 40px;
}

.footer-logo .text-logo {
    font-size: 1rem;
    font-weight: 600;
    margin: 0;
    color: #333;
}

.footer-info {
    font-size: 8pt;
    color: #666;
    line-height: 1.4;
}

.footer-info strong {
    color: #333;
}

/* --- Print Optimization --- */
@media print {
    @page {
        size: A4;
        margin: 0;
    }

    body {
        background: white;
        height: auto;
        overflow: visible;
    }

    .app-container {
        display: block;
    }

    .editor-panel {
        display: none;
    }

    .preview-panel {
        padding: 0;
        background: white;
        overflow: visible;
        display: block;
    }

    .preview-wrapper {
        width: 100%;
        box-shadow: none;
        margin: 0;
    }

    .invoice-paper {
        padding: 15mm;
        /* Standard A4 margins */
        min-height: 297mm;
    }

    /* Ensure colors print correctly */
    * {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }
}

/* --- Notification Toast --- */
.notification {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    background-color: var(--bg-panel);
    color: var(--text-main);
    padding: 1rem 1.5rem;
    border-radius: 0.5rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    z-index: 1000;
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 0.3s, transform 0.3s;
    pointer-events: none;
    border: 1px solid var(--border-color);
    max-width: 300px;
}

.notification.show {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.notification-success {
    border-left: 4px solid #10b981;
}

.notification-error {
    border-left: 4px solid var(--danger);
}

.notification-info {
    border-left: 4px solid var(--primary);
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .editor-panel {
        width: 100%;
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        z-index: 100;
        transform: translateX(-100%);
        transition: transform 0.3s;
    }

    .editor-panel.open {
        transform: translateX(0);
    }

    .preview-panel {
        width: 100%;
    }

    .notification {
        right: 1rem;
        left: 1rem;
        max-width: none;
    }
}