/* DN Print Styles */

/* ===== Variables ===== */
:root {
    --bg-primary: #f0f2f5;
    --bg-card: #ffffff;
    --text-primary: #1a1a2e;
    --text-secondary: #666;
    --text-muted: #888;
    --border: #d1d5db;
    --border-light: #e5e7eb;
    --accent: #0f3460;
    --accent-hover: #16213e;
    --success: #059669;
    --success-bg: #d1fae5;
    --warning: #f59e0b;
    --warning-bg: #fef3c7;
    --danger: #dc2626;
    --shadow: rgba(0,0,0,0.06);
}

[data-theme="dark"] {
    --bg-primary: #0d1117;
    --bg-card: #161b22;
    --text-primary: #e6edf3;
    --text-secondary: #8b949e;
    --text-muted: #6e7681;
    --border: #30363d;
    --border-light: #21262d;
    --accent: #58a6ff;
    --accent-hover: #79c0ff;
    --success: #3fb950;
    --success-bg: #0d2818;
    --warning: #d29922;
    --warning-bg: #2d1e00;
    --danger: #f85149;
    --shadow: rgba(0,0,0,0.3);
}

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    line-height: 1.5;
}

/* ===== Header ===== */
header {
    background: linear-gradient(135deg, #16213e 0%, #0f3460 100%);
    color: white;
    padding: 10px 16px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    position: sticky;
    top: 0;
    z-index: 100;
}
.header-main-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.header-left h1 { font-size: 17px; font-weight: 700; }
.header-left h1 a { color: white; text-decoration: none; }
.header-right { display: flex; align-items: center; gap: 8px; }
.user-info { font-size: 12px; opacity: 0.9; }
.btn-logout {
    background: rgba(255,255,255,0.15);
    color: white;
    border: 1px solid rgba(255,255,255,0.2);
}

/* ===== Main ===== */
main { max-width: 600px; margin: 0 auto; padding: 16px; }

/* ===== Buttons ===== */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 10px 20px; border-radius: 8px;
    font-size: 15px; font-weight: 600; cursor: pointer;
    text-decoration: none; border: none; transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}
.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-small { padding: 6px 12px; font-size: 12px; }
.btn-outline { background: transparent; color: var(--accent); border: 1px solid var(--accent); }
.btn-outline:hover { background: var(--accent); color: white; }
.btn-full { width: 100%; }
.btn-logout:hover { background: rgba(255,255,255,0.25); }

.btn-spinner {
    display: inline-block;
    width: 14px; height: 14px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Screen Reader Only ===== */
.sr-only {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0,0,0,0); border: 0;
}

/* ===== Error Message ===== */
.error-msg {
    background: var(--warning-bg); color: var(--danger);
    padding: 12px; border-radius: 8px; margin-bottom: 16px;
    font-size: 14px; font-weight: 500;
}

/* ===== Scan Screen ===== */
.scan-container {
    display: flex; justify-content: center;
    padding-top: 40px;
}
.scan-card {
    background: var(--bg-card); border-radius: 12px; padding: 32px 24px;
    width: 100%; max-width: 400px;
    box-shadow: 0 4px 20px var(--shadow);
    text-align: center;
}
.scan-icon { font-size: 48px; margin-bottom: 12px; }
.scan-card h2 { font-size: 22px; margin-bottom: 8px; }
.scan-desc { color: var(--text-secondary); font-size: 14px; margin-bottom: 20px; }

.form-group { margin-bottom: 16px; text-align: left; }
.form-group label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.form-group input {
    width: 100%; padding: 14px 12px; border: 2px solid var(--border); border-radius: 8px;
    font-size: 18px; /* prevents iOS zoom */ font-family: monospace;
    transition: border-color 0.2s; text-align: center;
}
.form-group input:focus { border-color: var(--accent); outline: none; }

/* ===== Configure Screen ===== */
.config-container {
    display: flex; flex-direction: column; gap: 16px;
}

.so-info-card {
    background: var(--bg-card); border-radius: 10px; padding: 16px;
    box-shadow: 0 1px 4px var(--shadow);
}
.so-info-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.so-info-header h2 { font-size: 18px; word-break: break-all; }
.so-customer { font-size: 14px; color: var(--text-secondary); margin-top: 2px; }
.so-info-meta { display: flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--text-secondary); text-align: right; }
.so-total { font-weight: 700; color: var(--success); }
.so-info-card .btn { margin-top: 10px; }

.items-card {
    background: var(--bg-card); border-radius: 10px; padding: 16px;
    box-shadow: 0 1px 4px var(--shadow);
}
.items-card h3 { font-size: 16px; margin-bottom: 4px; }
.items-hint { font-size: 12px; color: var(--text-muted); margin-bottom: 12px; }

.items-list { display: flex; flex-direction: column; gap: 8px; }

.item-row {
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px; border-radius: 8px;
    background: var(--bg-primary); gap: 12px;
}
.item-info { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
.item-code { font-size: 13px; font-weight: 700; color: var(--accent); font-family: monospace; }
.item-name { font-size: 11px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.item-qty-group { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.qty-display { display: flex; flex-direction: column; align-items: flex-end; }
.qty-label { font-size: 10px; color: var(--text-muted); text-transform: uppercase; }
.qty-pending { font-size: 11px; color: var(--text-secondary); }

.qty-input-wrap { display: flex; align-items: center; gap: 4px; }
.qty-input {
    width: 70px; padding: 8px; border: 2px solid var(--border); border-radius: 6px;
    font-size: 16px; text-align: center; font-weight: 600;
}
.qty-input:focus { border-color: var(--accent); outline: none; }
.qty-unit { font-size: 11px; color: var(--text-muted); }

.perbox-card {
    background: var(--bg-card); border-radius: 10px; padding: 16px;
    box-shadow: 0 1px 4px var(--shadow);
}
.perbox-card h3 { font-size: 16px; margin-bottom: 4px; }
.perbox-hint { font-size: 12px; color: var(--text-muted); margin-bottom: 12px; }

.perbox-input-group { display: flex; align-items: center; gap: 8px; }
.perbox-input-group input {
    width: 100px; padding: 10px; border: 2px solid var(--border); border-radius: 6px;
    font-size: 18px; text-align: center; font-weight: 600;
}
.perbox-input-group input:focus { border-color: var(--accent); outline: none; }
.perbox-unit { font-size: 14px; color: var(--text-secondary); }

.perbox-summary { margin-top: 10px; font-size: 14px; color: var(--text-secondary); }
.perbox-summary strong { color: var(--accent); }

.btn-generate {
    width: 100%; padding: 16px; font-size: 18px; border-radius: 10px;
    margin-top: 8px;
}

/* ===== Print Screen ===== */
.print-container { margin-bottom: 20px; }
.print-header {
    background: var(--bg-card); border-radius: 10px; padding: 16px;
    box-shadow: 0 1px 4px var(--shadow); text-align: center;
}
.print-header h2 { font-size: 18px; margin: 8px 0 4px; }
.print-header p { font-size: 13px; color: var(--text-secondary); margin-bottom: 12px; }
.print-actions { display: flex; gap: 10px; justify-content: center; }
.btn-print { padding: 12px 32px; font-size: 16px; }

/* ===== Label Preview (on screen) ===== */
.print-area {
    display: flex; flex-direction: column; gap: 20px;
    align-items: center;
}

.label-sheet {
    width: 100mm; height: 100mm;
    display: flex; flex-direction: column;
    border: 1px dashed var(--border);
    border-radius: 4px;
    background: white;
    box-shadow: 0 2px 8px var(--shadow);
    overflow: hidden;
    flex-shrink: 0;
}

.label {
    width: 100mm; height: 50mm;
    padding: 0;
    display: flex; flex-direction: column;
    border-bottom: 0.5pt dashed #ccc;
    overflow: hidden;
}
.label:last-child { border-bottom: none; }
.label-empty { background: #f9f9f9; }

/* Inner content wrapper — this has the padding */
.label-inner {
    width: 100%;
    height: 100%;
    padding: 3mm 4mm;
    display: flex; flex-direction: column;
    gap: 1mm;
    font-size: 8pt;
    box-sizing: border-box;
}

.label-top {
    display: flex; justify-content: space-between; align-items: flex-start;
    gap: 2mm;
}
.label-so {
    font-size: 10pt; font-weight: 700; color: #0f3460;
    font-family: monospace; line-height: 1.2;
}
.label-barcode { height: 15mm; flex-shrink: 0; }
.label-barcode svg { height: 15mm !important; display: block; }

.label-customer { font-size: 8pt; color: #666; line-height: 1.2; }

.label-item { display: flex; flex-direction: column; gap: 0; }
.label-item-code { font-size: 8pt; font-weight: 700; color: #333; font-family: monospace; line-height: 1.2; }
.label-item-name { font-size: 7pt; color: #888; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.label-qty { font-size: 11pt; font-weight: 800; color: #0f3460; line-height: 1.2; }

.label-footer {
    margin-top: auto;
    display: flex; justify-content: space-between;
    font-size: 6.5pt; color: #aaa;
}

/* ===== Dark mode label fix ===== */
[data-theme="dark"] .label-sheet { background: white; }
[data-theme="dark"] .label-inner { color: #1a1a2e; }
[data-theme="dark"] .label-so { color: #0f3460; }
[data-theme="dark"] .label-customer { color: #666; }
[data-theme="dark"] .label-item-code { color: #333; }
[data-theme="dark"] .label-qty { color: #0f3460; }

/* ===== Print Styles — Exact 10x10cm ===== */
@media print {
    @page {
        size: 100mm 100mm;
        margin: 0 !important;
    }

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

    /* Remove ALL browser defaults */
    html, body {
        margin: 0 !important;
        padding: 0 !important;
        width: 100mm !important;
        height: 100mm !important;
        background: white !important;
    }

    /* Hide everything except labels */
    header { display: none !important; }
    .no-print { display: none !important; }
    main {
        display: block !important;
        padding: 0 !important;
        margin: 0 !important;
        max-width: none !important;
        width: 100mm !important;
    }

    /* Print area: no gaps, no padding */
    .print-area {
        display: block !important;
        margin: 0 !important;
        padding: 0 !important;
        gap: 0 !important;
    }

    /* Label sheet = EXACTLY 100mm x 100mm, zero everything */
    .label-sheet {
        width: 100mm !important;
        height: 100mm !important;
        min-width: 100mm !important;
        min-height: 100mm !important;
        max-width: 100mm !important;
        max-height: 100mm !important;
        display: flex !important;
        flex-direction: column !important;
        flex-wrap: nowrap !important;
        border: none !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        margin: 0 !important;
        padding: 0 !important;
        gap: 0 !important;
        overflow: hidden !important;
        page-break-after: always !important;
        page-break-inside: avoid !important;
    }

    /* Each label = EXACTLY 100mm x 50mm, zero padding */
    .label {
        width: 100mm !important;
        height: 50mm !important;
        min-width: 100mm !important;
        min-height: 50mm !important;
        max-width: 100mm !important;
        max-height: 50mm !important;
        display: flex !important;
        flex-direction: column !important;
        flex-wrap: nowrap !important;
        padding: 0 !important;
        margin: 0 !important;
        gap: 0 !important;
        border: none !important;
        border-radius: 0 !important;
        overflow: hidden !important;
        page-break-inside: avoid !important;
        box-sizing: border-box !important;
    }

    .label:empty, .label-empty { display: none !important; }

    /* Barcode SVG must be visible */
    .label-barcode svg {
        visibility: visible !important;
        display: block !important;
    }
}

/* ===== Mobile ===== */
@media (max-width: 767px) {
    main { padding: 12px; }

    .scan-card { padding: 24px 16px; }
    .scan-icon { font-size: 40px; }
    .scan-card h2 { font-size: 20px; }

    .item-row { flex-direction: column; align-items: stretch; gap: 8px; }
    .item-qty-group { align-items: stretch; flex-direction: row; justify-content: space-between; }
    .qty-display { align-items: flex-start; }
    .qty-input-wrap { justify-content: flex-end; }

    .label-sheet { transform: scale(0.85); transform-origin: top center; }
}

@media (max-width: 400px) {
    .label-sheet { transform: scale(0.7); }
}

/* Touch-friendly tap targets */
@media (hover: none) and (pointer: coarse) {
    .btn { min-height: 48px; }
    .qty-input { min-height: 44px; }
    .perbox-input-group input { min-height: 44px; }
}

/* Safe area for notched phones */
@supports (padding: env(safe-area-inset-bottom)) {
    body { padding-bottom: env(safe-area-inset-bottom); }
}