/* Algorithm v2 — tool form kit.
   One anatomy for every tool form, top to bottom:
     .tform                 the card
       .tform-tabs          sibling tools / modes (optional)
       .tform-body          fields; .tform-steps splits it into numbered steps
       .tform-opts          collapsible option rows (<details class="tform-opt">)
       .tform-run           run bar: toggles left, count · cost + run button right
     .tform-history         saved runs, a separate block under the card
   Loads after v2.css / v2-tool.css; nothing here depends on legacy markup. */

.tform {
    position: relative;
    border: 1px solid var(--line-strong); border-radius: var(--r-lg);
    background: var(--bg-raised);
    box-shadow: 0 30px 80px -30px rgba(var(--shadow-rgb), calc(0.7 * var(--shadow-k)));
    text-align: left;
}
.tform + .tform-history { margin-top: 24px; }
.tform [hidden], .tform-history [hidden] { display: none !important; }

/* ── Tabs: sibling tools or modes, label + one-line description ── */
.tform-tabs {
    /* same side padding as .tform-body / .tform-run, so tab edges line up with the fields */
    display: flex; gap: 6px; padding: 14px 20px; overflow-x: auto; scrollbar-width: none;
    border-bottom: 1px solid var(--line);
}
.tform-tabs::-webkit-scrollbar { display: none; }
.tform-tab {
    position: relative; flex: 1 0 auto; min-width: 150px;
    display: flex; flex-direction: column; gap: 2px; padding: 9px 12px;
    border-radius: var(--r); color: var(--text-2); text-decoration: none; cursor: pointer;
    /* a faint frame so every tab reads as a button; the active one gets the
       accent frame of a picked .tform-choice (clustering methods) */
    box-shadow: inset 0 0 0 1px var(--line);
    transition: background 0.15s, color 0.15s, box-shadow 0.15s;
}
.tform-tab:hover { background: rgba(var(--fg-rgb), 0.03); color: var(--text); box-shadow: inset 0 0 0 1px var(--line-strong); }
.tform-tab > b { display: flex; align-items: center; gap: 6px; font-size: 14px; font-weight: 500; color: inherit; white-space: nowrap; }
.tform-tab > span { font-size: 12.5px; color: var(--text-3); white-space: nowrap; }
.tform-tab.is-active { background: rgba(var(--accent-rgb), 0.06); color: var(--text); cursor: default; box-shadow: inset 0 0 0 1px rgba(var(--accent-rgb), 0.55); }

/* ── Body and fields ── */
.tform-body { padding: 20px; display: grid; gap: 18px; }
.tform-field { display: grid; gap: 8px; min-width: 0; }
.tform-label {
    display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 8px;
    font-size: 13.5px; font-weight: 500; color: var(--text);
}
.tform-label small { font-weight: 400; color: var(--text-3); font-size: 13px; }
.tform-label .tform-actions { margin-left: auto; align-self: center; }
.tform-actions { display: inline-flex; align-items: center; gap: 4px; }
.tform-link {
    display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 8px;
    border: 0; border-radius: var(--r-sm); background: none;
    color: var(--text-3); font: inherit; font-size: 13px; font-weight: 400; white-space: nowrap; cursor: pointer;
}
.tform-link:hover { color: var(--text); background: var(--bg-hover); }
.tform-link svg { width: 13px; height: 13px; }
.tform-hint { font-size: 13px; line-height: 1.5; color: var(--text-3); }
.tform-hint b { color: var(--warn); font-weight: 500; }

.tform-input, .tform-select, .tform-textarea {
    display: block; width: 100%; min-width: 0;
    background: var(--bg); border: 1px solid var(--line-strong); border-radius: var(--r);
    color: var(--text); font: inherit; font-size: 14.5px;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.tform-input, .tform-select { height: 40px; padding: 0 12px; }
.tform-select {
    appearance: none; -webkit-appearance: none; padding-right: 32px; cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M3 4.5 6 7.5 9 4.5' fill='none' stroke='%236c6f78' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 10px center; background-size: 12px;
}
.tform-textarea { min-height: 160px; padding: 12px 14px; line-height: 1.6; resize: vertical; }
.tform-textarea--tall { min-height: 260px; }
.tform-textarea--mono { font-family: var(--mono); font-size: 13px; line-height: 1.8; }
.tform-input::placeholder, .tform-textarea::placeholder { color: var(--text-3); }
.tform-input:hover, .tform-select:hover, .tform-textarea:hover { border-color: rgba(var(--fg-rgb), 0.18); }
.tform-input:focus, .tform-select:focus, .tform-textarea:focus {
    outline: none; border-color: rgba(var(--accent-rgb), 0.6); box-shadow: 0 0 0 3px rgba(var(--accent-rgb), 0.15);
}
.tform-input[type="url"] { font-family: var(--mono); font-size: 13.5px; }

/* Input with a leading icon */
.tform-icon { position: relative; flex: 1; min-width: 0; }
.tform-icon svg { position: absolute; left: 12px; top: 50%; width: 15px; height: 15px; transform: translateY(-50%); color: var(--text-3); pointer-events: none; }
.tform-icon .tform-input { padding-left: 36px; }

/* Row of controls (input + button, two selects …) */
.tform-row { display: flex; gap: 8px; min-width: 0; }
.tform-row > * { flex: 1; min-width: 0; }
.tform-row > .btn { flex: none; height: 40px; }

/* Counter in the corner of a textarea */
.tform-wrap { position: relative; }
.tform-count { position: absolute; right: 12px; bottom: 10px; font-family: var(--mono); font-size: 12px; color: var(--text-3); pointer-events: none; }

/* Divider with a word ("or") */
.tform-or { display: flex; align-items: center; gap: 12px; font-size: 12.5px; color: var(--text-3); }
.tform-or::before, .tform-or::after { content: ''; flex: 1; height: 1px; background: var(--line); }

/* ── Segmented switch inside a field (Paste text / Use a URL) ── */
/* Frame in --line-strong, like .tform-input / .tform-select: a switch next to a field must not look lighter */
.tform-seg { display: inline-flex; gap: 2px; padding: 3px; border: 1px solid var(--line-strong); border-radius: var(--r); background: var(--bg); justify-self: start; max-width: 100%; overflow-x: auto; }
.tform-seg button {
    height: 32px; padding: 0 12px; border: 0; border-radius: var(--r-sm); background: none; /* 32 + 3px padding + 1px border = 40, the field height */
    color: var(--text-3); font: inherit; font-size: 13.5px; white-space: nowrap; cursor: pointer;
}
.tform-seg button:hover { color: var(--text); }
.tform-seg button.is-active { background: var(--bg-hover); color: var(--text); box-shadow: inset 0 0 0 1px var(--line-strong); }

/* ── Choice cards: pick one method (Stems / AI Semantic / SERP) ── */
.tform-choices { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 8px; }
.tform-choice { position: relative; display: grid; gap: 3px; padding: 12px 14px 12px 40px; border: 1px solid var(--line-strong); border-radius: var(--r); background: var(--bg); cursor: pointer; transition: border-color 0.15s, background 0.15s; }
.tform-choice:hover { border-color: rgba(var(--fg-rgb), 0.2); }
.tform-choice input { position: absolute; left: 14px; top: 14px; width: 16px; height: 16px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.tform-choice b { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 500; color: var(--text); }
.tform-choice > span { font-size: 13px; color: var(--text-3); line-height: 1.45; }
.tform-choice .tform-price { font-family: var(--mono); font-size: 12px; color: var(--text-2); margin-top: 4px; }
.tform-choice:has(input:checked) { border-color: rgba(var(--accent-rgb), 0.55); background: rgba(var(--accent-rgb), 0.06); }
.tform-choice:has(input:disabled) { opacity: 0.5; cursor: not-allowed; }
.tform-tag { display: inline-block; padding: 1px 6px; border-radius: var(--r-xs); background: rgba(var(--accent-rgb), 0.16); color: var(--accent); font-size: 11px; font-weight: 500; letter-spacing: 0.02em; }

/* ── Numbered steps (two columns on wide screens) ── */
.tform-steps { display: grid; grid-template-columns: 1fr 1fr; padding: 0; gap: 0; }
.tform-step { display: grid; gap: 14px; align-content: start; padding: 22px 20px; min-width: 0; }
.tform-step + .tform-step { border-left: 1px solid var(--line); }
.tform-step-head { display: grid; grid-template-columns: 24px 1fr; gap: 12px; }
.tform-step-num { display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; border: 1px solid var(--line-strong); font-family: var(--mono); font-size: 12px; color: var(--text-2); }
.tform-step-head > div > b { display: block; font-size: 15px; font-weight: 500; color: var(--text); letter-spacing: -0.01em; }
.tform-step-head > div > span { display: block; margin-top: 2px; font-size: 13.5px; color: var(--text-3); line-height: 1.5; }

/* ── Option rows: collapsible, same panel background as the card ── */
.tform-opts { border-top: 1px solid var(--line); }
.tform-opt + .tform-opt { border-top: 1px solid var(--line); }
.tform-opt > summary, .tform-opt-head {
    display: flex; align-items: center; gap: 10px; padding: 13px 20px;
    list-style: none; cursor: pointer; user-select: none; font-size: 14px; color: var(--text);
}
.tform-opt > summary::-webkit-details-marker { display: none; }
.tform-opt > summary:hover, .tform-opt-head:hover { background: rgba(var(--fg-rgb), 0.02); }
.tform-opt .chev { width: 12px; height: 12px; flex: none; color: var(--text-3); transition: transform 0.2s var(--ease); }
.tform-opt[open] > summary .chev, .tform-opt.is-open .chev { transform: rotate(90deg); }
.tform-opt small { font-size: 13px; color: var(--text-3); }
.tform-val { margin-left: auto; font-size: 13px; color: var(--text-2); white-space: nowrap; }
.tform-opt-head:focus-visible, .tform-opt > summary:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
/* Same left edge as the main field above (.tform-body padding), not indented under the chevron */
.tform-opt-body { display: grid; gap: 12px; padding: 2px 20px 20px; }

/* ── Run bar: the only place with the cost and the run button ── */
.tform-run {
    display: flex; flex-wrap: wrap; align-items: center; gap: 12px 24px;
    padding: 14px 20px; border-top: 1px solid var(--line);
    background: rgba(var(--well-rgb), calc(0.18 * var(--well-k))); border-radius: 0 0 var(--r-lg) var(--r-lg);
}
.tform-run-left { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 20px; min-width: 0; }
.tform-run-right { display: flex; align-items: center; gap: 20px; margin-left: auto; }
.tform-cost { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 4px 0; font-size: 13.5px; color: var(--text-2); font-variant-numeric: tabular-nums; }
.tform-cost > * { white-space: nowrap; }
/* Items are separated by a middle dot; hidden items (display:none) drop their dot too */
.tform-cost > * + *::before { content: '·'; margin: 0 8px; color: var(--text-3); }
.tform-cost:empty { display: none; }
.tform-go { height: 44px; min-width: 200px; padding: 0 24px; font-size: 15px; }
.tform-go:disabled { opacity: 0.5; cursor: progress; }
.tform-kbd { font-size: 12.5px; color: var(--text-3); white-space: nowrap; }
.tform-kbd kbd { font-family: var(--mono); font-size: 11px; padding: 1px 5px; border: 1px solid var(--line-strong); border-radius: var(--r-xs); color: var(--text-2); }

/* Toggle switch */
.tform-switch { position: relative; display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; font-size: 14px; color: var(--text-2); }
.tform-switch small { font-size: 13px; color: var(--text-3); font-family: var(--mono); }
.tform-track { position: relative; width: 32px; height: 18px; flex: none; border-radius: 9px; background: rgba(var(--fg-rgb), 0.12); transition: background 0.2s; }
.tform-track input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.tform-track i { position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: var(--text-2); transition: transform 0.2s var(--ease), background 0.2s; pointer-events: none; }
.tform-track:has(input:checked) { background: var(--accent); }
.tform-track input:checked ~ i { transform: translateX(14px); background: #fff; }
.tform-track:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
.tform-switch:has(input:checked) { color: var(--text); }
.tform-switch.disabled { opacity: 0.45; cursor: not-allowed; }
.tform-switch.disabled .tform-track input { cursor: not-allowed; }

/* Info dot with a popover (hover / focus) */
.tform-info { position: relative; display: inline-grid; place-items: center; width: 18px; height: 18px; border-radius: 50%; color: var(--text-3); cursor: help; }
.tform-info:hover, .tform-info:focus-visible { color: var(--text); outline: none; }
.tform-info svg { width: 14px; height: 14px; }
/* The 18px icon made a label 1px taller than its neighbour, so the field under it sat 1px lower */
.tform-label .tform-info { margin: -2px 0; }
.tform-pop {
    position: absolute; z-index: 40; left: 50%; bottom: calc(100% + 8px); transform: translateX(-50%);
    width: 300px; padding: 12px 14px; border: 1px solid var(--line-strong); border-radius: var(--r);
    background: var(--bg-overlay); box-shadow: 0 20px 50px -12px rgba(var(--shadow-rgb), calc(0.8 * var(--shadow-k)));
    font-size: 13px; font-weight: 400; line-height: 1.55; color: var(--text-2); text-align: left; white-space: normal;
    opacity: 0; visibility: hidden; transition: opacity 0.15s; pointer-events: none;
}
.tform-pop strong { color: var(--text); font-weight: 500; }
.tform-info:hover .tform-pop, .tform-info:focus-visible .tform-pop, .tform-pop.visible { opacity: 1; visibility: visible; pointer-events: auto; }
.tform-pop div { margin-top: 6px; }
.tform-run .tform-pop { left: 0; transform: none; }
.tform-tabs .tform-pop { bottom: auto; top: calc(100% + 8px); }

/* Progress line under the run bar while a job runs */
.tform-progress { display: none; padding: 12px 20px; border-top: 1px solid var(--line); font-size: 13.5px; color: var(--text-2); }
.tform-progress.is-on { display: flex; align-items: center; gap: 10px; }
.tform-progress .spinner { width: 14px; height: 14px; margin: 0; border-width: 2px; }

/* ── History: saved runs under the form ── */
.tform-history { border: 1px solid var(--line); border-radius: var(--r-lg); background: var(--bg-raised); }
.tform-history-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 20px; border-bottom: 1px solid var(--line); }
.tform-history-head b { font-size: 14px; font-weight: 500; color: var(--text); }
.tform-history-head b span { margin-left: 6px; font-family: var(--mono); font-size: 12px; font-weight: 400; color: var(--text-3); }
.tform-hrow { display: grid; grid-template-columns: 44px minmax(0, 1fr) auto auto; align-items: center; gap: 16px; padding: 11px 20px; }
.tform-hrow + .tform-hrow { border-top: 1px solid var(--line); }
.tform-hrow:hover { background: rgba(var(--fg-rgb), 0.02); }
.tform-hscore { display: grid; place-items: center; height: 26px; border-radius: var(--r-sm); background: var(--bg-hover); font-family: var(--mono); font-size: 13px; color: var(--text); }
.tform-hscore.good { color: var(--good); background: rgba(var(--good-rgb), 0.1); }
.tform-hscore.warn { color: var(--warn); background: rgba(var(--warn-rgb), 0.1); }
.tform-hscore.bad { color: var(--bad); background: rgba(var(--bad-rgb), 0.1); }
.tform-hmain { min-width: 0; }
.tform-htitle { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; color: var(--text); }
.tform-hmeta { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 2px; font-size: 12.5px; color: var(--text-3); }
.tform-htime { font-size: 13px; color: var(--text-3); white-space: nowrap; }
.tform-hact { display: flex; align-items: center; gap: 2px; }
.tform-hact .tform-link.is-current { color: var(--accent); cursor: default; background: none; }
.tform-hrow.is-current { box-shadow: inset 2px 0 0 var(--accent); }

/* ── Responsive ── */
@media (max-width: 960px) {
    .tform-steps { grid-template-columns: 1fr; }
    .tform-step + .tform-step { border-left: 0; border-top: 1px solid var(--line); }
}
@media (max-width: 720px) {
    .tform-body, .tform-step { padding: 16px; }
    .tform-body.tform-steps { padding: 0; }
    .tform-opt > summary, .tform-opt-head { padding: 13px 16px; }
    .tform-opt-body { padding: 2px 16px 16px; }
    .tform-opt .tform-val { display: none; }
    .tform-tab { min-width: 0; }
    .tform-seg { display: flex; justify-self: stretch; }
    .tform-seg button { flex: 1 1 0; padding: 0 8px; font-size: 13px; }
    .tform-run, .tform-tabs { padding: 14px 16px; }
    .tform-run-right { width: 100%; margin-left: 0; flex-direction: column; align-items: stretch; gap: 10px; }
    .tform-cost { justify-content: center; text-align: center; }
    .tform-go { width: 100%; min-width: 0; }
    .tform-kbd { display: none; }
    .tform-history-head, .tform-hrow { padding-left: 16px; padding-right: 16px; }
    .tform-hrow { grid-template-columns: 40px minmax(0, 1fr) auto; gap: 12px; }
    .tform-htime { display: none; }
    .tform-pop { width: 260px; }
    .tform-row--stack { flex-direction: column; }
}

/* Form placed right under the hero title (v2 tool pages) */
.tool-hero .hero-split + .tform { margin-top: 56px; }
@media (max-width: 860px) { .tool-hero .hero-split + .tform { margin-top: 36px; } }

/* Tabs scroll sideways only on narrower screens; there the info popovers
   would be clipped by the scroller, so the dots are hidden (the one-line
   descriptions stay). */
@media (min-width: 1101px) { .tform-tabs { overflow: visible; } }
@media (max-width: 1100px) { .tform-tabs .tform-info { display: none; } }

/* Legacy-layout pages: the form is a direct child of <main>; keep the usual
   gap to whatever follows (results, or the footer when nothing does) */
.app-container main > .tform { margin-bottom: 96px; }
@media (max-width: 860px) { .app-container main > .tform { margin-bottom: 64px; } }

/* ── Aliases for state classes that existing page scripts toggle ── */
.tform-seg button.active, .tform-seg button.on { background: var(--bg-hover); color: var(--text); box-shadow: inset 0 0 0 1px var(--line-strong); }
.tform-tab.active { background: rgba(var(--accent-rgb), 0.06); color: var(--text); cursor: default; box-shadow: inset 0 0 0 1px rgba(var(--accent-rgb), 0.55); }
.tform-progress.visible { display: flex; align-items: center; gap: 10px; }

/* ── Popovers that would leave the screen ──
   Run bar: anchor to the bar itself, not the small info dot.
   Last tab: open leftwards. */
.tform-run { position: relative; }
.tform-run .tform-info { position: static; }
.tform-run .tform-pop { left: 16px; right: auto; bottom: calc(100% - 6px); transform: none; }
/* Tabs: the popover hangs from the whole tab, aligned to its left edge
   (right edge for the last tab), so it never sticks out of the card */
.tform-tabs .tform-info { position: static; }
.tform-tabs .tform-pop { left: 0; right: auto; top: calc(100% + 6px); transform: none; }
.tform-tab:last-child .tform-pop { left: auto; right: 0; }

@media (max-width: 720px) {
    .tform-run .tform-pop { right: 16px; width: auto; }
    /* Stacked rows: children keep their own height, a bit more air between them */
    .tform-row--stack { gap: 12px; }
    .tform-row--stack > * { flex: none; }
    /* Long segment labels wrap instead of being cut */
    .tform-seg button { white-space: normal; height: auto; min-height: 32px; padding: 5px 8px; line-height: 1.3; }
}

/* Light theme: unchecked choice cards sit on the white card surface
   (on the grey page tone they looked disabled) */
:root[data-theme="light"] .tform-choice { background: var(--bg-raised); }
