/* ============================================================
   DrawTools Invoice Builder – Premium Styles
   ============================================================ */

/* ---- Reset ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ---- Design Tokens ---- */
:root {
    --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-doc: 'Open Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

    /* Brand / Accent */
    --accent: #7C3AED;
    --accent-light: #EDE9FE;
    --accent-dark: #5B21B6;
    --accent-glow: rgba(124, 58, 237, 0.25);

    /* Neutrals */
    --white: #ffffff;
    --black: #000000;
    --gray-50: #F9FAFB;
    --gray-100: #F3F4F6;
    --gray-200: #E5E7EB;
    --gray-300: #D1D5DB;
    --gray-400: #9CA3AF;
    --gray-500: #6B7280;
    --gray-600: #4B5563;
    --gray-700: #374151;
    --gray-800: #1F2937;
    --gray-900: #111827;

    /* Semantic */
    --danger: #EF4444;
    --danger-light: #FEF2F2;
    --success: #10B981;

    /* Shadows */
    --shadow-xs: 0 1px 2px rgba(0,0,0,0.04);
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.08), 0 2px 4px rgba(0,0,0,0.04);
    --shadow-lg: 0 10px 30px rgba(0,0,0,0.10), 0 4px 8px rgba(0,0,0,0.05);
    --shadow-xl: 0 20px 50px rgba(0,0,0,0.12), 0 8px 16px rgba(0,0,0,0.06);
    --shadow-accent: 0 4px 20px var(--accent-glow);

    /* Radius */
    --r-xs: 4px;
    --r-sm: 6px;
    --r-md: 10px;
    --r-lg: 14px;
    --r-xl: 20px;
    --r-full: 9999px;

    /* Transitions */
    --t-fast: 0.15s ease;
    --t-base: 0.2s ease;
    --t-slow: 0.35s ease;
}

/* ---- Base ---- */
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }

body {
    font-family: var(--font-ui);
    background: #F0EDF8;
    background-image:
        radial-gradient(ellipse at 20% 0%, rgba(124,58,237,0.08) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 100%, rgba(99,102,241,0.06) 0%, transparent 50%);
    background-attachment: fixed;
    color: var(--gray-900);
    min-height: 100vh;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ============================================================
   TOOLBAR
   ============================================================ */
.toolbar {
    position: sticky;
    top: 0;
    z-index: 200;
    background: rgba(255,255,255,0.80);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border-bottom: 1px solid rgba(124,58,237,0.12);
    padding: 0 20px;
    height: 58px;
}

.toolbar-inner {
    max-width: 960px;
    margin: 0 auto;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.toolbar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    flex-shrink: 0;
}

.toolbar-logo {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid var(--accent-light);
}

.toolbar-brand-text {
    display: flex;
    flex-direction: column;
    line-height: 1.2;
}

.toolbar-title {
    font-size: 13px;
    font-weight: 700;
    color: var(--gray-900);
    letter-spacing: -0.2px;
}

.toolbar-sub {
    font-size: 10px;
    font-weight: 500;
    color: var(--accent);
    letter-spacing: 0.3px;
    text-transform: uppercase;
}

.toolbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    border-radius: var(--r-sm);
    font-size: 13px;
    font-weight: 600;
    font-family: var(--font-ui);
    cursor: pointer;
    transition: all var(--t-base);
    border: 1px solid transparent;
    white-space: nowrap;
    letter-spacing: -0.1px;
    position: relative;
    overflow: hidden;
}

.btn::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(255,255,255,0);
    transition: background var(--t-fast);
}
.btn:hover::after { background: rgba(255,255,255,0.1); }
.btn:active { transform: scale(0.98); }

.btn-ghost {
    background: rgba(255,255,255,0.7);
    border-color: var(--gray-200);
    color: var(--gray-700);
}
.btn-ghost:hover {
    background: var(--white);
    border-color: var(--gray-300);
    color: var(--gray-900);
    box-shadow: var(--shadow-sm);
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent) 0%, #6D28D9 100%);
    color: var(--white);
    border-color: var(--accent-dark);
    box-shadow: var(--shadow-accent);
}
.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 24px var(--accent-glow);
}

.btn-accent {
    background: linear-gradient(135deg, #0EA5E9 0%, #6366F1 100%);
    color: var(--white);
    border: none;
    font-size: 12px;
    padding: 8px 14px;
    border-radius: var(--r-sm);
    box-shadow: 0 4px 14px rgba(14,165,233,0.3);
}
.btn-accent:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 20px rgba(14,165,233,0.4);
}

.btn-danger {
    background: var(--danger-light);
    color: var(--danger);
    border-color: #FECACA;
}
.btn-danger:hover {
    background: #FEE2E2;
    border-color: #FCA5A5;
}

/* ============================================================
   EXPORT OVERLAY
   ============================================================ */
.export-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 8, 30, 0.65);
    backdrop-filter: blur(8px);
    z-index: 9999;
    align-items: center;
    justify-content: center;
}
.export-overlay.active { display: flex; }

.export-card {
    background: var(--white);
    border-radius: var(--r-xl);
    padding: 40px 52px;
    text-align: center;
    box-shadow: var(--shadow-xl);
    border: 1px solid var(--gray-100);
}

.export-spinner-ring {
    width: 44px;
    height: 44px;
    border: 3px solid var(--accent-light);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 18px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.export-text {
    font-size: 15px;
    font-weight: 700;
    color: var(--gray-900);
    margin-bottom: 4px;
}
.export-sub {
    font-size: 12px;
    color: var(--gray-400);
}

/* ============================================================
   CONTROLS PANEL
   ============================================================ */
.controls-panel {
    background: rgba(255,255,255,0.75);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-bottom: 1px solid rgba(124,58,237,0.10);
    padding: 10px 20px;
}

.controls-inner {
    max-width: 960px;
    margin: 0 auto;
}

.controls-grid {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    flex-wrap: wrap;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 5px;
    flex: 1;
    min-width: 130px;
    max-width: 200px;
}

.control-group--action {
    flex: 0 0 auto;
    min-width: unset;
    max-width: unset;
    justify-content: flex-end;
}

.control-label {
    font-size: 11px;
    font-weight: 600;
    color: var(--gray-500);
    letter-spacing: 0.4px;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    gap: 5px;
}

.control-select-wrap {
    position: relative;
}
.control-select-wrap::after {
    content: '';
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 5px solid var(--gray-500);
    pointer-events: none;
}

.control-select,
.control-input {
    width: 100%;
    height: 36px;
    padding: 0 10px;
    border: 1px solid var(--gray-200);
    border-radius: var(--r-sm);
    background: var(--white);
    font-size: 13px;
    font-family: var(--font-ui);
    font-weight: 500;
    color: var(--gray-800);
    transition: all var(--t-base);
    outline: none;
    appearance: none;
    -webkit-appearance: none;
}
.control-select { padding-right: 28px; cursor: pointer; }

.control-select:focus,
.control-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.control-select:hover,
.control-input:hover {
    border-color: var(--gray-300);
}

.control-custom-input {
    margin-top: 4px;
}

/* ============================================================
   INVOICE WRAPPER
   ============================================================ */
.invoice-wrapper {
    padding: 28px 16px 80px;
    display: flex;
    justify-content: center;
}

.invoice-page {
    width: 100%;
    max-width: 620px;
    background: var(--white);
    box-shadow: var(--shadow-xl);
    border-radius: var(--r-xs);
    padding: 36px 44px 80px;
    position: relative;
    min-height: 877px;
    transition: box-shadow var(--t-slow);
}

.invoice-page:hover {
    box-shadow: 0 24px 60px rgba(0,0,0,0.14), 0 8px 20px rgba(0,0,0,0.07);
}

/* ============================================================
   EDITABLE ELEMENTS
   ============================================================ */
[contenteditable="true"] {
    outline: none;
    border-radius: var(--r-xs);
    transition: background var(--t-base), box-shadow var(--t-base);
    cursor: text;
    min-width: 10px;
}
[contenteditable="true"]:hover {
    background: rgba(124,58,237,0.05);
}
[contenteditable="true"]:focus {
    background: rgba(124,58,237,0.07);
    box-shadow: 0 0 0 2px rgba(124,58,237,0.3);
}

/* ============================================================
   INVOICE HEADER
   ============================================================ */
.invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 14px;
    gap: 20px;
}
.header-left { flex-shrink: 0; }

.company-logo {
    width: 88px;
    height: 88px;
    object-fit: contain;
    border-radius: 50%;
}

.header-right {
    text-align: right;
    flex: 1;
}

.tax-invoice-label {
    font-size: 8.5pt;
    font-weight: 700;
    font-family: var(--font-doc);
    margin-bottom: 2px;
}
.company-name {
    font-size: 9pt;
    font-family: var(--font-doc);
    margin-bottom: 2px;
}
.company-address {
    font-size: 8.5pt;
    font-family: var(--font-doc);
    line-height: 1.45;
    white-space: pre-line;
}

/* ============================================================
   COMPANY DETAILS
   ============================================================ */
.company-details {
    text-align: right;
    margin-bottom: 18px;
    padding-top: 6px;
    border-top: 1px solid var(--gray-200);
}
.detail-row {
    font-size: 8.5pt;
    font-family: var(--font-doc);
    line-height: 1.65;
}
.detail-label { font-weight: 600; }
.detail-value { font-weight: 400; }

/* ============================================================
   INVOICE TITLE
   ============================================================ */
.invoice-title {
    font-size: 15pt;
    font-weight: 800;
    font-family: var(--font-doc);
    letter-spacing: 1px;
    margin-bottom: 18px;
    color: var(--gray-900);
}

/* ============================================================
   META SECTION
   ============================================================ */
.invoice-meta-section {
    display: flex;
    justify-content: space-between;
    margin-bottom: 22px;
    gap: 20px;
}
.customer-info { flex: 1; }

.info-label {
    font-size: 7.5pt;
    font-weight: 700;
    font-family: var(--font-doc);
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--gray-500);
    margin-bottom: 4px;
}
.customer-address {
    font-size: 8.5pt;
    font-family: var(--font-doc);
    line-height: 1.6;
    white-space: pre-line;
}

.invoice-details { flex: 1; text-align: right; }
.meta-row {
    font-size: 8.5pt;
    font-family: var(--font-doc);
    line-height: 1.65;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 6px;
}
.meta-label { font-weight: 600; color: var(--gray-600); }
.meta-value { font-weight: 400; }

/* ---- Date Cell ---- */
.date-cell {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}
.date-display { cursor: text; }
.date-picker {
    position: absolute;
    width: 24px;
    height: 18px;
    right: -26px;
    top: 50%;
    transform: translateY(-50%);
    opacity: 0;
    cursor: pointer;
    border: none;
    background: transparent;
    padding: 0;
}
.date-picker::-webkit-calendar-picker-indicator {
    width: 16px;
    height: 16px;
    cursor: pointer;
    opacity: 0.5;
    filter: invert(35%) sepia(80%) saturate(400%) hue-rotate(240deg);
}
.date-cell::after {
    content: '📅';
    font-size: 11px;
    opacity: 0;
    transition: opacity var(--t-fast);
    pointer-events: none;
    position: absolute;
    right: -20px;
}
.date-cell:hover::after { opacity: 0.6; }

/* ============================================================
   PRODUCTS TABLE
   ============================================================ */
.invoice-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 0;
    font-size: 8.5pt;
    font-family: var(--font-doc);
}

.invoice-table thead tr {
    background: var(--gray-900);
    color: var(--white);
}
.invoice-table thead th {
    padding: 8px 10px;
    text-align: left;
    font-weight: 700;
    font-size: 8pt;
    letter-spacing: 0.3px;
}

.col-product { width: auto; }
.col-qty { width: 50px; text-align: center; }
.col-rate { width: 90px; text-align: right; }
.col-amount { width: 90px; text-align: right; }
.col-actions { width: 30px; padding: 0 !important; }

.invoice-table thead th.col-qty,
.invoice-table thead th.col-rate,
.invoice-table thead th.col-amount { text-align: center; }

.invoice-table tbody td {
    padding: 8px 10px;
    vertical-align: top;
    border-bottom: 1px solid var(--gray-200);
}

.cell-product { max-width: 260px; }
.product-name {
    font-size: 8.5pt;
    line-height: 1.4;
    margin-bottom: 5px;
    font-weight: 600;
}
.product-meta {
    display: flex;
    flex-direction: column;
    gap: 1px;
}
.meta-item {
    font-size: 7pt;
    color: var(--gray-600);
    line-height: 1.4;
}

.cell-qty {
    text-align: center;
    font-size: 8.5pt;
    vertical-align: middle !important;
}
.cell-rate, .cell-amount {
    text-align: right;
    font-size: 8.5pt;
    white-space: nowrap;
    vertical-align: middle !important;
}

.cell-actions {
    text-align: center;
    vertical-align: middle !important;
    padding: 0 4px !important;
}

.btn-remove-row {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--gray-300);
    padding: 3px;
    border-radius: var(--r-xs);
    transition: all var(--t-base);
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.btn-remove-row:hover {
    color: var(--danger);
    background: var(--danger-light);
}

/* ---- Inline Add-Row Bar ---- */
.add-row-bar {
    display: flex;
    justify-content: center;
    padding: 6px 0 2px;
    margin-bottom: 2px;
}

.add-row-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 18px;
    background: transparent;
    border: 1.5px dashed var(--gray-300);
    border-radius: var(--r-sm);
    color: var(--gray-500);
    font-size: 12px;
    font-weight: 600;
    font-family: var(--font-ui);
    cursor: pointer;
    transition: all var(--t-base);
    width: 100%;
    justify-content: center;
    letter-spacing: 0.1px;
}

.add-row-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-light);
}

.add-row-btn:active {
    transform: scale(0.99);
}

/* Row animations */
.product-row {
    transition: background var(--t-fast);
}
.product-row:hover {
    background: var(--gray-50);
}
.product-row:hover [contenteditable]:not(:focus) {
    background: rgba(124,58,237,0.04);
}

/* ============================================================
   TOTALS SECTION
   ============================================================ */
.totals-section {
    margin-left: auto;
    width: 52%;
    margin-top: 0;
    border-top: 2px solid var(--gray-900);
}
.totals-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 8px;
    font-size: 8.5pt;
    font-family: var(--font-doc);
}
.totals-row:not(.totals-grand):not(:last-child) {
    border-bottom: 1px dashed var(--gray-200);
}
.totals-label { font-weight: 400; color: var(--gray-700); }
.totals-value { text-align: right; white-space: nowrap; font-weight: 500; }

.totals-grand {
    font-weight: 700;
    border-top: 2px solid var(--gray-900);
    padding-top: 8px;
    margin-top: 2px;
}
.totals-grand .totals-label,
.totals-grand .totals-value {
    font-weight: 800;
    font-size: 10pt;
}

/* ============================================================
   FOOTER
   ============================================================ */
.invoice-footer {
    position: absolute;
    bottom: 28px;
    left: 44px;
    right: 44px;
    text-align: center;
    border-top: 1px solid var(--gray-200);
    padding-top: 10px;
}
.footer-text {
    font-size: 7.5pt;
    font-family: var(--font-doc);
    color: var(--gray-500);
}

/* ============================================================
   EDIT HINT
   ============================================================ */
.edit-hint {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: linear-gradient(135deg, var(--gray-900), #1E1130);
    color: var(--white);
    padding: 9px 18px;
    border-radius: var(--r-full);
    font-size: 11.5px;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 7px;
    box-shadow: var(--shadow-lg), 0 0 0 1px rgba(255,255,255,0.08);
    transition: opacity var(--t-slow), transform var(--t-slow);
    z-index: 50;
    pointer-events: none;
}
.edit-hint.hidden {
    opacity: 0;
    transform: translateX(-50%) translateY(16px);
}

/* ============================================================
   PRINT / PDF EXPORT
   ============================================================ */
@media print {
    .no-print { display: none !important; }

    body {
        background: white;
        background-image: none;
    }
    .invoice-wrapper { padding: 0; }
    .invoice-page {
        box-shadow: none;
        border-radius: 0;
        max-width: none;
        padding: 20px 40px 60px;
        min-height: unset;
    }
    [contenteditable="true"]:hover,
    [contenteditable="true"]:focus {
        background: none !important;
        box-shadow: none !important;
    }
}

.pdf-export-mode .no-print { display: none !important; }
.pdf-export-mode [contenteditable="true"]:hover,
.pdf-export-mode [contenteditable="true"]:focus {
    background: none !important;
    box-shadow: none !important;
}
.pdf-export-mode .invoice-page {
    box-shadow: none;
    border-radius: 0;
    padding: 24px 40px 60px;
}
.pdf-export-mode .date-picker { display: none; }
.pdf-export-mode .date-cell::after { display: none; }
.pdf-export-mode .product-row:hover { background: transparent; }
.pdf-export-mode .totals-row:not(.totals-grand) { border-bottom: none; }

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media screen and (max-width: 700px) {
    .toolbar { height: auto; padding: 10px 14px; }
    .toolbar-sub { display: none; }

    .controls-grid { gap: 8px; }
    .control-group { min-width: 110px; }

    .invoice-page { padding: 20px 16px 70px; min-height: auto; }

    .invoice-header { flex-direction: column; align-items: center; text-align: center; }
    .header-right { text-align: center; }
    .company-details { text-align: center; }
    .invoice-meta-section { flex-direction: column; gap: 14px; }
    .invoice-details { text-align: left; }
    .meta-row { justify-content: flex-start; }
    .totals-section { width: 100%; }
    .invoice-footer { position: relative; bottom: auto; left: auto; right: auto; margin-top: 32px; }

    .col-rate, .col-amount { display: none; }
    .invoice-table thead th.col-rate,
    .invoice-table thead th.col-amount { display: none; }

    .btn-text { display: none; }
    .btn { padding: 8px 10px; }
    .toolbar-brand span.toolbar-title { display: none; }
}

@media screen and (max-width: 420px) {
    .invoice-page { padding: 16px 12px 64px; }
    .toolbar { padding: 8px 10px; }
    .controls-panel { padding: 8px 10px; }
    .btn { padding: 7px 9px; font-size: 12px; }
}
