<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Kitchen Ticket - <?php echo sanitize($order['order_number']); ?></title>
    <style>
        @page { margin: 5mm; }
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: 'Courier New', monospace; font-size: 12px; color: #000; background: #fff; }
        .ticket { max-width: 80mm; margin: 0 auto; padding: 8px; }
        .ticket-header { text-align: center; border-bottom: 2px dashed #000; padding-bottom: 6px; margin-bottom: 6px; }
        .ticket-header .restaurant-name { font-size: 14px; font-weight: 900; text-transform: uppercase; }
        .ticket-meta { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 11px; }
        .ticket-meta .order-num { font-size: 16px; font-weight: 900; }
        .ticket-meta .meta-right { text-align: right; }
        .ticket-divider { border-top: 1px dashed #000; margin: 4px 0; }
        .ticket-divider-double { border-top: 2px dashed #000; margin: 6px 0; }
        .items-list { margin: 6px 0; }
        .ticket-item { margin-bottom: 6px; }
        .ticket-item .item-line { display: flex; gap: 6px; }
        .ticket-item .item-qty { font-weight: 900; min-width: 30px; font-size: 14px; }
        .ticket-item .item-name { font-weight: 700; font-size: 12px; }
        .ticket-item .item-variation { font-size: 10px; color: #333; margin-left: 36px; }
        .ticket-item .item-addons { font-size: 10px; color: #555; margin-left: 36px; font-style: italic; }
        .ticket-item .item-note { font-size: 11px; font-weight: 900; margin-left: 36px; margin-top: 2px; padding: 2px 4px; background: #ff0; border: 1px solid #000; }
        .ticket-footer { border-top: 2px dashed #000; padding-top: 6px; margin-top: 8px; text-align: center; font-size: 10px; }
        .ticket-footer .time-print { font-size: 10px; color: #333; }
        .priority-note { font-size: 13px; font-weight: 900; text-align: center; padding: 4px; margin: 6px 0; border: 2px solid #000; }
        @media print {
            body { background: #fff; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
            .no-print { display: none !important; }
            .ticket { padding: 0; max-width: none; }
        }
    </style>
</head>
<body>
    <div class="ticket">
        <div class="ticket-header">
            <div class="restaurant-name"><?php echo sanitize($restaurant['name'] ?? APP_NAME); ?></div>
            <div style="font-size:10px;">KITCHEN TICKET</div>
        </div>

        <div class="ticket-meta">
            <div>
                <div class="order-num">#<?php echo sanitize($order['order_number']); ?></div>
                <div><?php echo ucfirst(str_replace('_', ' ', $order['order_type'])); ?></div>
                <?php if (!empty($order['table_number'])): ?>
                    <div>Table: <?php echo sanitize($order['table_number']); ?></div>
                <?php endif; ?>
            </div>
            <div class="meta-right">
                <div><?php echo date('H:i', strtotime($order['created_at'])); ?></div>
                <div><?php echo date('Y-m-d', strtotime($order['created_at'])); ?></div>
                <?php if (!empty($order['customer_name'])): ?>
                    <div><?php echo sanitize($order['customer_name']); ?></div>
                <?php endif; ?>
            </div>
        </div>

        <div class="ticket-divider-double"></div>

        <?php if (!empty($order['order_notes'])): ?>
            <div class="priority-note">
                NOTE: <?php echo sanitize($order['order_notes']); ?>
            </div>
        <?php endif; ?>

        <div class="items-list">
            <?php foreach ($order['items'] as $item): ?>
                <div class="ticket-item">
                    <div class="item-line">
                        <span class="item-qty"><?php echo (int) $item['quantity']; ?>x</span>
                        <span class="item-name"><?php echo sanitize($item['item_name']); ?></span>
                    </div>
                    <?php if (!empty($item['variation_name'])): ?>
                        <div class="item-variation">Size: <?php echo sanitize($item['variation_name']); ?></div>
                    <?php endif; ?>
                    <?php if (!empty($item['addons'])): ?>
                        <?php foreach ($item['addons'] as $addon): ?>
                            <div class="item-addons">+ <?php echo sanitize($addon['addon_name']); ?></div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                    <?php if (!empty($item['item_notes'])): ?>
                        <div class="item-note">*** <?php echo sanitize($item['item_notes']); ?> ***</div>
                    <?php endif; ?>
                </div>
                <div class="ticket-divider"></div>
            <?php endforeach; ?>
        </div>

        <div class="ticket-footer">
            <div class="time-print">Printed: <?php echo date('H:i:s'); ?></div>
            <div><?php echo sanitize(APP_NAME); ?></div>
        </div>
    </div>
</body>
</html>
