* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif; background: #f5f6f8; color: #111; }
body { min-height: 100vh; display: flex; flex-direction: column; }
[hidden] { display: none !important; }

header#topbar {
  display: flex; align-items: center; gap: 1.5rem; padding: 0.75rem 1.5rem;
  background: #1f2937; color: #f9fafb; box-shadow: 0 1px 4px rgba(0,0,0,0.15);
}
header .brand { font-weight: 600; font-size: 1.05rem; }
header nav { display: flex; gap: 0.25rem; flex: 1; }
header nav .tab {
  background: transparent; border: 0; color: #d1d5db; padding: 0.5rem 0.9rem;
  cursor: pointer; border-radius: 6px; font-size: 0.92rem;
}
header nav .tab.active { background: #374151; color: #fff; }
header nav .tab:hover:not(.active) { background: rgba(255,255,255,0.07); color: #fff; }
header .user { display: flex; align-items: center; gap: 0.75rem; font-size: 0.9rem; }
header .user button { background: #374151; color: #f3f4f6; border: 0; padding: 0.45rem 0.8rem; border-radius: 6px; cursor: pointer; }
header .user button:hover { background: #4b5563; }

main { flex: 1; padding: 1.5rem; max-width: 1280px; margin: 0 auto; width: 100%; }

.view { display: block; }
.view[hidden] { display: none; }

.card {
  background: #fff; border-radius: 10px; padding: 1.5rem; box-shadow: 0 1px 3px rgba(0,0,0,0.06);
  max-width: 540px; margin: 0 auto 1.25rem;
}
.card h1, .card h2 { margin-top: 0; }
.card label { display: block; margin-bottom: 0.85rem; font-size: 0.88rem; color: #374151; }
.card label input, .card label select, .card label textarea {
  display: block; width: 100%; padding: 0.55rem 0.7rem; margin-top: 0.25rem;
  border: 1px solid #d1d5db; border-radius: 6px; font-size: 0.95rem; font-family: inherit;
}
.card label textarea { font-family: ui-monospace, SFMono-Regular, monospace; }
.card button[type="submit"] {
  background: #2563eb; color: #fff; border: 0; padding: 0.6rem 1.1rem;
  border-radius: 6px; cursor: pointer; font-size: 0.95rem;
}
.card button[type="submit"]:hover { background: #1d4ed8; }

#login-view { display: flex; align-items: center; justify-content: center; min-height: calc(100vh - 3rem); }

.toolbar { display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.toolbar input, .toolbar select { padding: 0.5rem 0.75rem; border: 1px solid #d1d5db; border-radius: 6px; background: #fff; }
.toolbar input { flex: 1; min-width: 200px; }
.toolbar button { background: #fff; border: 1px solid #d1d5db; border-radius: 6px; padding: 0.5rem 1rem; cursor: pointer; }
.toolbar button:hover { background: #f3f4f6; }

table.devices {
  width: 100%; border-collapse: collapse; background: #fff; border-radius: 10px;
  overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
table.devices th, table.devices td { padding: 0.65rem 0.85rem; text-align: left; font-size: 0.91rem; }
table.devices thead { background: #f9fafb; color: #374151; font-size: 0.82rem; text-transform: uppercase; letter-spacing: 0.03em; }
table.devices tbody tr { border-top: 1px solid #f3f4f6; }
table.devices tbody tr:hover { background: #f9fafb; }
.device-row, .user-row { cursor: pointer; }
.device-row:focus-visible { outline: 2px solid #2563eb; outline-offset: -2px; }
.device-id { font-family: ui-monospace, SFMono-Regular, monospace; font-size: 0.85rem; }
.link-window { min-width: 10rem; line-height: 1.35; }
.action-btn { background: #f3f4f6; border: 1px solid #d1d5db; border-radius: 5px; padding: 0.3rem 0.6rem; cursor: pointer; font-size: 0.85rem; }
.action-btn:hover { background: #e5e7eb; }

.status-pill { display: inline-block; padding: 0.18rem 0.55rem; border-radius: 999px; font-size: 0.78rem; font-weight: 500; line-height: 1.2; }
.status-in_office { background: #dbeafe; color: #1d4ed8; }
.status-assigned { background: #dcfce7; color: #166534; }
.status-returned { background: #fef3c7; color: #92400e; }
.status-broken { background: #fee2e2; color: #b91c1c; }
.status-lost { background: #f3e8ff; color: #6b21a8; }
.status-deleted { background: #e5e7eb; color: #374151; }
.is-deleted { color: #6b7280; }

.empty { text-align: center; padding: 2rem; color: #6b7280; }

.error { color: #b91c1c; font-size: 0.88rem; min-height: 1.2em; margin-top: 0.5rem; }
.success { color: #166534; font-size: 0.88rem; margin-top: 0.5rem; }

#admins-list { list-style: none; padding: 0; margin: 0; }
#admins-list li { display: flex; align-items: center; justify-content: space-between; padding: 0.5rem 0; border-bottom: 1px solid #f3f4f6; }
#admins-list li:last-child { border-bottom: 0; }
#admins-list .delete-admin { background: #fee2e2; color: #b91c1c; border: 0; padding: 0.3rem 0.6rem; border-radius: 5px; cursor: pointer; }
.staff-role { display: inline-block; margin-left: 0.35rem; padding: 0.12rem 0.45rem; border-radius: 999px; font-size: 0.72rem; font-weight: 600; }
.staff-role-admin { background: #dbeafe; color: #1d4ed8; }
.staff-role-operator { background: #f3e8ff; color: #6b21a8; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.45); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal[hidden] { display: none; }
.modal-content { background: #fff; border-radius: 12px; padding: 1.5rem; max-width: 720px; width: 95%; max-height: 90vh; overflow-y: auto; position: relative; }
.modal-close { position: absolute; top: 0.5rem; right: 0.75rem; background: transparent; border: 0; font-size: 1.6rem; cursor: pointer; color: #6b7280; }
.modal h2 { margin-top: 0; }
.modal .actions { display: flex; gap: 0.5rem; flex-wrap: wrap; margin: 0.75rem 0 1.25rem; }
.modal .actions button { background: #2563eb; color: #fff; border: 0; padding: 0.45rem 0.85rem; border-radius: 6px; cursor: pointer; }
.modal .actions button.secondary { background: #f3f4f6; color: #111; border: 1px solid #d1d5db; }
.modal .actions button.danger { background: #b91c1c; color: #fff; }
.modal .field { display: block; margin-bottom: 0.85rem; font-size: 0.9rem; }
.modal .field input, .modal .field select { width: 100%; padding: 0.5rem 0.7rem; border: 1px solid #d1d5db; border-radius: 6px; margin-top: 0.25rem; }
.modal .field label { display: block; font-size: 0.88rem; color: #374151; }

.events-table { font-size: 0.85rem; overflow-x: auto; }
.events-table table { width: 100%; border-collapse: collapse; }
.events-table th, .events-table td { padding: 0.4rem 0.6rem; text-align: left; vertical-align: top; }
.events-table thead { background: #f9fafb; font-size: 0.78rem; text-transform: uppercase; color: #374151; }
.events-table tr { border-top: 1px solid #f3f4f6; }
.events-table a { color: #2563eb; text-decoration: none; }
.events-table a:hover { text-decoration: underline; }

.dev-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0.75rem; background: #f9fafb; border-radius: 8px; padding: 0.85rem 1rem; margin-bottom: 1rem; }
.dev-summary > div div { font-weight: 500; }

.muted { color: #6b7280; font-size: 0.82rem; font-weight: 400; }
.hint { color: #6b7280; font-size: 0.88rem; margin-top: -0.25rem; }

.action-form { background: #f3f4f6; border-radius: 8px; padding: 0.85rem 1rem; margin-bottom: 1rem; }
.action-form h4 { margin: 0 0 0.5rem; }

.user-results { display: grid; gap: 0.35rem; margin-top: 0.4rem; }
.user-results button {
  display: flex; align-items: center; justify-content: space-between; gap: 0.75rem;
  width: 100%; padding: 0.45rem 0.6rem; border: 1px solid #d1d5db; border-radius: 6px;
  background: #fff; color: #111; cursor: pointer; text-align: left;
}
.user-results button:hover { background: #f9fafb; }
.user-results small { color: #6b7280; font-family: ui-monospace, SFMono-Regular, monospace; }
.user-result-empty { color: #6b7280; font-size: 0.86rem; margin-top: 0.35rem; }
.user-picker input[readonly] { background: #f9fafb; color: #374151; cursor: default; }
.selected-user {
  display: flex; align-items: center; justify-content: space-between; gap: 0.75rem;
  color: #166534; background: #dcfce7; border: 1px solid #bbf7d0; border-radius: 6px;
  font-size: 0.86rem; margin-top: 0.45rem; padding: 0.45rem 0.6rem;
}
.selected-user button {
  flex: 0 0 auto; border: 1px solid #86efac; background: #fff; color: #166534;
  border-radius: 5px; padding: 0.25rem 0.5rem; cursor: pointer;
}
.selected-user button:hover { background: #f0fdf4; }

.shipping-address { background: #f9fafb; border-radius: 8px; padding: 0.85rem 1rem; font-size: 0.9rem; line-height: 1.5; margin-bottom: 1rem; }
.shipping-cell { min-width: 12rem; line-height: 1.4; }

.user-type-select { padding: 0.35rem 0.5rem; border: 1px solid #d1d5db; border-radius: 6px; background: #fff; font-size: 0.9rem; font-family: inherit; }
.user-type-status { font-size: 0.82rem; }
.user-type-status.success { color: #166534; }
.user-type-status.error { color: #b91c1c; }
.user-type-status.muted { color: #6b7280; }
.user-row-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 0.6rem; min-width: 15rem; }

.notification-modal-content { max-width: 620px; }
.notification-audience-summary {
  margin: -0.35rem 0 1rem; padding: 0.7rem 0.85rem; border-radius: 7px;
  background: #f3f4f6; color: #374151; font-size: 0.9rem; line-height: 1.4;
}
.notification-confirm {
  display: flex; align-items: flex-start; gap: 0.55rem; margin: 0.9rem 0;
  padding: 0.75rem 0.85rem; border: 1px solid #fbbf24; border-radius: 7px;
  background: #fffbeb; color: #78350f; font-size: 0.88rem; line-height: 1.4;
}
.notification-confirm input { flex: 0 0 auto; margin: 0.15rem 0 0; }
.notification-result {
  margin-top: 0.75rem; padding: 0.75rem 0.85rem; border: 1px solid #d1d5db;
  border-radius: 7px; background: #f9fafb; color: #374151; font-size: 0.9rem;
  line-height: 1.45; white-space: pre-line;
}
.notification-result-success { border-color: #86efac; background: #f0fdf4; color: #166534; }
.notification-result-warning { border-color: #fbbf24; background: #fffbeb; color: #78350f; }
.notification-result-failure { border-color: #fca5a5; background: #fef2f2; color: #991b1b; }
.field-help { display: block; margin-top: 0.35rem; color: #6b7280; font-size: 0.82rem; line-height: 1.4; }
.field-help code { color: #374151; }

.user-detail-loading { color: #6b7280; padding: 2.5rem 1rem; text-align: center; }
.user-detail-error {
  padding: 1rem; border: 1px solid #fecaca; border-radius: 8px; background: #fef2f2;
  color: #991b1b;
}
.user-detail-error p { margin: 0 0 0.35rem; font-weight: 600; }
.user-detail-error .action-btn { margin-top: 1rem; color: #111; }
.user-detail-grid { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.user-detail-field, .metadata-context > div { min-width: 0; }
.user-detail-value, .break-all, .metadata-value { overflow-wrap: anywhere; word-break: break-word; }
.user-metadata-empty {
  padding: 1.25rem; border: 1px dashed #d1d5db; border-radius: 8px; background: #f9fafb;
  color: #6b7280; text-align: center;
}
.user-metadata-empty.compact { margin-top: 0.75rem; padding: 0.8rem; }
.metadata-context { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); margin-bottom: 0.75rem; }
.metadata-table-wrap { border: 1px solid #e5e7eb; border-radius: 8px; }
.metadata-table { table-layout: fixed; }
.metadata-table th:first-child, .metadata-table td:first-child { width: 35%; }
.metadata-key { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.85em; }
.metadata-value { white-space: pre-wrap; }

/* Store catalogue and fulfillment */
header#topbar { flex-wrap: wrap; }
header nav { flex-wrap: wrap; min-width: 0; }
main { max-width: 1500px; }

.page-heading {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem;
  margin-bottom: 1rem;
}
.page-heading h1 { margin: 0 0 0.25rem; font-size: 1.55rem; }
.page-heading .hint { margin: 0; }
.primary-action {
  flex: 0 0 auto; background: #2563eb; color: #fff; border: 0; border-radius: 6px;
  padding: 0.55rem 1rem; cursor: pointer; font: inherit;
}
.primary-action:hover { background: #1d4ed8; }
.primary-action:disabled { cursor: wait; opacity: 0.65; }
.table-scroll { overflow-x: auto; padding: 1px 1px 4px; }
.table-scroll table { min-width: 900px; }
.store-product-row { cursor: pointer; }
.store-product-row:focus-visible { outline: 2px solid #2563eb; outline-offset: -2px; }
.mono, code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.85em; }
.clamp-text { max-width: 28rem; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

.store-active, .store-fulfillment-fulfilled, .store-payment-paid { background: #dcfce7; color: #166534; }
.store-inactive, .store-fulfillment-cancelled, .store-payment-failed, .store-payment-refunded { background: #fee2e2; color: #b91c1c; }
.store-fulfillment-new, .store-payment-pending { background: #dbeafe; color: #1d4ed8; }
.store-fulfillment-processing, .store-fulfillment-sent, .store-payment-partially_refunded { background: #fef3c7; color: #92400e; }
.store-fulfillment-pending_payment { background: #f3f4f6; color: #4b5563; }

.section-heading {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem;
  margin: 1rem 0 0.65rem;
}
.section-heading h2 { margin: 0 0 0.2rem; font-size: 1.15rem; }
.section-heading .hint { margin: 0; }
.fulfillment-section { margin-top: 2rem; }
.count-pill { flex: 0 0 auto; background: #e5e7eb; color: #374151; border-radius: 999px; padding: 0.25rem 0.65rem; font-size: 0.8rem; font-weight: 600; }
.fulfillment-table { min-width: 920px !important; }
.fulfillment-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; min-width: 13rem; }
.fulfillment-actions .action-btn:disabled { cursor: wait; opacity: 0.6; }
.fulfillment-received { margin-top: 0.28rem; color: #6b7280; font-size: 0.76rem; line-height: 1.3; white-space: nowrap; }
.fulfillment-customer { margin-top: 0.25rem; color: #6b7280; font-size: 0.76rem; line-height: 1.3; }
.fulfillment-tracking { margin-top: 0.4rem; color: #4b5563; font-size: 0.76rem; line-height: 1.3; overflow-wrap: anywhere; }
.fulfillment-status-detail { margin-top: 0.35rem; color: #4b5563; font-size: 0.76rem; line-height: 1.3; }
.fulfillment-tracking .mono { font-size: 0.95em; }
.fulfillment-tracking-input { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.order-kind { display: inline-block; margin-bottom: 0.3rem; padding: 0.12rem 0.42rem; border-radius: 4px; font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.025em; }
.order-kind-store { background: #e0e7ff; color: #3730a3; }
.order-kind-company_device { background: #f3e8ff; color: #6b21a8; }
.fulfillment-status-new { background: #dbeafe; color: #1d4ed8; }
.fulfillment-status-processing, .fulfillment-status-sent { background: #fef3c7; color: #92400e; }
.fulfillment-status-fulfilled { background: #dcfce7; color: #166534; }
.fulfillment-status-return_due { background: #ffedd5; color: #9a3412; }
.fulfillment-status-returned { background: #ccfbf1; color: #0f766e; }
.fulfillment-status-cancelled { background: #fee2e2; color: #b91c1c; }
.fulfillment-status-pending_payment { background: #e5e7eb; color: #4b5563; }

.modal button:disabled { cursor: wait; opacity: 0.65; }

.store-form h3 { margin: 1.25rem 0 0.65rem; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.8rem 1rem; }
.form-grid .field { margin: 0; }
.form-grid .span-2 { grid-column: span 2; }
.modal .field textarea {
  width: 100%; padding: 0.5rem 0.7rem; border: 1px solid #d1d5db; border-radius: 6px;
  margin-top: 0.25rem; resize: vertical; font: inherit;
}
.check-grid {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.65rem 1rem;
  margin: 1rem 0;
}
.check-grid label { display: flex; align-items: center; gap: 0.45rem; font-size: 0.9rem; }
.order-summary { grid-template-columns: repeat(auto-fit, minmax(155px, 1fr)); }
.order-id-line {
  display: flex; align-items: baseline; gap: 0.7rem; flex-wrap: wrap; padding: 0.55rem 0.75rem;
  background: #f9fafb; border-radius: 7px; margin-bottom: 1rem;
}
.order-items-cell { min-width: 13rem; line-height: 1.35; }
.order-address-cell { min-width: 13rem; line-height: 1.35; }
.link-button {
  border: 0; background: transparent; color: #2563eb; padding: 0; cursor: pointer;
  font: inherit; text-decoration: none;
}
.link-button:hover { text-decoration: underline; }

@media (max-width: 850px) {
  header#topbar { gap: 0.75rem; padding: 0.7rem 1rem; }
  header nav { order: 3; flex-basis: 100%; overflow-x: auto; flex-wrap: nowrap; padding-bottom: 0.2rem; }
  header nav .tab { white-space: nowrap; }
  main { padding: 1rem; }
  .page-heading { align-items: stretch; flex-direction: column; }
  .primary-action { align-self: flex-start; }
  .form-grid, .check-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: auto; }
}
