/* ============ RESET & BASE ============ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
    --blue: #2563EB;
    --blue-light: #dbeafe;
    --green: #22c55e;
    --orange: #f59e0b;
    --red: #ef4444;
    --bg: #ffffff;
    --bg-secondary: #f3f4f6;
    --bg-tertiary: #e5e7eb;
    --text: #111827;
    --text-secondary: #6b7280;
    --text-tertiary: #9ca3af;
    --border: #e5e7eb;
    --card-bg: #f9fafb;
    --modal-bg: #ffffff;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
    --radius: 12px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1a1a1a;
        --bg-secondary: #2a2a2a;
        --bg-tertiary: #3a3a3a;
        --text: #f1f1f1;
        --text-secondary: #a1a1aa;
        --text-tertiary: #71717a;
        --border: #3a3a3a;
        --card-bg: #252525;
        --modal-bg: #2a2a2a;
        --blue-light: #1e3a5f;
    }
}

html {
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro', 'Segoe UI', system-ui, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

body {
    min-height: 100dvh;
    overflow-x: hidden;
}

/* ============ SCREENS ============ */
.screen { display: none; min-height: 100dvh; }
.screen.active { display: flex; flex-direction: column; }

/* ============ SIGN-IN ============ */
#sign-in-screen {
    justify-content: center;
    align-items: center;
    padding: 24px;
    text-align: center;
}

.sign-in-content { max-width: 360px; }

.sign-in-logo { margin-bottom: 24px; }
.sign-in-icon { width: 80px; height: 80px; border-radius: 18px; }

#sign-in-screen h1 {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 8px;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 15px;
    margin-bottom: 32px;
}

.error-text {
    color: var(--red);
    font-size: 13px;
    margin-top: 12px;
}

/* ============ HEADER ============ */
.app-header {
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    padding-top: calc(12px + var(--safe-top));
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

.header-title {
    font-size: 18px;
    font-weight: 700;
}

.header-left, .header-right {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 60px;
}

.header-right { justify-content: flex-end; }

.badge {
    font-size: 12px;
    font-weight: 600;
    padding: 3px 10px;
    border-radius: 100px;
    background: var(--blue-light);
    color: var(--blue);
}

.icon-btn {
    display: flex;
    align-items: center;
    gap: 4px;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 6px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 600;
}

.icon-btn:active { background: var(--bg-secondary); }

#pending-badge { color: var(--orange); }
#pending-count { font-size: 13px; font-weight: 700; }

/* ============ FORM ============ */
#shipment-form {
    padding: 16px;
    padding-bottom: calc(32px + var(--safe-bottom));
    max-width: 640px;
    margin: 0 auto;
    width: 100%;
}

.form-section {
    margin-bottom: 28px;
}

.form-section h2 {
    font-size: 17px;
    font-weight: 600;
    margin-bottom: 4px;
}

.section-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}

.required-tag {
    font-size: 11px;
    font-weight: 600;
    color: var(--blue);
    background: var(--blue-light);
    padding: 2px 8px;
    border-radius: 100px;
}

.optional-tag {
    font-size: 11px;
    font-weight: 500;
    color: var(--text-tertiary);
}

.helper-text {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 10px;
}

.helper-text.center { text-align: center; }

/* ============ PHOTO CAPTURE ============ */
.photo-capture-area {
    cursor: pointer;
    border-radius: var(--radius);
    overflow: hidden;
    border: 2px dashed var(--border);
    transition: border-color 0.2s;
}

.photo-capture-area:active { border-color: var(--blue); }

.capture-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 40px 20px;
    background: var(--bg-secondary);
}

.capture-placeholder .camera-icon { color: var(--blue); }

.capture-placeholder span {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
}

.capture-preview {
    position: relative;
}

.capture-preview img {
    width: 100%;
    max-height: 240px;
    object-fit: cover;
    display: block;
}

.retake-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 12px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text);
    background: rgba(255,255,255,0.9);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: none;
    border-radius: 100px;
    cursor: pointer;
}

@media (prefers-color-scheme: dark) {
    .retake-btn { background: rgba(50,50,50,0.9); }
}

.photo-capture-area.has-photo {
    border-style: solid;
    border-color: var(--border);
}

/* ============ ITEM PHOTOS ROW ============ */
.item-photos-row {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    padding: 4px 0;
    -webkit-overflow-scrolling: touch;
}

.item-photo-thumb {
    position: relative;
    flex-shrink: 0;
}

.item-photo-thumb img {
    width: 90px;
    height: 90px;
    object-fit: cover;
    border-radius: 8px;
    display: block;
}

.item-photo-thumb .remove-btn {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--red);
    border: 2px solid var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    padding: 0;
}

.item-photo-thumb .remove-btn svg { color: white; }

.add-item-photo-btn {
    flex-shrink: 0;
    width: 90px;
    height: 90px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: var(--bg-secondary);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    color: var(--blue);
    font-size: 12px;
    font-weight: 500;
}

.add-item-photo-btn:active { background: var(--bg-tertiary); }

/* ============ LINE ITEMS ============ */
.line-item {
    background: var(--card-bg);
    border-radius: 10px;
    margin-bottom: 10px;
    overflow: hidden;
    border: 1px solid var(--border);
}

.line-item-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 14px;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
}

.line-item-header:active { background: var(--bg-tertiary); }

.chevron {
    color: var(--text-secondary);
    transition: transform 0.2s;
    flex-shrink: 0;
}

.line-item.collapsed .chevron { transform: rotate(-90deg); }
.line-item.collapsed .line-item-body { display: none; }

.line-item-title {
    font-size: 15px;
    font-weight: 600;
}

.line-item-subtitle {
    font-size: 13px;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
}

.damage-indicator { display: flex; flex-shrink: 0; }

.duplicate-item-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    display: flex;
    flex-shrink: 0;
}

.duplicate-item-btn:active { background: var(--bg-tertiary); }

.delete-item-btn {
    background: none;
    border: none;
    color: var(--red);
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    display: flex;
    flex-shrink: 0;
}

.delete-item-btn:active { background: rgba(239,68,68,0.1); }

.line-item-body {
    padding: 0 14px 14px;
}

.field {
    margin-bottom: 14px;
}

.field:last-child { margin-bottom: 0; }

.field label {
    display: block;
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 4px;
    font-weight: 500;
}

.optional-inline {
    font-weight: 400;
    color: var(--text-tertiary);
    font-size: 11px;
}

.damage-label { color: var(--orange) !important; }

.field input[type="text"],
textarea {
    width: 100%;
    padding: 10px 12px;
    font-size: 16px;
    font-family: inherit;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    outline: none;
    transition: border-color 0.2s;
    -webkit-appearance: none;
}

.field input[type="text"]:focus,
textarea:focus {
    border-color: var(--blue);
}

textarea {
    resize: vertical;
    min-height: 80px;
}

/* Quantity stepper */
.quantity-stepper {
    display: flex;
    align-items: center;
    gap: 16px;
}

.stepper-btn {
    background: none;
    border: none;
    color: var(--blue);
    cursor: pointer;
    padding: 2px;
    display: flex;
    border-radius: 50%;
}

.stepper-btn:disabled { color: var(--text-tertiary); }
.stepper-btn:active:not(:disabled) { opacity: 0.7; }

.qty-value {
    font-size: 20px;
    font-weight: 700;
    width: 56px;
    text-align: center;
    font-variant-numeric: tabular-nums;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 4px 2px;
    background: var(--bg);
    color: var(--text);
    font-family: inherit;
    -moz-appearance: textfield;
    -webkit-appearance: none;
    appearance: none;
}

.qty-value:focus {
    outline: none;
    border-color: var(--blue);
}

.qty-value::-webkit-inner-spin-button,
.qty-value::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

/* Condition toggle */
.condition-toggle {
    display: flex;
    gap: 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
}

.toggle-btn {
    flex: 1;
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    border: none;
    background: var(--bg);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
}

.toggle-btn.active {
    background: var(--blue);
    color: white;
}

.toggle-btn[data-value="Damaged"].active {
    background: var(--orange);
}

/* ============ BUTTONS ============ */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 20px;
    font-size: 15px;
    font-weight: 600;
    font-family: inherit;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: opacity 0.2s;
    -webkit-appearance: none;
}

.btn:active { opacity: 0.85; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
    background: var(--blue);
    color: white;
}

.btn-secondary {
    background: var(--bg-secondary);
    color: var(--text);
    width: 100%;
}

.btn-secondary:active { background: var(--bg-tertiary); }

.btn-danger {
    background: transparent;
    color: var(--red);
    border: 1px solid var(--red);
    padding: 8px 16px;
    font-size: 14px;
}

.btn-large {
    width: 100%;
    padding: 14px 24px;
    font-size: 16px;
    border-radius: 12px;
}

.btn-add-item { margin-top: 4px; }

.btn-submit { margin-top: 8px; }

/* ============ SUBMIT LOADING ============ */
.submit-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 14px;
    font-size: 16px;
    font-weight: 600;
    color: var(--text-secondary);
}

.spinner {
    width: 22px;
    height: 22px;
    border: 3px solid var(--border);
    border-top-color: var(--blue);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ============ SUCCESS SCREEN ============ */
#success-screen {
    justify-content: center;
    align-items: center;
    padding: 24px;
    text-align: center;
}

.success-content { max-width: 360px; }

.success-check {
    margin-bottom: 20px;
    animation: pop-in 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes pop-in {
    0% { transform: scale(0.3); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

#success-screen h1 {
    font-size: 26px;
    font-weight: 700;
    margin-bottom: 6px;
}

#success-screen .subtitle { margin-bottom: 40px; }

/* ============ MODAL ============ */
.modal {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.4);
}

.modal-content {
    position: relative;
    background: var(--modal-bg);
    border-radius: 16px 16px 0 0;
    padding: 20px;
    padding-bottom: calc(20px + var(--safe-bottom));
    width: 100%;
    max-width: 480px;
    max-height: 80dvh;
    overflow-y: auto;
    animation: slide-up 0.25s ease-out;
}

@keyframes slide-up {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
}

.modal-header h2 { font-size: 18px; font-weight: 700; }

.settings-section {
    padding: 16px 0;
    border-top: 1px solid var(--border);
}

.settings-section h3 {
    font-size: 13px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
}

.settings-value {
    font-size: 15px;
    margin-bottom: 4px;
}

.settings-value.secondary {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

/* ============ TABLET LAYOUT ============ */
@media (min-width: 768px) {
    #shipment-form {
        max-width: 720px;
        padding: 24px 32px;
    }

    .capture-placeholder { padding: 48px 20px; }

    .line-item-body {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0 16px;
    }

    .line-item-body .field:first-child {
        grid-column: 1 / -1;
    }

    .damage-notes-field,
    .item-photos-field {
        grid-column: 1 / -1;
    }

    .modal-content {
        border-radius: 16px;
        margin-bottom: 40px;
    }
}
