{% load i18n %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <title>{% trans "Label" %} {{ parcel.tracking_number }}</title>
    <style>
        @page {
            size: 100mm 150mm;
            margin: 0;
        }
        * {
            box-sizing: border-box;
        }
        body {
            /* Prioritize Noto Sans for Arabic support */
            font-family: 'Noto Sans', 'Noto Sans Arabic', 'DejaVu Sans', sans-serif;
            margin: 0;
            padding: 0;
            width: 100mm;
            height: 150mm;
            color: #000;
            font-size: 10px; /* Reduced base font size */
            overflow: hidden; /* Prevent spillover */
        }
        .container {
            width: 94mm; 
            height: 144mm; /* Reduced height to be safely inside 150mm */
            margin: 3mm auto; /* 3mm top/bottom margin */
            border: 2px solid #000;
            display: flex;
            flex-direction: column;
            page-break-inside: avoid;
            background: #fff;
            overflow: hidden;
        }
        .header {
            border-bottom: 2px solid #000;
            padding: 3px;
            text-align: center;
            height: 18mm; /* Reduced */
            flex-shrink: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }
        .header img {
            max-height: 100%;
            max-width: 90%;
            object-fit: contain;
        }
        .header h1 {
            margin: 0;
            font-size: 16px; /* Reduced */
        }
        .big-code {
            font-size: 16px; /* Reduced */
            font-weight: bold;
            text-align: center;
            padding: 4px;
            border-bottom: 2px solid #000;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 10px;
            flex-shrink: 0;
            height: 10mm;
        }
        .address-box {
            border-bottom: 1px solid #000;
            padding: 5px;
            flex-grow: 1;
            overflow: hidden;
            font-size: 10px; /* Reduced */
        }
        .main-info {
            display: flex;
            border-bottom: 2px solid #000;
            flex-shrink: 0;
            height: 14mm; /* Reduced */
        }
        .section {
            padding: 2px;
            flex: 1;
            border-right: 1px solid #000;
            text-align: center;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }
        .section:last-child {
            border-right: none;
        }
        .qr-code {
            text-align: center;
            padding: 3px;
            border-bottom: 2px solid #000;
            flex-shrink: 0;
            height: 30mm; /* Reduced */
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }
        .qr-code img {
            width: 22mm; /* Reduced */
            height: 22mm;
        }
        .tracking-number {
            font-size: 12px;
            font-weight: bold;
            margin-top: 2px;
            font-family: monospace;
        }
        .footer {
            padding: 3px;
            text-align: center;
            font-size: 8px; /* Reduced */
            flex-shrink: 0;
            height: 8mm;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }
        h3 { 
            margin: 0 0 2px 0; 
            font-size: 9px; 
            text-transform: uppercase; 
            color: #333; 
            border-bottom: 1px dotted #ccc;
            padding-bottom: 1px;
            display: flex;
            justify-content: space-between;
        }
        h3 span.ar { font-weight: normal; }
        p { margin: 0 0 1px 0; line-height: 1.1; }
        .bold { font-weight: bold; font-size: 11px; }
        .dual-text {
            display: flex;
            flex-direction: row;
            gap: 5px;
        }
        .ar-text {
            font-family: 'Noto Sans', 'Noto Sans Arabic', 'DejaVu Sans', sans-serif;
            direction: rtl;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Platform Header -->
        <div class="header">
            {% if logo_base64 %}
                <img src="data:image/jpeg;base64,{{ logo_base64 }}" alt="Logo">
            {% else %}
                <h1>{{ platform_profile.name|default:"MASARX" }}</h1>
            {% endif %}
        </div>

        <!-- Destination City Code -->
        <div class="big-code">
            <span>{{ parcel.delivery_city.name_en|upper }}</span>
            <span class="ar-text">/ {{ parcel.delivery_city.name_ar }}</span>
        </div>

        <!-- Receiver Info -->
        <div class="address-box">
            <h3>
                <span>TO (Receiver)</span>
                <span class="ar-text">إلى (المستلم)</span>
            </h3>
            <p class="bold">{{ parcel.receiver_name }}</p>
            <p>{{ parcel.delivery_address }}</p>
            <p>
                {{ parcel.delivery_city.name_en }} / <span class="ar-text">{{ parcel.delivery_city.name_ar }}</span>
            </p>
            <p>
                {{ parcel.delivery_governate.name_en }} / <span class="ar-text">{{ parcel.delivery_governate.name_ar }}</span> - {{ parcel.delivery_country.name_en }}
            </p>
            <p><strong>Tel / هاتف:</strong> {{ parcel.receiver_phone }}</p>
        </div>

        <!-- Sender Info -->
        <div class="address-box">
            <h3>
                <span>FROM (Sender)</span>
                <span class="ar-text">من (المرسل)</span>
            </h3>
            <p class="bold">{{ parcel.shipper.get_full_name|default:parcel.shipper.username }}</p>
            <p>{{ parcel.pickup_address }}</p>
            <p>
                {{ parcel.pickup_city.name_en }} / <span class="ar-text">{{ parcel.pickup_city.name_ar }}</span>
            </p>
            <p><strong>Tel / هاتف:</strong> {{ parcel.shipper.profile.phone_number }}</p>
        </div>

        <!-- Info Row: Date, Weight, Price -->
        <div class="main-info">
            <div class="section">
                <h3>Date / التاريخ</h3>
                <p class="bold">{{ parcel.created_at|date:"Y-m-d" }}</p>
            </div>
            <div class="section">
                <h3>Weight / الوزن</h3>
                <p class="bold">{{ parcel.weight }} KG</p>
            </div>
            <div class="section">
                <h3>COD / الدفع</h3>
                <p class="bold">{{ parcel.price }} OMR / <span class="ar-text">ر.ع.</span></p>
            </div>
        </div>

        <!-- Tracking & QR -->
        <div class="qr-code">
            <img src="data:image/png;base64,{{ qr_code }}" alt="QR Code">
            <div class="tracking-number">{{ parcel.tracking_number }}</div>
        </div>

        <!-- Footer -->
        <div class="footer">
            Thank you for shipping with us.<br>
            <span class="ar-text">شكراً لاستخدامكم خدماتنا.</span>
        </div>
    </div>
</body>
</html>