# Fabric Costing Master v6: new design on the Bridge edition

## What changed from v5 (v6: your Figma design)

- **New look, built from `Fabric Costing.fig`.** Colours, radii, type sizes and spacing follow your file. Inter is bundled with the site (`fonts/`, SIL Open Font License) so the look is the same on every phone.
- **Pixel check against your file.** Fields are 167x42 (full width 344x42), rate tiles 156x56, measurement tiles 168x63, cost totals 344x41, colour rows 51 high with 98x35 inputs, buttons 43 high, summary cards 150x68. Cards have the same 13px side margin and 16px inner padding as the design.
- **One scale everywhere.** The Figma frames mixed Arimo and Inter, different title sizes (13 and 18 px), two frame widths (390 and 402) and section numbers 01, 02, 05, 03, 04, 05. v6 uses Inter, 18 px panel titles, a fluid layout, and numbers the sections 01 to 09 in reading order.
- **Section order as in your design.** Sizing & pick sits after the yarn rates, Finishing cost after the colour splits, Update yarn rates is inside section 02, and the Count system converter lives in the Save, retrieve or update card.
- **Friendlier dialogs.** Every question is now a bottom sheet with an icon, a plain-language explanation and a clear button label such as "Update #47" or "Save as new". Primary button on top, Cancel below.
- **Inputs are 16 px** so iPhone Safari does not zoom in when you tap a field. Placeholders stay 13 px as in the design.
- **Not-loaded screen** shows only the workbook card, the Save to iCloud card and a short message, as in your design.

## What changed from v4 (v5)

- **No File Picker extension and no file handles.** The workbook now arrives and leaves through two Apple Shortcuts, **Bridge Load** and **Bridge Save**, which you run from Safari's Share button.
- **Load once, work as long as you like, save when you choose.** Save costing, Retrieve, Clear and Update Yarn Rates change the workbook inside the page and prepare a ready-to-save package. Nothing reaches iCloud until you run Bridge Save.
- **Bridge Save is verified.** After saving, the Shortcut reads the file back and the page confirms it matches the prepared package byte for byte. Only then does the page show the work as saved.
- **Reload protection.** Every prepared change is also kept as a draft in this browser. If Safari reloads the page, the app offers to restore it. The draft is deleted after a confirmed Bridge Save. The workbook remains the only record.
- **Replace-protection.** If you run Bridge Load while the page holds unsaved changes, the app asks whether to keep your work or use the freshly loaded workbook.
- **Smaller changes inside the workbook.** Only the XML parts that were actually edited are rewritten. Everything else, including the VBA project, keeps its original bytes. No empty folder entries are added.
- **Normal Safari tab.** The manifest no longer asks for standalone mode, and the Home Screen capable tag is removed. The Shortcuts need Safari's Share button.
- **Pull-to-refresh** is switched off with CSS where Safari honours it (iOS 16 and later). The draft is the safety net if it still happens.

## How the hand-off works

The page owns two hidden elements: `bridgeData` (the workbook as base64 text) and `bridgeMeta` (flags `data-ready`, `data-token`, `data-acked`, `data-sha`).

- **Bridge Load** reads the workbook from iCloud, writes it into `bridgeData` and fires a `bridgeload` event. The page also polls, so it still works if the event is missed.
- After each change the page rebuilds the workbook, checks it reopens and still holds `vbaProject.bin`, writes it into `bridgeData` and sets `data-ready="1"`.
- **Bridge Save** returns `bridgeData` only if `data-ready="1"`. It saves the file over the original, reads it back, and if the read-back equals `bridgeData` it sets `data-acked` to the page's token. The page then marks everything saved.
- If nothing was prepared, Bridge Save finds an empty value and must stop before touching the file. See the shortcuts guide.

## Requirements

- iPhone Safari (normal tab) over HTTPS, JavaScript enabled, Shortcuts setting **Allow Running Scripts** on.
- The two Shortcuts built as in `Bridge_Shortcuts_Guide.md`.
- Internet for the first page load (JSZip is bundled, so the workbook never leaves the device).

## First-time test

1. Use a **duplicate** workbook (not the master). Put it in a test folder in iCloud Drive and point both Shortcuts at it.
2. Open the site in Safari. It says it is waiting for the workbook.
3. Share, then Bridge Load. The page shows "Workbook loaded and checked".
4. Retrieve S.No 47, then Save costing. The page shows "2 changes not yet saved to iCloud".
5. Share, then Bridge Save. The Shortcut shows "SAVED AND VERIFIED" and the page shows "Bridge Save confirmed".
6. Open the duplicate in Excel on the Mac. Check no repair message appears, the macros banner appears, and ARCHIVE and COSTING SNAPSHOTS hold the change.
7. Test the safety nets: make a change, reload the page and use Restore; run Bridge Load with unsaved work and choose Keep my work.

## Workbook compatibility

- Required sheets: `COSTING SHEET`, `ARCHIVE`, `COSTING SNAPSHOTS`, `YARN RATE MASTER`. Required tables: `CostArchives`, `YarnRate`. The snapshot sheet must have the 54 expected input headers.
- The workbook must contain `xl/vbaProject.bin` (the macro-enabled .xlsm). The app refuses a workbook without it, so a save can never strip macros.
- The app does not run VBA and is not Excel. It reimplements the macro actions in JavaScript and sets the workbook to recalculate fully when Excel next opens it. Validate the preview against Excel before relying on every cost path.

## Troubleshooting

- **"NOT THE COSTING APP" from Bridge Load**: the Safari tab is not on this site. Open the site first, then run the shortcut from that tab.
- **Bridge Save says nothing to save**: nothing was prepared in this page since loading. Run Save costing (or another action) first.
- **"WARNING: saved file does not match"**: do not trust the saved file. Restore the backup copy, reload the page, and report what happened.
- **Page reloaded**: use Restore unsaved work if the banner appears, otherwise run Bridge Load again.
- **A shortcut script is refused or blocked**: Settings, then Shortcuts, then Advanced, then Allow Running Scripts, and tap Allow when Safari asks.

## Deployment

Upload the contents of this ZIP to the Cloudflare Pages project (`textile-costing-helperv2`) as a new production deployment. All website files are at the archive root. Do not upload the workbook to Cloudflare. Keep the v4 ZIP in case you need to roll back.
