/* ── Reset & Base ──────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --primary:   #036b62;
    --primary-h: #025850;
    --success:   #0e9f6e;
    --warning:   #d97706;
    --danger:    #e02424;
    --info:      #0891b2;
    --bg:        #f3f4f6;
    --surface:   #ffffff;
    --border:    #e5e7eb;
    --text:      #111827;
    --muted:     #6b7280;
    --radius:    8px;
    --shadow:    0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
    --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

input, select, textarea, button {
    font-family: inherit;
    font-size: inherit;
}

/* ── Layout ────────────────────────────────────────────────────────────────── */
.app-header {
    background: var(--primary);
    color: #fff;
    padding: 0 24px;
    display: flex;
    align-items: center;
    height: 56px;
    gap: 24px;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-md);
}

.app-logo {
    font-weight: 700;
    font-size: 16px;
    white-space: nowrap;
    letter-spacing: -.3px;
}
.app-logo span { opacity: .7; font-weight: 400; font-size: 13px; margin-left: 6px; }

.nav-tabs {
    display: flex;
    gap: 4px;
    list-style: none;
    flex: 1;
}

.nav-tab {
    padding: 6px 14px;
    border-radius: var(--radius);
    cursor: pointer;
    color: rgba(255,255,255,.8);
    font-weight: 500;
    transition: background .15s, color .15s;
    user-select: none;
    position: relative;
    white-space: nowrap;
}
.nav-tab:hover { background: rgba(255,255,255,.15); color: #fff; }
.nav-tab.active { background: rgba(255,255,255,.2); color: #fff; }

.nav-badge {
    position: absolute;
    top: 2px; right: 4px;
    background: #ef4444;
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    min-width: 16px;
    height: 16px;
    border-radius: 9999px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
}

.main-content {
    width: 100%;
    padding: 24px 32px 48px;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ── Cards & Surfaces ─────────────────────────────────────────────────────── */
.card {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.card-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.card-title {
    font-size: 15px;
    font-weight: 600;
    flex: 1;
}

.card-body { padding: 20px; }

/* ── Buttons ──────────────────────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    border-radius: var(--radius);
    border: none;
    cursor: pointer;
    font-weight: 500;
    font-size: 13px;
    transition: background .15s, opacity .15s;
    white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary  { background: var(--primary);  color: #fff; }
.btn-primary:hover:not(:disabled)  { background: var(--primary-h); }
.btn-success  { background: var(--success);  color: #fff; }
.btn-success:hover:not(:disabled)  { background: #0a7a55; }
.btn-danger   { background: var(--danger);   color: #fff; }
.btn-danger:hover:not(:disabled)   { background: #b91c1c; }
.btn-warning  { background: var(--warning);  color: #fff; }
.btn-warning:hover:not(:disabled)  { background: #b45309; }
.btn-ghost    { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover:not(:disabled)   { background: var(--bg); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-lg { padding: 10px 20px; font-size: 15px; }

/* ── Form Elements ────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-weight: 500; margin-bottom: 6px; font-size: 13px; }
.form-label.required::after { content: ' *'; color: var(--danger); }

.form-control {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    transition: border-color .15s, box-shadow .15s;
}
.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(3,107,98,.15);
}
textarea.form-control { resize: vertical; min-height: 80px; }

.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
}

/* ── Filter Bar ───────────────────────────────────────────────────────────── */
.filter-bar {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: flex-end;
    padding: 16px 20px;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 16px;
}

.filter-bar .form-group { margin: 0; flex: 1; min-width: 140px; }

/* ── Badges ───────────────────────────────────────────────────────────────── */
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 9999px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .3px;
}
.badge-green  { background: #dcfce7; color: #166534; }
.badge-blue   { background: #dbeafe; color: #1e40af; }
.badge-orange { background: #fef3c7; color: #92400e; }
.badge-red    { background: #fee2e2; color: #991b1b; }
.badge-gray   { background: #f3f4f6; color: #4b5563; }
.badge-manual { background: #ede9fe; color: #5b21b6; }

/* ── Orders Table ─────────────────────────────────────────────────────────── */
.orders-table-wrap { overflow-x: auto; }

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

thead th {
    text-align: left;
    padding: 10px 12px;
    background: var(--bg);
    font-weight: 600;
    font-size: 12px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .4px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
thead th.sortable {
    cursor: pointer;
    user-select: none;
}
thead th.sortable:hover { color: var(--primary); }
thead th.sort-asc  .sort-icon::after { content: ' ↑'; }
thead th.sort-desc .sort-icon::after { content: ' ↓'; }
.sort-icon { opacity: .4; font-size: 11px; }
thead th.sort-asc  .sort-icon,
thead th.sort-desc .sort-icon { opacity: 1; color: var(--primary); }

tbody tr { border-bottom: 1px solid var(--border); }
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: #fafafa; }

td {
    padding: 10px 12px;
    vertical-align: top;
}

td.items-cell { max-width: 220px; }

.item-list {
    list-style: none;
    font-size: 12px;
    color: var(--muted);
}
.item-list li::before { content: '• '; }

/* ── Inline Date Picker ───────────────────────────────────────────────────── */
.date-save-wrap {
    display: flex;
    align-items: center;
    gap: 6px;
}
.date-save-wrap input[type="date"] {
    padding: 4px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 12px;
    background: var(--surface);
    color: var(--text);
}
.date-save-wrap input[type="date"]:focus {
    outline: none;
    border-color: var(--primary);
}
.date-indicator {
    font-size: 16px;
    opacity: 0;
    transition: opacity .3s;
}
.date-indicator.show { opacity: 1; }

/* ── Run Builder ──────────────────────────────────────────────────────────── */
.run-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    align-items: start;
}

@media (max-width: 900px) {
    .run-layout { grid-template-columns: 1fr; }
}

.stop-list { list-style: none; }

.stop-item {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 8px;
    background: var(--surface);
    cursor: grab;
    transition: box-shadow .15s, opacity .15s;
}
.stop-item:active { cursor: grabbing; }
.stop-item.drag-over { box-shadow: 0 0 0 2px var(--primary); }
.stop-item.dragging { opacity: .4; }

.stop-number {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 13px;
    flex-shrink: 0;
    margin-top: 2px;
}

.stop-info { flex: 1; min-width: 0; }
.stop-name { font-weight: 600; }
.stop-addr { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stop-items { font-size: 12px; color: var(--muted); margin-top: 4px; }

.drag-handle {
    color: var(--muted);
    font-size: 18px;
    cursor: grab;
    padding: 2px;
    flex-shrink: 0;
}

/* ── Order Select List (in Run Builder) ───────────────────────────────────── */
.order-select-item {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 6px;
    background: var(--surface);
    cursor: pointer;
    transition: border-color .15s;
}
.order-select-item:hover { border-color: var(--primary); }
.order-select-item.selected { border-color: var(--primary); background: #e6f4f3; }
.order-select-item input[type="checkbox"] { margin-top: 3px; flex-shrink: 0; }

.order-select-info { flex: 1; min-width: 0; }
.order-select-name { font-weight: 600; }
.order-select-meta { font-size: 12px; color: var(--muted); }

/* ── Generated Run Sheet ──────────────────────────────────────────────────── */
.run-sheet-result {
    margin-top: 20px;
    padding: 20px;
    background: #f0fdf4;
    border: 1px solid #86efac;
    border-radius: var(--radius);
}
.run-sheet-result h3 { color: var(--success); margin-bottom: 12px; }
.run-url {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    padding: 10px 14px;
    border-radius: var(--radius);
    font-family: monospace;
    font-size: 13px;
    word-break: break-all;
}

/* ── Manual Order Form ────────────────────────────────────────────────────── */
.manual-layout {
    display: grid;
    grid-template-columns: 420px 1fr;
    gap: 20px;
    align-items: start;
}

@media (max-width: 900px) {
    .manual-layout { grid-template-columns: 1fr; }
}

/* ── Past Runs ────────────────────────────────────────────────────────────── */
.progress-bar-wrap {
    background: var(--border);
    border-radius: 9999px;
    height: 8px;
    overflow: hidden;
    width: 120px;
}
.progress-bar-fill {
    height: 100%;
    background: var(--success);
    border-radius: 9999px;
    transition: width .3s;
}

/* ── Toast Notifications ──────────────────────────────────────────────────── */
.toast-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 18px;
    border-radius: var(--radius);
    color: #fff;
    font-weight: 500;
    font-size: 13px;
    box-shadow: var(--shadow-md);
    opacity: 0;
    transform: translateY(12px);
    transition: opacity .25s, transform .25s;
    pointer-events: auto;
    max-width: 360px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast-success { background: var(--success); }
.toast-error   { background: var(--danger); }
.toast-info    { background: var(--info); }

/* ── Empty State ──────────────────────────────────────────────────────────── */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--muted);
}
.empty-state .icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 15px; }

/* ── Alert ────────────────────────────────────────────────────────────────── */
.alert {
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 13px;
    margin-bottom: 16px;
    border-left: 4px solid transparent;
}
.alert-warning { background: #fef9c3; border-color: var(--warning); color: #713f12; }
.alert-error   { background: #fee2e2; border-color: var(--danger);  color: #7f1d1d; }
.alert-info    { background: #e0f2fe; border-color: var(--info);    color: #0c4a6e; }

/* ── Misc Utilities ───────────────────────────────────────────────────────── */
.flex { display: flex; }
.flex-center { display: flex; align-items: center; }
.gap-8  { gap: 8px; }
.gap-12 { gap: 12px; }
.mt-4   { margin-top: 4px; }
.mt-8   { margin-top: 8px; }
.mt-16  { margin-top: 16px; }
.mb-16  { margin-bottom: 16px; }
.text-muted  { color: var(--muted); }
.text-sm     { font-size: 12px; }
.text-bold   { font-weight: 600; }
.text-right  { text-align: right; }
.w-full      { width: 100%; }
.nowrap      { white-space: nowrap; }

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

.section-heading {
    font-size: 18px;
    font-weight: 700;
    margin-bottom: 16px;
}

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 640px) {
    .main-content { padding: 16px 16px 48px; }
    .app-header   { padding: 0 16px; gap: 12px; }
    .nav-tab      { padding: 6px 10px; font-size: 13px; }
    .filter-bar   { flex-direction: column; }
}

/* ── Settings ────────────────────────────────────────────────────────────── */
.settings-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    align-items: start;
}
@media (max-width: 860px) {
    .settings-layout { grid-template-columns: 1fr; }
}

.staff-row {
    display: flex;
    align-items: center;
    padding: 10px 6px;
    border-bottom: 1px solid var(--border);
    gap: 10px;
}
.staff-row:last-child { border-bottom: none; }
.staff-name { font-weight: 600; flex: 1; }
.staff-role-badge {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .4px;
    padding: 2px 8px;
    border-radius: 9999px;
    background: #e6f4f3;
    color: var(--primary);
}
.staff-role-badge.role-driver  { background: #e6f4f3; color: var(--primary); }
.staff-role-badge.role-staff   { background: #f0fdf4; color: var(--success); }
.staff-role-badge.role-manager { background: #faf5ff; color: #7c3aed; }

/* ── Staff Picker (Run Builder) ───────────────────────────────────────────── */
.staff-picker {
    position: relative;
}

.staff-picker-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    z-index: 200;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    min-width: 220px;
    max-height: 260px;
    overflow-y: auto;
    padding: 6px;
}
.staff-picker-dropdown.open { display: block; }

.staff-picker-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
}
.staff-picker-item:hover { background: var(--bg); }
.staff-picker-item input[type="checkbox"] { flex-shrink: 0; }

/* Staff chips on run builder */
.staff-tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: #e6f4f3;
    color: var(--primary);
    border-radius: 9999px;
    font-size: 12px;
    font-weight: 600;
    padding: 3px 10px 3px 10px;
    white-space: nowrap;
}
.staff-tag button {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--primary);
    font-size: 14px;
    line-height: 1;
    padding: 0 0 0 2px;
    opacity: .6;
}
.staff-tag button:hover { opacity: 1; }

/* Smaller staff name chips in past-runs table */
.staff-tag-small {
    display: inline-block;
    background: #e6f4f3;
    color: var(--primary);
    border-radius: 9999px;
    font-size: 11px;
    font-weight: 600;
    padding: 2px 8px;
    margin-bottom: 2px;
    white-space: nowrap;
}

/* ── Full Responsive Overhaul ─────────────────────────────────────────────── */

/* Header — on small screens shrink and stack properly */
@media (max-width: 640px) {
    .app-header {
        padding: 0 12px;
        gap: 8px;
        height: auto;
        flex-wrap: wrap;
        padding-top: 8px;
        padding-bottom: 8px;
    }
    .app-logo { font-size: 14px; }
    .app-logo span { display: none; }           /* hide subtitle */
    #header-logo { display: none !important; }   /* hide logo on mobile */
    #btn-setup-metafield { display: none; }      /* hide setup btn on mobile */
}

/* Nav tabs — horizontal scroll on mobile */
.nav-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap;
    min-width: 0;
}
.nav-tabs::-webkit-scrollbar { display: none; }

@media (max-width: 640px) {
    .nav-tab { padding: 6px 10px; font-size: 12px; }
}

/* Main content padding */
@media (max-width: 640px) {
    .main-content { padding: 12px 12px 48px; }
    .section-heading { font-size: 15px; margin-bottom: 12px; }
}

/* Filter bar — single column on mobile */
@media (max-width: 640px) {
    .filter-bar {
        flex-direction: column;
        padding: 12px;
        gap: 8px;
    }
    .filter-bar .form-group { min-width: 0; flex: none; width: 100%; }
}

/* Cards — remove overflow clip on mobile so dropdowns work */
@media (max-width: 640px) {
    .card { border-radius: 6px; }
    .card-header { padding: 12px 14px; flex-wrap: wrap; gap: 8px; }
    .card-body   { padding: 14px; }
}

/* Tables — horizontal scroll with touch */
.orders-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* On very small screens hide less critical columns */
@media (max-width: 480px) {
    .col-hide-xs { display: none; }
}
@media (max-width: 640px) {
    .col-hide-sm { display: none; }
}

/* Run builder — single column on mobile */
@media (max-width: 640px) {
    .run-layout { grid-template-columns: 1fr; gap: 12px; }
}

/* Manual order form — single column */
@media (max-width: 640px) {
    .manual-layout { grid-template-columns: 1fr; gap: 12px; }
}

/* Settings — single column */
@media (max-width: 640px) {
    .settings-layout { grid-template-columns: 1fr; gap: 12px; }
}

/* Form rows */
@media (max-width: 640px) {
    .form-row { grid-template-columns: 1fr; }
}

/* Buttons in card headers — full width on mobile */
@media (max-width: 480px) {
    .card-header .btn { font-size: 12px; padding: 6px 10px; }
}

/* Larger screens — use more horizontal space */
@media (min-width: 1200px) {
    .main-content  { padding: 28px 40px 64px; }
    .run-layout    { grid-template-columns: 1fr 1.2fr; }
    .manual-layout { grid-template-columns: 480px 1fr; }
}
@media (min-width: 1600px) {
    .main-content { padding: 32px 56px 64px; }
}

/* ── Order Detail Panel (slide-in drawer) ─────────────────────────────────── */
.order-panel-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.4);
    z-index: 499;
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s;
}
.order-panel-backdrop.open {
    opacity: 1;
    pointer-events: auto;
}

.order-panel {
    position: fixed;
    top: 56px;
    right: 0;
    width: min(720px, 100vw);
    height: calc(100vh - 56px);
    background: var(--surface);
    box-shadow: -6px 0 32px rgba(0,0,0,.15);
    z-index: 500;
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform .28s cubic-bezier(.4,0,.2,1);
}
.order-panel.open {
    transform: translateX(0);
}

.order-panel-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 20px;
    border-bottom: 2px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
    flex-wrap: wrap;
    row-gap: 8px;
}

.order-panel-body {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 24px;
}

/* Panel sections */
.panel-section {
    margin-bottom: 28px;
}
.panel-section-title {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .6px;
    color: var(--muted);
    margin-bottom: 12px;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border);
}
.panel-info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
}
.panel-info-item label {
    display: block;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .4px;
    color: var(--muted);
    margin-bottom: 3px;
}
.panel-info-item p {
    font-size: 14px;
    color: var(--text);
    word-break: break-word;
}
/* Per-item stock status rows */
.item-status-grid {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.item-status-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    background: var(--bg);
    border-radius: var(--radius);
    border: 1px solid var(--border);
}
.item-status-label {
    flex: 1;
    font-size: 13px;
    font-weight: 500;
    min-width: 0;
    word-break: break-word;
}
.item-status-select {
    flex-shrink: 0;
    padding: 5px 8px;
    border-radius: 6px;
    border-width: 2px;
    border-style: solid;
    font-size: 12px;
    font-weight: 600;
    background: #fff;
    cursor: pointer;
    min-width: 130px;
    font-family: inherit;
}
.item-status-select:focus { outline: none; }

.panel-note-existing {
    background: #f9fafb;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 14px;
    font-size: 13px;
    color: var(--text);
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 200px;
    overflow-y: auto;
    line-height: 1.6;
}

/* Clickable table rows */
tbody tr.clickable-row { cursor: pointer; }
tbody tr.clickable-row:hover { background: #f0f4ff; }

/* Delivery note indicator in table */
.note-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--warning);
    margin-left: 4px;
    vertical-align: middle;
    title: attr(data-note);
}

@media (max-width: 640px) {
    .order-panel { top: 0; height: 100vh; width: 100vw; }
    .order-panel-body { padding: 16px; }
}
