/*
 * DigiWorld Manual Key Dispatch - Premium Redesign
 * Light-theme priority, modern glassmorphism UI
 */

:root {
    --brand-primary: #ff7f3f;
    --brand-secondary: #f04b22;
    --brand-accent: #7c3aed;
    --bg-base: #f4f7fb;
    --bg-surface: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border-color: rgba(148, 163, 184, 0.2);
    --border-focus: #ff7f3f;
    --glass-bg: rgba(255, 255, 255, 0.85);
    --glass-border: rgba(255, 255, 255, 0.4);
    --shadow-sm: 0 4px 12px rgba(15, 23, 42, 0.05);
    --shadow-md: 0 12px 32px rgba(15, 23, 42, 0.08);
    --shadow-lg: 0 24px 64px rgba(15, 23, 42, 0.1);
    --radius-sm: 8px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --success: #10b981;
    --error: #ef4444;
}

body.dark-mode {
    --bg-base: #0f172a;
    --bg-surface: #1e293b;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --border-color: rgba(255, 255, 255, 0.1);
    --glass-bg: rgba(30, 41, 59, 0.85);
    --glass-border: rgba(255, 255, 255, 0.05);
    --shadow-sm: 0 4px 12px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 12px 32px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 24px 64px rgba(0, 0, 0, 0.4);
}

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

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-base);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    background-image: 
        radial-gradient(circle at 15% 50%, rgba(255, 127, 63, 0.08), transparent 25%),
        radial-gradient(circle at 85% 30%, rgba(124, 58, 237, 0.08), transparent 25%);
    background-attachment: fixed;
}

/* =========================================
   Layout & Container
   ========================================= */
.dispatch-container {
    width: 100%;
    max-width: 1200px;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 32px;
}

/* =========================================
   Header
   ========================================= */
.dispatch-header {
    background: var(--glass-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 12px 24px;
    box-shadow: var(--shadow-sm);
    position: sticky;
    top: 24px;
    z-index: 100;
}

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

.brand {
    display: flex;
    align-items: center;
    gap: 16px;
    text-decoration: none;
    color: var(--text-main);
}

.brand-logo {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    background: linear-gradient(135deg, var(--brand-primary), var(--brand-secondary));
    color: white;
    display: grid;
    place-items: center;
    font-size: 1.2rem;
    box-shadow: 0 8px 16px rgba(240, 75, 34, 0.25);
}

.brand-text {
    display: flex;
    flex-direction: column;
}

.brand-text strong {
    font-family: 'Outfit', sans-serif;
    font-size: 1.2rem;
    font-weight: 700;
    line-height: 1.1;
}

.brand-text span {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 500;
}

.back-button {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-main);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 600;
    transition: all 0.2s ease;
}

.back-button:hover {
    background: var(--bg-base);
    transform: translateY(-2px);
}

/* =========================================
   Main Grid Layout
   ========================================= */
.dispatch-main {
    display: grid;
    grid-template-columns: 350px 1fr;
    gap: 32px;
    align-items: flex-start;
}

/* =========================================
   Left Panel: Info
   ========================================= */
.dispatch-info {
    position: sticky;
    top: 120px;
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.info-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: rgba(16, 185, 129, 0.1);
    color: var(--success);
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 700;
    width: fit-content;
    border: 1px solid rgba(16, 185, 129, 0.2);
}

.info-title {
    font-family: 'Outfit', sans-serif;
    font-size: 3rem;
    line-height: 1.1;
    font-weight: 700;
    color: var(--text-main);
}

.info-desc {
    color: var(--text-muted);
    line-height: 1.6;
    font-size: 1.05rem;
}

.feature-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
}

.feature-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 16px 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    text-align: center;
    box-shadow: var(--shadow-sm);
}

.feature-icon {
    font-size: 1.2rem;
    color: var(--brand-primary);
}

.feature-text {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text-muted);
}

.step-indicator {
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin-top: 16px;
}

.step {
    display: flex;
    align-items: center;
    gap: 16px;
    opacity: 0.5;
    transition: opacity 0.3s ease;
}

.step.active {
    opacity: 1;
}

.step-num {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    display: grid;
    place-items: center;
    font-weight: 700;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.step.active .step-num {
    background: linear-gradient(135deg, var(--brand-primary), var(--brand-secondary));
    color: white;
    border: none;
    box-shadow: 0 4px 12px rgba(240, 75, 34, 0.3);
}

.step-text {
    font-family: 'Outfit', sans-serif;
    font-weight: 600;
    font-size: 1.1rem;
}

/* =========================================
   Right Panel: Workspace / Form
   ========================================= */
.dispatch-workspace {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    padding: 40px;
    position: relative;
    overflow: hidden;
}

/* Subtle glowing accent line at the top of the card */
.dispatch-workspace::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, var(--brand-accent), var(--brand-primary), var(--brand-secondary));
}

.auth-guard {
    text-align: center;
    padding: 40px 20px;
    background: rgba(239, 68, 68, 0.05);
    border: 1px solid rgba(239, 68, 68, 0.2);
    border-radius: var(--radius-md);
    color: var(--error);
}
.auth-guard i { font-size: 2rem; margin-bottom: 12px; }
.auth-guard a { color: var(--brand-primary); font-weight: 600; }

.hidden {
    display: none !important;
}

.form-header {
    margin-bottom: 32px;
}

.form-header h2 {
    font-family: 'Outfit', sans-serif;
    font-size: 1.8rem;
    font-weight: 600;
    color: var(--text-main);
    margin-bottom: 8px;
}

.form-header p {
    color: var(--text-muted);
    font-size: 0.95rem;
}

.dispatch-form {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.form-row {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 20px;
}

label {
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text-main);
    display: flex;
    align-items: center;
    gap: 8px;
}

label i {
    color: var(--brand-primary);
}

.field-hint {
    font-size: 0.8rem;
    color: var(--text-muted);
}

/* =========================================
   Inputs & Selects
   ========================================= */
input[type="text"],
input[type="email"],
input[type="number"],
.native-select,
.custom-select-trigger {
    width: 100%;
    min-height: 52px;
    padding: 0 16px;
    background: var(--bg-base);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-family: 'Inter', sans-serif;
    font-size: 1rem;
    color: var(--text-main);
    transition: all 0.2s ease;
    outline: none;
}

input:focus,
.native-select:focus,
.custom-select.open .custom-select-trigger {
    background: var(--bg-surface);
    border-color: var(--border-focus);
    box-shadow: 0 0 0 4px rgba(255, 127, 63, 0.15);
}

/* Custom Select Styling for JS compatibility */
.custom-select-wrapper {
    position: relative;
}

.custom-select-trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
}

.custom-select-trigger::after {
    content: '\f078';
    font-family: 'Font Awesome 5 Free';
    font-weight: 900;
    font-size: 0.8rem;
    color: var(--text-muted);
    transition: transform 0.2s ease;
}

.custom-select.open .custom-select-trigger::after {
    transform: rotate(180deg);
}

.custom-options {
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    right: 0;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    max-height: 300px;
    overflow-y: auto;
    z-index: 50;
    opacity: 0;
    pointer-events: none;
    transform: translateY(-10px);
    transition: all 0.2s ease;
}

.custom-select.open .custom-options {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}

.custom-option {
    padding: 14px 16px;
    cursor: pointer;
    border-bottom: 1px solid var(--border-color);
    transition: background 0.2s ease;
    white-space: normal;
    word-break: break-word;
    line-height: 1.4;
}

.custom-option:last-child {
    border-bottom: none;
}

.custom-option:hover,
.custom-option.selected {
    background: rgba(255, 127, 63, 0.08);
    color: var(--brand-primary);
    font-weight: 500;
}

/* =========================================
   Product Preview Component
   ========================================= */
.product-preview {
    display: flex;
    align-items: center;
    gap: 20px;
    padding: 16px;
    background: var(--bg-base);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    animation: fadeIn 0.3s ease forwards;
}

.product-preview img {
    width: 72px;
    height: 72px;
    border-radius: var(--radius-sm);
    object-fit: cover;
    box-shadow: var(--shadow-sm);
    background: white;
}

.preview-info h3 {
    font-family: 'Outfit', sans-serif;
    font-size: 1.1rem;
    color: var(--text-main);
    margin-bottom: 4px;
}

.preview-info p {
    font-size: 0.9rem;
    color: var(--text-muted);
}

/* =========================================
   Actions & Status
   ========================================= */
.form-actions {
    display: flex;
    gap: 16px;
    margin-top: 16px;
    padding-top: 24px;
    border-top: 1px solid var(--border-color);
}

.btn-submit,
.btn-reset {
    min-height: 54px;
    border-radius: var(--radius-sm);
    font-family: 'Outfit', sans-serif;
    font-size: 1.05rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
}

.btn-submit {
    flex: 2;
    background: linear-gradient(135deg, var(--brand-primary), var(--brand-secondary));
    color: white;
    box-shadow: 0 8px 24px rgba(240, 75, 34, 0.3);
}

.btn-submit:not(:disabled):hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 32px rgba(240, 75, 34, 0.4);
}

.btn-submit:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    box-shadow: none;
}

.btn-reset {
    flex: 1;
    background: var(--bg-base);
    border: 1px solid var(--border-color);
    color: var(--text-main);
}

.btn-reset:hover {
    background: var(--border-color);
}

.status-message {
    padding: 16px;
    border-radius: var(--radius-sm);
    font-weight: 500;
    font-size: 0.95rem;
    display: none;
}

.status-message.show {
    display: block;
    animation: fadeIn 0.3s ease forwards;
}

.status-message.success {
    background: rgba(16, 185, 129, 0.1);
    color: var(--success);
    border: 1px solid rgba(16, 185, 129, 0.2);
}

.status-message.error {
    background: rgba(239, 68, 68, 0.1);
    color: var(--error);
    border: 1px solid rgba(239, 68, 68, 0.2);
}

.status-message.warning {
    background: rgba(245, 158, 11, 0.1);
    color: #b45309;
    border: 1px solid rgba(245, 158, 11, 0.2);
}
body.dark-mode .status-message.warning { color: #fcd34d; }

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* =========================================
   Responsive Mobile Adjustments
   ========================================= */
@media (max-width: 992px) {
    .dispatch-main {
        grid-template-columns: 1fr;
    }
    
    .dispatch-info {
        position: static;
    }
    
    .dispatch-workspace {
        padding: 32px 24px;
    }
}

@media (max-width: 600px) {
    .dispatch-container {
        padding: 16px 12px;
        padding-bottom: 100px; /* Space for sticky bottom bar */
    }
    
    .info-title {
        font-size: 2.2rem;
    }
    
    .form-row {
        grid-template-columns: 1fr;
        gap: 16px;
    }
    
    .dispatch-workspace {
        padding: 24px 16px;
    }

    .form-actions {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 16px;
        margin: 0;
        background: var(--glass-bg);
        backdrop-filter: blur(20px);
        -webkit-backdrop-filter: blur(20px);
        border-top: 1px solid var(--border-color);
        z-index: 90;
        flex-direction: row-reverse;
    }
    
    .custom-select.open .custom-options {
        max-height: 40vh;
    }
}

/* Instant authenticated shell and non-blocking stale-while-revalidate status. */
.dispatch-auth-loader {
    min-height: 420px;
    display: grid;
    place-content: center;
    grid-template-columns: auto minmax(0, 300px);
    align-items: center;
    gap: 16px;
    color: var(--text-main);
}

.dispatch-auth-loader strong,
.dispatch-auth-loader small {
    display: block;
}

.dispatch-auth-loader strong {
    font-family: 'Outfit', sans-serif;
    font-size: 1.05rem;
    line-height: 1.35;
}

.dispatch-auth-loader small {
    margin-top: 4px;
    color: var(--text-muted);
    font-size: 0.8rem;
}

.dispatch-loader-mark {
    width: 48px;
    height: 48px;
    display: grid;
    place-items: center;
    border-radius: 15px;
    color: #fff;
    background: linear-gradient(135deg, var(--brand-primary), var(--brand-secondary));
    box-shadow: 0 14px 30px rgba(240, 75, 34, 0.28);
    animation: dispatchLoaderPulse 1.4s ease-in-out infinite;
}

.dispatch-data-status {
    min-height: 38px;
    display: flex;
    align-items: center;
    gap: 9px;
    margin-top: -15px;
    padding: 9px 11px;
    border: 1px solid var(--border-color);
    border-radius: 11px;
    color: var(--text-muted);
    background: var(--bg-base);
    font-size: 0.78rem;
    font-weight: 600;
    transition: color 160ms ease, background 160ms ease, border-color 160ms ease;
}

.dispatch-data-status.syncing {
    color: #c2410c;
    border-color: rgba(245, 158, 11, 0.24);
    background: rgba(255, 247, 237, 0.86);
}

.dispatch-data-status.fresh {
    color: #047857;
    border-color: rgba(16, 185, 129, 0.2);
    background: rgba(236, 253, 245, 0.84);
}

.dispatch-data-status.stale,
.dispatch-data-status.error {
    color: #b45309;
    border-color: rgba(245, 158, 11, 0.24);
    background: rgba(255, 251, 235, 0.86);
}

.dispatch-form.is-auth-pending {
    pointer-events: none;
}

.dispatch-form.is-auth-pending .form-actions {
    opacity: 0.72;
}

body.dark-mode .dispatch-data-status.syncing,
body.dark-mode .dispatch-data-status.stale,
body.dark-mode .dispatch-data-status.error {
    color: #fdba74;
    background: rgba(124, 45, 18, 0.22);
}

body.dark-mode .dispatch-data-status.fresh {
    color: #6ee7b7;
    background: rgba(6, 78, 59, 0.22);
}

@keyframes dispatchLoaderPulse {
    0%, 100% {
        transform: scale(1);
        box-shadow: 0 14px 30px rgba(240, 75, 34, 0.22);
    }
    50% {
        transform: scale(1.04);
        box-shadow: 0 18px 38px rgba(240, 75, 34, 0.35);
    }
}

@media (max-width: 600px) {
    .dispatch-auth-loader {
        min-height: 300px;
        grid-template-columns: auto minmax(0, 1fr);
    }

    .dispatch-data-status {
        margin-top: -10px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .dispatch-loader-mark {
        animation: none;
    }
}

/* Delivery receipts stay separate from the form so another order can be queued. */
.background-deliveries { margin-top: 24px; padding: 20px; border: 1px solid var(--border-color, #e5e7eb); border-radius: 16px; background: var(--bg-base, #fff); }
.background-deliveries h2 { font-size: 1.1rem; margin: 0 0 8px; }
.background-deliveries p { color: var(--text-muted, #64748b); font-size: .875rem; margin: 0 0 16px; }
.background-deliveries ul { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; }
.delivery-job { display: grid; gap: 5px; padding: 12px; border-left: 3px solid #e88c36; background: #fff8ef; border-radius: 8px; overflow-wrap: anywhere; }
.delivery-job strong { font-size: .875rem; }
.delivery-job span { font-size: .82rem; line-height: 1.5; color: #475569; }
.delivery-job-settled { border-color: #16a34a; background: #f0fdf4; }
.delivery-job-failed, .delivery-job-needs_verification, .delivery-job-not_queued { border-color: #dc2626; background: #fff1f2; }
body.dark-mode .background-deliveries { background: #162033; border-color: #334155; }
body.dark-mode .delivery-job { background: #263043; }
body.dark-mode .delivery-job span { color: #cbd5e1; }
