/* ==========================================================================
   Weftwork tool UI (loaded only on tool pages)
   ========================================================================== */
.tool-app { min-height: 520px; margin-bottom: var(--space-5); }
.tool-loading { height: 520px; border-radius: var(--radius-lg); background: linear-gradient(90deg, var(--linen-2), #f3eee5, var(--linen-2)); background-size: 200% 100%; animation: tl-shimmer 1.4s infinite linear; }
@keyframes tl-shimmer { to { background-position: -200% 0; } }

.tl { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: var(--space-4); align-items: start; }
.tl-stage { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius-lg); overflow: hidden; display: flex; flex-direction: column; min-height: 520px; }
.tl-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); padding: 10px 12px; border-bottom: 1px solid var(--line); min-height: 56px; }
.tl-toolbar:empty { display: none; }
.tl-toolbar .tl-field { margin: 0; flex-direction: row; align-items: center; }
.tl-toolbar .spacer { flex: 1; }
.tl-view { position: relative; flex: 1; display: grid; place-items: center; padding: var(--space-4); background-color: #ebe5da; background-image: linear-gradient(45deg, #e3dccf 25%, transparent 25%), linear-gradient(-45deg, #e3dccf 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #e3dccf 75%), linear-gradient(-45deg, transparent 75%, #e3dccf 75%); background-size: 20px 20px; background-position: 0 0, 0 10px, 10px -10px, -10px 0; overflow: auto; min-height: 420px; }
.tl-view canvas { max-width: 100%; height: auto; box-shadow: var(--shadow); background: #fff; }
.tl-view .tl-scroll { max-width: 100%; max-height: 72vh; overflow: auto; }
.tl-view .tl-scroll canvas { max-width: none; }
.tl-status { padding: 8px 14px; font-size: var(--fs-xs); color: var(--muted); border-top: 1px solid var(--line); min-height: 34px; }
.tl-status:empty { display: none; }

.tl-side { display: grid; gap: var(--space-3); position: sticky; top: calc(var(--header-h) + 12px); max-height: calc(100vh - var(--header-h) - 24px); overflow: auto; overscroll-behavior: contain; }
.tl-panel { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius-lg); padding: var(--space-2) var(--space-4); }
.tl-actions { display: grid; gap: var(--space-2); background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius-lg); padding: var(--space-4); }
.tl-actions:empty { display: none; }
.tl-actions .btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); }

.tl-group { border: 0; padding: var(--space-3) 0; margin: 0; border-bottom: 1px solid var(--line); min-width: 0; }
.tl-group:last-child { border-bottom: 0; }
.tl-group__title { font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .1em; font-weight: 700; color: var(--muted); padding: 0; margin-bottom: var(--space-3); }
.tl-group[disabled] { opacity: .55; }

.tl-field { display: flex; flex-direction: column; gap: 6px; margin-bottom: var(--space-3); }
.tl-field:last-child { margin-bottom: 0; }
.tl-field label, .tl-label { font-size: var(--fs-sm); font-weight: 600; color: var(--ink-2); }
.tl-field__row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.tl-val { font-variant-numeric: tabular-nums; font-size: var(--fs-xs); color: var(--muted); font-weight: 600; }
.tl-input { width: 100%; min-height: 40px; padding: 8px 10px; border: 1px solid var(--line-2); border-radius: var(--radius-sm); background: var(--paper); font-size: var(--fs-sm); }
.tl-input:focus { outline: none; border-color: var(--indigo); box-shadow: 0 0 0 3px rgba(44,62,143,.18); }
.tl-affix { display: flex; align-items: stretch; }
.tl-affix .tl-input { border-radius: var(--radius-sm) 0 0 var(--radius-sm); }
.tl-affix span { display: grid; place-items: center; padding: 0 10px; border: 1px solid var(--line-2); border-left: 0; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; background: var(--linen); font-size: var(--fs-xs); color: var(--muted); }
.tl-help { font-size: var(--fs-xs); color: var(--muted); }
.tl-field--check { flex-direction: row; align-items: center; gap: 10px; }
.tl-field--check input { width: 18px; height: 18px; accent-color: var(--indigo); }
.tl-field--check label { font-weight: 500; }
input[type="range"] { width: 100%; accent-color: var(--indigo); height: 28px; }
.tl-colorrow { display: flex; gap: 8px; align-items: center; }
.tl-color { width: 44px; height: 40px; padding: 2px; border: 1px solid var(--line-2); border-radius: var(--radius-sm); background: var(--paper); cursor: pointer; flex: none; }
.tl-hex { font-family: ui-monospace, Menlo, monospace; text-transform: uppercase; }
.tl-row2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-3); }
.tl-row2 .tl-field { margin-bottom: 0; }

.tl-seg { display: flex; flex-wrap: wrap; border: 1px solid var(--line-2); border-radius: var(--radius-sm); overflow: hidden; background: var(--paper); }
.tl-seg button { flex: 1 1 auto; min-height: 38px; padding: 6px 10px; border: 0; background: transparent; font-size: var(--fs-xs); font-weight: 600; color: var(--ink-2); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.tl-seg button + button { border-left: 1px solid var(--line-2); }
.tl-seg button[aria-pressed="true"] { background: var(--ink); color: #fff; }
.tl-seg button:disabled { cursor: not-allowed; }

.tl-note { font-size: var(--fs-xs); color: var(--muted); margin: 0 0 var(--space-3); line-height: 1.5; }
.tl-note--pro { padding: 8px 10px; background: var(--turmeric-50); border-radius: var(--radius-sm); color: #6b4a06; }
.tl-note--ok { padding: 8px 10px; background: var(--leaf-50); border-radius: var(--radius-sm); color: #2a5a3b; }
.tl-note--warn { padding: 8px 10px; background: var(--madder-50); border-radius: var(--radius-sm); color: #8b2f1d; }

.tl-stats { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-size: var(--fs-sm); }
.tl-stats dt { color: var(--muted); }
.tl-stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }

/* Dropzone */
.tl-drop { position: relative; width: min(100%, 560px); display: grid; justify-items: center; gap: 8px; padding: var(--space-7) var(--space-5); background: rgba(255,255,255,.92); border: 2px dashed var(--line-2); border-radius: var(--radius-lg); text-align: center; cursor: pointer; transition: border-color .15s ease, background .15s ease; }
.tl-drop:hover, .tl-drop.is-over { border-color: var(--indigo); background: #fff; }
.tl-drop:focus-visible { outline: 3px solid var(--turmeric); }
.tl-drop strong { font-family: var(--font-display); font-size: 1.35rem; font-weight: 600; color: var(--ink); }
.tl-drop__icon { width: 60px; height: 60px; border-radius: 50%; display: grid; place-items: center; background: var(--indigo-50); color: var(--indigo); }
.tl-drop__sub { font-size: var(--fs-sm); color: var(--muted); }
.tl-drop__sample { margin-top: var(--space-2); }
.tl-drop__err { color: var(--madder); font-size: var(--fs-sm); font-weight: 600; }
.tl-drop.is-loading { opacity: .6; pointer-events: none; }
.tl-drop.is-loading::after { content: "Reading image…"; font-size: var(--fs-sm); color: var(--muted); }
.tl--ready .tl-view .tl-drop { display: none; }

/* Swatches */
.tl-swatches { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: var(--space-2); }
.tl-swatch { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--paper); font-size: var(--fs-xs); display: flex; flex-direction: column; }
.tl-swatch__chip { height: 64px; position: relative; border: 0; padding: 0; cursor: pointer; display: block; width: 100%; }
.tl-swatch__chip input[type="color"] { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }
.tl-swatch__body { padding: 6px 8px; display: grid; gap: 2px; }
.tl-swatch code { font-family: ui-monospace, Menlo, monospace; font-weight: 700; color: var(--ink); font-size: .78rem; }
.tl-swatch small { color: var(--muted); }
.tl-swatch__tools { display: flex; gap: 2px; padding: 0 4px 4px; }
.tl-swatch__tools button { flex: 1; border: 0; background: var(--linen); border-radius: 4px; padding: 4px; cursor: pointer; color: var(--ink-2); display: grid; place-items: center; min-height: 28px; }
.tl-swatch__tools button:hover { background: var(--linen-2); }
.tl-swatch__tools button[aria-pressed="true"] { background: var(--ink); color: #fff; }
.tl-palette-strip { display: flex; height: 48px; border-radius: var(--radius-sm); overflow: hidden; border: 1px solid var(--line); }
.tl-palette-strip span { flex: 1; }
.tl-results { padding: var(--space-4); width: 100%; align-self: start; }
.tl-view--plain { background: var(--linen); place-items: start stretch; }
.tl-map { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: center; margin-bottom: 8px; }
.tl-map .tl-swatch__chip { height: 40px; border-radius: var(--radius-sm); border: 1px solid var(--line); }
.tl-map__arrow { color: var(--muted); }

/* Calculator results */
.tl-calc { display: grid; gap: var(--space-4); width: 100%; }
.tl-big { font-family: var(--font-display); font-size: clamp(2rem, 1.5rem + 2vw, 3rem); font-weight: 600; line-height: 1.1; color: var(--ink); }
.tl-big small { font-family: var(--font-ui); font-size: var(--fs-sm); color: var(--muted); font-weight: 500; }
.tl-card { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: var(--space-5); }
.tl-card h3 { font-size: 1rem; font-family: var(--font-ui); font-weight: 700; margin-bottom: var(--space-3); }
.tl-table { width: 100%; font-size: var(--fs-sm); border-collapse: collapse; }
.tl-table th, .tl-table td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; font-variant-numeric: tabular-nums; }
.tl-table th { color: var(--muted); font-weight: 600; font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .06em; }
.tl-table tr.is-hit td { background: var(--leaf-50); font-weight: 600; }
.tl-meter { height: 10px; border-radius: 999px; background: var(--linen-2); overflow: hidden; }
.tl-meter span { display: block; height: 100%; border-radius: inherit; }
.tl-fabric { position: relative; height: 120px; border: 1px solid var(--line-2); border-radius: var(--radius-sm); background: var(--linen); overflow: hidden; display: flex; }
.tl-fabric i { display: block; height: 100%; border-right: 1px dashed rgba(27,36,64,.35); background: repeating-linear-gradient(135deg, rgba(44,62,143,.32) 0 6px, rgba(44,62,143,.16) 6px 12px); flex: none; }
.tl-fabric i.part { background: repeating-linear-gradient(135deg, rgba(184,67,44,.18) 0 6px, rgba(184,67,44,.06) 6px 12px); }

/* File list (converter) */
.tl-files { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--space-2); width: 100%; }
.tl-files li { display: grid; grid-template-columns: 56px 1fr auto; gap: 12px; align-items: center; padding: 10px; background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); font-size: var(--fs-sm); }
.tl-files canvas, .tl-files img { width: 56px; height: 56px; object-fit: cover; border-radius: 6px; box-shadow: none; }
.tl-files .meta { color: var(--muted); font-size: var(--fs-xs); }

/* Crop overlay */
.tl-crop { position: relative; display: inline-block; touch-action: none; user-select: none; }
.tl-crop canvas { display: block; }
.tl-crop__box { position: absolute; border: 2px solid #fff; outline: 1px solid rgba(0,0,0,.5); box-shadow: 0 0 0 9999px rgba(27,36,64,.55); cursor: move; }
.tl-crop__box::before, .tl-crop__box::after { content: ""; position: absolute; inset: 33.33% 0; border-block: 1px solid rgba(255,255,255,.5); pointer-events: none; }
.tl-crop__box::after { inset: 0 33.33%; border-block: 0; border-inline: 1px solid rgba(255,255,255,.5); }
.tl-crop__h { position: absolute; width: 18px; height: 18px; background: #fff; border: 2px solid var(--ink); border-radius: 50%; }
.tl-crop__h[data-h="nw"] { left: -10px; top: -10px; cursor: nwse-resize; }
.tl-crop__h[data-h="ne"] { right: -10px; top: -10px; cursor: nesw-resize; }
.tl-crop__h[data-h="sw"] { left: -10px; bottom: -10px; cursor: nesw-resize; }
.tl-crop__h[data-h="se"] { right: -10px; bottom: -10px; cursor: nwse-resize; }

/* Colourway strip */
.tl-ways { display: flex; gap: 8px; flex-wrap: wrap; }
.tl-ways button { width: 56px; height: 56px; padding: 0; border: 2px solid var(--line); border-radius: var(--radius-sm); overflow: hidden; cursor: pointer; background: #fff; }
.tl-ways button:hover, .tl-ways button:focus-visible { border-color: var(--ink); }
.tl-ways canvas { width: 100%; height: 100%; box-shadow: none; }

@media (max-width: 980px) {
	.tl { grid-template-columns: 1fr; }
	.tl-side { position: static; max-height: none; overflow: visible; }
	.tl-stage { min-height: 0; }
	.tl-view { min-height: 300px; padding: var(--space-3); }
	.tool-app, .tool-loading { min-height: 360px; height: auto; }
}
@media (max-width: 520px) {
	.tl-view { padding: 8px; }
	.tl-drop { padding: var(--space-6) var(--space-4); }
	.tl-row2 { grid-template-columns: 1fr; }
}
.tl-toolbar .tl-field--range { flex-direction: row; align-items: center; gap: 10px; }
.tl-toolbar .tl-field__row { white-space: nowrap; gap: 6px; }
.tl-toolbar .tl-field--range input[type="range"] { width: 140px; }
