Jelajahi Sumber

Merge branch '0.2.0b' into fix/faile-to-start

MartinNYHC 3 bulan lalu
induk
melakukan
fac02ff7b3
100 mengubah file dengan 11525 tambahan dan 1196 penghapusan
  1. 2 0
      .gitignore
  2. 139 0
      BETA_TEST_PLAN.md
  3. 42 0
      CHANGELOG.md
  4. 1 0
      CONTRIBUTING.md
  5. 1 1
      DOCKERHUB.md
  6. 13 4
      README.md
  7. 7 0
      backend/app/api/routes/archives.py
  8. 19 0
      backend/app/api/routes/camera.py
  9. 109 3
      backend/app/api/routes/cloud.py
  10. 970 0
      backend/app/api/routes/inventory.py
  11. 3 0
      backend/app/api/routes/kprofiles.py
  12. 7 0
      backend/app/api/routes/library.py
  13. 98 15
      backend/app/api/routes/maintenance.py
  14. 247 56
      backend/app/api/routes/printers.py
  15. 47 3
      backend/app/api/routes/spoolman.py
  16. 1 1
      backend/app/api/routes/support.py
  17. 317 0
      backend/app/core/bambu_colors.py
  18. 357 0
      backend/app/core/catalog_defaults.py
  19. 1 1
      backend/app/core/config.py
  20. 145 0
      backend/app/core/database.py
  21. 18 0
      backend/app/core/permissions.py
  22. 365 16
      backend/app/main.py
  23. 12 0
      backend/app/models/__init__.py
  24. 20 0
      backend/app/models/color_catalog.py
  25. 2 1
      backend/app/models/external_link.py
  26. 1 0
      backend/app/models/maintenance.py
  27. 44 0
      backend/app/models/spool.py
  28. 35 0
      backend/app/models/spool_assignment.py
  29. 18 0
      backend/app/models/spool_catalog.py
  30. 31 0
      backend/app/models/spool_k_profile.py
  31. 21 0
      backend/app/models/spool_usage_history.py
  32. 1 0
      backend/app/schemas/cloud.py
  33. 3 0
      backend/app/schemas/external_link.py
  34. 31 2
      backend/app/schemas/notification_template.py
  35. 1 1
      backend/app/schemas/printer.py
  36. 109 0
      backend/app/schemas/spool.py
  37. 17 0
      backend/app/schemas/spool_usage.py
  38. 32 6
      backend/app/services/archive.py
  39. 1 1
      backend/app/services/bambu_ftp.py
  40. 162 59
      backend/app/services/bambu_mqtt.py
  41. 36 15
      backend/app/services/external_camera.py
  42. 75 11
      backend/app/services/firmware_check.py
  43. 7 2
      backend/app/services/mqtt_relay.py
  44. 8 2
      backend/app/services/mqtt_smart_plug.py
  45. 55 19
      backend/app/services/notification_service.py
  46. 48 45
      backend/app/services/print_scheduler.py
  47. 54 37
      backend/app/services/printer_manager.py
  48. 310 0
      backend/app/services/spool_tag_matcher.py
  49. 108 55
      backend/app/services/spoolman.py
  50. 9 8
      backend/app/services/spoolman_tracking.py
  51. 420 0
      backend/app/services/usage_tracker.py
  52. 63 0
      backend/app/utils/printer_models.py
  53. 56 0
      backend/app/utils/threemf_tools.py
  54. 43 0
      backend/tests/integration/test_camera_api.py
  55. 6 6
      backend/tests/unit/services/test_bambu_ftp.py
  56. 15 12
      backend/tests/unit/services/test_printer_manager.py
  57. 66 0
      backend/tests/unit/services/test_spoolman_service.py
  58. 2 2
      backend/tests/unit/services/test_spoolman_tracking.py
  59. 401 0
      backend/tests/unit/services/test_usage_tracker.py
  60. 1 1
      backend/tests/unit/test_code_quality.py
  61. 190 2
      backend/tests/unit/test_scheduler_ams_mapping.py
  62. 122 0
      backend/tests/unit/test_scheduler_clear_plate.py
  63. 726 0
      backend/tests/unit/test_usage_tracker.py
  64. 193 0
      docker-publish-beta.sh
  65. 2 0
      frontend/src/App.tsx
  66. 1 0
      frontend/src/__tests__/components/AddPrinterDiscovery.test.tsx
  67. 134 0
      frontend/src/__tests__/components/AssignSpoolModal.test.tsx
  68. 77 0
      frontend/src/__tests__/components/ConfigureAmsSlotModal.test.tsx
  69. 77 141
      frontend/src/__tests__/components/LinkSpoolModal.test.tsx
  70. 1 1
      frontend/src/__tests__/components/PrintModal.test.tsx
  71. 177 0
      frontend/src/__tests__/components/PrinterQueueWidgetClearPlate.test.tsx
  72. 58 120
      frontend/src/__tests__/components/SpoolmanSettings.test.tsx
  73. 182 3
      frontend/src/__tests__/hooks/useFilamentMapping.test.ts
  74. 1 0
      frontend/src/__tests__/pages/PrintersPage.test.tsx
  75. 3 3
      frontend/src/__tests__/pages/SettingsPage.test.tsx
  76. 1 1
      frontend/src/__tests__/pages/StatsPage.test.tsx
  77. 43 0
      frontend/src/__tests__/utils/currency.test.ts
  78. 216 16
      frontend/src/api/client.ts
  79. 22 0
      frontend/src/components/AddExternalLinkModal.tsx
  80. 233 0
      frontend/src/components/AssignSpoolModal.tsx
  81. 583 0
      frontend/src/components/ColorCatalogSettings.tsx
  82. 187 0
      frontend/src/components/ColumnConfigModal.tsx
  83. 251 62
      frontend/src/components/ConfigureAmsSlotModal.tsx
  84. 14 2
      frontend/src/components/ConfirmModal.tsx
  85. 57 3
      frontend/src/components/FilamentHoverCard.tsx
  86. 66 5
      frontend/src/components/KProfilesView.tsx
  87. 73 38
      frontend/src/components/Layout.tsx
  88. 107 143
      frontend/src/components/LinkSpoolModal.tsx
  89. 13 2
      frontend/src/components/PrintModal/FilamentMapping.tsx
  90. 1 0
      frontend/src/components/PrintModal/types.ts
  91. 66 4
      frontend/src/components/PrinterQueueWidget.tsx
  92. 18 1
      frontend/src/components/SkipObjectsModal.tsx
  93. 397 0
      frontend/src/components/SpoolCatalogSettings.tsx
  94. 504 0
      frontend/src/components/SpoolFormModal.tsx
  95. 101 0
      frontend/src/components/SpoolUsageHistory.tsx
  96. 317 264
      frontend/src/components/SpoolmanSettings.tsx
  97. 228 0
      frontend/src/components/spool-form/AdditionalSection.tsx
  98. 286 0
      frontend/src/components/spool-form/ColorSection.tsx
  99. 296 0
      frontend/src/components/spool-form/FilamentSection.tsx
  100. 268 0
      frontend/src/components/spool-form/PAProfileSection.tsx

+ 2 - 0
.gitignore

@@ -28,6 +28,8 @@ npm-debug.log*
 # Database
 *.db
 *.db-journal
+*.db-wal
+*.db-shm
 
 # Archive files (user data)
 archive/

+ 139 - 0
BETA_TEST_PLAN.md

@@ -0,0 +1,139 @@
+# Beta Test Plan — Spool Inventory & Related Features
+
+## Prerequisites
+
+- At least one printer connected with AMS
+- At least one Bambu Lab spool (RFID) loaded in AMS
+- At least one spool without RFID tag
+- At least one empty AMS slot (or slot with non-BL filament)
+- A 3MF file ready to print (small/fast test print recommended)
+
+---
+
+## 1. Filament Tracking Mode Switching
+
+**Location:** Settings > Filament
+
+| # | Test | Steps | Expected |
+|---|------|-------|----------|
+| 1.1 | Default mode | Open Settings > Filament | "Built-in Inventory" card is selected (green border), info panel shows RFID/usage/catalog bullet points |
+| 1.2 | Switch to Spoolman | Click "Spoolman" card | Spoolman config appears (URL input, Sync Mode, connection status). Built-in info panel disappears |
+| 1.3 | Switch back | Click "Built-in Inventory" card | Spoolman config disappears, built-in info panel reappears |
+| 1.4 | Persistence | Switch mode, reload page | Selected mode persists after reload |
+| 1.5 | Spoolman disabled state | Select Spoolman without URL, check Connect button | Connect button should be disabled when URL is empty |
+
+---
+
+## 2. Spool Management
+
+**Location:** Inventory page (sidebar)
+
+| # | Test | Steps | Expected |
+|---|------|-------|----------|
+| 2.1 | Add spool | Click "+ Add Spool", fill material/brand/color/weight, save | Spool appears in table with correct info |
+| 2.2 | Edit spool | Click a spool row, change fields, save | Changes reflected in table |
+| 2.3 | Remaining weight | Edit spool > Additional section > adjust remaining weight | Remaining weight = label_weight - weight_used, slider/input updates correctly |
+| 2.4 | Spool catalog | Edit spool > Additional > Empty Spool Weight dropdown | Pre-defined spool weights shown, selecting one updates the field |
+| 2.5 | Color picker | Edit spool > Color section | Recent colors, brand palettes, and hex input all work |
+| 2.6 | PA profile tab | Edit spool > PA Profile tab | Matching K-profiles shown grouped by printer/nozzle (requires calibration data) |
+| 2.7 | Archive spool | Archive a spool from context menu or edit | Spool moves to "Archived" tab |
+| 2.8 | Delete spool | Delete a spool | Spool removed from all views |
+| 2.9 | Summary cards | Check top of inventory page | Total Inventory, Total Consumed, By Material, In Printer, Low Stock cards show correct values |
+| 2.10 | Filters | Try Active/Archived/All tabs, material/brand dropdowns, search | Filtering works correctly |
+| 2.11 | View modes | Toggle between Table and Card view | Both views show correct spool data |
+
+---
+
+## 3. AMS Slot Assignment
+
+**Location:** Printers page, AMS hover cards
+
+| # | Test | Steps | Expected |
+|---|------|-------|----------|
+| 3.1 | Assign spool | Hover over an empty/non-BL AMS slot > click "Assign Spool" | Modal opens showing only manual (non-BL) spools |
+| 3.2 | BL spools filtered | Open assign modal | Bambu Lab spools (with RFID tags) are NOT in the list |
+| 3.3 | Assigned spools filtered | Assign spool A to slot 1, then open assign modal for slot 2 | Spool A is NOT in the list (already assigned) |
+| 3.4 | Current slot spool visible | Open assign for slot that already has a spool | The currently assigned spool IS still shown (for reassignment) |
+| 3.5 | Confirm assignment | Select spool, click "Assign Spool" | Slot shows the assigned spool info on hover |
+| 3.6 | Unassign spool | Hover over assigned slot > click "Unassign" | Spool removed from slot, slot shows default info |
+| 3.7 | BL slot — no buttons | Hover over a slot with a Bambu Lab spool (RFID) | No "Assign Spool" or "Unassign" buttons shown |
+| 3.8 | Empty slot display | Check an empty AMS slot | Shows type from AMS data or "Empty" (localized) |
+
+---
+
+## 4. Auto-Unlink on BL Spool Insertion
+
+| # | Test | Steps | Expected |
+|---|------|-------|----------|
+| 4.1 | BL spool replaces manual | Assign a manual spool to a slot, then physically insert a BL spool into that slot | Assignment automatically removed, slot now shows BL spool info |
+| 4.2 | Log message | Check backend logs after 4.1 | Log shows "Auto-unlink: spool X AMSY-TZ — Bambu Lab spool detected" |
+
+---
+
+## 5. Spool Tag Linking
+
+**Location:** Printers page, AMS hover cards for BL spools
+
+| # | Test | Steps | Expected |
+|---|------|-------|----------|
+| 5.1 | Link modal | Hover over BL spool slot > click "Link to Spool" (if available) | Modal opens showing only untagged spools |
+| 5.2 | Search filter | Type in search box | Spools filtered by material/brand/color |
+| 5.3 | Link spool | Click a spool in the list | Success toast, modal closes, spool now linked to tag |
+| 5.4 | Tagged spools hidden | After linking spool A, open link modal again | Spool A is no longer in the list |
+
+---
+
+## 6. Usage Tracking — BL Spools (AMS Remain%)
+
+| # | Test | Steps | Expected |
+|---|------|-------|----------|
+| 6.1 | Session capture | Start a print with a BL spool | Backend log shows "Captured start remain% for printer X (N trays)" |
+| 6.2 | Completed print | Let print finish | Spool's weight_used increases by (delta% * label_weight). Check inventory page remaining weight |
+| 6.3 | Failed/aborted print | Start and cancel a print mid-way | Usage still tracked based on remain% delta at time of stop |
+| 6.4 | No double-tracking | Complete a print with BL spool | Only AMS delta tracking applies (no 3MF fallback for this spool) |
+
+---
+
+## 7. Usage Tracking — Non-BL Spools (3MF Estimates)
+
+| # | Test | Steps | Expected |
+|---|------|-------|----------|
+| 7.1 | Assign and print | Assign a manual spool to AMS slot, start a print using that slot | Backend log shows "3MF fallback available" at print start |
+| 7.2 | Completed print | Let print finish | Spool's weight_used increases by the 3MF estimated used_g. Check inventory page |
+| 7.3 | Failed print scaling | Start a print, cancel at ~50% | Usage = 3MF estimate * (progress/100). E.g., 100g estimate at 50% = ~50g tracked |
+| 7.4 | Multi-slot print | Print using multiple filament slots (some BL, some manual) | BL spools tracked via remain% delta, manual spools via 3MF. No double-counting |
+| 7.5 | Slot mapping | Use a spool in AMS slot 5 (second AMS unit) | Correctly maps to AMS 1, Tray 0. Check usage history shows correct ams_id/tray_id |
+
+---
+
+## 8. Edge Cases
+
+| # | Test | Steps | Expected |
+|---|------|-------|----------|
+| 8.1 | No AMS data | Print from external spool (no AMS) | No crash, usage tracking gracefully skipped |
+| 8.2 | No archive | Print without 3MF archive available | AMS delta path still works for BL spools, 3MF path skipped gracefully |
+| 8.3 | Spool refilled | If remain% goes UP between start and end (spool swapped/refilled) | Negative delta skipped, no negative usage recorded |
+| 8.4 | Rapid print start/stop | Start and immediately cancel a print | No errors, minimal or zero usage tracked |
+| 8.5 | Concurrent printers | Print on two printers simultaneously | Each printer tracked independently, no cross-contamination |
+
+---
+
+## 9. UI/UX Checks
+
+| # | Test | Steps | Expected |
+|---|------|-------|----------|
+| 9.1 | Mobile layout | Open on mobile or narrow browser | Inventory page, assign modal, and settings all responsive |
+| 9.2 | Dark/light mode | Toggle theme | All inventory UI elements properly themed |
+| 9.3 | Language switching | Switch to German and Japanese | All inventory strings translated (no hardcoded English) |
+| 9.4 | Keyboard nav | Use keyboard shortcuts on printers page | AMS slot interactions accessible |
+
+---
+
+## Reporting Issues
+
+When reporting a bug, please include:
+- Test case number (e.g., "Beta 9.1 failed")
+- Browser + version
+- Screenshot or screen recording
+- Backend logs (if relevant — check Docker logs)
+- Steps to reproduce if different from above

+ 42 - 0
CHANGELOG.md

@@ -2,6 +2,47 @@
 
 All notable changes to Bambuddy will be documented in this file.
 
+## [0.2.0b] - Not released
+
+### New Features
+- **Spool Inventory — AMS Slot Assignment** — Assign inventory spools to AMS slots for filament tracking. Hover over any non-Bambu-Lab AMS slot to assign or unassign spools. The assign modal filters out Bambu Lab spools (tracked via RFID) and spools already assigned to other slots. Bambu Lab spool slots automatically hide assign/unassign UI since they are managed by the AMS. When a Bambu Lab spool is inserted into a slot with a manual assignment, the assignment is automatically unlinked.
+- **Spool Inventory — Remaining Weight Editing** — Edit the remaining filament weight when adding or editing a spool. The new "Remaining Weight" field in the Additional section shows current weight (label weight minus consumed) with a max reference. Edits are stored as `weight_used` internally.
+- **Spool Inventory — Unified 3MF-Based Usage Tracking** ([#336](https://github.com/maziggy/bambuddy/issues/336)) — All spools (Bambu Lab and third-party) now use 3MF slicer estimates as the primary tracking source. Per-filament `used_g` data from the archived 3MF file provides precise per-spool consumption. For failed or aborted prints, per-layer G-code analysis provides accurate partial usage up to the exact failure layer, with linear progress scaling as fallback. AMS remain% delta is the final fallback for G-code-only prints without an archived 3MF. Slot-to-tray mapping uses queue `ams_mapping` for queue-initiated prints and the printer's `tray_now` state for single-filament non-queue prints, ensuring the correct physical spool is always tracked.
+- **Notification Templates — Filament Usage Variables** ([#336](https://github.com/maziggy/bambuddy/issues/336)) — `print_complete`, `print_failed`, and `print_stopped` notification events now expose `{filament_grams}` (total grams, scaled by progress for partial prints), `{filament_details}` (per-filament breakdown with AMS slot info, e.g. "AMS-A T1 PLA: 12.4g | AMS-A T3 PETG: 2.8g"), and `{progress}` (completion percentage for failed/stopped prints). The `{filament_details}` variable includes the AMS unit and tray position for each filament used, with "Ext" shown for external spool holders. Falls back to type-only format (e.g. "PLA: 10.0g") when usage tracking data is unavailable. Webhook payloads include `filament_used`, `filament_details`, and `progress` fields. Per-slot filament data is stored in archive `extra_data` for downstream use.
+- **Printer Status Summary Bar — Next Available & Availability Count** ([#354](https://github.com/maziggy/bambuddy/issues/354)) — The status bar on the Printers page now shows an availability count ("X available") alongside the printing/offline counts, and a "Next available" indicator showing which printing printer will finish soonest — with printer name, mini progress bar, completion percentage, and remaining time. Useful for print farms to quickly identify the next free printer. Updates in real-time via WebSocket. Translated in all 4 locales (en, de, ja, it).
+- **Nozzle-Aware AMS Filament Mapping for Dual-Nozzle Printers** ([#318](https://github.com/maziggy/bambuddy/issues/318)) — On dual-nozzle printers (H2D, H2D Pro), each AMS unit is physically connected to either the left or right nozzle. Bambuddy now reads nozzle assignments from the 3MF file (`filament_nozzle_map` + `physical_extruder_map` in `project_settings.config`) and constrains filament matching to only AMS trays connected to the correct nozzle via `ams_extruder_map`. Applies to the print scheduler, reprint modal, queue modal, and multi-printer selection. Falls back gracefully to unfiltered matching when no trays exist on the target nozzle. The filament mapping UI shows L/R nozzle badges for dual-nozzle prints. Translated in all 4 locales (en, de, ja, it).
+- **Dual External Spool Support for H2D** — H2-series printers with two external spool holders (Ext-L and Ext-R) are now fully supported. The external spool section renders as a grid with both slots, each showing filament type, color, fill level, and hover card details. Previously only a single external spool was displayed. Applies to the printer card, filament mapping, print scheduler, usage tracking, and inventory assignment. The `vt_tray` field is now an array across the entire stack (MQTT, API, WebSocket, frontend).
+- **AMS Slot Configuration — Model Filtering & Pre-Population** — The Configure AMS Slot modal now filters filament presets by the connected printer model. Only presets matching the printer (e.g., "@BBL X1C" presets for X1C printers) and generic presets without a model suffix are shown. Local presets are filtered by their `compatible_printers` field. When re-configuring an already-configured slot, the modal pre-selects the saved preset, pre-populates the color, and auto-selects the active K-profile. The preset list auto-scrolls to the selected item. All modal strings are now fully translated in 5 locales (en, de, fr, it, ja).
+- **K-Profiles View — Accurate Filament Name Resolution** — K-profile filament names are now resolved from builtin filament tables and user cloud presets (via new `/cloud/filament-id-map` endpoint) instead of showing raw IDs like "GFU99" or "P4d64437". Falls back to extracting names from the profile name field.
+
+### Fixed
+- **Bulk Archive Delete Leaves Orphaned Database Records** — When bulk-deleting archives, the files were removed from disk before the database commit. If concurrent SQLite writes caused a lock timeout, the commit failed and rolled back — leaving database records pointing to deleted files (broken thumbnails, 404 errors). Fixed by deleting the database record first and only removing files after a successful commit.
+- **Model-Specific Maintenance Tasks for Carbon Rods vs Linear Rails** ([#351](https://github.com/maziggy/bambuddy/issues/351)) — Maintenance tasks "Clean Carbon Rods" and "Lubricate Linear Rails" were shown for all printers regardless of motion system. H2 and A1 series use linear rails (not carbon rods), and X1/P1/P2S series use carbon rods (not linear rails). Maintenance types are now classified by rod/rail type: "Lubricate Carbon Rods" and "Clean Carbon Rods" for X1/P1/P2S, "Lubricate Linear Rails" and "Clean Linear Rails" for A1/H2. Stale and duplicate system types are automatically cleaned up on startup. Includes model-specific wiki links and i18n keys for all 4 locales.
+- **AMS Slot Configuration Overwritten on Startup** — Bambuddy was resetting AMS slot filament presets on every startup and reconnection. The `on_ams_change` callback unconditionally unlinked Bambu Lab spool assignments on each MQTT push-all response, then re-assigned them by sending `ams_filament_setting` without a `setting_id`, which cleared the printer's filament preset. Now compares spool RFID identifiers (`tray_uuid` / `tag_uid`) before unlinking — if the same spool is still in the slot, the assignment is preserved and no `ams_filament_setting` command is sent.
+- **Bambu Lab Spool Detection False Positives** — The `is_bambu_lab_spool()` function (backend) and `isBambuLabSpool()` (frontend) incorrectly identified third-party spools as Bambu Lab spools when they used Bambu generic filament presets (e.g., "Generic PLA"). The `tray_info_idx` field (e.g., "GFA00") identifies the filament *type*, not the spool manufacturer — third-party spools using Bambu presets also have GF-prefixed values. Removed `tray_info_idx` from detection logic; now uses only hardware RFID identifiers (`tray_uuid` and `tag_uid`) which are physically embedded in genuine Bambu Lab spools.
+- **FTP Disconnect Raises EOFError When Server Dies** — `BambuFTPClient.disconnect()` only caught `OSError` and `ftplib.Error`, but `quit()` raises `EOFError` when the server has closed the connection mid-session. `EOFError` is not a subclass of either, so it propagated to callers. Now caught alongside the other exception types for clean best-effort disconnect.
+- **RFID Spool Data Erased by Periodic AMS Updates** — Periodic MQTT push-all responses cleared `tag_uid` and `tray_uuid` fields because they were included in the "always update" list. These fields are now preserved during updates and only cleared when a spool is physically removed (slot clearing detected by empty `tray_type`). This fixes the AMS "eye" icon disappearing for RFID spools after startup.
+- **AMS Slot Configuration Overwrites RFID Spool State** — Configuring an AMS slot for an RFID-detected Bambu Lab spool sent `ams_set_filament_setting`, which replaced the firmware's RFID-managed filament config with a manual one — causing the slicer's "eye" icon to change to a "pen" icon. Now detects RFID spools and skips the filament setting command, only sending K-profile selection.
+- **K-Profile Selection Corrupts Existing Profiles on X1C/P1S** — The `extrusion_cali_sel` command included a `setting_id` field that BambuStudio never sends, causing firmware to mislink calibration data. The `extrusion_cali_set` command was sent unconditionally, overwriting existing profile metadata. Now `setting_id` is removed from selection commands, and `extrusion_cali_set` is only sent when no existing profile is selected (`cali_idx < 0`).
+
+### Improved
+- **SQLite WAL Mode for Database Reliability** — Database now uses Write-Ahead Logging (WAL) mode with a 5-second busy timeout, reducing "database is locked" errors under concurrent access. WAL mode allows simultaneous reads during writes, improving responsiveness for multi-printer setups. Automatically enabled on startup.
+- **External Camera Not Used for Snapshot + Stream Dropping** ([#325](https://github.com/maziggy/bambuddy/issues/325)) — The snapshot endpoint (`/camera/snapshot`) always used the internal printer camera even when an external camera was configured. Now checks for external camera first, matching the existing stream endpoint behavior. Also fixed external MJPEG and RTSP streams silently dropping every ~60 seconds due to missing reconnect logic — the underlying stream generators exit on read timeout, and the caller now retries up to 3 times with a 2-second delay instead of ending the stream.
+- **H2C Nozzle Rack Text Unreadable on Light Filament Colors** ([#300](https://github.com/maziggy/bambuddy/issues/300)) — Nozzle rack slots use the loaded filament color as background, but white/light filaments made the white "0.4" text nearly invisible. Now uses a luminance check to switch to dark text on light backgrounds.
+- **File Downloads Show Generic Filenames** ([#334](https://github.com/maziggy/bambuddy/issues/334)) — Downloaded files with special characters in their names (spaces, umlauts, parentheses) were saved as generic `file_1`, `file_2` instead of the original filename. The `Content-Disposition` header parser now handles RFC 5987 percent-encoded filenames (`filename*=utf-8''...`) used by FastAPI for non-ASCII characters. Fix applied to all download endpoints (library files, archives, source files, F3D files, project exports, support bundles, printer files).
+- **Printer Card Cover Image Not Updating Between Prints** — The cover image on the printer card only refreshed on page reload. The `<img>` URL was always the same (`/printers/{id}/cover`) regardless of which print was active, so the browser served its cached image. Now appends the print name as a cache-busting query parameter so the browser fetches the new cover when a different print starts.
+- **Telegram Bold Title Broken by Underscores in Message** ([#332](https://github.com/maziggy/bambuddy/issues/332)) — Telegram notifications showed literal `*Title*` asterisks instead of bold text when the message body contained underscores (e.g. job name `A1_plate_8`, error code `0300_0001`). The code was disabling Markdown parsing entirely when underscores were detected. Now escapes underscores in the body with `\_` so Markdown rendering stays enabled.
+- **Queued Jobs Incorrectly Archived After Duplicate Execution Detection** ([#341](https://github.com/maziggy/bambuddy/issues/341)) — When the same file was added to the print queue multiple times, only the first job executed. All subsequent jobs were automatically skipped with "already printed X hours ago" because they shared the same archive reference, and a safety check incorrectly treated them as phantom reprints. The same issue also affected single queue items created from recently completed archives. Removed the overly broad 4-hour duplicate detection check — the crash recovery scenario it guarded against is already handled by the queue item status lifecycle.
+
+### New Features
+- **External Links: Open in New Tab** ([#338](https://github.com/maziggy/bambuddy/issues/338)) — External sidebar links can now optionally open in a new browser tab instead of an iframe. Sites behind reverse proxies (Traefik, nginx) that send `X-Frame-Options: SAMEORIGIN` or CSP `frame-ancestors` headers block iframe embedding, causing "refused to connect" errors. A new "Open in new tab" toggle in the add/edit link modal lets users choose per-link. Keyboard shortcuts (number keys) also respect the setting. Defaults to iframe (existing behavior) for backward compatibility.
+- **Print Queue: Clear Plate Confirmation** — When a print finishes or fails and more items are queued, the printer card now shows a "Clear Plate & Start Next" button. The scheduler no longer auto-starts the next print while the printer is in FINISH or FAILED state — the user must confirm the build plate has been cleared first. This prevents prints from starting on a dirty plate. The button respects the `printers:control` permission and is available in all supported languages (en/de/ja).
+
+### Improved
+- **Skip Objects: Confirmation Dialog** ([#346](https://github.com/maziggy/bambuddy/issues/346)) — Added a warning confirmation modal before skipping an object during a print. Shows the object name and warns the action is irreversible. Prevents accidentally skipping the wrong object. Translated in all 4 locales (en, de, ja, it).
+- **Additional Currency Options** ([#329](https://github.com/maziggy/bambuddy/issues/329), [#333](https://github.com/maziggy/bambuddy/issues/333)) — Added 17 additional currencies to the cost tracking dropdown: HKD, INR, KRW, SEK, NOK, DKK, PLN, BRL, TWD, SGD, NZD, MXN, CZK, THB, ZAR, RUB.
+- **Move Email Settings Under Authentication Tab** — Renamed the settings "Users" tab to "Authentication" and moved the standalone "Global Email" tab into it as an "Email Authentication" sub-tab. Groups email/SMTP configuration with user management where it logically belongs. Legacy `?tab=email` URLs are handled automatically.
+
 ## [0.1.9] - 2026-02-10
 
 ### New Features
@@ -37,6 +78,7 @@ All notable changes to Bambuddy will be documented in this file.
 - **Virtual Printer IP Override for Server Mode** ([#52](https://github.com/maziggy/bambuddy/issues/52)) — The `remote_interface_ip` setting (network interface override) was only used in proxy mode, but users with multiple network interfaces (LAN + Tailscale, Docker bridges) also needed it in server modes (immediate/review/print_queue). Auto-detected IP from `_get_local_ip()` followed the OS default route, causing wrong IP in TLS certificate SAN (handshake failures) and SSDP broadcasts (slicer can't discover printer). Now the interface override applies to all modes: included in certificate SAN, passed to SSDP server as advertise IP, and triggers service restart on change. UI dropdown shown for all modes when enabled (not just proxy).
 - **Wrong Thumbnail When Reprinting Same Project** ([#314](https://github.com/maziggy/bambuddy/issues/314)) — Reprinting a project with the same name but a different bed layout showed the old thumbnail during printing. The cover image cache was keyed by `subtask_name` and never invalidated between prints, so a cache hit returned the stale first-print thumbnail. Now the cover cache is cleared on every print start.
 - **Wrong Timelapse Attached to Archive** ([#315](https://github.com/maziggy/bambuddy/issues/315)) — After a print, the archive could receive a timelapse from a previous print instead of the just-completed one. The auto-scan sorted MP4 files by mtime and grabbed the "most recent," but in LAN-only mode (no NTP) the printer's clock is wrong, making mtime unreliable. Replaced with a snapshot-diff approach: baseline existing files before waiting, then detect the new file that appears after encoding. Falls back to print-name matching if no new file is found after retries.
+- **Timelapse Not Attached — Baseline Race Condition** ([#315](https://github.com/maziggy/bambuddy/issues/315)) — Follow-up to the snapshot-diff timelapse fix: the baseline of existing MP4 files was captured at print completion time inside a background task, but fast-encoding printers could finish writing the timelapse before the baseline was taken, causing the new file to appear in the baseline and never be detected as "new." Moved baseline capture to print start time, when the timelapse file cannot possibly exist yet. Falls back to completion-time baseline if the app was restarted mid-print.
 - **Calibration Prints Archived** ([#315](https://github.com/maziggy/bambuddy/issues/315)) — Standalone calibration prints (flow, vibration, bed leveling) were being archived as regular prints. The calibration gcode (`/usr/etc/print/auto_cali_for_user.gcode`) and other internal printer files under `/usr/` are now detected and skipped during print start.
 - **Camera Stop 401 When Auth Enabled** — Camera stop requests (`sendBeacon`) failed with 401 Unauthorized when authentication was enabled because `sendBeacon` cannot send auth headers. Replaced with `fetch` + `keepalive: true` which supports Authorization headers while remaining reliable during page unload.
 - **Spoolman Creates Duplicate Spools on Startup** ([#295](https://github.com/maziggy/bambuddy/pull/295)) — Each AMS tray independently fetched all spools from Spoolman, causing redundant API calls and duplicate spool creation with large databases (300+ spools). Now fetches spools once and reuses cached data across all tray operations. Added retry logic (3 attempts, 500ms delay) with connection recreation for transient network errors.

+ 1 - 0
CONTRIBUTING.md

@@ -166,6 +166,7 @@ Translations live in `frontend/src/i18n/locales/`:
 |------|----------|
 | `en.ts` | English (primary) |
 | `de.ts` | German |
+| `fr.ts` | French |
 | `ja.ts` | Japanese |
 
 ### Adding New Strings

+ 1 - 1
DOCKERHUB.md

@@ -92,7 +92,7 @@ docker compose pull && docker compose up -d
 
 | Series | Models | Status |
 |---|---|---|
-| H2 | H2D | Tested |
+| H2 | H2C, H2D, H2D Pro, H2S | Tested |
 | X1 | X1 Carbon, X1E | Tested |
 | P1 | P1P, P1S | Compatible |
 | P2 | P2S | Compatible |

+ 13 - 4
README.md

@@ -72,7 +72,7 @@ Perfect for remote print farms, traveling makers, or accessing your home printer
 - Duplicate detection & full-text search
 - Photo attachments & failure analysis
 - Timelapse editor (trim, speed, music)
-- Re-print to any connected printer with AMS mapping (auto-match or manual slot selection, multi-plate support)
+- Re-print to any connected printer with AMS mapping (auto-match or manual slot selection, multi-plate support, nozzle-aware matching for dual-nozzle H2D/H2D Pro)
 - Plate thumbnail browsing for multi-plate archives (hover to navigate between plates)
 - Archive comparison (side-by-side diff)
 - Tag management (rename/delete across all archives)
@@ -88,7 +88,8 @@ Perfect for remote print farms, traveling makers, or accessing your home printer
 - Resizable printer cards (S/M/L/XL)
 - Skip objects during print
 - AMS slot RFID re-read
-- AMS slot configuration (custom presets, K profiles, color picker)
+- AMS slot configuration (model-filtered presets, K profiles, color picker, pre-population for configured slots)
+- Dual external spool support for H2D (Ext-L / Ext-R)
 - HMS error monitoring with history
 - Print success rates & trends
 - Filament usage tracking
@@ -103,6 +104,7 @@ Perfect for remote print farms, traveling makers, or accessing your home printer
 - Per-printer AMS mapping (individual slot configuration for print farms)
 - Scheduled prints (date/time)
 - Queue Only mode (stage without auto-start)
+- Clear plate confirmation between queued prints
 - Smart plug integration (Tasmota, Home Assistant, MQTT)
 - MQTT smart plugs: Subscribe to Zigbee2MQTT, Shelly, or any MQTT topic for energy monitoring
 - Energy consumption tracking (per-print kWh and cost)
@@ -139,12 +141,19 @@ Perfect for remote print farms, traveling makers, or accessing your home printer
 - Email, Pushover, ntfy
 - Custom webhooks
 - Quiet hours & daily digest
-- Customizable message templates
+- Customizable message templates with per-filament usage details
 - Print finish photo URL in notifications
+- Filament usage and progress in failed/cancelled print notifications
 - HMS error alerts (AMS, nozzle, etc.)
 - Build plate detection alerts
 - Queue events (waiting, skipped, failed)
 
+### 🧵 Spool Inventory
+- Built-in spool inventory with AMS slot assignment, usage tracking, and remaining weight management
+- Automatic filament consumption tracking: 3MF slicer estimates for all spools (primary), AMS remain% delta as fallback
+- Per-layer gcode accuracy for partial prints (failed/cancelled), with linear scaling fallback
+- Spool catalog, color catalog, PA profile matching, and low-stock alerts
+
 ### 🔧 Integrations
 - [Spoolman](https://github.com/Donkie/Spoolman) filament sync with per-filament usage tracking and fill level display
 - MQTT publishing for Home Assistant, Node-RED, etc.
@@ -194,7 +203,7 @@ Perfect for remote print farms, traveling makers, or accessing your home printer
 </tr>
 </table>
 
-**Plus:** Configurable slicer (Bambu Studio / OrcaSlicer) • Customizable themes (style, background, accent) • Mobile responsive • Keyboard shortcuts • Multi-language (EN/DE) • Auto updates • Database backup/restore • System info dashboard
+**Plus:** Configurable slicer (Bambu Studio / OrcaSlicer) • Customizable themes (style, background, accent) • Mobile responsive • Keyboard shortcuts • Multi-language (EN/DE/JA/IT) • Auto updates • Database backup/restore • System info dashboard
 
 ---
 

+ 7 - 0
backend/app/api/routes/archives.py

@@ -21,6 +21,7 @@ from backend.app.models.filament import Filament
 from backend.app.models.user import User
 from backend.app.schemas.archive import ArchiveResponse, ArchiveStats, ArchiveUpdate, ReprintRequest
 from backend.app.services.archive import ArchiveService
+from backend.app.utils.threemf_tools import extract_nozzle_mapping_from_3mf
 
 logger = logging.getLogger(__name__)
 
@@ -2669,6 +2670,12 @@ async def get_filament_requirements(
             # Sort by slot ID
             filaments.sort(key=lambda x: x["slot_id"])
 
+            # Enrich with nozzle mapping for dual-nozzle printers
+            nozzle_mapping = extract_nozzle_mapping_from_3mf(zf)
+            if nozzle_mapping:
+                for filament in filaments:
+                    filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
+
     except Exception as e:
         logger.warning("Failed to parse filament requirements from archive %s: %s", archive_id, e)
 

+ 19 - 0
backend/app/api/routes/camera.py

@@ -547,6 +547,25 @@ async def camera_snapshot(
 
     printer = await get_printer_or_404(printer_id, db)
 
+    # Check for external camera first
+    if printer.external_camera_enabled and printer.external_camera_url:
+        from backend.app.services.external_camera import capture_frame
+
+        frame_data = await capture_frame(printer.external_camera_url, printer.external_camera_type, timeout=15)
+        if not frame_data:
+            raise HTTPException(
+                status_code=503,
+                detail="Failed to capture frame from external camera.",
+            )
+        return Response(
+            content=frame_data,
+            media_type="image/jpeg",
+            headers={
+                "Cache-Control": "no-cache, no-store, must-revalidate",
+                "Content-Disposition": f'inline; filename="snapshot_{printer_id}.jpg"',
+            },
+        )
+
     # Create temporary file for the snapshot
     with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
         temp_path = Path(f.name)

+ 109 - 3
backend/app/api/routes/cloud.py

@@ -246,11 +246,12 @@ async def get_slicer_settings(
 
         for api_key, our_type in type_mapping.items():
             type_data = data.get(api_key, {})
-            # Combine public and private presets, private (user's own) first
-            all_settings = type_data.get("private", []) + type_data.get("public", [])
+            private_settings = type_data.get("private", [])
+            public_settings = type_data.get("public", [])
 
             parsed = []
-            for s in all_settings:
+            # Private (custom) presets first
+            for s in private_settings:
                 parsed.append(
                     SlicerSetting(
                         setting_id=s.get("setting_id", s.get("id", "")),
@@ -259,6 +260,20 @@ async def get_slicer_settings(
                         version=s.get("version"),
                         user_id=s.get("user_id"),
                         updated_time=s.get("updated_time"),
+                        is_custom=True,
+                    )
+                )
+            # Public (default) presets
+            for s in public_settings:
+                parsed.append(
+                    SlicerSetting(
+                        setting_id=s.get("setting_id", s.get("id", "")),
+                        name=s.get("name", "Unknown"),
+                        type=our_type,
+                        version=s.get("version"),
+                        user_id=s.get("user_id"),
+                        updated_time=s.get("updated_time"),
+                        is_custom=False,
                     )
                 )
             setattr(result, our_type, parsed)
@@ -302,6 +317,22 @@ async def get_setting_detail(
         raise HTTPException(status_code=500, detail=str(e))
 
 
+@router.get("/filaments", response_model=list[SlicerSetting])
+async def get_filament_presets(
+    version: str = "02.04.00.70",
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
+):
+    """
+    Get just filament presets (convenience endpoint).
+
+    Returns all filament presets with custom presets first.
+    Uses the same cache as get_slicer_settings.
+    """
+    settings = await get_slicer_settings(version=version, db=db)
+    return settings.filament
+
+
 # Cache for filament preset info (setting_id -> {name, k})
 _filament_cache: dict[str, dict] = {}
 _filament_cache_time: float = 0
@@ -844,6 +875,81 @@ def _load_fields(preset_type: str) -> dict:
     return data
 
 
+@router.get("/builtin-filaments")
+async def get_builtin_filaments(
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
+):
+    """
+    Get built-in filament names as a fallback source.
+
+    Returns the static _BUILTIN_FILAMENT_NAMES table as a list of
+    {filament_id, name} objects.  Used by the frontend when cloud
+    and local profiles are unavailable.
+    """
+    return [{"filament_id": fid, "name": name} for fid, name in _BUILTIN_FILAMENT_NAMES.items()]
+
+
+# Cache for filament_id → name mapping (resolved from cloud preset details)
+_filament_id_name_cache: dict[str, str] = {}
+_filament_id_name_cache_time: float = 0
+
+
+@router.get("/filament-id-map")
+async def get_filament_id_map(
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
+):
+    """
+    Get filament_id → name mapping for user cloud presets.
+
+    K-profiles store a filament_id (e.g., "P4d64437") which is different from
+    the cloud preset setting_id (e.g., "PFUS9ac902733670a9"). This endpoint
+    fetches details for all custom presets and returns the mapping.
+    Cached for 5 minutes.
+    """
+    import time
+
+    global _filament_id_name_cache, _filament_id_name_cache_time
+
+    if _filament_id_name_cache and time.time() - _filament_id_name_cache_time < FILAMENT_CACHE_TTL:
+        return _filament_id_name_cache
+
+    token, _ = await get_stored_token(db)
+    if not token:
+        return _filament_id_name_cache or {}
+
+    cloud = get_cloud_service()
+    cloud.set_token(token)
+    if not cloud.is_authenticated:
+        return _filament_id_name_cache or {}
+
+    try:
+        data = await cloud.get_slicer_settings()
+        custom_presets = data.get("filament", {}).get("private", [])
+
+        result: dict[str, str] = {}
+        for preset in custom_presets:
+            setting_id = preset.get("setting_id", "")
+            if not setting_id:
+                continue
+            try:
+                detail = await cloud.get_setting_detail(setting_id)
+                fid = detail.get("filament_id", "")
+                name = detail.get("name", "")
+                if fid and name:
+                    # Strip printer/nozzle suffix: "Devil Design PLA Basic @Bambu Lab H2D 0.4 nozzle" → "Devil Design PLA Basic"
+                    clean_name = name.split(" @")[0].strip() if " @" in name else name
+                    result[fid] = clean_name
+            except Exception:
+                pass
+
+        _filament_id_name_cache = result
+        _filament_id_name_cache_time = time.time()
+        return result
+    except Exception:
+        return _filament_id_name_cache or {}
+
+
 @router.get("/fields/{preset_type}")
 async def get_preset_fields(
     preset_type: Literal["filament", "print", "process", "printer"],

+ 970 - 0
backend/app/api/routes/inventory.py

@@ -0,0 +1,970 @@
+import json
+import logging
+
+import httpx
+from fastapi import APIRouter, Depends, HTTPException
+from fastapi.responses import StreamingResponse
+from pydantic import BaseModel
+from sqlalchemy import func, select
+from sqlalchemy.ext.asyncio import AsyncSession
+from sqlalchemy.orm import selectinload
+
+from backend.app.core.auth import RequirePermissionIfAuthEnabled
+from backend.app.core.catalog_defaults import DEFAULT_COLOR_CATALOG, DEFAULT_SPOOL_CATALOG
+from backend.app.core.database import get_db
+from backend.app.core.permissions import Permission
+from backend.app.models.color_catalog import ColorCatalogEntry
+from backend.app.models.spool import Spool
+from backend.app.models.spool_assignment import SpoolAssignment
+from backend.app.models.spool_catalog import SpoolCatalogEntry
+from backend.app.models.spool_k_profile import SpoolKProfile
+from backend.app.models.user import User
+from backend.app.schemas.spool import (
+    SpoolAssignmentCreate,
+    SpoolAssignmentResponse,
+    SpoolCreate,
+    SpoolKProfileBase,
+    SpoolKProfileResponse,
+    SpoolResponse,
+    SpoolUpdate,
+)
+from backend.app.schemas.spool_usage import SpoolUsageHistoryResponse
+
+logger = logging.getLogger(__name__)
+
+router = APIRouter(prefix="/inventory", tags=["inventory"])
+
+# Material temperature defaults (nozzle min/max)
+MATERIAL_TEMPS: dict[str, tuple[int, int]] = {
+    "PLA": (190, 230),
+    "PETG": (220, 260),
+    "ABS": (240, 270),
+    "ASA": (240, 270),
+    "TPU": (200, 240),
+    "PA": (260, 290),
+    "PC": (250, 280),
+    "PVA": (190, 210),
+    "PLA-CF": (210, 240),
+    "PETG-CF": (240, 270),
+    "PA-CF": (270, 300),
+}
+
+# FilamentColors.xyz API
+FILAMENT_COLORS_API = "https://filamentcolors.xyz/api"
+
+
+# ── Spool Catalog Schemas ──────────────────────────────────────────────────
+
+
+class CatalogEntryResponse(BaseModel):
+    id: int
+    name: str
+    weight: int
+    is_default: bool
+
+    class Config:
+        from_attributes = True
+
+
+class CatalogEntryCreate(BaseModel):
+    name: str
+    weight: int
+
+
+class CatalogEntryUpdate(BaseModel):
+    name: str
+    weight: int
+
+
+# ── Color Catalog Schemas ──────────────────────────────────────────────────
+
+
+class ColorEntryResponse(BaseModel):
+    id: int
+    manufacturer: str
+    color_name: str
+    hex_color: str
+    material: str | None
+    is_default: bool
+
+    class Config:
+        from_attributes = True
+
+
+class ColorEntryCreate(BaseModel):
+    manufacturer: str
+    color_name: str
+    hex_color: str
+    material: str | None = None
+
+
+class ColorEntryUpdate(BaseModel):
+    manufacturer: str
+    color_name: str
+    hex_color: str
+    material: str | None = None
+
+
+class ColorLookupResult(BaseModel):
+    found: bool
+    hex_color: str | None = None
+    material: str | None = None
+
+
+# ── Spool Catalog CRUD ─────────────────────────────────────────────────────
+
+
+@router.get("/catalog", response_model=list[CatalogEntryResponse])
+async def get_spool_catalog(
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
+):
+    """Get all spool catalog entries."""
+    result = await db.execute(select(SpoolCatalogEntry).order_by(SpoolCatalogEntry.name))
+    return list(result.scalars().all())
+
+
+@router.post("/catalog", response_model=CatalogEntryResponse)
+async def add_catalog_entry(
+    entry: CatalogEntryCreate,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Add a new spool catalog entry."""
+    row = SpoolCatalogEntry(name=entry.name, weight=entry.weight, is_default=False)
+    db.add(row)
+    await db.commit()
+    await db.refresh(row)
+    return row
+
+
+@router.put("/catalog/{entry_id}", response_model=CatalogEntryResponse)
+async def update_catalog_entry(
+    entry_id: int,
+    entry: CatalogEntryUpdate,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Update a spool catalog entry."""
+    result = await db.execute(select(SpoolCatalogEntry).where(SpoolCatalogEntry.id == entry_id))
+    row = result.scalar_one_or_none()
+    if not row:
+        raise HTTPException(404, "Entry not found")
+    row.name = entry.name
+    row.weight = entry.weight
+    await db.commit()
+    await db.refresh(row)
+    return row
+
+
+@router.delete("/catalog/{entry_id}")
+async def delete_catalog_entry(
+    entry_id: int,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Delete a spool catalog entry."""
+    result = await db.execute(select(SpoolCatalogEntry).where(SpoolCatalogEntry.id == entry_id))
+    row = result.scalar_one_or_none()
+    if not row:
+        raise HTTPException(404, "Entry not found")
+    await db.delete(row)
+    await db.commit()
+    return {"status": "deleted"}
+
+
+@router.post("/catalog/reset")
+async def reset_spool_catalog(
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Reset spool catalog to defaults."""
+    await db.execute(select(SpoolCatalogEntry))  # ensure table loaded
+    # Delete all
+    result = await db.execute(select(SpoolCatalogEntry))
+    for row in result.scalars().all():
+        await db.delete(row)
+    # Re-seed defaults
+    for name, weight in DEFAULT_SPOOL_CATALOG:
+        db.add(SpoolCatalogEntry(name=name, weight=weight, is_default=True))
+    await db.commit()
+    return {"status": "reset"}
+
+
+# ── Color Catalog CRUD ─────────────────────────────────────────────────────
+
+
+@router.get("/colors", response_model=list[ColorEntryResponse])
+async def get_color_catalog(
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
+):
+    """Get all color catalog entries."""
+    result = await db.execute(
+        select(ColorCatalogEntry).order_by(
+            ColorCatalogEntry.manufacturer, ColorCatalogEntry.material, ColorCatalogEntry.color_name
+        )
+    )
+    return list(result.scalars().all())
+
+
+@router.post("/colors", response_model=ColorEntryResponse)
+async def add_color_entry(
+    entry: ColorEntryCreate,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Add a new color catalog entry."""
+    row = ColorCatalogEntry(
+        manufacturer=entry.manufacturer,
+        color_name=entry.color_name,
+        hex_color=entry.hex_color,
+        material=entry.material,
+        is_default=False,
+    )
+    db.add(row)
+    await db.commit()
+    await db.refresh(row)
+    return row
+
+
+@router.put("/colors/{entry_id}", response_model=ColorEntryResponse)
+async def update_color_entry(
+    entry_id: int,
+    entry: ColorEntryUpdate,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Update a color catalog entry."""
+    result = await db.execute(select(ColorCatalogEntry).where(ColorCatalogEntry.id == entry_id))
+    row = result.scalar_one_or_none()
+    if not row:
+        raise HTTPException(404, "Entry not found")
+    row.manufacturer = entry.manufacturer
+    row.color_name = entry.color_name
+    row.hex_color = entry.hex_color
+    row.material = entry.material
+    await db.commit()
+    await db.refresh(row)
+    return row
+
+
+@router.delete("/colors/{entry_id}")
+async def delete_color_entry(
+    entry_id: int,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Delete a color catalog entry."""
+    result = await db.execute(select(ColorCatalogEntry).where(ColorCatalogEntry.id == entry_id))
+    row = result.scalar_one_or_none()
+    if not row:
+        raise HTTPException(404, "Entry not found")
+    await db.delete(row)
+    await db.commit()
+    return {"status": "deleted"}
+
+
+@router.post("/colors/reset")
+async def reset_color_catalog(
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Reset color catalog to defaults."""
+    result = await db.execute(select(ColorCatalogEntry))
+    for row in result.scalars().all():
+        await db.delete(row)
+    for manufacturer, color_name, hex_color, material in DEFAULT_COLOR_CATALOG:
+        db.add(
+            ColorCatalogEntry(
+                manufacturer=manufacturer,
+                color_name=color_name,
+                hex_color=hex_color,
+                material=material,
+                is_default=True,
+            )
+        )
+    await db.commit()
+    return {"status": "reset"}
+
+
+@router.get("/colors/lookup", response_model=ColorLookupResult)
+async def lookup_color(
+    manufacturer: str,
+    color_name: str,
+    material: str | None = None,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
+):
+    """Look up a color by manufacturer and color name."""
+    query = select(ColorCatalogEntry).where(
+        ColorCatalogEntry.manufacturer == manufacturer,
+        ColorCatalogEntry.color_name == color_name,
+    )
+    if material:
+        query = query.where(ColorCatalogEntry.material == material)
+    query = query.limit(1)
+    result = await db.execute(query)
+    row = result.scalar_one_or_none()
+    if row:
+        return ColorLookupResult(found=True, hex_color=row.hex_color, material=row.material)
+    return ColorLookupResult(found=False)
+
+
+@router.get("/colors/search", response_model=list[ColorEntryResponse])
+async def search_colors(
+    manufacturer: str | None = None,
+    material: str | None = None,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
+):
+    """Search colors by manufacturer and/or material."""
+    query = select(ColorCatalogEntry)
+    if manufacturer:
+        query = query.where(func.lower(ColorCatalogEntry.manufacturer).contains(manufacturer.lower()))
+    if material:
+        query = query.where(func.lower(ColorCatalogEntry.material).contains(material.lower()))
+    query = query.order_by(ColorCatalogEntry.manufacturer, ColorCatalogEntry.color_name).limit(100)
+    result = await db.execute(query)
+    return list(result.scalars().all())
+
+
+@router.post("/colors/sync")
+async def sync_from_filamentcolors(
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Sync colors from FilamentColors.xyz API with progress streaming."""
+
+    async def generate():
+        from backend.app.core.database import async_session
+
+        added = 0
+        skipped = 0
+        total_fetched = 0
+        total_available = 0
+
+        try:
+            async with httpx.AsyncClient(timeout=120.0) as client:
+                page = 1
+                while True:
+                    response = await client.get(
+                        f"{FILAMENT_COLORS_API}/swatch/",
+                        params={"page": page},
+                    )
+                    response.raise_for_status()
+                    data = response.json()
+                    total_available = data.get("count", total_available)
+                    results = data.get("results", [])
+                    if not results:
+                        break
+
+                    async with async_session() as db:
+                        for swatch in results:
+                            total_fetched += 1
+                            manufacturer_data = swatch.get("manufacturer")
+                            manufacturer_name = (
+                                manufacturer_data.get("name", "") if isinstance(manufacturer_data, dict) else ""
+                            )
+                            filament_type_data = swatch.get("filament_type")
+                            mat = filament_type_data.get("name", "") if isinstance(filament_type_data, dict) else None
+                            color_name_val = swatch.get("color_name", "")
+                            hex_color_val = swatch.get("hex_color", "")
+
+                            if not manufacturer_name or not color_name_val or not hex_color_val:
+                                skipped += 1
+                                continue
+
+                            if not hex_color_val.startswith("#"):
+                                hex_color_val = f"#{hex_color_val}"
+
+                            # Check if entry already exists
+                            existing = await db.execute(
+                                select(ColorCatalogEntry)
+                                .where(
+                                    ColorCatalogEntry.manufacturer == manufacturer_name,
+                                    ColorCatalogEntry.color_name == color_name_val,
+                                    ColorCatalogEntry.material == mat,
+                                )
+                                .limit(1)
+                            )
+                            if existing.scalar_one_or_none():
+                                skipped += 1
+                            else:
+                                db.add(
+                                    ColorCatalogEntry(
+                                        manufacturer=manufacturer_name,
+                                        color_name=color_name_val,
+                                        hex_color=hex_color_val.upper(),
+                                        material=mat,
+                                        is_default=False,
+                                    )
+                                )
+                                added += 1
+
+                        await db.commit()
+
+                    progress = {
+                        "type": "progress",
+                        "added": added,
+                        "skipped": skipped,
+                        "total_fetched": total_fetched,
+                        "total_available": total_available,
+                    }
+                    yield f"data: {json.dumps(progress)}\n\n"
+
+                    if not data.get("next") or total_fetched >= total_available:
+                        break
+                    page += 1
+
+            result = {
+                "type": "complete",
+                "added": added,
+                "skipped": skipped,
+                "total_fetched": total_fetched,
+                "total_available": total_available,
+            }
+            yield f"data: {json.dumps(result)}\n\n"
+
+        except httpx.HTTPError as e:
+            logger.error("HTTP error syncing from FilamentColors.xyz: %s", e)
+            yield f"data: {json.dumps({'type': 'error', 'error': str(e)})}\n\n"
+        except Exception as e:
+            logger.error("Error syncing from FilamentColors.xyz: %s", e)
+            yield f"data: {json.dumps({'type': 'error', 'error': 'Unexpected error during sync'})}\n\n"
+
+    return StreamingResponse(generate(), media_type="text/event-stream")
+
+
+# ── Spool CRUD ───────────────────────────────────────────────────────────────
+
+
+@router.get("/spools", response_model=list[SpoolResponse])
+async def list_spools(
+    include_archived: bool = False,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
+):
+    """List all spools, excluding archived by default."""
+    query = select(Spool).options(selectinload(Spool.k_profiles))
+    if not include_archived:
+        query = query.where(Spool.archived_at.is_(None))
+    query = query.order_by(Spool.material, Spool.brand, Spool.color_name)
+    result = await db.execute(query)
+    return list(result.scalars().all())
+
+
+@router.get("/spools/{spool_id}", response_model=SpoolResponse)
+async def get_spool(
+    spool_id: int,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
+):
+    """Get a single spool with k_profiles."""
+    result = await db.execute(select(Spool).options(selectinload(Spool.k_profiles)).where(Spool.id == spool_id))
+    spool = result.scalar_one_or_none()
+    if not spool:
+        raise HTTPException(404, "Spool not found")
+    return spool
+
+
+@router.post("/spools", response_model=SpoolResponse)
+async def create_spool(
+    spool_data: SpoolCreate,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Create a new spool."""
+    spool = Spool(**spool_data.model_dump())
+    db.add(spool)
+    await db.commit()
+    await db.refresh(spool)
+    result = await db.execute(select(Spool).options(selectinload(Spool.k_profiles)).where(Spool.id == spool.id))
+    return result.scalar_one()
+
+
+@router.patch("/spools/{spool_id}", response_model=SpoolResponse)
+async def update_spool(
+    spool_id: int,
+    spool_data: SpoolUpdate,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Update a spool."""
+    result = await db.execute(select(Spool).where(Spool.id == spool_id))
+    spool = result.scalar_one_or_none()
+    if not spool:
+        raise HTTPException(404, "Spool not found")
+
+    for field, value in spool_data.model_dump(exclude_unset=True).items():
+        setattr(spool, field, value)
+
+    await db.commit()
+    result = await db.execute(select(Spool).options(selectinload(Spool.k_profiles)).where(Spool.id == spool_id))
+    return result.scalar_one()
+
+
+@router.delete("/spools/{spool_id}")
+async def delete_spool(
+    spool_id: int,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Hard delete a spool."""
+    result = await db.execute(select(Spool).where(Spool.id == spool_id))
+    spool = result.scalar_one_or_none()
+    if not spool:
+        raise HTTPException(404, "Spool not found")
+
+    await db.delete(spool)
+    await db.commit()
+    return {"status": "deleted"}
+
+
+@router.post("/spools/{spool_id}/archive", response_model=SpoolResponse)
+async def archive_spool(
+    spool_id: int,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Soft-delete a spool by setting archived_at."""
+    from datetime import datetime, timezone
+
+    result = await db.execute(select(Spool).where(Spool.id == spool_id))
+    spool = result.scalar_one_or_none()
+    if not spool:
+        raise HTTPException(404, "Spool not found")
+
+    spool.archived_at = datetime.now(timezone.utc)
+    await db.commit()
+    result = await db.execute(select(Spool).options(selectinload(Spool.k_profiles)).where(Spool.id == spool_id))
+    return result.scalar_one()
+
+
+@router.post("/spools/{spool_id}/restore", response_model=SpoolResponse)
+async def restore_spool(
+    spool_id: int,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Restore an archived spool."""
+    result = await db.execute(select(Spool).where(Spool.id == spool_id))
+    spool = result.scalar_one_or_none()
+    if not spool:
+        raise HTTPException(404, "Spool not found")
+
+    spool.archived_at = None
+    await db.commit()
+    result = await db.execute(select(Spool).options(selectinload(Spool.k_profiles)).where(Spool.id == spool_id))
+    return result.scalar_one()
+
+
+# ── K-Profiles ───────────────────────────────────────────────────────────────
+
+
+@router.get("/spools/{spool_id}/k-profiles", response_model=list[SpoolKProfileResponse])
+async def list_k_profiles(
+    spool_id: int,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
+):
+    """List K-profiles for a spool."""
+    result = await db.execute(select(SpoolKProfile).where(SpoolKProfile.spool_id == spool_id))
+    return list(result.scalars().all())
+
+
+@router.put("/spools/{spool_id}/k-profiles", response_model=list[SpoolKProfileResponse])
+async def replace_k_profiles(
+    spool_id: int,
+    profiles: list[SpoolKProfileBase],
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Replace all K-profiles for a spool (batch save)."""
+    # Verify spool exists
+    result = await db.execute(select(Spool).where(Spool.id == spool_id))
+    if not result.scalar_one_or_none():
+        raise HTTPException(404, "Spool not found")
+
+    # Delete existing
+    existing = await db.execute(select(SpoolKProfile).where(SpoolKProfile.spool_id == spool_id))
+    for old in existing.scalars().all():
+        await db.delete(old)
+
+    # Create new
+    new_profiles = []
+    for p in profiles:
+        kp = SpoolKProfile(spool_id=spool_id, **p.model_dump())
+        db.add(kp)
+        new_profiles.append(kp)
+
+    await db.commit()
+    for kp in new_profiles:
+        await db.refresh(kp)
+    return new_profiles
+
+
+# ── Spool Assignments ────────────────────────────────────────────────────────
+
+
+@router.get("/assignments", response_model=list[SpoolAssignmentResponse])
+async def list_assignments(
+    printer_id: int | None = None,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
+):
+    """List spool assignments, optionally filtered by printer."""
+    query = select(SpoolAssignment).options(
+        selectinload(SpoolAssignment.spool).selectinload(Spool.k_profiles),
+        selectinload(SpoolAssignment.printer),
+    )
+    if printer_id is not None:
+        query = query.where(SpoolAssignment.printer_id == printer_id)
+    result = await db.execute(query)
+    return list(result.scalars().all())
+
+
+@router.post("/assignments", response_model=SpoolAssignmentResponse)
+async def assign_spool(
+    data: SpoolAssignmentCreate,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Assign a spool to an AMS slot and auto-configure via MQTT."""
+    from backend.app.services.printer_manager import printer_manager
+
+    # 1. Validate spool exists and is not archived
+    result = await db.execute(select(Spool).options(selectinload(Spool.k_profiles)).where(Spool.id == data.spool_id))
+    spool = result.scalar_one_or_none()
+    if not spool:
+        raise HTTPException(404, "Spool not found")
+    if spool.archived_at:
+        raise HTTPException(400, "Cannot assign an archived spool")
+
+    # 2. Get current AMS tray state for fingerprint
+    fingerprint_color = None
+    fingerprint_type = None
+    state = printer_manager.get_status(data.printer_id)
+    if state and state.raw_data:
+        if data.ams_id == 255:
+            # External slot: look up tray from vt_tray by global ID
+            vt_tray = state.raw_data.get("vt_tray") or []
+            ext_id = data.tray_id + 254  # 0→254, 1→255
+            for vt in vt_tray:
+                if isinstance(vt, dict) and int(vt.get("id", 254)) == ext_id:
+                    fingerprint_color = vt.get("tray_color", "")
+                    fingerprint_type = vt.get("tray_type", "")
+                    break
+        else:
+            ams_data = state.raw_data.get("ams", {})
+            ams_list = (
+                ams_data.get("ams", [])
+                if isinstance(ams_data, dict)
+                else ams_data
+                if isinstance(ams_data, list)
+                else []
+            )
+            tray = _find_tray_in_ams_data(
+                ams_list,
+                data.ams_id,
+                data.tray_id,
+            )
+            if tray:
+                fingerprint_color = tray.get("tray_color", "")
+                fingerprint_type = tray.get("tray_type", "")
+
+    # 3. Upsert assignment (replace if same printer+ams+tray)
+    existing = await db.execute(
+        select(SpoolAssignment).where(
+            SpoolAssignment.printer_id == data.printer_id,
+            SpoolAssignment.ams_id == data.ams_id,
+            SpoolAssignment.tray_id == data.tray_id,
+        )
+    )
+    old = existing.scalar_one_or_none()
+    if old:
+        await db.delete(old)
+        await db.flush()
+
+    assignment = SpoolAssignment(
+        spool_id=data.spool_id,
+        printer_id=data.printer_id,
+        ams_id=data.ams_id,
+        tray_id=data.tray_id,
+        fingerprint_color=fingerprint_color,
+        fingerprint_type=fingerprint_type,
+    )
+    db.add(assignment)
+    await db.commit()
+    await db.refresh(assignment)
+
+    # 4. Auto-configure AMS slot via MQTT
+    configured = False
+    try:
+        client = printer_manager.get_client(data.printer_id)
+        if client:
+            # Build filament setting from spool data
+            tray_type = spool.material
+            tray_sub_brands = f"{spool.material} {spool.subtype}" if spool.subtype else spool.material
+            tray_color = spool.rgba or "FFFFFFFF"
+            tray_info_idx = spool.slicer_filament or ""
+            setting_id = ""
+
+            # Temperature: use spool overrides if set, else material defaults
+            temp_min, temp_max = MATERIAL_TEMPS.get(spool.material.upper(), (200, 240))
+            if spool.nozzle_temp_min is not None:
+                temp_min = spool.nozzle_temp_min
+            if spool.nozzle_temp_max is not None:
+                temp_max = spool.nozzle_temp_max
+
+            # a. Set filament setting
+            client.ams_set_filament_setting(
+                ams_id=data.ams_id,
+                tray_id=data.tray_id,
+                tray_info_idx=tray_info_idx,
+                tray_type=tray_type,
+                tray_sub_brands=tray_sub_brands,
+                tray_color=tray_color,
+                nozzle_temp_min=temp_min,
+                nozzle_temp_max=temp_max,
+                setting_id=setting_id,
+            )
+
+            # b. Look up K-profile for this spool + printer + nozzle + extruder
+            nozzle_diameter = "0.4"
+            if state and state.nozzles:
+                nd = state.nozzles[0].nozzle_diameter
+                if nd:
+                    nozzle_diameter = nd
+
+            # Determine slot's extruder from ams_extruder_map
+            slot_extruder = None
+            if state and state.ams_extruder_map:
+                if data.ams_id == 255:
+                    # External slots: ext-L (tray 0) → extruder 1, ext-R (tray 1) → extruder 0
+                    slot_extruder = 1 - data.tray_id  # 0→1, 1→0
+                else:
+                    slot_extruder = state.ams_extruder_map.get(str(data.ams_id))
+
+            matching_kp = None
+            for kp in spool.k_profiles:
+                if kp.printer_id == data.printer_id and kp.nozzle_diameter == nozzle_diameter:
+                    if slot_extruder is not None and kp.extruder_id is not None and kp.extruder_id != slot_extruder:
+                        continue
+                    matching_kp = kp
+                    break
+
+            if matching_kp and matching_kp.cali_idx is not None:
+                client.extrusion_cali_sel(
+                    ams_id=data.ams_id,
+                    tray_id=data.tray_id,
+                    cali_idx=matching_kp.cali_idx,
+                    filament_id=tray_info_idx,
+                    nozzle_diameter=nozzle_diameter,
+                )
+
+            configured = True
+            logger.info(
+                "Auto-configured AMS slot ams=%d tray=%d for spool %d on printer %d",
+                data.ams_id,
+                data.tray_id,
+                spool.id,
+                data.printer_id,
+            )
+    except Exception as e:
+        logger.warning("MQTT auto-configure failed for spool %d: %s", spool.id, e)
+
+    # Return assignment with spool data
+    result = await db.execute(
+        select(SpoolAssignment)
+        .options(
+            selectinload(SpoolAssignment.spool).selectinload(Spool.k_profiles),
+            selectinload(SpoolAssignment.printer),
+        )
+        .where(SpoolAssignment.id == assignment.id)
+    )
+    resp = result.scalar_one()
+    response = SpoolAssignmentResponse.model_validate(resp)
+    response.configured = configured
+    return response
+
+
+@router.delete("/assignments/{printer_id}/{ams_id}/{tray_id}")
+async def unassign_spool(
+    printer_id: int,
+    ams_id: int,
+    tray_id: int,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Unassign a spool from an AMS slot."""
+    result = await db.execute(
+        select(SpoolAssignment).where(
+            SpoolAssignment.printer_id == printer_id,
+            SpoolAssignment.ams_id == ams_id,
+            SpoolAssignment.tray_id == tray_id,
+        )
+    )
+    assignment = result.scalar_one_or_none()
+    if not assignment:
+        raise HTTPException(404, "Assignment not found")
+
+    await db.delete(assignment)
+    await db.commit()
+    return {"status": "deleted"}
+
+
+# ── Tag Linking ───────────────────────────────────────────────────────────────
+
+
+class LinkTagRequest(BaseModel):
+    tag_uid: str | None = None
+    tray_uuid: str | None = None
+    tag_type: str | None = None
+    data_origin: str | None = "nfc_link"
+
+
+@router.patch("/spools/{spool_id}/link-tag", response_model=SpoolResponse)
+async def link_tag_to_spool(
+    spool_id: int,
+    data: LinkTagRequest,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Link an RFID tag_uid/tray_uuid to an existing spool."""
+    result = await db.execute(select(Spool).options(selectinload(Spool.k_profiles)).where(Spool.id == spool_id))
+    spool = result.scalar_one_or_none()
+    if not spool:
+        raise HTTPException(404, "Spool not found")
+    if spool.archived_at:
+        raise HTTPException(400, "Cannot link tag to archived spool")
+
+    # Check for conflicts: tag already linked to another active spool
+    if data.tag_uid:
+        conflict = await db.execute(
+            select(Spool).where(
+                Spool.tag_uid == data.tag_uid,
+                Spool.id != spool_id,
+                Spool.archived_at.is_(None),
+            )
+        )
+        if conflict.scalar_one_or_none():
+            raise HTTPException(409, "Tag UID already linked to another active spool")
+        # Auto-clear from archived spools (tag recycling)
+        archived_with_tag = await db.execute(
+            select(Spool).where(
+                Spool.tag_uid == data.tag_uid,
+                Spool.id != spool_id,
+                Spool.archived_at.is_not(None),
+            )
+        )
+        for old_spool in archived_with_tag.scalars().all():
+            old_spool.tag_uid = None
+
+    if data.tray_uuid:
+        conflict = await db.execute(
+            select(Spool).where(
+                Spool.tray_uuid == data.tray_uuid,
+                Spool.id != spool_id,
+                Spool.archived_at.is_(None),
+            )
+        )
+        if conflict.scalar_one_or_none():
+            raise HTTPException(409, "Tray UUID already linked to another active spool")
+        archived_with_uuid = await db.execute(
+            select(Spool).where(
+                Spool.tray_uuid == data.tray_uuid,
+                Spool.id != spool_id,
+                Spool.archived_at.is_not(None),
+            )
+        )
+        for old_spool in archived_with_uuid.scalars().all():
+            old_spool.tray_uuid = None
+
+    if data.tag_uid is not None:
+        spool.tag_uid = data.tag_uid
+    if data.tray_uuid is not None:
+        spool.tray_uuid = data.tray_uuid
+    if data.tag_type is not None:
+        spool.tag_type = data.tag_type
+    if data.data_origin is not None:
+        spool.data_origin = data.data_origin
+
+    await db.commit()
+    result = await db.execute(select(Spool).options(selectinload(Spool.k_profiles)).where(Spool.id == spool_id))
+    return result.scalar_one()
+
+
+# ── Usage History ─────────────────────────────────────────────────────────────
+
+
+@router.get("/spools/{spool_id}/usage", response_model=list[SpoolUsageHistoryResponse])
+async def get_spool_usage_history(
+    spool_id: int,
+    limit: int = 50,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
+):
+    """Get usage history for a specific spool."""
+    from backend.app.models.spool_usage_history import SpoolUsageHistory
+
+    # Verify spool exists
+    spool_result = await db.execute(select(Spool).where(Spool.id == spool_id))
+    if not spool_result.scalar_one_or_none():
+        raise HTTPException(404, "Spool not found")
+
+    result = await db.execute(
+        select(SpoolUsageHistory)
+        .where(SpoolUsageHistory.spool_id == spool_id)
+        .order_by(SpoolUsageHistory.created_at.desc())
+        .limit(limit)
+    )
+    return list(result.scalars().all())
+
+
+@router.get("/usage", response_model=list[SpoolUsageHistoryResponse])
+async def get_all_usage_history(
+    limit: int = 100,
+    printer_id: int | None = None,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
+):
+    """Get global usage history, optionally filtered by printer."""
+    from backend.app.models.spool_usage_history import SpoolUsageHistory
+
+    query = select(SpoolUsageHistory).order_by(SpoolUsageHistory.created_at.desc()).limit(limit)
+    if printer_id is not None:
+        query = query.where(SpoolUsageHistory.printer_id == printer_id)
+    result = await db.execute(query)
+    return list(result.scalars().all())
+
+
+@router.delete("/spools/{spool_id}/usage")
+async def clear_spool_usage_history(
+    spool_id: int,
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
+):
+    """Clear usage history for a spool."""
+    from backend.app.models.spool_usage_history import SpoolUsageHistory
+
+    result = await db.execute(select(SpoolUsageHistory).where(SpoolUsageHistory.spool_id == spool_id))
+    for row in result.scalars().all():
+        await db.delete(row)
+    await db.commit()
+    return {"status": "cleared"}
+
+
+# ── Helpers ──────────────────────────────────────────────────────────────────
+
+
+def _find_tray_in_ams_data(ams_data: list, ams_id: int, tray_id: int) -> dict | None:
+    """Find a specific tray in the AMS data structure."""
+    if not ams_data:
+        return None
+    for ams_unit in ams_data:
+        if int(ams_unit.get("id", -1)) != ams_id:
+            continue
+        for tray in ams_unit.get("tray", []):
+            if int(tray.get("id", -1)) == tray_id:
+                return tray
+    return None

+ 3 - 0
backend/app/api/routes/kprofiles.py

@@ -278,6 +278,9 @@ async def delete_kprofile(
     if not success:
         raise HTTPException(500, "Failed to send K-profile delete command")
 
+    # Wait for printer to process the delete before frontend refetches
+    await asyncio.sleep(0.5)
+
     return {"success": True, "message": "K-profile deleted successfully"}
 
 

+ 7 - 0
backend/app/api/routes/library.py

@@ -56,6 +56,7 @@ from backend.app.schemas.library import (
 )
 from backend.app.services.archive import ArchiveService, ThreeMFParser
 from backend.app.services.stl_thumbnail import generate_stl_thumbnail
+from backend.app.utils.threemf_tools import extract_nozzle_mapping_from_3mf
 
 logger = logging.getLogger(__name__)
 
@@ -1711,6 +1712,12 @@ async def get_library_file_filament_requirements(
             # Sort by slot ID
             filaments.sort(key=lambda x: x["slot_id"])
 
+            # Enrich with nozzle mapping for dual-nozzle printers
+            nozzle_mapping = extract_nozzle_mapping_from_3mf(zf)
+            if nozzle_mapping:
+                for filament in filaments:
+                    filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
+
     except Exception as e:
         logger.warning("Failed to parse filament requirements from library file %s: %s", file_id, e)
 

+ 98 - 15
backend/app/api/routes/maintenance.py

@@ -26,6 +26,7 @@ from backend.app.schemas.maintenance import (
     PrinterMaintenanceUpdate,
 )
 from backend.app.services.notification_service import notification_service
+from backend.app.utils.printer_models import get_rod_type
 
 logger = logging.getLogger(__name__)
 
@@ -33,12 +34,33 @@ router = APIRouter(prefix="/maintenance", tags=["maintenance"])
 
 # Default maintenance types
 DEFAULT_MAINTENANCE_TYPES = [
+    # Carbon rod models only (X1/P1/P2S)
+    {
+        "name": "Lubricate Carbon Rods",
+        "description": "Apply lubricant to carbon rods for smooth motion",
+        "default_interval_hours": 50.0,
+        "icon": "Droplet",
+    },
+    {
+        "name": "Clean Carbon Rods",
+        "description": "Wipe carbon rods with a dry cloth",
+        "default_interval_hours": 100.0,
+        "icon": "Sparkles",
+    },
+    # Linear rail models only (A1/H2)
     {
         "name": "Lubricate Linear Rails",
-        "description": "Apply lubricant to linear rails and rods for smooth motion",
+        "description": "Apply lubricant to linear rails for smooth motion",
         "default_interval_hours": 50.0,
         "icon": "Droplet",
     },
+    {
+        "name": "Clean Linear Rails",
+        "description": "Wipe linear rails with a dry cloth to remove dust and debris",
+        "default_interval_hours": 100.0,
+        "icon": "Sparkles",
+    },
+    # Universal (all models)
     {
         "name": "Clean Nozzle/Hotend",
         "description": "Clean nozzle exterior and perform cold pull if needed",
@@ -51,12 +73,6 @@ DEFAULT_MAINTENANCE_TYPES = [
         "default_interval_hours": 200.0,
         "icon": "Ruler",
     },
-    {
-        "name": "Clean Carbon Rods",
-        "description": "Wipe carbon rods with a dry cloth",
-        "default_interval_hours": 100.0,
-        "icon": "Sparkles",
-    },
     {
         "name": "Clean Build Plate",
         "description": "Deep clean build plate with IPA or soap",
@@ -71,6 +87,30 @@ DEFAULT_MAINTENANCE_TYPES = [
     },
 ]
 
+# System types that only apply to printers with a specific rod/rail type.
+# "carbon" = X1/P1/P2S series (carbon rods), "linear_rail" = A1/H2 series.
+# Types not listed here apply to all printers.
+_ROD_TYPE_REQUIREMENTS: dict[str, str] = {
+    "Lubricate Carbon Rods": "carbon",
+    "Clean Carbon Rods": "carbon",
+    "Lubricate Linear Rails": "linear_rail",
+    "Clean Linear Rails": "linear_rail",
+}
+
+
+def _should_apply_to_printer(type_name: str, printer_model: str | None) -> bool:
+    """Check if a system maintenance type should apply to a given printer model."""
+    rod_requirement = _ROD_TYPE_REQUIREMENTS.get(type_name)
+    if rod_requirement is None:
+        return True  # Not model-specific, applies to all
+
+    rod_type = get_rod_type(printer_model)
+    if rod_type is None:
+        # Unknown model — default to carbon rods (legacy behavior)
+        return rod_requirement == "carbon"
+
+    return rod_type == rod_requirement
+
 
 async def get_printer_total_hours(db: AsyncSession, printer_id: int) -> float:
     """Calculate total active hours for a printer from runtime counter plus offset.
@@ -94,13 +134,27 @@ async def get_printer_total_hours(db: AsyncSession, printer_id: int) -> float:
 
 
 async def ensure_default_types(db: AsyncSession) -> None:
-    """Ensure default maintenance types exist."""
-    result = await db.execute(select(MaintenanceType).where(MaintenanceType.is_system.is_(True)))
+    """Ensure default maintenance types exist, remove stale/duplicate ones."""
+    result = await db.execute(
+        select(MaintenanceType).where(MaintenanceType.is_system.is_(True)).order_by(MaintenanceType.id)
+    )
     existing = result.scalars().all()
-    existing_names = {t.name for t in existing}
 
+    default_names = {t["name"] for t in DEFAULT_MAINTENANCE_TYPES}
+
+    # Remove stale system types no longer in defaults (e.g. renamed types)
+    # and deduplicate: if concurrent requests created the same type twice,
+    # keep only the first (lowest id) and delete the rest.
+    seen_names: set[str] = set()
+    for t in existing:
+        if t.name not in default_names or t.name in seen_names:
+            await db.delete(t)
+        else:
+            seen_names.add(t.name)
+
+    # Create any missing default types
     for type_def in DEFAULT_MAINTENANCE_TYPES:
-        if type_def["name"] not in existing_names:
+        if type_def["name"] not in seen_names:
             new_type = MaintenanceType(
                 name=type_def["name"],
                 description=type_def["description"],
@@ -123,7 +177,11 @@ async def get_maintenance_types(
 ):
     """Get all maintenance types."""
     await ensure_default_types(db)
-    result = await db.execute(select(MaintenanceType).order_by(MaintenanceType.is_system.desc(), MaintenanceType.name))
+    result = await db.execute(
+        select(MaintenanceType)
+        .where(MaintenanceType.is_deleted.is_(False))
+        .order_by(MaintenanceType.is_system.desc(), MaintenanceType.name)
+    )
     return result.scalars().all()
 
 
@@ -176,20 +234,40 @@ async def delete_maintenance_type(
     db: AsyncSession = Depends(get_db),
     _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_DELETE),
 ):
-    """Delete a custom maintenance type."""
+    """Delete a maintenance type."""
     result = await db.execute(select(MaintenanceType).where(MaintenanceType.id == type_id))
     maint_type = result.scalar_one_or_none()
     if not maint_type:
         raise HTTPException(status_code=404, detail="Maintenance type not found")
 
     if maint_type.is_system:
-        raise HTTPException(status_code=400, detail="Cannot delete system maintenance type")
+        maint_type.is_deleted = True
+        await db.commit()
+        return {"status": "deleted"}
 
     await db.delete(maint_type)
     await db.commit()
     return {"status": "deleted"}
 
 
+@router.post("/types/restore-defaults")
+async def restore_default_maintenance_types(
+    db: AsyncSession = Depends(get_db),
+    _: User | None = RequirePermissionIfAuthEnabled(Permission.MAINTENANCE_DELETE),
+):
+    """Restore deleted default maintenance types."""
+    await ensure_default_types(db)
+    result = await db.execute(
+        select(MaintenanceType).where(MaintenanceType.is_system.is_(True)).where(MaintenanceType.is_deleted.is_(True))
+    )
+    deleted_types = result.scalars().all()
+    for maint_type in deleted_types:
+        maint_type.is_deleted = False
+
+    await db.commit()
+    return {"restored": len(deleted_types)}
+
+
 # ============== Printer Maintenance ==============
 
 
@@ -210,7 +288,7 @@ async def _get_printer_maintenance_internal(
     total_hours = await get_printer_total_hours(db, printer_id)
 
     # Get all maintenance types
-    result = await db.execute(select(MaintenanceType))
+    result = await db.execute(select(MaintenanceType).where(MaintenanceType.is_deleted.is_(False)))
     all_types = result.scalars().all()
 
     # Get printer's maintenance items
@@ -228,6 +306,11 @@ async def _get_printer_maintenance_internal(
     now = datetime.utcnow()
 
     for maint_type in all_types:
+        # Skip system types that don't apply to this printer model
+        # (e.g., "Clean Carbon Rods" for H2D which has steel rods)
+        if maint_type.is_system and not _should_apply_to_printer(maint_type.name, printer.model):
+            continue
+
         item = existing_items.get(maint_type.id)
         default_interval_type = getattr(maint_type, "interval_type", "hours") or "hours"
 

+ 247 - 56
backend/app/api/routes/printers.py

@@ -237,7 +237,7 @@ async def get_printer_status(
 
     # Parse AMS data from raw_data
     ams_units = []
-    vt_tray = None
+    vt_tray = []
     ams_exists = False
     raw_data = state.raw_data or {}
 
@@ -319,38 +319,41 @@ async def get_printer_status(
                 )
             )
 
-    # Virtual tray (external spool holder) - comes from vt_tray in raw_data
+    # Virtual tray (external spool holder) - comes from vt_tray in raw_data (list)
     if "vt_tray" in raw_data:
-        vt_data = raw_data["vt_tray"]
-        # Filter out empty/invalid tag values for vt_tray
-        vt_tag_uid = vt_data.get("tag_uid", "")
-        if vt_tag_uid in ("", "0000000000000000"):
-            vt_tag_uid = None
-        vt_tray_uuid = vt_data.get("tray_uuid", "")
-        if vt_tray_uuid in ("", "00000000000000000000000000000000"):
-            vt_tray_uuid = None
-
-        # Get K value: first try tray's k field, then lookup from K-profiles
-        vt_k_value = vt_data.get("k")
-        vt_cali_idx = vt_data.get("cali_idx")
-        if vt_k_value is None and vt_cali_idx is not None and vt_cali_idx in kprofile_map:
-            vt_k_value = kprofile_map[vt_cali_idx]
-
-        vt_tray = AMSTray(
-            id=254,  # Virtual tray ID
-            tray_color=vt_data.get("tray_color"),
-            tray_type=vt_data.get("tray_type"),
-            tray_sub_brands=vt_data.get("tray_sub_brands"),
-            tray_id_name=vt_data.get("tray_id_name"),
-            tray_info_idx=vt_data.get("tray_info_idx"),
-            remain=vt_data.get("remain", 0),
-            k=vt_k_value,
-            cali_idx=vt_cali_idx,
-            tag_uid=vt_tag_uid,
-            tray_uuid=vt_tray_uuid,
-            nozzle_temp_min=vt_data.get("nozzle_temp_min"),
-            nozzle_temp_max=vt_data.get("nozzle_temp_max"),
-        )
+        for vt_data in raw_data["vt_tray"]:
+            # Filter out empty/invalid tag values for vt_tray
+            vt_tag_uid = vt_data.get("tag_uid", "")
+            if vt_tag_uid in ("", "0000000000000000"):
+                vt_tag_uid = None
+            vt_tray_uuid = vt_data.get("tray_uuid", "")
+            if vt_tray_uuid in ("", "00000000000000000000000000000000"):
+                vt_tray_uuid = None
+
+            # Get K value: first try tray's k field, then lookup from K-profiles
+            vt_k_value = vt_data.get("k")
+            vt_cali_idx = vt_data.get("cali_idx")
+            if vt_k_value is None and vt_cali_idx is not None and vt_cali_idx in kprofile_map:
+                vt_k_value = kprofile_map[vt_cali_idx]
+
+            tray_id = int(vt_data.get("id", 254))
+            vt_tray.append(
+                AMSTray(
+                    id=tray_id,
+                    tray_color=vt_data.get("tray_color"),
+                    tray_type=vt_data.get("tray_type"),
+                    tray_sub_brands=vt_data.get("tray_sub_brands"),
+                    tray_id_name=vt_data.get("tray_id_name"),
+                    tray_info_idx=vt_data.get("tray_info_idx"),
+                    remain=vt_data.get("remain", 0),
+                    k=vt_k_value,
+                    cali_idx=vt_cali_idx,
+                    tag_uid=vt_tag_uid,
+                    tray_uuid=vt_tray_uuid,
+                    nozzle_temp_min=vt_data.get("nozzle_temp_min"),
+                    nozzle_temp_max=vt_data.get("nozzle_temp_max"),
+                )
+            )
 
     # Convert nozzle info to response format
     nozzles = [
@@ -1637,40 +1640,72 @@ async def configure_ams_slot(
     if not client:
         raise HTTPException(status_code=400, detail="Printer not connected")
 
-    # Send the filament setting command (type, color, temp)
-    success = client.ams_set_filament_setting(
-        ams_id=ams_id,
-        tray_id=tray_id,
-        tray_info_idx=tray_info_idx,
-        tray_type=tray_type,
-        tray_sub_brands=tray_sub_brands,
-        tray_color=tray_color,
-        nozzle_temp_min=nozzle_temp_min,
-        nozzle_temp_max=nozzle_temp_max,
-        setting_id=setting_id,
-    )
+    # Detect RFID spool before sending commands
+    is_rfid_spool = False
+    state = printer_manager.get_status(printer_id)
+    if state and state.raw_data:
+        from backend.app.api.routes.inventory import _find_tray_in_ams_data
+        from backend.app.services.spool_tag_matcher import is_valid_tag
 
-    if not success:
-        raise HTTPException(status_code=500, detail="Failed to send filament configuration command")
+        ams_data = state.raw_data.get("ams", {})
+        ams_list = (
+            ams_data.get("ams", []) if isinstance(ams_data, dict) else ams_data if isinstance(ams_data, list) else []
+        )
+        current_tray = _find_tray_in_ams_data(ams_list, ams_id, tray_id)
+        if current_tray:
+            is_rfid_spool = is_valid_tag(
+                current_tray.get("tag_uid", ""),
+                current_tray.get("tray_uuid", ""),
+            )
 
-    # Send the calibration/K-profile commands
-    # Use the K profile's filament_id if provided, otherwise use tray_info_idx
+    # Send filament setting + K-profile commands
     filament_id_for_kprofile = kprofile_filament_id if kprofile_filament_id else tray_info_idx
 
+    if is_rfid_spool:
+        # RFID spool: skip ams_set_filament_setting to preserve RFID state (eye icon).
+        # The firmware already has filament config from the RFID tag.
+        logger.info("[configure_ams_slot] RFID spool detected — skipping ams_set_filament_setting")
+    else:
+        # Non-RFID spool: send filament setting (type, color, temp)
+        # When a K-profile is selected, use the K-profile's filament_id as
+        # tray_info_idx so BambuStudio queries the right PA history table.
+        # But always use the PRESET's setting_id (not the K-profile's) —
+        # BambuStudio uses setting_id to identify the filament preset and
+        # overriding it with the K-profile's setting_id confuses the slicer.
+        effective_tray_info_idx = filament_id_for_kprofile if cali_idx >= 0 else tray_info_idx
+        success = client.ams_set_filament_setting(
+            ams_id=ams_id,
+            tray_id=tray_id,
+            tray_info_idx=effective_tray_info_idx,
+            tray_type=tray_type,
+            tray_sub_brands=tray_sub_brands,
+            tray_color=tray_color,
+            nozzle_temp_min=nozzle_temp_min,
+            nozzle_temp_max=nozzle_temp_max,
+            setting_id=setting_id,
+        )
+
+        if not success:
+            raise HTTPException(status_code=500, detail="Failed to send filament configuration command")
+
     # Method 1: Select existing calibration profile by cali_idx
-    # IMPORTANT: Only pass setting_id if the K profile itself has one (from kprofile_setting_id)
-    # Do NOT use the preset's setting_id as fallback - it breaks the K profile linking in the slicer
+    # Do NOT include setting_id — BambuStudio never sends it in extrusion_cali_sel,
+    # and including it causes the firmware to mislink the profile on X1C/P1S.
     client.extrusion_cali_sel(
         ams_id=ams_id,
         tray_id=tray_id,
         cali_idx=cali_idx,
         filament_id=filament_id_for_kprofile,
         nozzle_diameter=nozzle_diameter,
-        setting_id=kprofile_setting_id if kprofile_setting_id else None,
     )
 
-    # Method 2: Also directly set the K value if provided (for better compatibility)
-    if k_value > 0:
+    # Method 2: Only send extrusion_cali_set when NO existing profile was selected
+    # (cali_idx == -1). When cali_idx >= 0, extrusion_cali_sel already selected the
+    # correct profile. Sending extrusion_cali_set with the same cali_idx would MODIFY
+    # the existing profile's metadata (extruder_id, nozzle_id, name, setting_id),
+    # corrupting it — e.g., overwriting a High Flow extruder 1 profile with
+    # hardcoded extruder_id=0 and nozzle_id=HS00.
+    if k_value > 0 and cali_idx < 0:
         # Calculate global tray ID for extrusion_cali_set
         if ams_id <= 3:
             global_tray_id = ams_id * 4 + tray_id
@@ -1682,11 +1717,12 @@ async def configure_ams_slot(
         client.extrusion_cali_set(
             tray_id=global_tray_id,
             k_value=k_value,
-            n_coef=0.0,
             nozzle_diameter=nozzle_diameter,
-            bed_temp=60,
             nozzle_temp=nozzle_temp_max,
-            max_volumetric_speed=20.0,
+            filament_id=filament_id_for_kprofile,
+            setting_id=kprofile_setting_id or "",
+            name=tray_sub_brands or "",
+            cali_idx=cali_idx,
         )
 
     # Request fresh status push from printer so frontend gets updated data via WebSocket
@@ -1821,6 +1857,37 @@ async def stop_print(
     return {"success": True, "message": "Print stop command sent"}
 
 
+@router.post("/{printer_id}/clear-plate")
+async def clear_plate(
+    printer_id: int,
+    _=RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
+    db: AsyncSession = Depends(get_db),
+):
+    """Acknowledge that the build plate has been cleared after a finished/failed print.
+
+    Sets a plate-cleared flag so the scheduler can start the next queued print.
+    No MQTT command is sent to the printer — the scheduler's start_print command
+    will override the FINISH/FAILED state when it sends the next job.
+    """
+    result = await db.execute(select(Printer).where(Printer.id == printer_id))
+    printer = result.scalar_one_or_none()
+    if not printer:
+        raise HTTPException(404, "Printer not found")
+
+    if not printer_manager.is_connected(printer_id):
+        raise HTTPException(400, "Printer not connected")
+
+    state = printer_manager.get_status(printer_id)
+    if not state or state.state not in ("FINISH", "FAILED"):
+        raise HTTPException(
+            400, f"Printer is not in FINISH or FAILED state (current: {state.state if state else 'unknown'})"
+        )
+
+    printer_manager.set_plate_cleared(printer_id)
+
+    return {"success": True, "message": "Plate cleared, next print will start shortly"}
+
+
 @router.post("/{printer_id}/print/pause")
 async def pause_print(
     printer_id: int,
@@ -2078,9 +2145,133 @@ async def refresh_ams_slot(
     if not success:
         raise HTTPException(400, message)
 
+    # Apply PA profile after delay (RFID re-read takes a few seconds)
+    asyncio.create_task(_apply_pa_after_refresh(printer_id, ams_id, slot_id))
+
     return {"success": True, "message": message}
 
 
+async def _apply_pa_after_refresh(printer_id: int, ams_id: int, slot_id: int):
+    """Apply PA profile after RFID re-read completes.
+
+    Waits for the printer to finish processing the RFID data, then selects
+    the K-profile via extrusion_cali_sel.  Does NOT re-send ams_set_filament_setting
+    because that would overwrite the RFID-provided filament data.
+    """
+    await asyncio.sleep(5)
+    try:
+        from backend.app.api.routes.inventory import _find_tray_in_ams_data
+        from backend.app.core.database import async_session
+        from backend.app.models.spool import Spool
+        from backend.app.models.spool_assignment import SpoolAssignment as SA
+        from backend.app.services.spool_tag_matcher import is_bambu_tag
+
+        client = printer_manager.get_client(printer_id)
+        if not client:
+            return
+
+        state = printer_manager.get_status(printer_id)
+        if not state or not state.raw_data:
+            return
+
+        # Find current tray data (should have RFID data by now)
+        ams_data = state.raw_data.get("ams", {})
+        ams_list = (
+            ams_data.get("ams", []) if isinstance(ams_data, dict) else ams_data if isinstance(ams_data, list) else []
+        )
+        tray = _find_tray_in_ams_data(ams_list, ams_id, slot_id)
+        if not tray or not tray.get("tray_type"):
+            logger.debug("PA re-apply: no tray data for AMS%d-T%d", ams_id, slot_id)
+            return
+
+        tag_uid = tray.get("tag_uid", "")
+        tray_uuid = tray.get("tray_uuid", "")
+        tray_info_idx = tray.get("tray_info_idx", "")
+        if not is_bambu_tag(tag_uid, tray_uuid, tray_info_idx):
+            return
+
+        async with async_session() as db:
+            from sqlalchemy import select as sa_select
+            from sqlalchemy.orm import selectinload
+
+            result = await db.execute(
+                sa_select(SA)
+                .options(selectinload(SA.spool).selectinload(Spool.k_profiles))
+                .where(SA.printer_id == printer_id, SA.ams_id == ams_id, SA.tray_id == slot_id)
+            )
+            assignment = result.scalar_one_or_none()
+            if not assignment or not assignment.spool or not assignment.spool.k_profiles:
+                return
+
+            spool = assignment.spool
+            nozzle_diameter = "0.4"
+            if state.nozzles:
+                nd = state.nozzles[0].nozzle_diameter
+                if nd:
+                    nozzle_diameter = nd
+
+            # Determine slot's extruder from ams_extruder_map
+            slot_extruder = None
+            if state.ams_extruder_map:
+                if ams_id == 255:
+                    # External slots: ext-L (tray 0) → extruder 1, ext-R (tray 1) → extruder 0
+                    slot_extruder = 1 - slot_id  # 0→1, 1→0
+                else:
+                    slot_extruder = state.ams_extruder_map.get(str(ams_id))
+
+            matching_kp = None
+            for kp in spool.k_profiles:
+                if kp.printer_id == printer_id and kp.nozzle_diameter == nozzle_diameter:
+                    if slot_extruder is not None and kp.extruder_id is not None and kp.extruder_id != slot_extruder:
+                        continue
+                    matching_kp = kp
+                    break
+
+            if not matching_kp or matching_kp.cali_idx is None:
+                return
+
+            # The filament_id in extrusion_cali_sel must match the filament preset
+            # under which the K-profile was calibrated. Use spool.slicer_filament
+            # (the preset assigned in inventory), falling back to tray's RFID value.
+            kp_filament_id = spool.slicer_filament or tray_info_idx
+
+            logger.info(
+                "PA re-apply AMS%d-T%d: cali_idx=%d, filament_id=%s",
+                ams_id,
+                slot_id,
+                matching_kp.cali_idx,
+                kp_filament_id,
+            )
+
+            # 1. Select K-profile
+            # NOTE: Do NOT send ams_set_filament_setting here — it tells the firmware
+            # "this is a manual config" which destroys the RFID-detected spool state
+            # (changes eye icon to pen icon in slicer).
+            client.extrusion_cali_sel(
+                ams_id=ams_id,
+                tray_id=slot_id,
+                cali_idx=matching_kp.cali_idx,
+                filament_id=kp_filament_id,
+                nozzle_diameter=nozzle_diameter,
+            )
+
+            # NOTE: Do NOT send extrusion_cali_set here. extrusion_cali_sel already
+            # selected the correct profile by cali_idx. Sending extrusion_cali_set with
+            # the same cali_idx would MODIFY the existing profile's metadata (extruder_id,
+            # nozzle_id, name), corrupting it.
+
+            logger.info(
+                "Applied PA profile cali_idx=%d k=%.3f to printer %d AMS%d-T%d",
+                matching_kp.cali_idx,
+                matching_kp.k_value or 0,
+                printer_id,
+                ams_id,
+                slot_id,
+            )
+    except Exception as e:
+        logger.warning("Failed to apply PA profile after RFID re-read: %s", e)
+
+
 @router.get("/{printer_id}/runtime-debug")
 async def get_runtime_debug(
     printer_id: int,

+ 47 - 3
backend/app/api/routes/spoolman.py

@@ -6,12 +6,14 @@ from fastapi import APIRouter, Depends, HTTPException
 from pydantic import BaseModel
 from sqlalchemy import select
 from sqlalchemy.ext.asyncio import AsyncSession
+from sqlalchemy.orm import selectinload
 
 from backend.app.core.auth import RequirePermissionIfAuthEnabled
 from backend.app.core.database import get_db
 from backend.app.core.permissions import Permission
 from backend.app.models.printer import Printer
 from backend.app.models.settings import Settings
+from backend.app.models.spool_assignment import SpoolAssignment
 from backend.app.models.user import User
 from backend.app.services.printer_manager import printer_manager
 from backend.app.services.spoolman import (
@@ -230,6 +232,22 @@ async def sync_printer_ams(
             detail=f"Failed to connect to Spoolman after multiple retries: {str(e)}",
         )
 
+    # Load inventory weights as fallback (when AMS MQTT data lacks remain values)
+    inv_weights: dict[tuple[int, int], float] = {}
+    try:
+        assign_result = await db.execute(
+            select(SpoolAssignment)
+            .options(selectinload(SpoolAssignment.spool))
+            .where(SpoolAssignment.printer_id == printer_id)
+        )
+        for assignment in assign_result.scalars().all():
+            spool = assignment.spool
+            if spool and spool.label_weight > 0:
+                remaining = max(0.0, spool.label_weight - (spool.weight_used or 0))
+                inv_weights[(assignment.ams_id, assignment.tray_id)] = remaining
+    except Exception as e:
+        logger.debug("Could not load inventory weights for printer %s: %s", printer_id, e)
+
     for ams_unit in ams_units:
         if not isinstance(ams_unit, dict):
             continue
@@ -270,11 +288,13 @@ async def sync_printer_ams(
                 current_tray_uuids.add(spool_tag.upper())
 
             try:
+                inv_remaining = inv_weights.get((ams_id, tray.tray_id))
                 sync_result = await client.sync_ams_tray(
                     tray,
                     printer.name,
                     disable_weight_sync=disable_weight_sync,
                     cached_spools=cached_spools,
+                    inventory_remaining=inv_remaining,
                 )
                 if sync_result:
                     synced += 1
@@ -345,6 +365,8 @@ async def sync_all_printers(
     all_errors = []
     # Track tray UUIDs per printer (for clearing removed spools)
     printer_tray_uuids: dict[str, set[str]] = {}
+    # Track synced spool IDs per printer (for location-based cleanup when no UUIDs available)
+    printer_synced_ids: dict[str, set[int]] = {}
 
     # OPTIMIZATION: Fetch all spools once before processing ALL printers/trays
     # This eliminates redundant API calls across all printers
@@ -359,6 +381,19 @@ async def sync_all_printers(
             detail=f"Failed to connect to Spoolman after multiple retries: {str(e)}",
         )
 
+    # Load inventory assignments for weight fallback (when AMS MQTT data lacks remain values)
+    # Key: (printer_id, ams_id, tray_id) → remaining_weight in grams
+    inventory_weights: dict[tuple[int, int, int], float] = {}
+    try:
+        assign_result = await db.execute(select(SpoolAssignment).options(selectinload(SpoolAssignment.spool)))
+        for assignment in assign_result.scalars().all():
+            spool = assignment.spool
+            if spool and spool.label_weight > 0:
+                remaining = max(0.0, spool.label_weight - (spool.weight_used or 0))
+                inventory_weights[(assignment.printer_id, assignment.ams_id, assignment.tray_id)] = remaining
+    except Exception as e:
+        logger.debug("Could not load inventory assignments for weight fallback: %s", e)
+
     for printer in printers:
         state = printer_manager.get_status(printer.id)
         if not state or not state.raw_data:
@@ -368,8 +403,9 @@ async def sync_all_printers(
         if not ams_data:
             continue
 
-        # Initialize tray UUID set for this printer
+        # Initialize tracking sets for this printer
         printer_tray_uuids[printer.name] = set()
+        printer_synced_ids[printer.name] = set()
 
         # Handle different AMS data structures
         # Traditional AMS: list of {"id": N, "tray": [...]} dicts
@@ -432,16 +468,21 @@ async def sync_all_printers(
                     printer_tray_uuids[printer.name].add(spool_tag.upper())
 
                 try:
+                    # Look up inventory weight as fallback when AMS data is invalid
+                    inv_remaining = inventory_weights.get((printer.id, ams_id, tray.tray_id))
                     sync_result = await client.sync_ams_tray(
                         tray,
                         printer.name,
                         disable_weight_sync=disable_weight_sync,
                         cached_spools=cached_spools,
+                        inventory_remaining=inv_remaining,
                     )
                     if sync_result:
                         total_synced += 1
-                        # Add newly created spool to cache
+                        # Track synced spool ID for cleanup
                         if sync_result.get("id"):
+                            printer_synced_ids[printer.name].add(sync_result["id"])
+                            # Add newly created spool to cache
                             spool_exists = any(s.get("id") == sync_result["id"] for s in cached_spools)
                             if not spool_exists:
                                 cached_spools.append(sync_result)
@@ -453,7 +494,10 @@ async def sync_all_printers(
     for printer_name, current_tray_uuids in printer_tray_uuids.items():
         try:
             cleared = await client.clear_location_for_removed_spools(
-                printer_name, current_tray_uuids, cached_spools=cached_spools
+                printer_name,
+                current_tray_uuids,
+                cached_spools=cached_spools,
+                synced_spool_ids=printer_synced_ids.get(printer_name, set()),
             )
             if cleared > 0:
                 logger.info("Cleared location for %s spools removed from %s", cleared, printer_name)

+ 1 - 1
backend/app/api/routes/support.py

@@ -473,7 +473,7 @@ async def _collect_support_info() -> dict:
                 for unit in ams_units:
                     trays = unit.get("tray", [])
                     ams_tray_count += len([t for t in trays if t.get("tray_type")])
-                has_vt_tray = state.raw_data.get("vt_tray") is not None
+                has_vt_tray = bool(state.raw_data.get("vt_tray"))
 
             info["printers"].append(
                 {

+ 317 - 0
backend/app/core/bambu_colors.py

@@ -0,0 +1,317 @@
+"""Bambu Lab filament color code to color name mapping.
+
+Source: https://github.com/queengooborg/Bambu-Lab-RFID-Library
+
+Maps tray_id_name codes (e.g. "A06-D0") to human-readable color names (e.g. "Titan Gray").
+"""
+
+# Full color code → name mapping by material prefix
+BAMBU_FILAMENT_COLORS: dict[str, str] = {
+    # PLA Basic (A00)
+    "A00-W1": "Jade White",
+    "A00-P0": "Beige",
+    "A00-D2": "Light Gray",
+    "A00-Y0": "Yellow",
+    "A00-Y2": "Sunflower Yellow",
+    "A00-A1": "Pumpkin Orange",
+    "A00-A0": "Orange",
+    "A00-Y4": "Gold",
+    "A00-G3": "Bright Green",
+    "A00-G1": "Bambu Green",
+    "A00-G2": "Mistletoe Green",
+    "A00-R3": "Hot Pink",
+    "A00-P6": "Magenta",
+    "A00-R0": "Red",
+    "A00-R2": "Maroon Red",
+    "A00-P5": "Purple",
+    "A00-P2": "Indigo Purple",
+    "A00-B5": "Turquoise",
+    "A00-B8": "Cyan",
+    "A00-B3": "Cobalt Blue",
+    "A00-N0": "Brown",
+    "A00-N1": "Cocoa Brown",
+    "A00-Y3": "Bronze",
+    "A00-D0": "Gray",
+    "A00-D1": "Silver",
+    "A00-B1": "Blue Grey",
+    "A00-D3": "Dark Gray",
+    "A00-K0": "Black",
+    # PLA Basic Gradient (A00-M*)
+    "A00-M3": "Pink Citrus",
+    "A00-M6": "Dusk Glare",
+    "A00-M0": "Arctic Whisper",
+    "A00-M1": "Solar Breeze",
+    "A00-M5": "Blueberry Bubblegum",
+    "A00-M4": "Mint Lime",
+    "A00-M2": "Ocean to Meadow",
+    "A00-M7": "Cotton Candy Cloud",
+    # PLA Lite (A18)
+    "A18-K0": "Black",
+    "A18-D0": "Gray",
+    "A18-W0": "White",
+    "A18-R0": "Red",
+    "A18-Y0": "Yellow",
+    "A18-B0": "Cyan",
+    "A18-B1": "Blue",
+    "A18-P0": "Matte Beige",
+    # PLA Matte (A01)
+    "A01-W2": "Ivory White",
+    "A01-W3": "Bone White",
+    "A01-Y2": "Lemon Yellow",
+    "A01-A2": "Mandarin Orange",
+    "A01-P3": "Sakura Pink",
+    "A01-P4": "Lilac Purple",
+    "A01-R3": "Plum",
+    "A01-R1": "Scarlet Red",
+    "A01-R4": "Dark Red",
+    "A01-G0": "Apple Green",
+    "A01-G1": "Grass Green",
+    "A01-G7": "Dark Green",
+    "A01-B4": "Ice Blue",
+    "A01-B0": "Sky Blue",
+    "A01-B3": "Marine Blue",
+    "A01-B6": "Dark Blue",
+    "A01-Y3": "Desert Tan",
+    "A01-N1": "Latte Brown",
+    "A01-N3": "Caramel",
+    "A01-R2": "Terracotta",
+    "A01-N2": "Dark Brown",
+    "A01-N0": "Dark Chocolate",
+    "A01-D3": "Ash Gray",
+    "A01-D0": "Nardo Gray",
+    "A01-K1": "Charcoal",
+    # PLA Glow (A12)
+    "A12-G0": "Green",
+    "A12-R0": "Pink",
+    "A12-A0": "Orange",
+    "A12-Y0": "Yellow",
+    "A12-B0": "Blue",
+    # PLA Marble (A07)
+    "A07-R5": "Red Granite",
+    "A07-D4": "White Marble",
+    # PLA Aero (A11)
+    "A11-W0": "White",
+    "A11-K0": "Black",
+    # PLA Sparkle (A08)
+    "A08-G3": "Alpine Green Sparkle",
+    "A08-D5": "Slate Gray Sparkle",
+    "A08-B7": "Royal Purple Sparkle",
+    "A08-R2": "Crimson Red Sparkle",
+    "A08-K2": "Onyx Black Sparkle",
+    "A08-Y1": "Classic Gold Sparkle",
+    # PLA Metal (A02)
+    "A02-B2": "Cobalt Blue Metallic",
+    "A02-G2": "Oxide Green Metallic",
+    "A02-Y1": "Iridium Gold Metallic",
+    "A02-D2": "Iron Gray Metallic",
+    # PLA Translucent (A17)
+    "A17-B1": "Blue",
+    "A17-A0": "Orange",
+    "A17-P0": "Purple",
+    # PLA Silk+ (A06)
+    "A06-Y1": "Gold",
+    "A06-D0": "Titan Gray",
+    "A06-D1": "Silver",
+    "A06-W0": "White",
+    "A06-R0": "Candy Red",
+    "A06-G0": "Candy Green",
+    "A06-G1": "Mint",
+    "A06-B1": "Blue",
+    "A06-B0": "Baby Blue",
+    "A06-P0": "Purple",
+    "A06-R1": "Rose Gold",
+    "A06-R2": "Pink",
+    "A06-Y0": "Champagne",
+    # PLA Silk Multi-Color (A05)
+    "A05-M8": "Dawn Radiance",
+    "A05-M4": "Aurora Purple",
+    "A05-M1": "South Beach",
+    "A05-T3": "Neon City",
+    "A05-T2": "Midnight Blaze",
+    "A05-T1": "Gilded Rose",
+    "A05-T4": "Blue Hawaii",
+    "A05-T5": "Velvet Eclipse",
+    # PLA Galaxy (A15)
+    "A15-B0": "Purple",
+    "A15-G0": "Green",
+    "A15-G1": "Nebulae",
+    "A15-R0": "Brown",
+    # PLA Wood (A16)
+    "A16-K0": "Black Walnut",
+    "A16-R0": "Rosewood",
+    "A16-N0": "Clay Brown",
+    "A16-G0": "Classic Birch",
+    "A16-W0": "White Oak",
+    "A16-Y0": "Ochre Yellow",
+    # PLA-CF (A50)
+    "A50-D6": "Lava Gray",
+    "A50-K0": "Black",
+    "A50-B6": "Royal Blue",
+    # PLA Tough+ (A10)
+    "A10-W0": "White",
+    "A10-D0": "Gray",
+    # PLA Tough (A09)
+    "A09-B5": "Lavender Blue",
+    "A09-B4": "Light Blue",
+    "A09-A0": "Orange",
+    "A09-D1": "Silver",
+    "A09-R3": "Vermilion Red",
+    "A09-Y0": "Yellow",
+    # PETG HF (G02)
+    "G02-K0": "Black",
+    "G02-W0": "White",
+    "G02-R0": "Red",
+    "G02-D0": "Gray",
+    "G02-D1": "Dark Gray",
+    "G02-Y1": "Cream",
+    "G02-Y0": "Yellow",
+    "G02-A0": "Orange",
+    "G02-N1": "Peanut Brown",
+    "G02-G1": "Lime Green",
+    "G02-G0": "Green",
+    "G02-G2": "Forest Green",
+    "G02-B1": "Lake Blue",
+    "G02-B0": "Blue",
+    # PETG Translucent (G01)
+    "G01-G1": "Translucent Teal",
+    "G01-B0": "Translucent Light Blue",
+    "G01-C0": "Clear",
+    "G01-D0": "Translucent Gray",
+    "G01-G0": "Translucent Olive",
+    "G01-N0": "Translucent Brown",
+    "G01-A0": "Translucent Orange",
+    "G01-P1": "Translucent Pink",
+    "G01-P0": "Translucent Purple",
+    # PETG-CF (G50)
+    "G50-P7": "Violet Purple",
+    "G50-K0": "Black",
+    # ABS (B00)
+    "B00-D1": "Silver",
+    "B00-K0": "Black",
+    "B00-W0": "White",
+    "B00-G6": "Bambu Green",
+    "B00-G7": "Olive",
+    "B00-Y1": "Tangerine Yellow",
+    "B00-A0": "Orange",
+    "B00-R0": "Red",
+    "B00-B4": "Azure",
+    "B00-B0": "Blue",
+    "B00-B6": "Navy Blue",
+    # ABS-GF (B50)
+    "B50-A0": "Orange",
+    "B50-K0": "Black",
+    # ASA (B01)
+    "B01-W0": "White",
+    "B01-K0": "Black",
+    "B01-D0": "Gray",
+    # ASA Aero (B02)
+    "B02-W0": "White",
+    # PC (C00)
+    "C00-C1": "Transparent",
+    "C00-C0": "Clear Black",
+    "C00-K0": "Black",
+    "C00-W0": "White",
+    # PC FR (C01)
+    "C01-K0": "Black",
+    # TPU for AMS (U02)
+    "U02-B0": "Blue",
+    "U02-D0": "Gray",
+    "U02-K0": "Black",
+    # PAHT-CF (N04)
+    "N04-K0": "Black",
+    # PA6-GF (N08)
+    "N08-K0": "Black",
+    # Support for PLA/PETG (S02, S05)
+    "S02-W0": "Nature",
+    "S02-W1": "White",
+    "S05-C0": "Black",
+    # Support for ABS (S06)
+    "S06-W0": "White",
+    # Support for PA/PET (S03)
+    "S03-G1": "Green",
+    # PVA (S04)
+    "S04-Y0": "Clear",
+}
+
+# Fallback: color code suffix → name (for unknown material prefixes)
+BAMBU_COLOR_CODE_FALLBACK: dict[str, str] = {
+    "W0": "White",
+    "W1": "Jade White",
+    "W2": "Ivory White",
+    "W3": "Bone White",
+    "Y0": "Yellow",
+    "Y1": "Gold",
+    "Y2": "Sunflower Yellow",
+    "Y3": "Bronze",
+    "Y4": "Gold",
+    "A0": "Orange",
+    "A1": "Pumpkin Orange",
+    "A2": "Mandarin Orange",
+    "R0": "Red",
+    "R1": "Scarlet Red",
+    "R2": "Maroon Red",
+    "R3": "Hot Pink",
+    "R4": "Dark Red",
+    "R5": "Red Granite",
+    "P0": "Beige",
+    "P1": "Pink",
+    "P2": "Indigo Purple",
+    "P3": "Sakura Pink",
+    "P4": "Lilac Purple",
+    "P5": "Purple",
+    "P6": "Magenta",
+    "P7": "Violet Purple",
+    "B0": "Blue",
+    "B1": "Blue Grey",
+    "B2": "Cobalt Blue",
+    "B3": "Cobalt Blue",
+    "B4": "Ice Blue",
+    "B5": "Turquoise",
+    "B6": "Navy Blue",
+    "B7": "Royal Purple",
+    "B8": "Cyan",
+    "G0": "Green",
+    "G1": "Grass Green",
+    "G2": "Mistletoe Green",
+    "G3": "Bright Green",
+    "G6": "Bambu Green",
+    "G7": "Dark Green",
+    "N0": "Brown",
+    "N1": "Peanut Brown",
+    "N2": "Dark Brown",
+    "N3": "Caramel",
+    "D0": "Gray",
+    "D1": "Silver",
+    "D2": "Light Gray",
+    "D3": "Dark Gray",
+    "D4": "White Marble",
+    "D5": "Slate Gray",
+    "D6": "Lava Gray",
+    "K0": "Black",
+    "K1": "Charcoal",
+    "K2": "Onyx Black",
+    "C0": "Clear Black",
+    "C1": "Transparent",
+}
+
+
+def resolve_bambu_color_name(tray_id_name: str) -> str | None:
+    """Resolve a Bambu Lab tray_id_name code to a human-readable color name.
+
+    Tries exact match first, then falls back to color code suffix lookup.
+    Returns None if the code cannot be resolved.
+    """
+    if not tray_id_name:
+        return None
+
+    # Exact match
+    name = BAMBU_FILAMENT_COLORS.get(tray_id_name)
+    if name:
+        return name
+
+    # Fallback: use color code suffix (e.g. "D0" from "A06-D0")
+    parts = tray_id_name.split("-")
+    if len(parts) >= 2:
+        return BAMBU_COLOR_CODE_FALLBACK.get(parts[1])
+
+    return None

+ 357 - 0
backend/app/core/catalog_defaults.py

@@ -0,0 +1,357 @@
+"""Default spool and color catalog entries."""
+
+# (name, weight_in_grams)
+DEFAULT_SPOOL_CATALOG: list[tuple[str, int]] = [
+    ("3D FilaPrint - Cardboard", 210),
+    ("3D FilaPrint - Plastic", 238),
+    ("3D Fuel - Plastic", 264),
+    ("3D Power - Plastic", 220),
+    ("3D Solutech - Plastic", 173),
+    ("3DE - Cardboard", 136),
+    ("3DE - Plastic", 181),
+    ("3DHOJOR - Cardboard", 157),
+    ("3DJake - Cardboard", 209),
+    ("3DJake - Plastic", 232),
+    ("3DJake 250g - Plastic", 91),
+    ("3DJake ecoPLA - Plastic", 210),
+    ("3DXTech - Plastic", 258),
+    ("Acccreate - Plastic", 161),
+    ("Amazon Basics - Plastic", 234),
+    ("Amolen - Plastic", 150),
+    ("AMZ3D - Plastic", 233),
+    ("Anycubic - Cardboard", 125),
+    ("Anycubic - Plastic", 127),
+    ("Atomic Filament - Plastic", 272),
+    ("Aurapol - Plastic", 220),
+    ("Azure Film - Plastic", 163),
+    ("Bambu Lab - Plastic High Temp", 216),
+    ("Bambu Lab - Plastic Low Temp", 250),
+    ("Bambu Lab - Plastic White", 253),
+    ("BQ - Plastic", 218),
+    ("Colorfabb - Plastic", 236),
+    ("Colorfabb 750g - Cardboard", 152),
+    ("Colorfabb 750g - Plastic", 254),
+    ("Comgrow - Cardboard", 166),
+    ("Creality - Cardboard", 180),
+    ("Creality - Plastic", 135),
+    ("Das Filament - Plastic", 211),
+    ("Devil Design - Plastic", 256),
+    ("Duramic 3D - Cardboard", 136),
+    ("Elegoo - Cardboard", 153),
+    ("Elegoo - Plastic", 111),
+    ("Eryone - Cardboard", 156),
+    ("Eryone - Plastic", 187),
+    ("eSUN - Cardboard", 147),
+    ("eSUN - Plastic", 240),
+    ("eSUN 2.5kg - Plastic", 634),
+    ("Extrudr - Plastic", 244),
+    ("Fiberlogy - Plastic", 260),
+    ("Filament PM - Plastic", 224),
+    ("Fillamentum - Plastic", 230),
+    ("Flashforge - Plastic", 167),
+    ("FormFutura - Cardboard", 155),
+    ("FormFutura 750g - Plastic", 212),
+    ("Geeetech - Plastic", 178),
+    ("Gembird - Cardboard", 143),
+    ("Hatchbox - Plastic", 225),
+    ("Inland - Cardboard", 142),
+    ("Inland - Plastic", 210),
+    ("Jayo - Cardboard", 120),
+    ("Jayo - Plastic", 126),
+    ("Jayo 250g - Plastic", 58),
+    ("Kingroon - Cardboard", 155),
+    ("Kingroon - Plastic", 156),
+    ("KVP - Plastic", 263),
+    ("Matter Hackers - Plastic", 215),
+    ("MG Chemicals - Cardboard", 150),
+    ("MG Chemicals - Plastic", 239),
+    ("Mika3D - Plastic", 175),
+    ("MonoPrice - Plastic", 221),
+    ("Overture - Cardboard", 150),
+    ("Overture - Plastic", 237),
+    ("PolyMaker - Cardboard", 137),
+    ("PolyMaker - Plastic", 220),
+    ("PolyMaker 3kg - Cardboard", 418),
+    ("PolyTerra PLA - Cardboard", 147),
+    ("PrimaSelect - Plastic", 222),
+    ("ProtoPasta - Cardboard", 80),
+    ("Prusament - Plastic", 201),
+    ("Prusament - Plastic w/ Cardboard Core", 196),
+    ("Rosa3D - Plastic", 245),
+    ("Sakata3D - Plastic", 205),
+    ("Snapmaker - Cardboard", 148),
+    ("Sovol - Cardboard", 145),
+    ("Spectrum - Cardboard", 180),
+    ("Spectrum - Plastic", 257),
+    ("Sunlu - Plastic", 117),
+    ("Sunlu - Plastic V2", 165),
+    ("Sunlu - Plastic V3", 179),
+    ("Sunlu 250g - Plastic", 55),
+    ("UltiMaker - Plastic", 235),
+    ("Voolt3D - Plastic", 190),
+    ("Voxelab - Plastic", 171),
+    ("Wanhao - Plastic", 267),
+    ("Ziro - Plastic", 166),
+    ("ZYLtech - Plastic", 179),
+]
+
+# (manufacturer, color_name, hex_color, material)
+DEFAULT_COLOR_CATALOG: list[tuple[str, str, str, str]] = [
+    # Bambu Lab PLA Basic (from official hex code PDF)
+    ("Bambu Lab", "Jade White", "#FFFFFF", "PLA Basic"),
+    ("Bambu Lab", "Black", "#000000", "PLA Basic"),
+    ("Bambu Lab", "Silver", "#A6A9AA", "PLA Basic"),
+    ("Bambu Lab", "Light Gray", "#D1D3D5", "PLA Basic"),
+    ("Bambu Lab", "Gray", "#8E9089", "PLA Basic"),
+    ("Bambu Lab", "Dark Gray", "#545454", "PLA Basic"),
+    ("Bambu Lab", "Red", "#C12E1F", "PLA Basic"),
+    ("Bambu Lab", "Maroon Red", "#9D2235", "PLA Basic"),
+    ("Bambu Lab", "Magenta", "#EC008C", "PLA Basic"),
+    ("Bambu Lab", "Hot Pink", "#F5547C", "PLA Basic"),
+    ("Bambu Lab", "Pink", "#F55A74", "PLA Basic"),
+    ("Bambu Lab", "Beige", "#F7E6DE", "PLA Basic"),
+    ("Bambu Lab", "Yellow", "#F4EE2A", "PLA Basic"),
+    ("Bambu Lab", "Sunflower Yellow", "#FEC600", "PLA Basic"),
+    ("Bambu Lab", "Gold", "#E4BD68", "PLA Basic"),
+    ("Bambu Lab", "Orange", "#FF6A13", "PLA Basic"),
+    ("Bambu Lab", "Pumpkin Orange", "#FF9016", "PLA Basic"),
+    ("Bambu Lab", "Bright Green", "#BECF00", "PLA Basic"),
+    ("Bambu Lab", "Bambu Green", "#00AE42", "PLA Basic"),
+    ("Bambu Lab", "Mistletoe Green", "#3F8E43", "PLA Basic"),
+    ("Bambu Lab", "Turquoise", "#00B1B7", "PLA Basic"),
+    ("Bambu Lab", "Cyan", "#0086D6", "PLA Basic"),
+    ("Bambu Lab", "Blue", "#0A2989", "PLA Basic"),
+    ("Bambu Lab", "Blue Grey", "#5B6579", "PLA Basic"),
+    ("Bambu Lab", "Cobalt Blue", "#0056B8", "PLA Basic"),
+    ("Bambu Lab", "Purple", "#5E43B7", "PLA Basic"),
+    ("Bambu Lab", "Indigo Purple", "#482960", "PLA Basic"),
+    ("Bambu Lab", "Brown", "#9D432C", "PLA Basic"),
+    ("Bambu Lab", "Cocoa Brown", "#6F5034", "PLA Basic"),
+    ("Bambu Lab", "Bronze", "#847D48", "PLA Basic"),
+    # Bambu Lab PLA Matte (from official hex code PDF)
+    ("Bambu Lab", "Ivory White", "#FFFFFF", "PLA Matte"),
+    ("Bambu Lab", "Bone White", "#CBC6B8", "PLA Matte"),
+    ("Bambu Lab", "Desert Tan", "#E8DBB7", "PLA Matte"),
+    ("Bambu Lab", "Latte Brown", "#D3B7A7", "PLA Matte"),
+    ("Bambu Lab", "Caramel", "#AE835B", "PLA Matte"),
+    ("Bambu Lab", "Terracotta", "#B15533", "PLA Matte"),
+    ("Bambu Lab", "Dark Brown", "#7D6556", "PLA Matte"),
+    ("Bambu Lab", "Dark Chocolate", "#4D3324", "PLA Matte"),
+    ("Bambu Lab", "Lilac Purple", "#AE96D4", "PLA Matte"),
+    ("Bambu Lab", "Sakura Pink", "#E8AFCF", "PLA Matte"),
+    ("Bambu Lab", "Mandarin Orange", "#F99963", "PLA Matte"),
+    ("Bambu Lab", "Lemon Yellow", "#F7D959", "PLA Matte"),
+    ("Bambu Lab", "Plum", "#950051", "PLA Matte"),
+    ("Bambu Lab", "Scarlet Red", "#DE4343", "PLA Matte"),
+    ("Bambu Lab", "Dark Red", "#BB3D43", "PLA Matte"),
+    ("Bambu Lab", "Dark Green", "#68724D", "PLA Matte"),
+    ("Bambu Lab", "Grass Green", "#61C680", "PLA Matte"),
+    ("Bambu Lab", "Apple Green", "#C2E189", "PLA Matte"),
+    ("Bambu Lab", "Ice Blue", "#A3D8E1", "PLA Matte"),
+    ("Bambu Lab", "Sky Blue", "#56B7E6", "PLA Matte"),
+    ("Bambu Lab", "Marine Blue", "#0078BF", "PLA Matte"),
+    ("Bambu Lab", "Dark Blue", "#042F56", "PLA Matte"),
+    ("Bambu Lab", "Ash Gray", "#9B9EA0", "PLA Matte"),
+    ("Bambu Lab", "Nardo Gray", "#757575", "PLA Matte"),
+    ("Bambu Lab", "Charcoal", "#000000", "PLA Matte"),
+    # Bambu Lab PLA Silk+ (from store page)
+    ("Bambu Lab", "Gold", "#F4A925", "PLA Silk"),
+    ("Bambu Lab", "Silver", "#C8C8C8", "PLA Silk"),
+    ("Bambu Lab", "Titan Gray", "#5F6367", "PLA Silk"),
+    ("Bambu Lab", "Blue", "#008BDA", "PLA Silk"),
+    ("Bambu Lab", "Purple", "#8671CB", "PLA Silk"),
+    ("Bambu Lab", "Candy Red", "#D02727", "PLA Silk"),
+    ("Bambu Lab", "Candy Green", "#018814", "PLA Silk"),
+    ("Bambu Lab", "Rose Gold", "#BA9594", "PLA Silk"),
+    ("Bambu Lab", "Baby Blue", "#A8C6EE", "PLA Silk"),
+    ("Bambu Lab", "Pink", "#F7ADA6", "PLA Silk"),
+    ("Bambu Lab", "Mint", "#96DCB9", "PLA Silk"),
+    ("Bambu Lab", "Champagne", "#F3CFB2", "PLA Silk"),
+    ("Bambu Lab", "White", "#FFFFFF", "PLA Silk"),
+    # Bambu Lab PLA Sparkle (from store page)
+    ("Bambu Lab", "Classic Gold Sparkle", "#CEA629", "PLA Sparkle"),
+    ("Bambu Lab", "Slate Gray Sparkle", "#8E9089", "PLA Sparkle"),
+    ("Bambu Lab", "Crimson Red Sparkle", "#792B36", "PLA Sparkle"),
+    ("Bambu Lab", "Royal Purple Sparkle", "#483D8B", "PLA Sparkle"),
+    ("Bambu Lab", "Alpine Green Sparkle", "#3F5443", "PLA Sparkle"),
+    ("Bambu Lab", "Onyx Black Sparkle", "#2D2B28", "PLA Sparkle"),
+    # Bambu Lab PLA Translucent (from official hex code PDF)
+    ("Bambu Lab", "Teal", "#009FA1", "PLA Translucent"),
+    ("Bambu Lab", "Light Jade", "#96D8AF", "PLA Translucent"),
+    ("Bambu Lab", "Blue", "#0047BB", "PLA Translucent"),
+    ("Bambu Lab", "Mellow Yellow", "#F5DBAB", "PLA Translucent"),
+    ("Bambu Lab", "Purple", "#8344B0", "PLA Translucent"),
+    ("Bambu Lab", "Cherry Pink", "#F5B6CD", "PLA Translucent"),
+    ("Bambu Lab", "Orange", "#F74E02", "PLA Translucent"),
+    ("Bambu Lab", "Ice Blue", "#B8CDE9", "PLA Translucent"),
+    ("Bambu Lab", "Red", "#B50011", "PLA Translucent"),
+    ("Bambu Lab", "Lavender", "#B8ACD6", "PLA Translucent"),
+    # Bambu Lab PLA Glow (from store page)
+    ("Bambu Lab", "Glow Green", "#A1FFAC", "PLA Glow"),
+    ("Bambu Lab", "Glow Yellow", "#F8FF80", "PLA Glow"),
+    ("Bambu Lab", "Glow Pink", "#F17B8F", "PLA Glow"),
+    ("Bambu Lab", "Glow Blue", "#7AC0E9", "PLA Glow"),
+    ("Bambu Lab", "Glow Orange", "#FF9D5B", "PLA Glow"),
+    # Bambu Lab PLA Galaxy (from store page)
+    ("Bambu Lab", "Brown", "#684A43", "PLA Galaxy"),
+    ("Bambu Lab", "Green", "#3B665E", "PLA Galaxy"),
+    ("Bambu Lab", "Nebulae", "#424379", "PLA Galaxy"),
+    ("Bambu Lab", "Purple", "#594177", "PLA Galaxy"),
+    # Bambu Lab PLA Metal (from store page)
+    ("Bambu Lab", "Iridium Gold Metallic", "#B39B84", "PLA Metal"),
+    ("Bambu Lab", "Copper Brown Metallic", "#AA6443", "PLA Metal"),
+    ("Bambu Lab", "Oxide Green Metallic", "#1D7C6A", "PLA Metal"),
+    ("Bambu Lab", "Cobalt Blue Metallic", "#39699E", "PLA Metal"),
+    ("Bambu Lab", "Iron Gray Metallic", "#43403D", "PLA Metal"),
+    # Bambu Lab PLA Marble (from store page)
+    ("Bambu Lab", "White Marble", "#F7F3F0", "PLA Marble"),
+    ("Bambu Lab", "Red Granite", "#AD4E38", "PLA Marble"),
+    # Bambu Lab PLA Wood (from store page)
+    ("Bambu Lab", "Black Walnut", "#4F3F24", "PLA Wood"),
+    ("Bambu Lab", "Rosewood", "#4C241C", "PLA Wood"),
+    ("Bambu Lab", "Clay Brown", "#995F11", "PLA Wood"),
+    ("Bambu Lab", "Classic Birch", "#918669", "PLA Wood"),
+    ("Bambu Lab", "White Oak", "#D6CCA3", "PLA Wood"),
+    ("Bambu Lab", "Ochre Yellow", "#C98935", "PLA Wood"),
+    # Bambu Lab PLA Tough+ (from official hex code PDF)
+    ("Bambu Lab", "White", "#FFFFFF", "PLA Tough"),
+    ("Bambu Lab", "Gray", "#AFB1AE", "PLA Tough"),
+    ("Bambu Lab", "Black", "#000000", "PLA Tough"),
+    ("Bambu Lab", "Silver", "#959698", "PLA Tough"),
+    ("Bambu Lab", "Yellow", "#F4D53F", "PLA Tough"),
+    ("Bambu Lab", "Cyan", "#009BD8", "PLA Tough"),
+    ("Bambu Lab", "Orange", "#DC3A27", "PLA Tough"),
+    # Bambu Lab PLA-CF (from official hex code PDF)
+    ("Bambu Lab", "Burgundy Red", "#951E23", "PLA-CF"),
+    ("Bambu Lab", "Iris Purple", "#69398E", "PLA-CF"),
+    ("Bambu Lab", "Matcha Green", "#5C9748", "PLA-CF"),
+    ("Bambu Lab", "Jeans Blue", "#6E88BC", "PLA-CF"),
+    ("Bambu Lab", "Royal Blue", "#2842AD", "PLA-CF"),
+    ("Bambu Lab", "Lava Gray", "#4D5054", "PLA-CF"),
+    ("Bambu Lab", "Black", "#000000", "PLA-CF"),
+    # Bambu Lab ABS (from official hex code PDF)
+    ("Bambu Lab", "White", "#FFFFFF", "ABS"),
+    ("Bambu Lab", "Desert Tan", "#E8DBB7", "ABS"),
+    ("Bambu Lab", "Olive", "#789D4A", "ABS"),
+    ("Bambu Lab", "Azure", "#489FDF", "ABS"),
+    ("Bambu Lab", "Navy Blue", "#0C2340", "ABS"),
+    ("Bambu Lab", "Blue", "#0A2CA5", "ABS"),
+    ("Bambu Lab", "Tangerine Yellow", "#FFC72C", "ABS"),
+    ("Bambu Lab", "Orange", "#FF6A13", "ABS"),
+    ("Bambu Lab", "Red", "#D32941", "ABS"),
+    ("Bambu Lab", "Purple", "#AF1685", "ABS"),
+    ("Bambu Lab", "Silver", "#87909A", "ABS"),
+    ("Bambu Lab", "Black", "#000000", "ABS"),
+    # Bambu Lab ASA (from store page)
+    ("Bambu Lab", "White", "#FFFAF2", "ASA"),
+    ("Bambu Lab", "Gray", "#8A949E", "ASA"),
+    ("Bambu Lab", "Red", "#E02928", "ASA"),
+    ("Bambu Lab", "Green", "#00A6A0", "ASA"),
+    ("Bambu Lab", "Blue", "#2140B4", "ASA"),
+    ("Bambu Lab", "Black", "#000000", "ASA"),
+    # Bambu Lab PETG HF (from store page)
+    ("Bambu Lab", "Yellow", "#FFD00B", "PETG HF"),
+    ("Bambu Lab", "Orange", "#F75403", "PETG HF"),
+    ("Bambu Lab", "Green", "#00AE42", "PETG HF"),
+    ("Bambu Lab", "Red", "#EB3A3A", "PETG HF"),
+    ("Bambu Lab", "Blue", "#002E96", "PETG HF"),
+    ("Bambu Lab", "Black", "#000000", "PETG HF"),
+    ("Bambu Lab", "White", "#FFFFFF", "PETG HF"),
+    ("Bambu Lab", "Cream", "#F9DFB9", "PETG HF"),
+    ("Bambu Lab", "Lime Green", "#6EE53C", "PETG HF"),
+    ("Bambu Lab", "Forest Green", "#39541A", "PETG HF"),
+    ("Bambu Lab", "Lake Blue", "#1F79E5", "PETG HF"),
+    ("Bambu Lab", "Peanut Brown", "#875718", "PETG HF"),
+    ("Bambu Lab", "Gray", "#ADB1B2", "PETG HF"),
+    ("Bambu Lab", "Dark Gray", "#515151", "PETG HF"),
+    # Bambu Lab PETG Translucent (from store page)
+    ("Bambu Lab", "Translucent Gray", "#8E8E8E", "PETG Translucent"),
+    ("Bambu Lab", "Translucent Light Blue", "#61B0FF", "PETG Translucent"),
+    ("Bambu Lab", "Translucent Olive", "#748C45", "PETG Translucent"),
+    ("Bambu Lab", "Translucent Brown", "#C9A381", "PETG Translucent"),
+    ("Bambu Lab", "Translucent Teal", "#77EDD7", "PETG Translucent"),
+    ("Bambu Lab", "Translucent Orange", "#FF911A", "PETG Translucent"),
+    ("Bambu Lab", "Translucent Purple", "#D6ABFF", "PETG Translucent"),
+    ("Bambu Lab", "Translucent Pink", "#F9C1BD", "PETG Translucent"),
+    # Bambu Lab PETG-CF (from official hex code PDF)
+    ("Bambu Lab", "Brick Red", "#9F332A", "PETG-CF"),
+    ("Bambu Lab", "Violet Purple", "#583061", "PETG-CF"),
+    ("Bambu Lab", "Indigo Blue", "#324585", "PETG-CF"),
+    ("Bambu Lab", "Malachite Green", "#16B08E", "PETG-CF"),
+    ("Bambu Lab", "Black", "#000000", "PETG-CF"),
+    ("Bambu Lab", "Titan Gray", "#565656", "PETG-CF"),
+    # Bambu Lab TPU 95A HF (from store page)
+    ("Bambu Lab", "White", "#FFFFFF", "TPU 95A"),
+    ("Bambu Lab", "Yellow", "#F3E600", "TPU 95A"),
+    ("Bambu Lab", "Blue", "#0072CE", "TPU 95A"),
+    ("Bambu Lab", "Red", "#C8102E", "TPU 95A"),
+    ("Bambu Lab", "Gray", "#898D8D", "TPU 95A"),
+    ("Bambu Lab", "Black", "#101820", "TPU 95A"),
+    # Bambu Lab TPU 90A (from official hex code PDF)
+    ("Bambu Lab", "Black", "#000000", "TPU 90A"),
+    ("Bambu Lab", "White", "#FFFFFF", "TPU 90A"),
+    ("Bambu Lab", "Grape Jelly", "#D6ABFF", "TPU 90A"),
+    ("Bambu Lab", "Crystal Blue", "#7EB4E1", "TPU 90A"),
+    ("Bambu Lab", "Cocoa Brown", "#5C4738", "TPU 90A"),
+    # Bambu Lab PAHT-CF
+    ("Bambu Lab", "Black", "#1A1A1A", "PAHT-CF"),
+    # Bambu Lab Support Materials
+    ("Bambu Lab", "Natural", "#F5F5DC", "PLA Support"),
+    ("Bambu Lab", "Natural", "#F5F5DC", "PVA Support"),
+    # Polymaker PolyTerra PLA
+    ("Polymaker", "Cotton White", "#F5F5F5", "PolyTerra PLA"),
+    ("Polymaker", "Charcoal Black", "#2B2B2B", "PolyTerra PLA"),
+    ("Polymaker", "Marble White", "#E8E8E8", "PolyTerra PLA"),
+    ("Polymaker", "Fossil Grey", "#6B6B6B", "PolyTerra PLA"),
+    ("Polymaker", "Shadow Black", "#1A1A1A", "PolyTerra PLA"),
+    ("Polymaker", "Army Red", "#8B0000", "PolyTerra PLA"),
+    ("Polymaker", "Lava Red", "#CF1020", "PolyTerra PLA"),
+    ("Polymaker", "Sakura Pink", "#FFB7C5", "PolyTerra PLA"),
+    ("Polymaker", "Rose", "#FF007F", "PolyTerra PLA"),
+    ("Polymaker", "Peach", "#FFCBA4", "PolyTerra PLA"),
+    ("Polymaker", "Banana", "#FFE135", "PolyTerra PLA"),
+    ("Polymaker", "Savannah Yellow", "#F4C430", "PolyTerra PLA"),
+    ("Polymaker", "Sunrise Orange", "#FF6600", "PolyTerra PLA"),
+    ("Polymaker", "Muted Green", "#4F7942", "PolyTerra PLA"),
+    ("Polymaker", "Forest Green", "#228B22", "PolyTerra PLA"),
+    ("Polymaker", "Mint", "#98FF98", "PolyTerra PLA"),
+    ("Polymaker", "Lavender Purple", "#B57EDC", "PolyTerra PLA"),
+    ("Polymaker", "Sapphire Blue", "#0F52BA", "PolyTerra PLA"),
+    ("Polymaker", "Ice", "#D6ECEF", "PolyTerra PLA"),
+    # Prusament PLA
+    ("Prusament", "Jet Black", "#1A1A1A", "PLA"),
+    ("Prusament", "Galaxy Black", "#1F1F1F", "PLA"),
+    ("Prusament", "Pristine White", "#FFFFFF", "PLA"),
+    ("Prusament", "Gentleman's Grey", "#5A5A5A", "PLA"),
+    ("Prusament", "Lipstick Red", "#C21E1E", "PLA"),
+    ("Prusament", "Orange", "#FF6600", "PLA"),
+    ("Prusament", "Pineapple Yellow", "#FFD700", "PLA"),
+    ("Prusament", "Jungle Green", "#29AB87", "PLA"),
+    ("Prusament", "Azure Blue", "#007FFF", "PLA"),
+    ("Prusament", "Royal Blue", "#4169E1", "PLA"),
+    ("Prusament", "Mystic Purple", "#7B68EE", "PLA"),
+    # eSUN PLA+
+    ("eSUN", "White", "#FFFFFF", "PLA+"),
+    ("eSUN", "Black", "#000000", "PLA+"),
+    ("eSUN", "Grey", "#808080", "PLA+"),
+    ("eSUN", "Red", "#FF0000", "PLA+"),
+    ("eSUN", "Blue", "#0000FF", "PLA+"),
+    ("eSUN", "Green", "#00FF00", "PLA+"),
+    ("eSUN", "Yellow", "#FFFF00", "PLA+"),
+    ("eSUN", "Orange", "#FFA500", "PLA+"),
+    ("eSUN", "Purple", "#800080", "PLA+"),
+    ("eSUN", "Pink", "#FFC0CB", "PLA+"),
+    # Hatchbox PLA
+    ("Hatchbox", "White", "#FFFFFF", "PLA"),
+    ("Hatchbox", "Black", "#000000", "PLA"),
+    ("Hatchbox", "Gray", "#808080", "PLA"),
+    ("Hatchbox", "Red", "#FF0000", "PLA"),
+    ("Hatchbox", "Blue", "#0000FF", "PLA"),
+    ("Hatchbox", "Green", "#00FF00", "PLA"),
+    ("Hatchbox", "Yellow", "#FFFF00", "PLA"),
+    ("Hatchbox", "Orange", "#FFA500", "PLA"),
+    ("Hatchbox", "Purple", "#800080", "PLA"),
+    ("Hatchbox", "Pink", "#FFC0CB", "PLA"),
+    ("Hatchbox", "True Blue", "#0073CF", "PLA"),
+    ("Hatchbox", "True Green", "#008000", "PLA"),
+]

+ 1 - 1
backend/app/core/config.py

@@ -5,7 +5,7 @@ from pathlib import Path
 from pydantic_settings import BaseSettings
 
 # Application version - single source of truth
-APP_VERSION = "0.1.9"
+APP_VERSION = "0.2.0b"
 GITHUB_REPO = "maziggy/bambuddy"
 
 # App directory - where the application is installed (for static files)

+ 145 - 0
backend/app/core/database.py

@@ -1,14 +1,30 @@
+from sqlalchemy import event
 from sqlalchemy.exc import OperationalError
 from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
 from sqlalchemy.orm import DeclarativeBase
 
 from backend.app.core.config import settings
 
+
+def _set_sqlite_pragmas(dbapi_conn, connection_record):
+    """Set SQLite pragmas on each new connection for concurrency and performance."""
+    cursor = dbapi_conn.cursor()
+    # WAL mode allows concurrent readers + one writer (vs default DELETE mode which locks entirely)
+    cursor.execute("PRAGMA journal_mode = WAL")
+    # Wait up to 5 seconds when the database is locked instead of failing immediately
+    cursor.execute("PRAGMA busy_timeout = 5000")
+    cursor.execute("PRAGMA synchronous = NORMAL")
+    cursor.close()
+
+
 engine = create_async_engine(
     settings.database_url,
     echo=settings.debug,
 )
 
+# Register the pragma listener on the underlying sync engine
+event.listen(engine.sync_engine, "connect", _set_sqlite_pragmas)
+
 async_session = async_sessionmaker(
     engine,
     class_=AsyncSession,
@@ -29,6 +45,7 @@ async def reinitialize_database():
         settings.database_url,
         echo=settings.debug,
     )
+    event.listen(engine.sync_engine, "connect", _set_sqlite_pragmas)
     async_session = async_sessionmaker(
         engine,
         class_=AsyncSession,
@@ -59,6 +76,7 @@ async def init_db():
         ams_history,
         api_key,
         archive,
+        color_catalog,
         external_link,
         filament,
         github_backup,
@@ -78,6 +96,11 @@ async def init_db():
         settings,
         slot_preset,
         smart_plug,
+        spool,
+        spool_assignment,
+        spool_catalog,
+        spool_k_profile,
+        spool_usage_history,
         user,
     )
 
@@ -93,6 +116,10 @@ async def init_db():
     # Seed default groups and migrate existing users
     await seed_default_groups()
 
+    # Seed default catalog entries
+    await seed_spool_catalog()
+    await seed_color_catalog()
+
 
 async def run_migrations(conn):
     """Add new columns to existing tables if they don't exist."""
@@ -161,6 +188,13 @@ async def run_migrations(conn):
         # Column already exists
         pass
 
+    # Migration: Add is_deleted column to maintenance_types for soft-deletes
+    try:
+        await conn.execute(text("ALTER TABLE maintenance_types ADD COLUMN is_deleted BOOLEAN DEFAULT 0"))
+    except OperationalError:
+        # Column already exists
+        pass
+
     # Migration: Add custom_interval_type column to printer_maintenance
     try:
         await conn.execute(text("ALTER TABLE printer_maintenance ADD COLUMN custom_interval_type VARCHAR(20)"))
@@ -1118,6 +1152,63 @@ async def run_migrations(conn):
     except OperationalError:
         pass  # Already applied
 
+    # Migration: Add inventory spool tracking columns
+    try:
+        await conn.execute(text("ALTER TABLE spool ADD COLUMN added_full BOOLEAN"))
+    except OperationalError:
+        pass  # Already applied
+    try:
+        await conn.execute(text("ALTER TABLE spool ADD COLUMN last_used DATETIME"))
+    except OperationalError:
+        pass  # Already applied
+    try:
+        await conn.execute(text("ALTER TABLE spool ADD COLUMN encode_time DATETIME"))
+    except OperationalError:
+        pass  # Already applied
+
+    # Migration: Add RFID tag matching columns to spool
+    try:
+        await conn.execute(text("ALTER TABLE spool ADD COLUMN tag_uid VARCHAR(16)"))
+    except OperationalError:
+        pass  # Already applied
+    try:
+        await conn.execute(text("ALTER TABLE spool ADD COLUMN tray_uuid VARCHAR(32)"))
+    except OperationalError:
+        pass  # Already applied
+    try:
+        await conn.execute(text("ALTER TABLE spool ADD COLUMN data_origin VARCHAR(20)"))
+    except OperationalError:
+        pass  # Already applied
+    try:
+        await conn.execute(text("ALTER TABLE spool ADD COLUMN tag_type VARCHAR(20)"))
+    except OperationalError:
+        pass  # Already applied
+
+    # Migration: Create spool_usage_history table for filament consumption tracking
+    try:
+        await conn.execute(
+            text("""
+            CREATE TABLE IF NOT EXISTS spool_usage_history (
+                id INTEGER PRIMARY KEY AUTOINCREMENT,
+                spool_id INTEGER NOT NULL REFERENCES spool(id) ON DELETE CASCADE,
+                printer_id INTEGER REFERENCES printers(id) ON DELETE SET NULL,
+                print_name VARCHAR(500),
+                weight_used REAL NOT NULL DEFAULT 0,
+                percent_used INTEGER NOT NULL DEFAULT 0,
+                status VARCHAR(20) NOT NULL DEFAULT 'completed',
+                created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
+            )
+        """)
+        )
+    except OperationalError:
+        pass  # Already applied
+
+    # Migration: Add open_in_new_tab column to external_links
+    try:
+        await conn.execute(text("ALTER TABLE external_links ADD COLUMN open_in_new_tab BOOLEAN DEFAULT 0"))
+    except OperationalError:
+        pass  # Already applied
+
 
 async def seed_notification_templates():
     """Seed default notification templates if they don't exist."""
@@ -1286,3 +1377,57 @@ async def seed_default_groups():
                     logger.info("Migrated user '%s' to Operators group", user.username)
 
             await session.commit()
+
+
+async def seed_spool_catalog():
+    """Seed the spool catalog with default entries if empty."""
+    import logging
+
+    from sqlalchemy import func, select
+
+    from backend.app.core.catalog_defaults import DEFAULT_SPOOL_CATALOG
+    from backend.app.models.spool_catalog import SpoolCatalogEntry
+
+    logger = logging.getLogger(__name__)
+
+    async with async_session() as session:
+        result = await session.execute(select(func.count()).select_from(SpoolCatalogEntry))
+        count = result.scalar() or 0
+        if count > 0:
+            return  # Already seeded
+
+        for name, weight in DEFAULT_SPOOL_CATALOG:
+            session.add(SpoolCatalogEntry(name=name, weight=weight, is_default=True))
+        await session.commit()
+        logger.info("Seeded %d default spool catalog entries", len(DEFAULT_SPOOL_CATALOG))
+
+
+async def seed_color_catalog():
+    """Seed the color catalog with default entries if empty."""
+    import logging
+
+    from sqlalchemy import func, select
+
+    from backend.app.core.catalog_defaults import DEFAULT_COLOR_CATALOG
+    from backend.app.models.color_catalog import ColorCatalogEntry
+
+    logger = logging.getLogger(__name__)
+
+    async with async_session() as session:
+        result = await session.execute(select(func.count()).select_from(ColorCatalogEntry))
+        count = result.scalar() or 0
+        if count > 0:
+            return  # Already seeded
+
+        for manufacturer, color_name, hex_color, material in DEFAULT_COLOR_CATALOG:
+            session.add(
+                ColorCatalogEntry(
+                    manufacturer=manufacturer,
+                    color_name=color_name,
+                    hex_color=hex_color,
+                    material=material,
+                    is_default=True,
+                )
+            )
+        await session.commit()
+        logger.info("Seeded %d default color catalog entries", len(DEFAULT_COLOR_CATALOG))

+ 18 - 0
backend/app/core/permissions.py

@@ -62,6 +62,12 @@ class Permission(StrEnum):
     FILAMENTS_UPDATE = "filaments:update"
     FILAMENTS_DELETE = "filaments:delete"
 
+    # Inventory (Spool Inventory, Spool Catalog, Color Catalog)
+    INVENTORY_READ = "inventory:read"
+    INVENTORY_CREATE = "inventory:create"
+    INVENTORY_UPDATE = "inventory:update"
+    INVENTORY_DELETE = "inventory:delete"
+
     # Smart Plugs
     SMART_PLUGS_READ = "smart_plugs:read"
     SMART_PLUGS_CREATE = "smart_plugs:create"
@@ -201,6 +207,12 @@ PERMISSION_CATEGORIES = {
         Permission.FILAMENTS_UPDATE,
         Permission.FILAMENTS_DELETE,
     ],
+    "Inventory": [
+        Permission.INVENTORY_READ,
+        Permission.INVENTORY_CREATE,
+        Permission.INVENTORY_UPDATE,
+        Permission.INVENTORY_DELETE,
+    ],
     "Smart Plugs": [
         Permission.SMART_PLUGS_READ,
         Permission.SMART_PLUGS_CREATE,
@@ -335,6 +347,11 @@ DEFAULT_GROUPS = {
             Permission.FILAMENTS_CREATE.value,
             Permission.FILAMENTS_UPDATE.value,
             Permission.FILAMENTS_DELETE.value,
+            # Inventory - full access
+            Permission.INVENTORY_READ.value,
+            Permission.INVENTORY_CREATE.value,
+            Permission.INVENTORY_UPDATE.value,
+            Permission.INVENTORY_DELETE.value,
             # Smart Plugs - full access
             Permission.SMART_PLUGS_READ.value,
             Permission.SMART_PLUGS_CREATE.value,
@@ -390,6 +407,7 @@ DEFAULT_GROUPS = {
             Permission.LIBRARY_READ.value,
             Permission.PROJECTS_READ.value,
             Permission.FILAMENTS_READ.value,
+            Permission.INVENTORY_READ.value,
             Permission.SMART_PLUGS_READ.value,
             Permission.CAMERA_VIEW.value,
             Permission.MAINTENANCE_READ.value,

+ 365 - 16
backend/app/main.py

@@ -184,6 +184,7 @@ from backend.app.api.routes import (
     firmware,
     github_backup,
     groups,
+    inventory,
     kprofiles,
     library,
     local_presets,
@@ -216,6 +217,7 @@ from backend.app.services.bambu_mqtt import PrinterState
 from backend.app.services.github_backup import github_backup_service
 from backend.app.services.homeassistant import homeassistant_service
 from backend.app.services.mqtt_relay import mqtt_relay
+from backend.app.services.mqtt_smart_plug import mqtt_smart_plug_service
 from backend.app.services.notification_service import notification_service
 from backend.app.services.print_scheduler import scheduler as print_scheduler
 from backend.app.services.printer_manager import (
@@ -253,6 +255,10 @@ _last_progress_milestone: dict[int, int] = {}
 # This prevents sending duplicate notifications for the same error
 _notified_hms_errors: dict[int, set[str]] = {}
 
+# Track timelapse file baselines at print start: {printer_id: set of MP4 filenames}
+# Used for snapshot-diff detection at print completion
+_timelapse_baselines: dict[int, set[str]] = {}
+
 
 async def _get_plug_energy(plug, db) -> dict | None:
     """Get energy from plug regardless of type (Tasmota, Home Assistant, or MQTT).
@@ -329,12 +335,14 @@ async def on_printer_status_change(printer_id: int, state: PrinterState):
     bed_target = round(temps.get("bed_target", 0))
     nozzle_target = round(temps.get("nozzle_target", 0))
 
+    # Include tray_now and vt_tray hash so external spool changes trigger broadcasts
+    vt_tray_key = hash(str(state.raw_data.get("vt_tray", []))) if state.raw_data else 0
     status_key = (
         f"{state.connected}:{state.state}:{state.progress}:{state.layer_num}:"
         f"{nozzle_temp}:{bed_temp}:{nozzle_2_temp}:{chamber_temp}:"
         f"{state.stg_cur}:{bed_target}:{nozzle_target}:"
         f"{state.cooling_fan_speed}:{state.big_fan1_speed}:{state.big_fan2_speed}:"
-        f"{state.chamber_light}:{state.active_extruder}"
+        f"{state.chamber_light}:{state.active_extruder}:{state.tray_now}:{vt_tray_key}"
     )
 
     # MQTT relay - publish status (before dedup check - always publish to MQTT)
@@ -410,6 +418,9 @@ async def on_printer_status_change(printer_id: int, state: PrinterState):
         # Find new errors that haven't been notified yet
         new_error_codes = current_error_codes - previously_notified
 
+        # Update tracking immediately to prevent duplicate notifications from concurrent callbacks
+        _notified_hms_errors[printer_id] = current_error_codes
+
         if new_error_codes:
             # Get the actual new errors for the notification
             # Filter to severity >= 2 (skip informational/status messages like H2D sends)
@@ -480,8 +491,6 @@ async def on_printer_status_change(printer_id: int, state: PrinterState):
             except Exception as e:
                 logging.getLogger(__name__).warning(f"HMS error notification failed: {e}")
 
-            # Update tracking with all current errors
-            _notified_hms_errors[printer_id] = current_error_codes
     else:
         # No HMS errors - clear tracking so future errors get notified
         if printer_id in _notified_hms_errors:
@@ -493,6 +502,11 @@ async def on_printer_status_change(printer_id: int, state: PrinterState):
     )
 
 
+def _is_bambu_uuid(tray_uuid: str) -> bool:
+    """Check if a tray UUID looks like a valid Bambu Lab RFID UUID (non-empty, non-zero)."""
+    return bool(tray_uuid) and tray_uuid not in ("", "0" * len(tray_uuid))
+
+
 async def on_ams_change(printer_id: int, ams_data: list):
     """Handle AMS data changes - sync to Spoolman if enabled and auto mode."""
     logger = logging.getLogger(__name__)
@@ -518,6 +532,229 @@ async def on_ams_change(printer_id: int, ams_data: list):
     except Exception as e:
         logger.warning("Failed to broadcast AMS change for printer %s: %s", printer_id, e)
 
+    # Auto-unlink spool assignments with stale fingerprints
+    try:
+        async with async_session() as db:
+            from sqlalchemy.orm import selectinload
+
+            from backend.app.api.routes.inventory import _find_tray_in_ams_data
+            from backend.app.models.spool_assignment import SpoolAssignment as SA
+
+            result = await db.execute(select(SA).where(SA.printer_id == printer_id).options(selectinload(SA.spool)))
+            stale = []
+            for assignment in result.scalars().all():
+                current_tray = _find_tray_in_ams_data(ams_data, assignment.ams_id, assignment.tray_id)
+                if not current_tray:
+                    logger.info(
+                        "Auto-unlink: spool %d AMS%d-T%d — tray not found in AMS data (slot empty?)",
+                        assignment.spool_id,
+                        assignment.ams_id,
+                        assignment.tray_id,
+                    )
+                    stale.append(assignment)  # Slot empty
+                elif _is_bambu_uuid(current_tray.get("tray_uuid", "")):
+                    # A Bambu Lab spool is in this slot — check if it's the same spool
+                    # that's currently assigned. If yes, keep the assignment (avoids
+                    # unnecessary unlink/re-assign/ams_filament_setting cycle that clears
+                    # the printer's filament preset on every startup).
+                    tray_uuid = current_tray.get("tray_uuid", "")
+                    tag_uid = current_tray.get("tag_uid", "")
+                    spool = assignment.spool
+                    spool_matches = False
+                    if spool:
+                        if (spool.tray_uuid and spool.tray_uuid.upper() == tray_uuid.upper()) or (
+                            spool.tag_uid
+                            and tag_uid
+                            and tag_uid != "0000000000000000"
+                            and spool.tag_uid.upper() == tag_uid.upper()
+                        ):
+                            spool_matches = True
+                    if spool_matches:
+                        # Same BL spool still in slot — keep assignment, update fingerprint if needed
+                        cur_color = current_tray.get("tray_color", "")
+                        cur_type = current_tray.get("tray_type", "")
+                        fp_color = assignment.fingerprint_color or ""
+                        fp_type = assignment.fingerprint_type or ""
+                        if cur_color.upper() != fp_color.upper() or cur_type.upper() != fp_type.upper():
+                            assignment.fingerprint_color = cur_color
+                            assignment.fingerprint_type = cur_type
+                            logger.debug(
+                                "Auto-unlink: spool %d AMS%d-T%d — same BL spool, updated fingerprint",
+                                assignment.spool_id,
+                                assignment.ams_id,
+                                assignment.tray_id,
+                            )
+                        continue
+                    # Different BL spool or unrecognized — unlink so auto-assign can match
+                    logger.info(
+                        "Auto-unlink: spool %d AMS%d-T%d — different Bambu Lab spool detected (uuid=%s)",
+                        assignment.spool_id,
+                        assignment.ams_id,
+                        assignment.tray_id,
+                        tray_uuid,
+                    )
+                    stale.append(assignment)
+                else:
+                    cur_color = current_tray.get("tray_color", "")
+                    cur_type = current_tray.get("tray_type", "")
+                    fp_color = assignment.fingerprint_color or ""
+                    fp_type = assignment.fingerprint_type or ""
+                    if cur_color.upper() != fp_color.upper() or cur_type.upper() != fp_type.upper():
+                        # Fingerprint mismatch — but check if tray now matches the
+                        # assigned spool (e.g. auto-configure changed the tray).
+                        spool = assignment.spool
+                        if spool:
+                            spool_color = (spool.rgba or "FFFFFFFF").upper()
+                            spool_type = (spool.material or "").upper()
+                            if cur_color.upper() == spool_color and cur_type.upper() == spool_type:
+                                # Tray was reconfigured to match the spool — update fingerprint
+                                logger.info(
+                                    "Auto-unlink: spool %d AMS%d-T%d — fingerprint mismatch but tray matches spool, updating fp",
+                                    assignment.spool_id,
+                                    assignment.ams_id,
+                                    assignment.tray_id,
+                                )
+                                assignment.fingerprint_color = cur_color
+                                assignment.fingerprint_type = cur_type
+                                continue
+                        logger.info(
+                            "Auto-unlink: spool %d AMS%d-T%d — fingerprint mismatch (cur=%s/%s fp=%s/%s spool=%s/%s)",
+                            assignment.spool_id,
+                            assignment.ams_id,
+                            assignment.tray_id,
+                            cur_color,
+                            cur_type,
+                            fp_color,
+                            fp_type,
+                            spool.rgba if spool else "?",
+                            spool.material if spool else "?",
+                        )
+                        stale.append(assignment)  # Spool changed
+            for a in stale:
+                await db.delete(a)
+            if stale:
+                logger.info("Auto-unlinked %d stale spool assignments for printer %d", len(stale), printer_id)
+            # Commit any changes (stale deletions and/or fingerprint updates)
+            await db.commit()
+    except Exception as e:
+        logger.warning("Spool assignment cleanup failed: %s", e)
+
+    # Auto-manage inventory spools from AMS tray data (skip if Spoolman manages AMS)
+    try:
+        async with async_session() as db:
+            from backend.app.api.routes.settings import get_setting
+            from backend.app.models.spool_assignment import SpoolAssignment as SA
+            from backend.app.services.spool_tag_matcher import (
+                auto_assign_spool,
+                create_spool_from_tray,
+                get_spool_by_tag,
+                is_bambu_tag,
+                is_valid_tag,
+            )
+
+            _spoolman_on = await get_setting(db, "spoolman_enabled")
+            if not _spoolman_on or _spoolman_on.lower() != "true":
+                for ams_unit in ams_data:
+                    if not isinstance(ams_unit, dict):
+                        continue
+                    ams_id = int(ams_unit.get("id", 0))
+                    for tray in ams_unit.get("tray", []):
+                        if not isinstance(tray, dict):
+                            continue
+                        tray_id = int(tray.get("id", 0))
+                        tag_uid = tray.get("tag_uid", "")
+                        tray_uuid = tray.get("tray_uuid", "")
+                        tray_info_idx = tray.get("tray_info_idx", "")
+                        if not tray.get("tray_type"):
+                            continue  # Empty slot
+                        # Check if assignment already exists for this slot
+                        existing = await db.execute(
+                            select(SA)
+                            .options(selectinload(SA.spool))
+                            .where(SA.printer_id == printer_id, SA.ams_id == ams_id, SA.tray_id == tray_id)
+                        )
+                        existing_assignment = existing.scalar_one_or_none()
+                        if existing_assignment:
+                            # Sync spool weight_used from AMS remain if valid
+                            remain_raw = tray.get("remain")
+                            if remain_raw is not None and existing_assignment.spool:
+                                try:
+                                    remain_val = int(remain_raw)
+                                except (TypeError, ValueError):
+                                    remain_val = -1
+                                if 0 <= remain_val <= 100:
+                                    lw = existing_assignment.spool.label_weight or 1000
+                                    new_used = round(lw * (100 - remain_val) / 100.0, 1)
+                                    if abs((existing_assignment.spool.weight_used or 0) - new_used) > 1:
+                                        logger.info(
+                                            "Weight sync: spool %d weight_used %s -> %s (remain=%d)",
+                                            existing_assignment.spool_id,
+                                            existing_assignment.spool.weight_used,
+                                            new_used,
+                                            remain_val,
+                                        )
+                                        existing_assignment.spool.weight_used = new_used
+                                        await db.commit()
+                            continue
+
+                        if is_bambu_tag(tag_uid, tray_uuid, tray_info_idx):
+                            # BL spool with RFID tag: auto-match or auto-create
+                            spool = await get_spool_by_tag(db, tag_uid, tray_uuid)
+                            if not spool:
+                                spool = await create_spool_from_tray(db, tray)
+                            await auto_assign_spool(
+                                printer_id,
+                                ams_id,
+                                tray_id,
+                                spool,
+                                printer_manager,
+                                db,
+                                tray_info_idx=tray_info_idx,
+                            )
+                            await db.commit()
+                            await ws_manager.broadcast(
+                                {
+                                    "type": "spool_auto_assigned",
+                                    "printer_id": printer_id,
+                                    "ams_id": ams_id,
+                                    "tray_id": tray_id,
+                                    "spool_id": spool.id,
+                                }
+                            )
+                            logger.info(
+                                "RFID auto-assigned spool %d to printer %d AMS%d-T%d",
+                                spool.id,
+                                printer_id,
+                                ams_id,
+                                tray_id,
+                            )
+                        elif is_valid_tag(tag_uid, tray_uuid):
+                            # Non-BL spool with some tag — let user choose
+                            await ws_manager.broadcast(
+                                {
+                                    "type": "unknown_tag",
+                                    "printer_id": printer_id,
+                                    "ams_id": ams_id,
+                                    "tray_id": tray_id,
+                                    "tag_uid": tag_uid,
+                                    "tray_uuid": tray_uuid,
+                                }
+                            )
+                        else:
+                            # No tag at all — let user choose from inventory
+                            await ws_manager.broadcast(
+                                {
+                                    "type": "unknown_tag",
+                                    "printer_id": printer_id,
+                                    "ams_id": ams_id,
+                                    "tray_id": tray_id,
+                                    "tag_uid": "",
+                                    "tray_uuid": "",
+                                }
+                            )
+    except Exception as e:
+        logger.warning("RFID spool auto-assign failed: %s", e)
+
     try:
         async with async_session() as db:
             from backend.app.api.routes.settings import get_setting
@@ -571,6 +808,26 @@ async def on_ams_change(printer_id: int, ams_data: list):
                 )
                 return
 
+            # Load inventory weights as fallback (when AMS MQTT data lacks remain values)
+            from sqlalchemy.orm import selectinload
+
+            from backend.app.models.spool_assignment import SpoolAssignment
+
+            inventory_weights: dict[tuple[int, int], float] = {}
+            try:
+                assign_result = await db.execute(
+                    select(SpoolAssignment)
+                    .options(selectinload(SpoolAssignment.spool))
+                    .where(SpoolAssignment.printer_id == printer_id)
+                )
+                for assignment in assign_result.scalars().all():
+                    spool = assignment.spool
+                    if spool and spool.label_weight > 0:
+                        remaining = max(0.0, spool.label_weight - (spool.weight_used or 0))
+                        inventory_weights[(assignment.ams_id, assignment.tray_id)] = remaining
+            except Exception as e:
+                logger.debug("Could not load inventory weights for printer %s: %s", printer_id, e)
+
             # Sync each AMS tray
             synced = 0
             for ams_unit in ams_data:
@@ -583,11 +840,13 @@ async def on_ams_change(printer_id: int, ams_data: list):
                         continue  # Empty tray
 
                     try:
+                        inv_remaining = inventory_weights.get((ams_id, tray.tray_id))
                         result = await client.sync_ams_tray(
                             tray,
                             printer_name,
                             disable_weight_sync=disable_weight_sync,
                             cached_spools=cached_spools,
+                            inventory_remaining=inv_remaining,
                         )
                         if result:
                             synced += 1
@@ -749,6 +1008,19 @@ async def on_print_start(printer_id: int, data: dict):
     except Exception:
         pass  # Don't fail print start callback if MQTT fails
 
+    # Capture AMS tray remain% for filament consumption tracking (skip if Spoolman handles usage)
+    try:
+        async with async_session() as db:
+            from backend.app.api.routes.settings import get_setting
+
+            _spoolman_on = await get_setting(db, "spoolman_enabled")
+        if not _spoolman_on or _spoolman_on.lower() != "true":
+            from backend.app.services.usage_tracker import on_print_start as usage_on_print_start
+
+            await usage_on_print_start(printer_id, data, printer_manager)
+    except Exception as e:
+        logger.warning("Usage tracker on_print_start failed: %s", e)
+
     # Track if notification was sent (to avoid sending twice)
     notification_sent = False
 
@@ -1404,6 +1676,18 @@ async def on_print_start(printer_id: int, data: dict):
                     await _store_spoolman_print_data(printer_id, archive.id, archive.file_path, db, printer_manager)
                 except Exception as e:
                     logger.warning("[SPOOLMAN] Failed to store tracking data: %s", e)
+
+                # Capture timelapse file baseline for snapshot-diff on completion
+                try:
+                    baseline_files, _ = await _list_timelapse_mp4s(printer)
+                    _timelapse_baselines[printer_id] = {f.get("name", "") for f in baseline_files}
+                    logger.info(
+                        "[TIMELAPSE] Baseline at print start: %s MP4 files for printer %s",
+                        len(_timelapse_baselines[printer_id]),
+                        printer_id,
+                    )
+                except Exception as e:
+                    logger.warning("[TIMELAPSE] Failed to capture baseline at print start: %s", e)
         finally:
             if temp_path and temp_path.exists():
                 temp_path.unlink()
@@ -1435,7 +1719,7 @@ async def _list_timelapse_mp4s(printer) -> tuple[list[dict], str | None]:
     return [], None
 
 
-async def _scan_for_timelapse_with_retries(archive_id: int):
+async def _scan_for_timelapse_with_retries(archive_id: int, baseline_names: set[str] | None = None):
     """
     Scan for timelapse with retries using a snapshot-diff approach.
 
@@ -1443,6 +1727,10 @@ async def _scan_for_timelapse_with_retries(archive_id: int):
     clock is wrong in LAN-only mode), we snapshot existing MP4 filenames BEFORE
     waiting, then look for any NEW filename that appears after each delay.
 
+    If baseline_names is provided (captured at print start), it is used directly.
+    Otherwise falls back to taking a baseline at completion time (best-effort
+    for prints started before app restart).
+
     Falls back to name-matching (print name contained in MP4 filename) if no
     new file appears after all retries.
     """
@@ -1468,18 +1756,28 @@ async def _scan_for_timelapse_with_retries(archive_id: int):
                 logger.warning("[TIMELAPSE] Archive %s has no printer, aborting", archive_id)
                 return
 
-            result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
-            printer = result.scalar_one_or_none()
-            if not printer:
-                logger.warning("[TIMELAPSE] Printer not found for archive %s, aborting", archive_id)
-                return
+            if baseline_names is not None:
+                # Use pre-captured baseline from print start (no race condition)
+                logger.info(
+                    "[TIMELAPSE] Using print-start baseline: %s existing MP4 files for archive %s",
+                    len(baseline_names),
+                    archive_id,
+                )
+            else:
+                # Fallback: take baseline now (e.g. app restarted mid-print)
+                result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
+                printer = result.scalar_one_or_none()
+                if not printer:
+                    logger.warning("[TIMELAPSE] Printer not found for archive %s, aborting", archive_id)
+                    return
 
-            # Snapshot current MP4 filenames as baseline
-            baseline_files, _ = await _list_timelapse_mp4s(printer)
-            baseline_names: set[str] = {f.get("name", "") for f in baseline_files}
-            logger.info(
-                "[TIMELAPSE] Baseline snapshot: %s existing MP4 files for archive %s", len(baseline_names), archive_id
-            )
+                baseline_files, _ = await _list_timelapse_mp4s(printer)
+                baseline_names = {f.get("name", "") for f in baseline_files}
+                logger.info(
+                    "[TIMELAPSE] Baseline snapshot (fallback): %s existing MP4 files for archive %s",
+                    len(baseline_names),
+                    archive_id,
+                )
 
             # Derive base_name for name-matching fallback
             base_name = Path(archive.filename).stem if archive.filename else ""
@@ -1837,6 +2135,32 @@ async def on_print_complete(printer_id: int, data: dict):
 
     log_timing("Archive status update")
 
+    # Track filament consumption from AMS remain% deltas (skip if Spoolman handles usage)
+    usage_results: list[dict] = []
+    try:
+        async with async_session() as db:
+            from backend.app.api.routes.settings import get_setting
+
+            _spoolman_on = await get_setting(db, "spoolman_enabled")
+        if not _spoolman_on or _spoolman_on.lower() != "true":
+            from backend.app.services.usage_tracker import on_print_complete as usage_on_print_complete
+
+            async with async_session() as db:
+                usage_results = await usage_on_print_complete(
+                    printer_id, data, printer_manager, db, archive_id=archive_id
+                )
+                if usage_results:
+                    await ws_manager.broadcast(
+                        {
+                            "type": "spool_usage_logged",
+                            "printer_id": printer_id,
+                            "usage": usage_results,
+                        }
+                    )
+                    log_timing("Usage tracker")
+    except Exception as e:
+        logger.warning("Usage tracker on_print_complete failed: %s", e)
+
     # Report filament usage to Spoolman if print completed successfully
     if data.get("status") == "completed":
         try:
@@ -2028,6 +2352,25 @@ async def on_print_complete(printer_id: int, data: dict):
                             "actual_filament_grams": archive.filament_used_grams,
                             "failure_reason": archive.failure_reason,
                         }
+
+                        # Scale filament usage for partial prints
+                        if print_status != "completed" and archive.filament_used_grams:
+                            progress = data.get("progress") or 0
+                            scale = max(0.0, min(progress / 100.0, 1.0))
+                            archive_data["actual_filament_grams"] = round(archive.filament_used_grams * scale, 1)
+                            archive_data["progress"] = progress
+
+                        # Pass per-slot data from archive.extra_data
+                        if archive.extra_data and archive.extra_data.get("filament_slots"):
+                            slots = archive.extra_data["filament_slots"]
+                            if print_status != "completed":
+                                scale = max(0.0, min((data.get("progress") or 0) / 100.0, 1.0))
+                                slots = [{**s, "used_g": round(s["used_g"] * scale, 1)} for s in slots]
+                            archive_data["filament_slots"] = slots
+
+                        # Pass usage tracker results for AMS slot info in notifications
+                        if usage_results:
+                            archive_data["usage_results"] = usage_results
                         # Add finish photo URL and image bytes if available
                         if finish_photo_filename:
                             from backend.app.api.routes.settings import get_setting
@@ -2179,7 +2522,8 @@ async def on_print_complete(printer_id: int, data: dict):
         logger.info("[TIMELAPSE] Timelapse was active during print, scheduling auto-scan for archive %s", archive_id)
         # Schedule timelapse scan as background task with retries
         # The printer needs time to encode the video after print completion
-        asyncio.create_task(_scan_for_timelapse_with_retries(archive_id))
+        baseline = _timelapse_baselines.pop(printer_id, None)
+        asyncio.create_task(_scan_for_timelapse_with_retries(archive_id, baseline))
         log_timing("Timelapse scan scheduled")
 
     # Update queue item if this was a scheduled print
@@ -2746,6 +3090,10 @@ async def lifespan(app: FastAPI):
     if virtual_printer_manager.is_enabled:
         await virtual_printer_manager.configure(enabled=False)
 
+    await mqtt_smart_plug_service.disconnect(timeout=2)
+
+    await mqtt_relay.disconnect(timeout=2)
+
 
 app = FastAPI(
     title=app_settings.app_name,
@@ -2905,6 +3253,7 @@ app.include_router(groups.router, prefix=app_settings.api_prefix)
 app.include_router(printers.router, prefix=app_settings.api_prefix)
 app.include_router(archives.router, prefix=app_settings.api_prefix)
 app.include_router(filaments.router, prefix=app_settings.api_prefix)
+app.include_router(inventory.router, prefix=app_settings.api_prefix)
 app.include_router(settings_routes.router, prefix=app_settings.api_prefix)
 app.include_router(cloud.router, prefix=app_settings.api_prefix)
 app.include_router(local_presets.router, prefix=app_settings.api_prefix)

+ 12 - 0
backend/app/models/__init__.py

@@ -1,6 +1,7 @@
 from backend.app.models.ams_history import AMSSensorHistory
 from backend.app.models.api_key import APIKey
 from backend.app.models.archive import PrintArchive
+from backend.app.models.color_catalog import ColorCatalogEntry
 from backend.app.models.filament import Filament
 from backend.app.models.github_backup import GitHubBackupConfig, GitHubBackupLog
 from backend.app.models.group import Group, user_groups
@@ -16,6 +17,11 @@ from backend.app.models.printer import Printer
 from backend.app.models.project import Project
 from backend.app.models.settings import Settings
 from backend.app.models.smart_plug import SmartPlug
+from backend.app.models.spool import Spool
+from backend.app.models.spool_assignment import SpoolAssignment
+from backend.app.models.spool_catalog import SpoolCatalogEntry
+from backend.app.models.spool_k_profile import SpoolKProfile
+from backend.app.models.spool_usage_history import SpoolUsageHistory
 from backend.app.models.user import User
 
 __all__ = [
@@ -43,4 +49,10 @@ __all__ = [
     "GitHubBackupLog",
     "LocalPreset",
     "OrcaBaseProfile",
+    "Spool",
+    "SpoolKProfile",
+    "SpoolAssignment",
+    "SpoolCatalogEntry",
+    "SpoolUsageHistory",
+    "ColorCatalogEntry",
 ]

+ 20 - 0
backend/app/models/color_catalog.py

@@ -0,0 +1,20 @@
+from datetime import datetime
+
+from sqlalchemy import Boolean, DateTime, String, func
+from sqlalchemy.orm import Mapped, mapped_column
+
+from backend.app.core.database import Base
+
+
+class ColorCatalogEntry(Base):
+    """Color catalog entry for automatic color lookup when adding spools."""
+
+    __tablename__ = "color_catalog"
+
+    id: Mapped[int] = mapped_column(primary_key=True)
+    manufacturer: Mapped[str] = mapped_column(String(200))
+    color_name: Mapped[str] = mapped_column(String(200))
+    hex_color: Mapped[str] = mapped_column(String(7))  # #RRGGBB
+    material: Mapped[str | None] = mapped_column(String(100))
+    is_default: Mapped[bool] = mapped_column(Boolean, default=False)
+    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

+ 2 - 1
backend/app/models/external_link.py

@@ -1,6 +1,6 @@
 from datetime import datetime
 
-from sqlalchemy import DateTime, Integer, String, func
+from sqlalchemy import Boolean, DateTime, Integer, String, func
 from sqlalchemy.orm import Mapped, mapped_column
 
 from backend.app.core.database import Base
@@ -16,6 +16,7 @@ class ExternalLink(Base):
     url: Mapped[str] = mapped_column(String(500))
     icon: Mapped[str] = mapped_column(String(50), default="link")
     custom_icon: Mapped[str | None] = mapped_column(String(255), nullable=True)  # Filename of uploaded icon
+    open_in_new_tab: Mapped[bool] = mapped_column(Boolean, default=False)
     sort_order: Mapped[int] = mapped_column(Integer, default=0)
     created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
     updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())

+ 1 - 0
backend/app/models/maintenance.py

@@ -22,6 +22,7 @@ class MaintenanceType(Base):
     icon: Mapped[str | None] = mapped_column(String(50))  # Icon name for UI
     wiki_url: Mapped[str | None] = mapped_column(String(500))  # Documentation link
     is_system: Mapped[bool] = mapped_column(Boolean, default=False)  # Pre-defined vs custom
+    is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)  # Hidden/removed type
     created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
 
     # Relationships

+ 44 - 0
backend/app/models/spool.py

@@ -0,0 +1,44 @@
+from datetime import datetime
+
+from sqlalchemy import DateTime, Float, Integer, String, func
+from sqlalchemy.orm import Mapped, mapped_column, relationship
+
+from backend.app.core.database import Base
+
+
+class Spool(Base):
+    """Spool inventory item for tracking filament spools and their properties."""
+
+    __tablename__ = "spool"
+
+    id: Mapped[int] = mapped_column(primary_key=True)
+    material: Mapped[str] = mapped_column(String(50))  # PLA, PETG, ABS, etc.
+    subtype: Mapped[str | None] = mapped_column(String(50))  # Basic, Matte, Silk, etc.
+    color_name: Mapped[str | None] = mapped_column(String(100))  # "Jade White"
+    rgba: Mapped[str | None] = mapped_column(String(8))  # RRGGBBAA hex
+    brand: Mapped[str | None] = mapped_column(String(100))  # "Polymaker"
+    label_weight: Mapped[int] = mapped_column(Integer, default=1000)  # Advertised net weight (g)
+    core_weight: Mapped[int] = mapped_column(Integer, default=250)  # Empty spool weight (g)
+    weight_used: Mapped[float] = mapped_column(Float, default=0)  # Consumed grams
+    slicer_filament: Mapped[str | None] = mapped_column(String(50))  # Preset ID (e.g. "GFL99")
+    slicer_filament_name: Mapped[str | None] = mapped_column(String(100))  # Preset name for slicer
+    nozzle_temp_min: Mapped[int | None] = mapped_column()  # Override min temp
+    nozzle_temp_max: Mapped[int | None] = mapped_column()  # Override max temp
+    note: Mapped[str | None] = mapped_column(String(500))
+    added_full: Mapped[bool | None] = mapped_column()  # Whether spool was added as full (unused)
+    last_used: Mapped[datetime | None] = mapped_column(DateTime)  # Last time this spool was used in a print
+    encode_time: Mapped[datetime | None] = mapped_column(DateTime)  # When spool was encoded/written to tag
+    tag_uid: Mapped[str | None] = mapped_column(String(16))  # RFID tag UID (16 hex chars)
+    tray_uuid: Mapped[str | None] = mapped_column(String(32))  # Bambu Lab spool UUID (32 hex chars)
+    data_origin: Mapped[str | None] = mapped_column(String(20))  # How data was populated: manual, rfid_auto, nfc_link
+    tag_type: Mapped[str | None] = mapped_column(String(20))  # Tag vendor: bambulab, generic, etc.
+    archived_at: Mapped[datetime | None] = mapped_column(DateTime)  # NULL = active
+    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
+    updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
+
+    k_profiles: Mapped[list["SpoolKProfile"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
+    assignments: Mapped[list["SpoolAssignment"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
+
+
+from backend.app.models.spool_assignment import SpoolAssignment  # noqa: E402
+from backend.app.models.spool_k_profile import SpoolKProfile  # noqa: E402

+ 35 - 0
backend/app/models/spool_assignment.py

@@ -0,0 +1,35 @@
+from datetime import datetime
+
+from sqlalchemy import DateTime, ForeignKey, Integer, String, UniqueConstraint, func
+from sqlalchemy.orm import Mapped, mapped_column, relationship
+
+from backend.app.core.database import Base
+
+
+class SpoolAssignment(Base):
+    """Assignment of a spool to a specific AMS slot on a printer."""
+
+    __tablename__ = "spool_assignment"
+
+    id: Mapped[int] = mapped_column(primary_key=True)
+    spool_id: Mapped[int] = mapped_column(ForeignKey("spool.id", ondelete="CASCADE"))
+    printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"))
+    ams_id: Mapped[int] = mapped_column(Integer)  # 0-3, 128+ (HT), 254/255 (ext)
+    tray_id: Mapped[int] = mapped_column(Integer)  # 0-3
+    fingerprint_color: Mapped[str | None] = mapped_column(String(8))  # tray_color snapshot
+    fingerprint_type: Mapped[str | None] = mapped_column(String(50))  # tray_type snapshot
+    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
+
+    spool: Mapped["Spool"] = relationship(back_populates="assignments")
+    printer: Mapped["Printer"] = relationship()
+
+    __table_args__ = (UniqueConstraint("printer_id", "ams_id", "tray_id"),)
+
+    @property
+    def printer_name(self) -> str | None:
+        """Get printer name from loaded relationship."""
+        return self.printer.name if self.printer else None
+
+
+from backend.app.models.printer import Printer  # noqa: E402, F401
+from backend.app.models.spool import Spool  # noqa: E402, F401

+ 18 - 0
backend/app/models/spool_catalog.py

@@ -0,0 +1,18 @@
+from datetime import datetime
+
+from sqlalchemy import Boolean, DateTime, Integer, String, func
+from sqlalchemy.orm import Mapped, mapped_column
+
+from backend.app.core.database import Base
+
+
+class SpoolCatalogEntry(Base):
+    """Spool weight catalog entry for weight lookup when adding spools."""
+
+    __tablename__ = "spool_catalog"
+
+    id: Mapped[int] = mapped_column(primary_key=True)
+    name: Mapped[str] = mapped_column(String(200))
+    weight: Mapped[int] = mapped_column(Integer)
+    is_default: Mapped[bool] = mapped_column(Boolean, default=False)
+    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

+ 31 - 0
backend/app/models/spool_k_profile.py

@@ -0,0 +1,31 @@
+from datetime import datetime
+
+from sqlalchemy import DateTime, Float, ForeignKey, Integer, String, func
+from sqlalchemy.orm import Mapped, mapped_column, relationship
+
+from backend.app.core.database import Base
+
+
+class SpoolKProfile(Base):
+    """K-value calibration profile for a spool on a specific printer/nozzle combo."""
+
+    __tablename__ = "spool_k_profile"
+
+    id: Mapped[int] = mapped_column(primary_key=True)
+    spool_id: Mapped[int] = mapped_column(ForeignKey("spool.id", ondelete="CASCADE"))
+    printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"))
+    extruder: Mapped[int] = mapped_column(Integer, default=0)  # 0 or 1 (H2D)
+    nozzle_diameter: Mapped[str] = mapped_column(String(10), default="0.4")  # "0.4", "0.6"
+    nozzle_type: Mapped[str | None] = mapped_column(String(50))
+    k_value: Mapped[float] = mapped_column(Float)  # e.g. 0.020
+    name: Mapped[str | None] = mapped_column(String(100))  # Profile display name
+    cali_idx: Mapped[int | None] = mapped_column(Integer)  # Calibration index on printer
+    setting_id: Mapped[str | None] = mapped_column(String(50))  # Full setting ID
+    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
+
+    spool: Mapped["Spool"] = relationship(back_populates="k_profiles")
+    printer: Mapped["Printer"] = relationship()
+
+
+from backend.app.models.printer import Printer  # noqa: E402, F401
+from backend.app.models.spool import Spool  # noqa: E402, F401

+ 21 - 0
backend/app/models/spool_usage_history.py

@@ -0,0 +1,21 @@
+from datetime import datetime
+
+from sqlalchemy import DateTime, Float, ForeignKey, Integer, String, func
+from sqlalchemy.orm import Mapped, mapped_column
+
+from backend.app.core.database import Base
+
+
+class SpoolUsageHistory(Base):
+    """Record of filament consumption for a spool during a print."""
+
+    __tablename__ = "spool_usage_history"
+
+    id: Mapped[int] = mapped_column(primary_key=True)
+    spool_id: Mapped[int] = mapped_column(ForeignKey("spool.id", ondelete="CASCADE"))
+    printer_id: Mapped[int | None] = mapped_column(ForeignKey("printers.id", ondelete="SET NULL"))
+    print_name: Mapped[str | None] = mapped_column(String(500))
+    weight_used: Mapped[float] = mapped_column(Float, default=0)
+    percent_used: Mapped[int] = mapped_column(Integer, default=0)
+    status: Mapped[str] = mapped_column(String(20), default="completed")  # completed/failed/aborted
+    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

+ 1 - 0
backend/app/schemas/cloud.py

@@ -49,6 +49,7 @@ class SlicerSetting(BaseModel):
     version: str | None = None
     user_id: str | None = None
     updated_time: str | None = None
+    is_custom: bool = False
 
 
 class SlicerSettingsResponse(BaseModel):

+ 3 - 0
backend/app/schemas/external_link.py

@@ -9,6 +9,7 @@ class ExternalLinkBase(BaseModel):
     name: str = Field(..., min_length=1, max_length=50, description="Display name for the link")
     url: str = Field(..., min_length=1, max_length=500, description="External URL")
     icon: str = Field(default="link", max_length=50, description="Lucide icon name")
+    open_in_new_tab: bool = False
 
     @field_validator("url")
     @classmethod
@@ -31,6 +32,7 @@ class ExternalLinkUpdate(BaseModel):
     name: str | None = Field(default=None, min_length=1, max_length=50)
     url: str | None = Field(default=None, min_length=1, max_length=500)
     icon: str | None = Field(default=None, max_length=50)
+    open_in_new_tab: bool | None = None
 
     @field_validator("url")
     @classmethod
@@ -45,6 +47,7 @@ class ExternalLinkResponse(ExternalLinkBase):
     """Response schema for external links."""
 
     id: int
+    open_in_new_tab: bool
     custom_icon: str | None = None
     sort_order: int
     created_at: datetime

+ 31 - 2
backend/app/schemas/notification_template.py

@@ -31,12 +31,34 @@ EVENT_VARIABLES: dict[str, list[str]] = {
         "filename",
         "duration",
         "filament_grams",
+        "filament_details",
+        "finish_photo_url",
+        "timestamp",
+        "app_name",
+    ],
+    "print_failed": [
+        "printer",
+        "filename",
+        "duration",
+        "filament_grams",
+        "filament_details",
+        "progress",
+        "reason",
+        "finish_photo_url",
+        "timestamp",
+        "app_name",
+    ],
+    "print_stopped": [
+        "printer",
+        "filename",
+        "duration",
+        "filament_grams",
+        "filament_details",
+        "progress",
         "finish_photo_url",
         "timestamp",
         "app_name",
     ],
-    "print_failed": ["printer", "filename", "duration", "reason", "finish_photo_url", "timestamp", "app_name"],
-    "print_stopped": ["printer", "filename", "duration", "finish_photo_url", "timestamp", "app_name"],
     "print_progress": ["printer", "filename", "progress", "remaining_time", "timestamp", "app_name"],
     "printer_offline": ["printer", "timestamp", "app_name"],
     "printer_error": ["printer", "error_type", "error_detail", "timestamp", "app_name"],
@@ -72,6 +94,7 @@ SAMPLE_DATA: dict[str, dict[str, str]] = {
         "filename": "Benchy.3mf",
         "duration": "1h 18m",
         "filament_grams": "15.2",
+        "filament_details": "AMS-A T1 PLA: 12.4g | AMS-A T3 PETG: 2.8g",
         "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_154800_abc12345.jpg",
         "timestamp": "2024-01-15 15:48",
         "app_name": "Bambuddy",
@@ -80,6 +103,9 @@ SAMPLE_DATA: dict[str, dict[str, str]] = {
         "printer": "Bambu X1C",
         "filename": "Benchy.3mf",
         "duration": "0h 45m",
+        "filament_grams": "7.6",
+        "filament_details": "AMS-A T1 PLA: 7.6g",
+        "progress": "50",
         "reason": "Filament runout",
         "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_151500_def67890.jpg",
         "timestamp": "2024-01-15 15:15",
@@ -89,6 +115,9 @@ SAMPLE_DATA: dict[str, dict[str, str]] = {
         "printer": "Bambu X1C",
         "filename": "Benchy.3mf",
         "duration": "0h 30m",
+        "filament_grams": "4.6",
+        "filament_details": "AMS-A T2 PLA: 4.6g",
+        "progress": "30",
         "finish_photo_url": "/api/v1/archives/123/photos/finish_20240115_150000_ghi11223.jpg",
         "timestamp": "2024-01-15 15:00",
         "app_name": "Bambuddy",

+ 1 - 1
backend/app/schemas/printer.py

@@ -199,7 +199,7 @@ class PrinterStatus(BaseModel):
     hms_errors: list[HMSErrorResponse] = []
     ams: list[AMSUnit] = []
     ams_exists: bool = False
-    vt_tray: AMSTray | None = None  # Virtual tray / external spool
+    vt_tray: list[AMSTray] = []  # Virtual tray / external spool(s)
     sdcard: bool = False  # SD card inserted
     store_to_sdcard: bool = False  # Store sent files on SD card
     timelapse: bool = False  # Timelapse recording active

+ 109 - 0
backend/app/schemas/spool.py

@@ -0,0 +1,109 @@
+from datetime import datetime
+
+from pydantic import BaseModel, Field
+
+
+class SpoolBase(BaseModel):
+    material: str = Field(..., min_length=1, max_length=50)
+    subtype: str | None = None
+    color_name: str | None = None
+    rgba: str | None = Field(None, pattern=r"^[0-9A-Fa-f]{8}$")
+    brand: str | None = None
+    label_weight: int = 1000
+    core_weight: int = 250
+    weight_used: float = 0
+    slicer_filament: str | None = None
+    slicer_filament_name: str | None = None
+    nozzle_temp_min: int | None = None
+    nozzle_temp_max: int | None = None
+    note: str | None = None
+    tag_uid: str | None = None
+    tray_uuid: str | None = None
+    data_origin: str | None = None
+    tag_type: str | None = None
+
+
+class SpoolCreate(SpoolBase):
+    pass
+
+
+class SpoolUpdate(BaseModel):
+    material: str | None = None
+    subtype: str | None = None
+    color_name: str | None = None
+    rgba: str | None = None
+    brand: str | None = None
+    label_weight: int | None = None
+    core_weight: int | None = None
+    weight_used: float | None = None
+    slicer_filament: str | None = None
+    slicer_filament_name: str | None = None
+    nozzle_temp_min: int | None = None
+    nozzle_temp_max: int | None = None
+    note: str | None = None
+    tag_uid: str | None = None
+    tray_uuid: str | None = None
+    data_origin: str | None = None
+    tag_type: str | None = None
+
+
+class SpoolKProfileBase(BaseModel):
+    printer_id: int
+    extruder: int = 0
+    nozzle_diameter: str = "0.4"
+    nozzle_type: str | None = None
+    k_value: float
+    name: str | None = None
+    cali_idx: int | None = None
+    setting_id: str | None = None
+
+
+class SpoolKProfileResponse(SpoolKProfileBase):
+    id: int
+    spool_id: int
+    created_at: datetime
+
+    class Config:
+        from_attributes = True
+
+
+class SpoolResponse(SpoolBase):
+    id: int
+    added_full: bool | None = None
+    last_used: datetime | None = None
+    encode_time: datetime | None = None
+    tag_uid: str | None = None
+    tray_uuid: str | None = None
+    data_origin: str | None = None
+    tag_type: str | None = None
+    archived_at: datetime | None = None
+    created_at: datetime
+    updated_at: datetime
+    k_profiles: list[SpoolKProfileResponse] = []
+
+    class Config:
+        from_attributes = True
+
+
+class SpoolAssignmentCreate(BaseModel):
+    spool_id: int
+    printer_id: int
+    ams_id: int
+    tray_id: int
+
+
+class SpoolAssignmentResponse(BaseModel):
+    id: int
+    spool_id: int
+    printer_id: int
+    printer_name: str | None = None
+    ams_id: int
+    tray_id: int
+    fingerprint_color: str | None = None
+    fingerprint_type: str | None = None
+    created_at: datetime
+    spool: SpoolResponse | None = None
+    configured: bool = False
+
+    class Config:
+        from_attributes = True

+ 17 - 0
backend/app/schemas/spool_usage.py

@@ -0,0 +1,17 @@
+from datetime import datetime
+
+from pydantic import BaseModel
+
+
+class SpoolUsageHistoryResponse(BaseModel):
+    id: int
+    spool_id: int
+    printer_id: int | None = None
+    print_name: str | None = None
+    weight_used: float
+    percent_used: int
+    status: str
+    created_at: datetime
+
+    class Config:
+        from_attributes = True

+ 32 - 6
backend/app/services/archive.py

@@ -151,6 +151,27 @@ class ThreeMFParser:
                         self.metadata["_slice_filament_type"] = ", ".join(types)
                     if colors:
                         self.metadata["_slice_filament_color"] = ",".join(colors)
+
+                    # Collect per-slot filament usage for tracking & notifications
+                    filament_slots = []
+                    for f in filaments:
+                        slot_id = f.get("id")
+                        used_g_str = f.get("used_g", "0")
+                        try:
+                            used_g = float(used_g_str)
+                        except (ValueError, TypeError):
+                            used_g = 0
+                        if used_g > 0 and slot_id:
+                            filament_slots.append(
+                                {
+                                    "slot_id": int(slot_id),
+                                    "used_g": round(used_g, 2),
+                                    "type": f.get("type", ""),
+                                    "color": f.get("color", ""),
+                                }
+                            )
+                    if filament_slots:
+                        self.metadata["filament_slots"] = filament_slots
         except Exception:
             pass  # Skip unparseable slice_info metadata
 
@@ -1026,8 +1047,9 @@ class ArchiveService:
         if not archive:
             return False
 
-        # Delete files - with CRITICAL safety checks to prevent accidental deletion
-        # of parent directories (e.g., /opt) if file_path is empty/malformed
+        # Resolve the directory to delete BEFORE committing the DB change
+        dir_to_delete: Path | None = None
+
         if archive.file_path and archive.file_path.strip():
             file_path = settings.base_dir / archive.file_path
             if file_path.exists():
@@ -1041,13 +1063,11 @@ class ArchiveService:
                         f"SECURITY: Refusing to delete archive {archive_id} - "
                         f"path {archive_dir} is outside archive directory {settings.archive_dir}"
                     )
-                    # Still delete the database record, just not the files
                     await self.db.delete(archive)
                     await self.db.commit()
                     return True
 
                 # Safety check 2: archive_dir must be at least 1 level deep inside archive_dir
-                # (should be archive_dir/uuid/file.3mf, so parent should be archive_dir/uuid)
                 try:
                     relative_path = archive_dir.resolve().relative_to(settings.archive_dir.resolve())
                     if len(relative_path.parts) < 1:
@@ -1061,16 +1081,22 @@ class ArchiveService:
                 except ValueError:
                     pass  # Already handled above
 
-                shutil.rmtree(archive_dir, ignore_errors=True)
+                dir_to_delete = archive_dir
         else:
             logger.error(
                 f"SECURITY: Refusing to delete files for archive {archive_id} - "
                 f"file_path is empty or invalid: '{archive.file_path}'"
             )
 
-        # Delete database record
+        # Delete database record FIRST — if the commit fails (e.g. database locked
+        # during concurrent bulk deletes), the files stay on disk and nothing is lost.
         await self.db.delete(archive)
         await self.db.commit()
+
+        # Only delete files AFTER the DB commit succeeds to avoid orphaned records
+        if dir_to_delete:
+            shutil.rmtree(dir_to_delete, ignore_errors=True)
+
         return True
 
     async def attach_timelapse(

+ 1 - 1
backend/app/services/bambu_ftp.py

@@ -181,7 +181,7 @@ class BambuFTPClient:
         if self._ftp:
             try:
                 self._ftp.quit()
-            except (OSError, ftplib.Error):
+            except (OSError, ftplib.Error, EOFError):
                 pass  # Best-effort FTP cleanup; connection may already be closed
             self._ftp = None
 

+ 162 - 59
backend/app/services/bambu_mqtt.py

@@ -11,6 +11,7 @@ import asyncio
 import json
 import logging
 import ssl
+import threading
 import time
 from collections import deque
 from collections.abc import Callable
@@ -127,7 +128,7 @@ class PrinterState:
     chamber_light: bool = False
     # Active extruder for dual nozzle (0=right, 1=left) - from device.extruder.info[X].hnow
     active_extruder: int = 0
-    # Currently loaded tray (global ID): 254 = external spool, 255 = no filament
+    # Currently loaded tray (global ID): 254/255 = external spools, 255 = no filament on legacy printers
     tray_now: int = 255
     # Pending load target - used to track what tray we're loading for H2D disambiguation
     pending_tray_target: int | None = None
@@ -279,6 +280,7 @@ class BambuMQTTClient:
         self._message_log: deque[MQTTLogEntry] = deque(maxlen=100)
         self._logging_enabled: bool = False
         self._last_message_time: float = 0.0  # Track when we last received a message
+        self._disconnection_event: threading.Event | None = None
         self._previous_ams_hash: str | None = None  # Track AMS changes
 
         # K-profile command tracking
@@ -357,6 +359,8 @@ class BambuMQTTClient:
         self.state.connected = False
         if self.on_state_change:
             self.on_state_change(self.state)
+        if self._disconnection_event:
+            self._disconnection_event.set()
 
     def _on_message(self, client, userdata, msg):
         try:
@@ -447,14 +451,33 @@ class BambuMQTTClient:
                 except Exception as e:
                     logger.error("[%s] Error handling AMS data from print: %s", self.serial_number, e)
 
+            # Handle vir_slot (H2-series external spool data) — list of external trays
+            # Process vir_slot FIRST so it takes priority over vt_tray
+            if "vir_slot" in print_data:
+                vir_slot = print_data["vir_slot"]
+                if isinstance(vir_slot, list) and vir_slot:
+                    # Fix: single-nozzle printers (X1C, P1S, A1) report their single
+                    # external slot with id=255 in vir_slot, but tray_now=254 when active.
+                    # Remap id=255→254 for single-slot printers so active detection works.
+                    # Dual-nozzle (H2D) has 2 slots: id=254 (Ext-L) and id=255 (Ext-R).
+                    if len(vir_slot) == 1 and str(vir_slot[0].get("id", "")) == "255":
+                        vir_slot[0]["id"] = "254"
+                    self.state.raw_data["vt_tray"] = vir_slot
+
             # Handle vt_tray (virtual tray / external spool) data
-            if "vt_tray" in print_data:
+            # Only use vt_tray if vir_slot is NOT in this message AND we don't already
+            # have vir_slot data (H2-series sends vt_tray as a single active spool dict
+            # which would overwrite the correct multi-slot vir_slot data)
+            if "vt_tray" in print_data and "vir_slot" not in print_data:
                 vt_tray = print_data["vt_tray"]
-                self.state.raw_data["vt_tray"] = vt_tray
-                # Log vt_tray to investigate per-extruder data for H2D
-                if not hasattr(self, "_vt_tray_logged") or not self._vt_tray_logged:
-                    logger.info("[%s] vt_tray data: %s", self.serial_number, vt_tray)
-                    self._vt_tray_logged = True
+                existing = self.state.raw_data.get("vt_tray")
+                # Don't let a single-spool vt_tray dict overwrite multi-slot vir_slot data
+                if isinstance(vt_tray, dict) and isinstance(existing, list) and len(existing) > 1:
+                    pass  # Keep the vir_slot data
+                else:
+                    if isinstance(vt_tray, dict):
+                        vt_tray = [vt_tray]
+                    self.state.raw_data["vt_tray"] = vt_tray
 
             # Parse ams_status directly from print data (NOT from print.ams)
             # ams_status is a combined value: lower 8 bits = sub status, bits 8-15 = main status
@@ -482,7 +505,10 @@ class BambuMQTTClient:
 
             # Check for K-profile response (extrusion_cali)
             if "command" in print_data:
-                logger.debug("[%s] Received command response: %s", self.serial_number, print_data.get("command"))
+                cmd = print_data.get("command")
+                logger.debug("[%s] Received command response: %s", self.serial_number, cmd)
+                if cmd in ("extrusion_cali_sel", "extrusion_cali_set", "extrusion_cali_del", "ams_filament_setting"):
+                    logger.info("[%s] %s response: %s", self.serial_number, cmd, print_data)
             if "command" in print_data and print_data.get("command") == "extrusion_cali_get":
                 self._handle_kprofile_response(print_data)
 
@@ -937,12 +963,20 @@ class BambuMQTTClient:
                         if tray_id is not None and tray_id in existing_trays:
                             # Merge: start with existing, update with new non-empty values
                             merged_tray = existing_trays[tray_id].copy()
+                            # Detect slot-clearing updates (spool removal):
+                            # When tray_type is explicitly empty, clear everything
+                            # including RFID data (tag_uid/tray_uuid).
+                            slot_clearing = new_tray.get("tray_type") == ""
                             for key, value in new_tray.items():
                                 # Fields that should always be updated (even with empty/zero values):
                                 # - remain, k, id, cali_idx: status indicators where 0 is valid
-                                # - tray_type, tray_sub_brands, tag_uid, tray_uuid, tray_info_idx,
-                                #   tray_color, tray_id_name: slot content indicators that must be
-                                #   cleared when a spool is removed (fixes #147 - old AMS empty slot)
+                                # - tray_type, tray_sub_brands, tray_info_idx, tray_color,
+                                #   tray_id_name: slot content indicators that must be cleared
+                                #   when a spool is removed (fixes #147 - old AMS empty slot)
+                                # NOTE: tag_uid and tray_uuid are NOT in always_update_fields.
+                                # They are only cleared during spool removal (slot_clearing=True).
+                                # Periodic AMS updates often include empty RFID fields which
+                                # would overwrite valid data from the initial pushall.
                                 always_update_fields = (
                                     "remain",
                                     "k",
@@ -950,17 +984,20 @@ class BambuMQTTClient:
                                     "cali_idx",
                                     "tray_type",
                                     "tray_sub_brands",
-                                    "tag_uid",
-                                    "tray_uuid",
                                     "tray_info_idx",
                                     "tray_color",
                                     "tray_id_name",
                                 )
-                                if key in always_update_fields or value not in (
-                                    None,
-                                    "",
-                                    "0000000000000000",
-                                    "00000000000000000000000000000000",
+                                if (
+                                    key in always_update_fields
+                                    or slot_clearing
+                                    or value
+                                    not in (
+                                        None,
+                                        "",
+                                        "0000000000000000",
+                                        "00000000000000000000000000000000",
+                                    )
                                 ):
                                     merged_tray[key] = value
                             merged_trays.append(merged_tray)
@@ -1062,7 +1099,9 @@ class BambuMQTTClient:
             self._previous_ams_hash = ams_hash
             if self.on_ams_change:
                 logger.info("[%s] AMS data changed, triggering sync callback", self.serial_number)
-                self.on_ams_change(ams_list)
+                # Pass merged AMS data (not raw ams_list) — partial MQTT updates
+                # may lack fields like 'remain' that the merged state preserves
+                self.on_ams_change(merged_ams)
 
     def _update_state(self, data: dict):
         """Update printer state from message data."""
@@ -2371,11 +2410,13 @@ class BambuMQTTClient:
 
         return True
 
-    def disconnect(self):
+    def disconnect(self, timeout: float = 0):
         """Disconnect from the printer."""
         if self._client:
-            self._client.loop_stop()
+            self._disconnection_event = threading.Event()
             self._client.disconnect()
+            self._disconnection_event.wait(timeout=timeout)
+            self._client.loop_stop()
             self._client = None
             self.state.connected = False
 
@@ -2415,7 +2456,7 @@ class BambuMQTTClient:
     def _handle_kprofile_response(self, data: dict):
         """Handle K-profile response from printer."""
         response_nozzle = data.get("nozzle_diameter")
-        _response_seq_id = data.get("sequence_id", "?")
+        response_seq_id = data.get("sequence_id", "?")
         filaments = data.get("filaments", [])
         expected_nozzle = getattr(self, "_expected_kprofile_nozzle", None)
         has_pending_request = self._pending_kprofile_response is not None
@@ -2423,7 +2464,8 @@ class BambuMQTTClient:
         # Log all incoming responses when we have a pending request (for debugging)
         if has_pending_request:
             logger.info(
-                f"[{self.serial_number}] K-profile response: nozzle={response_nozzle}, {len(filaments)} profiles, expected={expected_nozzle}"
+                f"[{self.serial_number}] K-profile response: nozzle={response_nozzle}, "
+                f"seq_id={response_seq_id}, {len(filaments)} profiles, expected={expected_nozzle}"
             )
 
         # If we have a pending request, only accept responses with matching nozzle_diameter
@@ -3296,6 +3338,7 @@ class BambuMQTTClient:
         command = {"print": {"command": "ams_get_rfid", "ams_id": ams_id, "slot_id": tray_id, "sequence_id": "0"}}
         self._client.publish(self.topic_publish, json.dumps(command), qos=1)
         logger.info("[%s] Triggering RFID re-read: AMS %s, slot %s", self.serial_number, ams_id, tray_id)
+
         return True, f"Refreshing AMS {ams_id} tray {tray_id}"
 
     def ams_set_filament_setting(
@@ -3332,18 +3375,33 @@ class BambuMQTTClient:
             logger.warning("[%s] Cannot set AMS filament setting: not connected", self.serial_number)
             return False
 
-        # Calculate slot_id based on AMS type
-        if ams_id <= 3:
+        # Calculate mqtt IDs based on AMS type
+        if ams_id == 255:
+            vt_tray = self.state.raw_data.get("vt_tray", []) if self.state.raw_data else []
+            if len(vt_tray) > 1:
+                # Dual external slots (H2D): each ext slot is its own virtual AMS unit
+                # (254=ext-L / slot 0, 255=ext-R / slot 1)
+                mqtt_ams_id = 254 + tray_id
+            else:
+                # Single external slot (X1C, P1S, A1): always ams_id=255
+                mqtt_ams_id = 255
+            mqtt_tray_id = 0
+            slot_id = 0
+        elif ams_id <= 3:
+            mqtt_ams_id = ams_id
+            mqtt_tray_id = tray_id
             slot_id = tray_id
         else:
-            # AMS-HT or external: slot_id = 0
+            # AMS-HT: single tray per unit
+            mqtt_ams_id = ams_id
+            mqtt_tray_id = tray_id
             slot_id = 0
 
         command = {
             "print": {
                 "command": "ams_filament_setting",
-                "ams_id": ams_id,
-                "tray_id": tray_id,
+                "ams_id": mqtt_ams_id,
+                "tray_id": mqtt_tray_id,
                 "slot_id": slot_id,
                 "tray_info_idx": tray_info_idx,
                 "tray_type": tray_type,
@@ -3381,17 +3439,32 @@ class BambuMQTTClient:
             logger.warning("[%s] Cannot reset AMS slot: not connected", self.serial_number)
             return False
 
-        # Calculate slot_id based on AMS type
-        if ams_id <= 3:
+        # Calculate mqtt IDs based on AMS type
+        if ams_id == 255:
+            vt_tray = self.state.raw_data.get("vt_tray", []) if self.state.raw_data else []
+            if len(vt_tray) > 1:
+                # Dual external slots (H2D): each ext slot is its own virtual AMS unit
+                mqtt_ams_id = 254 + tray_id
+            else:
+                # Single external slot (X1C, P1S, A1): always ams_id=255
+                mqtt_ams_id = 255
+            mqtt_tray_id = 0
+            slot_id = 0
+        elif ams_id <= 3:
+            mqtt_ams_id = ams_id
+            mqtt_tray_id = tray_id
             slot_id = tray_id
         else:
+            # AMS-HT: single tray per unit
+            mqtt_ams_id = ams_id
+            mqtt_tray_id = tray_id
             slot_id = 0
 
         command = {
             "print": {
                 "command": "ams_filament_setting",
-                "ams_id": ams_id,
-                "tray_id": tray_id,
+                "ams_id": mqtt_ams_id,
+                "tray_id": mqtt_tray_id,
                 "slot_id": slot_id,
                 "tray_info_idx": "",
                 "tray_type": "",
@@ -3416,20 +3489,21 @@ class BambuMQTTClient:
         cali_idx: int,
         filament_id: str,
         nozzle_diameter: str = "0.4",
-        setting_id: str | None = None,
     ) -> bool:
         """Set calibration profile (K value) for an AMS slot.
 
         This command selects a K profile from the printer's calibration list.
         Use cali_idx=-1 to use the default K value (0.020).
 
+        Note: Do NOT send setting_id in this command — BambuStudio never includes
+        it, and adding it causes the firmware to mislink the profile on X1C/P1S.
+
         Args:
             ams_id: AMS unit ID (0-3 for regular AMS, 128-135 for HT AMS)
             tray_id: Tray ID within the AMS (0-3)
             cali_idx: Calibration profile index (-1 for default)
             filament_id: Filament preset ID (same as tray_info_idx)
             nozzle_diameter: Nozzle diameter string (e.g., "0.4")
-            setting_id: Full setting ID with version (e.g., "GFSL05_07") - optional
 
         Returns:
             True if command was sent, False otherwise
@@ -3438,13 +3512,34 @@ class BambuMQTTClient:
             logger.warning("[%s] Cannot set calibration: not connected", self.serial_number)
             return False
 
-        # Calculate slot_id based on AMS type
-        # tray_id in the command should be the local tray index (0-3)
-        if ams_id <= 3:
+        # Calculate mqtt IDs based on AMS type.
+        # IMPORTANT: extrusion_cali_sel uses GLOBAL tray_id (unlike ams_filament_setting
+        # which uses LOCAL).  BambuStudio confirms: tray_id = ams_id * 4 + slot.
+        if ams_id == 255:
+            # External spool: extrusion_cali_sel uses GLOBAL tray_id (unlike
+            # ams_filament_setting which uses LOCAL tray_id=0).
+            vt_tray = self.state.raw_data.get("vt_tray", []) if self.state.raw_data else []
+            if len(vt_tray) > 1:
+                # Dual external slots (H2D): each ext slot is its own virtual AMS unit
+                # Confirmed from BambuStudio logs: ext-R sends ams_id=255, tray_id=255
+                mqtt_ams_id = 254 + tray_id
+                mqtt_tray_id = 254 + tray_id
+            else:
+                # Single external slot (X1C, P1S, A1): global tray_id=254
+                mqtt_ams_id = 254
+                mqtt_tray_id = 254
+            slot_id = 0
+        elif ams_id <= 3:
+            mqtt_ams_id = ams_id
+            mqtt_tray_id = ams_id * 4 + tray_id
             slot_id = tray_id
         elif ams_id >= 128 and ams_id <= 135:
+            mqtt_ams_id = ams_id
+            mqtt_tray_id = tray_id
             slot_id = 0
         else:
+            mqtt_ams_id = ams_id
+            mqtt_tray_id = tray_id
             slot_id = 0
 
         command = {
@@ -3453,20 +3548,16 @@ class BambuMQTTClient:
                 "cali_idx": cali_idx,
                 "filament_id": filament_id,
                 "nozzle_diameter": nozzle_diameter,
-                "ams_id": ams_id,
-                "tray_id": tray_id,  # Local tray index (0-3), not global
+                "ams_id": mqtt_ams_id,
+                "tray_id": mqtt_tray_id,
                 "slot_id": slot_id,
                 "sequence_id": "0",
             }
         }
 
-        # Include setting_id if provided (helps slicer show correct K profile)
-        if setting_id:
-            command["print"]["setting_id"] = setting_id
-
         command_json = json.dumps(command)
         logger.info(
-            f"[{self.serial_number}] Publishing extrusion_cali_sel: AMS {ams_id}, tray {tray_id}, cali_idx={cali_idx}, setting_id={setting_id}"
+            f"[{self.serial_number}] Publishing extrusion_cali_sel: AMS {ams_id}, tray {tray_id}, cali_idx={cali_idx}"
         )
         logger.debug("[%s] extrusion_cali_sel command: %s", self.serial_number, command_json)
         self._client.publish(self.topic_publish, command_json, qos=1)
@@ -3476,25 +3567,26 @@ class BambuMQTTClient:
         self,
         tray_id: int,
         k_value: float,
-        n_coef: float = 0.0,
         nozzle_diameter: str = "0.4",
-        bed_temp: int = 60,
         nozzle_temp: int = 220,
-        max_volumetric_speed: float = 20.0,
+        filament_id: str = "",
+        setting_id: str = "",
+        name: str = "",
+        cali_idx: int = -1,
     ) -> bool:
         """Directly set K value (pressure advance) for a tray.
 
-        This command sets the K value directly without selecting from stored profiles.
-        Use this when you want to apply a specific K value to a tray.
+        Uses the filaments array format required by current firmware.
 
         Args:
             tray_id: Global tray ID (ams_id * 4 + slot)
             k_value: Pressure advance K value (e.g., 0.020)
-            n_coef: N coefficient (usually 0.0 for manual, 1.4 for auto-calibration)
             nozzle_diameter: Nozzle diameter string (e.g., "0.4")
-            bed_temp: Bed temperature for calibration reference
             nozzle_temp: Nozzle temperature for calibration reference
-            max_volumetric_speed: Max volumetric speed for calibration reference
+            filament_id: Filament preset ID (e.g., "GFA02")
+            setting_id: Setting ID (e.g., "GFSA02_07")
+            name: Profile display name
+            cali_idx: Calibration index (-1 for new)
 
         Returns:
             True if command was sent, False otherwise
@@ -3503,17 +3595,28 @@ class BambuMQTTClient:
             logger.warning("[%s] Cannot set K value: not connected", self.serial_number)
             return False
 
+        nozzle_id = f"HS00-{nozzle_diameter}"
+
+        filament_entry = {
+            "ams_id": 0,
+            "cali_idx": cali_idx,
+            "extruder_id": 0,
+            "filament_id": filament_id,
+            "k_value": f"{k_value:.6f}",
+            "n_coef": "1.400000",
+            "name": name,
+            "nozzle_diameter": nozzle_diameter,
+            "nozzle_id": nozzle_id,
+            "setting_id": setting_id,
+            "tray_id": tray_id,
+        }
+
         command = {
             "print": {
                 "command": "extrusion_cali_set",
-                "tray_id": tray_id,
-                "k_value": k_value,
-                "n_coef": n_coef,
+                "filaments": [filament_entry],
                 "nozzle_diameter": nozzle_diameter,
-                "bed_temp": bed_temp,
-                "nozzle_temp": nozzle_temp,
-                "max_volumetric_speed": max_volumetric_speed,
-                "sequence_id": "0",
+                "sequence_id": str(self._sequence_id),
             }
         }
 

+ 36 - 15
backend/app/services/external_camera.py

@@ -362,7 +362,7 @@ async def _capture_rtsp_frame(url: str, timeout: int) -> bytes | None:
     ]
 
     try:
-        print(f"[EXT-CAM] Running ffmpeg command: {' '.join(cmd[:6])}...")
+        logger.debug(f"Running ffmpeg command: {' '.join(cmd[:6])}...")
         process = await asyncio.create_subprocess_exec(
             *cmd,
             stdout=asyncio.subprocess.PIPE,
@@ -370,13 +370,12 @@ async def _capture_rtsp_frame(url: str, timeout: int) -> bytes | None:
         )
 
         stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=timeout)
-        print(
-            f"[EXT-CAM] ffmpeg returned: code={process.returncode}, stdout={len(stdout)} bytes, stderr={len(stderr)} bytes"
+        logger.debug(
+            f"ffmpeg returned: code={process.returncode}, stdout={len(stdout)} bytes, stderr={len(stderr)} bytes"
         )
 
         if process.returncode != 0:
             logger.error("ffmpeg RTSP capture failed: %s", stderr.decode()[:200])
-            print(f"[EXT-CAM] ffmpeg error: {stderr.decode()[:300]}")
             return None
 
         if not stdout or len(stdout) < 100:
@@ -440,11 +439,9 @@ async def test_connection(url: str, camera_type: str) -> dict:
     Returns:
         Dict with {success: bool, error?: str, resolution?: str}
     """
-    print(f"[EXT-CAM] Testing camera connection: type={camera_type}, url={url[:50]}...")
     logger.info("Testing camera connection: type=%s, url=%s...", camera_type, url[:50])
     try:
         frame = await capture_frame(url, camera_type, timeout=10)
-        print(f"[EXT-CAM] Capture result: {len(frame) if frame else 0} bytes")
         logger.info("Capture result: %s bytes", len(frame) if frame else 0)
 
         if frame:
@@ -490,17 +487,41 @@ async def generate_mjpeg_stream(url: str, camera_type: str, fps: int = 10) -> As
     last_frame_time = 0.0
 
     if camera_type == "mjpeg":
-        # Proxy MJPEG stream directly
-        async for frame in _stream_mjpeg(url):
-            current_time = asyncio.get_event_loop().time()
-            if current_time - last_frame_time >= frame_interval:
-                last_frame_time = current_time
-                yield _format_mjpeg_frame(frame)
+        # Proxy MJPEG stream directly, with reconnect on timeout
+        max_retries = 3
+        for attempt in range(max_retries + 1):
+            frame_yielded = False
+            async for frame in _stream_mjpeg(url):
+                frame_yielded = True
+                current_time = asyncio.get_event_loop().time()
+                if current_time - last_frame_time >= frame_interval:
+                    last_frame_time = current_time
+                    yield _format_mjpeg_frame(frame)
+            if not frame_yielded or attempt == max_retries:
+                break
+            logger.warning(
+                "External MJPEG stream ended, reconnecting (attempt %d/%d)...",
+                attempt + 1,
+                max_retries,
+            )
+            await asyncio.sleep(2)
 
     elif camera_type == "rtsp":
-        # Use ffmpeg to convert RTSP to MJPEG
-        async for frame in _stream_rtsp(url, fps):
-            yield _format_mjpeg_frame(frame)
+        # Use ffmpeg to convert RTSP to MJPEG, with reconnect on timeout
+        max_retries = 3
+        for attempt in range(max_retries + 1):
+            frame_yielded = False
+            async for frame in _stream_rtsp(url, fps):
+                frame_yielded = True
+                yield _format_mjpeg_frame(frame)
+            if not frame_yielded or attempt == max_retries:
+                break
+            logger.warning(
+                "External RTSP stream ended, reconnecting (attempt %d/%d)...",
+                attempt + 1,
+                max_retries,
+            )
+            await asyncio.sleep(2)
 
     elif camera_type == "usb":
         # Use ffmpeg to stream from USB camera

+ 75 - 11
backend/app/services/firmware_check.py

@@ -1,8 +1,9 @@
 """
 Firmware Check Service
 
-Checks for firmware updates by fetching from Bambu Lab's official firmware download page.
-Also provides firmware download functionality for offline updates.
+Checks for firmware updates by fetching from Bambu Lab's official wiki and firmware
+download page. The wiki is used as the primary version source (always up-to-date),
+while the download page provides firmware file URLs for offline updates.
 """
 
 import logging
@@ -18,10 +19,13 @@ from backend.app.core.config import _data_dir
 
 logger = logging.getLogger(__name__)
 
-# Bambu Lab firmware download page
+# Bambu Lab firmware download page (for download URLs)
 BAMBU_FIRMWARE_BASE = "https://bambulab.com"
 FIRMWARE_PAGE = "/en/support/firmware-download/all"
 
+# Bambu Lab wiki (primary source for latest version detection)
+BAMBU_WIKI_BASE = "https://wiki.bambulab.com"
+
 # Cache TTL in seconds (1 hour)
 CACHE_TTL = 3600
 
@@ -61,6 +65,20 @@ API_KEY_TO_DEV_MODEL = {
     "h2d-pro": "O1E",
 }
 
+# Wiki firmware release history pages (primary version source)
+API_KEY_TO_WIKI_PATH = {
+    "x1": "/en/x1/manual/X1-X1C-firmware-release-history",
+    "x1e": "/en/x1/manual/X1E-firmware-release-history",
+    "p1": "/en/p1/manual/p1p-firmware-release-history",
+    "a1": "/en/a1/manual/a1-firmware-release-history",
+    "a1-mini": "/en/a1-mini/manual/a1-mini-firmware-release-history",
+    "h2d": "/en/h2d/manual/h2d-firmware-release-history",
+    "h2c": "/en/h2c/manual/h2c-firmware-release-history",
+    "h2s": "/en/h2s/manual/h2s-firmware-release-history",
+    "p2s": "/en/p2s/manual/p2s-firmware-release-history",
+    "h2d-pro": "/en/h2d-pro/manual/firmware-release-history",
+}
+
 
 @dataclass
 class FirmwareVersion:
@@ -109,11 +127,34 @@ class FirmwareCheckService:
 
         return self._build_id  # Return cached value if available
 
-    async def _fetch_firmware_versions(self, api_key: str) -> FirmwareVersion | None:
-        """Fetch firmware versions for a specific printer from Bambu Lab API."""
+    async def _fetch_version_from_wiki(self, api_key: str) -> str | None:
+        """Fetch the latest firmware version from Bambu Lab's wiki release history page."""
+        wiki_path = API_KEY_TO_WIKI_PATH.get(api_key)
+        if not wiki_path:
+            return None
+
+        try:
+            url = f"{BAMBU_WIKI_BASE}{wiki_path}"
+            response = await self._client.get(url, follow_redirects=True)
+
+            if response.status_code == 200:
+                # Extract version strings (format: XX.XX.XX.XX), first match is the latest
+                versions = re.findall(r"(\d{2}\.\d{2}\.\d{2}\.\d{2})", response.text)
+                if versions:
+                    logger.debug("Wiki firmware for %s: %s", api_key, versions[0])
+                    return versions[0]
+            else:
+                logger.debug("Wiki firmware page for %s returned %s", api_key, response.status_code)
+
+        except Exception as e:
+            logger.debug("Error fetching wiki firmware for %s: %s", api_key, e)
+
+        return None
+
+    async def _fetch_from_download_page(self, api_key: str) -> FirmwareVersion | None:
+        """Fetch firmware info from Bambu Lab's download page (has download URLs)."""
         build_id = await self._get_build_id()
         if not build_id:
-            logger.warning("No build ID available, cannot fetch firmware versions")
             return None
 
         try:
@@ -135,16 +176,39 @@ class FirmwareCheckService:
                         release_notes=latest.get("release_notes_en"),
                         release_time=latest.get("release_time"),
                     )
-            else:
-                # api_key is a printer model identifier (e.g. "x1", "p1"), not a secret
-                logger.warning("Failed to fetch firmware for %s: %s", api_key, response.status_code)
 
         except Exception as e:
-            # api_key is a printer model identifier (e.g. "x1", "p1"), not a secret
-            logger.error("Error fetching firmware for %s: %s", api_key, e)
+            logger.debug("Error fetching download page firmware for %s: %s", api_key, e)
 
         return None
 
+    async def _fetch_firmware_versions(self, api_key: str) -> FirmwareVersion | None:
+        """Fetch firmware version info, using wiki as primary source and download page as fallback."""
+        # Try wiki first (always has the latest version)
+        wiki_version = await self._fetch_version_from_wiki(api_key)
+
+        # Try download page (has download URLs, may lag behind wiki)
+        download_info = await self._fetch_from_download_page(api_key)
+
+        if wiki_version:
+            # Wiki has the latest version — use it, attach download URL if available
+            download_url = ""
+            release_notes = None
+            if download_info and download_info.version == wiki_version:
+                download_url = download_info.download_url
+                release_notes = download_info.release_notes
+            return FirmwareVersion(
+                version=wiki_version,
+                download_url=download_url,
+                release_notes=release_notes,
+            )
+
+        if download_info:
+            return download_info
+
+        logger.warning("Could not fetch firmware info for %s from wiki or download page", api_key)
+        return None
+
     async def get_latest_version(self, model: str) -> FirmwareVersion | None:
         """
         Get the latest firmware version for a printer model.

+ 7 - 2
backend/app/services/mqtt_relay.py

@@ -36,6 +36,7 @@ class MQTTRelayService:
         self._last_printer_status: dict[int, float] = {}  # printer_id -> last publish timestamp
         self._smart_plug_service = None  # Lazy import to avoid circular dependency
         self._settings: dict = {}  # Store settings for smart plug service
+        self._disconnection_event: threading.Event | None = None
 
     async def configure(self, settings: dict) -> bool:
         """Configure MQTT connection from settings.
@@ -187,15 +188,19 @@ class MQTTRelayService:
             logger.warning("MQTT relay disconnected: %s", rc)
         else:
             logger.info("MQTT relay disconnected cleanly")
+        if self._disconnection_event:
+            self._disconnection_event.set()
 
-    async def disconnect(self):
+    async def disconnect(self, timeout: float = 0):
         """Disconnect from MQTT broker."""
         if self.client:
             try:
                 # Publish offline status before disconnecting
                 self._publish_status("offline")
-                self.client.loop_stop()
+                self._disconnection_event = threading.Event()
                 self.client.disconnect()
+                await asyncio.to_thread(self._disconnection_event.wait, timeout=timeout)
+                self.client.loop_stop()
             except Exception as e:
                 logger.debug("MQTT disconnect error (ignored): %s", e)
             finally:

+ 8 - 2
backend/app/services/mqtt_smart_plug.py

@@ -3,6 +3,7 @@
 This service enables integration with Shelly, Zigbee2MQTT, and other MQTT-based energy monitoring devices.
 """
 
+import asyncio
 import json
 import logging
 import threading
@@ -52,6 +53,7 @@ class MQTTSmartPlugService:
         self.plug_configs: dict[int, dict[str, MQTTDataSourceConfig]] = {}
         # plug_id -> latest data
         self.plug_data: dict[int, SmartPlugMQTTData] = {}
+        self._disconnection_event: threading.Event | None = None
         self._configured = False
         self._broker = ""
         self._port = 1883
@@ -209,6 +211,8 @@ class MQTTSmartPlugService:
             logger.warning("MQTT smart plug service disconnected: %s", rc)
         else:
             logger.info("MQTT smart plug service disconnected cleanly")
+        if self._disconnection_event:
+            self._disconnection_event.set()
 
     def _on_message(self, client: mqtt.Client, userdata: Any, msg: mqtt.MQTTMessage):
         """Handle incoming MQTT message, extract data using JSON path."""
@@ -471,12 +475,14 @@ class MQTTSmartPlugService:
         timeout = timedelta(minutes=self.REACHABLE_TIMEOUT_MINUTES)
         return datetime.utcnow() - data.last_seen < timeout
 
-    async def disconnect(self):
+    async def disconnect(self, timeout: float = 0):
         """Disconnect from MQTT broker."""
         if self.client:
             try:
-                self.client.loop_stop()
+                self._disconnection_event = threading.Event()
                 self.client.disconnect()
+                await asyncio.to_thread(self._disconnection_event.wait, timeout=timeout)
+                self.client.loop_stop()
             except Exception as e:
                 logger.debug("MQTT smart plug disconnect error (ignored): %s", e)
             finally:

+ 55 - 19
backend/app/services/notification_service.py

@@ -267,19 +267,20 @@ class NotificationService:
 
         url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
 
-        # Check if message contains characters that break Markdown parsing
-        # URLs and error codes with underscores cause issues
-        has_url = "http://" in message or "https://" in message
-        # Check for underscores outside of the bold title (odd number of _ breaks markdown)
-        body_part = message.split("\n", 1)[1] if "\n" in message else ""
-        has_problematic_underscore = "_" in body_part
+        # Escape underscores in the message body so Telegram Markdown
+        # parsing doesn't break on job names like "A1_plate_8" or error
+        # codes like "0300_0001".  The title is already wrapped in *bold*
+        # markers, so only escape after the first newline.
+        if "\n" in message:
+            title_part, body_part = message.split("\n", 1)
+            body_part = body_part.replace("_", "\\_")
+            message = f"{title_part}\n{body_part}"
 
         data = {
             "chat_id": chat_id,
             "text": message,
+            "parse_mode": "Markdown",
         }
-        if not has_url and not has_problematic_underscore:
-            data["parse_mode"] = "Markdown"
 
         client = await self._get_client()
         response = await client.post(url, json=data)
@@ -344,7 +345,9 @@ class NotificationService:
         except Exception as e:
             return False, f"Email error: {str(e)}"
 
-    async def _send_discord(self, config: dict, title: str, message: str) -> tuple[bool, str]:
+    async def _send_discord(
+        self, config: dict, title: str, message: str, image_data: bytes | None = None
+    ) -> tuple[bool, str]:
         """Send notification via Discord webhook."""
         webhook_url = config.get("webhook_url", "").strip()
 
@@ -355,18 +358,25 @@ class NotificationService:
             return False, "Invalid Discord webhook URL"
 
         # Discord embed format for nicer messages
-        data = {
-            "embeds": [
-                {
-                    "title": title,
-                    "description": message,
-                    "color": 0x00AE42,  # Bambu green
-                }
-            ]
+        embed = {
+            "title": title,
+            "description": message,
+            "color": 0x00AE42,  # Bambu green
         }
 
         client = await self._get_client()
-        response = await client.post(webhook_url, json=data)
+
+        if image_data:
+            # Attach image via multipart form-data and reference in embed
+            embed["image"] = {"url": "attachment://photo.jpg"}
+            payload = {"embeds": [embed]}
+            response = await client.post(
+                webhook_url,
+                data={"payload_json": json.dumps(payload)},
+                files={"files[0]": ("photo.jpg", image_data, "image/jpeg")},
+            )
+        else:
+            response = await client.post(webhook_url, json={"embeds": [embed]})
 
         if response.status_code in (200, 204):
             return True, "Message sent successfully"
@@ -449,7 +459,7 @@ class NotificationService:
             elif provider.provider_type == "email":
                 return await self._send_email(config, title, message)
             elif provider.provider_type == "discord":
-                return await self._send_discord(config, title, message)
+                return await self._send_discord(config, title, message, image_data=image_data)
             elif provider.provider_type == "webhook":
                 return await self._send_webhook(config, title, message)
             else:
@@ -718,6 +728,32 @@ class NotificationService:
             if archive_data.get("finish_photo_url"):
                 variables["finish_photo_url"] = archive_data["finish_photo_url"]
 
+            # Build per-slot breakdown string with AMS info when available
+            if archive_data.get("usage_results"):
+                parts = []
+                for u in archive_data["usage_results"]:
+                    ams_id = u.get("ams_id", 0)
+                    tray_id = u.get("tray_id", 0)
+                    material = u.get("material", "Unknown") or "Unknown"
+                    used = u.get("weight_used", 0)
+                    if ams_id >= 128:
+                        slot_label = "Ext"
+                    else:
+                        slot_label = f"AMS-{chr(65 + ams_id)} T{tray_id + 1}"
+                    parts.append(f"{slot_label} {material}: {used:.1f}g")
+                variables["filament_details"] = " | ".join(parts)
+            elif archive_data.get("filament_slots"):
+                parts = []
+                for slot in archive_data["filament_slots"]:
+                    ftype = slot.get("type", "Unknown") or "Unknown"
+                    used = slot.get("used_g", 0)
+                    parts.append(f"{ftype}: {used:.1f}g")
+                variables["filament_details"] = " | ".join(parts)
+
+            # Add progress for partial prints
+            if archive_data.get("progress") is not None:
+                variables["progress"] = str(archive_data["progress"])
+
         # Extract image data for providers that support attachments (e.g. Pushover)
         image_data = None
         if archive_data:

+ 48 - 45
backend/app/services/print_scheduler.py

@@ -4,7 +4,7 @@ import asyncio
 import json
 import logging
 import zipfile
-from datetime import datetime, timedelta
+from datetime import datetime
 from pathlib import Path
 
 import defusedxml.ElementTree as ET
@@ -23,6 +23,7 @@ from backend.app.services.notification_service import notification_service
 from backend.app.services.printer_manager import printer_manager
 from backend.app.services.smart_plug_manager import smart_plug_manager
 from backend.app.utils.printer_models import normalize_printer_model
+from backend.app.utils.threemf_tools import extract_nozzle_mapping_from_3mf
 
 logger = logging.getLogger(__name__)
 
@@ -332,10 +333,9 @@ class PrintScheduler:
                     if tray_type:
                         loaded_types.add(tray_type.upper())
 
-        # Check external spool (virtual tray, stored in raw_data["vt_tray"])
-        vt_tray = status.raw_data.get("vt_tray")
-        if vt_tray:
-            vt_type = vt_tray.get("tray_type")
+        # Check external spool(s) (virtual tray, stored in raw_data["vt_tray"] as list)
+        for vt in status.raw_data.get("vt_tray") or []:
+            vt_type = vt.get("tray_type")
             if vt_type:
                 loaded_types.add(vt_type.upper())
 
@@ -477,6 +477,12 @@ class PrintScheduler:
                             pass  # Skip filament entry with unparseable usage data
 
                 filaments.sort(key=lambda x: x["slot_id"])
+
+                # Enrich with nozzle mapping for dual-nozzle printers
+                nozzle_mapping = extract_nozzle_mapping_from_3mf(zf)
+                if nozzle_mapping:
+                    for filament in filaments:
+                        filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
         except Exception as e:
             logger.warning("Failed to parse filament requirements: %s", e)
             return None
@@ -494,6 +500,9 @@ class PrintScheduler:
         """
         filaments = []
 
+        # Get ams_extruder_map for dual-nozzle printers (H2D, H2D Pro)
+        ams_extruder_map = status.raw_data.get("ams_extruder_map", {})
+
         # Parse AMS units from raw_data
         ams_data = status.raw_data.get("ams", [])
         for ams_unit in ams_data:
@@ -524,25 +533,28 @@ class PrintScheduler:
                             "is_ht": is_ht,
                             "is_external": False,
                             "global_tray_id": global_tray_id,
+                            "extruder_id": ams_extruder_map.get(str(ams_id)),
                         }
                     )
 
-        # Check external spool (vt_tray)
-        vt_tray = status.raw_data.get("vt_tray")
-        if vt_tray and vt_tray.get("tray_type"):
-            color = self._normalize_color(vt_tray.get("tray_color", ""))
-            filaments.append(
-                {
-                    "type": vt_tray["tray_type"],
-                    "color": color,
-                    "tray_info_idx": vt_tray.get("tray_info_idx", ""),
-                    "ams_id": -1,
-                    "tray_id": 0,
-                    "is_ht": False,
-                    "is_external": True,
-                    "global_tray_id": 254,
-                }
-            )
+        # Check external spool(s) (vt_tray is a list)
+        for idx, vt in enumerate(status.raw_data.get("vt_tray") or []):
+            if vt.get("tray_type"):
+                color = self._normalize_color(vt.get("tray_color", ""))
+                tray_id = int(vt.get("id", 254))
+                filaments.append(
+                    {
+                        "type": vt["tray_type"],
+                        "color": color,
+                        "tray_info_idx": vt.get("tray_info_idx", ""),
+                        "ams_id": -1,
+                        "tray_id": idx,
+                        "is_ht": False,
+                        "is_external": True,
+                        "global_tray_id": tray_id,
+                        "extruder_id": (tray_id - 254) if ams_extruder_map else None,
+                    }
+                )
 
         return filaments
 
@@ -616,6 +628,13 @@ class PrintScheduler:
             # Get available trays (not already used)
             available = [f for f in loaded if f["global_tray_id"] not in used_tray_ids]
 
+            # Nozzle-aware filtering: restrict to trays on the correct nozzle
+            req_nozzle_id = req.get("nozzle_id")
+            if req_nozzle_id is not None:
+                nozzle_filtered = [f for f in available if f.get("extruder_id") == req_nozzle_id]
+                if nozzle_filtered:
+                    available = nozzle_filtered
+
             # Check if tray_info_idx is unique among available trays
             if req_tray_info_idx:
                 idx_matches = [f for f in available if f.get("tray_info_idx") == req_tray_info_idx]
@@ -694,9 +713,11 @@ class PrintScheduler:
         if not state:
             return False
 
-        # Printer is idle if state is IDLE, FINISH, FAILED, or unknown
-        # FAILED means previous print failed, printer is ready for new print
-        return state.state in ("IDLE", "FINISH", "FAILED", "unknown")
+        # IDLE = ready for next print
+        # FINISH/FAILED = ready only if user confirmed plate is cleared
+        return state.state == "IDLE" or (
+            state.state in ("FINISH", "FAILED") and printer_manager.is_plate_cleared(printer_id)
+        )
 
     async def _get_smart_plug(self, db: AsyncSession, printer_id: int) -> SmartPlug | None:
         """Get the smart plug associated with a printer."""
@@ -860,27 +881,6 @@ class PrintScheduler:
                 await self._power_off_if_needed(db, item)
                 return
 
-            # Safety: Check if this archive was printed recently (within 4 hours)
-            # This prevents phantom reprints if a queue item got stuck in "pending"
-            # after its print already started due to a crash/restart
-            if archive.status == "completed" and archive.completed_at:
-                completed_at = (
-                    archive.completed_at.replace(tzinfo=None) if archive.completed_at.tzinfo else archive.completed_at
-                )
-                time_since_completed = datetime.utcnow() - completed_at
-                if time_since_completed < timedelta(hours=4):
-                    logger.warning(
-                        f"Queue item {item.id}: Archive {item.archive_id} was already printed "
-                        f"{time_since_completed.total_seconds() / 3600:.1f} hours ago, skipping to prevent duplicate"
-                    )
-                    item.status = "skipped"
-                    item.error_message = (
-                        f"Archive was already printed {time_since_completed.total_seconds() / 3600:.1f} hours ago"
-                    )
-                    item.completed_at = datetime.utcnow()
-                    await db.commit()
-                    return
-
             file_path = settings.base_dir / archive.file_path
             filename = archive.filename
 
@@ -1034,6 +1034,9 @@ class PrintScheduler:
         item.status = "printing"
         item.started_at = datetime.utcnow()
         await db.commit()
+
+        # Consume the plate-cleared flag now that we're starting a print
+        printer_manager.consume_plate_cleared(item.printer_id)
         logger.info("Queue item %s: Status set to 'printing', sending print command...", item.id)
 
         # Start the print with AMS mapping, plate_id and print options

+ 54 - 37
backend/app/services/printer_manager.py

@@ -100,6 +100,8 @@ class PrinterManager:
         self._loop: asyncio.AbstractEventLoop | None = None
         # Track who started the current print (Issue #206)
         self._current_print_user: dict[int, dict] = {}  # {printer_id: {"user_id": int, "username": str}}
+        # Track plate-cleared acknowledgments for queue flow
+        self._plate_cleared: set[int] = set()  # printer_ids where user confirmed plate is cleared
 
     def get_printer(self, printer_id: int) -> PrinterInfo | None:
         """Get printer info by ID."""
@@ -117,6 +119,18 @@ class PrinterManager:
         """Clear the current print user when print completes (Issue #206)."""
         self._current_print_user.pop(printer_id, None)
 
+    def set_plate_cleared(self, printer_id: int):
+        """Mark that user has cleared the build plate for this printer."""
+        self._plate_cleared.add(printer_id)
+
+    def is_plate_cleared(self, printer_id: int) -> bool:
+        """Check if user has confirmed the plate is cleared."""
+        return printer_id in self._plate_cleared
+
+    def consume_plate_cleared(self, printer_id: int):
+        """Clear the plate-cleared flag (called when scheduler starts next print)."""
+        self._plate_cleared.discard(printer_id)
+
     def set_event_loop(self, loop: asyncio.AbstractEventLoop):
         """Set the event loop for async callbacks."""
         self._loop = loop
@@ -209,18 +223,18 @@ class PrinterManager:
         await asyncio.sleep(1)
         return client.state.connected
 
-    def disconnect_printer(self, printer_id: int):
+    def disconnect_printer(self, printer_id: int, timeout: float = 0):
         """Disconnect from a printer."""
         if printer_id in self._clients:
-            self._clients[printer_id].disconnect()
+            self._clients[printer_id].disconnect(timeout=timeout)
             del self._clients[printer_id]
         self._models.pop(printer_id, None)  # Clean up model cache
         self._printer_info.pop(printer_id, None)  # Clean up printer info cache
 
-    def disconnect_all(self):
+    def disconnect_all(self, timeout: float = 0):
         """Disconnect from all printers."""
         for printer_id in list(self._clients.keys()):
-            self.disconnect_printer(printer_id)
+            self.disconnect_printer(printer_id, timeout=timeout)
 
     def get_status(self, printer_id: int) -> PrinterState | None:
         """Get the current status of a printer (checks for stale connections)."""
@@ -488,7 +502,7 @@ def printer_state_to_dict(state: PrinterState, printer_id: int | None = None, mo
     """
     # Parse AMS data from raw_data
     ams_units = []
-    vt_tray = None
+    vt_tray = []
     raw_data = state.raw_data or {}
 
     # Build K-profile lookup map: cali_idx -> k_value
@@ -519,7 +533,7 @@ def printer_state_to_dict(state: PrinterState, printer_id: int | None = None, mo
 
                 trays.append(
                     {
-                        "id": tray.get("id", 0),
+                        "id": int(tray.get("id", 0)),
                         "tray_color": tray.get("tray_color"),
                         "tray_type": tray.get("tray_type"),
                         "tray_sub_brands": tray.get("tray_sub_brands"),
@@ -556,7 +570,7 @@ def printer_state_to_dict(state: PrinterState, printer_id: int | None = None, mo
 
             ams_units.append(
                 {
-                    "id": ams_data.get("id", 0),
+                    "id": int(ams_data.get("id", 0)),
                     "humidity": humidity_value,
                     "temp": ams_data.get("temp"),
                     "is_ams_ht": is_ams_ht,
@@ -564,37 +578,40 @@ def printer_state_to_dict(state: PrinterState, printer_id: int | None = None, mo
                 }
             )
 
-    # Parse virtual tray (external spool)
+    # Parse virtual tray (external spool) — now a list
     if "vt_tray" in raw_data:
-        vt_data = raw_data["vt_tray"]
-        vt_tag_uid = vt_data.get("tag_uid")
-        if vt_tag_uid in ("", "0000000000000000"):
-            vt_tag_uid = None
-        vt_tray_uuid = vt_data.get("tray_uuid")
-        if vt_tray_uuid in ("", "00000000000000000000000000000000"):
-            vt_tray_uuid = None
-
-        # Get K value for vt_tray
-        vt_k_value = vt_data.get("k")
-        vt_cali_idx = vt_data.get("cali_idx")
-        if vt_k_value is None and vt_cali_idx is not None and vt_cali_idx in kprofile_map:
-            vt_k_value = kprofile_map[vt_cali_idx]
-
-        vt_tray = {
-            "id": 254,
-            "tray_color": vt_data.get("tray_color"),
-            "tray_type": vt_data.get("tray_type"),
-            "tray_sub_brands": vt_data.get("tray_sub_brands"),
-            "tray_id_name": vt_data.get("tray_id_name"),
-            "tray_info_idx": vt_data.get("tray_info_idx"),
-            "remain": vt_data.get("remain", 0),
-            "k": vt_k_value,
-            "cali_idx": vt_cali_idx,
-            "tag_uid": vt_tag_uid,
-            "tray_uuid": vt_tray_uuid,
-            "nozzle_temp_min": vt_data.get("nozzle_temp_min"),
-            "nozzle_temp_max": vt_data.get("nozzle_temp_max"),
-        }
+        for vt_data in raw_data["vt_tray"]:
+            vt_tag_uid = vt_data.get("tag_uid")
+            if vt_tag_uid in ("", "0000000000000000"):
+                vt_tag_uid = None
+            vt_tray_uuid = vt_data.get("tray_uuid")
+            if vt_tray_uuid in ("", "00000000000000000000000000000000"):
+                vt_tray_uuid = None
+
+            # Get K value for vt_tray
+            vt_k_value = vt_data.get("k")
+            vt_cali_idx = vt_data.get("cali_idx")
+            if vt_k_value is None and vt_cali_idx is not None and vt_cali_idx in kprofile_map:
+                vt_k_value = kprofile_map[vt_cali_idx]
+
+            tray_id = int(vt_data.get("id", 254))
+            vt_tray.append(
+                {
+                    "id": tray_id,
+                    "tray_color": vt_data.get("tray_color"),
+                    "tray_type": vt_data.get("tray_type"),
+                    "tray_sub_brands": vt_data.get("tray_sub_brands"),
+                    "tray_id_name": vt_data.get("tray_id_name"),
+                    "tray_info_idx": vt_data.get("tray_info_idx"),
+                    "remain": vt_data.get("remain", 0),
+                    "k": vt_k_value,
+                    "cali_idx": vt_cali_idx,
+                    "tag_uid": vt_tag_uid,
+                    "tray_uuid": vt_tray_uuid,
+                    "nozzle_temp_min": vt_data.get("nozzle_temp_min"),
+                    "nozzle_temp_max": vt_data.get("nozzle_temp_max"),
+                }
+            )
 
     # Get ams_extruder_map from raw_data (populated by MQTT handler from AMS info field)
     ams_extruder_map = raw_data.get("ams_extruder_map", {})

+ 310 - 0
backend/app/services/spool_tag_matcher.py

@@ -0,0 +1,310 @@
+"""RFID tag matching and auto-assignment for spool inventory."""
+
+import logging
+
+from sqlalchemy import func, select
+from sqlalchemy.ext.asyncio import AsyncSession
+from sqlalchemy.orm import selectinload
+
+from backend.app.models.spool import Spool
+from backend.app.models.spool_assignment import SpoolAssignment
+
+logger = logging.getLogger(__name__)
+
+# Zero-value constants for tag validation
+ZERO_TAG_UID = "0000000000000000"
+ZERO_TRAY_UUID = "00000000000000000000000000000000"
+
+
+def is_valid_tag(tag_uid: str, tray_uuid: str) -> bool:
+    """Check if a tag/UUID pair contains a non-zero, non-empty value."""
+    uid_valid = bool(tag_uid) and tag_uid != ZERO_TAG_UID and tag_uid != "0" * len(tag_uid)
+    uuid_valid = bool(tray_uuid) and tray_uuid != ZERO_TRAY_UUID and tray_uuid != "0" * len(tray_uuid)
+    return uid_valid or uuid_valid
+
+
+def is_bambu_tag(tag_uid: str, tray_uuid: str, tray_info_idx: str) -> bool:
+    """Check if an AMS tray contains a Bambu Lab RFID spool (has valid UUID or slicer preset)."""
+    uuid_valid = bool(tray_uuid) and tray_uuid != ZERO_TRAY_UUID and tray_uuid != "0" * len(tray_uuid)
+    has_preset = bool(tray_info_idx)
+    return uuid_valid or (is_valid_tag(tag_uid, tray_uuid) and has_preset)
+
+
+async def create_spool_from_tray(db: AsyncSession, tray_data: dict) -> Spool:
+    """Create a new Spool inventory entry from AMS tray MQTT data.
+
+    Extracts material, subtype, color, temps, and tag info from the tray dict.
+    Looks up core_weight from the spool catalog if a Bambu Lab entry matches.
+    """
+    from backend.app.models.color_catalog import ColorCatalogEntry
+    from backend.app.models.spool_catalog import SpoolCatalogEntry
+
+    tray_type = tray_data.get("tray_type", "")  # "PLA"
+    tray_sub_brands = tray_data.get("tray_sub_brands", "")  # "PLA Basic"
+    tray_color = tray_data.get("tray_color", "FFFFFFFF")  # RRGGBBAA
+    tray_id_name = tray_data.get("tray_id_name", "")  # Color name e.g. "Jade White"
+    tag_uid = tray_data.get("tag_uid", "")
+    tray_uuid = tray_data.get("tray_uuid", "")
+    tray_info_idx = tray_data.get("tray_info_idx", "")
+    nozzle_min = tray_data.get("nozzle_temp_min", 0)
+    nozzle_max = tray_data.get("nozzle_temp_max", 0)
+    label_weight = int(tray_data.get("tray_weight", 1000))
+
+    # Parse material and subtype from tray_sub_brands ("PLA Basic" → material="PLA", subtype="Basic")
+    material = tray_type or "PLA"
+    subtype = None
+    if tray_sub_brands and " " in tray_sub_brands:
+        parts = tray_sub_brands.split(" ", 1)
+        if parts[0].upper() == material.upper():
+            subtype = parts[1]
+        else:
+            # tray_sub_brands is the full material name (e.g. "PETG-HF")
+            material = tray_sub_brands
+    elif tray_sub_brands and tray_sub_brands.upper() != material.upper():
+        material = tray_sub_brands
+
+    # Resolve color name from tray_id_name code, hex catalog, or raw tray_id_name
+    from backend.app.core.bambu_colors import resolve_bambu_color_name
+
+    rgba = tray_color if tray_color else None
+    color_name = None
+
+    # 1. Try Bambu color code mapping (e.g. "A06-D0" → "Titan Gray")
+    if tray_id_name:
+        color_name = resolve_bambu_color_name(tray_id_name)
+        logger.info("Color resolve: tray_id_name=%r → resolved=%r", tray_id_name, color_name)
+        # If not a known code, use tray_id_name directly (it may be a readable name)
+        if not color_name and "-" not in tray_id_name:
+            color_name = tray_id_name
+    else:
+        logger.info("Color resolve: tray_id_name is empty, rgba=%r", rgba)
+
+    # 2. Try color catalog lookup by hex color
+    if not color_name and rgba and len(rgba) >= 6:
+        hex_prefix = f"#{rgba[:6].upper()}"
+        cat_result = await db.execute(
+            select(ColorCatalogEntry)
+            .where(func.upper(ColorCatalogEntry.hex_color) == hex_prefix)
+            .where(func.upper(ColorCatalogEntry.manufacturer) == "BAMBU LAB")
+            .limit(1)
+        )
+        entry = cat_result.scalar_one_or_none()
+        if entry:
+            color_name = entry.color_name
+
+    # Look up core weight from spool catalog
+    core_weight = 250  # Default for Bambu Lab plastic spools
+    cat_result = await db.execute(select(SpoolCatalogEntry).where(SpoolCatalogEntry.name.ilike("Bambu Lab%")).limit(10))
+    for entry in cat_result.scalars().all():
+        # Pick the best match (prefer exact, fallback to first Bambu Lab entry)
+        core_weight = entry.weight
+        break
+
+    # Resolve slicer filament name from builtin table
+    slicer_filament_name = None
+    if tray_info_idx:
+        try:
+            from backend.app.api.routes.cloud import _BUILTIN_FILAMENT_NAMES
+
+            slicer_filament_name = _BUILTIN_FILAMENT_NAMES.get(tray_info_idx)
+        except Exception:
+            pass
+        # Fallback: use tray_sub_brands as the display name
+        if not slicer_filament_name and tray_sub_brands:
+            slicer_filament_name = tray_sub_brands
+
+    # Calculate initial weight_used from AMS remain percentage
+    remain_raw = tray_data.get("remain")
+    try:
+        remain_pct = int(remain_raw) if remain_raw is not None else 100
+    except (TypeError, ValueError):
+        remain_pct = 100
+    # Clamp to valid range: negative means unknown, >100 is invalid
+    if remain_pct < 0 or remain_pct > 100:
+        remain_pct = 100  # Unknown → assume full
+    weight_used = round(label_weight * (100 - remain_pct) / 100.0, 1)
+
+    spool = Spool(
+        material=material,
+        subtype=subtype,
+        color_name=color_name,
+        rgba=rgba,
+        brand="Bambu Lab",
+        label_weight=label_weight,
+        core_weight=core_weight,
+        weight_used=weight_used,
+        slicer_filament=tray_info_idx or None,
+        slicer_filament_name=slicer_filament_name,
+        nozzle_temp_min=int(nozzle_min) if nozzle_min else None,
+        nozzle_temp_max=int(nozzle_max) if nozzle_max else None,
+        tag_uid=tag_uid if tag_uid and tag_uid != ZERO_TAG_UID else None,
+        tray_uuid=tray_uuid if tray_uuid and tray_uuid != ZERO_TRAY_UUID else None,
+        data_origin="rfid_auto",
+        tag_type="bambulab",
+    )
+    db.add(spool)
+    await db.flush()
+
+    logger.info(
+        "Auto-created spool %d from AMS tray data: %s %s %s (tag=%s uuid=%s)",
+        spool.id,
+        material,
+        subtype or "",
+        color_name or "",
+        tag_uid,
+        tray_uuid,
+    )
+    return spool
+
+
+async def get_spool_by_tag(db: AsyncSession, tag_uid: str, tray_uuid: str) -> Spool | None:
+    """Look up an active spool by RFID tag UID or Bambu Lab tray UUID.
+
+    Prefers tray_uuid match over tag_uid (more reliable).
+    """
+    # Try tray_uuid first (Bambu Lab spools — more reliable)
+    if tray_uuid and tray_uuid != ZERO_TRAY_UUID and tray_uuid != "0" * len(tray_uuid):
+        result = await db.execute(
+            select(Spool)
+            .options(selectinload(Spool.k_profiles))
+            .where(Spool.tray_uuid == tray_uuid, Spool.archived_at.is_(None))
+            .limit(1)
+        )
+        spool = result.scalar_one_or_none()
+        if spool:
+            return spool
+
+    # Fall back to tag_uid
+    if tag_uid and tag_uid != ZERO_TAG_UID and tag_uid != "0" * len(tag_uid):
+        result = await db.execute(
+            select(Spool)
+            .options(selectinload(Spool.k_profiles))
+            .where(Spool.tag_uid == tag_uid, Spool.archived_at.is_(None))
+            .limit(1)
+        )
+        spool = result.scalar_one_or_none()
+        if spool:
+            return spool
+
+    return None
+
+
+async def auto_assign_spool(
+    printer_id: int,
+    ams_id: int,
+    tray_id: int,
+    spool: Spool,
+    printer_manager,
+    db: AsyncSession,
+    tray_info_idx: str = "",
+) -> SpoolAssignment:
+    """Create a SpoolAssignment and auto-configure the AMS slot via MQTT.
+
+    For BL spools (RFID-detected), only K-profile commands are sent.
+    ams_set_filament_setting is NOT sent because the firmware already has
+    filament configuration from the RFID tag, and sending it would destroy
+    the RFID-detected state (eye → pen icon in BambuStudio).
+    """
+    # Get current tray state for fingerprint
+    fingerprint_color = None
+    fingerprint_type = None
+    tray = None
+    state = printer_manager.get_status(printer_id)
+    if state and state.raw_data:
+        from backend.app.api.routes.inventory import _find_tray_in_ams_data
+
+        ams = state.raw_data.get("ams", [])
+        if isinstance(ams, dict):
+            ams = ams.get("ams", [])
+        tray = _find_tray_in_ams_data(
+            ams,
+            ams_id,
+            tray_id,
+        )
+        if tray:
+            fingerprint_color = tray.get("tray_color", "")
+            fingerprint_type = tray.get("tray_type", "")
+
+    # Upsert: remove old assignment for this slot
+    existing = await db.execute(
+        select(SpoolAssignment).where(
+            SpoolAssignment.printer_id == printer_id,
+            SpoolAssignment.ams_id == ams_id,
+            SpoolAssignment.tray_id == tray_id,
+        )
+    )
+    old = existing.scalar_one_or_none()
+    if old:
+        await db.delete(old)
+        await db.flush()
+
+    assignment = SpoolAssignment(
+        spool_id=spool.id,
+        printer_id=printer_id,
+        ams_id=ams_id,
+        tray_id=tray_id,
+        fingerprint_color=fingerprint_color,
+        fingerprint_type=fingerprint_type,
+    )
+    db.add(assignment)
+    await db.flush()
+
+    # Apply K-profile via MQTT (if available)
+    # NOTE: Do NOT send ams_set_filament_setting here. This function is only
+    # called for BL spools (RFID-detected). The firmware already has the filament
+    # configuration from the RFID tag. Sending ams_set_filament_setting would
+    # destroy the RFID-detected state (eye → pen icon in BambuStudio/OrcaSlicer).
+    try:
+        client = printer_manager.get_client(printer_id)
+        if client:
+            # Apply K-profile if available
+            nozzle_diameter = "0.4"
+            if state and state.nozzles:
+                nd = state.nozzles[0].nozzle_diameter
+                if nd:
+                    nozzle_diameter = nd
+
+            matching_kp = None
+            for kp in spool.k_profiles:
+                if kp.printer_id == printer_id and kp.nozzle_diameter == nozzle_diameter:
+                    matching_kp = kp
+                    break
+
+            if matching_kp and matching_kp.cali_idx is not None:
+                # The filament_id in extrusion_cali_sel must match the filament preset
+                # under which the K-profile was calibrated. Use spool.slicer_filament
+                # (the preset assigned in inventory), falling back to tray's RFID value.
+                cali_filament_id = spool.slicer_filament or tray_info_idx or ""
+                client.extrusion_cali_sel(
+                    ams_id=ams_id,
+                    tray_id=tray_id,
+                    cali_idx=matching_kp.cali_idx,
+                    filament_id=cali_filament_id,
+                    nozzle_diameter=nozzle_diameter,
+                )
+
+                # NOTE: Do NOT send extrusion_cali_set here. extrusion_cali_sel already
+                # selected the correct profile by cali_idx. Sending extrusion_cali_set
+                # with the same cali_idx would MODIFY the existing profile's metadata
+                # (extruder_id, nozzle_id, name), corrupting it.
+
+                logger.info(
+                    "Applied K-profile cali_idx=%d for spool %d on printer %d AMS%d-T%d",
+                    matching_kp.cali_idx,
+                    spool.id,
+                    printer_id,
+                    ams_id,
+                    tray_id,
+                )
+
+            logger.info(
+                "Auto-assigned spool %d to printer %d AMS%d-T%d (RFID match)",
+                spool.id,
+                printer_id,
+                ams_id,
+                tray_id,
+            )
+    except Exception as e:
+        logger.warning("K-profile apply failed for spool %d (RFID match): %s", spool.id, e)
+
+    return assignment

+ 108 - 55
backend/app/services/spoolman.py

@@ -468,6 +468,26 @@ class SpoolmanClient:
                         return spool
         return None
 
+    def _find_spool_by_location(self, location: str, cached_spools: list[dict] | None) -> dict | None:
+        """Find a spool by exact location match.
+
+        Used as fallback when RFID tag data is unavailable (e.g., newer firmware
+        that doesn't expose tray_uuid/tag_uid via MQTT).
+
+        Args:
+            location: Exact location string (e.g., "H2D-1 - AMS A1")
+            cached_spools: Pre-fetched list of spools to search
+
+        Returns:
+            Spool dictionary or None if not found.
+        """
+        if not cached_spools:
+            return None
+        for spool in cached_spools:
+            if spool.get("location") == location:
+                return spool
+        return None
+
     async def find_spools_by_location_prefix(
         self, location_prefix: str, cached_spools: list[dict] | None = None
     ) -> list[dict]:
@@ -494,17 +514,21 @@ class SpoolmanClient:
         printer_name: str,
         current_tray_uuids: set[str],
         cached_spools: list[dict] | None = None,
+        synced_spool_ids: set[int] | None = None,
     ) -> int:
         """Clear location for spools that are no longer in the AMS.
 
         When a spool is removed from the AMS, its location should be cleared
         in Spoolman. This method finds all spools with locations for this printer
-        and clears the location for any that are not in the current_tray_uuids set.
+        and clears the location for any that are not in the current_tray_uuids set
+        and were not synced in this cycle (synced_spool_ids).
 
         Args:
             printer_name: The printer name used as location prefix
             current_tray_uuids: Set of tray_uuids currently in the AMS
             cached_spools: Optional pre-fetched list of spools to search (avoids API call)
+            synced_spool_ids: Set of spool IDs that were synced in this cycle
+                (protects location-matched spools when RFID data is unavailable)
 
         Returns:
             Number of spools whose location was cleared.
@@ -514,6 +538,12 @@ class SpoolmanClient:
         cleared_count = 0
 
         for spool in spools_at_printer:
+            spool_id = spool.get("id")
+
+            # Skip spools that were just synced (matched by location or tag)
+            if synced_spool_ids and spool_id in synced_spool_ids:
+                continue
+
             # Get the tray_uuid (stored as "tag" in extra field)
             extra = spool.get("extra", {}) or {}
             stored_tag = extra.get("tag", "")
@@ -526,10 +556,10 @@ class SpoolmanClient:
             # If this spool's UUID is not in the current AMS, clear its location
             if spool_uuid not in current_tray_uuids:
                 logger.info(
-                    f"Clearing location for spool {spool['id']} "
+                    f"Clearing location for spool {spool_id} "
                     f"(was: {spool.get('location')}, uuid: {spool_uuid[:16] if spool_uuid else 'none'}...)"
                 )
-                result = await self.update_spool(spool_id=spool["id"], clear_location=True)
+                result = await self.update_spool(spool_id=spool_id, clear_location=True)
                 if result:
                     cleared_count += 1
 
@@ -628,8 +658,8 @@ class SpoolmanClient:
         # Get tray_info_idx (Bambu filament preset ID like "GFA00")
         tray_info_idx = tray_data.get("tray_info_idx", "") or ""
 
-        # Get remaining percentage, ensure non-negative
-        remain = max(0, int(tray_data.get("remain", 0)))
+        # Get remaining percentage (-1 means unknown/not read by AMS)
+        remain = int(tray_data.get("remain", -1))
 
         return AMSTray(
             ams_id=ams_id,
@@ -663,31 +693,22 @@ class SpoolmanClient:
     def is_bambu_lab_spool(self, tray_uuid: str, tag_uid: str = "", tray_info_idx: str = "") -> bool:
         """Check if a tray has a valid Bambu Lab spool.
 
-        Bambu Lab spools can be identified by:
+        Bambu Lab spools are identified by hardware RFID identifiers only:
         1. tray_uuid: 32-character hex string (preferred, consistent across printers)
         2. tag_uid: 16-character hex string (RFID tag, varies between readers)
-        3. tray_info_idx: Bambu filament preset ID like "GFA00" (most reliable)
 
-        Non-Bambu Lab spools (SpoolEase, third-party) won't have these identifiers.
+        Note: tray_info_idx (e.g. "GFA00") is NOT a reliable indicator — third-party
+        spools using Bambu generic presets also have GF-prefixed tray_info_idx values.
+        The tray_info_idx parameter is kept for API compatibility but ignored.
 
         Args:
             tray_uuid: The tray UUID to check (32 hex chars)
             tag_uid: The RFID tag UID to check as fallback (16 hex chars)
-            tray_info_idx: Bambu filament preset ID like "GFA00", "GFB00"
+            tray_info_idx: Ignored (kept for API compatibility)
 
         Returns:
-            True if the spool has valid Bambu Lab identifiers, False otherwise.
+            True if the spool has valid Bambu Lab RFID identifiers, False otherwise.
         """
-        # Check tray_info_idx first - Bambu filament preset IDs like "GFA00", "GFB00", etc.
-        # This is the most reliable indicator as it's set when the spool is recognized
-        if tray_info_idx:
-            idx = tray_info_idx.strip()
-            # Bambu Lab preset IDs start with "GF" followed by letter and digits
-            # e.g., GFA00, GFB00, GFL00, GFN00, GFG00, GFS00, GFU00
-            if idx and len(idx) >= 3 and idx.startswith("GF"):
-                logger.debug("Identified Bambu Lab spool via tray_info_idx: %s", idx)
-                return True
-
         # Check tray_uuid (preferred - consistent across printer models)
         if tray_uuid:
             uuid = tray_uuid.strip()
@@ -730,6 +751,7 @@ class SpoolmanClient:
         printer_name: str,
         disable_weight_sync: bool = False,
         cached_spools: list[dict] | None = None,
+        inventory_remaining: float | None = None,
     ) -> dict | None:
         """Sync a single AMS tray to Spoolman.
 
@@ -747,6 +769,8 @@ class SpoolmanClient:
             cached_spools: Optional pre-fetched list of spools to search (avoids API calls).
                 When provided, this cache is passed to find_spool_by_tag to avoid redundant
                 API calls during batch sync operations.
+            inventory_remaining: Optional fallback remaining weight (grams) from the built-in
+                inventory when AMS MQTT data has invalid remain/tray_weight values.
 
         Returns:
             Synced spool dictionary or None if skipped or failed.
@@ -770,53 +794,82 @@ class SpoolmanClient:
             return None
 
         # Determine which identifier to use for Spoolman (prefer tray_uuid, fallback to tag_uid)
-        spool_tag = (
-            tray.tray_uuid if tray.tray_uuid and tray.tray_uuid != "00000000000000000000000000000000" else tray.tag_uid
-        )
-
-        # If no unique identifier available, we can't sync even if it's a Bambu Lab spool
-        if not spool_tag:
-            logger.warning(
-                f"Bambu Lab spool detected but no unique identifier for Spoolman: "
-                f"{printer_name} AMS {tray.ams_id} tray {tray.tray_id} (tray_info_idx={tray.tray_info_idx})"
-            )
-            return None
+        # Zero-filled values mean the AMS hasn't read the RFID tag — treat as no tag
+        zero_uuid = "00000000000000000000000000000000"
+        zero_tag = "0000000000000000"
+        spool_tag = None
+        if tray.tray_uuid and tray.tray_uuid != zero_uuid:
+            spool_tag = tray.tray_uuid
+        elif tray.tag_uid and tray.tag_uid != zero_tag:
+            spool_tag = tray.tag_uid
 
         # Calculate remaining weight
-        remaining = self.calculate_remaining_weight(tray.remain, tray.tray_weight)
+        # Primary: AMS MQTT data (remain percentage + tray_weight)
+        # Fallback: Built-in inventory tracked weight (when firmware sends invalid remain/tray_weight)
+        if tray.remain >= 0 and tray.tray_weight > 0:
+            remaining = self.calculate_remaining_weight(tray.remain, tray.tray_weight)
+        elif inventory_remaining is not None:
+            remaining = inventory_remaining
+            logger.debug(
+                "Using inventory weight fallback for %s AMS %s tray %s: %.1fg",
+                printer_name,
+                tray.ams_id,
+                tray.tray_id,
+                remaining,
+            )
+        else:
+            remaining = None
         location = f"{printer_name} - {self.convert_ams_slot_to_location(tray.ams_id, tray.tray_id)}"
 
-        # Find existing spool by tag (tray_uuid or tag_uid, stored as "tag" in Spoolman)
-        existing = await self.find_spool_by_tag(spool_tag, cached_spools=cached_spools)
+        if spool_tag:
+            # Primary path: match by RFID tag
+            existing = await self.find_spool_by_tag(spool_tag, cached_spools=cached_spools)
+            if existing:
+                logger.info("Updating existing spool %s for tag %s...", existing["id"], spool_tag[:16])
+                return await self.update_spool(
+                    spool_id=existing["id"],
+                    remaining_weight=None if disable_weight_sync else remaining,
+                    location=location,
+                )
+
+            # Spool not found by tag - auto-create it
+            logger.info("Creating new spool in Spoolman for %s (tag: %s...)", tray.tray_sub_brands, spool_tag[:16])
+            filament = await self._find_or_create_filament(tray)
+            if not filament:
+                logger.error("Failed to find or create filament for %s", tray.tray_sub_brands)
+                return None
+
+            import json
+
+            return await self.create_spool(
+                filament_id=filament["id"],
+                remaining_weight=remaining,
+                location=location,
+                comment="Created by Bambuddy",
+                extra={"tag": json.dumps(spool_tag)},
+            )
+
+        # Fallback path: no RFID tag available (newer firmware may not expose UUIDs)
+        # Only update existing spools matched by location — never create new ones without a tag
+        # to avoid duplicates when old spools exist from previous RFID-based syncs
+        existing = self._find_spool_by_location(location, cached_spools)
         if existing:
-            # Update existing spool
-            logger.info("Updating existing spool %s for tag %s...", existing["id"], spool_tag[:16])
+            logger.info(
+                "Updating spool %s by location match '%s' (no RFID tag available)",
+                existing["id"],
+                location,
+            )
             return await self.update_spool(
                 spool_id=existing["id"],
                 remaining_weight=None if disable_weight_sync else remaining,
                 location=location,
             )
 
-        # Spool not found - auto-create it
-        logger.info("Creating new spool in Spoolman for %s (tag: %s...)", tray.tray_sub_brands, spool_tag[:16])
-
-        # First find or create the filament type
-        filament = await self._find_or_create_filament(tray)
-        if not filament:
-            logger.error("Failed to find or create filament for %s", tray.tray_sub_brands)
-            return None
-
-        # Create the spool with identifier stored as "tag" in extra field
-        # Note: Spoolman extra field values must be valid JSON, so we encode the string
-        import json
-
-        return await self.create_spool(
-            filament_id=filament["id"],
-            remaining_weight=remaining,
-            location=location,
-            comment="Created by Bambuddy",
-            extra={"tag": json.dumps(spool_tag)},
+        logger.info(
+            "No existing spool found at '%s' — skipping (no RFID tag to create with)",
+            location,
         )
+        return None
 
     async def _find_or_create_filament(self, tray: AMSTray) -> dict | None:
         """Find existing filament or create new one.

+ 9 - 8
backend/app/services/spoolman_tracking.py

@@ -66,14 +66,15 @@ def build_ams_tray_lookup(raw_data: dict) -> dict[int, dict]:
                 "tray_type": tray.get("tray_type", ""),
             }
 
-    # External spool (global_tray_id = 254)
-    vt_tray = raw_data.get("vt_tray")
-    if vt_tray and vt_tray.get("tray_type"):
-        lookup[254] = {
-            "tray_uuid": vt_tray.get("tray_uuid", ""),
-            "tag_uid": vt_tray.get("tag_uid", ""),
-            "tray_type": vt_tray.get("tray_type", ""),
-        }
+    # External spool(s) (vt_tray is a list, global_tray_id from each entry's "id")
+    for vt in raw_data.get("vt_tray") or []:
+        if vt.get("tray_type"):
+            tray_id = int(vt.get("id", 254))
+            lookup[tray_id] = {
+                "tray_uuid": vt.get("tray_uuid", ""),
+                "tag_uid": vt.get("tag_uid", ""),
+                "tray_type": vt.get("tray_type", ""),
+            }
 
     return lookup
 

+ 420 - 0
backend/app/services/usage_tracker.py

@@ -0,0 +1,420 @@
+"""Automatic filament consumption tracking.
+
+Captures AMS tray remain% at print start, then computes consumption
+deltas at print complete to update spool weight_used and last_used.
+
+Primary tracking uses 3MF slicer estimates (precise per-filament data).
+AMS remain% delta is the fallback for trays not covered by 3MF data.
+"""
+
+import json
+import logging
+from dataclasses import dataclass, field
+from datetime import datetime, timezone
+
+from sqlalchemy import select
+from sqlalchemy.ext.asyncio import AsyncSession
+
+from backend.app.models.spool import Spool
+from backend.app.models.spool_assignment import SpoolAssignment
+from backend.app.models.spool_usage_history import SpoolUsageHistory
+
+logger = logging.getLogger(__name__)
+
+
+@dataclass
+class PrintSession:
+    printer_id: int
+    print_name: str
+    started_at: datetime
+    tray_remain_start: dict[tuple[int, int], int] = field(default_factory=dict)
+
+
+# Module-level storage, keyed by printer_id
+_active_sessions: dict[int, PrintSession] = {}
+
+
+async def on_print_start(printer_id: int, data: dict, printer_manager) -> None:
+    """Capture AMS tray remain% at print start."""
+    state = printer_manager.get_status(printer_id)
+    if not state or not state.raw_data:
+        logger.debug("[UsageTracker] No state for printer %d, skipping", printer_id)
+        return
+
+    ams_raw = state.raw_data.get("ams", [])
+    ams_data = ams_raw.get("ams", []) if isinstance(ams_raw, dict) else ams_raw if isinstance(ams_raw, list) else []
+    if not ams_data:
+        logger.debug("[UsageTracker] No AMS data for printer %d, skipping", printer_id)
+        return
+
+    tray_remain_start: dict[tuple[int, int], int] = {}
+    for ams_unit in ams_data:
+        ams_id = int(ams_unit.get("id", 0))
+        for tray in ams_unit.get("tray", []):
+            tray_id = int(tray.get("id", 0))
+            remain = tray.get("remain", -1)
+            if isinstance(remain, int) and 0 <= remain <= 100:
+                tray_remain_start[(ams_id, tray_id)] = remain
+
+    print_name = data.get("subtask_name", "") or data.get("filename", "unknown")
+
+    # Always create session (even without valid remain data) so print_name
+    # is available at completion for 3MF-based tracking
+    session = PrintSession(
+        printer_id=printer_id,
+        print_name=print_name,
+        started_at=datetime.now(timezone.utc),
+        tray_remain_start=tray_remain_start,
+    )
+    _active_sessions[printer_id] = session
+
+    if tray_remain_start:
+        logger.info(
+            "[UsageTracker] Captured start remain%% for printer %d (%d trays): %s",
+            printer_id,
+            len(tray_remain_start),
+            {f"{k[0]}-{k[1]}": v for k, v in tray_remain_start.items()},
+        )
+    else:
+        logger.debug("[UsageTracker] No valid remain%% for printer %d, 3MF fallback available", printer_id)
+
+
+async def on_print_complete(
+    printer_id: int,
+    data: dict,
+    printer_manager,
+    db: AsyncSession,
+    archive_id: int | None = None,
+) -> list[dict]:
+    """Compute consumption deltas and update spool weight_used/last_used.
+
+    Uses two tracking strategies in priority order:
+    1. 3MF per-filament estimates (primary) — precise slicer data for all spools
+    2. AMS remain% delta (fallback) — only for trays not already handled by 3MF
+
+    Returns a list of dicts describing what was logged (for WebSocket broadcast).
+    """
+    session = _active_sessions.pop(printer_id, None)
+    status = data.get("status", "completed")
+    results = []
+    handled_trays: set[tuple[int, int]] = set()
+
+    # --- Path 1 (PRIMARY): 3MF per-filament estimates ---
+    if archive_id:
+        print_name = (
+            (session.print_name if session else None) or data.get("subtask_name", "") or data.get("filename", "unknown")
+        )
+        threemf_results = await _track_from_3mf(
+            printer_id, archive_id, status, print_name, handled_trays, printer_manager, db
+        )
+        results.extend(threemf_results)
+
+    # --- Path 2 (FALLBACK): AMS remain% delta (only for trays not handled by 3MF) ---
+    if session and session.tray_remain_start:
+        state = printer_manager.get_status(printer_id)
+        if state and state.raw_data:
+            ams_raw = state.raw_data.get("ams", [])
+            ams_data = (
+                ams_raw.get("ams", []) if isinstance(ams_raw, dict) else ams_raw if isinstance(ams_raw, list) else []
+            )
+
+            for ams_unit in ams_data:
+                ams_id = int(ams_unit.get("id", 0))
+                for tray in ams_unit.get("tray", []):
+                    tray_id = int(tray.get("id", 0))
+                    key = (ams_id, tray_id)
+
+                    if key in handled_trays:
+                        continue  # Already tracked via 3MF
+
+                    if key not in session.tray_remain_start:
+                        continue
+
+                    current_remain = tray.get("remain", -1)
+                    if not isinstance(current_remain, int) or current_remain < 0 or current_remain > 100:
+                        continue
+
+                    start_remain = session.tray_remain_start[key]
+                    delta_pct = start_remain - current_remain
+
+                    if delta_pct <= 0:
+                        continue  # No consumption or tray was refilled
+
+                    # Look up SpoolAssignment for this slot
+                    result = await db.execute(
+                        select(SpoolAssignment).where(
+                            SpoolAssignment.printer_id == printer_id,
+                            SpoolAssignment.ams_id == ams_id,
+                            SpoolAssignment.tray_id == tray_id,
+                        )
+                    )
+                    assignment = result.scalar_one_or_none()
+                    if not assignment:
+                        continue
+
+                    # Load spool
+                    spool_result = await db.execute(select(Spool).where(Spool.id == assignment.spool_id))
+                    spool = spool_result.scalar_one_or_none()
+                    if not spool:
+                        continue
+
+                    # Compute weight consumed
+                    weight_grams = (delta_pct / 100.0) * spool.label_weight
+
+                    # Update spool
+                    spool.weight_used = (spool.weight_used or 0) + weight_grams
+                    spool.last_used = datetime.now(timezone.utc)
+
+                    # Insert usage history record
+                    history = SpoolUsageHistory(
+                        spool_id=spool.id,
+                        printer_id=printer_id,
+                        print_name=session.print_name,
+                        weight_used=round(weight_grams, 1),
+                        percent_used=delta_pct,
+                        status=status,
+                    )
+                    db.add(history)
+
+                    handled_trays.add(key)
+                    results.append(
+                        {
+                            "spool_id": spool.id,
+                            "weight_used": round(weight_grams, 1),
+                            "percent_used": delta_pct,
+                            "ams_id": ams_id,
+                            "tray_id": tray_id,
+                            "material": spool.material,
+                        }
+                    )
+
+                    logger.info(
+                        "[UsageTracker] Spool %d consumed %.1fg (%d%%) on printer %d AMS%d-T%d (AMS fallback, %s)",
+                        spool.id,
+                        weight_grams,
+                        delta_pct,
+                        printer_id,
+                        ams_id,
+                        tray_id,
+                        status,
+                    )
+
+    if results:
+        await db.commit()
+
+    return results
+
+
+async def _track_from_3mf(
+    printer_id: int,
+    archive_id: int,
+    status: str,
+    print_name: str,
+    handled_trays: set[tuple[int, int]],
+    printer_manager,
+    db: AsyncSession,
+) -> list[dict]:
+    """Track usage from 3MF per-filament slicer data (primary path).
+
+    Uses slicer-estimated filament weight for all spools (BL and non-BL).
+    For partial prints (failed/aborted), tries per-layer gcode data first,
+    then falls back to linear scaling by progress.
+
+    Slot-to-tray mapping priority:
+    1. Queue item ams_mapping (for queue-initiated prints)
+    2. tray_now from printer state (for single-filament non-queue prints)
+    3. Default mapping: slot_id - 1 = global_tray_id (last resort)
+    """
+    from backend.app.core.config import settings as app_settings
+    from backend.app.models.archive import PrintArchive
+    from backend.app.models.print_queue import PrintQueueItem
+    from backend.app.utils.threemf_tools import extract_filament_usage_from_3mf
+
+    result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
+    archive = result.scalar_one_or_none()
+    if not archive or not archive.file_path:
+        return []
+
+    file_path = app_settings.base_dir / archive.file_path
+    if not file_path.exists():
+        return []
+
+    filament_usage = extract_filament_usage_from_3mf(file_path)
+    if not filament_usage:
+        return []
+
+    # --- Resolve slot-to-tray mapping ---
+    # 1. Try queue item ams_mapping (queue-initiated prints store the exact mapping)
+    slot_to_tray = None
+    queue_result = await db.execute(
+        select(PrintQueueItem)
+        .where(PrintQueueItem.archive_id == archive_id)
+        .where(PrintQueueItem.status.in_(["printing", "completed", "failed"]))
+    )
+    queue_item = queue_result.scalar_one_or_none()
+    if queue_item and queue_item.ams_mapping:
+        try:
+            slot_to_tray = json.loads(queue_item.ams_mapping)
+        except (json.JSONDecodeError, TypeError):
+            pass
+
+    # 2. For single-filament non-queue prints, use tray_now from printer state
+    nonzero_slots = [u for u in filament_usage if u.get("used_g", 0) > 0]
+    tray_now_override: int | None = None
+    if not slot_to_tray and len(nonzero_slots) == 1:
+        state = printer_manager.get_status(printer_id)
+        if state and 0 <= state.tray_now <= 254:
+            tray_now_override = state.tray_now
+        elif state and state.tray_now == 255:
+            # 255 = "no filament" on legacy printers, but valid 2nd external spool on H2-series
+            vt_tray = state.raw_data.get("vt_tray") or []
+            if any(int(vt.get("id", 0)) == 255 for vt in vt_tray if isinstance(vt, dict)):
+                tray_now_override = state.tray_now
+
+    # Scale factor for partial prints (failed/aborted)
+    if status == "completed":
+        scale = 1.0
+    else:
+        state = printer_manager.get_status(printer_id)
+        progress = state.progress if state else 0
+        scale = max(0.0, min(progress / 100.0, 1.0))
+
+    # Per-layer gcode accuracy for partial prints
+    layer_grams: dict[int, float] | None = None
+    if status != "completed":
+        state = printer_manager.get_status(printer_id)
+        current_layer = state.layer_num if state else 0
+        if current_layer > 0:
+            try:
+                from backend.app.utils.threemf_tools import (
+                    extract_filament_properties_from_3mf,
+                    extract_layer_filament_usage_from_3mf,
+                    get_cumulative_usage_at_layer,
+                    mm_to_grams,
+                )
+
+                layer_usage = extract_layer_filament_usage_from_3mf(file_path)
+                if layer_usage:
+                    cumulative_mm = get_cumulative_usage_at_layer(layer_usage, current_layer)
+                    filament_props = extract_filament_properties_from_3mf(file_path)
+                    layer_grams = {}
+                    for filament_id, mm_used in cumulative_mm.items():
+                        slot_id = filament_id + 1  # 0-based to 1-based
+                        props = filament_props.get(slot_id, {})
+                        density = props.get("density", 1.24)
+                        diameter = props.get("diameter", 1.75)
+                        layer_grams[slot_id] = mm_to_grams(mm_used, diameter, density)
+            except Exception:
+                pass  # Fall back to linear scaling
+
+    results = []
+
+    for usage in filament_usage:
+        slot_id = usage.get("slot_id", 0)
+        used_g = usage.get("used_g", 0)
+        if used_g <= 0:
+            continue
+
+        # Map 3MF slot_id to physical (ams_id, tray_id) using resolved mapping
+        if tray_now_override is not None:
+            # Single-filament non-queue print: use actual tray from printer state
+            global_tray_id = tray_now_override
+        else:
+            # Queue mapping or default: slot_id - 1, overridden by ams_mapping
+            global_tray_id = slot_id - 1
+            if slot_to_tray and slot_id <= len(slot_to_tray):
+                mapped = slot_to_tray[slot_id - 1]
+                if isinstance(mapped, int) and mapped >= 0:
+                    global_tray_id = mapped
+
+        if global_tray_id >= 254:
+            # External spool: ams_id=255 (sentinel), tray_id=slot index (0 or 1)
+            ams_id = 255
+            tray_id = global_tray_id - 254
+        elif global_tray_id >= 128:
+            ams_id = global_tray_id
+            tray_id = 0
+        else:
+            ams_id = global_tray_id // 4
+            tray_id = global_tray_id % 4
+
+        key = (ams_id, tray_id)
+        if key in handled_trays:
+            continue
+
+        # Find spool assignment for this tray
+        assign_result = await db.execute(
+            select(SpoolAssignment).where(
+                SpoolAssignment.printer_id == printer_id,
+                SpoolAssignment.ams_id == ams_id,
+                SpoolAssignment.tray_id == tray_id,
+            )
+        )
+        assignment = assign_result.scalar_one_or_none()
+        if not assignment:
+            continue
+
+        # Load spool
+        spool_result = await db.execute(select(Spool).where(Spool.id == assignment.spool_id))
+        spool = spool_result.scalar_one_or_none()
+        if not spool:
+            continue
+
+        # Use per-layer grams if available, otherwise linear scale
+        if layer_grams and slot_id in layer_grams:
+            weight_grams = layer_grams[slot_id]
+        else:
+            weight_grams = used_g * scale
+
+        if weight_grams <= 0:
+            continue
+
+        # Update spool
+        spool.weight_used = (spool.weight_used or 0) + weight_grams
+        spool.last_used = datetime.now(timezone.utc)
+
+        percent = round(weight_grams / (spool.label_weight or 1000) * 100)
+
+        # Insert usage history record
+        history = SpoolUsageHistory(
+            spool_id=spool.id,
+            printer_id=printer_id,
+            print_name=print_name,
+            weight_used=round(weight_grams, 1),
+            percent_used=percent,
+            status=status,
+        )
+        db.add(history)
+
+        handled_trays.add(key)
+        results.append(
+            {
+                "spool_id": spool.id,
+                "weight_used": round(weight_grams, 1),
+                "percent_used": percent,
+                "ams_id": ams_id,
+                "tray_id": tray_id,
+                "material": spool.material,
+            }
+        )
+
+        # Determine mapping source for debug logging
+        if tray_now_override is not None:
+            map_src = ", tray_now"
+        elif slot_to_tray:
+            map_src = ", queue_map"
+        else:
+            map_src = ""
+        logger.info(
+            "[UsageTracker] Spool %d consumed %.1fg (3MF%s%s) on printer %d AMS%d-T%d (%s)",
+            spool.id,
+            weight_grams,
+            " per-layer" if (layer_grams and slot_id in layer_grams) else (f" scaled {scale:.0%}" if scale < 1 else ""),
+            map_src,
+            printer_id,
+            ams_id,
+            tray_id,
+            status,
+        )
+
+    return results

+ 63 - 0
backend/app/utils/printer_models.py

@@ -48,6 +48,69 @@ PRINTER_MODEL_ID_MAP = {
 }
 
 
+# Rod/rail type classification for maintenance tasks.
+# Carbon rods: X1, P1, P2S series (CoreXY with carbon fiber rods)
+# Linear rails: A1, H2 series (linear rail motion system)
+# Values must be uppercase with spaces stripped for normalized comparison.
+CARBON_ROD_MODELS = frozenset(
+    [
+        # Display names (uppercase, no spaces)
+        "X1",
+        "X1C",
+        "X1E",
+        "P1P",
+        "P1S",
+        "P2S",
+        # Internal codes
+        "C11",  # X1C
+        "C12",  # X1
+        "C13",  # X1E
+        "N7",  # P2S
+    ]
+)
+
+LINEAR_RAIL_MODELS = frozenset(
+    [
+        # Display names (uppercase, no spaces)
+        "A1",
+        "A1MINI",
+        "H2D",
+        "H2DPRO",
+        "H2C",
+        "H2S",
+        # Internal codes
+        "N1",  # A1
+        "N2S",  # A1 Mini
+        "A04",  # A1 Mini (alternate)
+        "A11",  # A1
+        "A12",  # A1 Mini
+        "O1D",  # H2D
+        "O1E",  # H2D Pro
+        "O2D",  # H2D Pro (alternate)
+        "O1C",  # H2C
+        "O1S",  # H2S
+    ]
+)
+
+
+def get_rod_type(model: str | None) -> str | None:
+    """Return the rod/rail type for a printer model.
+
+    Returns:
+        "carbon" for X1/P1/P2S series (carbon fiber rods),
+        "linear_rail" for A1/H2 series (linear rails),
+        None for unknown models.
+    """
+    if not model:
+        return None
+    normalized = model.strip().upper().replace(" ", "").replace("-", "")
+    if normalized in CARBON_ROD_MODELS:
+        return "carbon"
+    if normalized in LINEAR_RAIL_MODELS:
+        return "linear_rail"
+    return None
+
+
 def normalize_printer_model_id(model_id: str | None) -> str | None:
     """Convert printer_model_id (internal code) to normalized short name.
 

+ 56 - 0
backend/app/utils/threemf_tools.py

@@ -264,6 +264,62 @@ def extract_filament_properties_from_3mf(file_path: Path) -> dict[int, dict]:
     return properties
 
 
+def extract_nozzle_mapping_from_3mf(zf: zipfile.ZipFile) -> dict[int, int] | None:
+    """Extract per-slot nozzle/extruder mapping from a 3MF file's project settings.
+
+    On dual-nozzle printers (H2D, H2D Pro), each filament slot is assigned to a
+    specific nozzle. This reads the slicer's nozzle assignment from
+    Metadata/project_settings.config.
+
+    Translation chain:
+        filament_nozzle_map[slot_id - 1] -> slicer extruder index
+        physical_extruder_map[slicer_ext] -> MQTT extruder ID (0=right, 1=left)
+
+    Args:
+        zf: An open ZipFile of the 3MF archive
+
+    Returns:
+        Dictionary mapping {slot_id: extruder_id} for dual-nozzle files,
+        or None if single-nozzle, missing data, or parse error.
+    """
+    try:
+        if "Metadata/project_settings.config" not in zf.namelist():
+            return None
+
+        content = zf.read("Metadata/project_settings.config").decode()
+        data = json.loads(content)
+
+        filament_nozzle_map = data.get("filament_nozzle_map")
+        physical_extruder_map = data.get("physical_extruder_map")
+
+        if not filament_nozzle_map or not physical_extruder_map:
+            return None
+
+        # Build slot_id (1-based) -> extruder_id mapping
+        nozzle_mapping: dict[int, int] = {}
+        for i, slicer_ext_str in enumerate(filament_nozzle_map):
+            slot_id = i + 1
+            try:
+                slicer_ext = int(slicer_ext_str)
+                if slicer_ext < len(physical_extruder_map):
+                    extruder_id = int(physical_extruder_map[slicer_ext])
+                    nozzle_mapping[slot_id] = extruder_id
+            except (ValueError, TypeError, IndexError):
+                pass  # Skip slots with unparseable nozzle mapping
+
+        if not nozzle_mapping:
+            return None
+
+        # If all slots map to the same extruder, this is a single-nozzle printer
+        unique_extruders = set(nozzle_mapping.values())
+        if len(unique_extruders) <= 1:
+            return None
+
+        return nozzle_mapping
+    except Exception:
+        return None
+
+
 def extract_filament_usage_from_3mf(file_path: Path) -> list[dict]:
     """Extract per-filament total usage from 3MF slice_info.config.
 

+ 43 - 0
backend/tests/integration/test_camera_api.py

@@ -192,6 +192,49 @@ class TestCameraAPI:
         assert response.status_code == 503
         assert "Failed to capture" in response.json()["detail"]
 
+    @pytest.mark.asyncio
+    @pytest.mark.integration
+    async def test_camera_snapshot_external_camera_success(self, async_client: AsyncClient, printer_factory):
+        """Verify snapshot uses external camera when configured."""
+        printer = await printer_factory(
+            external_camera_enabled=True,
+            external_camera_url="http://192.168.1.50/mjpeg",
+            external_camera_type="mjpeg",
+        )
+
+        fake_jpeg = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00"
+
+        with patch(
+            "backend.app.services.external_camera.capture_frame",
+            new_callable=AsyncMock,
+            return_value=fake_jpeg,
+        ):
+            response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/snapshot")
+
+        assert response.status_code == 200
+        assert response.headers["content-type"] == "image/jpeg"
+        assert response.content == fake_jpeg
+
+    @pytest.mark.asyncio
+    @pytest.mark.integration
+    async def test_camera_snapshot_external_camera_failure(self, async_client: AsyncClient, printer_factory):
+        """Verify 503 when external camera capture fails."""
+        printer = await printer_factory(
+            external_camera_enabled=True,
+            external_camera_url="http://192.168.1.50/mjpeg",
+            external_camera_type="mjpeg",
+        )
+
+        with patch(
+            "backend.app.services.external_camera.capture_frame",
+            new_callable=AsyncMock,
+            return_value=None,
+        ):
+            response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/snapshot")
+
+        assert response.status_code == 503
+        assert "external camera" in response.json()["detail"].lower()
+
     # ========================================================================
     # Camera Stream Endpoint
     # ========================================================================

+ 6 - 6
backend/tests/unit/services/test_bambu_ftp.py

@@ -123,11 +123,10 @@ class TestDisconnectServerGone:
     """Test disconnect behavior when the server has stopped."""
 
     def test_disconnect_after_server_gone(self, ftp_certs, tmp_path):
-        """Disconnect after server has stopped raises EOFError.
+        """Disconnect after server has stopped does not raise.
 
-        Note: The current disconnect() catches (OSError, ftplib.Error) but
-        EOFError is neither. This documents actual behavior — a future fix
-        could add EOFError to the except clause.
+        disconnect() catches OSError, ftplib.Error, and EOFError so that
+        best-effort cleanup never propagates exceptions to the caller.
         """
         from backend.tests.unit.services.mock_ftp_server import (
             MockBambuFTPServer,
@@ -145,8 +144,9 @@ class TestDisconnectServerGone:
         client.connect()
 
         server.stop()
-        with pytest.raises(EOFError):
-            client.disconnect()
+        # Should not raise — disconnect() catches all connection errors
+        client.disconnect()
+        assert client._ftp is None
 
 
 # ---------------------------------------------------------------------------

+ 15 - 12
backend/tests/unit/services/test_printer_manager.py

@@ -734,23 +734,26 @@ class TestPrinterStateToDict:
         assert result["ams"][0]["tray"][0]["tag_uid"] is None
 
     def test_vt_tray_parsing(self, mock_state):
-        """Verify virtual tray is parsed correctly."""
+        """Verify virtual tray is parsed correctly as a list."""
         mock_state.raw_data = {
-            "vt_tray": {
-                "tray_color": "00FF00",
-                "tray_type": "PETG",
-                "tray_sub_brands": "Generic",
-                "remain": 60,
-                "tag_uid": "VT123",
-            }
+            "vt_tray": [
+                {
+                    "tray_color": "00FF00",
+                    "tray_type": "PETG",
+                    "tray_sub_brands": "Generic",
+                    "remain": 60,
+                    "tag_uid": "VT123",
+                }
+            ]
         }
 
         result = printer_state_to_dict(mock_state)
 
-        assert result["vt_tray"] is not None
-        assert result["vt_tray"]["id"] == 254
-        assert result["vt_tray"]["tray_color"] == "00FF00"
-        assert result["vt_tray"]["tray_type"] == "PETG"
+        assert isinstance(result["vt_tray"], list)
+        assert len(result["vt_tray"]) == 1
+        assert result["vt_tray"][0]["id"] == 254
+        assert result["vt_tray"][0]["tray_color"] == "00FF00"
+        assert result["vt_tray"][0]["tray_type"] == "PETG"
 
     def test_hms_errors_conversion(self, mock_state):
         """Verify HMS errors are converted correctly."""

+ 66 - 0
backend/tests/unit/services/test_spoolman_service.py

@@ -2,6 +2,7 @@
 
 These tests specifically target the sync_ams_tray method's disable_weight_sync
 functionality that controls whether remaining_weight is updated.
+Also includes tests for is_bambu_lab_spool RFID detection.
 """
 
 from unittest.mock import AsyncMock, Mock, patch
@@ -11,6 +12,71 @@ import pytest
 from backend.app.services.spoolman import AMSTray, SpoolmanClient
 
 
+class TestIsBambuLabSpool:
+    """Tests for is_bambu_lab_spool — detects BL spools via RFID hardware identifiers only."""
+
+    @pytest.fixture
+    def client(self):
+        return SpoolmanClient("http://localhost:7912")
+
+    def test_valid_tray_uuid_returns_true(self, client):
+        """A non-zero 32-char hex tray_uuid identifies a BL spool."""
+        assert client.is_bambu_lab_spool("A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4") is True
+
+    def test_valid_tag_uid_returns_true(self, client):
+        """A non-zero 16-char hex tag_uid identifies a BL spool (fallback)."""
+        assert client.is_bambu_lab_spool("", tag_uid="A1B2C3D4E5F6A1B2") is True
+
+    def test_zero_tray_uuid_returns_false(self, client):
+        """All-zero tray_uuid means no RFID tag read."""
+        assert client.is_bambu_lab_spool("00000000000000000000000000000000") is False
+
+    def test_zero_tag_uid_returns_false(self, client):
+        """All-zero tag_uid means no RFID tag read."""
+        assert client.is_bambu_lab_spool("", tag_uid="0000000000000000") is False
+
+    def test_empty_identifiers_returns_false(self, client):
+        """No identifiers means no BL spool."""
+        assert client.is_bambu_lab_spool("") is False
+        assert client.is_bambu_lab_spool("", tag_uid="") is False
+
+    def test_tray_info_idx_ignored(self, client):
+        """tray_info_idx is NOT a reliable BL indicator — third-party spools
+        using Bambu generic presets also have GF-prefixed tray_info_idx values."""
+        # Third-party spool with Bambu preset but no RFID identifiers
+        assert client.is_bambu_lab_spool("", tray_info_idx="GFA00") is False
+        assert client.is_bambu_lab_spool("", tray_info_idx="GFB00") is False
+        assert client.is_bambu_lab_spool("", tray_info_idx="GFSA02_04") is False
+
+    def test_tray_info_idx_with_valid_uuid_returns_true(self, client):
+        """BL spool with both RFID UUID and preset ID — detected by UUID."""
+        assert (
+            client.is_bambu_lab_spool(
+                "A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4",
+                tray_info_idx="GFA00",
+            )
+            is True
+        )
+
+    def test_tray_uuid_preferred_over_tag_uid(self, client):
+        """tray_uuid is checked before tag_uid (both valid)."""
+        assert (
+            client.is_bambu_lab_spool(
+                "A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4",
+                tag_uid="A1B2C3D4E5F6A1B2",
+            )
+            is True
+        )
+
+    def test_short_tray_uuid_returns_false(self, client):
+        """UUID must be exactly 32 hex chars."""
+        assert client.is_bambu_lab_spool("A1B2C3D4") is False
+
+    def test_non_hex_tray_uuid_returns_false(self, client):
+        """UUID must be valid hex."""
+        assert client.is_bambu_lab_spool("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ") is False
+
+
 class TestSpoolmanClient:
     """Tests for SpoolmanClient class."""
 

+ 2 - 2
backend/tests/unit/services/test_spoolman_tracking.py

@@ -99,14 +99,14 @@ class TestBuildAmsTrayLookup:
     def test_external_spool(self):
         raw = {
             "ams": [],
-            "vt_tray": {"tray_uuid": "EXT", "tag_uid": "X", "tray_type": "TPU"},
+            "vt_tray": [{"tray_uuid": "EXT", "tag_uid": "X", "tray_type": "TPU"}],
         }
         lookup = build_ams_tray_lookup(raw)
         assert 254 in lookup
         assert lookup[254]["tray_type"] == "TPU"
 
     def test_empty_external_spool_skipped(self):
-        raw = {"ams": [], "vt_tray": {"tray_type": ""}}
+        raw = {"ams": [], "vt_tray": [{"tray_type": ""}]}
         lookup = build_ams_tray_lookup(raw)
         assert 254 not in lookup
 

+ 401 - 0
backend/tests/unit/services/test_usage_tracker.py

@@ -0,0 +1,401 @@
+"""Unit tests for the filament usage tracker.
+
+Tests 3MF-primary tracking (Path 1) and AMS remain% delta fallback
+(Path 2) for spools not covered by 3MF data.
+"""
+
+from datetime import datetime, timezone
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from backend.app.services.usage_tracker import (
+    PrintSession,
+    _active_sessions,
+    _track_from_3mf,
+    on_print_complete,
+    on_print_start,
+)
+
+
+def _make_spool(*, id=1, label_weight=1000, weight_used=0, tag_uid=None, tray_uuid=None):
+    """Create a mock Spool object."""
+    spool = MagicMock()
+    spool.id = id
+    spool.label_weight = label_weight
+    spool.weight_used = weight_used
+    spool.tag_uid = tag_uid
+    spool.tray_uuid = tray_uuid
+    spool.last_used = None
+    return spool
+
+
+def _make_assignment(*, spool_id=1, printer_id=1, ams_id=0, tray_id=0):
+    """Create a mock SpoolAssignment object."""
+    assignment = MagicMock()
+    assignment.spool_id = spool_id
+    assignment.printer_id = printer_id
+    assignment.ams_id = ams_id
+    assignment.tray_id = tray_id
+    return assignment
+
+
+def _make_printer_state(ams_data, progress=0, layer_num=0, tray_now=255):
+    """Create a mock printer state with AMS data."""
+    state = MagicMock()
+    state.raw_data = {"ams": ams_data}
+    state.progress = progress
+    state.layer_num = layer_num
+    state.tray_now = tray_now
+    return state
+
+
+def _make_printer_manager(state=None):
+    """Create a mock printer manager."""
+    pm = MagicMock()
+    pm.get_status.return_value = state
+    return pm
+
+
+class TestOnPrintStart:
+    """Tests for on_print_start — capturing AMS remain%."""
+
+    @pytest.fixture(autouse=True)
+    def _clear_sessions(self):
+        _active_sessions.clear()
+        yield
+        _active_sessions.clear()
+
+    @pytest.mark.asyncio
+    async def test_creates_session_with_valid_remain(self):
+        """Session created with remain% data for trays reporting 0-100."""
+        ams_data = [{"id": 0, "tray": [{"id": 0, "remain": 80}]}]
+        pm = _make_printer_manager(_make_printer_state(ams_data))
+
+        await on_print_start(1, {"subtask_name": "test_print"}, pm)
+
+        assert 1 in _active_sessions
+        session = _active_sessions[1]
+        assert session.print_name == "test_print"
+        assert session.tray_remain_start == {(0, 0): 80}
+
+    @pytest.mark.asyncio
+    async def test_creates_session_even_without_valid_remain(self):
+        """Session still created when remain=-1 (for 3MF fallback path)."""
+        ams_data = [{"id": 0, "tray": [{"id": 0, "remain": -1}]}]
+        pm = _make_printer_manager(_make_printer_state(ams_data))
+
+        await on_print_start(1, {"subtask_name": "test_print"}, pm)
+
+        assert 1 in _active_sessions
+        session = _active_sessions[1]
+        assert session.tray_remain_start == {}  # Empty, no valid remain
+
+    @pytest.mark.asyncio
+    async def test_skips_without_ams_data(self):
+        """No session created when no AMS data available."""
+        state = MagicMock()
+        state.raw_data = {"ams": []}
+        pm = _make_printer_manager(state)
+
+        await on_print_start(1, {"subtask_name": "test"}, pm)
+
+        assert 1 not in _active_sessions
+
+
+class TestOnPrintCompleteAMSDelta:
+    """Tests for Path 1: AMS remain% delta tracking."""
+
+    @pytest.fixture(autouse=True)
+    def _clear_sessions(self):
+        _active_sessions.clear()
+        yield
+        _active_sessions.clear()
+
+    @pytest.mark.asyncio
+    async def test_computes_delta_and_updates_spool(self):
+        """Spool weight_used updated by remain% delta * label_weight."""
+        # Set up session with start remain = 80%
+        _active_sessions[1] = PrintSession(
+            printer_id=1,
+            print_name="test",
+            started_at=datetime.now(timezone.utc),
+            tray_remain_start={(0, 0): 80},
+        )
+
+        # Current remain = 70% → 10% consumed → 100g on 1000g spool
+        ams_data = [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]
+        pm = _make_printer_manager(_make_printer_state(ams_data))
+
+        spool = _make_spool(label_weight=1000, weight_used=50)
+        assignment = _make_assignment()
+
+        db = AsyncMock()
+        # First execute → assignment, second → spool
+        db.execute = AsyncMock(
+            side_effect=[
+                MagicMock(scalar_one_or_none=MagicMock(return_value=assignment)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=spool)),
+            ]
+        )
+
+        results = await on_print_complete(1, {"status": "completed"}, pm, db)
+
+        assert len(results) == 1
+        assert results[0]["weight_used"] == 100.0
+        assert results[0]["percent_used"] == 10
+        # weight_used should be old (50) + delta (100)
+        assert spool.weight_used == 150.0
+        db.commit.assert_called_once()
+
+    @pytest.mark.asyncio
+    async def test_skips_negative_delta(self):
+        """No tracking when remain increased (spool refilled)."""
+        _active_sessions[1] = PrintSession(
+            printer_id=1,
+            print_name="test",
+            started_at=datetime.now(timezone.utc),
+            tray_remain_start={(0, 0): 50},
+        )
+
+        # Remain went UP: 50 → 80 (refilled)
+        ams_data = [{"id": 0, "tray": [{"id": 0, "remain": 80}]}]
+        pm = _make_printer_manager(_make_printer_state(ams_data))
+        db = AsyncMock()
+
+        results = await on_print_complete(1, {"status": "completed"}, pm, db)
+
+        assert results == []
+        db.commit.assert_not_called()
+
+    @pytest.mark.asyncio
+    async def test_no_session_falls_through_to_3mf(self):
+        """When no session exists, AMS delta path skipped (3MF may still run)."""
+        pm = _make_printer_manager()
+        db = AsyncMock()
+
+        results = await on_print_complete(1, {"status": "completed"}, pm, db)
+
+        assert results == []
+
+
+class TestTrackFrom3MF:
+    """Tests for Path 2: 3MF per-filament fallback tracking."""
+
+    @pytest.mark.asyncio
+    async def test_updates_non_bl_spool_from_3mf(self):
+        """Non-BL spool gets weight_used from 3MF used_g for completed print."""
+        spool = _make_spool(id=5, label_weight=1000, weight_used=100)
+        assignment = _make_assignment(spool_id=5)
+        archive = MagicMock()
+        archive.file_path = "archives/test.3mf"
+
+        db = AsyncMock()
+        # archive, queue_item(None), assignment, spool
+        db.execute = AsyncMock(
+            side_effect=[
+                MagicMock(scalar_one_or_none=MagicMock(return_value=archive)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=None)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=assignment)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=spool)),
+            ]
+        )
+
+        pm = _make_printer_manager(_make_printer_state([], tray_now=0))
+        filament_usage = [{"slot_id": 1, "used_g": 25.5, "type": "PLA", "color": "#FF0000"}]
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=filament_usage),
+        ):
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=10,
+                status="completed",
+                print_name="test_print",
+                handled_trays=set(),
+                printer_manager=pm,
+                db=db,
+            )
+
+        assert len(results) == 1
+        assert results[0]["spool_id"] == 5
+        assert results[0]["weight_used"] == 25.5
+        # weight_used = old (100) + 3MF (25.5)
+        assert spool.weight_used == 125.5
+
+    @pytest.mark.asyncio
+    async def test_scales_by_progress_for_failed_print(self):
+        """Failed print scales 3MF estimate by progress percentage."""
+        spool = _make_spool(id=1, label_weight=1000, weight_used=0)
+        assignment = _make_assignment()
+        archive = MagicMock()
+        archive.file_path = "archives/test.3mf"
+
+        db = AsyncMock()
+        # archive, queue_item(None), assignment, spool
+        db.execute = AsyncMock(
+            side_effect=[
+                MagicMock(scalar_one_or_none=MagicMock(return_value=archive)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=None)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=assignment)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=spool)),
+            ]
+        )
+
+        # Print failed at 50% progress → 50g consumed from 100g estimate
+        pm = _make_printer_manager(_make_printer_state([], progress=50, tray_now=0))
+        filament_usage = [{"slot_id": 1, "used_g": 100.0, "type": "PLA", "color": ""}]
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=filament_usage),
+        ):
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=10,
+                status="failed",
+                print_name="test",
+                handled_trays=set(),
+                printer_manager=pm,
+                db=db,
+            )
+
+        assert len(results) == 1
+        assert results[0]["weight_used"] == 50.0
+        assert spool.weight_used == 50.0
+
+    @pytest.mark.asyncio
+    async def test_tracks_bl_spools_via_3mf(self):
+        """BL spools (with tag_uid) ARE now tracked via 3MF (unified tracking)."""
+        spool = _make_spool(tag_uid="ABCD1234", tray_uuid="A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4")
+        assignment = _make_assignment()
+        archive = MagicMock()
+        archive.file_path = "archives/test.3mf"
+
+        db = AsyncMock()
+        # archive, queue_item(None), assignment, spool
+        db.execute = AsyncMock(
+            side_effect=[
+                MagicMock(scalar_one_or_none=MagicMock(return_value=archive)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=None)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=assignment)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=spool)),
+            ]
+        )
+
+        pm = _make_printer_manager(_make_printer_state([], tray_now=0))
+        filament_usage = [{"slot_id": 1, "used_g": 50.0, "type": "PLA", "color": ""}]
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=filament_usage),
+        ):
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=10,
+                status="completed",
+                print_name="test",
+                handled_trays=set(),
+                printer_manager=pm,
+                db=db,
+            )
+
+        assert len(results) == 1
+        assert results[0]["spool_id"] == 1
+        assert results[0]["weight_used"] == 50.0
+
+    @pytest.mark.asyncio
+    async def test_skips_already_handled_trays(self):
+        """Trays handled by AMS remain% delta are not double-tracked via 3MF."""
+        archive = MagicMock()
+        archive.file_path = "archives/test.3mf"
+
+        db = AsyncMock()
+        # archive, queue_item(None)
+        db.execute = AsyncMock(
+            side_effect=[
+                MagicMock(scalar_one_or_none=MagicMock(return_value=archive)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=None)),
+            ]
+        )
+
+        pm = _make_printer_manager(_make_printer_state([], tray_now=0))
+        filament_usage = [{"slot_id": 1, "used_g": 50.0, "type": "PLA", "color": ""}]
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=filament_usage),
+        ):
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=10,
+                status="completed",
+                print_name="test",
+                handled_trays={(0, 0)},  # slot_id=1 → ams_id=0, tray_id=0
+                printer_manager=pm,
+                db=db,
+            )
+
+        assert results == []
+
+    @pytest.mark.asyncio
+    async def test_slot_to_tray_mapping(self):
+        """3MF slot_id maps correctly to (ams_id, tray_id) via tray_now."""
+        # tray_now=4 → ams_id=1, tray_id=0 (single filament uses tray_now)
+        spool = _make_spool(id=9)
+        assignment = _make_assignment(spool_id=9, ams_id=1, tray_id=0)
+        archive = MagicMock()
+        archive.file_path = "archives/test.3mf"
+
+        db = AsyncMock()
+        # archive, queue_item(None), assignment, spool
+        db.execute = AsyncMock(
+            side_effect=[
+                MagicMock(scalar_one_or_none=MagicMock(return_value=archive)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=None)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=assignment)),
+                MagicMock(scalar_one_or_none=MagicMock(return_value=spool)),
+            ]
+        )
+
+        pm = _make_printer_manager(_make_printer_state([], tray_now=4))
+        filament_usage = [{"slot_id": 5, "used_g": 30.0, "type": "PETG", "color": ""}]
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=filament_usage),
+        ):
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=10,
+                status="completed",
+                print_name="test",
+                handled_trays=set(),
+                printer_manager=pm,
+                db=db,
+            )
+
+        assert len(results) == 1
+        assert results[0]["ams_id"] == 1
+        assert results[0]["tray_id"] == 0

+ 1 - 1
backend/tests/unit/test_code_quality.py

@@ -137,7 +137,7 @@ def find_import_shadowing(file_path: Path) -> list[tuple[str, int, str]]:
     Returns list of (name, line_number, function_name) tuples.
     """
     try:
-        with open(file_path) as f:
+        with open(file_path, encoding="utf-8") as f:
             source = f.read()
         tree = ast.parse(source)
         visitor = DangerousImportVisitor()

+ 190 - 2
backend/tests/unit/test_scheduler_ams_mapping.py

@@ -1,8 +1,13 @@
 """Tests for the AMS mapping computation in the print scheduler."""
 
+import io
+import json
+import zipfile
+
 import pytest
 
 from backend.app.services.print_scheduler import PrintScheduler
+from backend.app.utils.threemf_tools import extract_nozzle_mapping_from_3mf
 
 
 class TestSchedulerAmsMappingHelpers:
@@ -135,7 +140,7 @@ class TestBuildLoadedFilaments:
         """Should include external spool."""
 
         class MockStatus:
-            raw_data = {"vt_tray": {"tray_type": "TPU", "tray_color": "0000FF"}}
+            raw_data = {"vt_tray": [{"tray_type": "TPU", "tray_color": "0000FF"}]}
 
         result = scheduler._build_loaded_filaments(MockStatus())
         assert len(result) == 1
@@ -461,9 +466,192 @@ class TestBuildLoadedFilamentsTrayInfoIdx:
         """Should extract tray_info_idx from external spool."""
 
         class MockStatus:
-            raw_data = {"vt_tray": {"tray_type": "TPU", "tray_color": "0000FF", "tray_info_idx": "P4d64437"}}
+            raw_data = {"vt_tray": [{"tray_type": "TPU", "tray_color": "0000FF", "tray_info_idx": "P4d64437"}]}
 
         result = scheduler._build_loaded_filaments(MockStatus())
         assert len(result) == 1
         assert result[0]["tray_info_idx"] == "P4d64437"
         assert result[0]["is_external"] is True
+
+
+def _make_3mf_zip(project_settings: dict | None = None) -> zipfile.ZipFile:
+    """Create an in-memory ZipFile mimicking a 3MF with project_settings.config."""
+    buf = io.BytesIO()
+    with zipfile.ZipFile(buf, "w") as zf:
+        if project_settings is not None:
+            zf.writestr("Metadata/project_settings.config", json.dumps(project_settings))
+    buf.seek(0)
+    return zipfile.ZipFile(buf, "r")
+
+
+class TestExtractNozzleMappingFrom3mf:
+    """Test the extract_nozzle_mapping_from_3mf utility."""
+
+    def test_dual_nozzle_mapping(self):
+        """Should return slot->extruder mapping for dual-nozzle files."""
+        zf = _make_3mf_zip(
+            {
+                "filament_nozzle_map": ["0", "1", "0"],
+                "physical_extruder_map": ["0", "1"],
+            }
+        )
+        result = extract_nozzle_mapping_from_3mf(zf)
+        assert result == {1: 0, 2: 1, 3: 0}
+        zf.close()
+
+    def test_single_nozzle_returns_none(self):
+        """All slots on same extruder should return None (single-nozzle)."""
+        zf = _make_3mf_zip(
+            {
+                "filament_nozzle_map": ["0", "0", "0"],
+                "physical_extruder_map": ["0"],
+            }
+        )
+        result = extract_nozzle_mapping_from_3mf(zf)
+        assert result is None
+        zf.close()
+
+    def test_missing_project_settings_returns_none(self):
+        """Missing project_settings.config should return None."""
+        zf = _make_3mf_zip(None)
+        result = extract_nozzle_mapping_from_3mf(zf)
+        assert result is None
+        zf.close()
+
+    def test_missing_fields_returns_none(self):
+        """Missing filament_nozzle_map or physical_extruder_map should return None."""
+        zf = _make_3mf_zip({"some_other_key": "value"})
+        result = extract_nozzle_mapping_from_3mf(zf)
+        assert result is None
+        zf.close()
+
+    def test_physical_extruder_map_remapping(self):
+        """Should apply physical_extruder_map to remap slicer extruder to MQTT extruder."""
+        # Slicer ext 0 -> MQTT ext 1, slicer ext 1 -> MQTT ext 0
+        zf = _make_3mf_zip(
+            {
+                "filament_nozzle_map": ["0", "1"],
+                "physical_extruder_map": ["1", "0"],
+            }
+        )
+        result = extract_nozzle_mapping_from_3mf(zf)
+        assert result == {1: 1, 2: 0}
+        zf.close()
+
+
+class TestNozzleAwareMapping:
+    """Test nozzle-aware filament matching in the print scheduler."""
+
+    @pytest.fixture
+    def scheduler(self):
+        return PrintScheduler()
+
+    def test_dual_nozzle_matching(self, scheduler):
+        """Filaments assigned to different nozzles should match to correct AMS units."""
+        required = [
+            {"slot_id": 1, "type": "PLA", "color": "#FF0000", "nozzle_id": 0},  # Right nozzle
+            {"slot_id": 2, "type": "PLA", "color": "#00FF00", "nozzle_id": 1},  # Left nozzle
+        ]
+        loaded = [
+            {"type": "PLA", "color": "#00FF00", "global_tray_id": 0, "extruder_id": 0},  # AMS0 on right
+            {"type": "PLA", "color": "#FF0000", "global_tray_id": 4, "extruder_id": 1},  # AMS1 on left
+        ]
+        # Without nozzle filtering, slot 1 (red, right) would match tray 4 (red, left) by color.
+        # With nozzle filtering, slot 1 (right nozzle) can only use tray 0 (right extruder),
+        # and slot 2 (left nozzle) can only use tray 4 (left extruder).
+        result = scheduler._match_filaments_to_slots(required, loaded)
+        assert result == [0, 4]
+
+    def test_nozzle_fallback_when_no_match(self, scheduler):
+        """Should fall back to unfiltered list when nozzle-filtered list is empty."""
+        required = [
+            {"slot_id": 1, "type": "PLA", "color": "#FF0000", "nozzle_id": 0},  # Right nozzle
+        ]
+        loaded = [
+            # Only a tray on the left nozzle, none on right
+            {"type": "PLA", "color": "#FF0000", "global_tray_id": 4, "extruder_id": 1},
+        ]
+        # No trays on extruder 0, so fallback to unfiltered -> should still match
+        result = scheduler._match_filaments_to_slots(required, loaded)
+        assert result == [4]
+
+    def test_no_nozzle_id_skips_filtering(self, scheduler):
+        """When nozzle_id is None, no nozzle filtering should be applied."""
+        required = [
+            {"slot_id": 1, "type": "PLA", "color": "#FF0000"},  # No nozzle_id
+        ]
+        loaded = [
+            {"type": "PLA", "color": "#FF0000", "global_tray_id": 0, "extruder_id": 0},
+            {"type": "PLA", "color": "#FF0000", "global_tray_id": 4, "extruder_id": 1},
+        ]
+        # Should match first available (tray 0) regardless of extruder
+        result = scheduler._match_filaments_to_slots(required, loaded)
+        assert result == [0]
+
+    def test_extruder_id_in_loaded_filaments(self, scheduler):
+        """_build_loaded_filaments should include extruder_id from ams_extruder_map."""
+
+        class MockStatus:
+            raw_data = {
+                "ams": [
+                    {"id": 0, "tray": [{"id": 0, "tray_type": "PLA", "tray_color": "FF0000"}]},
+                    {"id": 1, "tray": [{"id": 0, "tray_type": "PLA", "tray_color": "00FF00"}]},
+                ],
+                "ams_extruder_map": {"0": 0, "1": 1},
+            }
+
+        result = scheduler._build_loaded_filaments(MockStatus())
+        assert len(result) == 2
+        assert result[0]["extruder_id"] == 0
+        assert result[1]["extruder_id"] == 1
+
+    def test_extruder_id_none_without_map(self, scheduler):
+        """extruder_id should be None when ams_extruder_map is absent."""
+
+        class MockStatus:
+            raw_data = {
+                "ams": [
+                    {"id": 0, "tray": [{"id": 0, "tray_type": "PLA", "tray_color": "FF0000"}]},
+                ]
+            }
+
+        result = scheduler._build_loaded_filaments(MockStatus())
+        assert len(result) == 1
+        assert result[0]["extruder_id"] is None
+
+    def test_external_spool_extruder_id(self, scheduler):
+        """External spool should have extruder_id=0 when ams_extruder_map exists."""
+
+        class MockStatus:
+            raw_data = {
+                "vt_tray": [{"tray_type": "TPU", "tray_color": "0000FF"}],
+                "ams_extruder_map": {"0": 0},
+            }
+
+        result = scheduler._build_loaded_filaments(MockStatus())
+        assert len(result) == 1
+        assert result[0]["extruder_id"] == 0
+        assert result[0]["is_external"] is True
+
+    def test_external_spool_no_extruder_map(self, scheduler):
+        """External spool extruder_id should be None without ams_extruder_map."""
+
+        class MockStatus:
+            raw_data = {"vt_tray": [{"tray_type": "TPU", "tray_color": "0000FF"}]}
+
+        result = scheduler._build_loaded_filaments(MockStatus())
+        assert len(result) == 1
+        assert result[0]["extruder_id"] is None
+
+    def test_dual_nozzle_with_tray_info_idx(self, scheduler):
+        """Nozzle filtering should work together with tray_info_idx matching."""
+        required = [
+            {"slot_id": 1, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA00", "nozzle_id": 0},
+            {"slot_id": 2, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA01", "nozzle_id": 1},
+        ]
+        loaded = [
+            {"type": "PLA", "color": "#000000", "global_tray_id": 0, "tray_info_idx": "GFA00", "extruder_id": 0},
+            {"type": "PLA", "color": "#000000", "global_tray_id": 4, "tray_info_idx": "GFA01", "extruder_id": 1},
+        ]
+        result = scheduler._match_filaments_to_slots(required, loaded)
+        assert result == [0, 4]

+ 122 - 0
backend/tests/unit/test_scheduler_clear_plate.py

@@ -0,0 +1,122 @@
+"""Tests for the clear plate queue flow in the print scheduler."""
+
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from backend.app.services.print_scheduler import PrintScheduler
+from backend.app.services.printer_manager import PrinterManager
+
+
+class TestPrinterManagerPlateCleared:
+    """Test the plate-cleared flag management in PrinterManager."""
+
+    @pytest.fixture
+    def manager(self):
+        return PrinterManager()
+
+    def test_plate_cleared_initially_false(self, manager):
+        """No printers should have plate cleared by default."""
+        assert not manager.is_plate_cleared(1)
+        assert not manager.is_plate_cleared(999)
+
+    def test_set_plate_cleared(self, manager):
+        """Setting plate cleared should make is_plate_cleared return True."""
+        manager.set_plate_cleared(1)
+        assert manager.is_plate_cleared(1)
+        assert not manager.is_plate_cleared(2)
+
+    def test_consume_plate_cleared(self, manager):
+        """Consuming plate cleared should reset the flag."""
+        manager.set_plate_cleared(1)
+        assert manager.is_plate_cleared(1)
+        manager.consume_plate_cleared(1)
+        assert not manager.is_plate_cleared(1)
+
+    def test_consume_plate_cleared_idempotent(self, manager):
+        """Consuming when not set should not raise."""
+        manager.consume_plate_cleared(1)  # Should not raise
+        assert not manager.is_plate_cleared(1)
+
+    def test_set_plate_cleared_multiple_printers(self, manager):
+        """Plate cleared should be tracked per printer."""
+        manager.set_plate_cleared(1)
+        manager.set_plate_cleared(3)
+        assert manager.is_plate_cleared(1)
+        assert not manager.is_plate_cleared(2)
+        assert manager.is_plate_cleared(3)
+
+    def test_consume_only_affects_target_printer(self, manager):
+        """Consuming plate cleared for one printer should not affect others."""
+        manager.set_plate_cleared(1)
+        manager.set_plate_cleared(2)
+        manager.consume_plate_cleared(1)
+        assert not manager.is_plate_cleared(1)
+        assert manager.is_plate_cleared(2)
+
+
+class TestSchedulerIdleCheckWithPlateCleared:
+    """Test _is_printer_idle with plate-cleared flag interactions."""
+
+    @pytest.fixture
+    def scheduler(self):
+        return PrintScheduler()
+
+    @patch("backend.app.services.print_scheduler.printer_manager")
+    def test_idle_state_is_idle(self, mock_pm, scheduler):
+        """Printer in IDLE state should be considered idle."""
+        mock_pm.is_connected.return_value = True
+        mock_pm.get_status.return_value = MagicMock(state="IDLE")
+        assert scheduler._is_printer_idle(1) is True
+
+    @patch("backend.app.services.print_scheduler.printer_manager")
+    def test_running_state_not_idle(self, mock_pm, scheduler):
+        """Printer in RUNNING state should not be idle."""
+        mock_pm.is_connected.return_value = True
+        mock_pm.get_status.return_value = MagicMock(state="RUNNING")
+        assert scheduler._is_printer_idle(1) is False
+
+    @patch("backend.app.services.print_scheduler.printer_manager")
+    def test_finish_state_not_idle_without_plate_cleared(self, mock_pm, scheduler):
+        """Printer in FINISH state should NOT be idle without plate cleared."""
+        mock_pm.is_connected.return_value = True
+        mock_pm.get_status.return_value = MagicMock(state="FINISH")
+        mock_pm.is_plate_cleared.return_value = False
+        assert scheduler._is_printer_idle(1) is False
+
+    @patch("backend.app.services.print_scheduler.printer_manager")
+    def test_finish_state_idle_with_plate_cleared(self, mock_pm, scheduler):
+        """Printer in FINISH state should be idle when plate is cleared."""
+        mock_pm.is_connected.return_value = True
+        mock_pm.get_status.return_value = MagicMock(state="FINISH")
+        mock_pm.is_plate_cleared.return_value = True
+        assert scheduler._is_printer_idle(1) is True
+
+    @patch("backend.app.services.print_scheduler.printer_manager")
+    def test_failed_state_not_idle_without_plate_cleared(self, mock_pm, scheduler):
+        """Printer in FAILED state should NOT be idle without plate cleared."""
+        mock_pm.is_connected.return_value = True
+        mock_pm.get_status.return_value = MagicMock(state="FAILED")
+        mock_pm.is_plate_cleared.return_value = False
+        assert scheduler._is_printer_idle(1) is False
+
+    @patch("backend.app.services.print_scheduler.printer_manager")
+    def test_failed_state_idle_with_plate_cleared(self, mock_pm, scheduler):
+        """Printer in FAILED state should be idle when plate is cleared."""
+        mock_pm.is_connected.return_value = True
+        mock_pm.get_status.return_value = MagicMock(state="FAILED")
+        mock_pm.is_plate_cleared.return_value = True
+        assert scheduler._is_printer_idle(1) is True
+
+    @patch("backend.app.services.print_scheduler.printer_manager")
+    def test_disconnected_printer_not_idle(self, mock_pm, scheduler):
+        """Disconnected printer should never be idle."""
+        mock_pm.is_connected.return_value = False
+        assert scheduler._is_printer_idle(1) is False
+
+    @patch("backend.app.services.print_scheduler.printer_manager")
+    def test_no_status_not_idle(self, mock_pm, scheduler):
+        """Printer with no status should not be idle."""
+        mock_pm.is_connected.return_value = True
+        mock_pm.get_status.return_value = None
+        assert scheduler._is_printer_idle(1) is False

+ 726 - 0
backend/tests/unit/test_usage_tracker.py

@@ -0,0 +1,726 @@
+"""Unit tests for usage_tracker.py — 3MF-primary filament tracking.
+
+Tests the unified tracking logic: 3MF slicer estimates as primary path,
+AMS remain% delta as fallback, per-layer gcode for partial prints,
+slot-to-tray mapping resolution, and notification variable formatting.
+"""
+
+from datetime import datetime, timezone
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from backend.app.services.usage_tracker import (
+    PrintSession,
+    _active_sessions,
+    _track_from_3mf,
+    on_print_complete,
+    on_print_start,
+)
+
+
+def _make_spool(spool_id=1, label_weight=1000, weight_used=0, tag_uid=None, tray_uuid=None):
+    """Create a mock Spool object."""
+    spool = MagicMock()
+    spool.id = spool_id
+    spool.label_weight = label_weight
+    spool.weight_used = weight_used
+    spool.tag_uid = tag_uid
+    spool.tray_uuid = tray_uuid
+    spool.last_used = None
+    return spool
+
+
+def _make_assignment(spool_id=1, printer_id=1, ams_id=0, tray_id=0):
+    """Create a mock SpoolAssignment object."""
+    assignment = MagicMock()
+    assignment.spool_id = spool_id
+    assignment.printer_id = printer_id
+    assignment.ams_id = ams_id
+    assignment.tray_id = tray_id
+    return assignment
+
+
+def _make_archive(archive_id=1, file_path="archives/1/test.3mf", extra_data=None):
+    """Create a mock PrintArchive object."""
+    archive = MagicMock()
+    archive.id = archive_id
+    archive.file_path = file_path
+    archive.extra_data = extra_data
+    return archive
+
+
+def _make_queue_item(ams_mapping=None, status="printing"):
+    """Create a mock PrintQueueItem object."""
+    item = MagicMock()
+    item.ams_mapping = ams_mapping
+    item.status = status
+    return item
+
+
+def _mock_db_execute(*return_values):
+    """Create a mock db with execute() that returns values in sequence."""
+    db = AsyncMock()
+    results = []
+    for val in return_values:
+        result = MagicMock()
+        result.scalar_one_or_none.return_value = val
+        results.append(result)
+    db.execute = AsyncMock(side_effect=results)
+    return db
+
+
+def _mock_db_sequential(responses):
+    """Create mock db that returns responses in order."""
+    db = AsyncMock()
+    call_count = [0]
+
+    async def mock_execute(*args, **kwargs):
+        idx = call_count[0]
+        call_count[0] += 1
+        result = MagicMock()
+        if idx < len(responses):
+            result.scalar_one_or_none.return_value = responses[idx]
+        else:
+            result.scalar_one_or_none.return_value = None
+        return result
+
+    db.execute = mock_execute
+    return db
+
+
+class TestOnPrintStart:
+    """Tests for on_print_start()."""
+
+    @pytest.fixture(autouse=True)
+    def _clear_sessions(self):
+        _active_sessions.clear()
+        yield
+        _active_sessions.clear()
+
+    @pytest.mark.asyncio
+    async def test_captures_remain_data(self):
+        """Captures AMS remain% at print start."""
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 80}, {"id": 1, "remain": 50}]}]}
+        )
+
+        await on_print_start(1, {"subtask_name": "Benchy"}, printer_manager)
+
+        assert 1 in _active_sessions
+        session = _active_sessions[1]
+        assert session.print_name == "Benchy"
+        assert session.tray_remain_start == {(0, 0): 80, (0, 1): 50}
+
+    @pytest.mark.asyncio
+    async def test_creates_session_without_remain(self):
+        """Creates session even without valid remain data (for 3MF tracking)."""
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": -1}]}]}
+        )
+
+        await on_print_start(1, {"subtask_name": "Test"}, printer_manager)
+
+        assert 1 in _active_sessions
+        assert _active_sessions[1].tray_remain_start == {}
+
+
+class TestOnPrintComplete:
+    """Tests for on_print_complete() — path ordering and interaction."""
+
+    @pytest.fixture(autouse=True)
+    def _clear_sessions(self):
+        _active_sessions.clear()
+        yield
+        _active_sessions.clear()
+
+    @pytest.mark.asyncio
+    async def test_bl_spool_uses_3mf(self):
+        """BL spool (with tag_uid) is tracked via 3MF, not just AMS delta."""
+        spool = _make_spool(spool_id=1, tag_uid="AABB1122", label_weight=1000)
+        assignment = _make_assignment(spool_id=1, printer_id=1, ams_id=0, tray_id=0)
+        archive = _make_archive(archive_id=10)
+
+        # Setup: session with AMS remain data
+        _active_sessions[1] = PrintSession(
+            printer_id=1,
+            print_name="Benchy",
+            started_at=datetime.now(timezone.utc),
+            tray_remain_start={(0, 0): 80},
+        )
+
+        # Mock printer state: tray_now=0 (AMS0-T0), single filament
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
+            progress=100,
+            layer_num=50,
+            tray_now=0,
+        )
+
+        # db returns: archive, queue_item(None), assignment, spool
+        db = _mock_db_sequential([archive, None, assignment, spool])
+
+        filament_usage = [{"slot_id": 1, "used_g": 15.0, "type": "PLA", "color": "#FF0000"}]
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch(
+                "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
+                return_value=filament_usage,
+            ),
+        ):
+            mock_settings.base_dir = MagicMock()
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await on_print_complete(
+                printer_id=1,
+                data={"status": "completed"},
+                printer_manager=printer_manager,
+                db=db,
+                archive_id=10,
+            )
+
+        # 3MF path should handle it (BL guard removed)
+        assert len(results) >= 1
+        assert results[0]["spool_id"] == 1
+        assert results[0]["weight_used"] == 15.0
+
+    @pytest.mark.asyncio
+    async def test_ams_delta_fallback_no_archive(self):
+        """AMS delta tracks consumption when archive_id is None."""
+        spool = _make_spool(spool_id=2, label_weight=1000)
+        assignment = _make_assignment(spool_id=2)
+
+        _active_sessions[1] = PrintSession(
+            printer_id=1,
+            print_name="Test",
+            started_at=datetime.now(timezone.utc),
+            tray_remain_start={(0, 0): 80},
+        )
+
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
+        )
+
+        # db returns assignment then spool
+        db = _mock_db_sequential([assignment, spool])
+
+        results = await on_print_complete(
+            printer_id=1,
+            data={"status": "completed"},
+            printer_manager=printer_manager,
+            db=db,
+            archive_id=None,
+        )
+
+        assert len(results) == 1
+        assert results[0]["spool_id"] == 2
+        # 10% of 1000g = 100g
+        assert results[0]["weight_used"] == 100.0
+        assert results[0]["percent_used"] == 10
+
+    @pytest.mark.asyncio
+    async def test_no_double_tracking(self):
+        """When 3MF handles a tray, AMS delta skips it."""
+        spool = _make_spool(spool_id=1, label_weight=1000)
+        assignment = _make_assignment(spool_id=1)
+        archive = _make_archive(archive_id=10)
+
+        _active_sessions[1] = PrintSession(
+            printer_id=1,
+            print_name="Benchy",
+            started_at=datetime.now(timezone.utc),
+            tray_remain_start={(0, 0): 80},
+        )
+
+        # tray_now=0 matches the single filament slot
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
+            progress=100,
+            layer_num=50,
+            tray_now=0,
+        )
+
+        # db returns: archive, queue_item(None), assignment, spool
+        db = _mock_db_sequential([archive, None, assignment, spool])
+
+        filament_usage = [{"slot_id": 1, "used_g": 15.0, "type": "PLA", "color": "#FF0000"}]
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch(
+                "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
+                return_value=filament_usage,
+            ),
+        ):
+            mock_settings.base_dir = MagicMock()
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await on_print_complete(
+                printer_id=1,
+                data={"status": "completed"},
+                printer_manager=printer_manager,
+                db=db,
+                archive_id=10,
+            )
+
+        # Only 1 result (3MF), NOT 2 (3MF + AMS delta)
+        assert len(results) == 1
+        assert results[0]["weight_used"] == 15.0
+
+
+class TestTrackFrom3mf:
+    """Tests for _track_from_3mf() — per-layer, linear scaling, and slot mapping."""
+
+    @pytest.mark.asyncio
+    async def test_linear_fallback_for_partial_print(self):
+        """Falls back to linear scaling when gcode layer data unavailable."""
+        spool = _make_spool(spool_id=1, label_weight=1000)
+        assignment = _make_assignment(spool_id=1)
+        archive = _make_archive(archive_id=10)
+
+        # db: archive, queue_item(None), assignment, spool
+        db = _mock_db_sequential([archive, None, assignment, spool])
+
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            progress=50,
+            layer_num=25,
+            tray_now=0,
+        )
+
+        filament_usage = [{"slot_id": 1, "used_g": 20.0, "type": "PLA", "color": ""}]
+        handled_trays: set[tuple[int, int]] = set()
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch(
+                "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
+                return_value=filament_usage,
+            ),
+            patch(
+                "backend.app.utils.threemf_tools.extract_layer_filament_usage_from_3mf",
+                return_value=None,  # No layer data available
+            ),
+        ):
+            mock_settings.base_dir = MagicMock()
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=10,
+                status="failed",
+                print_name="Benchy",
+                handled_trays=handled_trays,
+                printer_manager=printer_manager,
+                db=db,
+            )
+
+        assert len(results) == 1
+        # 50% of 20g = 10g
+        assert results[0]["weight_used"] == 10.0
+        # Tray should be marked as handled
+        assert (0, 0) in handled_trays
+
+    @pytest.mark.asyncio
+    async def test_per_layer_partial_print(self):
+        """Failed print at layer N uses gcode cumulative data."""
+        spool = _make_spool(spool_id=1, label_weight=1000)
+        assignment = _make_assignment(spool_id=1)
+        archive = _make_archive(archive_id=10)
+
+        # db: archive, queue_item(None), assignment, spool
+        db = _mock_db_sequential([archive, None, assignment, spool])
+
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            progress=50,
+            layer_num=25,
+            tray_now=0,
+        )
+
+        filament_usage = [{"slot_id": 1, "used_g": 20.0, "type": "PLA", "color": ""}]
+        # Per-layer data: at layer 25, filament 0 used 5000mm
+        layer_data = {10: {0: 2000.0}, 25: {0: 5000.0}, 50: {0: 10000.0}}
+        filament_props = {1: {"density": 1.24, "diameter": 1.75}}
+        handled_trays: set[tuple[int, int]] = set()
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch(
+                "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
+                return_value=filament_usage,
+            ),
+            patch(
+                "backend.app.utils.threemf_tools.extract_layer_filament_usage_from_3mf",
+                return_value=layer_data,
+            ),
+            patch(
+                "backend.app.utils.threemf_tools.get_cumulative_usage_at_layer",
+                return_value={0: 5000.0},
+            ),
+            patch(
+                "backend.app.utils.threemf_tools.extract_filament_properties_from_3mf",
+                return_value=filament_props,
+            ),
+            patch(
+                "backend.app.utils.threemf_tools.mm_to_grams",
+                return_value=12.0,  # 5000mm at 1.75mm/1.24g/cm3
+            ),
+        ):
+            mock_settings.base_dir = MagicMock()
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=10,
+                status="failed",
+                print_name="Benchy",
+                handled_trays=handled_trays,
+                printer_manager=printer_manager,
+                db=db,
+            )
+
+        assert len(results) == 1
+        # Should use per-layer grams (12.0g), not linear scale (10.0g)
+        assert results[0]["weight_used"] == 12.0
+
+    @pytest.mark.asyncio
+    async def test_completed_print_uses_full_weight(self):
+        """Completed print uses full 3MF weight (scale=1.0)."""
+        spool = _make_spool(spool_id=1, label_weight=1000)
+        assignment = _make_assignment(spool_id=1)
+        archive = _make_archive(archive_id=10)
+
+        # db: archive, queue_item(None), assignment, spool
+        db = _mock_db_sequential([archive, None, assignment, spool])
+
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            progress=100,
+            layer_num=50,
+            tray_now=0,
+        )
+
+        filament_usage = [{"slot_id": 1, "used_g": 20.0, "type": "PLA", "color": ""}]
+        handled_trays: set[tuple[int, int]] = set()
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch(
+                "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
+                return_value=filament_usage,
+            ),
+        ):
+            mock_settings.base_dir = MagicMock()
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=10,
+                status="completed",
+                print_name="Benchy",
+                handled_trays=handled_trays,
+                printer_manager=printer_manager,
+                db=db,
+            )
+
+        assert len(results) == 1
+        assert results[0]["weight_used"] == 20.0
+
+    @pytest.mark.asyncio
+    async def test_tray_now_override_for_single_filament(self):
+        """Single-filament non-queue print uses tray_now instead of slot_id mapping."""
+        # Spool 2 is at AMS1-T3 (global_tray_id=7)
+        spool = _make_spool(spool_id=2, label_weight=1000)
+        assignment = _make_assignment(spool_id=2, ams_id=1, tray_id=3)
+        archive = _make_archive(archive_id=10)
+
+        # db: archive, queue_item(None), assignment, spool
+        db = _mock_db_sequential([archive, None, assignment, spool])
+
+        # tray_now=7 = (ams_id=1, tray_id=3), the ACTUAL tray used
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            progress=100,
+            layer_num=50,
+            tray_now=7,
+        )
+
+        # 3MF has slot_id=12 (would default-map to ams_id=2, tray_id=3 — WRONG)
+        filament_usage = [{"slot_id": 12, "used_g": 10.6, "type": "PLA", "color": "#FF0000"}]
+        handled_trays: set[tuple[int, int]] = set()
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch(
+                "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
+                return_value=filament_usage,
+            ),
+        ):
+            mock_settings.base_dir = MagicMock()
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=10,
+                status="completed",
+                print_name="Test",
+                handled_trays=handled_trays,
+                printer_manager=printer_manager,
+                db=db,
+            )
+
+        assert len(results) == 1
+        assert results[0]["spool_id"] == 2
+        assert results[0]["ams_id"] == 1
+        assert results[0]["tray_id"] == 3
+        assert results[0]["weight_used"] == 10.6
+        assert (1, 3) in handled_trays
+
+    @pytest.mark.asyncio
+    async def test_queue_ams_mapping_overrides_default(self):
+        """Queue item ams_mapping overrides default slot_id mapping."""
+        # Spool at AMS1-T3 (global_tray_id=7)
+        spool = _make_spool(spool_id=5, label_weight=1000)
+        assignment = _make_assignment(spool_id=5, ams_id=1, tray_id=3)
+        archive = _make_archive(archive_id=20)
+        # Queue item maps slot 1 → global tray 7 (ams_id=1, tray_id=3)
+        queue_item = _make_queue_item(ams_mapping="[7, -1, -1, -1]")
+
+        # db: archive, queue_item, assignment, spool
+        db = _mock_db_sequential([archive, queue_item, assignment, spool])
+
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            progress=100,
+            layer_num=50,
+            tray_now=7,
+        )
+
+        filament_usage = [{"slot_id": 1, "used_g": 25.0, "type": "PETG", "color": ""}]
+        handled_trays: set[tuple[int, int]] = set()
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch(
+                "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
+                return_value=filament_usage,
+            ),
+        ):
+            mock_settings.base_dir = MagicMock()
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=20,
+                status="completed",
+                print_name="Queue Print",
+                handled_trays=handled_trays,
+                printer_manager=printer_manager,
+                db=db,
+            )
+
+        assert len(results) == 1
+        assert results[0]["spool_id"] == 5
+        assert results[0]["ams_id"] == 1
+        assert results[0]["tray_id"] == 3
+        assert results[0]["weight_used"] == 25.0
+
+    @pytest.mark.asyncio
+    async def test_multi_filament_uses_queue_mapping(self):
+        """Multi-filament queue prints use ams_mapping for each slot."""
+        spool_a = _make_spool(spool_id=1, label_weight=1000)
+        spool_b = _make_spool(spool_id=2, label_weight=1000)
+        assign_a = _make_assignment(spool_id=1, ams_id=0, tray_id=0)
+        assign_b = _make_assignment(spool_id=2, ams_id=1, tray_id=2)
+        archive = _make_archive(archive_id=30)
+        # slot 1 → tray 0 (AMS0-T0), slot 2 → tray 6 (AMS1-T2)
+        queue_item = _make_queue_item(ams_mapping="[0, 6]")
+
+        # db: archive, queue_item, assign_a, spool_a, assign_b, spool_b
+        db = _mock_db_sequential([archive, queue_item, assign_a, spool_a, assign_b, spool_b])
+
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            progress=100,
+            layer_num=50,
+            tray_now=6,
+        )
+
+        filament_usage = [
+            {"slot_id": 1, "used_g": 10.0, "type": "PLA", "color": ""},
+            {"slot_id": 2, "used_g": 5.0, "type": "PETG", "color": ""},
+        ]
+        handled_trays: set[tuple[int, int]] = set()
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch(
+                "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
+                return_value=filament_usage,
+            ),
+        ):
+            mock_settings.base_dir = MagicMock()
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=30,
+                status="completed",
+                print_name="Multi",
+                handled_trays=handled_trays,
+                printer_manager=printer_manager,
+                db=db,
+            )
+
+        assert len(results) == 2
+        assert results[0]["spool_id"] == 1
+        assert results[0]["ams_id"] == 0
+        assert results[0]["tray_id"] == 0
+        assert results[0]["weight_used"] == 10.0
+        assert results[1]["spool_id"] == 2
+        assert results[1]["ams_id"] == 1
+        assert results[1]["tray_id"] == 2
+        assert results[1]["weight_used"] == 5.0
+
+    @pytest.mark.asyncio
+    async def test_no_tray_now_override_for_multi_filament(self):
+        """Multi-filament non-queue prints fall back to default mapping, not tray_now."""
+        spool = _make_spool(spool_id=1, label_weight=1000)
+        assignment = _make_assignment(spool_id=1, ams_id=0, tray_id=0)
+        archive = _make_archive(archive_id=10)
+
+        # db: archive, queue_item(None), assignment, spool (2nd slot has no assignment)
+        db = _mock_db_sequential([archive, None, assignment, spool, None])
+
+        printer_manager = MagicMock()
+        printer_manager.get_status.return_value = SimpleNamespace(
+            progress=100,
+            layer_num=50,
+            tray_now=4,  # tray_now won't be used
+        )
+
+        # Two filament slots with usage
+        filament_usage = [
+            {"slot_id": 1, "used_g": 10.0, "type": "PLA", "color": ""},
+            {"slot_id": 2, "used_g": 5.0, "type": "PETG", "color": ""},
+        ]
+        handled_trays: set[tuple[int, int]] = set()
+
+        with (
+            patch("backend.app.core.config.settings") as mock_settings,
+            patch(
+                "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
+                return_value=filament_usage,
+            ),
+        ):
+            mock_settings.base_dir = MagicMock()
+            mock_path = MagicMock()
+            mock_path.exists.return_value = True
+            mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
+
+            results = await _track_from_3mf(
+                printer_id=1,
+                archive_id=10,
+                status="completed",
+                print_name="Test",
+                handled_trays=handled_trays,
+                printer_manager=printer_manager,
+                db=db,
+            )
+
+        # Should use default mapping (slot 1 → tray 0, slot 2 → tray 1)
+        assert len(results) == 1  # Only slot 1 has assignment
+        assert results[0]["ams_id"] == 0
+        assert results[0]["tray_id"] == 0
+
+
+class TestNotificationVariables:
+    """Tests for filament_details formatting in notifications."""
+
+    def test_filament_details_single_slot(self):
+        """Single slot produces 'PLA: 15.2g' format."""
+        slots = [{"type": "PLA", "used_g": 15.2, "slot_id": 1, "color": "#FF0000"}]
+        parts = []
+        for slot in slots:
+            ftype = slot.get("type", "Unknown") or "Unknown"
+            used = slot.get("used_g", 0)
+            parts.append(f"{ftype}: {used:.1f}g")
+        result = " | ".join(parts)
+        assert result == "PLA: 15.2g"
+
+    def test_filament_details_multi_slot(self):
+        """Multiple slots produce 'PLA: 10.0g | PETG: 5.0g' format."""
+        slots = [
+            {"type": "PLA", "used_g": 10.0, "slot_id": 1, "color": ""},
+            {"type": "PETG", "used_g": 5.0, "slot_id": 2, "color": ""},
+        ]
+        parts = []
+        for slot in slots:
+            ftype = slot.get("type", "Unknown") or "Unknown"
+            used = slot.get("used_g", 0)
+            parts.append(f"{ftype}: {used:.1f}g")
+        result = " | ".join(parts)
+        assert result == "PLA: 10.0g | PETG: 5.0g"
+
+    def test_filament_details_empty_type(self):
+        """Empty type defaults to 'Unknown'."""
+        slots = [{"type": "", "used_g": 5.0, "slot_id": 1, "color": ""}]
+        parts = []
+        for slot in slots:
+            ftype = slot.get("type", "Unknown") or "Unknown"
+            used = slot.get("used_g", 0)
+            parts.append(f"{ftype}: {used:.1f}g")
+        result = " | ".join(parts)
+        assert result == "Unknown: 5.0g"
+
+    def test_filament_grams_scaled_for_partial(self):
+        """filament_grams is scaled by progress for partial prints."""
+        filament_used_grams = 20.0
+        progress = 50
+        scale = max(0.0, min(progress / 100.0, 1.0))
+        scaled = round(filament_used_grams * scale, 1)
+        assert scaled == 10.0
+
+    def test_filament_grams_zero_progress(self):
+        """Progress=0 at cancellation gives 0.0g."""
+        filament_used_grams = 20.0
+        progress = 0
+        scale = max(0.0, min(progress / 100.0, 1.0))
+        scaled = round(filament_used_grams * scale, 1)
+        assert scaled == 0.0
+
+    def test_slot_scaling_for_partial(self):
+        """Per-slot usage is scaled linearly for partial prints."""
+        slots = [
+            {"type": "PLA", "used_g": 20.0, "slot_id": 1, "color": ""},
+            {"type": "PETG", "used_g": 10.0, "slot_id": 2, "color": ""},
+        ]
+        progress = 30
+        scale = max(0.0, min(progress / 100.0, 1.0))
+        scaled_slots = [{**s, "used_g": round(s["used_g"] * scale, 1)} for s in slots]
+        assert scaled_slots[0]["used_g"] == 6.0
+        assert scaled_slots[1]["used_g"] == 3.0

+ 193 - 0
docker-publish-beta.sh

@@ -0,0 +1,193 @@
+#!/bin/bash
+# Build and push multi-architecture Docker image to GHCR (private beta)
+#
+# Usage:
+#   ./docker-publish-beta.sh [version] [--parallel]
+#
+# Examples:
+#   ./docker-publish-beta.sh 0.2.0b            # Sequential build
+#   ./docker-publish-beta.sh 0.2.0b --parallel # Build both archs simultaneously
+#
+# All versions are also tagged as 'beta'
+#
+# Prerequisites:
+#   1. Log in to ghcr.io:
+#      echo $GITHUB_TOKEN | docker login ghcr.io -u YOUR_USERNAME --password-stdin
+#
+#   2. Create a GitHub Personal Access Token with 'write:packages' scope:
+#      https://github.com/settings/tokens/new?scopes=write:packages
+#
+#   3. After first push, set package to Private in GitHub → Packages → Settings
+#      and add beta testers via Manage Access
+#
+# Supported architectures:
+#   - linux/amd64 (x86_64, most servers/desktops)
+#   - linux/arm64 (Raspberry Pi 4/5, Apple Silicon via emulation)
+
+set -e
+
+# Configuration
+GHCR_REGISTRY="ghcr.io"
+IMAGE_NAME="maziggy/bambuddy-beta"
+GHCR_IMAGE="${GHCR_REGISTRY}/${IMAGE_NAME}"
+PLATFORMS="linux/amd64,linux/arm64"
+BUILDER_NAME="bambuddy-builder"
+
+# Colors for output
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+YELLOW='\033[1;33m'
+BLUE='\033[0;34m'
+NC='\033[0m' # No Color
+
+# Parse arguments
+VERSION=""
+PARALLEL=false
+for arg in "$@"; do
+    case $arg in
+        --parallel)
+            PARALLEL=true
+            ;;
+        *)
+            if [ -z "$VERSION" ]; then
+                VERSION="$arg"
+            fi
+            ;;
+    esac
+done
+
+if [ -z "$VERSION" ]; then
+    echo -e "${YELLOW}Usage: $0 <version> [--parallel]${NC}"
+    echo "Example: $0 0.2.0b"
+    echo "         $0 0.2.0b --parallel  # Build both architectures simultaneously"
+    exit 1
+fi
+
+# Get CPU count
+CPU_COUNT=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
+
+echo -e "${GREEN}================================================${NC}"
+echo -e "${GREEN}  Building multi-arch BETA image${NC}"
+echo -e "${GREEN}  Version: ${VERSION}${NC}"
+echo -e "${GREEN}  Platforms: ${PLATFORMS}${NC}"
+echo -e "${GREEN}  CPU cores: ${CPU_COUNT}${NC}"
+if [ "$PARALLEL" = true ]; then
+    echo -e "${GREEN}  Mode: PARALLEL (both archs simultaneously)${NC}"
+else
+    echo -e "${GREEN}  Mode: Sequential (amd64 → arm64)${NC}"
+fi
+echo -e "${GREEN}  Registry: ${GHCR_IMAGE}${NC}"
+echo -e "${GREEN}================================================${NC}"
+echo ""
+
+# Check registry login
+if ! grep -q "ghcr.io" ~/.docker/config.json 2>/dev/null; then
+    echo -e "${YELLOW}Warning: You may not be logged in to ghcr.io${NC}"
+    echo "Run: echo \$GITHUB_TOKEN | docker login ghcr.io -u YOUR_USERNAME --password-stdin"
+    echo ""
+fi
+
+# Setup buildx builder if not exists
+echo -e "${BLUE}[1/4] Setting up Docker Buildx...${NC}"
+if ! docker buildx inspect "$BUILDER_NAME" >/dev/null 2>&1; then
+    echo "Creating new buildx builder: $BUILDER_NAME (optimized for ${CPU_COUNT} cores)"
+    docker buildx create \
+        --name "$BUILDER_NAME" \
+        --driver docker-container \
+        --driver-opt network=host \
+        --driver-opt "env.BUILDKIT_STEP_LOG_MAX_SIZE=10000000" \
+        --buildkitd-flags "--allow-insecure-entitlement network.host --oci-worker-gc=false" \
+        --config /dev/stdin <<EOF
+[worker.oci]
+  max-parallelism = ${CPU_COUNT}
+EOF
+    docker buildx inspect --bootstrap "$BUILDER_NAME"
+fi
+docker buildx use "$BUILDER_NAME"
+
+# Verify builder supports multi-platform
+echo -e "${BLUE}[2/4] Verifying multi-platform support...${NC}"
+if ! docker buildx inspect --bootstrap | grep -q "linux/arm64"; then
+    echo -e "${YELLOW}Installing QEMU for cross-platform builds...${NC}"
+    docker run --privileged --rm tonistiigi/binfmt --install all
+fi
+
+# Build tags — version + beta (not latest)
+TAGS="-t ${GHCR_IMAGE}:${VERSION} -t ${GHCR_IMAGE}:beta"
+
+echo -e "${BLUE}[3/4] Building and pushing...${NC}"
+
+# Common build args (no cache to ensure clean builds)
+BUILD_ARGS="--provenance=false --sbom=false --no-cache --pull"
+
+if [ "$PARALLEL" = true ]; then
+    # Parallel build: Build each architecture separately then combine manifests
+    echo -e "${YELLOW}Building amd64 and arm64 in parallel (${CPU_COUNT} cores each, no cache)...${NC}"
+
+    # Build amd64 in background
+    (
+        echo -e "${BLUE}[amd64] Starting build...${NC}"
+        docker buildx build \
+            --platform linux/amd64 \
+            -t "${GHCR_IMAGE}:${VERSION}-amd64" \
+            ${BUILD_ARGS} \
+            --push \
+            . 2>&1 | sed 's/^/[amd64] /'
+        echo -e "${GREEN}[amd64] Complete!${NC}"
+    ) &
+    PID_AMD64=$!
+
+    # Build arm64 in background
+    (
+        echo -e "${BLUE}[arm64] Starting build...${NC}"
+        docker buildx build \
+            --platform linux/arm64 \
+            -t "${GHCR_IMAGE}:${VERSION}-arm64" \
+            ${BUILD_ARGS} \
+            --push \
+            . 2>&1 | sed 's/^/[arm64] /'
+        echo -e "${GREEN}[arm64] Complete!${NC}"
+    ) &
+    PID_ARM64=$!
+
+    # Wait for both builds
+    echo "Waiting for parallel builds to complete..."
+    wait $PID_AMD64
+    wait $PID_ARM64
+
+    # Create multi-arch manifest
+    echo -e "${BLUE}Creating multi-arch manifest...${NC}"
+    docker buildx imagetools create \
+        -t "${GHCR_IMAGE}:${VERSION}" -t "${GHCR_IMAGE}:beta" \
+        "${GHCR_IMAGE}:${VERSION}-amd64" \
+        "${GHCR_IMAGE}:${VERSION}-arm64"
+else
+    # Sequential build (default): Build both platforms in one command
+    echo -e "${YELLOW}Building sequentially with ${CPU_COUNT} cores (no cache)...${NC}"
+    DOCKER_BUILDKIT=1 docker buildx build \
+        --platform "$PLATFORMS" \
+        ${BUILD_ARGS} \
+        $TAGS \
+        --push \
+        .
+fi
+
+echo -e "${BLUE}[4/4] Verifying manifest...${NC}"
+docker buildx imagetools inspect "${GHCR_IMAGE}:${VERSION}"
+
+echo ""
+echo -e "${GREEN}================================================${NC}"
+echo -e "${GREEN}  Successfully pushed multi-arch BETA image:${NC}"
+echo -e "${GREEN}================================================${NC}"
+echo "  ${GHCR_IMAGE}:${VERSION}"
+echo "  ${GHCR_IMAGE}:beta"
+echo ""
+echo -e "${BLUE}Supported platforms:${NC}"
+echo "  - linux/amd64 (Intel/AMD servers, desktops)"
+echo "  - linux/arm64 (Raspberry Pi 4/5, Apple Silicon)"
+echo ""
+echo -e "${GREEN}Beta testers can run:${NC}"
+echo "  docker pull ${GHCR_IMAGE}:${VERSION}"
+echo "  docker pull ${GHCR_IMAGE}:beta"
+echo ""
+echo -e "${YELLOW}Reminder: Set package to Private in GitHub → Packages → Settings${NC}"

+ 2 - 0
frontend/src/App.tsx

@@ -14,6 +14,7 @@ import { FileManagerPage } from './pages/FileManagerPage';
 import { CameraPage } from './pages/CameraPage';
 import { StreamOverlayPage } from './pages/StreamOverlayPage';
 import { ExternalLinkPage } from './pages/ExternalLinkPage';
+import InventoryPage from './pages/InventoryPage';
 import { SystemInfoPage } from './pages/SystemInfoPage';
 import { LoginPage } from './pages/LoginPage';
 import { SetupPage } from './pages/SetupPage';
@@ -122,6 +123,7 @@ function App() {
                   <Route path="maintenance" element={<MaintenancePage />} />
                   <Route path="projects" element={<ProjectsPage />} />
                   <Route path="projects/:id" element={<ProjectDetailPage />} />
+                  <Route path="inventory" element={<InventoryPage />} />
                   <Route path="files" element={<FileManagerPage />} />
                   <Route path="settings" element={<AdminRoute><SettingsPage /></AdminRoute>} />
                   <Route path="users" element={<Navigate to="/settings?tab=users" replace />} />

+ 1 - 0
frontend/src/__tests__/components/AddPrinterDiscovery.test.tsx

@@ -38,6 +38,7 @@ const mockPrinterStatus = {
   remaining_time: 0,
   filename: null,
   wifi_signal: -50,
+  vt_tray: [],
 };
 
 describe('AddPrinterModal Discovery', () => {

+ 134 - 0
frontend/src/__tests__/components/AssignSpoolModal.test.tsx

@@ -0,0 +1,134 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { screen, waitFor } from '@testing-library/react';
+import { render } from '../utils';
+import { AssignSpoolModal } from '../../components/AssignSpoolModal';
+import { api } from '../../api/client';
+
+vi.mock('../../api/client', () => ({
+  api: {
+    getSpools: vi.fn(),
+    getAssignments: vi.fn(),
+    assignSpool: vi.fn(),
+    getSettings: vi.fn().mockResolvedValue({}),
+    getAuthStatus: vi.fn().mockResolvedValue({ auth_enabled: false }),
+  },
+}));
+
+const defaultProps = {
+  isOpen: true,
+  onClose: vi.fn(),
+  printerId: 1,
+  amsId: 0,
+  trayId: 0,
+  trayInfo: { type: 'PLA', color: 'FF0000', location: 'AMS 1 - Slot 1' },
+};
+
+const manualSpool = {
+  id: 1,
+  material: 'PLA',
+  subtype: 'Basic',
+  brand: 'Polymaker',
+  color_name: 'Red',
+  rgba: 'FF0000FF',
+  label_weight: 1000,
+  weight_used: 0,
+  tag_uid: null,
+  tray_uuid: null,
+};
+
+const blSpool = {
+  id: 2,
+  material: 'PLA',
+  subtype: 'Basic',
+  brand: 'Bambu',
+  color_name: 'Jade White',
+  rgba: 'FFFFFFFE',
+  label_weight: 1000,
+  weight_used: 50,
+  tag_uid: '05CC1E0F00000100',
+  tray_uuid: 'A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4',
+};
+
+const anotherManualSpool = {
+  id: 3,
+  material: 'PETG',
+  subtype: 'HF',
+  brand: 'Overture',
+  color_name: 'Black',
+  rgba: '000000FF',
+  label_weight: 1000,
+  weight_used: 200,
+  tag_uid: null,
+  tray_uuid: null,
+};
+
+describe('AssignSpoolModal', () => {
+  beforeEach(() => {
+    vi.clearAllMocks();
+    (api.getSpools as ReturnType<typeof vi.fn>).mockResolvedValue([manualSpool, blSpool, anotherManualSpool]);
+    (api.getAssignments as ReturnType<typeof vi.fn>).mockResolvedValue([]);
+  });
+
+  it('renders nothing when closed', () => {
+    render(<AssignSpoolModal {...defaultProps} isOpen={false} />);
+    expect(screen.queryByText('Assign Spool')).not.toBeInTheDocument();
+  });
+
+  it('filters out Bambu Lab spools (with tag_uid/tray_uuid)', async () => {
+    render(<AssignSpoolModal {...defaultProps} />);
+
+    await waitFor(() => {
+      expect(screen.getByText(/Polymaker/)).toBeInTheDocument();
+    });
+
+    // Manual spools should be visible
+    expect(screen.getByText(/Polymaker/)).toBeInTheDocument();
+    expect(screen.getByText(/Overture/)).toBeInTheDocument();
+
+    // BL spool should NOT be visible
+    expect(screen.queryByText(/Jade White/)).not.toBeInTheDocument();
+  });
+
+  it('filters out spools already assigned to other slots', async () => {
+    (api.getAssignments as ReturnType<typeof vi.fn>).mockResolvedValue([
+      { id: 1, spool_id: 3, printer_id: 1, ams_id: 0, tray_id: 1 }, // spool 3 assigned to different slot
+    ]);
+
+    render(<AssignSpoolModal {...defaultProps} />);
+
+    await waitFor(() => {
+      expect(screen.getByText(/Polymaker/)).toBeInTheDocument();
+    });
+
+    // Spool 1 (not assigned) should be visible
+    expect(screen.getByText(/Polymaker/)).toBeInTheDocument();
+
+    // Spool 3 (assigned to another slot) should NOT be visible
+    expect(screen.queryByText(/Overture/)).not.toBeInTheDocument();
+  });
+
+  it('keeps spool visible if assigned to the current slot', async () => {
+    (api.getAssignments as ReturnType<typeof vi.fn>).mockResolvedValue([
+      { id: 1, spool_id: 1, printer_id: 1, ams_id: 0, tray_id: 0 }, // spool 1 assigned to THIS slot
+    ]);
+
+    render(<AssignSpoolModal {...defaultProps} />);
+
+    await waitFor(() => {
+      expect(screen.getByText(/Polymaker/)).toBeInTheDocument();
+    });
+
+    // Spool 1 (assigned to current slot) should still be visible for re-assignment
+    expect(screen.getByText(/Polymaker/)).toBeInTheDocument();
+  });
+
+  it('shows noManualSpools message when all spools are BL or assigned', async () => {
+    (api.getSpools as ReturnType<typeof vi.fn>).mockResolvedValue([blSpool]);
+
+    render(<AssignSpoolModal {...defaultProps} />);
+
+    await waitFor(() => {
+      expect(screen.getByText(/No manually added spools/i)).toBeInTheDocument();
+    });
+  });
+});

+ 77 - 0
frontend/src/__tests__/components/ConfigureAmsSlotModal.test.tsx

@@ -18,6 +18,11 @@ vi.mock('../../api/client', () => ({
     saveSlotPreset: vi.fn(),
     getSettings: vi.fn().mockResolvedValue({}),
     updateSettings: vi.fn().mockResolvedValue({}),
+    getLocalPresets: vi.fn(),
+    getBuiltinFilaments: vi.fn(),
+    searchColors: vi.fn(),
+    getColorCatalog: vi.fn(),
+    resetAmsSlot: vi.fn(),
   },
 }));
 
@@ -69,10 +74,17 @@ const defaultProps = {
 describe('ConfigureAmsSlotModal', () => {
   beforeEach(() => {
     vi.clearAllMocks();
+    // Mock scrollIntoView which is not available in jsdom
+    Element.prototype.scrollIntoView = vi.fn();
     (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue(mockCloudSettings);
     (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue(mockKProfiles);
     (api.configureAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
     (api.saveSlotPreset as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
+    (api.getLocalPresets as ReturnType<typeof vi.fn>).mockResolvedValue({ filament: [] });
+    (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([]);
+    (api.searchColors as ReturnType<typeof vi.fn>).mockResolvedValue([]);
+    (api.getColorCatalog as ReturnType<typeof vi.fn>).mockResolvedValue([]);
+    (api.resetAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true, message: 'ok' });
   });
 
   it('renders nothing visible when closed', () => {
@@ -204,4 +216,69 @@ describe('ConfigureAmsSlotModal', () => {
     const configureButton = screen.getByRole('button', { name: /Configure Slot/i });
     expect(configureButton).toBeInTheDocument();
   });
+
+  it('filters presets by printer model', async () => {
+    // Render with printerModel="H2D"
+    render(<ConfigureAmsSlotModal {...defaultProps} printerModel="H2D" />);
+    // Wait for presets to load - the H2D preset should be visible
+    await waitFor(() => {
+      expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
+    });
+    // The X1C preset should NOT be visible (filtered out by model)
+    expect(screen.queryByText(/Bambu PLA Basic @BBL X1C/)).not.toBeInTheDocument();
+  });
+
+  it('shows current preset even when it does not match model filter', async () => {
+    // Render with printerModel="H2D" but savedPresetId pointing to the X1C preset
+    const slotInfo = {
+      ...defaultProps.slotInfo,
+      savedPresetId: 'GFSL05_09',  // X1C preset
+    };
+    render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} printerModel="H2D" />);
+    await waitFor(() => {
+      // Both should be visible - H2D matches model, X1C is saved preset
+      // Use the full preset name to match the list item (not the "Filtering for" label)
+      expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
+      expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
+    });
+  });
+
+  it('pre-selects saved preset when opening configured slot', async () => {
+    const slotInfo = {
+      ...defaultProps.slotInfo,
+      savedPresetId: 'GFSL05_09',
+    };
+    render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
+    await waitFor(() => {
+      // The saved preset should have the selected style (green border)
+      // Use the full preset name to avoid matching the "Filtering for" label
+      const presetButton = screen.getByText('Bambu PLA Basic @BBL X1C').closest('button');
+      expect(presetButton).toHaveClass('bg-bambu-green/20');
+    });
+  });
+
+  it('pre-populates color from trayColor', async () => {
+    const slotInfo = {
+      ...defaultProps.slotInfo,
+      trayColor: 'FF0000FF',  // Red with alpha
+    };
+    render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
+    await waitFor(() => {
+      expect(screen.getByTitle('White')).toBeInTheDocument();
+    });
+    // The hex display should show the pre-populated color
+    expect(screen.getByText('Hex: #FF0000', { exact: false })).toBeInTheDocument();
+  });
+
+  it('uses translated text for modal elements', async () => {
+    render(<ConfigureAmsSlotModal {...defaultProps} />);
+    await waitFor(() => {
+      expect(screen.getByText('Configure AMS Slot')).toBeInTheDocument();
+      expect(screen.getByText('Filament Profile')).toBeInTheDocument();
+    });
+    // Check footer buttons
+    expect(screen.getByRole('button', { name: /Configure Slot/i })).toBeInTheDocument();
+    expect(screen.getByRole('button', { name: /Cancel/i })).toBeInTheDocument();
+    expect(screen.getByRole('button', { name: /Reset Slot/i })).toBeInTheDocument();
+  });
 });

+ 77 - 141
frontend/src/__tests__/components/LinkSpoolModal.test.tsx

@@ -1,11 +1,11 @@
 /**
  * Tests for the LinkSpoolModal component.
  *
- * Tests the Spoolman link spool modal including:
- * - Displaying unlinked spools
- * - Selecting a spool to link
- * - Link success with toast notification
- * - Link error with toast notification
+ * Tests the inventory link-to-spool modal including:
+ * - Rendering modal with tag/tray info
+ * - Displaying untagged spools
+ * - Linking a spool via click
+ * - Search filtering
  */
 
 import { describe, it, expect, vi, beforeEach } from 'vitest';
@@ -16,10 +16,10 @@ import { LinkSpoolModal } from '../../components/LinkSpoolModal';
 // Mock the API client
 vi.mock('../../api/client', () => ({
   api: {
-    getUnlinkedSpools: vi.fn(),
-    linkSpool: vi.fn(),
+    getSpools: vi.fn(),
+    linkTagToSpool: vi.fn(),
     getSettings: vi.fn().mockResolvedValue({}),
-    getAuthStatus: vi.fn().mockResolvedValue({ enabled: false, configured: false }),
+    getAuthStatus: vi.fn().mockResolvedValue({ auth_enabled: false }),
   },
 }));
 
@@ -40,37 +40,56 @@ describe('LinkSpoolModal', () => {
   const defaultProps = {
     isOpen: true,
     onClose: vi.fn(),
+    tagUid: 'ABCD1234',
     trayUuid: 'A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4',
-    trayInfo: {
-      type: 'PLA Basic',
-      color: 'FF0000',
-      location: 'AMS A1',
-    },
+    printerId: 1,
+    amsId: 0,
+    trayId: 0,
   };
 
-  const mockUnlinkedSpools = [
+  const mockSpools = [
     {
       id: 1,
-      filament_name: 'PLA Red',
-      filament_material: 'PLA',
-      filament_color_hex: 'FF0000',
-      remaining_weight: 800,
-      location: 'Shelf A',
+      material: 'PLA',
+      brand: 'Generic',
+      subtype: '',
+      color_name: 'Red',
+      rgba: 'FF0000FF',
+      label_weight: 1000,
+      weight_used: 200,
+      tag_uid: null,
+      tray_uuid: null,
     },
     {
       id: 2,
-      filament_name: 'PETG Blue',
-      filament_material: 'PETG',
-      filament_color_hex: '0000FF',
-      remaining_weight: 500,
-      location: null,
+      material: 'PETG',
+      brand: 'Bambu',
+      subtype: 'Basic',
+      color_name: 'Blue',
+      rgba: '0000FFFF',
+      label_weight: 1000,
+      weight_used: 500,
+      tag_uid: null,
+      tray_uuid: null,
+    },
+    {
+      id: 3,
+      material: 'ABS',
+      brand: 'Brand',
+      subtype: '',
+      color_name: 'White',
+      rgba: 'FFFFFFFF',
+      label_weight: 1000,
+      weight_used: 0,
+      tag_uid: 'EXISTING_TAG',
+      tray_uuid: 'EXISTING_UUID',
     },
   ];
 
   beforeEach(() => {
     vi.clearAllMocks();
-    vi.mocked(api.getUnlinkedSpools).mockResolvedValue(mockUnlinkedSpools);
-    vi.mocked(api.linkSpool).mockResolvedValue({ success: true, message: 'Linked' });
+    vi.mocked(api.getSpools).mockResolvedValue(mockSpools);
+    vi.mocked(api.linkTagToSpool).mockResolvedValue({});
   });
 
   describe('rendering', () => {
@@ -78,33 +97,21 @@ describe('LinkSpoolModal', () => {
       render(<LinkSpoolModal {...defaultProps} />);
 
       await waitFor(() => {
-        // Look for the title in h2 element
-        expect(screen.getByRole('heading', { name: /link to spoolman/i })).toBeInTheDocument();
-      });
-    });
-
-    it('displays tray info', async () => {
-      render(<LinkSpoolModal {...defaultProps} />);
-
-      await waitFor(() => {
-        expect(screen.getByText('PLA Basic')).toBeInTheDocument();
-        expect(screen.getByText('(AMS A1)')).toBeInTheDocument();
+        expect(screen.getByRole('heading', { name: /link to spool/i })).toBeInTheDocument();
       });
     });
 
-    it('displays tray UUID', async () => {
+    it('displays printer and tray info', async () => {
       render(<LinkSpoolModal {...defaultProps} />);
 
       await waitFor(() => {
-        expect(screen.getByText(defaultProps.trayUuid)).toBeInTheDocument();
+        expect(screen.getByText(/AMS 0 T0/)).toBeInTheDocument();
+        expect(screen.getByText(/Printer #1/)).toBeInTheDocument();
       });
     });
 
     it('shows loading state while fetching spools', async () => {
-      // Delay the response
-      vi.mocked(api.getUnlinkedSpools).mockImplementation(
-        () => new Promise(() => {})
-      );
+      vi.mocked(api.getSpools).mockImplementation(() => new Promise(() => {}));
 
       render(<LinkSpoolModal {...defaultProps} />);
 
@@ -113,132 +120,74 @@ describe('LinkSpoolModal', () => {
       });
     });
 
-    it('displays unlinked spools list', async () => {
+    it('displays untagged spools only', async () => {
       render(<LinkSpoolModal {...defaultProps} />);
 
       await waitFor(() => {
-        expect(screen.getByText('PLA Red')).toBeInTheDocument();
-        expect(screen.getByText('PETG Blue')).toBeInTheDocument();
+        // Spools 1 and 2 have no tag_uid/tray_uuid — should be shown
+        expect(screen.getByText(/Generic PLA/)).toBeInTheDocument();
+        expect(screen.getByText(/Bambu PETG/)).toBeInTheDocument();
       });
-    });
-
-    it('shows message when no unlinked spools', async () => {
-      vi.mocked(api.getUnlinkedSpools).mockResolvedValue([]);
 
-      render(<LinkSpoolModal {...defaultProps} />);
-
-      await waitFor(() => {
-        expect(screen.getByText('No unlinked spools available')).toBeInTheDocument();
-      });
+      // Spool 3 has tag_uid — should be filtered out
+      expect(screen.queryByText(/Brand ABS/)).not.toBeInTheDocument();
     });
 
     it('does not render when isOpen is false', () => {
       render(<LinkSpoolModal {...defaultProps} isOpen={false} />);
-      expect(screen.queryByRole('heading', { name: /link to spoolman/i })).not.toBeInTheDocument();
-    });
-  });
-
-  describe('spool selection', () => {
-    it('allows selecting a spool', async () => {
-      render(<LinkSpoolModal {...defaultProps} />);
-
-      await waitFor(() => {
-        expect(screen.getByText('PLA Red')).toBeInTheDocument();
-      });
-
-      // Click to select spool
-      fireEvent.click(screen.getByText('PLA Red'));
-
-      // Should show check mark (via visual styling)
-      const selectedButton = screen.getByText('PLA Red').closest('button');
-      expect(selectedButton).toHaveClass('border-bambu-green');
-    });
-
-    it('link button is disabled until spool is selected', async () => {
-      render(<LinkSpoolModal {...defaultProps} />);
-
-      await waitFor(() => {
-        expect(screen.getByText('PLA Red')).toBeInTheDocument();
-      });
-
-      const linkButton = screen.getByRole('button', { name: /link to spoolman/i });
-      expect(linkButton).toBeDisabled();
-
-      // Select a spool
-      fireEvent.click(screen.getByText('PLA Red'));
-
-      expect(linkButton).not.toBeDisabled();
+      expect(screen.queryByRole('heading', { name: /link to spool/i })).not.toBeInTheDocument();
     });
   });
 
   describe('linking', () => {
-    it('calls linkSpool API on submit', async () => {
-      render(<LinkSpoolModal {...defaultProps} />);
-
-      await waitFor(() => {
-        expect(screen.getByText('PLA Red')).toBeInTheDocument();
-      });
-
-      // Select a spool
-      fireEvent.click(screen.getByText('PLA Red'));
-
-      // Click link button
-      fireEvent.click(screen.getByRole('button', { name: /link to spoolman/i }));
-
-      await waitFor(() => {
-        expect(api.linkSpool).toHaveBeenCalledWith(1, defaultProps.trayUuid);
-      });
-    });
-
-    it('shows success toast on successful link', async () => {
+    it('calls linkTagToSpool on spool click', async () => {
       render(<LinkSpoolModal {...defaultProps} />);
 
       await waitFor(() => {
-        expect(screen.getByText('PLA Red')).toBeInTheDocument();
+        expect(screen.getByText(/Generic PLA/)).toBeInTheDocument();
       });
 
-      fireEvent.click(screen.getByText('PLA Red'));
-      fireEvent.click(screen.getByRole('button', { name: /link to spoolman/i }));
+      fireEvent.click(screen.getByText(/Generic PLA/).closest('button')!);
 
       await waitFor(() => {
-        expect(mockShowToast).toHaveBeenCalledWith(
-          'Spool linked to Spoolman successfully',
-          'success'
-        );
+        expect(api.linkTagToSpool).toHaveBeenCalledWith(1, {
+          tag_uid: 'ABCD1234',
+          tray_uuid: 'A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4',
+          tag_type: 'bambulab',
+          data_origin: 'nfc_link',
+        });
       });
     });
 
-    it('calls onClose after successful link', async () => {
+    it('shows success toast and calls onClose', async () => {
       render(<LinkSpoolModal {...defaultProps} />);
 
       await waitFor(() => {
-        expect(screen.getByText('PLA Red')).toBeInTheDocument();
+        expect(screen.getByText(/Generic PLA/)).toBeInTheDocument();
       });
 
-      fireEvent.click(screen.getByText('PLA Red'));
-      fireEvent.click(screen.getByRole('button', { name: /link to spoolman/i }));
+      fireEvent.click(screen.getByText(/Generic PLA/).closest('button')!);
 
       await waitFor(() => {
+        expect(mockShowToast).toHaveBeenCalled();
         expect(defaultProps.onClose).toHaveBeenCalled();
       });
     });
 
-    it('shows error toast on link failure', async () => {
-      const errorMessage = 'Failed to update spool';
-      vi.mocked(api.linkSpool).mockRejectedValue(new Error(errorMessage));
+    it('shows error toast on failure', async () => {
+      vi.mocked(api.linkTagToSpool).mockRejectedValue(new Error('Link failed'));
 
       render(<LinkSpoolModal {...defaultProps} />);
 
       await waitFor(() => {
-        expect(screen.getByText('PLA Red')).toBeInTheDocument();
+        expect(screen.getByText(/Generic PLA/)).toBeInTheDocument();
       });
 
-      fireEvent.click(screen.getByText('PLA Red'));
-      fireEvent.click(screen.getByRole('button', { name: /link to spoolman/i }));
+      fireEvent.click(screen.getByText(/Generic PLA/).closest('button')!);
 
       await waitFor(() => {
         expect(mockShowToast).toHaveBeenCalledWith(
-          `Failed to link spool: ${errorMessage}`,
+          expect.stringContaining('Link failed'),
           'error'
         );
       });
@@ -246,25 +195,13 @@ describe('LinkSpoolModal', () => {
   });
 
   describe('modal actions', () => {
-    it('calls onClose when cancel button is clicked', async () => {
-      render(<LinkSpoolModal {...defaultProps} />);
-
-      await waitFor(() => {
-        expect(screen.getByText('Cancel')).toBeInTheDocument();
-      });
-
-      fireEvent.click(screen.getByText('Cancel'));
-      expect(defaultProps.onClose).toHaveBeenCalled();
-    });
-
     it('calls onClose when backdrop is clicked', async () => {
       render(<LinkSpoolModal {...defaultProps} />);
 
       await waitFor(() => {
-        expect(screen.getByRole('heading', { name: /link to spoolman/i })).toBeInTheDocument();
+        expect(screen.getByRole('heading', { name: /link to spool/i })).toBeInTheDocument();
       });
 
-      // Click the backdrop (the element with bg-black/60)
       const backdrop = document.querySelector('.bg-black\\/60');
       if (backdrop) {
         fireEvent.click(backdrop);
@@ -276,10 +213,9 @@ describe('LinkSpoolModal', () => {
       render(<LinkSpoolModal {...defaultProps} />);
 
       await waitFor(() => {
-        expect(screen.getByRole('heading', { name: /link to spoolman/i })).toBeInTheDocument();
+        expect(screen.getByRole('heading', { name: /link to spool/i })).toBeInTheDocument();
       });
 
-      // Find and click the X button in the header
       const closeButtons = screen.getAllByRole('button');
       const xButton = closeButtons.find(btn => btn.querySelector('svg.lucide-x'));
       if (xButton) {

+ 1 - 1
frontend/src/__tests__/components/PrintModal.test.tsx

@@ -67,7 +67,7 @@ describe('PrintModal', () => {
         return HttpResponse.json({ filaments: [] });
       }),
       http.get('/api/v1/printers/:id/status', () => {
-        return HttpResponse.json({ connected: true, state: 'IDLE', ams: [], vt_tray: null });
+        return HttpResponse.json({ connected: true, state: 'IDLE', ams: [], vt_tray: [] });
       }),
       http.post('/api/v1/archives/:id/reprint', () => {
         return HttpResponse.json({ success: true });

+ 177 - 0
frontend/src/__tests__/components/PrinterQueueWidgetClearPlate.test.tsx

@@ -0,0 +1,177 @@
+/**
+ * Tests for the PrinterQueueWidget clear plate behavior.
+ *
+ * When the printer is in FINISH or FAILED state and has pending queue items,
+ * the widget shows a "Clear Plate & Start Next" button instead of the
+ * passive queue link. After clicking, it shows a confirmation state.
+ */
+
+import { describe, it, expect, beforeEach } from 'vitest';
+import { screen, waitFor } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { render } from '../utils';
+import { PrinterQueueWidget } from '../../components/PrinterQueueWidget';
+import { http, HttpResponse } from 'msw';
+import { server } from '../mocks/server';
+
+const mockQueueItems = [
+  {
+    id: 1,
+    printer_id: 1,
+    archive_id: 1,
+    position: 1,
+    status: 'pending',
+    archive_name: 'First Print',
+    printer_name: 'X1 Carbon',
+    print_time_seconds: 3600,
+    scheduled_time: null,
+  },
+  {
+    id: 2,
+    printer_id: 1,
+    archive_id: 2,
+    position: 2,
+    status: 'pending',
+    archive_name: 'Second Print',
+    printer_name: 'X1 Carbon',
+    print_time_seconds: 7200,
+    scheduled_time: null,
+  },
+];
+
+describe('PrinterQueueWidget - Clear Plate', () => {
+  beforeEach(() => {
+    server.use(
+      http.get('/api/v1/queue/', ({ request }) => {
+        const url = new URL(request.url);
+        const printerId = url.searchParams.get('printer_id');
+        if (printerId === '1') {
+          return HttpResponse.json(mockQueueItems);
+        }
+        return HttpResponse.json([]);
+      }),
+      http.post('/api/v1/printers/:id/clear-plate', () => {
+        return HttpResponse.json({ success: true, message: 'Plate cleared' });
+      })
+    );
+  });
+
+  describe('clear plate button visibility', () => {
+    it('shows clear plate button when printer state is FINISH', async () => {
+      render(<PrinterQueueWidget printerId={1} printerState="FINISH" />);
+
+      await waitFor(() => {
+        expect(screen.getByText('Clear Plate & Start Next')).toBeInTheDocument();
+      });
+    });
+
+    it('shows clear plate button when printer state is FAILED', async () => {
+      render(<PrinterQueueWidget printerId={1} printerState="FAILED" />);
+
+      await waitFor(() => {
+        expect(screen.getByText('Clear Plate & Start Next')).toBeInTheDocument();
+      });
+    });
+
+    it('shows passive link when printer state is IDLE', async () => {
+      render(<PrinterQueueWidget printerId={1} printerState="IDLE" />);
+
+      await waitFor(() => {
+        const link = screen.getByRole('link');
+        expect(link).toHaveAttribute('href', '/queue');
+      });
+
+      expect(screen.queryByText('Clear Plate & Start Next')).not.toBeInTheDocument();
+    });
+
+    it('shows passive link when printer state is RUNNING', async () => {
+      render(<PrinterQueueWidget printerId={1} printerState="RUNNING" />);
+
+      await waitFor(() => {
+        const link = screen.getByRole('link');
+        expect(link).toHaveAttribute('href', '/queue');
+      });
+    });
+
+    it('shows passive link when printerState is not provided', async () => {
+      render(<PrinterQueueWidget printerId={1} />);
+
+      await waitFor(() => {
+        const link = screen.getByRole('link');
+        expect(link).toHaveAttribute('href', '/queue');
+      });
+    });
+  });
+
+  describe('clear plate button shows queue info', () => {
+    it('shows next item name in clear plate mode', async () => {
+      render(<PrinterQueueWidget printerId={1} printerState="FINISH" />);
+
+      await waitFor(() => {
+        expect(screen.getByText('First Print')).toBeInTheDocument();
+      });
+    });
+
+    it('shows additional items badge in clear plate mode', async () => {
+      render(<PrinterQueueWidget printerId={1} printerState="FINISH" />);
+
+      await waitFor(() => {
+        expect(screen.getByText('+1')).toBeInTheDocument();
+      });
+    });
+  });
+
+  describe('clear plate action', () => {
+    it('shows confirmation state after clicking clear plate', async () => {
+      const user = userEvent.setup();
+      render(<PrinterQueueWidget printerId={1} printerState="FINISH" />);
+
+      await waitFor(() => {
+        expect(screen.getByText('Clear Plate & Start Next')).toBeInTheDocument();
+      });
+
+      await user.click(screen.getByText('Clear Plate & Start Next'));
+
+      await waitFor(() => {
+        // Both the widget confirmation and the toast show this text
+        const elements = screen.getAllByText('Plate cleared — ready for next print');
+        expect(elements.length).toBeGreaterThanOrEqual(1);
+      });
+    });
+
+    it('shows error toast on API failure', async () => {
+      server.use(
+        http.post('/api/v1/printers/:id/clear-plate', () => {
+          return HttpResponse.json(
+            { detail: 'Printer not connected' },
+            { status: 400 }
+          );
+        })
+      );
+
+      const user = userEvent.setup();
+      render(<PrinterQueueWidget printerId={1} printerState="FAILED" />);
+
+      await waitFor(() => {
+        expect(screen.getByText('Clear Plate & Start Next')).toBeInTheDocument();
+      });
+
+      await user.click(screen.getByText('Clear Plate & Start Next'));
+
+      // Button should remain visible (not transition to success state)
+      await waitFor(() => {
+        expect(screen.getByText('Clear Plate & Start Next')).toBeInTheDocument();
+      });
+    });
+  });
+
+  describe('empty queue', () => {
+    it('renders nothing in FINISH state with no queue items', async () => {
+      const { container } = render(<PrinterQueueWidget printerId={999} printerState="FINISH" />);
+
+      await waitFor(() => {
+        expect(container.querySelector('button')).not.toBeInTheDocument();
+      });
+    });
+  });
+});

+ 58 - 120
frontend/src/__tests__/components/SpoolmanSettings.test.tsx

@@ -1,15 +1,16 @@
 /**
  * Tests for the SpoolmanSettings component.
  *
- * Tests the Spoolman integration UI including:
- * - Enable/disable toggle
- * - URL configuration
- * - Connection status
- * - Sync functionality
+ * Tests the filament tracking mode selector and Spoolman integration UI:
+ * - Mode selector (Built-in Inventory vs Spoolman)
+ * - Built-in Inventory info panel
+ * - Spoolman URL, sync mode, connection status
+ * - Weight sync and partial usage toggles
  */
 
 import { describe, it, expect, vi, beforeEach } from 'vitest';
 import { screen, waitFor } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
 import { render } from '../utils';
 import { SpoolmanSettings } from '../../components/SpoolmanSettings';
 
@@ -26,6 +27,7 @@ vi.mock('../../api/client', () => ({
     syncAllPrintersAms: vi.fn(),
     syncPrinterAms: vi.fn(),
     getPrinters: vi.fn(),
+    getAuthStatus: vi.fn().mockResolvedValue({ auth_enabled: false }),
   },
 }));
 
@@ -36,7 +38,7 @@ describe('SpoolmanSettings', () => {
   beforeEach(() => {
     vi.clearAllMocks();
 
-    // Default API mocks
+    // Default API mocks — Spoolman disabled (Built-in Inventory mode)
     vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
       spoolman_enabled: 'false',
       spoolman_url: '',
@@ -70,90 +72,61 @@ describe('SpoolmanSettings', () => {
 
   describe('rendering', () => {
     it('renders loading state initially', () => {
-      // Delay the API response to catch loading state
       vi.mocked(api.getSpoolmanSettings).mockImplementation(() => new Promise(() => {}));
       render(<SpoolmanSettings />);
 
-      // Should show loading spinner
       expect(document.querySelector('.animate-spin')).toBeInTheDocument();
     });
 
-    it('renders component title', async () => {
+    it('renders filament tracking title', async () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByText('Spoolman Integration')).toBeInTheDocument();
+        expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
       });
     });
 
-    it('renders enable toggle', async () => {
+    it('renders mode selector cards', async () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByText('Enable Spoolman')).toBeInTheDocument();
-      });
-    });
-
-    it('renders URL input', async () => {
-      render(<SpoolmanSettings />);
-
-      await waitFor(() => {
-        expect(screen.getByText('Spoolman URL')).toBeInTheDocument();
-        expect(screen.getByPlaceholderText('http://192.168.1.100:7912')).toBeInTheDocument();
-      });
-    });
-
-    it('renders sync mode selector', async () => {
-      render(<SpoolmanSettings />);
-
-      await waitFor(() => {
-        expect(screen.getByText('Sync Mode')).toBeInTheDocument();
-      });
-    });
-
-    it('renders info banner about sync', async () => {
-      render(<SpoolmanSettings />);
-
-      await waitFor(() => {
-        expect(screen.getByText('How Sync Works')).toBeInTheDocument();
-        expect(screen.getByText(/Only official Bambu Lab spools/)).toBeInTheDocument();
+        expect(screen.getByText('Built-in Inventory')).toBeInTheDocument();
+        expect(screen.getByText('Spoolman')).toBeInTheDocument();
       });
     });
   });
 
-  describe('disabled state', () => {
-    it('URL input is disabled when Spoolman is disabled', async () => {
+  describe('built-in inventory mode (default)', () => {
+    it('shows built-in inventory as selected by default', async () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        const urlInput = screen.getByPlaceholderText('http://192.168.1.100:7912');
-        expect(urlInput).toBeDisabled();
+        // Built-in Inventory card should have the active border
+        const builtInBtn = screen.getByText('Built-in Inventory').closest('button');
+        expect(builtInBtn).toHaveClass('border-bambu-green');
       });
     });
 
-    it('sync mode selector is disabled when Spoolman is disabled', async () => {
+    it('shows built-in info panel when selected', async () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        // Find the select by its display value
-        const selectElement = screen.getByDisplayValue('Automatic');
-        expect(selectElement).toBeDisabled();
+        expect(screen.getByText(/Automatically detects Bambu Lab RFID spools/)).toBeInTheDocument();
       });
     });
 
-    it('does not show connection status when disabled', async () => {
+    it('does not show Spoolman URL input', async () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByText('Spoolman Integration')).toBeInTheDocument();
+        expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
       });
 
-      // Status section should not be visible when disabled
-      expect(screen.queryByText('Status:')).not.toBeInTheDocument();
+      expect(screen.queryByPlaceholderText('http://192.168.1.100:7912')).not.toBeInTheDocument();
     });
   });
 
-  describe('enabled state', () => {
+  describe('spoolman mode', () => {
     beforeEach(() => {
       vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
         spoolman_enabled: 'true',
@@ -171,67 +144,62 @@ describe('SpoolmanSettings', () => {
       });
     });
 
-    it('URL input is enabled when Spoolman is enabled', async () => {
+    it('shows Spoolman card as selected', async () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        const urlInput = screen.getByPlaceholderText('http://192.168.1.100:7912');
-        expect(urlInput).not.toBeDisabled();
+        const spoolmanBtn = screen.getByText('Spoolman').closest('button');
+        expect(spoolmanBtn).toHaveClass('border-bambu-green');
       });
     });
 
-    it('shows connection status section when enabled', async () => {
+    it('shows URL input when Spoolman is selected', async () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByText('Status:')).toBeInTheDocument();
+        expect(screen.getByPlaceholderText('http://192.168.1.100:7912')).toBeInTheDocument();
       });
     });
 
-    it('shows Disconnected when not connected', async () => {
-      vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
-        enabled: true,
-        connected: false,
-        url: 'http://localhost:7912',
-      });
-
+    it('shows sync mode selector', async () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByText('Disconnected')).toBeInTheDocument();
+        expect(screen.getByText('Sync Mode')).toBeInTheDocument();
       });
     });
 
-    it('shows Connect button when disconnected', async () => {
-      vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
-        enabled: true,
-        connected: false,
-        url: 'http://localhost:7912',
+    it('shows how sync works info', async () => {
+      render(<SpoolmanSettings />);
+
+      await waitFor(() => {
+        expect(screen.getByText('How Sync Works')).toBeInTheDocument();
       });
+    });
 
+    it('shows connection status section', async () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByText('Connect')).toBeInTheDocument();
+        expect(screen.getByText('Status:')).toBeInTheDocument();
       });
     });
 
-    it('shows Connected and Disconnect button when connected', async () => {
+    it('shows Disconnected when not connected', async () => {
       vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
         enabled: true,
-        connected: true,
+        connected: false,
         url: 'http://localhost:7912',
       });
 
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByText('Connected')).toBeInTheDocument();
-        expect(screen.getByText('Disconnect')).toBeInTheDocument();
+        expect(screen.getByText('Disconnected')).toBeInTheDocument();
       });
     });
 
-    it('shows sync section when connected', async () => {
+    it('shows Connected and Disconnect button when connected', async () => {
       vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
         enabled: true,
         connected: true,
@@ -241,12 +209,12 @@ describe('SpoolmanSettings', () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByText('Sync AMS Data')).toBeInTheDocument();
-        expect(screen.getByText('Sync')).toBeInTheDocument();
+        expect(screen.getByText('Connected')).toBeInTheDocument();
+        expect(screen.getByText('Disconnect')).toBeInTheDocument();
       });
     });
 
-    it('shows All Printers option in sync dropdown', async () => {
+    it('shows sync section when connected', async () => {
       vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
         enabled: true,
         connected: true,
@@ -256,13 +224,13 @@ describe('SpoolmanSettings', () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByRole('option', { name: 'All Printers' })).toBeInTheDocument();
+        expect(screen.getByText('Sync AMS Data')).toBeInTheDocument();
       });
     });
   });
 
   describe('weight sync toggle', () => {
-    it('shows weight sync toggle when sync mode is auto and enabled', async () => {
+    it('shows weight sync toggle when Spoolman enabled and sync mode is auto', async () => {
       vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
         spoolman_enabled: 'true',
         spoolman_url: 'http://localhost:7912',
@@ -290,20 +258,11 @@ describe('SpoolmanSettings', () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByText('Spoolman Integration')).toBeInTheDocument();
+        expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
       });
 
       expect(screen.queryByText('Disable AMS Estimated Weight Sync')).not.toBeInTheDocument();
     });
-
-    it('shows weight sync toggle in disabled state when sync mode is auto', async () => {
-      render(<SpoolmanSettings />);
-
-      await waitFor(() => {
-        // Toggle label is visible since sync mode defaults to auto
-        expect(screen.getByText('Disable AMS Estimated Weight Sync')).toBeInTheDocument();
-      });
-    });
   });
 
   describe('partial usage toggle', () => {
@@ -335,49 +294,28 @@ describe('SpoolmanSettings', () => {
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByText('Spoolman Integration')).toBeInTheDocument();
+        expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
       });
 
       expect(screen.queryByText('Report Partial Usage for Failed Prints')).not.toBeInTheDocument();
     });
   });
 
-  describe('sync mode options', () => {
-    it('shows Automatic option', async () => {
+  describe('mode switching', () => {
+    it('can switch to Spoolman mode', async () => {
+      const user = userEvent.setup();
       render(<SpoolmanSettings />);
 
       await waitFor(() => {
-        expect(screen.getByRole('option', { name: 'Automatic' })).toBeInTheDocument();
+        expect(screen.getByText('Built-in Inventory')).toBeInTheDocument();
       });
-    });
 
-    it('shows Manual Only option', async () => {
-      render(<SpoolmanSettings />);
+      // Click Spoolman card
+      await user.click(screen.getByText('Spoolman').closest('button')!);
 
+      // Spoolman settings should now be visible
       await waitFor(() => {
-        expect(screen.getByRole('option', { name: 'Manual Only' })).toBeInTheDocument();
-      });
-    });
-  });
-
-  describe('info text', () => {
-    it('shows URL help text', async () => {
-      render(<SpoolmanSettings />);
-
-      await waitFor(() => {
-        expect(
-          screen.getByText('URL of your Spoolman server (e.g., http://localhost:7912)')
-        ).toBeInTheDocument();
-      });
-    });
-
-    it('shows sync mode description for auto mode', async () => {
-      render(<SpoolmanSettings />);
-
-      await waitFor(() => {
-        expect(
-          screen.getByText('AMS data syncs automatically when changes are detected')
-        ).toBeInTheDocument();
+        expect(screen.getByPlaceholderText('http://192.168.1.100:7912')).toBeInTheDocument();
       });
     });
   });

+ 182 - 3
frontend/src/__tests__/hooks/useFilamentMapping.test.ts

@@ -13,7 +13,7 @@ import {
 import type { PrinterStatus } from '../../api/client';
 
 // Helper to create a minimal printer status with AMS data
-function createPrinterStatus(ams: PrinterStatus['ams'], vt_tray?: PrinterStatus['vt_tray']): PrinterStatus {
+function createPrinterStatus(ams: PrinterStatus['ams'], vt_tray: PrinterStatus['vt_tray'] = []): PrinterStatus {
   return {
     ams,
     vt_tray,
@@ -89,7 +89,7 @@ describe('buildLoadedFilaments', () => {
   it('extracts external spool with tray_info_idx', () => {
     const status = createPrinterStatus(
       [],
-      { tray_type: 'TPU', tray_color: '0000FF', tray_info_idx: 'EXT001' }
+      [{ tray_type: 'TPU', tray_color: '0000FF', tray_info_idx: 'EXT001' }]
     );
 
     const result = buildLoadedFilaments(status);
@@ -339,7 +339,7 @@ describe('computeAmsMapping', () => {
     };
     const status = createPrinterStatus(
       [],
-      { tray_type: 'TPU', tray_color: '0000FF', tray_info_idx: 'EXT001' }
+      [{ tray_type: 'TPU', tray_color: '0000FF', tray_info_idx: 'EXT001' }]
     );
 
     const result = computeAmsMapping(reqs, status);
@@ -347,3 +347,182 @@ describe('computeAmsMapping', () => {
     expect(result).toEqual([254]);  // External spool global ID
   });
 });
+
+describe('buildLoadedFilaments - nozzle awareness', () => {
+  it('sets extruderId from ams_extruder_map', () => {
+    const status = createPrinterStatus([
+      {
+        id: 0,
+        tray: [{ id: 0, tray_type: 'PLA', tray_color: 'FF0000' }],
+      },
+      {
+        id: 1,
+        tray: [{ id: 0, tray_type: 'PETG', tray_color: '00FF00' }],
+      },
+    ]);
+    (status as any).ams_extruder_map = { '0': 1, '1': 0 };
+
+    const result = buildLoadedFilaments(status);
+
+    expect(result[0].extruderId).toBe(1);  // AMS 0 → left nozzle
+    expect(result[1].extruderId).toBe(0);  // AMS 1 → right nozzle
+  });
+
+  it('leaves extruderId undefined when no ams_extruder_map', () => {
+    const status = createPrinterStatus([
+      {
+        id: 0,
+        tray: [{ id: 0, tray_type: 'PLA', tray_color: 'FF0000' }],
+      },
+    ]);
+
+    const result = buildLoadedFilaments(status);
+
+    expect(result[0].extruderId).toBeUndefined();
+  });
+});
+
+describe('computeAmsMapping - nozzle filtering', () => {
+  it('filters candidates by nozzle_id when set', () => {
+    // Filament requires left nozzle (extruder 1), only AMS 0 is on left
+    const reqs = {
+      filaments: [
+        { slot_id: 1, type: 'PLA', color: '#FF0000', used_grams: 10, nozzle_id: 1 },
+      ],
+    };
+    const status = createPrinterStatus([
+      {
+        id: 0,  // Left nozzle
+        tray: [{ id: 0, tray_type: 'PLA', tray_color: 'FF0000' }],
+      },
+      {
+        id: 1,  // Right nozzle
+        tray: [{ id: 0, tray_type: 'PLA', tray_color: 'FF0000' }],
+      },
+    ]);
+    (status as any).ams_extruder_map = { '0': 1, '1': 0 };
+
+    const result = computeAmsMapping(reqs, status);
+
+    expect(result).toEqual([0]);  // AMS 0, tray 0 (on left nozzle)
+  });
+
+  it('filters to right nozzle when nozzle_id=0', () => {
+    const reqs = {
+      filaments: [
+        { slot_id: 1, type: 'PLA', color: '#FF0000', used_grams: 10, nozzle_id: 0 },
+      ],
+    };
+    const status = createPrinterStatus([
+      {
+        id: 0,  // Left nozzle
+        tray: [{ id: 0, tray_type: 'PLA', tray_color: 'FF0000' }],
+      },
+      {
+        id: 1,  // Right nozzle
+        tray: [{ id: 0, tray_type: 'PLA', tray_color: 'FF0000' }],
+      },
+    ]);
+    (status as any).ams_extruder_map = { '0': 1, '1': 0 };
+
+    const result = computeAmsMapping(reqs, status);
+
+    expect(result).toEqual([4]);  // AMS 1, tray 0 (global ID = 1*4+0 = 4, on right nozzle)
+  });
+
+  it('falls back to all trays when target nozzle has no trays at all', () => {
+    // Requires nozzle_id=1 (left), but no AMS units are on left nozzle
+    const reqs = {
+      filaments: [
+        { slot_id: 1, type: 'PLA', color: '#FF0000', used_grams: 10, nozzle_id: 1 },
+      ],
+    };
+    const status = createPrinterStatus([
+      {
+        id: 0,  // Right nozzle only
+        tray: [{ id: 0, tray_type: 'PLA', tray_color: 'FF0000' }],
+      },
+    ]);
+    (status as any).ams_extruder_map = { '0': 0 };  // AMS 0 → right nozzle, none on left
+
+    const result = computeAmsMapping(reqs, status);
+
+    expect(result).toEqual([0]);  // Falls back to unfiltered (right nozzle PLA)
+  });
+
+  it('stays restricted when target nozzle has trays but wrong type', () => {
+    // Left nozzle has PETG, right has PLA — but requires PLA on left
+    const reqs = {
+      filaments: [
+        { slot_id: 1, type: 'PLA', color: '#FF0000', used_grams: 10, nozzle_id: 1 },
+      ],
+    };
+    const status = createPrinterStatus([
+      {
+        id: 0,  // Left nozzle - only PETG
+        tray: [{ id: 0, tray_type: 'PETG', tray_color: '00FF00' }],
+      },
+      {
+        id: 1,  // Right nozzle - has PLA
+        tray: [{ id: 0, tray_type: 'PLA', tray_color: 'FF0000' }],
+      },
+    ]);
+    (status as any).ams_extruder_map = { '0': 1, '1': 0 };
+
+    const result = computeAmsMapping(reqs, status);
+
+    expect(result).toEqual([-1]);  // No PLA on left nozzle, stays restricted
+  });
+
+  it('skips nozzle filtering when nozzle_id is undefined', () => {
+    const reqs = {
+      filaments: [
+        { slot_id: 1, type: 'PLA', color: '#FF0000', used_grams: 10 },  // No nozzle_id
+      ],
+    };
+    const status = createPrinterStatus([
+      {
+        id: 0,
+        tray: [{ id: 0, tray_type: 'PETG', tray_color: '00FF00' }],
+      },
+      {
+        id: 1,
+        tray: [{ id: 0, tray_type: 'PLA', tray_color: 'FF0000' }],
+      },
+    ]);
+    (status as any).ams_extruder_map = { '0': 1, '1': 0 };
+
+    const result = computeAmsMapping(reqs, status);
+
+    expect(result).toEqual([4]);  // Picks best match regardless of nozzle
+  });
+
+  it('handles dual-nozzle multi-slot mapping', () => {
+    // Two filaments: one for left, one for right
+    const reqs = {
+      filaments: [
+        { slot_id: 1, type: 'PLA', color: '#FF0000', used_grams: 10, nozzle_id: 1 },  // Left
+        { slot_id: 2, type: 'PETG', color: '#00FF00', used_grams: 10, nozzle_id: 0 }, // Right
+      ],
+    };
+    const status = createPrinterStatus([
+      {
+        id: 0,  // Left nozzle
+        tray: [
+          { id: 0, tray_type: 'PLA', tray_color: 'FF0000' },
+        ],
+      },
+      {
+        id: 1,  // Right nozzle
+        tray: [
+          { id: 0, tray_type: 'PETG', tray_color: '00FF00' },
+        ],
+      },
+    ]);
+    (status as any).ams_extruder_map = { '0': 1, '1': 0 };
+
+    const result = computeAmsMapping(reqs, status);
+
+    expect(result).toEqual([0, 4]);  // Left gets AMS0-T0, Right gets AMS1-T0
+  });
+});

+ 1 - 0
frontend/src/__tests__/pages/PrintersPage.test.tsx

@@ -56,6 +56,7 @@ const mockPrinterStatus = {
   remaining_time: 0,
   filename: null,
   wifi_signal: -50,
+  vt_tray: [],
 };
 
 describe('PrintersPage', () => {

+ 3 - 3
frontend/src/__tests__/pages/SettingsPage.test.tsx

@@ -86,7 +86,7 @@ describe('SettingsPage', () => {
         expect(screen.getAllByText('General').length).toBeGreaterThan(0);
         expect(screen.getByText('Smart Plugs')).toBeInTheDocument();
         expect(screen.getByText('Notifications')).toBeInTheDocument();
-        expect(screen.getByText('Filament')).toBeInTheDocument();
+        expect(screen.getAllByText('Filament').length).toBeGreaterThan(0);
         expect(screen.getByText('Network')).toBeInTheDocument();
         expect(screen.getByText('API Keys')).toBeInTheDocument();
       });
@@ -207,10 +207,10 @@ describe('SettingsPage', () => {
       render(<SettingsPage />);
 
       await waitFor(() => {
-        expect(screen.getByText('Filament')).toBeInTheDocument();
+        expect(screen.getAllByText('Filament').length).toBeGreaterThan(0);
       });
 
-      await user.click(screen.getByText('Filament'));
+      await user.click(screen.getAllByText('Filament')[0]);
 
       await waitFor(() => {
         expect(screen.getByText('AMS Display Thresholds')).toBeInTheDocument();

+ 1 - 1
frontend/src/__tests__/pages/StatsPage.test.tsx

@@ -46,7 +46,7 @@ const mockArchives = [
 ];
 
 const mockSettings = {
-  currency: '$',
+  currency: 'USD',
   check_updates: false,
   check_printer_firmware: false,
 };

+ 43 - 0
frontend/src/__tests__/utils/currency.test.ts

@@ -0,0 +1,43 @@
+import { describe, it, expect } from 'vitest';
+import { getCurrencySymbol, SUPPORTED_CURRENCIES } from '../../utils/currency';
+
+describe('getCurrencySymbol', () => {
+  it('returns $ for USD', () => {
+    expect(getCurrencySymbol('USD')).toBe('$');
+  });
+
+  it('returns € for EUR', () => {
+    expect(getCurrencySymbol('EUR')).toBe('€');
+  });
+
+  it('returns £ for GBP', () => {
+    expect(getCurrencySymbol('GBP')).toBe('£');
+  });
+
+  it('returns ₹ for INR', () => {
+    expect(getCurrencySymbol('INR')).toBe('₹');
+  });
+
+  it('returns HK$ for HKD', () => {
+    expect(getCurrencySymbol('HKD')).toBe('HK$');
+  });
+
+  it('returns the code itself for unknown currencies', () => {
+    expect(getCurrencySymbol('XYZ')).toBe('XYZ');
+  });
+
+  it('is case-insensitive', () => {
+    expect(getCurrencySymbol('usd')).toBe('$');
+    expect(getCurrencySymbol('eur')).toBe('€');
+  });
+});
+
+describe('SUPPORTED_CURRENCIES', () => {
+  it('contains INR', () => {
+    expect(SUPPORTED_CURRENCIES.find((c) => c.code === 'INR')).toBeDefined();
+  });
+
+  it('has 25 entries', () => {
+    expect(SUPPORTED_CURRENCIES).toHaveLength(25);
+  });
+});

+ 216 - 16
frontend/src/api/client.ts

@@ -18,6 +18,18 @@ export function getAuthToken(): string | null {
   return authToken;
 }
 
+function parseContentDispositionFilename(header: string | null): string | null {
+  if (!header) return null;
+  // RFC 5987: filename*=utf-8''percent-encoded-name
+  const rfc5987Match = header.match(/filename\*=(?:UTF-8|utf-8)''(.+?)(?:;|$)/);
+  if (rfc5987Match) {
+    try { return decodeURIComponent(rfc5987Match[1]); } catch { /* fall through */ }
+  }
+  // Standard: filename="name" or filename=name
+  const standardMatch = header.match(/filename="?([^";\n]+)"?/);
+  return standardMatch?.[1] || null;
+}
+
 async function request<T>(
   endpoint: string,
   options: RequestInit = {}
@@ -192,7 +204,7 @@ export interface PrinterStatus {
   hms_errors: HMSError[];
   ams: AMSUnit[];
   ams_exists: boolean;
-  vt_tray: AMSTray | null;  // Virtual tray / external spool
+  vt_tray: AMSTray[];  // Virtual tray / external spool(s)
   sdcard: boolean;  // SD card inserted
   store_to_sdcard: boolean;  // Store sent files on SD card
   timelapse: boolean;  // Timelapse recording active
@@ -812,6 +824,29 @@ export interface SlicerSetting {
   version: string | null;
   user_id: string | null;
   updated_time: string | null;
+  is_custom: boolean;
+}
+
+export interface SpoolCatalogEntry {
+  id: number;
+  name: string;
+  weight: number;
+  is_default: boolean;
+}
+
+export interface ColorCatalogEntry {
+  id: number;
+  manufacturer: string;
+  color_name: string;
+  hex_color: string;
+  material: string | null;
+  is_default: boolean;
+}
+
+export interface ColorLookupResult {
+  found: boolean;
+  hex_color: string | null;
+  material: string | null;
 }
 
 export interface SlicerSettingsResponse {
@@ -853,6 +888,12 @@ export interface SlicerSettingDeleteResponse {
   message: string;
 }
 
+// Built-in filament fallback (static table from backend)
+export interface BuiltinFilament {
+  filament_id: string;
+  name: string;
+}
+
 // Local preset types (OrcaSlicer imports)
 export interface LocalPreset {
   id: number;
@@ -1686,6 +1727,85 @@ export interface LinkedSpoolsMap {
   linked: Record<string, LinkedSpoolInfo>; // tag (uppercase) -> spool info
 }
 
+// Inventory types
+export interface InventorySpool {
+  id: number;
+  material: string;
+  subtype: string | null;
+  color_name: string | null;
+  rgba: string | null;
+  brand: string | null;
+  label_weight: number;
+  core_weight: number;
+  weight_used: number;
+  slicer_filament: string | null;
+  slicer_filament_name: string | null;
+  nozzle_temp_min: number | null;
+  nozzle_temp_max: number | null;
+  note: string | null;
+  added_full: boolean | null;
+  last_used: string | null;
+  encode_time: string | null;
+  tag_uid: string | null;
+  tray_uuid: string | null;
+  data_origin: string | null;
+  tag_type: string | null;
+  archived_at: string | null;
+  created_at: string;
+  updated_at: string;
+  k_profiles?: SpoolKProfile[];
+}
+
+export interface SpoolUsageRecord {
+  id: number;
+  spool_id: number;
+  printer_id: number | null;
+  print_name: string | null;
+  weight_used: number;
+  percent_used: number;
+  status: string;
+  created_at: string;
+}
+
+export interface SpoolKProfile {
+  id: number;
+  spool_id: number;
+  printer_id: number;
+  extruder: number;
+  nozzle_diameter: string;
+  nozzle_type: string | null;
+  k_value: number;
+  name: string | null;
+  cali_idx: number | null;
+  setting_id: string | null;
+  created_at: string;
+}
+
+export interface SpoolKProfileInput {
+  printer_id: number;
+  extruder?: number;
+  nozzle_diameter?: string;
+  nozzle_type?: string | null;
+  k_value: number;
+  name?: string | null;
+  cali_idx?: number | null;
+  setting_id?: string | null;
+}
+
+export interface SpoolAssignment {
+  id: number;
+  spool_id: number;
+  printer_id: number;
+  printer_name: string | null;
+  ams_id: number;
+  tray_id: number;
+  fingerprint_color: string | null;
+  fingerprint_type: string | null;
+  spool?: InventorySpool | null;
+  configured: boolean;
+  created_at: string;
+}
+
 // Update types
 export interface VersionInfo {
   version: string;
@@ -1792,6 +1912,7 @@ export interface ExternalLink {
   name: string;
   url: string;
   icon: string;
+  open_in_new_tab: boolean;
   custom_icon: string | null;
   sort_order: number;
   created_at: string;
@@ -1802,12 +1923,14 @@ export interface ExternalLinkCreate {
   name: string;
   url: string;
   icon: string;
+  open_in_new_tab?: boolean;
 }
 
 export interface ExternalLinkUpdate {
   name?: string;
   url?: string;
   icon?: string;
+  open_in_new_tab?: boolean;
 }
 
 // Permission type - all available permissions
@@ -2019,7 +2142,7 @@ export const api = {
     request<{ message: string; auth_enabled: boolean }>('/auth/disable', {
       method: 'POST',
     }),
-  
+
   // Advanced Authentication
   testSMTP: (data: TestSMTPRequest) =>
     request<TestSMTPResponse>('/auth/smtp/test', {
@@ -2155,6 +2278,10 @@ export const api = {
     request<{ success: boolean; message: string }>(`/printers/${printerId}/print/resume`, {
       method: 'POST',
     }),
+  clearPlate: (printerId: number) =>
+    request<{ success: boolean; message: string }>(`/printers/${printerId}/clear-plate`, {
+      method: 'POST',
+    }),
 
   // Get current print user (for reprint tracking - Issue #206)
   getCurrentPrintUser: (printerId: number) =>
@@ -2263,8 +2390,7 @@ export const api = {
       throw new Error(error.detail || `HTTP ${response.status}`);
     }
     const disposition = response.headers.get('Content-Disposition');
-    const filenameMatch = disposition?.match(/filename="?([^";\n]+)"?/);
-    const filename = filenameMatch?.[1] || path.split('/').pop() || 'download';
+    const filename = parseContentDispositionFilename(disposition) || path.split('/').pop() || 'download';
     const blob = await response.blob();
     const url = window.URL.createObjectURL(blob);
     const a = document.createElement('a');
@@ -2464,8 +2590,7 @@ export const api = {
       throw new Error(error.detail || `HTTP ${response.status}`);
     }
     const disposition = response.headers.get('Content-Disposition');
-    const filenameMatch = disposition?.match(/filename="?([^";\n]+)"?/);
-    const downloadFilename = filenameMatch?.[1] || filename || `archive_${id}.3mf`;
+    const downloadFilename = parseContentDispositionFilename(disposition) || filename || `archive_${id}.3mf`;
     const blob = await response.blob();
     const url = window.URL.createObjectURL(blob);
     const a = document.createElement('a');
@@ -2605,8 +2730,7 @@ export const api = {
       throw new Error(error.detail || `HTTP ${response.status}`);
     }
     const disposition = response.headers.get('Content-Disposition');
-    const filenameMatch = disposition?.match(/filename="?([^";\n]+)"?/);
-    const filename = filenameMatch?.[1] || `source_${archiveId}.3mf`;
+    const filename = parseContentDispositionFilename(disposition) || `source_${archiveId}.3mf`;
     const blob = await response.blob();
     const url = window.URL.createObjectURL(blob);
     const a = document.createElement('a');
@@ -2655,8 +2779,7 @@ export const api = {
       throw new Error(error.detail || `HTTP ${response.status}`);
     }
     const disposition = response.headers.get('Content-Disposition');
-    const filenameMatch = disposition?.match(/filename="?([^";\n]+)"?/);
-    const filename = filenameMatch?.[1] || `archive_${archiveId}.f3d`;
+    const filename = parseContentDispositionFilename(disposition) || `archive_${archiveId}.f3d`;
     const blob = await response.blob();
     const url = window.URL.createObjectURL(blob);
     const a = document.createElement('a');
@@ -2903,6 +3026,10 @@ export const api = {
     request<{ success: boolean }>('/cloud/logout', { method: 'POST' }),
   getCloudSettings: (version = '02.04.00.70') =>
     request<SlicerSettingsResponse>(`/cloud/settings?version=${version}`),
+  getBuiltinFilaments: () =>
+    request<BuiltinFilament[]>('/cloud/builtin-filaments'),
+  getFilamentIdMap: () =>
+    request<Record<string, string>>('/cloud/filament-id-map'),
   getCloudSettingDetail: (settingId: string) =>
     request<SlicerSettingDetail>(`/cloud/settings/${settingId}`),
   createCloudSetting: (data: SlicerSettingCreate) =>
@@ -3242,6 +3369,80 @@ export const api = {
       body: JSON.stringify(data),
     }),
 
+  // Inventory
+  getSpools: (includeArchived = false) =>
+    request<InventorySpool[]>(`/inventory/spools?include_archived=${includeArchived}`),
+  getSpool: (id: number) => request<InventorySpool>(`/inventory/spools/${id}`),
+  createSpool: (data: Omit<InventorySpool, 'id' | 'archived_at' | 'created_at' | 'updated_at' | 'k_profiles'>) =>
+    request<InventorySpool>('/inventory/spools', {
+      method: 'POST',
+      body: JSON.stringify(data),
+    }),
+  updateSpool: (id: number, data: Partial<Omit<InventorySpool, 'id' | 'archived_at' | 'created_at' | 'updated_at' | 'k_profiles'>>) =>
+    request<InventorySpool>(`/inventory/spools/${id}`, {
+      method: 'PATCH',
+      body: JSON.stringify(data),
+    }),
+  deleteSpool: (id: number) =>
+    request<{ status: string }>(`/inventory/spools/${id}`, { method: 'DELETE' }),
+  archiveSpool: (id: number) =>
+    request<InventorySpool>(`/inventory/spools/${id}/archive`, { method: 'POST' }),
+  restoreSpool: (id: number) =>
+    request<InventorySpool>(`/inventory/spools/${id}/restore`, { method: 'POST' }),
+  getSpoolKProfiles: (spoolId: number) =>
+    request<SpoolKProfile[]>(`/inventory/spools/${spoolId}/k-profiles`),
+  saveSpoolKProfiles: (spoolId: number, profiles: SpoolKProfileInput[]) =>
+    request<SpoolKProfile[]>(`/inventory/spools/${spoolId}/k-profiles`, {
+      method: 'PUT',
+      body: JSON.stringify(profiles),
+    }),
+  getAssignments: (printerId?: number) =>
+    request<SpoolAssignment[]>(`/inventory/assignments${printerId ? `?printer_id=${printerId}` : ''}`),
+  assignSpool: (data: { spool_id: number; printer_id: number; ams_id: number; tray_id: number }) =>
+    request<SpoolAssignment>('/inventory/assignments', {
+      method: 'POST',
+      body: JSON.stringify(data),
+    }),
+  unassignSpool: (printerId: number, amsId: number, trayId: number) =>
+    request<{ status: string }>(`/inventory/assignments/${printerId}/${amsId}/${trayId}`, { method: 'DELETE' }),
+  getSpoolCatalog: () =>
+    request<SpoolCatalogEntry[]>('/inventory/catalog'),
+  addCatalogEntry: (data: { name: string; weight: number }) =>
+    request<SpoolCatalogEntry>('/inventory/catalog', { method: 'POST', body: JSON.stringify(data) }),
+  updateCatalogEntry: (id: number, data: { name: string; weight: number }) =>
+    request<SpoolCatalogEntry>(`/inventory/catalog/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
+  deleteCatalogEntry: (id: number) =>
+    request<{ status: string }>(`/inventory/catalog/${id}`, { method: 'DELETE' }),
+  resetSpoolCatalog: () =>
+    request<{ status: string }>('/inventory/catalog/reset', { method: 'POST' }),
+  getColorCatalog: () =>
+    request<ColorCatalogEntry[]>('/inventory/colors'),
+  addColorEntry: (data: { manufacturer: string; color_name: string; hex_color: string; material: string | null }) =>
+    request<ColorCatalogEntry>('/inventory/colors', { method: 'POST', body: JSON.stringify(data) }),
+  updateColorEntry: (id: number, data: { manufacturer: string; color_name: string; hex_color: string; material: string | null }) =>
+    request<ColorCatalogEntry>(`/inventory/colors/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
+  deleteColorEntry: (id: number) =>
+    request<{ status: string }>(`/inventory/colors/${id}`, { method: 'DELETE' }),
+  resetColorCatalog: () =>
+    request<{ status: string }>('/inventory/colors/reset', { method: 'POST' }),
+  lookupColor: (manufacturer: string, colorName: string, material?: string) =>
+    request<ColorLookupResult>(`/inventory/colors/lookup?manufacturer=${encodeURIComponent(manufacturer)}&color_name=${encodeURIComponent(colorName)}${material ? `&material=${encodeURIComponent(material)}` : ''}`),
+  searchColors: (manufacturer?: string, material?: string) =>
+    request<ColorCatalogEntry[]>(`/inventory/colors/search?${manufacturer ? `manufacturer=${encodeURIComponent(manufacturer)}` : ''}${manufacturer && material ? '&' : ''}${material ? `material=${encodeURIComponent(material)}` : ''}`),
+  linkTagToSpool: (spoolId: number, data: { tag_uid?: string; tray_uuid?: string; tag_type?: string; data_origin?: string }) =>
+    request<InventorySpool>(`/inventory/spools/${spoolId}/link-tag`, {
+      method: 'PATCH',
+      body: JSON.stringify(data),
+    }),
+  getSpoolUsageHistory: (spoolId: number, limit = 50) =>
+    request<SpoolUsageRecord[]>(`/inventory/spools/${spoolId}/usage?limit=${limit}`),
+  getAllUsageHistory: (limit = 100, printerId?: number) =>
+    request<SpoolUsageRecord[]>(`/inventory/usage?limit=${limit}${printerId ? `&printer_id=${printerId}` : ''}`),
+  clearSpoolUsageHistory: (spoolId: number) =>
+    request<{ status: string }>(`/inventory/spools/${spoolId}/usage`, { method: 'DELETE' }),
+  getFilamentPresets: () =>
+    request<SlicerSetting[]>('/cloud/filaments'),
+
   // Updates
   getVersion: () => request<VersionInfo>('/updates/version'),
   checkForUpdates: () => request<UpdateCheckResult>('/updates/check'),
@@ -3265,6 +3466,8 @@ export const api = {
     }),
   deleteMaintenanceType: (id: number) =>
     request<{ status: string }>(`/maintenance/types/${id}`, { method: 'DELETE' }),
+  restoreDefaultMaintenanceTypes: () =>
+    request<{ restored: number }>(`/maintenance/types/restore-defaults`, { method: 'POST' }),
   getMaintenanceOverview: () => request<PrinterMaintenanceOverview[]>('/maintenance/overview'),
   getPrinterMaintenance: (printerId: number) =>
     request<PrinterMaintenanceOverview>(`/maintenance/printers/${printerId}`),
@@ -3539,8 +3742,7 @@ export const api = {
       throw new Error(error.detail || `HTTP ${response.status}`);
     }
     const contentDisposition = response.headers.get('Content-Disposition');
-    const filenameMatch = contentDisposition?.match(/filename="(.+)"/);
-    const filename = filenameMatch?.[1] || `project_${projectId}.zip`;
+    const filename = parseContentDispositionFilename(contentDisposition) || `project_${projectId}.zip`;
     const blob = await response.blob();
     return { blob, filename };
   },
@@ -3668,8 +3870,7 @@ export const api = {
       throw new Error(error.detail || `HTTP ${response.status}`);
     }
     const disposition = response.headers.get('Content-Disposition');
-    const filenameMatch = disposition?.match(/filename="?([^";\n]+)"?/);
-    const downloadFilename = filenameMatch?.[1] || filename || `file_${id}`;
+    const downloadFilename = parseContentDispositionFilename(disposition) || filename || `file_${id}`;
     const blob = await response.blob();
     const url = window.URL.createObjectURL(blob);
     const a = document.createElement('a');
@@ -4348,8 +4549,7 @@ export const supportApi = {
     }
     // Get filename from Content-Disposition header or use default
     const disposition = response.headers.get('Content-Disposition');
-    const filenameMatch = disposition?.match(/filename=(.+)/);
-    const filename = filenameMatch ? filenameMatch[1] : 'bambuddy-support.zip';
+    const filename = parseContentDispositionFilename(disposition) || 'bambuddy-support.zip';
 
     // Download the blob
     const blob = await response.blob();

+ 22 - 0
frontend/src/components/AddExternalLinkModal.tsx

@@ -1,6 +1,7 @@
 import { useState, useEffect, useRef } from 'react';
 import { useMutation, useQueryClient } from '@tanstack/react-query';
 import { X, Save, Loader2, Upload, Trash2 } from 'lucide-react';
+import { useTranslation } from 'react-i18next';
 import { api } from '../api/client';
 import type { ExternalLink, ExternalLinkCreate, ExternalLinkUpdate } from '../api/client';
 import { Button } from './Button';
@@ -11,6 +12,7 @@ interface AddExternalLinkModalProps {
 }
 
 export function AddExternalLinkModal({ link, onClose }: AddExternalLinkModalProps) {
+  const { t } = useTranslation();
   const queryClient = useQueryClient();
   const isEditing = !!link;
   const fileInputRef = useRef<HTMLInputElement>(null);
@@ -18,6 +20,7 @@ export function AddExternalLinkModal({ link, onClose }: AddExternalLinkModalProp
   const [name, setName] = useState(link?.name || '');
   const [url, setUrl] = useState(link?.url || '');
   const [icon, setIcon] = useState(link?.icon || 'link');
+  const [openInNewTab, setOpenInNewTab] = useState(link?.open_in_new_tab || false);
   const [useCustomIcon, setUseCustomIcon] = useState(!!link?.custom_icon);
   const [customIconPreview, setCustomIconPreview] = useState<string | null>(
     link?.custom_icon ? api.getExternalLinkIconUrl(link.id) : null
@@ -137,6 +140,7 @@ export function AddExternalLinkModal({ link, onClose }: AddExternalLinkModalProp
       name: name.trim(),
       url: url.trim(),
       icon: useCustomIcon ? icon : icon, // Keep preset icon as fallback
+      open_in_new_tab: openInNewTab,
     };
 
     if (isEditing) {
@@ -213,6 +217,24 @@ export function AddExternalLinkModal({ link, onClose }: AddExternalLinkModalProp
             />
           </div>
 
+          {/* Open in New Tab */}
+          <div className="flex items-center justify-between">
+            <label className="text-sm text-bambu-gray">{t('externalLinks.openInNewTab')}</label>
+            <button
+              type="button"
+              onClick={() => setOpenInNewTab(!openInNewTab)}
+              className={`relative inline-flex h-6 w-11 items-center rounded-full transition-colors ${
+                openInNewTab ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
+              }`}
+            >
+              <span
+                className={`inline-block h-4 w-4 transform rounded-full bg-white transition-transform ${
+                  openInNewTab ? 'translate-x-6' : 'translate-x-1'
+                }`}
+              />
+            </button>
+          </div>
+
           {/* Icon Section */}
           <div className="space-y-3">
             <label className="block text-sm text-bambu-gray">Icon</label>

+ 233 - 0
frontend/src/components/AssignSpoolModal.tsx

@@ -0,0 +1,233 @@
+import { useState } from 'react';
+import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
+import { useTranslation } from 'react-i18next';
+import { X, Loader2, Package, Check, Search } from 'lucide-react';
+import { api } from '../api/client';
+import type { InventorySpool, SpoolAssignment } from '../api/client';
+import { Button } from './Button';
+import { useToast } from '../contexts/ToastContext';
+
+interface AssignSpoolModalProps {
+  isOpen: boolean;
+  onClose: () => void;
+  printerId: number;
+  amsId: number;
+  trayId: number;
+  trayInfo?: {
+    type: string;
+    color: string;
+    location: string;
+  };
+}
+
+export function AssignSpoolModal({ isOpen, onClose, printerId, amsId, trayId, trayInfo }: AssignSpoolModalProps) {
+  const { t } = useTranslation();
+  const queryClient = useQueryClient();
+  const { showToast } = useToast();
+  const [selectedSpoolId, setSelectedSpoolId] = useState<number | null>(null);
+  const [searchFilter, setSearchFilter] = useState('');
+
+  const { data: spools, isLoading } = useQuery({
+    queryKey: ['inventory-spools'],
+    queryFn: () => api.getSpools(),
+    enabled: isOpen,
+  });
+
+  const { data: assignments } = useQuery({
+    queryKey: ['spool-assignments'],
+    queryFn: () => api.getAssignments(),
+    enabled: isOpen,
+  });
+
+  const assignMutation = useMutation({
+    mutationFn: (spoolId: number) =>
+      api.assignSpool({ spool_id: spoolId, printer_id: printerId, ams_id: amsId, tray_id: trayId }),
+    onSuccess: (newAssignment) => {
+      // Immediately update cache so UI reflects the new assignment without waiting for refetch
+      queryClient.setQueryData<SpoolAssignment[]>(['spool-assignments'], (old) => {
+        const filtered = (old || []).filter(a =>
+          !(a.printer_id === printerId && a.ams_id === amsId && a.tray_id === trayId)
+        );
+        filtered.push(newAssignment);
+        return filtered;
+      });
+      queryClient.invalidateQueries({ queryKey: ['spool-assignments'] });
+      showToast(t('inventory.assignSuccess'), 'success');
+      onClose();
+    },
+    onError: (error: Error) => {
+      showToast(`${t('inventory.assignFailed')}: ${error.message}`, 'error');
+    },
+  });
+
+  if (!isOpen) return null;
+
+  // Filter out spools already assigned to other slots
+  const assignedSpoolIds = new Set(
+    (assignments || [])
+      .filter(a => !(a.printer_id === printerId && a.ams_id === amsId && a.tray_id === trayId))
+      .map(a => a.spool_id)
+  );
+  // External slots (amsId 254 or 255) have no RFID reader, so show all spools.
+  // AMS slots only show manual spools (no tag_uid or tray_uuid).
+  const isExternalSlot = amsId === 254 || amsId === 255;
+  const manualSpools = spools?.filter((spool: InventorySpool) =>
+    !assignedSpoolIds.has(spool.id) && (isExternalSlot || (!spool.tag_uid && !spool.tray_uuid))
+  );
+
+  const filteredSpools = manualSpools?.filter((spool: InventorySpool) => {
+    if (!searchFilter) return true;
+    const q = searchFilter.toLowerCase();
+    return (
+      spool.material.toLowerCase().includes(q) ||
+      (spool.brand?.toLowerCase().includes(q) ?? false) ||
+      (spool.color_name?.toLowerCase().includes(q) ?? false) ||
+      (spool.subtype?.toLowerCase().includes(q) ?? false)
+    );
+  });
+
+  const handleAssign = () => {
+    if (selectedSpoolId) {
+      assignMutation.mutate(selectedSpoolId);
+    }
+  };
+
+  return (
+    <div className="fixed inset-0 z-50 flex items-center justify-center">
+      <div
+        className="absolute inset-0 bg-black/60 backdrop-blur-sm"
+        onClick={onClose}
+      />
+
+      <div className="relative w-full max-w-md mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl">
+        {/* Header */}
+        <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
+          <div className="flex items-center gap-2">
+            <Package className="w-5 h-5 text-bambu-green" />
+            <h2 className="text-lg font-semibold text-white">{t('inventory.assignSpool')}</h2>
+          </div>
+          <button
+            onClick={onClose}
+            className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
+          >
+            <X className="w-5 h-5" />
+          </button>
+        </div>
+
+        {/* Content */}
+        <div className="p-4 space-y-4">
+          {/* Tray info */}
+          {trayInfo && (
+            <div className="p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
+              <p className="text-xs text-bambu-gray mb-1">{t('inventory.selectSpool')}:</p>
+              <div className="flex items-center gap-2">
+                {trayInfo.color && (
+                  <span
+                    className="w-4 h-4 rounded-full border border-white/20"
+                    style={{ backgroundColor: `#${trayInfo.color}` }}
+                  />
+                )}
+                <span className="text-white font-medium">{trayInfo.type || t('ams.emptySlot')}</span>
+                <span className="text-bambu-gray">({trayInfo.location})</span>
+              </div>
+            </div>
+          )}
+
+          {/* Search filter */}
+          <div className="relative">
+            <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray" />
+            <input
+              type="text"
+              value={searchFilter}
+              onChange={(e) => setSearchFilter(e.target.value)}
+              placeholder={t('inventory.searchSpools')}
+              className="w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray focus:outline-none focus:border-bambu-green"
+            />
+          </div>
+
+          {/* Spool list */}
+          <div>
+            {isLoading ? (
+              <div className="flex justify-center py-8">
+                <Loader2 className="w-6 h-6 text-bambu-green animate-spin" />
+              </div>
+            ) : filteredSpools && filteredSpools.length > 0 ? (
+              <div className="max-h-64 overflow-y-auto space-y-2">
+                {filteredSpools.map((spool: InventorySpool) => (
+                  <button
+                    key={spool.id}
+                    onClick={() => setSelectedSpoolId(spool.id)}
+                    className={`w-full p-3 rounded-lg border text-left transition-colors ${
+                      selectedSpoolId === spool.id
+                        ? 'bg-bambu-green/20 border-bambu-green'
+                        : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
+                    }`}
+                  >
+                    <div className="flex items-center gap-2">
+                      {spool.rgba && (
+                        <span
+                          className="w-4 h-4 rounded-full border border-white/20 flex-shrink-0"
+                          style={{ backgroundColor: `#${spool.rgba.substring(0, 6)}` }}
+                        />
+                      )}
+                      <div className="flex-1 min-w-0">
+                        <p className="text-white font-medium truncate">
+                          {spool.brand ? `${spool.brand} ` : ''}{spool.material}{spool.subtype ? ` ${spool.subtype}` : ''}
+                        </p>
+                        <p className="text-xs text-bambu-gray">
+                          {spool.color_name || ''}
+                          {spool.label_weight ? ` - ${spool.label_weight}g` : ''}
+                          {spool.weight_used > 0 ? ` (${Math.round(spool.weight_used)}g used)` : ''}
+                        </p>
+                      </div>
+                      {selectedSpoolId === spool.id && (
+                        <Check className="w-4 h-4 text-bambu-green flex-shrink-0" />
+                      )}
+                    </div>
+                  </button>
+                ))}
+              </div>
+            ) : manualSpools && manualSpools.length === 0 ? (
+              <div className="text-center py-8 text-bambu-gray">
+                <p>{t('inventory.noManualSpools')}</p>
+              </div>
+            ) : (
+              <div className="text-center py-8 text-bambu-gray">
+                <p>{t('inventory.noSpoolsMatch')}</p>
+              </div>
+            )}
+          </div>
+        </div>
+
+        {/* Footer */}
+        <div className="flex justify-end gap-2 p-4 border-t border-bambu-dark-tertiary">
+          <Button variant="secondary" onClick={onClose}>
+            {t('common.cancel')}
+          </Button>
+          <Button
+            onClick={handleAssign}
+            disabled={!selectedSpoolId || assignMutation.isPending}
+          >
+            {assignMutation.isPending ? (
+              <>
+                <Loader2 className="w-4 h-4 animate-spin" />
+                {t('inventory.assigning')}
+              </>
+            ) : (
+              <>
+                <Package className="w-4 h-4" />
+                {t('inventory.assignSpool')}
+              </>
+            )}
+          </Button>
+        </div>
+
+        {assignMutation.isError && (
+          <div className="mx-4 mb-4 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400">
+            {(assignMutation.error as Error).message}
+          </div>
+        )}
+      </div>
+    </div>
+  );
+}

+ 583 - 0
frontend/src/components/ColorCatalogSettings.tsx

@@ -0,0 +1,583 @@
+import { useState, useEffect, useCallback, useRef } from 'react';
+import { useTranslation } from 'react-i18next';
+import { Palette, Plus, Trash2, RotateCcw, Loader2, Pencil, Check, X, Search, Download, Upload, Cloud } from 'lucide-react';
+import { api, getAuthToken } from '../api/client';
+import type { ColorCatalogEntry } from '../api/client';
+import { useToast } from '../contexts/ToastContext';
+import { Card, CardHeader, CardContent } from './Card';
+import { ConfirmModal } from './ConfirmModal';
+
+export function ColorCatalogSettings() {
+  const { t } = useTranslation();
+  const { showToast } = useToast();
+  const [catalog, setCatalog] = useState<ColorCatalogEntry[]>([]);
+  const [loading, setLoading] = useState(true);
+  const [search, setSearch] = useState('');
+  const [filterManufacturer, setFilterManufacturer] = useState<string>('Bambu Lab');
+  const fileInputRef = useRef<HTMLInputElement>(null);
+
+  // Add/Edit form state
+  const [showAddForm, setShowAddForm] = useState(false);
+  const [editingId, setEditingId] = useState<number | null>(null);
+  const [formManufacturer, setFormManufacturer] = useState('');
+  const [formColorName, setFormColorName] = useState('');
+  const [formHexColor, setFormHexColor] = useState('#FFFFFF');
+  const [formMaterial, setFormMaterial] = useState('');
+  const [saving, setSaving] = useState(false);
+
+  // Sync state
+  const [syncing, setSyncing] = useState(false);
+  const [syncProgress, setSyncProgress] = useState<{ fetched: number; total: number } | null>(null);
+
+  // Confirmation modals
+  const [deleteEntry, setDeleteEntry] = useState<ColorCatalogEntry | null>(null);
+  const [showResetConfirm, setShowResetConfirm] = useState(false);
+
+  const loadCatalog = useCallback(async () => {
+    try {
+      const entries = await api.getColorCatalog();
+      setCatalog(entries);
+    } catch {
+      showToast(t('settings.colorCatalog.loadFailed'), 'error');
+    } finally {
+      setLoading(false);
+    }
+  }, [showToast, t]);
+
+  useEffect(() => {
+    loadCatalog();
+  }, [loadCatalog]);
+
+  const manufacturers = [...new Set(catalog.map(e => e.manufacturer))].sort();
+
+  const filteredCatalog = catalog.filter(entry => {
+    const matchesSearch = search === '' ||
+      entry.manufacturer.toLowerCase().includes(search.toLowerCase()) ||
+      entry.color_name.toLowerCase().includes(search.toLowerCase()) ||
+      (entry.material?.toLowerCase().includes(search.toLowerCase()) ?? false);
+    const matchesManufacturer = filterManufacturer === '' || entry.manufacturer === filterManufacturer;
+    return matchesSearch && matchesManufacturer;
+  });
+
+  const resetForm = () => {
+    setFormManufacturer('');
+    setFormColorName('');
+    setFormHexColor('#FFFFFF');
+    setFormMaterial('');
+  };
+
+  const handleAdd = async () => {
+    if (!formManufacturer.trim() || !formColorName.trim() || !formHexColor) {
+      showToast(t('settings.colorCatalog.fieldsRequired'), 'error');
+      return;
+    }
+    setSaving(true);
+    try {
+      const entry = await api.addColorEntry({
+        manufacturer: formManufacturer.trim(),
+        color_name: formColorName.trim(),
+        hex_color: formHexColor,
+        material: formMaterial.trim() || null,
+      });
+      setCatalog(prev => [...prev, entry].sort((a, b) =>
+        a.manufacturer.localeCompare(b.manufacturer) ||
+        (a.material || '').localeCompare(b.material || '') ||
+        a.color_name.localeCompare(b.color_name)
+      ));
+      setShowAddForm(false);
+      resetForm();
+      showToast(t('settings.colorCatalog.colorAdded'), 'success');
+    } catch {
+      showToast(t('settings.colorCatalog.addFailed'), 'error');
+    } finally {
+      setSaving(false);
+    }
+  };
+
+  const startEdit = (entry: ColorCatalogEntry) => {
+    setEditingId(entry.id);
+    setFormManufacturer(entry.manufacturer);
+    setFormColorName(entry.color_name);
+    setFormHexColor(entry.hex_color);
+    setFormMaterial(entry.material || '');
+  };
+
+  const cancelEdit = () => {
+    setEditingId(null);
+    resetForm();
+  };
+
+  const handleUpdate = async (id: number) => {
+    if (!formManufacturer.trim() || !formColorName.trim() || !formHexColor) {
+      showToast(t('settings.colorCatalog.fieldsRequired'), 'error');
+      return;
+    }
+    setSaving(true);
+    try {
+      const updated = await api.updateColorEntry(id, {
+        manufacturer: formManufacturer.trim(),
+        color_name: formColorName.trim(),
+        hex_color: formHexColor,
+        material: formMaterial.trim() || null,
+      });
+      setCatalog(prev =>
+        prev.map(e => e.id === id ? updated : e).sort((a, b) =>
+          a.manufacturer.localeCompare(b.manufacturer) ||
+          (a.material || '').localeCompare(b.material || '') ||
+          a.color_name.localeCompare(b.color_name)
+        )
+      );
+      setEditingId(null);
+      resetForm();
+      showToast(t('settings.colorCatalog.colorUpdated'), 'success');
+    } catch {
+      showToast(t('settings.colorCatalog.updateFailed'), 'error');
+    } finally {
+      setSaving(false);
+    }
+  };
+
+  const handleDelete = async () => {
+    if (!deleteEntry) return;
+    try {
+      await api.deleteColorEntry(deleteEntry.id);
+      setCatalog(prev => prev.filter(e => e.id !== deleteEntry.id));
+      showToast(t('settings.colorCatalog.colorDeleted'), 'success');
+    } catch {
+      showToast(t('settings.colorCatalog.deleteFailed'), 'error');
+    } finally {
+      setDeleteEntry(null);
+    }
+  };
+
+  const handleReset = async () => {
+    setShowResetConfirm(false);
+    setLoading(true);
+    try {
+      await api.resetColorCatalog();
+      await loadCatalog();
+      showToast(t('settings.colorCatalog.resetSuccess'), 'success');
+    } catch {
+      showToast(t('settings.colorCatalog.resetFailed'), 'error');
+      setLoading(false);
+    }
+  };
+
+  const handleSync = async () => {
+    setSyncing(true);
+    setSyncProgress(null);
+    try {
+      const headers: Record<string, string> = {};
+      const token = getAuthToken();
+      if (token) {
+        headers['Authorization'] = `Bearer ${token}`;
+      }
+      const response = await fetch('/api/v1/inventory/colors/sync', { method: 'POST', headers });
+      if (!response.ok) throw new Error('Failed to start sync');
+
+      const reader = response.body?.getReader();
+      if (!reader) throw new Error('No response body');
+
+      const decoder = new TextDecoder();
+      let buffer = '';
+
+      while (true) {
+        const { done, value } = await reader.read();
+        if (done) break;
+
+        buffer += decoder.decode(value, { stream: true });
+        const lines = buffer.split('\n');
+        buffer = lines.pop() || '';
+
+        for (const line of lines) {
+          if (line.startsWith('data: ')) {
+            try {
+              const data = JSON.parse(line.slice(6));
+              if (data.type === 'progress') {
+                setSyncProgress({ fetched: data.total_fetched, total: data.total_available });
+              } else if (data.type === 'complete') {
+                if (data.added === 0) {
+                  showToast(t('settings.colorCatalog.syncUpToDate', { count: data.total_fetched }), 'success');
+                } else {
+                  showToast(t('settings.colorCatalog.syncComplete', { added: data.added, skipped: data.skipped }), 'success');
+                }
+              } else if (data.type === 'error') {
+                showToast(`${t('settings.colorCatalog.syncError')}: ${data.error}`, 'error');
+              }
+            } catch {
+              // Ignore parse errors
+            }
+          }
+        }
+      }
+      await loadCatalog();
+    } catch {
+      showToast(t('settings.colorCatalog.syncFailed'), 'error');
+    } finally {
+      setSyncing(false);
+      setSyncProgress(null);
+    }
+  };
+
+  const handleExport = () => {
+    const exportData = catalog.map(({ manufacturer, color_name, hex_color, material }) => ({
+      manufacturer, color_name, hex_color, material,
+    }));
+    const blob = new Blob([JSON.stringify(exportData, null, 2)], { type: 'application/json' });
+    const url = URL.createObjectURL(blob);
+    const a = document.createElement('a');
+    a.href = url;
+    a.download = 'color-catalog.json';
+    document.body.appendChild(a);
+    a.click();
+    document.body.removeChild(a);
+    URL.revokeObjectURL(url);
+    showToast(t('settings.colorCatalog.exported', { count: catalog.length }), 'success');
+  };
+
+  const handleImport = async (e: React.ChangeEvent<HTMLInputElement>) => {
+    const file = e.target.files?.[0];
+    if (!file) return;
+    try {
+      const text = await file.text();
+      const data = JSON.parse(text) as Array<{
+        manufacturer: string; color_name: string; hex_color: string; material?: string | null;
+      }>;
+      if (!Array.isArray(data)) throw new Error('Invalid format');
+
+      let added = 0;
+      let skipped = 0;
+      for (const item of data) {
+        if (!item.manufacturer || !item.color_name || !item.hex_color) { skipped++; continue; }
+        const exists = catalog.some(c =>
+          c.manufacturer.toLowerCase() === item.manufacturer.toLowerCase() &&
+          c.color_name.toLowerCase() === item.color_name.toLowerCase() &&
+          (c.material || '').toLowerCase() === (item.material || '').toLowerCase()
+        );
+        if (exists) { skipped++; continue; }
+        try {
+          const entry = await api.addColorEntry({
+            manufacturer: item.manufacturer,
+            color_name: item.color_name,
+            hex_color: item.hex_color,
+            material: item.material || null,
+          });
+          setCatalog(prev => [...prev, entry].sort((a, b) =>
+            a.manufacturer.localeCompare(b.manufacturer) ||
+            (a.material || '').localeCompare(b.material || '') ||
+            a.color_name.localeCompare(b.color_name)
+          ));
+          added++;
+        } catch { skipped++; }
+      }
+      showToast(t('settings.colorCatalog.imported', { added, skipped }), 'success');
+    } catch {
+      showToast(t('settings.colorCatalog.importFailed'), 'error');
+    }
+    if (fileInputRef.current) fileInputRef.current.value = '';
+  };
+
+  return (
+    <Card>
+      <CardHeader>
+        <div className="flex items-center gap-2 mb-3">
+          <Palette className="w-5 h-5 text-bambu-gray" />
+          <h2 className="text-lg font-semibold text-white">{t('settings.colorCatalog.title')}</h2>
+          <span className="text-sm text-bambu-gray">({catalog.length})</span>
+        </div>
+        <div className="flex items-center gap-2 flex-wrap">
+          <button
+            onClick={handleExport}
+            className="px-3 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white transition-colors flex items-center gap-1.5"
+          >
+            <Download className="w-4 h-4" />
+            <span className="hidden sm:inline">{t('common.export')}</span>
+          </button>
+          <button
+            onClick={() => fileInputRef.current?.click()}
+            className="px-3 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white transition-colors flex items-center gap-1.5"
+          >
+            <Upload className="w-4 h-4" />
+            <span className="hidden sm:inline">{t('common.import')}</span>
+          </button>
+          <input ref={fileInputRef} type="file" accept=".json" className="hidden" onChange={handleImport} />
+          <button
+            onClick={handleSync}
+            disabled={syncing}
+            className="px-3 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white transition-colors flex items-center gap-1.5"
+            title={t('settings.colorCatalog.syncTooltip')}
+          >
+            {syncing ? <Loader2 className="w-4 h-4 animate-spin" /> : <Cloud className="w-4 h-4" />}
+            <span className="hidden sm:inline">
+              {syncing
+                ? syncProgress
+                  ? `${Math.min(syncProgress.fetched, syncProgress.total)} / ${syncProgress.total}`
+                  : t('settings.colorCatalog.starting')
+                : t('settings.colorCatalog.sync')}
+            </span>
+          </button>
+          <button
+            onClick={() => setShowResetConfirm(true)}
+            className="px-3 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white transition-colors flex items-center gap-1.5"
+          >
+            <RotateCcw className="w-4 h-4" />
+            <span className="hidden sm:inline">{t('common.reset')}</span>
+          </button>
+          <button
+            onClick={() => setShowAddForm(true)}
+            className="px-3 py-1.5 text-sm bg-bambu-green text-white rounded-lg hover:bg-bambu-green/80 transition-colors flex items-center gap-1.5"
+          >
+            <Plus className="w-4 h-4" />
+            <span className="hidden sm:inline">{t('common.add')}</span>
+          </button>
+        </div>
+      </CardHeader>
+      <CardContent className="space-y-4">
+        <p className="text-sm text-bambu-gray">
+          {t('settings.colorCatalog.description')}
+        </p>
+
+        {/* Search and filter */}
+        <div className="flex gap-2 flex-wrap">
+          <div className="relative flex-1 min-w-[200px]">
+            <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray" />
+            <input
+              type="text"
+              className="w-full pl-10 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"
+              placeholder={t('settings.colorCatalog.searchColors')}
+              value={search}
+              onChange={(e) => setSearch(e.target.value)}
+            />
+          </div>
+          <select
+            className="px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
+            value={filterManufacturer}
+            onChange={(e) => setFilterManufacturer(e.target.value)}
+          >
+            <option value="">{t('settings.colorCatalog.allManufacturers')}</option>
+            {manufacturers.map(m => (
+              <option key={m} value={m}>{m}</option>
+            ))}
+          </select>
+        </div>
+
+        {/* Add form */}
+        {showAddForm && (
+          <div className="p-4 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
+            <h3 className="text-sm font-medium text-white mb-3">{t('settings.colorCatalog.addNewColor')}</h3>
+            <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-2 items-end">
+              <input
+                type="text"
+                className="px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"
+                placeholder={t('settings.colorCatalog.manufacturer')}
+                value={formManufacturer}
+                onChange={(e) => setFormManufacturer(e.target.value)}
+              />
+              <input
+                type="text"
+                className="px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"
+                placeholder={t('settings.colorCatalog.colorName')}
+                value={formColorName}
+                onChange={(e) => setFormColorName(e.target.value)}
+              />
+              <div className="flex items-center gap-2">
+                <input
+                  type="color"
+                  className="w-10 h-10 rounded cursor-pointer border border-bambu-dark-tertiary"
+                  value={formHexColor}
+                  onChange={(e) => setFormHexColor(e.target.value)}
+                />
+                <input
+                  type="text"
+                  className="flex-1 px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"
+                  placeholder="#FFFFFF"
+                  value={formHexColor}
+                  onChange={(e) => setFormHexColor(e.target.value)}
+                />
+              </div>
+              <input
+                type="text"
+                className="px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"
+                placeholder={t('settings.colorCatalog.materialOptional')}
+                value={formMaterial}
+                onChange={(e) => setFormMaterial(e.target.value)}
+              />
+              <div className="flex gap-2">
+                <button
+                  onClick={handleAdd}
+                  disabled={saving}
+                  className="flex-1 px-3 py-2 bg-bambu-green text-white rounded-lg hover:bg-bambu-green/80 flex items-center justify-center gap-1"
+                >
+                  {saving ? <Loader2 className="w-4 h-4 animate-spin" /> : <Check className="w-4 h-4" />}
+                  {t('common.add')}
+                </button>
+                <button
+                  onClick={() => { setShowAddForm(false); resetForm(); }}
+                  className="p-2 rounded-lg text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"
+                >
+                  <X className="w-4 h-4" />
+                </button>
+              </div>
+            </div>
+          </div>
+        )}
+
+        {/* Filter info */}
+        {(search || filterManufacturer) && (
+          <div className="text-xs text-bambu-gray">
+            {t('settings.colorCatalog.showing', { filtered: filteredCatalog.length, total: catalog.length })}
+          </div>
+        )}
+
+        {/* Catalog list */}
+        {loading ? (
+          <div className="flex items-center justify-center py-8 text-bambu-gray">
+            <Loader2 className="w-5 h-5 animate-spin mr-2" />
+            {t('common.loading')}
+          </div>
+        ) : (
+          <div className="max-h-[400px] overflow-auto border border-bambu-dark-tertiary rounded-lg">
+            <table className="w-full text-sm">
+              <thead className="bg-bambu-dark sticky top-0">
+                <tr>
+                  <th className="px-3 py-2 text-left text-bambu-gray font-medium w-12"></th>
+                  <th className="px-3 py-2 text-left text-bambu-gray font-medium">{t('settings.colorCatalog.manufacturer')}</th>
+                  <th className="px-3 py-2 text-left text-bambu-gray font-medium">{t('inventory.material')}</th>
+                  <th className="px-3 py-2 text-left text-bambu-gray font-medium">{t('settings.colorCatalog.colorName')}</th>
+                  <th className="px-3 py-2 text-left text-bambu-gray font-medium w-24">{t('settings.colorCatalog.hex')}</th>
+                  <th className="px-3 py-2 w-16"></th>
+                </tr>
+              </thead>
+              <tbody>
+                {filteredCatalog.length === 0 ? (
+                  <tr>
+                    <td colSpan={6} className="px-3 py-8 text-center text-bambu-gray">
+                      {search || filterManufacturer ? t('settings.colorCatalog.noMatch') : t('settings.colorCatalog.empty')}
+                    </td>
+                  </tr>
+                ) : (
+                  filteredCatalog.map(entry => (
+                    <tr key={entry.id} className="border-t border-bambu-dark-tertiary hover:bg-bambu-dark">
+                      {editingId === entry.id ? (
+                        <>
+                          <td className="px-3 py-2">
+                            <input
+                              type="color"
+                              className="w-8 h-8 rounded cursor-pointer border border-bambu-dark-tertiary"
+                              value={formHexColor}
+                              onChange={(e) => setFormHexColor(e.target.value)}
+                            />
+                          </td>
+                          <td className="px-3 py-2">
+                            <input
+                              type="text"
+                              className="w-full px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"
+                              value={formManufacturer}
+                              onChange={(e) => setFormManufacturer(e.target.value)}
+                            />
+                          </td>
+                          <td className="px-3 py-2">
+                            <input
+                              type="text"
+                              className="w-full px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"
+                              value={formMaterial}
+                              onChange={(e) => setFormMaterial(e.target.value)}
+                            />
+                          </td>
+                          <td className="px-3 py-2">
+                            <input
+                              type="text"
+                              className="w-full px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"
+                              value={formColorName}
+                              onChange={(e) => setFormColorName(e.target.value)}
+                            />
+                          </td>
+                          <td className="px-3 py-2">
+                            <input
+                              type="text"
+                              className="w-full px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"
+                              value={formHexColor}
+                              onChange={(e) => setFormHexColor(e.target.value)}
+                            />
+                          </td>
+                          <td className="px-3 py-2">
+                            <div className="flex justify-end gap-1">
+                              <button
+                                onClick={() => handleUpdate(entry.id)}
+                                disabled={saving}
+                                className="p-1.5 rounded hover:bg-green-500/20 text-green-500"
+                              >
+                                {saving ? <Loader2 className="w-4 h-4 animate-spin" /> : <Check className="w-4 h-4" />}
+                              </button>
+                              <button onClick={cancelEdit} className="p-1.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray">
+                                <X className="w-4 h-4" />
+                              </button>
+                            </div>
+                          </td>
+                        </>
+                      ) : (
+                        <>
+                          <td className="px-3 py-2">
+                            <div
+                              className="w-8 h-8 rounded border border-bambu-dark-tertiary"
+                              style={{ backgroundColor: entry.hex_color }}
+                              title={entry.hex_color}
+                            />
+                          </td>
+                          <td className="px-3 py-2 text-white">{entry.manufacturer}</td>
+                          <td className="px-3 py-2 text-bambu-gray">{entry.material || '-'}</td>
+                          <td className="px-3 py-2 text-white">{entry.color_name}</td>
+                          <td className="px-3 py-2 font-mono text-xs text-bambu-gray">{entry.hex_color}</td>
+                          <td className="px-3 py-2">
+                            <div className="flex justify-end gap-1">
+                              <button
+                                onClick={() => startEdit(entry)}
+                                className="p-1.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white"
+                              >
+                                <Pencil className="w-4 h-4" />
+                              </button>
+                              <button
+                                onClick={() => setDeleteEntry(entry)}
+                                className="p-1.5 rounded bg-red-500/10 hover:bg-red-500/20 text-red-500"
+                              >
+                                <Trash2 className="w-4 h-4" />
+                              </button>
+                            </div>
+                          </td>
+                        </>
+                      )}
+                    </tr>
+                  ))
+                )}
+              </tbody>
+            </table>
+          </div>
+        )}
+      </CardContent>
+
+      {/* Delete confirmation */}
+      {deleteEntry && (
+        <ConfirmModal
+          title={t('settings.colorCatalog.deleteColor')}
+          message={t('settings.colorCatalog.deleteConfirm', { name: `${deleteEntry.manufacturer} - ${deleteEntry.color_name}` })}
+          confirmText={t('common.delete')}
+          variant="danger"
+          onConfirm={handleDelete}
+          onCancel={() => setDeleteEntry(null)}
+        />
+      )}
+
+      {/* Reset confirmation */}
+      {showResetConfirm && (
+        <ConfirmModal
+          title={t('settings.colorCatalog.resetCatalog')}
+          message={t('settings.colorCatalog.resetConfirm')}
+          confirmText={t('common.reset')}
+          variant="danger"
+          onConfirm={handleReset}
+          onCancel={() => setShowResetConfirm(false)}
+        />
+      )}
+    </Card>
+  );
+}

+ 187 - 0
frontend/src/components/ColumnConfigModal.tsx

@@ -0,0 +1,187 @@
+import { useState, useEffect, useRef } from 'react';
+import { useTranslation } from 'react-i18next';
+import { GripVertical, Eye, EyeOff, ChevronUp, ChevronDown, RotateCcw } from 'lucide-react';
+import { Card, CardContent } from './Card';
+import { Button } from './Button';
+
+export interface ColumnConfig {
+  id: string;
+  label: string;
+  visible: boolean;
+}
+
+interface ColumnConfigModalProps {
+  isOpen: boolean;
+  onClose: () => void;
+  columns: ColumnConfig[];
+  defaultColumns: ColumnConfig[];
+  onSave: (columns: ColumnConfig[]) => void;
+}
+
+export function ColumnConfigModal({ isOpen, onClose, columns, defaultColumns, onSave }: ColumnConfigModalProps) {
+  const { t } = useTranslation();
+  const [localColumns, setLocalColumns] = useState<ColumnConfig[]>(columns);
+  const [draggedIndex, setDraggedIndex] = useState<number | null>(null);
+  const draggedIndexRef = useRef<number | null>(null);
+
+  useEffect(() => {
+    if (isOpen) {
+      setLocalColumns(columns.map((c) => ({ ...c })));
+    }
+  }, [isOpen, columns]);
+
+  useEffect(() => {
+    if (!isOpen) return;
+    const handleKeyDown = (e: KeyboardEvent) => {
+      if (e.key === 'Escape') onClose();
+    };
+    window.addEventListener('keydown', handleKeyDown);
+    return () => window.removeEventListener('keydown', handleKeyDown);
+  }, [isOpen, onClose]);
+
+  if (!isOpen) return null;
+
+  const toggleVisibility = (index: number) => {
+    setLocalColumns((prev) =>
+      prev.map((col, i) => (i === index ? { ...col, visible: !col.visible } : col))
+    );
+  };
+
+  const moveColumn = (fromIndex: number, toIndex: number) => {
+    if (toIndex < 0 || toIndex >= localColumns.length) return;
+    setLocalColumns((prev) => {
+      const newColumns = [...prev];
+      const [moved] = newColumns.splice(fromIndex, 1);
+      newColumns.splice(toIndex, 0, moved);
+      return newColumns;
+    });
+  };
+
+  const handleDragStart = (e: React.DragEvent, index: number) => {
+    draggedIndexRef.current = index;
+    setDraggedIndex(index);
+    e.dataTransfer.effectAllowed = 'move';
+  };
+
+  const handleDragOver = (e: React.DragEvent, index: number) => {
+    e.preventDefault();
+    e.dataTransfer.dropEffect = 'move';
+    const from = draggedIndexRef.current;
+    if (from !== null && from !== index) {
+      moveColumn(from, index);
+      draggedIndexRef.current = index;
+      setDraggedIndex(index);
+    }
+  };
+
+  const handleDrop = (e: React.DragEvent) => {
+    e.preventDefault();
+  };
+
+  const handleDragEnd = () => {
+    draggedIndexRef.current = null;
+    setDraggedIndex(null);
+  };
+
+  const resetToDefaults = () => {
+    setLocalColumns(defaultColumns.map((c) => ({ ...c })));
+  };
+
+  const handleSave = () => {
+    onSave(localColumns);
+    onClose();
+  };
+
+  const visibleCount = localColumns.filter((c) => c.visible).length;
+
+  return (
+    <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4" onClick={onClose}>
+      <Card className="w-full max-w-md max-h-[80vh] flex flex-col" onClick={(e: React.MouseEvent) => e.stopPropagation()}>
+        <CardContent className="p-6 flex flex-col min-h-0">
+          {/* Header */}
+          <h3 className="text-lg font-semibold text-white mb-2">{t('inventory.configureColumns')}</h3>
+          <p className="text-sm text-bambu-gray mb-4">
+            {t('inventory.configureColumnsDesc')}
+            <span className="ml-2 text-bambu-gray/60">
+              ({visibleCount} {t('inventory.of')} {localColumns.length} {t('inventory.visible')})
+            </span>
+          </p>
+
+          {/* Column list */}
+          <div className="space-y-1 overflow-y-auto flex-1 min-h-0 pr-1">
+            {localColumns.map((column, index) => (
+              <div
+                key={column.id}
+                className={`flex items-center gap-2 p-2 rounded-lg border transition-colors ${
+                  draggedIndex === index
+                    ? 'border-bambu-green bg-bambu-green/10'
+                    : 'border-bambu-dark-tertiary bg-bambu-dark-tertiary/50'
+                } ${!column.visible ? 'opacity-50' : ''}`}
+                draggable
+                onDragStart={(e) => handleDragStart(e, index)}
+                onDragOver={(e) => handleDragOver(e, index)}
+                onDrop={handleDrop}
+                onDragEnd={handleDragEnd}
+              >
+                {/* Drag Handle */}
+                <div className="cursor-grab text-bambu-gray/50 hover:text-bambu-gray">
+                  <GripVertical className="w-4 h-4" />
+                </div>
+
+                {/* Column Name */}
+                <span className="flex-1 font-medium text-sm text-white">{column.label}</span>
+
+                {/* Move Buttons */}
+                <div className="flex items-center gap-0.5">
+                  <button
+                    onClick={() => moveColumn(index, index - 1)}
+                    disabled={index === 0}
+                    className="p-1 rounded text-bambu-gray hover:bg-bambu-dark-secondary disabled:opacity-30 disabled:cursor-not-allowed"
+                    title={t('inventory.moveUp')}
+                  >
+                    <ChevronUp className="w-4 h-4" />
+                  </button>
+                  <button
+                    onClick={() => moveColumn(index, index + 1)}
+                    disabled={index === localColumns.length - 1}
+                    className="p-1 rounded text-bambu-gray hover:bg-bambu-dark-secondary disabled:opacity-30 disabled:cursor-not-allowed"
+                    title={t('inventory.moveDown')}
+                  >
+                    <ChevronDown className="w-4 h-4" />
+                  </button>
+                </div>
+
+                {/* Visibility Toggle */}
+                <button
+                  onClick={() => toggleVisibility(index)}
+                  className={`p-1.5 rounded transition-colors ${
+                    column.visible
+                      ? 'text-bambu-green hover:bg-bambu-green/10'
+                      : 'text-bambu-gray/50 hover:bg-bambu-dark-secondary'
+                  }`}
+                  title={column.visible ? t('inventory.hideColumn') : t('inventory.showColumn')}
+                >
+                  {column.visible ? <Eye className="w-4 h-4" /> : <EyeOff className="w-4 h-4" />}
+                </button>
+              </div>
+            ))}
+          </div>
+
+          {/* Footer */}
+          <div className="flex items-center gap-3 mt-4 pt-4 border-t border-bambu-dark-tertiary">
+            <Button variant="secondary" onClick={resetToDefaults} className="mr-auto">
+              <RotateCcw className="w-4 h-4" />
+              {t('inventory.reset')}
+            </Button>
+            <Button variant="secondary" onClick={onClose}>
+              {t('inventory.cancel')}
+            </Button>
+            <Button onClick={handleSave}>
+              {t('inventory.applyChanges')}
+            </Button>
+          </div>
+        </CardContent>
+      </Card>
+    </div>
+  );
+}

+ 251 - 62
frontend/src/components/ConfigureAmsSlotModal.tsx

@@ -14,6 +14,9 @@ interface SlotInfo {
   trayColor?: string;
   traySubBrands?: string;
   trayInfoIdx?: string;
+  extruderId?: number;
+  caliIdx?: number | null;
+  savedPresetId?: string;
 }
 
 // Get proper AMS label (handles HT AMS with ID 128+)
@@ -72,6 +75,7 @@ interface ConfigureAmsSlotModalProps {
   printerId: number;
   slotInfo: SlotInfo;
   nozzleDiameter?: string;
+  printerModel?: string;
   onSuccess?: () => void;
 }
 
@@ -209,12 +213,23 @@ function colorNameToHex(name: string): string | null {
   return COLOR_NAME_MAP[normalized] || null;
 }
 
+// Extract printer model from preset name suffix "@BBL X1C 0.4 nozzle" → "X1C"
+function extractPresetModel(name: string): string | null {
+  const atIdx = name.indexOf('@');
+  if (atIdx < 0) return null;
+  const suffix = name.slice(atIdx + 1).trim();
+  const bblMatch = suffix.match(/^BBL\s+(.+?)(?:\s+[\d.]+\s*nozzle)?$/i);
+  if (bblMatch) return bblMatch[1].trim();
+  return null;
+}
+
 export function ConfigureAmsSlotModal({
   isOpen,
   onClose,
   printerId,
   slotInfo,
   nozzleDiameter = '0.4',
+  printerModel,
   onSuccess,
 }: ConfigureAmsSlotModalProps) {
   const { t } = useTranslation();
@@ -226,11 +241,12 @@ export function ConfigureAmsSlotModal({
   const [showSuccess, setShowSuccess] = useState(false);
   const [showExtendedColors, setShowExtendedColors] = useState(false);
 
-  // Fetch cloud settings
-  const { data: cloudSettings, isLoading: settingsLoading } = useQuery({
+  // Fetch cloud settings (gracefully handle 401 when logged out)
+  const { data: cloudSettings, isLoading: settingsLoading, isError: cloudError } = useQuery({
     queryKey: ['cloudSettings'],
     queryFn: () => api.getCloudSettings(),
     enabled: isOpen,
+    retry: false,
   });
 
   // Fetch local presets
@@ -240,6 +256,14 @@ export function ConfigureAmsSlotModal({
     enabled: isOpen,
   });
 
+  // Fetch built-in filament names (static fallback)
+  const { data: builtinFilaments, isLoading: builtinLoading } = useQuery({
+    queryKey: ['builtinFilaments'],
+    queryFn: () => api.getBuiltinFilaments(),
+    enabled: isOpen,
+    staleTime: Infinity,
+  });
+
   // Fetch K profiles
   const { data: kprofilesData, isLoading: kprofilesLoading } = useQuery({
     queryKey: ['kprofiles', printerId, nozzleDiameter],
@@ -247,28 +271,42 @@ export function ConfigureAmsSlotModal({
     enabled: isOpen && !!printerId,
   });
 
+  // Fetch color catalog
+  const { data: colorCatalog } = useQuery({
+    queryKey: ['colorCatalog'],
+    queryFn: () => api.getColorCatalog(),
+    enabled: isOpen,
+    staleTime: Infinity,
+  });
+
   // Configure slot mutation
   const configureMutation = useMutation({
     mutationFn: async () => {
       if (!selectedPresetId) throw new Error('No filament preset selected');
 
-      // Check if this is a local preset
+      // Determine preset source
       const isLocal = selectedPresetId.startsWith('local_');
+      const isBuiltin = selectedPresetId.startsWith('builtin_');
       const localId = isLocal ? parseInt(selectedPresetId.replace('local_', ''), 10) : null;
+      const builtinFilamentId = isBuiltin ? selectedPresetId.replace('builtin_', '') : null;
       const localPreset = isLocal
         ? localPresets?.filament.find(p => p.id === localId)
         : null;
+      const builtinPreset = isBuiltin
+        ? builtinFilaments?.find(b => b.filament_id === builtinFilamentId)
+        : null;
 
-      // Get the selected cloud preset details (null for local presets)
-      const selectedPreset = !isLocal
+      // Get the selected cloud preset details (null for local/builtin presets)
+      const selectedPreset = (!isLocal && !isBuiltin)
         ? cloudSettings?.filament.find(p => p.setting_id === selectedPresetId)
         : null;
 
-      if (!isLocal && !selectedPreset) throw new Error('Selected preset not found');
+      if (!isLocal && !isBuiltin && !selectedPreset) throw new Error('Selected preset not found');
       if (isLocal && !localPreset) throw new Error('Selected local preset not found');
+      if (isBuiltin && !builtinPreset) throw new Error('Selected builtin preset not found');
 
       // Parse the preset name for filament info
-      const presetName = isLocal ? localPreset!.name : selectedPreset!.name;
+      const presetName = isLocal ? localPreset!.name : isBuiltin ? builtinPreset!.name : selectedPreset!.name;
       const parsed = parsePresetName(presetName);
 
       // Get cali_idx from selected K profile's slot_id (-1 = use default 0.020)
@@ -287,6 +325,10 @@ export function ConfigureAmsSlotModal({
         // Local presets have no Bambu Cloud mapping
         trayInfoIdx = '';
         settingId = '';
+      } else if (isBuiltin) {
+        // Built-in presets use the filament_id directly as tray_info_idx
+        trayInfoIdx = builtinFilamentId!;
+        settingId = '';
       } else {
         // Get tray_info_idx: for user presets, fetch detail to get filament_id or derive from base_id
         trayInfoIdx = convertToTrayInfoIdx(selectedPresetId);
@@ -312,7 +354,7 @@ export function ConfigureAmsSlotModal({
       let tempMin = isLocal && localPreset?.nozzle_temp_min ? localPreset.nozzle_temp_min : 190;
       let tempMax = isLocal && localPreset?.nozzle_temp_max ? localPreset.nozzle_temp_max : 230;
 
-      if (!isLocal || (!localPreset?.nozzle_temp_min && !localPreset?.nozzle_temp_max)) {
+      if (!isLocal || isBuiltin || (!localPreset?.nozzle_temp_min && !localPreset?.nozzle_temp_max)) {
         // Fall back to material-based defaults
         const material = (isLocal ? (localPreset?.filament_type || parsed.material) : parsed.material).toUpperCase();
         if (material.includes('PLA')) {
@@ -368,8 +410,8 @@ export function ConfigureAmsSlotModal({
       // Save the preset mapping so we can display the correct name in the UI
       // This is needed because user presets use filament_id (e.g., P285e239) as tray_info_idx,
       // which can't be resolved to a name via the filamentInfo API
-      const mappingPresetId = isLocal ? `local_${localId}` : selectedPresetId;
-      const mappingSource = isLocal ? 'local' : 'cloud';
+      const mappingPresetId = isLocal ? `local_${localId}` : isBuiltin ? `builtin_${builtinFilamentId}` : selectedPresetId;
+      const mappingSource = isLocal ? 'local' : isBuiltin ? 'builtin' : 'cloud';
       try {
         await api.saveSlotPreset(printerId, slotInfo.amsId, slotInfo.trayId, mappingPresetId, traySubBrands, mappingSource);
       } catch (e) {
@@ -405,52 +447,98 @@ export function ConfigureAmsSlotModal({
     },
   });
 
-  // Unified preset item for the list (cloud + local)
-  type PresetItem = { id: string; name: string; source: 'cloud' | 'local'; isUser: boolean };
+  // Unified preset item for the list (cloud + local + builtin fallback)
+  type PresetItem = { id: string; name: string; source: 'cloud' | 'local' | 'builtin'; isUser: boolean };
 
-  // Filter filament presets based on search (merged cloud + local)
+  // Filter filament presets based on search (merged cloud + local + builtin)
   const filteredPresets = useMemo(() => {
     const query = searchQuery.toLowerCase();
     const items: PresetItem[] = [];
 
-    // Add local presets first
+    // Collect IDs already covered by cloud and local to avoid duplicates in fallback
+    const coveredIds = new Set<string>();
+
+    // Currently-configured preset should always be shown (bypass model filter)
+    const savedId = slotInfo.savedPresetId;
+    const trayIdx = slotInfo.trayInfoIdx;
+
+    // 1. Cloud presets
+    if (cloudSettings?.filament) {
+      for (const cp of cloudSettings.filament) {
+        coveredIds.add(cp.setting_id);
+        // Keep preset if it matches the slot's saved mapping or current tray_info_idx
+        const isCurrentPreset = savedId === cp.setting_id
+          || (trayIdx && (cp.setting_id === trayIdx || convertToTrayInfoIdx(cp.setting_id) === trayIdx));
+        if (!isCurrentPreset && query && !cp.name.toLowerCase().includes(query)) continue;
+        // Filter by printer model if set (skip for current preset)
+        if (!isCurrentPreset && printerModel) {
+          const presetModel = extractPresetModel(cp.name);
+          if (presetModel && presetModel.toUpperCase() !== printerModel.toUpperCase()) continue;
+        }
+        items.push({ id: cp.setting_id, name: cp.name, source: 'cloud', isUser: isUserPreset(cp.setting_id) });
+      }
+    }
+
+    // 2. Local presets
     if (localPresets?.filament) {
       for (const lp of localPresets.filament) {
-        if (!query || lp.name.toLowerCase().includes(query)) {
-          items.push({ id: `local_${lp.id}`, name: lp.name, source: 'local', isUser: false });
+        const localId = `local_${lp.id}`;
+        const isSaved = savedId === localId;
+        if (!isSaved && query && !lp.name.toLowerCase().includes(query)) continue;
+        // Filter by compatible_printers if set (skip for saved preset)
+        if (!isSaved && printerModel && lp.compatible_printers) {
+          const compatModels = lp.compatible_printers.split(';').map(p => {
+            // Extract model from "BBL X1C" → "X1C"
+            const trimmed = p.trim();
+            const bblMatch = trimmed.match(/^BBL\s+(.+)/i);
+            return bblMatch ? bblMatch[1].trim().toUpperCase() : trimmed.toUpperCase();
+          }).filter(Boolean);
+          if (compatModels.length > 0 && !compatModels.includes(printerModel.toUpperCase())) continue;
         }
+        items.push({ id: localId, name: lp.name, source: 'local', isUser: false });
       }
     }
 
-    // Add cloud presets
-    if (cloudSettings?.filament) {
-      for (const cp of cloudSettings.filament) {
-        if (!query || cp.name.toLowerCase().includes(query)) {
-          items.push({ id: cp.setting_id, name: cp.name, source: 'cloud', isUser: isUserPreset(cp.setting_id) });
+    // 3. Built-in filament names (fallback — only add entries not already covered)
+    if (builtinFilaments) {
+      for (const bf of builtinFilaments) {
+        if (coveredIds.has(bf.filament_id)) continue;
+        // Convert filament_id to setting_id format for cloud compatibility
+        // e.g. "GFA00" → cloud setting_id would be "GFSA00" (insert S after GF)
+        const settingId = bf.filament_id.startsWith('GF')
+          ? 'GFS' + bf.filament_id.slice(2)
+          : bf.filament_id;
+        if (coveredIds.has(settingId)) continue;
+        if (!query || bf.name.toLowerCase().includes(query)) {
+          items.push({ id: `builtin_${bf.filament_id}`, name: bf.name, source: 'builtin', isUser: false });
         }
       }
     }
 
-    // Sort: local first, then user cloud presets, then built-in, alphabetically within groups
+    // Sort: cloud user presets first, then cloud built-in, then local, then builtin fallback
     return items.sort((a, b) => {
-      if (a.source === 'local' && b.source !== 'local') return -1;
-      if (a.source !== 'local' && b.source === 'local') return 1;
+      const sourceOrder = { cloud: 0, local: 1, builtin: 2 };
+      if (a.source !== b.source) return sourceOrder[a.source] - sourceOrder[b.source];
       if (a.isUser && !b.isUser) return -1;
       if (!a.isUser && b.isUser) return 1;
       return a.name.localeCompare(b.name);
     });
-  }, [cloudSettings?.filament, localPresets?.filament, searchQuery]);
+  }, [cloudSettings?.filament, localPresets?.filament, builtinFilaments, searchQuery, printerModel, slotInfo.savedPresetId, slotInfo.trayInfoIdx]);
 
   // Get full preset name for K profile filtering (brand + material, without printer suffix)
   const selectedPresetInfo = useMemo(() => {
     if (!selectedPresetId) return null;
 
-    // Resolve the name from either local or cloud presets
+    // Resolve the name from cloud, local, or builtin presets
     let presetName: string | null = null;
     if (selectedPresetId.startsWith('local_')) {
       const localId = parseInt(selectedPresetId.replace('local_', ''), 10);
       const lp = localPresets?.filament.find(p => p.id === localId);
       presetName = lp?.name || null;
+    } else if (selectedPresetId.startsWith('builtin_')) {
+      const filamentId = selectedPresetId.replace('builtin_', '');
+      const bf = builtinFilaments?.find(b => b.filament_id === filamentId);
+      presetName = bf?.name || null;
     } else if (cloudSettings?.filament) {
       const cp = cloudSettings.filament.find(p => p.setting_id === selectedPresetId);
       presetName = cp?.name || null;
@@ -470,11 +558,46 @@ export function ConfigureAmsSlotModal({
       material: parsed.material,
       brand: parsed.brand,
     };
-  }, [selectedPresetId, cloudSettings?.filament, localPresets?.filament]);
+  }, [selectedPresetId, cloudSettings?.filament, localPresets?.filament, builtinFilaments]);
 
   // For backwards compatibility with the label
   const selectedMaterial = selectedPresetInfo?.fullName || '';
 
+  // Filter color catalog entries matching the selected preset's brand + material
+  const catalogColors = useMemo(() => {
+    if (!colorCatalog || !selectedPresetInfo) return [];
+
+    const { fullName, brand } = selectedPresetInfo;
+
+    // Try to find colors matching the full preset name (e.g., "PLA Metal")
+    // The catalog uses the variant as part of the material field (e.g., material="PLA Metal")
+    // Extract the full material+variant from the preset name
+    const materialVariant = fullName.replace(/^(Bambu\s*(Lab)?|eSUN|Polymaker|Overture|Sunlu|Hatchbox)\s*/i, '').trim();
+
+    return colorCatalog.filter(entry => {
+      const entryMaterial = (entry.material || '').toUpperCase();
+      const entryManufacturer = entry.manufacturer.toUpperCase();
+
+      // Match material: try full material+variant first, then just material type
+      const materialMatch = entryMaterial === materialVariant.toUpperCase()
+        || entryMaterial.includes(materialVariant.toUpperCase())
+        || materialVariant.toUpperCase().includes(entryMaterial);
+
+      if (!materialMatch) return false;
+
+      // If brand is present, also match manufacturer
+      if (brand) {
+        const upperBrand = brand.toUpperCase();
+        // Fuzzy match: "Bambu" matches "Bambu Lab", etc.
+        if (!entryManufacturer.includes(upperBrand) && !upperBrand.includes(entryManufacturer)) {
+          return false;
+        }
+      }
+
+      return true;
+    });
+  }, [colorCatalog, selectedPresetInfo]);
+
   const matchingKProfiles = useMemo(() => {
     if (!kprofilesData?.profiles || !selectedPresetInfo) return [];
 
@@ -532,34 +655,52 @@ export function ConfigureAmsSlotModal({
     });
 
     // Deduplicate profiles with same name and k_value (multi-nozzle printers have duplicates)
-    // Prefer extruder_id=1 (High Flow) profiles as they're more commonly used on H2D
+    // Prefer the profile matching the slot's extruder (e.g. ext-R uses extruder 0, ext-L uses extruder 1)
     const seen = new Map<string, KProfile>();
     for (const profile of filtered) {
       const key = `${profile.name}|${profile.k_value}`;
       const existing = seen.get(key);
       if (!existing) {
         seen.set(key, profile);
-      } else if (profile.extruder_id === 1 && existing.extruder_id === 0) {
-        // Replace extruder_id=0 profile with extruder_id=1 (High Flow) profile
+      } else if (slotInfo.extruderId !== undefined && profile.extruder_id === slotInfo.extruderId && existing.extruder_id !== slotInfo.extruderId) {
+        // Replace with profile matching slot's extruder
         seen.set(key, profile);
       }
     }
     return Array.from(seen.values());
-  }, [kprofilesData?.profiles, selectedPresetInfo]);
+  }, [kprofilesData?.profiles, selectedPresetInfo, slotInfo.extruderId]);
 
   // Pre-select current profile when modal opens, reset when closes
   useEffect(() => {
-    if (isOpen && cloudSettings?.filament) {
-      // Try to pre-select current profile based on trayInfoIdx
-      if (slotInfo.trayInfoIdx) {
-        const currentPreset = cloudSettings.filament.find(
+    if (isOpen) {
+      // Pre-populate from saved preset mapping (most reliable)
+      if (slotInfo.savedPresetId) {
+        setSelectedPresetId(slotInfo.savedPresetId);
+      } else if (slotInfo.trayInfoIdx && cloudSettings?.filament) {
+        // Fallback: try to match by tray_info_idx in cloud presets
+        // First try exact match on setting_id
+        let currentPreset = cloudSettings.filament.find(
           p => p.setting_id === slotInfo.trayInfoIdx
         );
+        // Then try matching by converting setting_id → filament_id format
+        if (!currentPreset) {
+          currentPreset = cloudSettings.filament.find(
+            p => convertToTrayInfoIdx(p.setting_id) === slotInfo.trayInfoIdx
+          );
+        }
         if (currentPreset) {
           setSelectedPresetId(currentPreset.setting_id);
         }
       }
-    } else if (!isOpen) {
+
+      // Pre-populate color from current slot
+      if (slotInfo.trayColor) {
+        const hex = slotInfo.trayColor.slice(0, 6);
+        if (hex && hex !== '000000') {
+          setColorHex(hex);
+        }
+      }
+    } else {
       // Reset when modal closes
       setSelectedPresetId('');
       setSelectedKProfile(null);
@@ -568,17 +709,25 @@ export function ConfigureAmsSlotModal({
       setSearchQuery('');
       setShowSuccess(false);
     }
-  }, [isOpen, cloudSettings?.filament, slotInfo.trayInfoIdx]);
+  }, [isOpen, slotInfo.savedPresetId, slotInfo.trayInfoIdx, slotInfo.trayColor, cloudSettings?.filament]);
 
   // Auto-select best matching K profile when preset changes
   useEffect(() => {
     if (matchingKProfiles.length > 0) {
-      // Auto-select first matching profile
+      // Prefer the currently-active K-profile (by cali_idx) if available
+      if (slotInfo.caliIdx != null && slotInfo.caliIdx > 0) {
+        const active = matchingKProfiles.find(p => p.slot_id === slotInfo.caliIdx);
+        if (active) {
+          setSelectedKProfile(active);
+          return;
+        }
+      }
+      // Fallback: first matching profile
       setSelectedKProfile(matchingKProfiles[0]);
     } else {
       setSelectedKProfile(null);
     }
-  }, [selectedPresetId, matchingKProfiles]);
+  }, [selectedPresetId, matchingKProfiles, slotInfo.caliIdx]);
 
   // Escape key handler
   const handleKeyDown = useCallback((e: KeyboardEvent) => {
@@ -596,7 +745,7 @@ export function ConfigureAmsSlotModal({
 
   if (!isOpen) return null;
 
-  const isLoading = settingsLoading || localLoading || kprofilesLoading;
+  const isLoading = (settingsLoading && !cloudError) || localLoading || builtinLoading || kprofilesLoading;
   const canSave = selectedPresetId && !configureMutation.isPending;
 
   // Get display color (custom or slot default)
@@ -616,7 +765,7 @@ export function ConfigureAmsSlotModal({
         <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
           <div className="flex items-center gap-2">
             <Settings2 className="w-5 h-5 text-bambu-blue" />
-            <h2 className="text-lg font-semibold text-white">Configure AMS Slot</h2>
+            <h2 className="text-lg font-semibold text-white">{t('configureAmsSlot.title')}</h2>
           </div>
           <button
             onClick={onClose}
@@ -633,7 +782,7 @@ export function ConfigureAmsSlotModal({
             <div className="absolute inset-0 bg-bambu-dark-secondary/95 z-10 flex items-center justify-center rounded-xl">
               <div className="text-center space-y-3">
                 <CheckCircle2 className="w-16 h-16 text-bambu-green mx-auto" />
-                <p className="text-lg font-semibold text-white">Slot Configured!</p>
+                <p className="text-lg font-semibold text-white">{t('configureAmsSlot.slotConfigured')}</p>
                 <p className="text-sm text-bambu-gray">{t('configureAmsSlot.settingsSentToPrinter')}</p>
               </div>
             </div>
@@ -641,7 +790,7 @@ export function ConfigureAmsSlotModal({
 
           {/* Slot info */}
           <div className="p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
-            <p className="text-xs text-bambu-gray mb-1">Configuring slot:</p>
+            <p className="text-xs text-bambu-gray mb-1">{t('configureAmsSlot.configuringSlot')}</p>
             <div className="flex items-center gap-2">
               {slotInfo.trayColor && (
                 <span
@@ -650,7 +799,7 @@ export function ConfigureAmsSlotModal({
                 />
               )}
               <span className="text-white font-medium">
-                {getAmsLabel(slotInfo.amsId, slotInfo.trayCount)} Slot {slotInfo.trayId + 1}
+                {t('configureAmsSlot.slotLabel', { ams: getAmsLabel(slotInfo.amsId, slotInfo.trayCount), slot: slotInfo.trayId + 1 })}
               </span>
               {slotInfo.traySubBrands && (
                 <span className="text-bambu-gray">({slotInfo.traySubBrands})</span>
@@ -667,7 +816,7 @@ export function ConfigureAmsSlotModal({
               {/* Filament Profile Select */}
               <div>
                 <label className="block text-sm text-bambu-gray mb-2">
-                  Filament Profile <span className="text-red-400">*</span>
+                  {t('configureAmsSlot.filamentProfile')} <span className="text-red-400">*</span>
                 </label>
                 <div className="relative">
                   <input
@@ -681,13 +830,16 @@ export function ConfigureAmsSlotModal({
                     {filteredPresets.length === 0 ? (
                       <p className="text-center py-4 text-bambu-gray">
                         {(cloudSettings?.filament?.length === 0 && !localPresets?.filament?.length)
-                          ? 'No presets available. Login to Bambu Cloud or import local profiles.'
-                          : 'No matching presets found.'}
+                          ? t('configureAmsSlot.noPresetsAvailable')
+                          : t('configureAmsSlot.noMatchingPresets')}
                       </p>
                     ) : (
                       filteredPresets.map((preset) => (
                         <button
                           key={preset.id}
+                          ref={selectedPresetId === preset.id ? (el) => {
+                            el?.scrollIntoView({ block: 'nearest' });
+                          } : undefined}
                           onClick={() => setSelectedPresetId(preset.id)}
                           className={`w-full p-2 rounded-lg border text-left transition-colors ${
                             selectedPresetId === preset.id
@@ -703,6 +855,11 @@ export function ConfigureAmsSlotModal({
                                   {t('profiles.localProfiles.badge')}
                                 </span>
                               )}
+                              {preset.source === 'builtin' && (
+                                <span className="text-xs px-1.5 py-0.5 rounded bg-amber-500/20 text-amber-400">
+                                  {t('configureAmsSlot.builtin')}
+                                </span>
+                              )}
                               {preset.isUser && (
                                 <span className="text-xs px-1.5 py-0.5 rounded bg-bambu-blue/20 text-bambu-blue">
                                   {t('configureAmsSlot.custom')}
@@ -720,10 +877,10 @@ export function ConfigureAmsSlotModal({
               {/* K Profile Select */}
               <div>
                 <label className="block text-sm text-bambu-gray mb-2">
-                  K Profile (Pressure Advance)
+                  {t('configureAmsSlot.kProfileLabel')}
                   {selectedMaterial && (
                     <span className="ml-2 text-xs text-bambu-blue">
-                      Filtering for: {selectedMaterial}
+                      {t('configureAmsSlot.filteringFor', { material: selectedMaterial })}
                     </span>
                   )}
                 </label>
@@ -737,7 +894,7 @@ export function ConfigureAmsSlotModal({
                       }}
                       className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none appearance-none pr-10"
                     >
-                      <option value="">No K profile (use default 0.020)</option>
+                      <option value="">{t('configureAmsSlot.noKProfile')}</option>
                       {matchingKProfiles.map((profile) => (
                         <option key={`${profile.name}-${profile.extruder_id}`} value={profile.name}>
                           {profile.name} (K={profile.k_value})
@@ -748,16 +905,16 @@ export function ConfigureAmsSlotModal({
                   </div>
                 ) : selectedPresetId ? (
                   <p className="text-sm text-bambu-gray italic py-2">
-                    No matching K profiles found. Default K=0.020 will be used.
+                    {t('configureAmsSlot.noMatchingKProfiles')}
                   </p>
                 ) : (
                   <span className="inline-block text-xs px-2 py-1 rounded bg-amber-500/20 text-amber-400 border border-amber-500/30">
-                    Select a filament profile first
+                    {t('configureAmsSlot.selectFilamentFirst')}
                   </span>
                 )}
                 {selectedKProfile && (
                   <p className="text-xs text-bambu-green mt-1">
-                    K={selectedKProfile.k_value} from printer calibration
+                    {t('configureAmsSlot.kFromCalibration', { value: selectedKProfile.k_value })}
                   </p>
                 )}
               </div>
@@ -765,8 +922,40 @@ export function ConfigureAmsSlotModal({
               {/* Optional: Custom color */}
               <div>
                 <label className="block text-sm text-bambu-gray mb-2">
-                  Custom Color (optional)
+                  {t('configureAmsSlot.customColorLabel')}
                 </label>
+                {/* Catalog colors matching selected preset */}
+                {catalogColors.length > 0 && (
+                  <div className="mb-3">
+                    <p className="text-xs text-bambu-gray mb-1.5">
+                      {t('configureAmsSlot.presetColors', { name: selectedPresetInfo?.fullName })}
+                    </p>
+                    <div className="flex flex-wrap gap-1.5">
+                      {catalogColors.map((entry) => (
+                        <button
+                          key={entry.id}
+                          onClick={() => {
+                            const hex = entry.hex_color.replace('#', '').toUpperCase();
+                            setColorHex(hex);
+                            setColorInput(entry.color_name);
+                          }}
+                          className={`h-7 px-2 rounded-md border-2 transition-all flex items-center gap-1.5 ${
+                            colorHex === entry.hex_color.replace('#', '').toUpperCase()
+                              ? 'border-bambu-green scale-105'
+                              : 'border-white/20 hover:border-white/40'
+                          }`}
+                          title={entry.color_name}
+                        >
+                          <span
+                            className="w-4 h-4 rounded-full border border-white/30 flex-shrink-0"
+                            style={{ backgroundColor: entry.hex_color }}
+                          />
+                          <span className="text-xs text-white/80 whitespace-nowrap">{entry.color_name}</span>
+                        </button>
+                      ))}
+                    </div>
+                  </div>
+                )}
                 {/* Quick color buttons */}
                 <div className="flex flex-wrap gap-1.5 mb-2">
                   {QUICK_COLORS_BASIC.map((color) => (
@@ -788,7 +977,7 @@ export function ConfigureAmsSlotModal({
                   <button
                     onClick={() => setShowExtendedColors(!showExtendedColors)}
                     className="w-7 h-7 rounded-md border-2 border-white/20 hover:border-white/40 flex items-center justify-center text-white/60 hover:text-white/80 transition-all text-xs"
-                    title={showExtendedColors ? 'Show less colors' : 'Show more colors'}
+                    title={showExtendedColors ? t('configureAmsSlot.showLessColors') : t('configureAmsSlot.showMoreColors')}
                   >
                     {showExtendedColors ? '−' : '+'}
                   </button>
@@ -854,13 +1043,13 @@ export function ConfigureAmsSlotModal({
                       className="px-2 py-1 text-xs text-bambu-gray hover:text-white bg-bambu-dark-tertiary rounded"
                       title={t('configureAmsSlot.clearCustomColor')}
                     >
-                      Clear
+                      {t('configureAmsSlot.clear')}
                     </button>
                   )}
                 </div>
                 {colorHex && (
                   <p className="text-xs text-bambu-gray mt-1.5">
-                    Hex: #{colorHex}
+                    {t('configureAmsSlot.hexLabel', { hex: colorHex })}
                   </p>
                 )}
               </div>
@@ -880,19 +1069,19 @@ export function ConfigureAmsSlotModal({
             {resetMutation.isPending ? (
               <>
                 <Loader2 className="w-4 h-4 animate-spin" />
-                Resetting...
+                {t('configureAmsSlot.resetting')}
               </>
             ) : (
               <>
                 <RotateCcw className="w-4 h-4" />
-                Reset Slot
+                {t('configureAmsSlot.resetSlot')}
               </>
             )}
           </Button>
           {/* Cancel and Configure buttons on the right */}
           <div className="flex gap-2">
             <Button variant="secondary" onClick={onClose}>
-              Cancel
+              {t('configureAmsSlot.cancel')}
             </Button>
             <Button
               onClick={() => configureMutation.mutate()}
@@ -901,12 +1090,12 @@ export function ConfigureAmsSlotModal({
               {configureMutation.isPending ? (
                 <>
                   <Loader2 className="w-4 h-4 animate-spin" />
-                  Configuring...
+                  {t('configureAmsSlot.configuring')}
                 </>
               ) : (
                 <>
                   <Settings2 className="w-4 h-4" />
-                  Configure Slot
+                  {t('configureAmsSlot.configureSlot')}
                 </>
               )}
             </Button>

+ 14 - 2
frontend/src/components/ConfirmModal.tsx

@@ -9,6 +9,8 @@ interface ConfirmModalProps {
   message: string;
   confirmText?: string;
   cancelText?: string;
+  cancelVariant?: 'primary' | 'secondary' | 'danger' | 'ghost';
+  cardClassName?: string;
   variant?: 'danger' | 'warning' | 'default';
   isLoading?: boolean;
   loadingText?: string;
@@ -21,6 +23,8 @@ export function ConfirmModal({
   message,
   confirmText,
   cancelText,
+  cancelVariant,
+  cardClassName,
   variant = 'default',
   isLoading = false,
   loadingText,
@@ -62,7 +66,10 @@ export function ConfirmModal({
       className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"
       onClick={isLoading ? undefined : onCancel}
     >
-      <Card className="w-full max-w-md" onClick={(e: React.MouseEvent) => e.stopPropagation()}>
+      <Card
+        className={`w-full max-w-md ${cardClassName ?? ''}`}
+        onClick={(e: React.MouseEvent) => e.stopPropagation()}
+      >
         <CardContent className="p-6">
           <div className="flex items-start gap-4">
             <div className={`p-2 rounded-full bg-bambu-dark ${styles.icon}`}>
@@ -74,7 +81,12 @@ export function ConfirmModal({
             </div>
           </div>
           <div className="flex gap-3 mt-6">
-            <Button variant="secondary" onClick={onCancel} className="flex-1" disabled={isLoading}>
+            <Button
+              variant={cancelVariant ?? 'secondary'}
+              onClick={onCancel}
+              className="flex-1"
+              disabled={isLoading}
+            >
               {resolvedCancelText}
             </Button>
             <Button

+ 57 - 3
frontend/src/components/FilamentHoverCard.tsx

@@ -1,6 +1,6 @@
 import { useState, useRef, useEffect, type ReactNode } from 'react';
 import { useTranslation } from 'react-i18next';
-import { Droplets, Link2, Copy, Check, Settings2, ExternalLink } from 'lucide-react';
+import { Droplets, Link2, Copy, Check, Settings2, ExternalLink, Package, Unlink } from 'lucide-react';
 
 interface FilamentData {
   vendor: 'Bambu Lab' | 'Generic';
@@ -10,7 +10,7 @@ interface FilamentData {
   kFactor: string;
   fillLevel: number | null; // null = unknown
   trayUuid?: string | null; // Bambu Lab spool UUID for Spoolman linking
-  fillSource?: 'ams' | 'spoolman'; // Source of fill level data
+  fillSource?: 'ams' | 'spoolman' | 'inventory'; // Source of fill level data
 }
 
 interface SpoolmanConfig {
@@ -21,6 +21,12 @@ interface SpoolmanConfig {
   spoolmanUrl?: string | null; // Base URL for Spoolman (for "Open in Spoolman" link)
 }
 
+interface InventoryConfig {
+  onAssignSpool?: () => void;
+  onUnassignSpool?: () => void;
+  assignedSpool?: { id: number; material: string; brand: string | null; color_name: string | null } | null;
+}
+
 interface ConfigureSlotConfig {
   enabled: boolean;
   onConfigure?: () => void;
@@ -32,6 +38,7 @@ interface FilamentHoverCardProps {
   disabled?: boolean;
   className?: string;
   spoolman?: SpoolmanConfig;
+  inventory?: InventoryConfig;
   configureSlot?: ConfigureSlotConfig;
 }
 
@@ -39,7 +46,7 @@ interface FilamentHoverCardProps {
  * A hover card that displays filament details when hovering over AMS slots.
  * Replaces the basic browser tooltip with a styled popover.
  */
-export function FilamentHoverCard({ data, children, disabled, className = '', spoolman, configureSlot }: FilamentHoverCardProps) {
+export function FilamentHoverCard({ data, children, disabled, className = '', spoolman, inventory, configureSlot }: FilamentHoverCardProps) {
   const { t } = useTranslation();
   const [isVisible, setIsVisible] = useState(false);
   const [position, setPosition] = useState<'top' | 'bottom'>('top');
@@ -235,6 +242,9 @@ export function FilamentHoverCard({ data, children, disabled, className = '', sp
                     {data.fillSource === 'spoolman' && data.fillLevel !== null && (
                       <span className="text-[9px] text-bambu-gray font-normal">{t('spoolman.fillSourceLabel')}</span>
                     )}
+                    {data.fillSource === 'inventory' && data.fillLevel !== null && (
+                      <span className="text-[9px] text-bambu-gray font-normal">{t('inventory.fillSourceLabel')}</span>
+                    )}
                   </span>
                 </div>
                 {/* Fill bar */}
@@ -319,6 +329,50 @@ export function FilamentHoverCard({ data, children, disabled, className = '', sp
                 </div>
               )}
 
+              {/* Inventory section - only for non-Bambu spools */}
+              {inventory && data.vendor !== 'Bambu Lab' && (
+                <div className="pt-2 mt-2 border-t border-bambu-dark-tertiary space-y-2">
+                  {inventory.assignedSpool ? (
+                    <>
+                      <div className="flex items-center gap-1.5">
+                        <Package className="w-3 h-3 text-bambu-green" />
+                        <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
+                          {t('inventory.assigned')}
+                        </span>
+                      </div>
+                      <p className="text-xs text-white truncate">
+                        {inventory.assignedSpool.brand ? `${inventory.assignedSpool.brand} ` : ''}
+                        {inventory.assignedSpool.material}
+                        {inventory.assignedSpool.color_name ? ` - ${inventory.assignedSpool.color_name}` : ''}
+                      </p>
+                      {inventory.onUnassignSpool && (
+                        <button
+                          onClick={(e) => {
+                            e.stopPropagation();
+                            inventory.onUnassignSpool?.();
+                          }}
+                          className="w-full flex items-center justify-center gap-1.5 px-2 py-1.5 text-xs font-medium rounded transition-colors bg-red-500/20 hover:bg-red-500/30 text-red-400"
+                        >
+                          <Unlink className="w-3.5 h-3.5" />
+                          {t('inventory.unassignSpool')}
+                        </button>
+                      )}
+                    </>
+                  ) : inventory.onAssignSpool ? (
+                    <button
+                      onClick={(e) => {
+                        e.stopPropagation();
+                        inventory.onAssignSpool?.();
+                      }}
+                      className="w-full flex items-center justify-center gap-1.5 px-2 py-1.5 text-xs font-medium rounded transition-colors bg-bambu-blue/20 hover:bg-bambu-blue/30 text-bambu-blue"
+                    >
+                      <Package className="w-3.5 h-3.5" />
+                      {t('inventory.assignSpool')}
+                    </button>
+                  ) : null}
+                </div>
+              )}
+
               {/* Configure slot section - always show if enabled */}
               {configureSlot?.enabled && (
                 <div className={`${spoolman?.enabled && data.trayUuid ? '' : 'pt-2 mt-2 border-t border-bambu-dark-tertiary'}`}>

+ 66 - 5
frontend/src/components/KProfilesView.tsx

@@ -150,6 +150,7 @@ interface KProfileModalProps {
   printerId: number;
   nozzleDiameter: string;
   existingProfiles?: KProfile[];  // Existing profiles for filament selection
+  builtinFilaments?: { filament_id: string; name: string }[];  // Filament ID → name lookup
   isDualNozzle?: boolean;  // Whether this is a dual-nozzle printer
   initialNote?: string;  // Initial note value for the profile
   initialNoteKey?: string | null;  // Key the note was stored under (for clearing)
@@ -164,6 +165,7 @@ function KProfileModal({
   printerId,
   nozzleDiameter,
   existingProfiles = [],
+  builtinFilaments = [],
   isDualNozzle = false,
   initialNote = '',
   initialNoteKey = null,
@@ -197,12 +199,21 @@ function KProfileModal({
   const [note, setNote] = useState(initialNote);
 
   // Extract unique filaments from existing K-profiles on the printer
-  // These have valid filament_ids that the printer recognizes
+  // Use builtin filament table for accurate name resolution (filament_id → name)
+  // Falls back to extracting from profile name for custom/unknown presets
   const knownFilaments = React.useMemo(() => {
+    // Build lookup map from builtin filament names (includes cloud presets from parent)
+    const builtinMap = new Map<string, string>();
+    for (const bf of builtinFilaments) {
+      builtinMap.set(bf.filament_id, bf.name);
+    }
+
     const filamentMap = new Map<string, { id: string; name: string }>();
     for (const p of existingProfiles) {
       if (p.filament_id && !filamentMap.has(p.filament_id)) {
-        const filamentName = extractFilamentName(p.name || '');
+        // Prefer builtin name (accurate), fall back to extracting from profile name
+        const builtinName = builtinMap.get(p.filament_id);
+        const filamentName = builtinName || extractFilamentName(p.name || '');
         filamentMap.set(p.filament_id, {
           id: p.filament_id,
           name: filamentName || p.filament_id,
@@ -212,7 +223,7 @@ function KProfileModal({
     return Array.from(filamentMap.values()).sort((a, b) =>
       a.name.localeCompare(b.name)
     );
-  }, [existingProfiles]);
+  }, [existingProfiles, builtinFilaments]);
 
   const saveMutation = useMutation({
     mutationFn: (data: KProfileCreate) => {
@@ -769,6 +780,20 @@ export function KProfilesView() {
     staleTime: 60000,  // Cache for 1 minute
   });
 
+  // Fetch builtin filament names for accurate filament_id → name resolution
+  const { data: builtinFilaments } = useQuery({
+    queryKey: ['builtinFilaments'],
+    queryFn: () => api.getBuiltinFilaments(),
+    staleTime: 300000,  // Cache for 5 minutes (static data)
+  });
+
+  // Fetch filament_id → name mapping for user cloud presets (P* IDs)
+  const { data: filamentIdMap } = useQuery({
+    queryKey: ['filamentIdMap'],
+    queryFn: () => api.getFilamentIdMap(),
+    staleTime: 300000,  // Cache for 5 minutes
+  });
+
   // Fetch K-profile notes (stored locally)
   const {
     data: notesData,
@@ -807,6 +832,39 @@ export function KProfilesView() {
   // Get connected printers for display
   const connectedPrinters = printers?.filter((p) => p.is_active) || [];
 
+  // Build filament lookup for name resolution (builtin + user cloud presets)
+  const builtinFilamentMap = React.useMemo(() => {
+    const map = new Map<string, string>();
+    if (builtinFilaments) {
+      for (const bf of builtinFilaments) {
+        map.set(bf.filament_id, bf.name);
+      }
+    }
+    // Also add user cloud presets (P* filament_ids resolved from cloud details)
+    if (filamentIdMap) {
+      for (const [fid, name] of Object.entries(filamentIdMap)) {
+        if (!map.has(fid)) {
+          map.set(fid, name);
+        }
+      }
+    }
+    return map;
+  }, [builtinFilaments, filamentIdMap]);
+
+  // Enriched builtin filaments array (builtin + cloud presets merged)
+  // Pass this to modals so they have the full filament name lookup
+  const enrichedBuiltinFilaments = React.useMemo(() => {
+    return Array.from(builtinFilamentMap.entries()).map(([fid, name]) => ({
+      filament_id: fid,
+      name,
+    }));
+  }, [builtinFilamentMap]);
+
+  // Resolve filament name: builtin table first, then extract from profile name
+  const resolveFilamentName = React.useCallback((profile: KProfile) => {
+    return builtinFilamentMap.get(profile.filament_id) || extractFilamentName(profile.name);
+  }, [builtinFilamentMap]);
+
   // Filter and sort profiles
   // Note: nozzle diameter filtering is done server-side via MQTT request
   const filteredProfiles = React.useMemo(() => {
@@ -841,13 +899,13 @@ export function KProfilesView() {
         case 'k_value':
           return parseFloat(a.k_value) - parseFloat(b.k_value);
         case 'filament':
-          return extractFilamentName(a.name).localeCompare(extractFilamentName(b.name));
+          return resolveFilamentName(a).localeCompare(resolveFilamentName(b));
         case 'name':
         default:
           return a.name.localeCompare(b.name);
       }
     });
-  }, [kprofiles?.profiles, searchQuery, extruderFilter, flowTypeFilter, sortOption]);
+  }, [kprofiles?.profiles, searchQuery, extruderFilter, flowTypeFilter, sortOption, resolveFilamentName]);
 
   // Check if selected printer is dual-nozzle (auto-detected from MQTT temperature data)
   const selectedPrinterData = printers?.find((p) => p.id === selectedPrinter);
@@ -1394,6 +1452,7 @@ export function KProfilesView() {
             printerId={selectedPrinter}
             nozzleDiameter={nozzleDiameter}
             existingProfiles={allProfiles?.profiles || kprofiles?.profiles}
+            builtinFilaments={enrichedBuiltinFilaments}
             isDualNozzle={isDualNozzle}
             initialNote={note}
             initialNoteKey={key}
@@ -1418,6 +1477,7 @@ export function KProfilesView() {
           printerId={selectedPrinter}
           nozzleDiameter={nozzleDiameter}
           existingProfiles={allProfiles?.profiles || kprofiles?.profiles}
+          builtinFilaments={enrichedBuiltinFilaments}
           isDualNozzle={isDualNozzle}
           onSaveNote={handleSaveNote}
           hasPermission={hasPermission}
@@ -1438,6 +1498,7 @@ export function KProfilesView() {
           printerId={selectedPrinter}
           nozzleDiameter={nozzleDiameter}
           existingProfiles={allProfiles?.profiles || kprofiles?.profiles}
+          builtinFilaments={enrichedBuiltinFilaments}
           isDualNozzle={isDualNozzle}
           onSaveNote={handleSaveNote}
           hasPermission={hasPermission}

+ 73 - 38
frontend/src/components/Layout.tsx

@@ -1,6 +1,6 @@
 import { useState, useEffect, useCallback, useRef, useMemo } from 'react';
 import { NavLink, Outlet, useNavigate, useLocation } from 'react-router-dom';
-import { Printer, Archive, Calendar, BarChart3, Cloud, Settings, Sun, Moon, ChevronLeft, ChevronRight, Keyboard, Github, GripVertical, ArrowUpCircle, Wrench, FolderKanban, FolderOpen, X, Menu, Info, Plug, Bug, LogOut, Key, Loader2, type LucideIcon } from 'lucide-react';
+import { Printer, Archive, Calendar, BarChart3, Cloud, Settings, Sun, Moon, ChevronLeft, ChevronRight, Keyboard, Github, GripVertical, ArrowUpCircle, Wrench, FolderKanban, FolderOpen, X, Menu, Info, Plug, Bug, LogOut, Key, Loader2, Package, type LucideIcon } from 'lucide-react';
 import { useTranslation } from 'react-i18next';
 import { useTheme } from '../contexts/ThemeContext';
 import { KeyboardShortcutsModal } from './KeyboardShortcutsModal';
@@ -29,6 +29,7 @@ export const defaultNavItems: NavItem[] = [
   { id: 'profiles', to: '/profiles', icon: Cloud, labelKey: 'nav.profiles' },
   { id: 'maintenance', to: '/maintenance', icon: Wrench, labelKey: 'nav.maintenance' },
   { id: 'projects', to: '/projects', icon: FolderKanban, labelKey: 'nav.projects' },
+  { id: 'inventory', to: '/inventory', icon: Package, labelKey: 'nav.inventory' },
   { id: 'files', to: '/files', icon: FolderOpen, labelKey: 'nav.files' },
   { id: 'settings', to: '/settings', icon: Settings, labelKey: 'nav.settings' },
 ];
@@ -118,6 +119,13 @@ export function Layout() {
     refetchInterval: 60 * 60 * 1000, // Check every hour
   });
 
+  // Fetch Spoolman settings to determine if inventory should be hidden
+  const { data: spoolmanSettings } = useQuery({
+    queryKey: ['spoolman-settings'],
+    queryFn: api.getSpoolmanSettings,
+    staleTime: 5 * 60 * 1000,
+  });
+
   // Fetch external links for sidebar
   const { data: externalLinks } = useQuery({
     queryKey: ['external-links'],
@@ -189,13 +197,13 @@ export function Layout() {
 
     // Determine if settings should be hidden (user role and auth enabled)
     const hideSettings = authEnabled && user?.role === 'user';
+    // Hide inventory when Spoolman mode is active
+    const hideInventory = spoolmanSettings?.spoolman_enabled === 'true';
 
     // Add items in stored order
     for (const id of sidebarOrder) {
-      // Skip settings if user is not admin
-      if (hideSettings && id === 'settings') {
-        continue;
-      }
+      if (hideSettings && id === 'settings') continue;
+      if (hideInventory && id === 'inventory') continue;
       if (navItemsMap.has(id) || extLinksMap.has(id)) {
         result.push(id);
         seen.add(id);
@@ -204,10 +212,8 @@ export function Layout() {
 
     // Add any new internal nav items not in stored order
     for (const item of defaultNavItems) {
-      // Skip settings if user is not admin
-      if (hideSettings && item.id === 'settings') {
-        continue;
-      }
+      if (hideSettings && item.id === 'settings') continue;
+      if (hideInventory && item.id === 'inventory') continue;
       if (!seen.has(item.id)) {
         result.push(item.id);
         seen.add(item.id);
@@ -347,9 +353,14 @@ export function Layout() {
         e.preventDefault();
 
         if (isExternalLinkId(id)) {
-          // External link - navigate to iframe page
-          const linkId = id.replace('ext-', '');
-          navigate(`/external/${linkId}`);
+          // External link
+          const extLink = extLinksMap.get(id);
+          if (extLink?.open_in_new_tab) {
+            window.open(extLink.url, '_blank', 'noopener,noreferrer');
+          } else {
+            const linkId = id.replace('ext-', '');
+            navigate(`/external/${linkId}`);
+          }
         } else {
           // Internal nav item
           const navItem = navItemsMap.get(id);
@@ -370,7 +381,7 @@ export function Layout() {
           break;
       }
     }
-  }, [navigate, orderedSidebarIds, navItemsMap]);
+  }, [navigate, orderedSidebarIds, navItemsMap, extLinksMap]);
 
   useEffect(() => {
     document.addEventListener('keydown', handleKeyDown);
@@ -451,31 +462,55 @@ export function Layout() {
                         : ''
                     }`}
                   >
-                    <NavLink
-                      to={`/external/${link.id}`}
-                      className={({ isActive }) =>
-                        `flex items-center ${isMobile || sidebarExpanded ? 'gap-3 px-4' : 'justify-center px-2'} py-3 rounded-lg transition-colors group ${
-                          isActive
-                            ? 'bg-bambu-green text-white'
-                            : 'text-bambu-gray-light hover:bg-bambu-dark-tertiary hover:text-white'
-                        }`
-                      }
-                      title={!isMobile && !sidebarExpanded ? link.name : undefined}
-                    >
-                      {sidebarExpanded && !isMobile && (
-                        <GripVertical className="w-4 h-4 flex-shrink-0 opacity-0 group-hover:opacity-50 cursor-grab active:cursor-grabbing -ml-1" />
-                      )}
-                      {link.custom_icon ? (
-                        <img
-                          src={`/api/v1/external-links/${link.id}/icon`}
-                          alt=""
-                          className="w-5 h-5 flex-shrink-0"
-                        />
-                      ) : (
-                        LinkIcon && <LinkIcon className="w-5 h-5 flex-shrink-0" />
-                      )}
-                      {(isMobile || sidebarExpanded) && <span>{link.name}</span>}
-                    </NavLink>
+                    {link.open_in_new_tab ? (
+                      <a
+                        href={link.url}
+                        target="_blank"
+                        rel="noopener noreferrer"
+                        className={`flex items-center ${isMobile || sidebarExpanded ? 'gap-3 px-4' : 'justify-center px-2'} py-3 rounded-lg transition-colors group text-bambu-gray-light hover:bg-bambu-dark-tertiary hover:text-white`}
+                        title={!isMobile && !sidebarExpanded ? link.name : undefined}
+                      >
+                        {sidebarExpanded && !isMobile && (
+                          <GripVertical className="w-4 h-4 flex-shrink-0 opacity-0 group-hover:opacity-50 cursor-grab active:cursor-grabbing -ml-1" />
+                        )}
+                        {link.custom_icon ? (
+                          <img
+                            src={`/api/v1/external-links/${link.id}/icon`}
+                            alt=""
+                            className="w-5 h-5 flex-shrink-0"
+                          />
+                        ) : (
+                          LinkIcon && <LinkIcon className="w-5 h-5 flex-shrink-0" />
+                        )}
+                        {(isMobile || sidebarExpanded) && <span>{link.name}</span>}
+                      </a>
+                    ) : (
+                      <NavLink
+                        to={`/external/${link.id}`}
+                        className={({ isActive }) =>
+                          `flex items-center ${isMobile || sidebarExpanded ? 'gap-3 px-4' : 'justify-center px-2'} py-3 rounded-lg transition-colors group ${
+                            isActive
+                              ? 'bg-bambu-green text-white'
+                              : 'text-bambu-gray-light hover:bg-bambu-dark-tertiary hover:text-white'
+                          }`
+                        }
+                        title={!isMobile && !sidebarExpanded ? link.name : undefined}
+                      >
+                        {sidebarExpanded && !isMobile && (
+                          <GripVertical className="w-4 h-4 flex-shrink-0 opacity-0 group-hover:opacity-50 cursor-grab active:cursor-grabbing -ml-1" />
+                        )}
+                        {link.custom_icon ? (
+                          <img
+                            src={`/api/v1/external-links/${link.id}/icon`}
+                            alt=""
+                            className="w-5 h-5 flex-shrink-0"
+                          />
+                        ) : (
+                          LinkIcon && <LinkIcon className="w-5 h-5 flex-shrink-0" />
+                        )}
+                        {(isMobile || sidebarExpanded) && <span>{link.name}</span>}
+                      </NavLink>
+                    )}
                   </li>
                 );
               } else {

+ 107 - 143
frontend/src/components/LinkSpoolModal.tsx

@@ -1,190 +1,154 @@
-import { useState } from 'react';
+import { useState, useMemo } from 'react';
 import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
 import { useTranslation } from 'react-i18next';
-import { X, Loader2, Link2, Check } from 'lucide-react';
+import { X, Loader2, Search, Link } from 'lucide-react';
 import { api } from '../api/client';
+import type { InventorySpool } from '../api/client';
 import { Button } from './Button';
 import { useToast } from '../contexts/ToastContext';
 
 interface LinkSpoolModalProps {
   isOpen: boolean;
   onClose: () => void;
+  tagUid: string;
   trayUuid: string;
-  trayInfo?: {
-    type: string;
-    color: string;
-    location: string;
-  };
+  printerId: number;
+  amsId: number;
+  trayId: number;
 }
 
-export function LinkSpoolModal({ isOpen, onClose, trayUuid, trayInfo }: LinkSpoolModalProps) {
+export function LinkSpoolModal({ isOpen, onClose, tagUid, trayUuid, printerId, amsId, trayId }: LinkSpoolModalProps) {
   const { t } = useTranslation();
   const queryClient = useQueryClient();
   const { showToast } = useToast();
-  const [selectedSpoolId, setSelectedSpoolId] = useState<number | null>(null);
+  const [search, setSearch] = useState('');
 
-  // Fetch unlinked spools
-  const { data: unlinkedSpools, isLoading } = useQuery({
-    queryKey: ['unlinked-spools'],
-    queryFn: api.getUnlinkedSpools,
+  const { data: spools, isLoading } = useQuery({
+    queryKey: ['inventory-spools'],
+    queryFn: () => api.getSpools(false),
     enabled: isOpen,
   });
 
-  // Link mutation
+  // Filter to untagged spools matching search
+  const filteredSpools = useMemo(() => {
+    if (!spools) return [];
+    return spools.filter((s: InventorySpool) => {
+      if (s.tag_uid || s.tray_uuid) return false; // Already tagged
+      if (!search) return true;
+      const q = search.toLowerCase();
+      return (
+        s.material.toLowerCase().includes(q) ||
+        (s.brand && s.brand.toLowerCase().includes(q)) ||
+        (s.color_name && s.color_name.toLowerCase().includes(q))
+      );
+    });
+  }, [spools, search]);
+
   const linkMutation = useMutation({
-    mutationFn: (spoolId: number) => api.linkSpool(spoolId, trayUuid),
+    mutationFn: (spoolId: number) =>
+      api.linkTagToSpool(spoolId, {
+        tag_uid: tagUid || undefined,
+        tray_uuid: trayUuid || undefined,
+        tag_type: trayUuid ? 'bambulab' : 'generic',
+        data_origin: 'nfc_link',
+      }),
     onSuccess: () => {
-      queryClient.invalidateQueries({ queryKey: ['unlinked-spools'] });
-      queryClient.invalidateQueries({ queryKey: ['linked-spools'] });
-      queryClient.invalidateQueries({ queryKey: ['spoolman-status'] });
-      showToast(t('spoolman.linkSuccess'), 'success');
+      queryClient.invalidateQueries({ queryKey: ['inventory-spools'] });
+      queryClient.invalidateQueries({ queryKey: ['inventory-assignments'] });
+      showToast(t('inventory.tagLinked'), 'success');
       onClose();
     },
-    onError: (error: Error) => {
-      showToast(`${t('spoolman.linkFailed')}: ${error.message}`, 'error');
+    onError: (err: Error) => {
+      showToast(err.message || t('inventory.tagLinkFailed'), 'error');
     },
   });
 
   if (!isOpen) return null;
 
-  const handleLink = () => {
-    if (selectedSpoolId) {
-      linkMutation.mutate(selectedSpoolId);
-    }
-  };
-
   return (
     <div className="fixed inset-0 z-50 flex items-center justify-center">
-      {/* Backdrop */}
-      <div
-        className="absolute inset-0 bg-black/60 backdrop-blur-sm"
-        onClick={onClose}
-      />
-
-      {/* Modal */}
-      <div className="relative w-full max-w-md mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl">
+      <div className="absolute inset-0 bg-black/60 backdrop-blur-sm" onClick={onClose} />
+      <div className="relative bg-bambu-dark-secondary rounded-xl shadow-xl w-full max-w-md mx-4 max-h-[80vh] flex flex-col border border-bambu-dark-tertiary">
         {/* Header */}
-        <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
-          <div className="flex items-center gap-2">
-            <Link2 className="w-5 h-5 text-bambu-green" />
-            <h2 className="text-lg font-semibold text-white">{t('spoolman.linkToSpoolman')}</h2>
+        <div className="flex items-center justify-between p-4 border-b border-white/10">
+          <div>
+            <h3 className="text-lg font-semibold text-white flex items-center gap-2">
+              <Link className="w-5 h-5 text-bambu-green" />
+              {t('inventory.linkToSpool')}
+            </h3>
+            <p className="text-xs text-bambu-gray mt-1">
+              AMS {amsId} T{trayId} &middot; Printer #{printerId}
+            </p>
           </div>
-          <button
-            onClick={onClose}
-            className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
-          >
+          <button onClick={onClose} className="p-1 text-bambu-gray hover:text-white rounded transition-colors">
             <X className="w-5 h-5" />
           </button>
         </div>
 
-        {/* Content */}
-        <div className="p-4 space-y-4">
-          {/* Tray info */}
-          {trayInfo && (
-            <div className="p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
-              <p className="text-xs text-bambu-gray mb-1">Linking AMS tray:</p>
-              <div className="flex items-center gap-2">
-                {trayInfo.color && (
-                  <span
-                    className="w-4 h-4 rounded-full border border-white/20"
-                    style={{ backgroundColor: `#${trayInfo.color}` }}
-                  />
-                )}
-                <span className="text-white font-medium">{trayInfo.type}</span>
-                <span className="text-bambu-gray">({trayInfo.location})</span>
-              </div>
-            </div>
-          )}
-
-          {/* Spool UUID */}
-          <div className="p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
-            <p className="text-xs text-bambu-gray mb-1">{t('spoolman.spoolId')}:</p>
-            <code className="text-xs text-bambu-green font-mono break-all">{trayUuid}</code>
+        {/* Search */}
+        <div className="p-4 border-b border-white/10">
+          <div className="relative">
+            <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray" />
+            <input
+              type="text"
+              value={search}
+              onChange={(e) => setSearch(e.target.value)}
+              placeholder={t('inventory.searchSpools')}
+              className="w-full pl-9 pr-3 py-2 bg-bambu-dark rounded-lg border border-white/10 text-white text-sm placeholder:text-bambu-gray focus:outline-none focus:border-bambu-green"
+            />
           </div>
-
-          {/* Spool list */}
-          <div>
-            <p className="text-sm text-bambu-gray mb-2">
-              {t('spoolman.selectSpool')}:
+          {(tagUid || trayUuid) && (
+            <p className="text-xs text-bambu-gray mt-2 font-mono truncate" title={tagUid || trayUuid}>
+              Tag: {tagUid || trayUuid}
             </p>
+          )}
+        </div>
 
-            {isLoading ? (
-              <div className="flex justify-center py-8">
-                <Loader2 className="w-6 h-6 text-bambu-green animate-spin" />
-              </div>
-            ) : unlinkedSpools && unlinkedSpools.length > 0 ? (
-              <div className="max-h-64 overflow-y-auto space-y-2">
-                {unlinkedSpools.map((spool) => (
-                  <button
-                    key={spool.id}
-                    onClick={() => setSelectedSpoolId(spool.id)}
-                    className={`w-full p-3 rounded-lg border text-left transition-colors ${
-                      selectedSpoolId === spool.id
-                        ? 'bg-bambu-green/20 border-bambu-green'
-                        : 'bg-bambu-dark border-bambu-dark-tertiary hover:border-bambu-gray'
-                    }`}
-                  >
-                    <div className="flex items-center gap-2">
-                      {spool.filament_color_hex && (
-                        <span
-                          className="w-4 h-4 rounded-full border border-white/20 flex-shrink-0"
-                          style={{ backgroundColor: `#${spool.filament_color_hex}` }}
-                        />
-                      )}
-                      <div className="flex-1 min-w-0">
-                        <p className="text-white font-medium truncate">
-                          {spool.filament_name || 'Unknown filament'}
-                        </p>
-                        <p className="text-xs text-bambu-gray">
-                          {spool.filament_material || 'Unknown'}
-                          {spool.remaining_weight !== null && ` - ${Math.round(spool.remaining_weight)}g`}
-                          {spool.location && ` - ${spool.location}`}
-                        </p>
-                      </div>
-                      {selectedSpoolId === spool.id && (
-                        <Check className="w-4 h-4 text-bambu-green flex-shrink-0" />
-                      )}
-                    </div>
-                  </button>
-                ))}
-              </div>
-            ) : (
-              <div className="text-center py-8 text-bambu-gray">
-                <p>{t('spoolman.noUnlinkedSpools')}</p>
-              </div>
-            )}
-          </div>
+        {/* Spool List */}
+        <div className="flex-1 overflow-y-auto p-2 min-h-0">
+          {isLoading ? (
+            <div className="flex justify-center py-8">
+              <Loader2 className="w-6 h-6 animate-spin text-bambu-green" />
+            </div>
+          ) : filteredSpools.length === 0 ? (
+            <p className="text-center text-bambu-gray py-8 text-sm">
+              {t('inventory.noSpoolsMatch')}
+            </p>
+          ) : (
+            filteredSpools.map((spool: InventorySpool) => (
+              <button
+                key={spool.id}
+                onClick={() => linkMutation.mutate(spool.id)}
+                disabled={linkMutation.isPending}
+                className="w-full flex items-center gap-3 p-3 rounded-lg hover:bg-white/5 transition-colors text-left"
+              >
+                <span
+                  className="w-6 h-6 rounded-full border border-white/20 flex-shrink-0"
+                  style={{ backgroundColor: spool.rgba ? `#${spool.rgba.substring(0, 6)}` : '#808080' }}
+                />
+                <div className="flex-1 min-w-0">
+                  <div className="text-sm text-white font-medium truncate">
+                    {spool.brand ? `${spool.brand} ` : ''}{spool.material}
+                    {spool.subtype ? ` ${spool.subtype}` : ''}
+                  </div>
+                  <div className="text-xs text-bambu-gray truncate">
+                    {spool.color_name || 'No color'} &middot; #{spool.id}
+                  </div>
+                </div>
+                <span className="text-xs text-bambu-gray">
+                  {Math.round(spool.label_weight - spool.weight_used)}g
+                </span>
+              </button>
+            ))
+          )}
         </div>
 
         {/* Footer */}
-        <div className="flex justify-end gap-2 p-4 border-t border-bambu-dark-tertiary">
-          <Button variant="secondary" onClick={onClose}>
-            {t('common.cancel')}
-          </Button>
-          <Button
-            onClick={handleLink}
-            disabled={!selectedSpoolId || linkMutation.isPending}
-          >
-            {linkMutation.isPending ? (
-              <>
-                <Loader2 className="w-4 h-4 animate-spin" />
-                {t('spoolman.syncing')}
-              </>
-            ) : (
-              <>
-                <Link2 className="w-4 h-4" />
-                {t('spoolman.linkToSpoolman')}
-              </>
-            )}
+        <div className="p-4 border-t border-white/10 flex justify-end">
+          <Button variant="ghost" onClick={onClose}>
+            {t('inventory.cancel') || 'Cancel'}
           </Button>
         </div>
-
-        {/* Error */}
-        {linkMutation.isError && (
-          <div className="mx-4 mb-4 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400">
-            {(linkMutation.error as Error).message}
-          </div>
-        )}
       </div>
     </div>
   );

+ 13 - 2
frontend/src/components/PrintModal/FilamentMapping.tsx

@@ -1,4 +1,5 @@
 import { useState } from 'react';
+import { useTranslation } from 'react-i18next';
 import { useQuery, useQueryClient } from '@tanstack/react-query';
 import { Circle, Check, AlertTriangle, RefreshCw, ChevronDown, ChevronUp } from 'lucide-react';
 import { api } from '../../api/client';
@@ -17,6 +18,7 @@ export function FilamentMapping({
   onManualMappingChange,
   defaultExpanded = false,
 }: FilamentMappingProps & { defaultExpanded?: boolean }) {
+  const { t } = useTranslation();
   const queryClient = useQueryClient();
   const [isRefreshing, setIsRefreshing] = useState(false);
   const [isExpanded, setIsExpanded] = useState(defaultExpanded);
@@ -32,6 +34,7 @@ export function FilamentMapping({
     useFilamentMapping(filamentReqs, printerStatus, manualMappings);
 
   const hasFilamentReqs = filamentReqs?.filaments && filamentReqs.filaments.length > 0;
+  const isDualNozzle = filamentReqs?.filaments?.some((f) => f.nozzle_id != null) ?? false;
 
   // Don't render if no filament requirements
   if (!hasFilamentReqs) {
@@ -126,8 +129,16 @@ export function FilamentMapping({
               <span title={`Required: ${item.type} - ${getColorName(item.color)}`}>
                 <Circle className="w-3 h-3" fill={item.color} stroke={item.color} />
               </span>
-              {/* Required type + grams */}
-              <span className="text-white truncate">
+              {/* Required type + grams + nozzle badge */}
+              <span className="text-white truncate flex items-center gap-1">
+                {isDualNozzle && item.nozzle_id != null && (
+                  <span
+                    className="inline-flex items-center justify-center w-3.5 h-3.5 rounded text-[9px] font-bold leading-none bg-bambu-gray/20 text-bambu-gray shrink-0"
+                    title={item.nozzle_id === 1 ? t('printModal.leftNozzleTooltip') : t('printModal.rightNozzleTooltip')}
+                  >
+                    {item.nozzle_id === 1 ? t('printModal.leftNozzle') : t('printModal.rightNozzle')}
+                  </span>
+                )}
                 {item.type} <span className="text-bambu-gray">({item.used_grams}g)</span>
               </span>
               {/* Arrow */}

+ 1 - 0
frontend/src/components/PrintModal/types.ts

@@ -158,6 +158,7 @@ export interface FilamentReqsData {
     color: string;
     used_grams: number;
     used_meters: number;
+    nozzle_id?: number;
   }>;
 }
 

+ 66 - 4
frontend/src/components/PrinterQueueWidget.tsx

@@ -1,11 +1,15 @@
-import { useQuery } from '@tanstack/react-query';
-import { Clock, Calendar, ChevronRight } from 'lucide-react';
+import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
+import { Clock, Calendar, ChevronRight, Loader2, CircleCheck } from 'lucide-react';
 import { Link } from 'react-router-dom';
+import { useTranslation } from 'react-i18next';
 import { api } from '../api/client';
+import { useAuth } from '../contexts/AuthContext';
+import { useToast } from '../contexts/ToastContext';
 import { parseUTCDate } from '../utils/date';
 
 interface PrinterQueueWidgetProps {
   printerId: number;
+  printerState?: string | null;
 }
 
 function formatRelativeTime(dateString: string | null): string {
@@ -22,13 +26,29 @@ function formatRelativeTime(dateString: string | null): string {
   return date.toLocaleDateString();
 }
 
-export function PrinterQueueWidget({ printerId }: PrinterQueueWidgetProps) {
+export function PrinterQueueWidget({ printerId, printerState }: PrinterQueueWidgetProps) {
+  const { t } = useTranslation();
+  const queryClient = useQueryClient();
+  const { showToast } = useToast();
+  const { hasPermission } = useAuth();
   const { data: queue } = useQuery({
     queryKey: ['queue', printerId, 'pending'],
     queryFn: () => api.getQueue(printerId, 'pending'),
     refetchInterval: 30000,
   });
 
+  const clearPlateMutation = useMutation({
+    mutationFn: () => api.clearPlate(printerId),
+    onSuccess: () => {
+      queryClient.invalidateQueries({ queryKey: ['queue', printerId] });
+      queryClient.invalidateQueries({ queryKey: ['printerStatus', printerId] });
+      showToast(t('queue.clearPlateSuccess'), 'success');
+    },
+    onError: (err: Error) => {
+      showToast(err.message, 'error');
+    },
+  });
+
   const nextItem = queue?.[0];
   const totalPending = queue?.length || 0;
 
@@ -36,6 +56,48 @@ export function PrinterQueueWidget({ printerId }: PrinterQueueWidgetProps) {
     return null;
   }
 
+  const needsClearPlate = printerState === 'FINISH' || printerState === 'FAILED';
+
+  if (needsClearPlate) {
+    return (
+      <div className="mb-3 p-3 bg-bambu-dark rounded-lg border border-yellow-400/30">
+        <div className="flex items-center gap-3 mb-2">
+          <Calendar className="w-5 h-5 text-yellow-400 flex-shrink-0" />
+          <div className="min-w-0 flex-1">
+            <p className="text-xs text-bambu-gray">{t('queue.nextInQueue')}</p>
+            <p className="text-sm text-white truncate">
+              {nextItem?.archive_name || `Archive #${nextItem?.archive_id}`}
+            </p>
+          </div>
+          {totalPending > 1 && (
+            <span className="text-xs px-1.5 py-0.5 bg-yellow-400/20 text-yellow-400 rounded flex-shrink-0">
+              +{totalPending - 1}
+            </span>
+          )}
+        </div>
+        {clearPlateMutation.isSuccess ? (
+          <div className="w-full py-2 px-3 rounded-lg bg-bambu-green/10 border border-bambu-green/20 text-bambu-green text-sm flex items-center justify-center gap-2">
+            <CircleCheck className="w-4 h-4" />
+            {t('queue.plateReady')}
+          </div>
+        ) : (
+          <button
+            onClick={() => clearPlateMutation.mutate()}
+            disabled={clearPlateMutation.isPending || !hasPermission('printers:control')}
+            className="w-full py-2 px-3 rounded-lg bg-bambu-green/20 border border-bambu-green/40 text-bambu-green hover:bg-bambu-green/30 transition-colors text-sm font-medium flex items-center justify-center gap-2 disabled:opacity-50"
+          >
+            {clearPlateMutation.isPending ? (
+              <Loader2 className="w-4 h-4 animate-spin" />
+            ) : (
+              <CircleCheck className="w-4 h-4" />
+            )}
+            {t('queue.clearPlate')}
+          </button>
+        )}
+      </div>
+    );
+  }
+
   return (
     <Link
       to="/queue"
@@ -45,7 +107,7 @@ export function PrinterQueueWidget({ printerId }: PrinterQueueWidgetProps) {
         <div className="flex items-center gap-3 min-w-0 flex-1">
           <Calendar className="w-5 h-5 text-yellow-400 flex-shrink-0" />
           <div className="min-w-0 flex-1">
-            <p className="text-xs text-bambu-gray">Next in queue</p>
+            <p className="text-xs text-bambu-gray">{t('queue.nextInQueue')}</p>
             <p className="text-sm text-white truncate">
               {nextItem?.archive_name || `Archive #${nextItem?.archive_id}`}
             </p>

+ 18 - 1
frontend/src/components/SkipObjectsModal.tsx

@@ -1,9 +1,11 @@
+import { useState } from 'react';
 import { useQuery, useMutation } from '@tanstack/react-query';
 import { useTranslation } from 'react-i18next';
 import { X, Loader2, Monitor, AlertCircle, Box } from 'lucide-react';
 import { api } from '../api/client';
 import { useToast } from '../contexts/ToastContext';
 import { useAuth } from '../contexts/AuthContext';
+import { ConfirmModal } from './ConfirmModal';
 
 // Custom Skip Objects icon - arrow jumping over boxes
 export const SkipObjectsIcon = ({ className }: { className?: string }) => (
@@ -28,6 +30,7 @@ export function SkipObjectsModal({ printerId, isOpen, onClose }: SkipObjectsModa
   const { t } = useTranslation();
   const { showToast } = useToast();
   const { hasPermission } = useAuth();
+  const [pendingSkip, setPendingSkip] = useState<{ id: number; name: string } | null>(null);
 
   const { data: status } = useQuery({
     queryKey: ['printerStatus', printerId],
@@ -47,6 +50,7 @@ export function SkipObjectsModal({ printerId, isOpen, onClose }: SkipObjectsModa
     mutationFn: (objectIds: number[]) => api.skipObjects(printerId, objectIds),
     onSuccess: (data) => {
       showToast(data.message || t('printers.skipObjects.objectsSkipped'));
+      setPendingSkip(null);
       refetchObjects();
     },
     onError: (error: Error) => showToast(error.message || t('printers.toast.failedToSkipObjects'), 'error'),
@@ -55,6 +59,7 @@ export function SkipObjectsModal({ printerId, isOpen, onClose }: SkipObjectsModa
   if (!isOpen) return null;
 
   return (
+    <>
     <div
       className="fixed inset-0 z-50 flex items-center justify-center"
       onClick={onClose}
@@ -243,7 +248,7 @@ export function SkipObjectsModal({ printerId, isOpen, onClose }: SkipObjectsModa
                     {/* Skip button */}
                     {!obj.skipped ? (
                       <button
-                        onClick={() => skipObjectsMutation.mutate([obj.id])}
+                        onClick={() => setPendingSkip({ id: obj.id, name: obj.name })}
                         disabled={skipObjectsMutation.isPending || (status?.layer_num ?? 0) <= 1 || !hasPermission('printers:control')}
                         className={`px-4 py-2 text-xs font-medium rounded-lg transition-colors ${
                           (status?.layer_num ?? 0) <= 1 || !hasPermission('printers:control')
@@ -267,5 +272,17 @@ export function SkipObjectsModal({ printerId, isOpen, onClose }: SkipObjectsModa
         )}
       </div>
     </div>
+    {pendingSkip && (
+      <ConfirmModal
+        variant="warning"
+        title={t('printers.skipObjects.confirmTitle')}
+        message={t('printers.skipObjects.confirmMessage', { name: pendingSkip.name })}
+        confirmText={t('printers.skipObjects.skip')}
+        isLoading={skipObjectsMutation.isPending}
+        onConfirm={() => skipObjectsMutation.mutate([pendingSkip.id])}
+        onCancel={() => setPendingSkip(null)}
+      />
+    )}
+  </>
   );
 }

+ 397 - 0
frontend/src/components/SpoolCatalogSettings.tsx

@@ -0,0 +1,397 @@
+import { useState, useEffect, useCallback, useRef } from 'react';
+import { useTranslation } from 'react-i18next';
+import { Database, Plus, Trash2, RotateCcw, Loader2, Pencil, Check, X, Search, Download, Upload } from 'lucide-react';
+import { api } from '../api/client';
+import type { SpoolCatalogEntry } from '../api/client';
+import { useToast } from '../contexts/ToastContext';
+import { Card, CardHeader, CardContent } from './Card';
+import { ConfirmModal } from './ConfirmModal';
+
+export function SpoolCatalogSettings() {
+  const { t } = useTranslation();
+  const { showToast } = useToast();
+  const [catalog, setCatalog] = useState<SpoolCatalogEntry[]>([]);
+  const [loading, setLoading] = useState(true);
+  const [search, setSearch] = useState('');
+  const fileInputRef = useRef<HTMLInputElement>(null);
+
+  // Add/Edit form state
+  const [showAddForm, setShowAddForm] = useState(false);
+  const [editingId, setEditingId] = useState<number | null>(null);
+  const [formName, setFormName] = useState('');
+  const [formWeight, setFormWeight] = useState('');
+  const [saving, setSaving] = useState(false);
+
+  // Confirmation modals
+  const [deleteEntry, setDeleteEntry] = useState<SpoolCatalogEntry | null>(null);
+  const [showResetConfirm, setShowResetConfirm] = useState(false);
+
+  const loadCatalog = useCallback(async () => {
+    try {
+      const entries = await api.getSpoolCatalog();
+      setCatalog(entries);
+    } catch {
+      showToast(t('settings.catalog.loadFailed'), 'error');
+    } finally {
+      setLoading(false);
+    }
+  }, [showToast, t]);
+
+  useEffect(() => {
+    loadCatalog();
+  }, [loadCatalog]);
+
+  const filteredCatalog = catalog.filter(entry =>
+    entry.name.toLowerCase().includes(search.toLowerCase())
+  );
+
+  const handleAdd = async () => {
+    if (!formName.trim() || !formWeight) {
+      showToast(t('settings.catalog.nameWeightRequired'), 'error');
+      return;
+    }
+    setSaving(true);
+    try {
+      const entry = await api.addCatalogEntry({ name: formName.trim(), weight: parseInt(formWeight) });
+      setCatalog(prev => [...prev, entry].sort((a, b) => a.name.localeCompare(b.name)));
+      setShowAddForm(false);
+      setFormName('');
+      setFormWeight('');
+      showToast(t('settings.catalog.entryAdded'), 'success');
+    } catch {
+      showToast(t('settings.catalog.addFailed'), 'error');
+    } finally {
+      setSaving(false);
+    }
+  };
+
+  const startEdit = (entry: SpoolCatalogEntry) => {
+    setEditingId(entry.id);
+    setFormName(entry.name);
+    setFormWeight(entry.weight.toString());
+  };
+
+  const cancelEdit = () => {
+    setEditingId(null);
+    setFormName('');
+    setFormWeight('');
+  };
+
+  const handleUpdate = async (id: number) => {
+    if (!formName.trim() || !formWeight) {
+      showToast(t('settings.catalog.nameWeightRequired'), 'error');
+      return;
+    }
+    setSaving(true);
+    try {
+      const updated = await api.updateCatalogEntry(id, { name: formName.trim(), weight: parseInt(formWeight) });
+      setCatalog(prev => prev.map(e => e.id === id ? updated : e).sort((a, b) => a.name.localeCompare(b.name)));
+      setEditingId(null);
+      setFormName('');
+      setFormWeight('');
+      showToast(t('settings.catalog.entryUpdated'), 'success');
+    } catch {
+      showToast(t('settings.catalog.updateFailed'), 'error');
+    } finally {
+      setSaving(false);
+    }
+  };
+
+  const handleDelete = async () => {
+    if (!deleteEntry) return;
+    try {
+      await api.deleteCatalogEntry(deleteEntry.id);
+      setCatalog(prev => prev.filter(e => e.id !== deleteEntry.id));
+      showToast(t('settings.catalog.entryDeleted'), 'success');
+    } catch {
+      showToast(t('settings.catalog.deleteFailed'), 'error');
+    } finally {
+      setDeleteEntry(null);
+    }
+  };
+
+  const handleReset = async () => {
+    setShowResetConfirm(false);
+    setLoading(true);
+    try {
+      await api.resetSpoolCatalog();
+      await loadCatalog();
+      showToast(t('settings.catalog.resetSuccess'), 'success');
+    } catch {
+      showToast(t('settings.catalog.resetFailed'), 'error');
+      setLoading(false);
+    }
+  };
+
+  const handleExport = () => {
+    const exportData = catalog.map(({ name, weight }) => ({ name, weight }));
+    const blob = new Blob([JSON.stringify(exportData, null, 2)], { type: 'application/json' });
+    const url = URL.createObjectURL(blob);
+    const a = document.createElement('a');
+    a.href = url;
+    a.download = 'spool-catalog.json';
+    document.body.appendChild(a);
+    a.click();
+    document.body.removeChild(a);
+    URL.revokeObjectURL(url);
+    showToast(t('settings.catalog.exported', { count: catalog.length }), 'success');
+  };
+
+  const handleImport = async (e: React.ChangeEvent<HTMLInputElement>) => {
+    const file = e.target.files?.[0];
+    if (!file) return;
+    try {
+      const text = await file.text();
+      const data = JSON.parse(text) as Array<{ name: string; weight: number }>;
+      if (!Array.isArray(data)) throw new Error('Invalid format');
+
+      let added = 0;
+      let skipped = 0;
+      for (const item of data) {
+        if (!item.name || typeof item.weight !== 'number') { skipped++; continue; }
+        const exists = catalog.some(c => c.name.toLowerCase() === item.name.toLowerCase());
+        if (exists) { skipped++; continue; }
+        try {
+          const entry = await api.addCatalogEntry({ name: item.name, weight: item.weight });
+          setCatalog(prev => [...prev, entry].sort((a, b) => a.name.localeCompare(b.name)));
+          added++;
+        } catch { skipped++; }
+      }
+      showToast(t('settings.catalog.imported', { added, skipped }), 'success');
+    } catch {
+      showToast(t('settings.catalog.importFailed'), 'error');
+    }
+    if (fileInputRef.current) fileInputRef.current.value = '';
+  };
+
+  return (
+    <Card>
+      <CardHeader>
+        <div className="flex items-center gap-2 mb-3">
+          <Database className="w-5 h-5 text-bambu-gray" />
+          <h2 className="text-lg font-semibold text-white">{t('settings.catalog.spoolCatalog')}</h2>
+          <span className="text-sm text-bambu-gray">({catalog.length})</span>
+        </div>
+        <div className="flex items-center gap-2 flex-wrap">
+          <button
+            onClick={handleExport}
+            className="px-3 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white transition-colors flex items-center gap-1.5"
+            title={t('settings.catalog.exportTooltip')}
+          >
+            <Download className="w-4 h-4" />
+            <span className="hidden sm:inline">{t('common.export')}</span>
+          </button>
+          <button
+            onClick={() => fileInputRef.current?.click()}
+            className="px-3 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white transition-colors flex items-center gap-1.5"
+            title={t('settings.catalog.importTooltip')}
+          >
+            <Upload className="w-4 h-4" />
+            <span className="hidden sm:inline">{t('common.import')}</span>
+          </button>
+          <input ref={fileInputRef} type="file" accept=".json" className="hidden" onChange={handleImport} />
+          <button
+            onClick={() => setShowResetConfirm(true)}
+            className="px-3 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white transition-colors flex items-center gap-1.5"
+            title={t('settings.catalog.resetTooltip')}
+          >
+            <RotateCcw className="w-4 h-4" />
+            <span className="hidden sm:inline">{t('common.reset')}</span>
+          </button>
+          <button
+            onClick={() => setShowAddForm(true)}
+            className="px-3 py-1.5 text-sm bg-bambu-green text-white rounded-lg hover:bg-bambu-green/80 transition-colors flex items-center gap-1.5"
+          >
+            <Plus className="w-4 h-4" />
+            <span className="hidden sm:inline">{t('common.add')}</span>
+          </button>
+        </div>
+      </CardHeader>
+      <CardContent className="space-y-4">
+        <p className="text-sm text-bambu-gray">
+          {t('settings.catalog.spoolCatalogDescription')}
+        </p>
+
+        {/* Search */}
+        <div className="relative">
+          <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray" />
+          <input
+            type="text"
+            className="w-full pl-10 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"
+            placeholder={t('settings.catalog.searchCatalog')}
+            value={search}
+            onChange={(e) => setSearch(e.target.value)}
+          />
+        </div>
+
+        {/* Add form */}
+        {showAddForm && (
+          <div className="p-4 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
+            <h3 className="text-sm font-medium text-white mb-3">{t('settings.catalog.addNewEntry')}</h3>
+            <div className="flex gap-2 items-center">
+              <div className="flex-1 min-w-0">
+                <input
+                  type="text"
+                  className="w-full px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"
+                  placeholder={t('settings.catalog.namePlaceholder')}
+                  value={formName}
+                  onChange={(e) => setFormName(e.target.value)}
+                />
+              </div>
+              <input
+                type="number"
+                className="w-20 px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white text-center focus:border-bambu-green focus:outline-none"
+                placeholder="g"
+                value={formWeight}
+                onChange={(e) => setFormWeight(e.target.value)}
+              />
+              <span className="text-bambu-gray shrink-0">g</span>
+              <button
+                onClick={handleAdd}
+                disabled={saving}
+                className="px-3 py-2 bg-bambu-green text-white rounded-lg hover:bg-bambu-green/80 flex items-center gap-1 shrink-0"
+              >
+                {saving ? <Loader2 className="w-4 h-4 animate-spin" /> : <Check className="w-4 h-4" />}
+                {t('common.add')}
+              </button>
+              <button
+                onClick={() => { setShowAddForm(false); setFormName(''); setFormWeight(''); }}
+                className="p-2 rounded-lg text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"
+              >
+                <X className="w-4 h-4" />
+              </button>
+            </div>
+          </div>
+        )}
+
+        {/* Catalog list */}
+        {loading ? (
+          <div className="flex items-center justify-center py-8 text-bambu-gray">
+            <Loader2 className="w-5 h-5 animate-spin mr-2" />
+            {t('common.loading')}
+          </div>
+        ) : (
+          <div className="max-h-[400px] overflow-y-auto border border-bambu-dark-tertiary rounded-lg">
+            <table className="w-full text-sm">
+              <thead className="bg-bambu-dark sticky top-0">
+                <tr>
+                  <th className="px-4 py-2 text-left text-bambu-gray font-medium">{t('common.name')}</th>
+                  <th className="px-4 py-2 text-right text-bambu-gray font-medium w-24">{t('settings.catalog.weight')}</th>
+                  <th className="px-4 py-2 text-center text-bambu-gray font-medium w-20">{t('settings.catalog.type')}</th>
+                  <th className="px-4 py-2 w-24"></th>
+                </tr>
+              </thead>
+              <tbody>
+                {filteredCatalog.length === 0 ? (
+                  <tr>
+                    <td colSpan={4} className="px-4 py-8 text-center text-bambu-gray">
+                      {search ? t('settings.catalog.noMatch') : t('settings.catalog.empty')}
+                    </td>
+                  </tr>
+                ) : (
+                  filteredCatalog.map(entry => (
+                    <tr key={entry.id} className="border-t border-bambu-dark-tertiary hover:bg-bambu-dark">
+                      {editingId === entry.id ? (
+                        <>
+                          <td className="px-4 py-2">
+                            <input
+                              type="text"
+                              className="w-full px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white focus:border-bambu-green focus:outline-none"
+                              value={formName}
+                              onChange={(e) => setFormName(e.target.value)}
+                            />
+                          </td>
+                          <td className="px-4 py-2">
+                            <input
+                              type="number"
+                              className="w-full px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-right focus:border-bambu-green focus:outline-none"
+                              value={formWeight}
+                              onChange={(e) => setFormWeight(e.target.value)}
+                            />
+                          </td>
+                          <td className="px-4 py-2 text-center">
+                            <span className="text-xs text-bambu-gray">-</span>
+                          </td>
+                          <td className="px-4 py-2">
+                            <div className="flex justify-end gap-1">
+                              <button
+                                onClick={() => handleUpdate(entry.id)}
+                                disabled={saving}
+                                className="p-1.5 rounded hover:bg-green-500/20 text-green-500"
+                              >
+                                {saving ? <Loader2 className="w-4 h-4 animate-spin" /> : <Check className="w-4 h-4" />}
+                              </button>
+                              <button onClick={cancelEdit} className="p-1.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray">
+                                <X className="w-4 h-4" />
+                              </button>
+                            </div>
+                          </td>
+                        </>
+                      ) : (
+                        <>
+                          <td className="px-4 py-2 text-white">{entry.name}</td>
+                          <td className="px-4 py-2 text-right font-mono text-white">{entry.weight}g</td>
+                          <td className="px-4 py-2 text-center">
+                            {entry.is_default ? (
+                              <span className="text-xs px-2 py-0.5 rounded bg-bambu-dark-tertiary text-bambu-gray">
+                                {t('settings.catalog.default')}
+                              </span>
+                            ) : (
+                              <span className="text-xs px-2 py-0.5 rounded bg-bambu-green/20 text-bambu-green">
+                                {t('settings.catalog.custom')}
+                              </span>
+                            )}
+                          </td>
+                          <td className="px-4 py-2">
+                            <div className="flex justify-end gap-1">
+                              <button
+                                onClick={() => startEdit(entry)}
+                                className="p-1.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white"
+                              >
+                                <Pencil className="w-4 h-4" />
+                              </button>
+                              <button
+                                onClick={() => setDeleteEntry(entry)}
+                                className="p-1.5 rounded bg-red-500/10 hover:bg-red-500/20 text-red-500"
+                              >
+                                <Trash2 className="w-4 h-4" />
+                              </button>
+                            </div>
+                          </td>
+                        </>
+                      )}
+                    </tr>
+                  ))
+                )}
+              </tbody>
+            </table>
+          </div>
+        )}
+      </CardContent>
+
+      {/* Delete confirmation */}
+      {deleteEntry && (
+        <ConfirmModal
+          title={t('settings.catalog.deleteEntry')}
+          message={t('settings.catalog.deleteConfirm', { name: deleteEntry.name })}
+          confirmText={t('common.delete')}
+          variant="danger"
+          onConfirm={handleDelete}
+          onCancel={() => setDeleteEntry(null)}
+        />
+      )}
+
+      {/* Reset confirmation */}
+      {showResetConfirm && (
+        <ConfirmModal
+          title={t('settings.catalog.resetCatalog')}
+          message={t('settings.catalog.resetConfirm')}
+          confirmText={t('common.reset')}
+          variant="danger"
+          onConfirm={handleReset}
+          onCancel={() => setShowResetConfirm(false)}
+        />
+      )}
+    </Card>
+  );
+}

+ 504 - 0
frontend/src/components/SpoolFormModal.tsx

@@ -0,0 +1,504 @@
+import { useState, useEffect, useMemo } from 'react';
+import { useMutation, useQueryClient } from '@tanstack/react-query';
+import { useTranslation } from 'react-i18next';
+import { X, Loader2, Save, Beaker, Palette } from 'lucide-react';
+import { api } from '../api/client';
+import type { InventorySpool, SlicerSetting, SpoolCatalogEntry, LocalPreset } from '../api/client';
+import { Button } from './Button';
+import { useToast } from '../contexts/ToastContext';
+import type { SpoolFormData, PrinterWithCalibrations, ColorPreset } from './spool-form/types';
+import { defaultFormData, validateForm } from './spool-form/types';
+import { buildFilamentOptions, extractBrandsFromPresets, findPresetOption, loadRecentColors, saveRecentColor } from './spool-form/utils';
+import { FilamentSection } from './spool-form/FilamentSection';
+import { ColorSection } from './spool-form/ColorSection';
+import { AdditionalSection } from './spool-form/AdditionalSection';
+import { PAProfileSection } from './spool-form/PAProfileSection';
+import { SpoolUsageHistory } from './SpoolUsageHistory';
+
+type TabId = 'filament' | 'pa-profile';
+
+interface SpoolFormModalProps {
+  isOpen: boolean;
+  onClose: () => void;
+  spool?: InventorySpool | null;
+  printersWithCalibrations?: PrinterWithCalibrations[];
+}
+
+export function SpoolFormModal({ isOpen, onClose, spool, printersWithCalibrations = [] }: SpoolFormModalProps) {
+  const { t } = useTranslation();
+  const queryClient = useQueryClient();
+  const { showToast } = useToast();
+
+  const isEditing = !!spool;
+
+  // Form state
+  const [formData, setFormData] = useState<SpoolFormData>(defaultFormData);
+  const [errors, setErrors] = useState<Partial<Record<keyof SpoolFormData, string>>>({});
+  const [activeTab, setActiveTab] = useState<TabId>('filament');
+
+  // Cloud presets
+  const [cloudAuthenticated, setCloudAuthenticated] = useState(false);
+  const [loadingCloudPresets, setLoadingCloudPresets] = useState(false);
+  const [cloudPresets, setCloudPresets] = useState<SlicerSetting[]>([]);
+  const [presetInputValue, setPresetInputValue] = useState('');
+
+  // Spool catalog
+  const [spoolCatalog, setSpoolCatalog] = useState<SpoolCatalogEntry[]>([]);
+
+  // Local presets (OrcaSlicer imports)
+  const [localPresets, setLocalPresets] = useState<LocalPreset[]>([]);
+
+  // Color catalog
+  const [colorCatalog, setColorCatalog] = useState<{ manufacturer: string; color_name: string; hex_color: string; material: string | null }[]>([]);
+
+  // Color state
+  const [recentColors, setRecentColors] = useState<ColorPreset[]>([]);
+
+  // PA Profile state
+  const [fetchedCalibrations, setFetchedCalibrations] = useState<PrinterWithCalibrations[]>([]);
+  const [selectedProfiles, setSelectedProfiles] = useState<Set<string>>(new Set());
+  const [expandedPrinters, setExpandedPrinters] = useState<Set<string>>(new Set());
+
+  // Use prop if provided, otherwise use self-fetched data
+  const resolvedCalibrations = printersWithCalibrations.length > 0
+    ? printersWithCalibrations
+    : fetchedCalibrations;
+
+  // Count selected PA profiles for tab badge
+  const selectedProfileCount = useMemo(() => {
+    return selectedProfiles.size;
+  }, [selectedProfiles]);
+
+  // Load recent colors on mount
+  useEffect(() => {
+    setRecentColors(loadRecentColors());
+  }, []);
+
+  // Fetch cloud presets and catalog when modal opens
+  useEffect(() => {
+    if (isOpen) {
+      const fetchData = async () => {
+        setLoadingCloudPresets(true);
+        try {
+          const status = await api.getCloudStatus();
+          setCloudAuthenticated(status.is_authenticated);
+          if (status.is_authenticated) {
+            const presets = await api.getFilamentPresets();
+            setCloudPresets(presets);
+          }
+        } catch (e) {
+          console.error('Failed to fetch cloud presets:', e);
+          setCloudAuthenticated(false);
+        } finally {
+          setLoadingCloudPresets(false);
+        }
+      };
+      fetchData();
+      api.getSpoolCatalog().then(setSpoolCatalog).catch(console.error);
+      api.getColorCatalog().then(setColorCatalog).catch(console.error);
+      api.getLocalPresets().then(r => setLocalPresets(r.filament)).catch(console.error);
+
+      // Fetch printer calibrations if not provided via props
+      if (printersWithCalibrations.length === 0) {
+        (async () => {
+          try {
+            const printers = await api.getPrinters();
+            const statuses = await Promise.all(
+              printers.map(p => api.getPrinterStatus(p.id).catch(() => null)),
+            );
+            const results: PrinterWithCalibrations[] = [];
+            for (let i = 0; i < printers.length; i++) {
+              const printer = printers[i];
+              const status = statuses[i];
+              const connected = status?.connected ?? false;
+              let calibrations: PrinterWithCalibrations['calibrations'] = [];
+              if (connected) {
+                try {
+                  const kRes = await api.getKProfiles(printer.id);
+                  calibrations = kRes.profiles.map(p => ({
+                    cali_idx: p.slot_id,
+                    filament_id: p.filament_id,
+                    setting_id: p.setting_id || '',
+                    name: p.name,
+                    k_value: parseFloat(p.k_value) || 0,
+                    n_coef: parseFloat(p.n_coef) || 0,
+                    extruder_id: p.extruder_id,
+                    nozzle_diameter: p.nozzle_diameter,
+                  }));
+                } catch {
+                  // Printer may not support K-profiles
+                }
+              }
+              results.push({ printer: { ...printer, connected }, calibrations });
+            }
+            setFetchedCalibrations(results);
+          } catch (e) {
+            console.error('Failed to fetch printer calibrations:', e);
+          }
+        })();
+      }
+    }
+  }, [isOpen, printersWithCalibrations.length]);
+
+  // Build filament options: cloud → local → fallback
+  const filamentOptions = useMemo(
+    () => buildFilamentOptions(cloudPresets, new Set(), localPresets),
+    [cloudPresets, localPresets],
+  );
+
+  // Extract brands from presets
+  const availableBrands = useMemo(
+    () => extractBrandsFromPresets(cloudPresets, localPresets),
+    [cloudPresets, localPresets],
+  );
+
+  // Find selected preset option
+  const selectedPresetOption = useMemo(
+    () => findPresetOption(formData.slicer_filament, filamentOptions),
+    [formData.slicer_filament, filamentOptions],
+  );
+
+  // Reset form when modal opens/closes or spool changes
+  useEffect(() => {
+    if (isOpen) {
+      if (spool) {
+        setFormData({
+          material: spool.material || '',
+          subtype: spool.subtype || '',
+          brand: spool.brand || '',
+          color_name: spool.color_name || '',
+          rgba: spool.rgba || '808080FF',
+          label_weight: spool.label_weight || 1000,
+          core_weight: spool.core_weight || 250,
+          weight_used: spool.weight_used || 0,
+          slicer_filament: spool.slicer_filament || '',
+          note: spool.note || '',
+        });
+        setPresetInputValue(spool.slicer_filament_name || spool.slicer_filament || '');
+
+        // Load K-profiles for this spool
+        if (spool.k_profiles && spool.k_profiles.length > 0) {
+          const profileKeys = new Set<string>();
+          for (const p of spool.k_profiles) {
+            if (p.cali_idx !== null && p.cali_idx !== undefined) {
+              profileKeys.add(`${p.printer_id}:${p.cali_idx}:${p.extruder ?? 'null'}`);
+            }
+          }
+          setSelectedProfiles(profileKeys);
+        } else {
+          setSelectedProfiles(new Set());
+        }
+      } else {
+        setFormData(defaultFormData);
+        setPresetInputValue('');
+        setSelectedProfiles(new Set());
+      }
+      setErrors({});
+      setActiveTab('filament');
+    }
+  }, [isOpen, spool]);
+
+  // Expand all printers in PA profile section when calibrations are available
+  useEffect(() => {
+    if (isOpen && resolvedCalibrations.length > 0) {
+      setExpandedPrinters(new Set(resolvedCalibrations.map(p => String(p.printer.id))));
+    }
+  }, [isOpen, resolvedCalibrations]);
+
+  // Update field helper
+  const updateField = <K extends keyof SpoolFormData>(key: K, value: SpoolFormData[K]) => {
+    setFormData(prev => ({ ...prev, [key]: value }));
+    if (errors[key]) {
+      setErrors(prev => ({ ...prev, [key]: undefined }));
+    }
+  };
+
+  // Handle color selection
+  const handleColorUsed = (color: ColorPreset) => {
+    setRecentColors(prev => saveRecentColor(color, prev));
+  };
+
+  // Mutations
+  const createMutation = useMutation({
+    mutationFn: (data: Record<string, unknown>) =>
+      api.createSpool(data as Parameters<typeof api.createSpool>[0]),
+    onSuccess: async (newSpool) => {
+      // Save K-profiles if any selected
+      if (selectedProfiles.size > 0 && newSpool?.id) {
+        await saveKProfiles(newSpool.id);
+      }
+      await queryClient.invalidateQueries({ queryKey: ['inventory-spools'] });
+      showToast(t('inventory.spoolCreated'), 'success');
+      onClose();
+    },
+    onError: (error: Error) => {
+      showToast(error.message, 'error');
+    },
+  });
+
+  const updateMutation = useMutation({
+    mutationFn: (data: Record<string, unknown>) =>
+      api.updateSpool(spool!.id, data as Parameters<typeof api.updateSpool>[1]),
+    onSuccess: async () => {
+      // Save K-profiles
+      if (spool?.id) {
+        await saveKProfiles(spool.id);
+      }
+      await queryClient.invalidateQueries({ queryKey: ['inventory-spools'] });
+      showToast(t('inventory.spoolUpdated'), 'success');
+      onClose();
+    },
+    onError: (error: Error) => {
+      showToast(error.message, 'error');
+    },
+  });
+
+  // Save K-profiles for selected calibrations
+  const saveKProfiles = async (spoolId: number) => {
+    if (selectedProfiles.size === 0) {
+      // Clear existing K-profiles
+      try {
+        await api.saveSpoolKProfiles(spoolId, []);
+      } catch {
+        // Ignore
+      }
+      return;
+    }
+
+    const profiles = [];
+    for (const key of selectedProfiles) {
+      const [printerIdStr, caliIdxStr, extruderStr] = key.split(':');
+      const printerId = parseInt(printerIdStr);
+      const caliIdx = parseInt(caliIdxStr);
+      const extruder = extruderStr === 'null' ? 0 : parseInt(extruderStr);
+
+      // Find the matching calibration
+      const pc = resolvedCalibrations.find(p => p.printer.id === printerId);
+      if (pc) {
+        const cal = pc.calibrations.find(c => c.cali_idx === caliIdx);
+        if (cal) {
+          profiles.push({
+            printer_id: printerId,
+            extruder,
+            nozzle_diameter: cal.nozzle_diameter || '0.4',
+            k_value: cal.k_value,
+            name: cal.name || null,
+            cali_idx: cal.cali_idx,
+            setting_id: cal.setting_id || null,
+          });
+        }
+      }
+    }
+
+    if (profiles.length > 0) {
+      try {
+        await api.saveSpoolKProfiles(spoolId, profiles);
+      } catch (e) {
+        console.error('Failed to save K-profiles:', e);
+      }
+    }
+  };
+
+  // Close on Escape key
+  useEffect(() => {
+    if (!isOpen) return;
+    const handleKeyDown = (e: KeyboardEvent) => {
+      if (e.key === 'Escape') onClose();
+    };
+    document.addEventListener('keydown', handleKeyDown);
+    return () => document.removeEventListener('keydown', handleKeyDown);
+  }, [isOpen, onClose]);
+
+  if (!isOpen) return null;
+
+  const handleSubmit = () => {
+    const validation = validateForm(formData);
+    if (!validation.isValid) {
+      setErrors(validation.errors);
+      // Switch to filament tab if there are errors there
+      if (validation.errors.slicer_filament || validation.errors.material) {
+        setActiveTab('filament');
+      }
+      return;
+    }
+
+    // Find preset name from selected option
+    const presetName = selectedPresetOption?.displayName || presetInputValue || null;
+
+    const data: Record<string, unknown> = {
+      material: formData.material,
+      subtype: formData.subtype || null,
+      brand: formData.brand || null,
+      color_name: formData.color_name || null,
+      rgba: formData.rgba || null,
+      label_weight: formData.label_weight,
+      core_weight: formData.core_weight,
+      weight_used: formData.weight_used,
+      slicer_filament: formData.slicer_filament || null,
+      slicer_filament_name: presetName,
+      nozzle_temp_min: null,
+      nozzle_temp_max: null,
+      note: formData.note || null,
+    };
+
+    if (isEditing) {
+      updateMutation.mutate(data);
+    } else {
+      createMutation.mutate(data);
+    }
+  };
+
+  const isPending = createMutation.isPending || updateMutation.isPending;
+
+  return (
+    <div className="fixed inset-0 z-50 flex items-center justify-center">
+      <div
+        className="absolute inset-0 bg-black/60 backdrop-blur-sm"
+        onClick={onClose}
+      />
+
+      <div className="relative w-full max-w-lg mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] flex flex-col">
+        {/* Header */}
+        <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary flex-shrink-0">
+          <h2 className="text-lg font-semibold text-white">
+            {isEditing ? t('inventory.editSpool') : t('inventory.addSpool')}
+          </h2>
+          <button
+            onClick={onClose}
+            className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
+          >
+            <X className="w-5 h-5" />
+          </button>
+        </div>
+
+        {/* Tabs */}
+        <div className="flex border-b border-bambu-dark-tertiary flex-shrink-0">
+          <button
+            onClick={() => setActiveTab('filament')}
+            className={`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${
+              activeTab === 'filament'
+                ? 'text-bambu-green border-b-2 border-bambu-green'
+                : 'text-bambu-gray hover:text-white'
+            }`}
+          >
+            <Palette className="w-4 h-4" />
+            {t('inventory.filamentInfoTab')}
+          </button>
+          <button
+            onClick={() => setActiveTab('pa-profile')}
+            className={`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${
+              activeTab === 'pa-profile'
+                ? 'text-bambu-green border-b-2 border-bambu-green'
+                : 'text-bambu-gray hover:text-white'
+            }`}
+          >
+            <Beaker className="w-4 h-4" />
+            {t('inventory.paProfileTab')}
+            {selectedProfileCount > 0 && (
+              <span className="text-xs px-1.5 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green">
+                {selectedProfileCount}
+              </span>
+            )}
+          </button>
+        </div>
+
+        {/* Content */}
+        <div className="p-4 overflow-y-auto flex-1">
+          {activeTab === 'filament' ? (
+            <div className="space-y-6">
+              {/* Filament Info Section */}
+              <div>
+                <h3 className="text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3">
+                  {t('inventory.filamentInfo')}
+                </h3>
+                <FilamentSection
+                  formData={formData}
+                  updateField={updateField}
+                  cloudAuthenticated={cloudAuthenticated}
+                  loadingCloudPresets={loadingCloudPresets}
+                  presetInputValue={presetInputValue}
+                  setPresetInputValue={setPresetInputValue}
+                  selectedPresetOption={selectedPresetOption}
+                  filamentOptions={filamentOptions}
+                  availableBrands={availableBrands}
+                />
+                {errors.slicer_filament && (
+                  <p className="mt-1 text-xs text-red-400">{errors.slicer_filament}</p>
+                )}
+                {errors.material && (
+                  <p className="mt-1 text-xs text-red-400">{errors.material}</p>
+                )}
+              </div>
+
+              {/* Color Section */}
+              <div>
+                <h3 className="text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3">
+                  {t('inventory.color')}
+                </h3>
+                <ColorSection
+                  formData={formData}
+                  updateField={updateField}
+                  recentColors={recentColors}
+                  onColorUsed={handleColorUsed}
+                  catalogColors={colorCatalog}
+                />
+              </div>
+
+              {/* Additional Section */}
+              <div>
+                <h3 className="text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3">
+                  {t('inventory.additional')}
+                </h3>
+                <AdditionalSection
+                  formData={formData}
+                  updateField={updateField}
+                  spoolCatalog={spoolCatalog}
+                />
+              </div>
+
+              {/* Usage History (only when editing) */}
+              {isEditing && spool && (
+                <div>
+                  <SpoolUsageHistory spoolId={spool.id} />
+                </div>
+              )}
+            </div>
+          ) : (
+            <PAProfileSection
+              formData={formData}
+              updateField={updateField}
+              printersWithCalibrations={resolvedCalibrations}
+              selectedProfiles={selectedProfiles}
+              setSelectedProfiles={setSelectedProfiles}
+              expandedPrinters={expandedPrinters}
+              setExpandedPrinters={setExpandedPrinters}
+            />
+          )}
+        </div>
+
+        {/* Footer */}
+        <div className="flex justify-end gap-2 p-4 border-t border-bambu-dark-tertiary flex-shrink-0">
+          <Button variant="secondary" onClick={onClose}>
+            {t('common.cancel')}
+          </Button>
+          <Button
+            onClick={handleSubmit}
+            disabled={isPending}
+          >
+            {isPending ? (
+              <>
+                <Loader2 className="w-4 h-4 animate-spin" />
+                {t('common.saving')}
+              </>
+            ) : (
+              <>
+                <Save className="w-4 h-4" />
+                {isEditing ? t('common.save') : t('inventory.addSpool')}
+              </>
+            )}
+          </Button>
+        </div>
+      </div>
+    </div>
+  );
+}

+ 101 - 0
frontend/src/components/SpoolUsageHistory.tsx

@@ -0,0 +1,101 @@
+import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
+import { useTranslation } from 'react-i18next';
+import { Loader2, Trash2, Clock } from 'lucide-react';
+import { api } from '../api/client';
+import type { SpoolUsageRecord } from '../api/client';
+import { Button } from './Button';
+import { useToast } from '../contexts/ToastContext';
+
+interface SpoolUsageHistoryProps {
+  spoolId: number;
+}
+
+function formatDate(dateStr: string): string {
+  const date = new Date(dateStr);
+  return date.toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: '2-digit' }) +
+    ' ' + date.toLocaleTimeString('en-GB', { hour: '2-digit', minute: '2-digit' });
+}
+
+const STATUS_COLORS: Record<string, string> = {
+  completed: 'text-bambu-green',
+  failed: 'text-red-400',
+  aborted: 'text-yellow-400',
+};
+
+export function SpoolUsageHistory({ spoolId }: SpoolUsageHistoryProps) {
+  const { t } = useTranslation();
+  const queryClient = useQueryClient();
+  const { showToast } = useToast();
+
+  const { data: history, isLoading } = useQuery({
+    queryKey: ['spool-usage', spoolId],
+    queryFn: () => api.getSpoolUsageHistory(spoolId),
+  });
+
+  const clearMutation = useMutation({
+    mutationFn: () => api.clearSpoolUsageHistory(spoolId),
+    onSuccess: () => {
+      queryClient.invalidateQueries({ queryKey: ['spool-usage', spoolId] });
+      showToast(t('inventory.historyCleared'), 'success');
+    },
+  });
+
+  if (isLoading) {
+    return (
+      <div className="flex justify-center py-4">
+        <Loader2 className="w-5 h-5 animate-spin text-bambu-green" />
+      </div>
+    );
+  }
+
+  if (!history || history.length === 0) {
+    return (
+      <div className="text-center py-4 text-bambu-gray text-sm">
+        <Clock className="w-5 h-5 mx-auto mb-2 opacity-50" />
+        {t('inventory.noUsageHistory')}
+      </div>
+    );
+  }
+
+  return (
+    <div className="space-y-2">
+      <div className="flex items-center justify-between">
+        <h4 className="text-sm font-medium text-white">{t('inventory.usageHistory')}</h4>
+        <Button
+          variant="ghost"
+          size="sm"
+          onClick={() => clearMutation.mutate()}
+          disabled={clearMutation.isPending}
+          className="text-xs text-bambu-gray hover:text-red-400"
+        >
+          <Trash2 className="w-3 h-3 mr-1" />
+          {t('inventory.clearHistory')}
+        </Button>
+      </div>
+      <div className="max-h-48 overflow-y-auto space-y-1">
+        {history.map((record: SpoolUsageRecord) => (
+          <div
+            key={record.id}
+            className="flex items-center justify-between p-2 rounded bg-bambu-dark/50 text-xs"
+          >
+            <div className="flex-1 min-w-0">
+              <span className="text-bambu-gray">{formatDate(record.created_at)}</span>
+              {record.print_name && (
+                <span className="text-white ml-2 truncate" title={record.print_name}>
+                  {record.print_name}
+                </span>
+              )}
+            </div>
+            <div className="flex items-center gap-2 flex-shrink-0 ml-2">
+              <span className="text-white font-medium">{record.weight_used.toFixed(1)}g</span>
+              <span className="text-bambu-gray">({record.percent_used}%)</span>
+              <span className={STATUS_COLORS[record.status] || 'text-bambu-gray'}>
+                {record.status}
+              </span>
+            </div>
+          </div>
+        ))}
+      </div>
+    </div>
+  );
+}

+ 317 - 264
frontend/src/components/SpoolmanSettings.tsx

@@ -1,7 +1,7 @@
 import { useState, useEffect } from 'react';
 import { useTranslation } from 'react-i18next';
 import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
-import { Loader2, Check, X, RefreshCw, Link2, Link2Off, Database, ChevronDown, Info, AlertTriangle } from 'lucide-react';
+import { Loader2, Check, X, RefreshCw, Link2, Link2Off, Database, ChevronDown, Info, AlertTriangle, Package, ExternalLink } from 'lucide-react';
 import { api } from '../api/client';
 import type { SpoolmanSyncResult, Printer } from '../api/client';
 import { Card, CardContent, CardHeader } from './Card';
@@ -146,7 +146,7 @@ export function SpoolmanSettings() {
         <CardHeader>
           <div className="flex items-center gap-2">
             <Database className="w-5 h-5 text-bambu-green" />
-            <h2 className="text-lg font-semibold text-white">Spoolman Integration</h2>
+            <h2 className="text-lg font-semibold text-white">{t('settings.filamentTracking')}</h2>
           </div>
         </CardHeader>
         <CardContent>
@@ -164,7 +164,7 @@ export function SpoolmanSettings() {
         <div className="flex items-center justify-between">
           <div className="flex items-center gap-2">
             <Database className="w-5 h-5 text-bambu-green" />
-            <h2 className="text-lg font-semibold text-white">Spoolman Integration</h2>
+            <h2 className="text-lg font-semibold text-white">{t('settings.filamentTracking')}</h2>
           </div>
           {saveMutation.isPending && (
             <Loader2 className="w-4 h-4 text-bambu-green animate-spin" />
@@ -173,303 +173,356 @@ export function SpoolmanSettings() {
       </CardHeader>
       <CardContent className="space-y-4">
         <p className="text-sm text-bambu-gray">
-          Connect to Spoolman for filament inventory tracking. AMS data will sync automatically.
+          {t('settings.filamentTrackingDesc')}
         </p>
 
-        {/* Info banner about sync requirements */}
-        <div className="p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg">
-          <div className="flex gap-2">
-            <Info className="w-4 h-4 text-blue-400 flex-shrink-0 mt-0.5" />
-            <div className="text-xs text-blue-300">
-              <p className="font-medium mb-1">How Sync Works</p>
-              <ul className="list-disc list-inside space-y-0.5 text-blue-300/80">
-                <li>Only official Bambu Lab spools with RFID are synced</li>
-                <li>New spools are auto-created in Spoolman on first sync</li>
-                <li>Non-Bambu Lab spools (third-party, refilled) are skipped</li>
-              </ul>
-              <p className="font-medium mt-2 mb-1">Linking Existing Spools</p>
-              <p className="text-blue-300/80">
-                To link existing Spoolman spools to your AMS, hover over an AMS slot and click "Link to Spoolman".
-              </p>
+        {/* Mode selector cards */}
+        <div className="grid grid-cols-2 gap-3">
+          {/* Built-in Inventory */}
+          <button
+            type="button"
+            onClick={() => setLocalEnabled(false)}
+            className={`p-3 rounded-lg border-2 text-left transition-colors ${
+              !localEnabled
+                ? 'border-bambu-green bg-bambu-green/10'
+                : 'border-bambu-dark-tertiary bg-bambu-dark hover:border-bambu-gray/50'
+            }`}
+          >
+            <div className="flex items-center gap-2 mb-1.5">
+              <Package className={`w-4 h-4 ${!localEnabled ? 'text-bambu-green' : 'text-bambu-gray'}`} />
+              <span className={`text-sm font-medium ${!localEnabled ? 'text-white' : 'text-bambu-gray'}`}>
+                {t('settings.trackingModeBuiltIn')}
+              </span>
             </div>
-          </div>
-        </div>
-
-        {/* Enable toggle */}
-        <div className="flex items-center justify-between">
-          <div>
-            <p className="text-white">Enable Spoolman</p>
-            <p className="text-sm text-bambu-gray">
-              Sync filament data with Spoolman server
+            <p className={`text-xs ${!localEnabled ? 'text-bambu-gray' : 'text-bambu-gray/60'}`}>
+              {t('settings.trackingModeBuiltInDesc')}
             </p>
-          </div>
-          <label className="relative inline-flex items-center cursor-pointer">
-            <input
-              type="checkbox"
-              checked={localEnabled}
-              onChange={(e) => setLocalEnabled(e.target.checked)}
-              className="sr-only peer"
-            />
-            <div className="w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"></div>
-          </label>
-        </div>
-
-        {/* URL input */}
-        <div>
-          <label className="block text-sm text-bambu-gray mb-1">
-            Spoolman URL
-          </label>
-          <input
-            type="text"
-            placeholder="http://192.168.1.100:7912"
-            value={localUrl}
-            onChange={(e) => setLocalUrl(e.target.value)}
-            disabled={!localEnabled}
-            className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray/50 focus:border-bambu-green focus:outline-none disabled:opacity-50"
-          />
-          <p className="text-xs text-bambu-gray mt-1">
-            URL of your Spoolman server (e.g., http://localhost:7912)
-          </p>
-        </div>
+            {!localEnabled && (
+              <div className="flex items-center gap-1 mt-2">
+                <Check className="w-3 h-3 text-bambu-green" />
+                <span className="text-xs text-bambu-green">{t('common.enabled')}</span>
+              </div>
+            )}
+          </button>
 
-        {/* Sync mode */}
-        <div>
-          <label className="block text-sm text-bambu-gray mb-1">
-            Sync Mode
-          </label>
-          <select
-            value={localSyncMode}
-            onChange={(e) => setLocalSyncMode(e.target.value)}
-            disabled={!localEnabled}
-            className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none disabled:opacity-50"
+          {/* Spoolman */}
+          <button
+            type="button"
+            onClick={() => setLocalEnabled(true)}
+            className={`p-3 rounded-lg border-2 text-left transition-colors ${
+              localEnabled
+                ? 'border-bambu-green bg-bambu-green/10'
+                : 'border-bambu-dark-tertiary bg-bambu-dark hover:border-bambu-gray/50'
+            }`}
           >
-            <option value="auto">Automatic</option>
-            <option value="manual">Manual Only</option>
-          </select>
-          <p className="text-xs text-bambu-gray mt-1">
-            {localSyncMode === 'auto'
-              ? 'AMS data syncs automatically when changes are detected'
-              : 'Only sync when manually triggered'}
-          </p>
+            <div className="flex items-center gap-2 mb-1.5">
+              <ExternalLink className={`w-4 h-4 ${localEnabled ? 'text-bambu-green' : 'text-bambu-gray'}`} />
+              <span className={`text-sm font-medium ${localEnabled ? 'text-white' : 'text-bambu-gray'}`}>
+                Spoolman
+              </span>
+            </div>
+            <p className={`text-xs ${localEnabled ? 'text-bambu-gray' : 'text-bambu-gray/60'}`}>
+              {t('settings.trackingModeSpoolmanDesc')}
+            </p>
+            {localEnabled && (
+              <div className="flex items-center gap-1 mt-2">
+                <Check className="w-3 h-3 text-bambu-green" />
+                <span className="text-xs text-bambu-green">{t('common.enabled')}</span>
+              </div>
+            )}
+          </button>
         </div>
 
-        {/* Disable Weight Sync toggle - only show when sync mode is auto */}
-        {localSyncMode === 'auto' && (
-          <div className="flex items-center justify-between">
-            <div>
-              <p className="text-white">{t('spoolman.disableWeightSync')}</p>
-              <p className="text-sm text-bambu-gray">
-                {t('spoolman.disableWeightSyncDesc')}
-              </p>
+        {/* Built-in Inventory details */}
+        {!localEnabled && (
+          <div className="p-3 bg-bambu-green/5 border border-bambu-green/20 rounded-lg">
+            <div className="flex gap-2">
+              <Info className="w-4 h-4 text-bambu-green flex-shrink-0 mt-0.5" />
+              <div className="text-xs text-bambu-gray">
+                <ul className="list-disc list-inside space-y-0.5">
+                  <li>{t('settings.builtInFeatureRfid')}</li>
+                  <li>{t('settings.builtInFeatureUsage')}</li>
+                  <li>{t('settings.builtInFeatureCatalog')}</li>
+                </ul>
+              </div>
             </div>
-            <label className="relative inline-flex items-center cursor-pointer">
-              <input
-                type="checkbox"
-                checked={localDisableWeightSync}
-                onChange={(e) => setLocalDisableWeightSync(e.target.checked)}
-                disabled={!localEnabled}
-                className="sr-only peer"
-              />
-              <div className="w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"></div>
-            </label>
           </div>
         )}
 
-        {/* Report Partial Usage toggle - only show when weight sync is disabled */}
-        {localDisableWeightSync && (
-          <div className="flex items-center justify-between">
-            <div>
-              <p className="text-white">{t('spoolman.reportPartialUsage')}</p>
-              <p className="text-sm text-bambu-gray">
-                {t('spoolman.reportPartialUsageDesc')}
-              </p>
+        {/* Spoolman settings - only shown when Spoolman mode is selected */}
+        {localEnabled && (
+          <div className="space-y-4">
+            {/* Info banner about sync requirements */}
+            <div className="p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg">
+              <div className="flex gap-2">
+                <Info className="w-4 h-4 text-blue-400 flex-shrink-0 mt-0.5" />
+                <div className="text-xs text-blue-300">
+                  <p className="font-medium mb-1">{t('settings.howSyncWorks')}</p>
+                  <ul className="list-disc list-inside space-y-0.5 text-blue-300/80">
+                    <li>{t('settings.syncInfoRfidOnly')}</li>
+                    <li>{t('settings.syncInfoAutoCreate')}</li>
+                    <li>{t('settings.syncInfoThirdPartySkipped')}</li>
+                  </ul>
+                  <p className="font-medium mt-2 mb-1">{t('settings.linkingExistingSpools')}</p>
+                  <p className="text-blue-300/80">
+                    {t('settings.linkingExistingSpoolsDesc')}
+                  </p>
+                </div>
+              </div>
             </div>
-            <label className="relative inline-flex items-center cursor-pointer">
+
+            {/* URL input */}
+            <div>
+              <label className="block text-sm text-bambu-gray mb-1">
+                {t('settings.spoolmanUrl')}
+              </label>
               <input
-                type="checkbox"
-                checked={localReportPartialUsage}
-                onChange={(e) => setLocalReportPartialUsage(e.target.checked)}
-                disabled={!localEnabled}
-                className="sr-only peer"
+                type="text"
+                placeholder="http://192.168.1.100:7912"
+                value={localUrl}
+                onChange={(e) => setLocalUrl(e.target.value)}
+                className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray/50 focus:border-bambu-green focus:outline-none"
               />
-              <div className="w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"></div>
-            </label>
-          </div>
-        )}
+              <p className="text-xs text-bambu-gray mt-1">
+                {t('settings.spoolmanUrlHint')}
+              </p>
+            </div>
 
-        {/* Connection status */}
-        {localEnabled && (
-          <div className="pt-2 border-t border-bambu-dark-tertiary">
-            <div className="flex items-center justify-between mb-3">
-              <div className="flex items-center gap-2">
-                <span className="text-sm text-bambu-gray">Status:</span>
-                {statusLoading ? (
-                  <Loader2 className="w-4 h-4 text-bambu-gray animate-spin" />
-                ) : status?.connected ? (
-                  <span className="flex items-center gap-1 text-sm text-green-500">
-                    <Check className="w-4 h-4" />
-                    Connected
-                  </span>
-                ) : (
-                  <span className="flex items-center gap-1 text-sm text-red-500">
-                    <X className="w-4 h-4" />
-                    Disconnected
-                  </span>
-                )}
-              </div>
-              <div className="flex gap-2">
-                {status?.connected ? (
-                  <Button
-                    variant="secondary"
-                    size="sm"
-                    onClick={() => disconnectMutation.mutate()}
-                    disabled={disconnectMutation.isPending}
-                  >
-                    {disconnectMutation.isPending ? (
-                      <Loader2 className="w-4 h-4 animate-spin" />
-                    ) : (
-                      <Link2Off className="w-4 h-4" />
-                    )}
-                    Disconnect
-                  </Button>
-                ) : (
-                  <Button
-                    size="sm"
-                    onClick={() => connectMutation.mutate()}
-                    disabled={connectMutation.isPending || !localUrl}
-                  >
-                    {connectMutation.isPending ? (
-                      <Loader2 className="w-4 h-4 animate-spin" />
-                    ) : (
-                      <Link2 className="w-4 h-4" />
-                    )}
-                    Connect
-                  </Button>
-                )}
-              </div>
+            {/* Sync mode */}
+            <div>
+              <label className="block text-sm text-bambu-gray mb-1">
+                {t('settings.syncMode')}
+              </label>
+              <select
+                value={localSyncMode}
+                onChange={(e) => setLocalSyncMode(e.target.value)}
+                className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
+              >
+                <option value="auto">{t('settings.syncModeAuto')}</option>
+                <option value="manual">{t('settings.syncModeManual')}</option>
+              </select>
+              <p className="text-xs text-bambu-gray mt-1">
+                {localSyncMode === 'auto'
+                  ? t('settings.syncModeAutoDesc')
+                  : t('settings.syncModeManualDesc')}
+              </p>
             </div>
 
-            {/* Error display */}
-            {connectMutation.isError && (
-              <div className="mb-3 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400">
-                {(connectMutation.error as Error).message}
+            {/* Disable Weight Sync toggle - only show when sync mode is auto */}
+            {localSyncMode === 'auto' && (
+              <div className="flex items-center justify-between">
+                <div>
+                  <p className="text-white">{t('spoolman.disableWeightSync')}</p>
+                  <p className="text-sm text-bambu-gray">
+                    {t('spoolman.disableWeightSyncDesc')}
+                  </p>
+                </div>
+                <label className="relative inline-flex items-center cursor-pointer">
+                  <input
+                    type="checkbox"
+                    checked={localDisableWeightSync}
+                    onChange={(e) => setLocalDisableWeightSync(e.target.checked)}
+                    className="sr-only peer"
+                  />
+                  <div className="w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"></div>
+                </label>
               </div>
             )}
 
-            {/* Manual sync section */}
-            {status?.connected && (
-              <div className="space-y-3">
+            {/* Report Partial Usage toggle - only show when weight sync is disabled */}
+            {localDisableWeightSync && (
+              <div className="flex items-center justify-between">
                 <div>
-                  <p className="text-sm text-white">Sync AMS Data</p>
-                  <p className="text-xs text-bambu-gray">
-                    Manually sync printer AMS data to Spoolman
+                  <p className="text-white">{t('spoolman.reportPartialUsage')}</p>
+                  <p className="text-sm text-bambu-gray">
+                    {t('spoolman.reportPartialUsageDesc')}
                   </p>
                 </div>
+                <label className="relative inline-flex items-center cursor-pointer">
+                  <input
+                    type="checkbox"
+                    checked={localReportPartialUsage}
+                    onChange={(e) => setLocalReportPartialUsage(e.target.checked)}
+                    className="sr-only peer"
+                  />
+                  <div className="w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"></div>
+                </label>
+              </div>
+            )}
+
+            {/* Connection status */}
+            <div className="pt-2 border-t border-bambu-dark-tertiary">
+              <div className="flex items-center justify-between mb-3">
                 <div className="flex items-center gap-2">
-                  {/* Printer selector */}
-                  <div className="relative flex-1">
-                    <select
-                      value={selectedPrinterId}
-                      onChange={(e) => setSelectedPrinterId(e.target.value === 'all' ? 'all' : Number(e.target.value))}
-                      className="w-full px-3 py-2 pr-8 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none appearance-none cursor-pointer"
+                  <span className="text-sm text-bambu-gray">{t('settings.status')}:</span>
+                  {statusLoading ? (
+                    <Loader2 className="w-4 h-4 text-bambu-gray animate-spin" />
+                  ) : status?.connected ? (
+                    <span className="flex items-center gap-1 text-sm text-green-500">
+                      <Check className="w-4 h-4" />
+                      {t('settings.spoolmanConnected')}
+                    </span>
+                  ) : (
+                    <span className="flex items-center gap-1 text-sm text-red-500">
+                      <X className="w-4 h-4" />
+                      {t('settings.spoolmanDisconnected')}
+                    </span>
+                  )}
+                </div>
+                <div className="flex gap-2">
+                  {status?.connected ? (
+                    <Button
+                      variant="secondary"
+                      size="sm"
+                      onClick={() => disconnectMutation.mutate()}
+                      disabled={disconnectMutation.isPending}
                     >
-                      <option value="all">All Printers</option>
-                      {printers?.map((printer: Printer) => (
-                        <option key={printer.id} value={printer.id}>
-                          {printer.name}
-                        </option>
-                      ))}
-                    </select>
-                    <ChevronDown className="absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
-                  </div>
-                  {/* Sync button */}
-                  <Button
-                    variant="secondary"
-                    size="sm"
-                    onClick={handleSync}
-                    disabled={isSyncing}
-                  >
-                    {isSyncing ? (
-                      <Loader2 className="w-4 h-4 animate-spin" />
-                    ) : (
-                      <RefreshCw className="w-4 h-4" />
-                    )}
-                    Sync
-                  </Button>
+                      {disconnectMutation.isPending ? (
+                        <Loader2 className="w-4 h-4 animate-spin" />
+                      ) : (
+                        <Link2Off className="w-4 h-4" />
+                      )}
+                      {t('settings.disconnect')}
+                    </Button>
+                  ) : (
+                    <Button
+                      size="sm"
+                      onClick={() => connectMutation.mutate()}
+                      disabled={connectMutation.isPending || !localUrl}
+                    >
+                      {connectMutation.isPending ? (
+                        <Loader2 className="w-4 h-4 animate-spin" />
+                      ) : (
+                        <Link2 className="w-4 h-4" />
+                      )}
+                      {t('settings.connect')}
+                    </Button>
+                  )}
                 </div>
               </div>
-            )}
 
-            {/* Sync result */}
-            {syncSuccess && syncResult && (
-              <div className="mt-3 space-y-2">
-                {/* Main result */}
-                <div
-                  className={`p-2 rounded text-sm ${
-                    syncResult.success
-                      ? 'bg-green-500/20 border border-green-500/50 text-green-400'
-                      : 'bg-yellow-500/20 border border-yellow-500/50 text-yellow-400'
-                  }`}
-                >
-                  {syncResult.success
-                    ? `Synced ${syncResult.synced_count} spool${syncResult.synced_count !== 1 ? 's' : ''} successfully`
-                    : `Synced ${syncResult.synced_count} spool${syncResult.synced_count !== 1 ? 's' : ''} with ${syncResult.errors.length} error${syncResult.errors.length !== 1 ? 's' : ''}`}
+              {/* Error display */}
+              {connectMutation.isError && (
+                <div className="mb-3 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400">
+                  {(connectMutation.error as Error).message}
                 </div>
+              )}
 
-                {/* Skipped spools */}
-                {syncResult.skipped_count > 0 && (
-                  <div className="p-2 bg-amber-500/10 border border-amber-500/30 rounded text-sm">
-                    <div className="flex items-center justify-between text-amber-400 mb-1">
-                      <div className="flex items-center gap-1.5">
-                        <AlertTriangle className="w-3.5 h-3.5" />
-                        <span className="font-medium">
-                          {syncResult.skipped_count} spool{syncResult.skipped_count !== 1 ? 's' : ''} skipped
-                        </span>
-                      </div>
-                      {syncResult.skipped_count > 5 && (
-                        <button
-                          onClick={() => setShowAllSkipped(!showAllSkipped)}
-                          className="text-xs text-amber-400 hover:text-amber-300 underline"
-                        >
-                          {showAllSkipped ? 'Show less' : 'Show all'}
-                        </button>
-                      )}
+              {/* Manual sync section */}
+              {status?.connected && (
+                <div className="space-y-3">
+                  <div>
+                    <p className="text-sm text-white">{t('settings.syncAmsData')}</p>
+                    <p className="text-xs text-bambu-gray">
+                      {t('settings.syncAmsDataDesc')}
+                    </p>
+                  </div>
+                  <div className="flex items-center gap-2">
+                    {/* Printer selector */}
+                    <div className="relative flex-1">
+                      <select
+                        value={selectedPrinterId}
+                        onChange={(e) => setSelectedPrinterId(e.target.value === 'all' ? 'all' : Number(e.target.value))}
+                        className="w-full px-3 py-2 pr-8 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none appearance-none cursor-pointer"
+                      >
+                        <option value="all">{t('settings.allPrinters')}</option>
+                        {printers?.map((printer: Printer) => (
+                          <option key={printer.id} value={printer.id}>
+                            {printer.name}
+                          </option>
+                        ))}
+                      </select>
+                      <ChevronDown className="absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
                     </div>
-                    <ul className="text-xs text-amber-300/80 space-y-0.5">
-                      {(showAllSkipped ? syncResult.skipped : syncResult.skipped.slice(0, 5)).map((s, i) => (
-                        <li key={i} className="flex items-center gap-2">
-                          {s.color && (
-                            <span
-                              className="w-3 h-3 rounded-full border border-white/20"
-                              style={{ backgroundColor: `#${s.color}` }}
-                            />
-                          )}
-                          <span>{s.location}</span>
-                          <span className="text-amber-300/60">- {s.reason}</span>
-                        </li>
-                      ))}
-                      {!showAllSkipped && syncResult.skipped_count > 5 && (
-                        <li className="text-amber-300/60 italic">
-                          ...and {syncResult.skipped_count - 5} more
-                        </li>
+                    {/* Sync button */}
+                    <Button
+                      variant="secondary"
+                      size="sm"
+                      onClick={handleSync}
+                      disabled={isSyncing}
+                    >
+                      {isSyncing ? (
+                        <Loader2 className="w-4 h-4 animate-spin" />
+                      ) : (
+                        <RefreshCw className="w-4 h-4" />
                       )}
-                    </ul>
+                      {t('spoolman.sync')}
+                    </Button>
                   </div>
-                )}
+                </div>
+              )}
 
-                {/* Errors */}
-                {syncResult.errors.length > 0 && (
-                  <div className="p-2 bg-red-500/10 border border-red-500/30 rounded text-sm">
-                    <div className="text-red-400 font-medium mb-1">Errors:</div>
-                    <ul className="text-xs text-red-300/80 space-y-0.5">
-                      {syncResult.errors.map((err, i) => (
-                        <li key={i}>{err}</li>
-                      ))}
-                    </ul>
+              {/* Sync result */}
+              {syncSuccess && syncResult && (
+                <div className="mt-3 space-y-2">
+                  {/* Main result */}
+                  <div
+                    className={`p-2 rounded text-sm ${
+                      syncResult.success
+                        ? 'bg-green-500/20 border border-green-500/50 text-green-400'
+                        : 'bg-yellow-500/20 border border-yellow-500/50 text-yellow-400'
+                    }`}
+                  >
+                    {syncResult.success
+                      ? `Synced ${syncResult.synced_count} spool${syncResult.synced_count !== 1 ? 's' : ''} successfully`
+                      : `Synced ${syncResult.synced_count} spool${syncResult.synced_count !== 1 ? 's' : ''} with ${syncResult.errors.length} error${syncResult.errors.length !== 1 ? 's' : ''}`}
                   </div>
-                )}
-              </div>
-            )}
+
+                  {/* Skipped spools */}
+                  {syncResult.skipped_count > 0 && (
+                    <div className="p-2 bg-amber-500/10 border border-amber-500/30 rounded text-sm">
+                      <div className="flex items-center justify-between text-amber-400 mb-1">
+                        <div className="flex items-center gap-1.5">
+                          <AlertTriangle className="w-3.5 h-3.5" />
+                          <span className="font-medium">
+                            {syncResult.skipped_count} spool{syncResult.skipped_count !== 1 ? 's' : ''} skipped
+                          </span>
+                        </div>
+                        {syncResult.skipped_count > 5 && (
+                          <button
+                            onClick={() => setShowAllSkipped(!showAllSkipped)}
+                            className="text-xs text-amber-400 hover:text-amber-300 underline"
+                          >
+                            {showAllSkipped ? 'Show less' : 'Show all'}
+                          </button>
+                        )}
+                      </div>
+                      <ul className="text-xs text-amber-300/80 space-y-0.5">
+                        {(showAllSkipped ? syncResult.skipped : syncResult.skipped.slice(0, 5)).map((s, i) => (
+                          <li key={i} className="flex items-center gap-2">
+                            {s.color && (
+                              <span
+                                className="w-3 h-3 rounded-full border border-white/20"
+                                style={{ backgroundColor: `#${s.color}` }}
+                              />
+                            )}
+                            <span>{s.location}</span>
+                            <span className="text-amber-300/60">- {s.reason}</span>
+                          </li>
+                        ))}
+                        {!showAllSkipped && syncResult.skipped_count > 5 && (
+                          <li className="text-amber-300/60 italic">
+                            ...and {syncResult.skipped_count - 5} more
+                          </li>
+                        )}
+                      </ul>
+                    </div>
+                  )}
+
+                  {/* Errors */}
+                  {syncResult.errors.length > 0 && (
+                    <div className="p-2 bg-red-500/10 border border-red-500/30 rounded text-sm">
+                      <div className="text-red-400 font-medium mb-1">Errors:</div>
+                      <ul className="text-xs text-red-300/80 space-y-0.5">
+                        {syncResult.errors.map((err, i) => (
+                          <li key={i}>{err}</li>
+                        ))}
+                      </ul>
+                    </div>
+                  )}
+                </div>
+              )}
+            </div>
           </div>
         )}
       </CardContent>

+ 228 - 0
frontend/src/components/spool-form/AdditionalSection.tsx

@@ -0,0 +1,228 @@
+import { useState, useRef, useEffect, useMemo } from 'react';
+import { Scale } from 'lucide-react';
+import { useTranslation } from 'react-i18next';
+import { useToast } from '../../contexts/ToastContext';
+import type { AdditionalSectionProps } from './types';
+
+function SpoolWeightPicker({
+  catalog,
+  value,
+  onChange,
+}: {
+  catalog: { id: number; name: string; weight: number }[];
+  value: number;
+  onChange: (weight: number) => void;
+}) {
+  const { t } = useTranslation();
+  const [isOpen, setIsOpen] = useState(false);
+  const [search, setSearch] = useState('');
+  const [selectedId, setSelectedId] = useState<number | null>(null);
+  const dropdownRef = useRef<HTMLDivElement>(null);
+  const inputRef = useRef<HTMLInputElement>(null);
+
+  useEffect(() => {
+    const handleClick = (e: MouseEvent) => {
+      if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
+        setIsOpen(false);
+      }
+    };
+    document.addEventListener('mousedown', handleClick);
+    return () => document.removeEventListener('mousedown', handleClick);
+  }, []);
+
+  const filtered = useMemo(() => {
+    if (!search) return catalog;
+    const s = search.toLowerCase();
+    return catalog.filter(e =>
+      e.name.toLowerCase().includes(s) ||
+      e.weight.toString().includes(s),
+    );
+  }, [catalog, search]);
+
+  // Display value: show catalog name if selected, or the weight
+  const displayValue = useMemo(() => {
+    if (isOpen) return search;
+    if (selectedId) {
+      const entry = catalog.find(e => e.id === selectedId);
+      if (entry) return entry.name;
+    }
+    const match = catalog.find(e => e.weight === value);
+    if (match) return match.name;
+    return '';
+  }, [isOpen, search, selectedId, catalog, value]);
+
+  return (
+    <div>
+      <label className="block text-sm font-medium text-bambu-gray mb-1">
+        <span className="flex items-center gap-2">
+          <Scale className="w-3.5 h-3.5 text-bambu-gray" />
+          {t('inventory.coreWeight')}
+        </span>
+      </label>
+      <div className="flex gap-2 items-center">
+        <div className="flex-1 min-w-0 relative" ref={dropdownRef}>
+          <input
+            ref={inputRef}
+            type="text"
+            className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"
+            placeholder={t('inventory.searchSpoolWeight')}
+            value={displayValue}
+            onFocus={() => {
+              setIsOpen(true);
+              setSearch('');
+            }}
+            onChange={(e) => {
+              setSearch(e.target.value);
+              setIsOpen(true);
+            }}
+          />
+          {isOpen && (
+            <div className="absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-64 overflow-y-auto">
+              {filtered.length === 0 ? (
+                <div className="px-3 py-2 text-sm text-bambu-gray">{t('inventory.noResults')}</div>
+              ) : (
+                filtered.map(entry => (
+                  <button
+                    key={entry.id}
+                    type="button"
+                    className={`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex justify-between items-center ${
+                      (selectedId ? entry.id === selectedId : entry.weight === value)
+                        ? 'bg-bambu-green/10 text-bambu-green'
+                        : 'text-white'
+                    }`}
+                    onClick={() => {
+                      setSelectedId(entry.id);
+                      onChange(entry.weight);
+                      setIsOpen(false);
+                      setSearch('');
+                    }}
+                  >
+                    <span className="truncate">{entry.name}</span>
+                    <span className="font-mono text-xs text-bambu-gray ml-2 shrink-0">{entry.weight}g</span>
+                  </button>
+                ))
+              )}
+            </div>
+          )}
+        </div>
+        <div className="flex items-center gap-1 shrink-0">
+          <input
+            type="number"
+            className="w-16 px-2 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm text-center font-mono focus:outline-none focus:border-bambu-green"
+            value={value}
+            min={0}
+            max={2000}
+            onChange={(e) => {
+              const val = parseInt(e.target.value);
+              if (!isNaN(val) && val >= 0) onChange(val);
+            }}
+          />
+          <span className="text-bambu-gray text-sm">g</span>
+        </div>
+      </div>
+    </div>
+  );
+}
+
+export function AdditionalSection({
+  formData,
+  updateField,
+  spoolCatalog,
+}: AdditionalSectionProps) {
+  const { t } = useTranslation();
+  const { showToast } = useToast();
+  const [measuredInput, setMeasuredInput] = useState('');
+  const [isMeasuredFocused, setIsMeasuredFocused] = useState(false);
+
+  const remainingWeight = Math.max(0, formData.label_weight - formData.weight_used);
+  const measuredDefault = formData.core_weight + remainingWeight;
+
+  useEffect(() => {
+    if (!isMeasuredFocused) {
+      setMeasuredInput(String(measuredDefault));
+    }
+  }, [isMeasuredFocused, measuredDefault]);
+
+  return (
+    <div className="space-y-4">
+      {/* Empty Spool Weight */}
+      <SpoolWeightPicker
+        catalog={spoolCatalog}
+        value={formData.core_weight}
+        onChange={(weight) => updateField('core_weight', weight)}
+      />
+
+      {/* Current Weight (remaining filament) */}
+      <div>
+        <label className="block text-sm font-medium text-bambu-gray mb-1">{t('inventory.currentWeight')}</label>
+        <div className="flex items-center gap-2">
+          <div className="relative flex-1">
+            <input
+              type="number"
+              value={remainingWeight}
+              min={0}
+              max={formData.label_weight}
+              onChange={(e) => {
+                const remaining = parseInt(e.target.value) || 0;
+                updateField('weight_used', Math.max(0, formData.label_weight - remaining));
+              }}
+              className="w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"
+            />
+            <span className="absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray">g</span>
+          </div>
+          <span className="text-xs text-bambu-gray shrink-0">/ {formData.label_weight}g</span>
+        </div>
+      </div>
+
+      {/* Measured Weight (empty spool + remaining filament) */}
+      <div>
+        <label className="block text-sm font-medium text-bambu-gray mb-1">{t('inventory.measuredWeight')}</label>
+        <div className="flex items-center gap-2">
+          <div className="relative flex-1">
+            <input
+              type="number"
+              value={measuredInput}
+              min={0}
+              onFocus={() => setIsMeasuredFocused(true)}
+              onChange={(e) => {
+                setMeasuredInput(e.target.value);
+              }}
+              onBlur={() => {
+                setIsMeasuredFocused(false);
+                const raw = measuredInput.trim();
+                const measured = Number(raw);
+                const minAllowed = formData.core_weight;
+                const maxAllowed = formData.core_weight + formData.label_weight;
+
+                if (!raw || !Number.isFinite(measured) || measured < minAllowed || measured > maxAllowed) {
+                  showToast(t('inventory.measuredWeightError', { min: minAllowed, max: maxAllowed }), 'error');
+                  setMeasuredInput(String(measuredDefault));
+                  return;
+                }
+
+                const rounded = Math.round(measured);
+                const remaining = Math.max(0, Math.min(formData.label_weight, rounded - formData.core_weight));
+                updateField('weight_used', Math.max(0, formData.label_weight - remaining));
+                setMeasuredInput(String(rounded));
+              }}
+              className="w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"
+            />
+            <span className="absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray">g</span>
+          </div>
+          <span className="text-xs text-bambu-gray shrink-0">/ {formData.core_weight + formData.label_weight}g</span>
+        </div>
+      </div>
+
+      {/* Note */}
+      <div>
+        <label className="block text-sm font-medium text-bambu-gray mb-1">{t('inventory.note')}</label>
+        <textarea
+          className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green resize-none min-h-[80px]"
+          placeholder={t('inventory.notePlaceholder')}
+          value={formData.note}
+          onChange={(e) => updateField('note', e.target.value)}
+        />
+      </div>
+    </div>
+  );
+}

+ 286 - 0
frontend/src/components/spool-form/ColorSection.tsx

@@ -0,0 +1,286 @@
+import { useState, useMemo } from 'react';
+import { Search, Clock, ChevronDown, ChevronUp } from 'lucide-react';
+import { useTranslation } from 'react-i18next';
+import type { ColorSectionProps } from './types';
+import { QUICK_COLORS, ALL_COLORS } from './constants';
+
+export function ColorSection({
+  formData,
+  updateField,
+  recentColors,
+  onColorUsed,
+  catalogColors,
+}: ColorSectionProps) {
+  const { t } = useTranslation();
+  const [showAllColors, setShowAllColors] = useState(false);
+  const [colorSearch, setColorSearch] = useState('');
+
+  // Current hex without # prefix
+  const currentHex = formData.rgba.replace('#', '').substring(0, 6);
+
+  const isSelected = (hex: string) => {
+    return currentHex.toUpperCase() === hex.toUpperCase();
+  };
+
+  const selectColor = (hex: string, name: string) => {
+    // Store as RRGGBBAA (with FF alpha)
+    updateField('rgba', hex.toUpperCase() + 'FF');
+    updateField('color_name', name);
+    onColorUsed({ name, hex });
+  };
+
+  // Filter catalog colors by the selected brand + material + subtype
+  // Brand matching is word-based: "mz - Bambu" matches "Bambu Lab" because both contain "Bambu"
+  // Material matching: try exact "PETG Basic" first, fall back to base material "PETG" prefix
+  const matchedCatalogColors = useMemo(() => {
+    if (catalogColors.length === 0) return [];
+    const brand = formData.brand?.trim();
+    const material = formData.material?.toLowerCase().trim();
+    const subtype = formData.subtype?.toLowerCase().trim();
+    if (!brand && !material) return [];
+
+    // Split brand into words (>= 2 chars) for word-based matching
+    const brandWords = brand
+      ? brand.toLowerCase().split(/[\s\-_]+/).filter(w => w.length >= 2)
+      : [];
+
+    const brandMatches = (manufacturer: string) => {
+      if (brandWords.length === 0) return true; // no brand filter
+      const mfrLower = manufacturer.toLowerCase();
+      // Any significant brand word found in manufacturer name
+      return brandWords.some(w => mfrLower.includes(w));
+    };
+
+    // Build the combined material+subtype string to match catalog entries
+    const fullMaterial = material && subtype ? `${material} ${subtype}` : '';
+
+    // First pass: try exact fullMaterial match (e.g. "PETG Basic")
+    if (fullMaterial) {
+      const exact = catalogColors.filter(c =>
+        brandMatches(c.manufacturer) &&
+        c.material?.toLowerCase() === fullMaterial,
+      );
+      if (exact.length > 0) {
+        return exact.map(c => ({
+          name: c.color_name,
+          hex: c.hex_color.replace('#', '').substring(0, 6),
+        }));
+      }
+      // Try without trailing "+" (e.g. "PLA Silk+" -> "PLA Silk")
+      const normalized = fullMaterial.replace(/\+$/, '');
+      if (normalized !== fullMaterial) {
+        const normMatch = catalogColors.filter(c =>
+          brandMatches(c.manufacturer) &&
+          c.material?.toLowerCase() === normalized,
+        );
+        if (normMatch.length > 0) {
+          return normMatch.map(c => ({
+            name: c.color_name,
+            hex: c.hex_color.replace('#', '').substring(0, 6),
+          }));
+        }
+      }
+    }
+
+    // Second pass: match base material prefix (e.g. "PETG" matches "PETG Basic", "PETG-HF")
+    if (material) {
+      const byMaterial = catalogColors.filter(c =>
+        brandMatches(c.manufacturer) &&
+        (!c.material || c.material.toLowerCase().startsWith(material)),
+      );
+      if (byMaterial.length > 0) {
+        return byMaterial.map(c => ({
+          name: c.color_name,
+          hex: c.hex_color.replace('#', '').substring(0, 6),
+        }));
+      }
+    }
+
+    return [];
+  }, [catalogColors, formData.brand, formData.material, formData.subtype]);
+
+  const hasCatalogMatch = matchedCatalogColors.length > 0;
+
+  // Search within matched catalog colors
+  const filteredCatalogColors = useMemo(() => {
+    if (!colorSearch) return matchedCatalogColors;
+    const q = colorSearch.toLowerCase();
+    return matchedCatalogColors.filter(c => c.name.toLowerCase().includes(q));
+  }, [matchedCatalogColors, colorSearch]);
+
+  // Fallback hardcoded colors for search/expand
+  const filteredFallbackColors = useMemo(() => {
+    if (colorSearch) {
+      return ALL_COLORS.filter(c =>
+        c.name.toLowerCase().includes(colorSearch.toLowerCase()),
+      );
+    }
+    return showAllColors ? ALL_COLORS : QUICK_COLORS;
+  }, [colorSearch, showAllColors]);
+
+  return (
+    <div className="space-y-3">
+      {/* Color preview banner */}
+      <div
+        className="h-10 rounded-lg border border-bambu-dark-tertiary"
+        style={{ backgroundColor: `#${currentHex}` }}
+      />
+
+      {/* Recently Used Colors */}
+      {recentColors.length > 0 && (
+        <div className="flex items-center gap-2">
+          <div className="flex items-center gap-1.5 text-xs text-bambu-gray shrink-0">
+            <Clock className="w-3 h-3" />
+            <span>{t('inventory.recentColors')}</span>
+          </div>
+          <div className="flex flex-wrap gap-1.5">
+            {recentColors.map(color => (
+              <button
+                key={color.hex}
+                type="button"
+                onClick={() => selectColor(color.hex, color.name)}
+                className={`w-6 h-6 rounded border-2 transition-all hover:scale-110 ${
+                  isSelected(color.hex)
+                    ? 'border-bambu-green ring-1 ring-bambu-green/30 scale-110'
+                    : 'border-bambu-dark-tertiary'
+                }`}
+                style={{ backgroundColor: `#${color.hex}` }}
+                title={color.name}
+              />
+            ))}
+          </div>
+        </div>
+      )}
+
+      {/* Color Search */}
+      <div className="relative">
+        <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none" />
+        <input
+          type="text"
+          className="w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"
+          placeholder={t('inventory.searchColors')}
+          value={colorSearch}
+          onChange={(e) => setColorSearch(e.target.value)}
+        />
+      </div>
+
+      {/* Color Swatches */}
+      {hasCatalogMatch ? (
+        /* Catalog colors matching selected brand/material */
+        <div className="space-y-1.5">
+          <span className="text-xs text-bambu-gray">
+            {colorSearch ? t('inventory.searchResults') : `${formData.brand}${formData.material ? ` ${formData.material}` : ''}`}
+          </span>
+          <div className="flex flex-wrap gap-1.5">
+            {filteredCatalogColors.map(color => (
+              <button
+                key={`${color.hex}-${color.name}`}
+                type="button"
+                onClick={() => selectColor(color.hex, color.name)}
+                className={`w-6 h-6 rounded border-2 transition-all hover:scale-110 relative group ${
+                  isSelected(color.hex)
+                    ? 'border-bambu-green ring-1 ring-bambu-green/30 scale-110'
+                    : 'border-bambu-dark-tertiary'
+                }`}
+                style={{ backgroundColor: `#${color.hex}` }}
+                title={color.name}
+              >
+                <span className="absolute -bottom-7 left-1/2 -translate-x-1/2 px-2 py-0.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-xs whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none z-10 shadow-lg text-white">
+                  {color.name}
+                </span>
+              </button>
+            ))}
+            {filteredCatalogColors.length === 0 && (
+              <p className="text-sm text-bambu-gray py-1">{t('inventory.noColorsFound')}</p>
+            )}
+          </div>
+        </div>
+      ) : (
+        /* Fallback: hardcoded color palette (no brand/material selected or no catalog matches) */
+        <div className="space-y-1.5">
+          <div className="flex items-center justify-between text-xs text-bambu-gray">
+            <span>{colorSearch ? t('inventory.searchResults') : (showAllColors ? t('inventory.allColors') : t('inventory.commonColors'))}</span>
+            {!colorSearch && (
+              <button
+                type="button"
+                onClick={() => setShowAllColors(!showAllColors)}
+                className="flex items-center gap-1 hover:text-white transition-colors"
+              >
+                {showAllColors ? (
+                  <>{t('inventory.showLess')} <ChevronUp className="w-3 h-3" /></>
+                ) : (
+                  <>{t('inventory.showAll')} <ChevronDown className="w-3 h-3" /></>
+                )}
+              </button>
+            )}
+          </div>
+          <div className="flex flex-wrap gap-1.5">
+            {filteredFallbackColors.map(color => (
+              <button
+                key={color.hex}
+                type="button"
+                onClick={() => selectColor(color.hex, color.name)}
+                className={`w-6 h-6 rounded border-2 transition-all hover:scale-110 relative group ${
+                  isSelected(color.hex)
+                    ? 'border-bambu-green ring-1 ring-bambu-green/30 scale-110'
+                    : 'border-bambu-dark-tertiary'
+                }`}
+                style={{ backgroundColor: `#${color.hex}` }}
+                title={color.name}
+              >
+                <span className="absolute -bottom-7 left-1/2 -translate-x-1/2 px-2 py-0.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-xs whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none z-10 shadow-lg text-white">
+                  {color.name}
+                </span>
+              </button>
+            ))}
+            {filteredFallbackColors.length === 0 && (
+              <p className="text-sm text-bambu-gray py-1">{t('inventory.noColorsFound')}</p>
+            )}
+          </div>
+        </div>
+      )}
+
+      {/* Manual Color Input */}
+      <div className="grid grid-cols-2 gap-3">
+        <div>
+          <label className="block text-sm font-medium text-bambu-gray mb-1">{t('inventory.colorName')}</label>
+          <input
+            type="text"
+            className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"
+            placeholder={t('inventory.colorNamePlaceholder')}
+            value={formData.color_name}
+            onChange={(e) => updateField('color_name', e.target.value)}
+          />
+        </div>
+        <div>
+          <label className="block text-sm font-medium text-bambu-gray mb-1">{t('inventory.hexColor')}</label>
+          <div className="flex gap-2">
+            <div className="relative flex-1">
+              <span className="absolute left-3 top-1/2 -translate-y-1/2 text-bambu-gray">#</span>
+              <input
+                type="text"
+                className="w-full pl-7 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm font-mono uppercase focus:outline-none focus:border-bambu-green"
+                placeholder="RRGGBB"
+                value={currentHex.toUpperCase()}
+                onChange={(e) => {
+                  const val = e.target.value.replace('#', '').replace(/[^0-9A-Fa-f]/g, '');
+                  if (val.length <= 8) updateField('rgba', val.toUpperCase() + (val.length <= 6 ? 'FF' : ''));
+                }}
+              />
+            </div>
+            <input
+              type="color"
+              className="w-11 h-[38px] rounded-lg cursor-pointer border border-bambu-dark-tertiary shrink-0 bg-transparent"
+              value={`#${currentHex}`}
+              onChange={(e) => {
+                const hex = e.target.value.replace('#', '').toUpperCase();
+                updateField('rgba', hex + 'FF');
+              }}
+              title={t('inventory.pickColor')}
+            />
+          </div>
+        </div>
+      </div>
+    </div>
+  );
+}

+ 296 - 0
frontend/src/components/spool-form/FilamentSection.tsx

@@ -0,0 +1,296 @@
+import { useState, useRef, useEffect, useMemo } from 'react';
+import { Search, Loader2, ChevronDown, Cloud, CloudOff } from 'lucide-react';
+import { useTranslation } from 'react-i18next';
+import type { FilamentSectionProps, FilamentOption } from './types';
+import { MATERIALS, KNOWN_VARIANTS } from './constants';
+import { parsePresetName } from './utils';
+
+export function FilamentSection({
+  formData,
+  updateField,
+  cloudAuthenticated,
+  loadingCloudPresets,
+  presetInputValue,
+  setPresetInputValue,
+  selectedPresetOption,
+  filamentOptions,
+  availableBrands,
+}: FilamentSectionProps) {
+  const { t } = useTranslation();
+  const [presetDropdownOpen, setPresetDropdownOpen] = useState(false);
+  const [brandDropdownOpen, setBrandDropdownOpen] = useState(false);
+  const [subtypeDropdownOpen, setSubtypeDropdownOpen] = useState(false);
+  const [brandSearch, setBrandSearch] = useState('');
+  const [subtypeSearch, setSubtypeSearch] = useState('');
+  const presetRef = useRef<HTMLDivElement>(null);
+  const brandRef = useRef<HTMLDivElement>(null);
+  const subtypeRef = useRef<HTMLDivElement>(null);
+
+  // Close dropdowns on outside click
+  useEffect(() => {
+    const handleClick = (e: MouseEvent) => {
+      if (presetRef.current && !presetRef.current.contains(e.target as Node)) {
+        setPresetDropdownOpen(false);
+      }
+      if (brandRef.current && !brandRef.current.contains(e.target as Node)) {
+        setBrandDropdownOpen(false);
+      }
+      if (subtypeRef.current && !subtypeRef.current.contains(e.target as Node)) {
+        setSubtypeDropdownOpen(false);
+      }
+    };
+    document.addEventListener('mousedown', handleClick);
+    return () => document.removeEventListener('mousedown', handleClick);
+  }, []);
+
+  // Filtered presets based on search
+  const filteredPresets = useMemo(() => {
+    if (!presetInputValue) return filamentOptions;
+    const search = presetInputValue.toLowerCase();
+    return filamentOptions.filter(o =>
+      o.displayName.toLowerCase().includes(search) ||
+      o.code.toLowerCase().includes(search),
+    );
+  }, [filamentOptions, presetInputValue]);
+
+  // Filtered brands
+  const filteredBrands = useMemo(() => {
+    if (!brandSearch) return availableBrands;
+    const search = brandSearch.toLowerCase();
+    return availableBrands.filter(b => b.toLowerCase().includes(search));
+  }, [availableBrands, brandSearch]);
+
+  const filteredVariants = useMemo(() => {
+    if (!subtypeSearch) return KNOWN_VARIANTS;
+    const search = subtypeSearch.toLowerCase();
+    return KNOWN_VARIANTS.filter(v => v.toLowerCase().includes(search));
+  }, [subtypeSearch]);
+
+  // Handle preset selection
+  const handlePresetSelect = (option: FilamentOption) => {
+    updateField('slicer_filament', option.code);
+    setPresetInputValue(option.displayName);
+    setPresetDropdownOpen(false);
+
+    // Auto-fill material, brand, subtype from preset name
+    const parsed = parsePresetName(option.name);
+    if (parsed.material) updateField('material', parsed.material);
+    if (parsed.brand) updateField('brand', parsed.brand);
+    if (parsed.variant) updateField('subtype', parsed.variant);
+  };
+
+  return (
+    <div className="space-y-4">
+      {/* Cloud status indicator */}
+      <div className="flex items-center gap-2 text-xs text-bambu-gray">
+        {loadingCloudPresets ? (
+          <><Loader2 className="w-3 h-3 animate-spin" /> {t('inventory.loadingPresets')}</>
+        ) : cloudAuthenticated ? (
+          <><Cloud className="w-3 h-3 text-bambu-green" /> {t('inventory.cloudConnected')}</>
+        ) : (
+          <><CloudOff className="w-3 h-3" /> {t('inventory.cloudNotConnected')}</>
+        )}
+      </div>
+
+      {/* Slicer Preset (autocomplete) */}
+      <div>
+        <label className="block text-sm font-medium text-bambu-gray mb-1">
+          {t('inventory.slicerPreset')} *
+        </label>
+        <div className="relative" ref={presetRef}>
+          <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none" />
+          <input
+            type="text"
+            className="w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"
+            placeholder={t('inventory.searchPresets')}
+            value={presetInputValue}
+            onChange={(e) => {
+              setPresetInputValue(e.target.value);
+              setPresetDropdownOpen(true);
+            }}
+            onFocus={() => {
+              setPresetDropdownOpen(true);
+              setPresetInputValue('');
+            }}
+          />
+          {presetDropdownOpen && (
+            <div className="absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-64 overflow-y-auto">
+              {filteredPresets.length === 0 ? (
+                <div className="px-3 py-2 text-sm text-bambu-gray">{t('inventory.noPresetsFound')}</div>
+              ) : (
+                filteredPresets.map(option => (
+                  <button
+                    key={option.code}
+                    type="button"
+                    className={`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex justify-between items-center ${
+                      selectedPresetOption?.code === option.code
+                        ? 'bg-bambu-green/10 text-bambu-green'
+                        : 'text-white'
+                    }`}
+                    onClick={() => handlePresetSelect(option)}
+                  >
+                    <span className="truncate">{option.displayName}</span>
+                    <span className="font-mono text-xs text-bambu-gray ml-2 shrink-0">{option.code}</span>
+                  </button>
+                ))
+              )}
+            </div>
+          )}
+        </div>
+        {selectedPresetOption && (
+          <div className="mt-1 text-xs text-bambu-gray">
+            {t('inventory.selectedPreset')}: <span className="font-mono text-bambu-green">{selectedPresetOption.code}</span>
+          </div>
+        )}
+      </div>
+
+      {/* Material */}
+      <div>
+        <label className="block text-sm font-medium text-bambu-gray mb-1">{t('inventory.material')} *</label>
+        <select
+          value={formData.material}
+          onChange={(e) => updateField('material', e.target.value)}
+          className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"
+        >
+          <option value="">{t('inventory.selectMaterial')}</option>
+          {MATERIALS.map((m) => (
+            <option key={m} value={m}>{m}</option>
+          ))}
+        </select>
+      </div>
+
+      {/* Brand (dropdown with search) */}
+      <div>
+        <label className="block text-sm font-medium text-bambu-gray mb-1">{t('inventory.brand')}</label>
+        <div className="relative" ref={brandRef}>
+          <input
+            type="text"
+            className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"
+            placeholder={t('inventory.searchBrand')}
+            value={brandDropdownOpen ? brandSearch : formData.brand}
+            onChange={(e) => {
+              setBrandSearch(e.target.value);
+              setBrandDropdownOpen(true);
+            }}
+            onFocus={() => {
+              setBrandDropdownOpen(true);
+              setBrandSearch('');
+            }}
+          />
+          <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none" />
+          {brandDropdownOpen && (
+            <div className="absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto">
+              {filteredBrands.length === 0 ? (
+                <div className="px-3 py-2 text-sm text-bambu-gray">{t('inventory.noResults')}</div>
+              ) : (
+                filteredBrands.map(brand => (
+                  <button
+                    key={brand}
+                    type="button"
+                    className={`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${
+                      formData.brand === brand ? 'bg-bambu-green/10 text-bambu-green' : 'text-white'
+                    }`}
+                    onClick={() => {
+                      updateField('brand', brand);
+                      setBrandDropdownOpen(false);
+                      setBrandSearch('');
+                    }}
+                  >
+                    {brand}
+                  </button>
+                ))
+              )}
+              {/* Allow custom brand */}
+              {brandSearch && !filteredBrands.includes(brandSearch) && (
+                <button
+                  type="button"
+                  className="w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary text-bambu-green border-t border-bambu-dark-tertiary"
+                  onClick={() => {
+                    updateField('brand', brandSearch);
+                    setBrandDropdownOpen(false);
+                    setBrandSearch('');
+                  }}
+                >
+                  {t('inventory.useCustomBrand', { brand: brandSearch })}
+                </button>
+              )}
+            </div>
+          )}
+        </div>
+      </div>
+
+      {/* Variant / Subtype */}
+      <div>
+        <label className="block text-sm font-medium text-bambu-gray mb-1">{t('inventory.subtype')}</label>
+        <div className="relative" ref={subtypeRef}>
+          <input
+            type="text"
+            value={subtypeDropdownOpen ? subtypeSearch : formData.subtype}
+            onChange={(e) => {
+              setSubtypeSearch(e.target.value);
+              setSubtypeDropdownOpen(true);
+            }}
+            onFocus={() => {
+              setSubtypeDropdownOpen(true);
+              setSubtypeSearch('');
+            }}
+            placeholder="Basic, Matte, Silk..."
+            className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"
+          />
+          <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none" />
+          {subtypeDropdownOpen && (
+            <div className="absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto">
+              {filteredVariants.length === 0 ? (
+                <div className="px-3 py-2 text-sm text-bambu-gray">{t('inventory.noResults')}</div>
+              ) : (
+                filteredVariants.map(variant => (
+                  <button
+                    key={variant}
+                    type="button"
+                    className={`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${
+                      formData.subtype === variant ? 'bg-bambu-green/10 text-bambu-green' : 'text-white'
+                    }`}
+                    onClick={() => {
+                      updateField('subtype', variant);
+                      setSubtypeDropdownOpen(false);
+                      setSubtypeSearch('');
+                    }}
+                  >
+                    {variant}
+                  </button>
+                ))
+              )}
+              {subtypeSearch && !KNOWN_VARIANTS.some(v => v.toLowerCase() === subtypeSearch.toLowerCase().trim()) && (
+                <button
+                  type="button"
+                  className="w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary text-bambu-green border-t border-bambu-dark-tertiary"
+                  onClick={() => {
+                    updateField('subtype', subtypeSearch);
+                    setSubtypeDropdownOpen(false);
+                    setSubtypeSearch('');
+                  }}
+                >
+                  {t('inventory.useCustomBrand', { brand: subtypeSearch })}
+                </button>
+              )}
+            </div>
+          )}
+        </div>
+      </div>
+
+      {/* Label Weight */}
+      <div>
+        <label className="block text-sm font-medium text-bambu-gray mb-1">{t('inventory.labelWeight')}</label>
+        <div className="relative">
+          <input
+            type="number"
+            value={formData.label_weight}
+            onChange={(e) => updateField('label_weight', parseInt(e.target.value) || 0)}
+            className="w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"
+          />
+          <span className="absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray">g</span>
+        </div>
+      </div>
+    </div>
+  );
+}

+ 268 - 0
frontend/src/components/spool-form/PAProfileSection.tsx

@@ -0,0 +1,268 @@
+import { ChevronDown, ChevronRight, Sparkles } from 'lucide-react';
+import { useTranslation } from 'react-i18next';
+import type { CalibrationProfile, PAProfileSectionProps } from './types';
+import { isMatchingCalibration } from './utils';
+
+export function PAProfileSection({
+  formData,
+  printersWithCalibrations,
+  selectedProfiles,
+  setSelectedProfiles,
+  expandedPrinters,
+  setExpandedPrinters,
+}: PAProfileSectionProps) {
+  const { t } = useTranslation();
+
+  const togglePrinterExpanded = (printerId: string) => {
+    setExpandedPrinters((prev) => {
+      const next = new Set(prev);
+      if (next.has(printerId)) next.delete(printerId);
+      else next.add(printerId);
+      return next;
+    });
+  };
+
+  const toggleProfileSelected = (printerId: string, caliIdx: number, extruderId?: number | null) => {
+    const key = `${printerId}:${caliIdx}:${extruderId ?? 'null'}`;
+    const printerNozzleKey = `${printerId}:${extruderId ?? 'null'}`;
+
+    setSelectedProfiles((prev) => {
+      const next = new Set(prev);
+      if (next.has(key)) {
+        next.delete(key);
+      } else {
+        // Remove existing profile for same printer/nozzle
+        for (const existingKey of Array.from(next)) {
+          const parts = existingKey.split(':');
+          const existingPrinterNozzle = `${parts[0]}:${parts[2]}`;
+          if (existingPrinterNozzle === printerNozzleKey) {
+            next.delete(existingKey);
+          }
+        }
+        next.add(key);
+      }
+      return next;
+    });
+  };
+
+  // Auto-select best matching profiles
+  const autoSelectProfiles = () => {
+    const newSelection = new Set<string>();
+
+    for (const { printer, calibrations } of printersWithCalibrations) {
+      if (!printer.connected) continue;
+
+      const matchingCals = calibrations.filter(cal =>
+        isMatchingCalibration(cal, formData),
+      );
+
+      // Group by extruder
+      const byExtruder = new Map<string, CalibrationProfile[]>();
+      for (const cal of matchingCals) {
+        const extKey = `${cal.extruder_id ?? 'null'}`;
+        if (!byExtruder.has(extKey)) byExtruder.set(extKey, []);
+        byExtruder.get(extKey)!.push(cal);
+      }
+
+      // Select best (highest K) for each extruder
+      for (const [extKey, cals] of byExtruder) {
+        if (cals.length > 0) {
+          const sorted = [...cals].sort((a, b) => b.k_value - a.k_value);
+          const best = sorted[0];
+          newSelection.add(`${printer.id}:${best.cali_idx}:${extKey}`);
+        }
+      }
+    }
+
+    setSelectedProfiles(newSelection);
+  };
+
+  if (!formData.material) {
+    return (
+      <div className="p-6 bg-bambu-dark rounded-lg text-center">
+        <p className="text-bambu-gray">
+          {t('inventory.selectMaterialFirst')}
+        </p>
+      </div>
+    );
+  }
+
+  if (printersWithCalibrations.length === 0) {
+    return (
+      <div className="p-6 bg-bambu-dark rounded-lg text-center">
+        <p className="text-bambu-gray">
+          {t('inventory.noPrintersConfigured')}
+        </p>
+      </div>
+    );
+  }
+
+  // Count total matching profiles
+  const totalMatching = printersWithCalibrations.reduce((sum, { printer, calibrations }) => {
+    if (!printer.connected) return sum;
+    return sum + calibrations.filter(cal => isMatchingCalibration(cal, formData)).length;
+  }, 0);
+
+  const renderProfile = (printer: { id: number }, cal: CalibrationProfile) => {
+    const key = `${printer.id}:${cal.cali_idx}:${cal.extruder_id ?? 'null'}`;
+    const isSelected = selectedProfiles.has(key);
+    return (
+      <label
+        key={`${cal.cali_idx}-${cal.extruder_id}`}
+        className={`flex items-center gap-3 p-3 rounded-lg cursor-pointer transition-all border ${
+          isSelected
+            ? 'bg-bambu-green/10 border-bambu-green/30'
+            : 'bg-bambu-dark border-transparent hover:bg-bambu-dark/80'
+        }`}
+      >
+        <input
+          type="checkbox"
+          checked={isSelected}
+          onChange={() => toggleProfileSelected(String(printer.id), cal.cali_idx, cal.extruder_id)}
+          className="w-4 h-4 rounded border-bambu-dark-tertiary text-bambu-green focus:ring-bambu-green"
+        />
+        <div className="flex-1 min-w-0">
+          <span className={`text-sm font-medium ${isSelected ? 'text-bambu-green' : 'text-white'}`}>
+            {cal.name || cal.filament_id}
+          </span>
+        </div>
+        <div className="flex items-center gap-2 shrink-0">
+          <span className="text-xs font-mono px-2 py-0.5 rounded bg-bambu-dark text-bambu-gray">
+            K={cal.k_value.toFixed(3)}
+          </span>
+        </div>
+      </label>
+    );
+  };
+
+  return (
+    <div className="space-y-4">
+      {/* Header with auto-select */}
+      <div className="flex items-center justify-between">
+        <p className="text-xs text-bambu-gray">
+          {t('inventory.matchingFilter')}: {formData.brand || t('inventory.anyBrand')} / {formData.material} / {formData.subtype || t('inventory.anyVariant')}
+        </p>
+        {totalMatching > 0 && (
+          <button
+            type="button"
+            onClick={autoSelectProfiles}
+            className="flex items-center gap-1.5 px-2 py-1 text-xs bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white hover:border-bambu-green transition-colors"
+          >
+            <Sparkles className="w-3.5 h-3.5" />
+            {t('inventory.autoSelect')} ({totalMatching})
+          </button>
+        )}
+      </div>
+
+      {/* Printer sections */}
+      <div className="space-y-3">
+        {printersWithCalibrations.map(({ printer, calibrations }) => {
+          const isExpanded = expandedPrinters.has(String(printer.id));
+          const matchingCals = calibrations.filter(cal => isMatchingCalibration(cal, formData));
+          const matchingCount = matchingCals.length;
+
+          // Multi-nozzle grouping
+          const isMultiNozzle = matchingCals.some(cal =>
+            cal.extruder_id !== undefined && cal.extruder_id !== null && cal.extruder_id > 0,
+          );
+          const leftNozzleCals = matchingCals.filter(cal => cal.extruder_id === 1);
+          const rightNozzleCals = matchingCals.filter(cal =>
+            cal.extruder_id === 0 || cal.extruder_id === undefined || cal.extruder_id === null,
+          );
+
+          return (
+            <div
+              key={printer.id}
+              className="border border-bambu-dark-tertiary rounded-lg overflow-hidden"
+            >
+              {/* Printer Header */}
+              <button
+                type="button"
+                onClick={() => togglePrinterExpanded(String(printer.id))}
+                className="w-full px-4 py-3 flex items-center justify-between bg-bambu-dark-secondary hover:bg-bambu-dark-tertiary transition-colors"
+              >
+                <div className="flex items-center gap-3">
+                  {isExpanded ? (
+                    <ChevronDown className="w-4 h-4 text-bambu-gray" />
+                  ) : (
+                    <ChevronRight className="w-4 h-4 text-bambu-gray" />
+                  )}
+                  <span className="font-medium text-white">
+                    {printer.name}
+                  </span>
+                  {matchingCount > 0 ? (
+                    <span className="text-xs px-2 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green">
+                      {matchingCount} {matchingCount !== 1 ? t('inventory.matches') : t('inventory.match')}
+                    </span>
+                  ) : (
+                    <span className="text-xs px-2 py-0.5 rounded-full bg-bambu-dark-tertiary text-bambu-gray">
+                      {t('inventory.noMatches')}
+                    </span>
+                  )}
+                </div>
+                <span className={`text-xs px-2 py-1 rounded-full ${
+                  printer.connected
+                    ? 'bg-green-500/20 text-green-500'
+                    : 'bg-bambu-gray/20 text-bambu-gray'
+                }`}>
+                  {printer.connected ? t('inventory.connected') : t('inventory.offline')}
+                </span>
+              </button>
+
+              {/* Calibration Profiles */}
+              {isExpanded && (
+                <div className="px-4 py-3 space-y-3 bg-bambu-dark border-t border-bambu-dark-tertiary">
+                  {!printer.connected ? (
+                    <p className="text-sm text-bambu-gray italic py-2">
+                      {t('inventory.printerOffline')}
+                    </p>
+                  ) : matchingCount === 0 ? (
+                    <p className="text-sm text-bambu-gray italic py-2">
+                      {t('inventory.noKProfilesMatch')}
+                    </p>
+                  ) : isMultiNozzle ? (
+                    <>
+                      {leftNozzleCals.length > 0 && (
+                        <div className="space-y-2">
+                          <p className="text-xs font-medium text-bambu-gray uppercase tracking-wide">
+                            {t('inventory.leftNozzle')}
+                          </p>
+                          <div className="space-y-2">
+                            {leftNozzleCals.map(cal => renderProfile(printer, cal))}
+                          </div>
+                        </div>
+                      )}
+                      {rightNozzleCals.length > 0 && (
+                        <div className="space-y-2">
+                          <p className="text-xs font-medium text-bambu-gray uppercase tracking-wide">
+                            {t('inventory.rightNozzle')}
+                          </p>
+                          <div className="space-y-2">
+                            {rightNozzleCals.map(cal => renderProfile(printer, cal))}
+                          </div>
+                        </div>
+                      )}
+                    </>
+                  ) : (
+                    <div className="space-y-2">
+                      {matchingCals.map(cal => renderProfile(printer, cal))}
+                    </div>
+                  )}
+                </div>
+              )}
+            </div>
+          );
+        })}
+      </div>
+
+      {/* Summary */}
+      {selectedProfiles.size > 0 && (
+        <div className="p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg">
+          <p className="text-sm text-white">
+            <span className="font-semibold">{selectedProfiles.size}</span> {t('inventory.profilesSelected')}
+          </p>
+        </div>
+      )}
+    </div>
+  );
+}

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini