/* Teaspoons-to-grams interactive components. Uses the site's tokens from style.css when
   present (--accent, --text, ...) and falls back to the same values when used standalone. */
[data-tg-widget] {
    --tg-bg: var(--card, rgba(255, 255, 255, 0.03));
    --tg-border: var(--border, rgba(255, 255, 255, 0.08));
    --tg-border-h: var(--border-h, rgba(245, 158, 11, 0.35));
    --tg-text: var(--text, #fafafa);
    --tg-text2: var(--text2, rgba(250, 250, 250, 0.72));
    --tg-muted: rgba(250, 250, 250, 0.62);
    --tg-accent: var(--accent, #f59e0b);
    --tg-accent2: var(--accent2, #fbbf24);
    --tg-green: #34d399;
    --tg-blue: #38bdf8;
    --tg-pink: #f472b6;
    --tg-error: #fca5a5;
    --tg-focus: #fbbf24;
    --tg-radius: 14px;
    --tg-font: var(--font, 'Inter', system-ui, sans-serif);
    font-family: var(--tg-font);
    color: var(--tg-text);
    margin: 20px 0;
}
[data-tg-widget] *, [data-tg-widget] *::before, [data-tg-widget] *::after { box-sizing: border-box; }

.tg-card { background: var(--tg-bg); border: 1px solid var(--tg-border); border-radius: var(--tg-radius); padding: 20px; display: grid; gap: 14px; min-width: 0; }

/* Fields */
.tg-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; align-items: end; }
.tg-fields-3 { grid-template-columns: 1fr auto 1fr; }
.tg-field { display: grid; gap: 6px; min-width: 0; }
.tg-field label, .tg-seg legend, .tg-checks legend, .tg-result-label {
    font-size: .75rem; font-weight: 700; text-transform: uppercase; letter-spacing: .8px; color: var(--tg-text2);
}
.tg-input, .tg-select {
    width: 100%; min-width: 0; min-height: 44px; padding: 10px 14px;
    background: rgba(255, 255, 255, .05); border: 1px solid var(--tg-border); border-radius: 8px;
    color: var(--tg-text); font: 600 1rem var(--tg-font);
}
.tg-select { text-overflow: ellipsis; }
.tg-select option { background: #1a1a1c; color: var(--tg-text); }
.tg-input[aria-invalid="true"] { border-color: var(--tg-error); }
.tg-error { color: var(--tg-error); font-size: .88rem; margin: 0; }
.tg-hint, .tg-note, .tg-status { color: var(--tg-muted); font-size: .85rem; margin: 0; }
.tg-status:empty { display: none; }

/* Focus: always visible for keyboard users */
[data-tg-widget] :focus-visible { outline: 3px solid var(--tg-focus); outline-offset: 2px; }
.tg-seg-option input:focus-visible + span,
.tg-check-chip input:focus-visible + span { outline: 3px solid var(--tg-focus); outline-offset: 2px; }

/* Segmented radio group (native radios, visually restyled) */
.tg-seg, .tg-checks { border: 0; margin: 0; padding: 0; min-width: 0; }
.tg-seg legend, .tg-checks legend { padding: 0; margin-bottom: 6px; }
.tg-seg-options { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.tg-seg-option, .tg-check-chip { position: relative; display: block; }
.tg-seg-option input, .tg-check-chip input {
    position: absolute; opacity: 0; width: 1px; height: 1px; margin: 0; /* stays focusable */
}
.tg-seg-option span, .tg-check-chip span {
    display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
    min-height: 44px; padding: 6px 8px; border: 1px solid var(--tg-border); border-radius: 8px;
    background: rgba(255, 255, 255, .04); color: var(--tg-text2); font-weight: 600; font-size: .88rem; cursor: pointer;
}
.tg-seg-option small { font-size: .72rem; font-weight: 500; color: var(--tg-muted); }
.tg-seg-option input:checked + span, .tg-check-chip input:checked + span {
    background: rgba(245, 158, 11, .15); border-color: var(--tg-accent); color: var(--tg-accent2);
}
.tg-seg-option input:checked + span small { color: var(--tg-accent2); }
.tg-check { display: flex; align-items: center; gap: 10px; min-height: 44px; font-size: .9rem; color: var(--tg-text2); cursor: pointer; }
.tg-check input { width: 20px; height: 20px; accent-color: var(--tg-accent); flex-shrink: 0; }

/* Results */
.tg-result { border-top: 1px solid var(--tg-border); padding-top: 16px; display: grid; gap: 8px; }
.tg-result p { margin: 0; }
.tg-result-main { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.tg-big { font-size: 2rem; font-weight: 900; color: var(--tg-green); letter-spacing: -1px; line-height: 1; }
.tg-unit { color: var(--tg-text2); font-weight: 600; }
.tg-spoons { list-style: none; display: flex; flex-wrap: wrap; gap: 8px; margin: 0; padding: 0; }
.tg-spoon { padding: 8px 14px; border-radius: 999px; background: rgba(251, 191, 36, .12); border: 1px solid rgba(251, 191, 36, .35); color: var(--tg-accent2); font-weight: 700; }
.tg-spoon-none { background: none; color: var(--tg-text2); border-style: dashed; }

/* Formula breakdown */
.tg-equation { display: flex; flex-wrap: wrap; align-items: stretch; gap: 8px; }
.tg-op { align-self: center; font-size: 1.3rem; font-weight: 800; color: var(--tg-text2); }
.tg-term {
    flex: 1 1 110px; min-height: 56px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    padding: 10px 12px; border-radius: 10px; border: 1px solid var(--tg-border); background: rgba(255, 255, 255, .04);
    color: var(--tg-text); font-family: var(--tg-font); cursor: pointer;
}
.tg-term-value { display: block; font-size: 1.2rem; font-weight: 800; }
.tg-term-label { display: block; font-size: .72rem; text-transform: uppercase; letter-spacing: .8px; color: var(--tg-muted); }
.tg-c-amount .tg-term-value { color: var(--tg-accent2); }
.tg-c-volume .tg-term-value { color: var(--tg-blue); }
.tg-c-density .tg-term-value { color: var(--tg-pink); }
.tg-c-result .tg-term-value { color: var(--tg-green); }
.tg-term[aria-pressed="true"] { border-color: var(--tg-accent); background: rgba(245, 158, 11, .1); }
.tg-simplified { margin: 0; color: var(--tg-text2); }
.tg-simplified strong { color: var(--tg-green); }
.tg-explain { margin: 0; padding: 12px 14px; border-left: 3px solid var(--tg-accent); background: rgba(245, 158, 11, .06); border-radius: 0 8px 8px 0; color: var(--tg-text2); }
.tg-explain:empty { display: none; }

/* Comparison */
.tg-presets { display: flex; flex-wrap: wrap; gap: 8px; }
.tg-chip {
    min-height: 44px; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--tg-border);
    background: rgba(255, 255, 255, .04); color: var(--tg-text2); font: 600 .85rem var(--tg-font); cursor: pointer;
}
.tg-chip:hover, .tg-swap:hover, .tg-term:hover { border-color: var(--tg-border-h); color: var(--tg-text); }
.tg-swap {
    width: 44px; height: 44px; border-radius: 10px; border: 1px solid var(--tg-border); background: rgba(255, 255, 255, .04);
    color: var(--tg-accent); font-size: 1.2rem; cursor: pointer;
}
.tg-balance svg { display: block; width: 100%; max-width: 300px; height: auto; margin: 0 auto; overflow: visible; }
.tg-balance .tg-stand, .tg-balance .tg-base { fill: rgba(255, 255, 255, .12); }
.tg-balance .tg-beam rect { fill: var(--tg-accent); }
.tg-balance .tg-beam circle { fill: var(--tg-accent2); }
.tg-balance line { stroke: rgba(255, 255, 255, .35); stroke-width: 1.5; }
.tg-balance .tg-pan-a path { fill: rgba(251, 191, 36, .35); stroke: var(--tg-accent2); }
.tg-balance .tg-pan-b path { fill: rgba(52, 211, 153, .3); stroke: var(--tg-green); }
.tg-balance text { fill: var(--tg-text); font: 700 15px var(--tg-font); }
.tg-beam, .tg-pan-a, .tg-pan-b { transform-box: view-box; transition: transform .6s cubic-bezier(.4, 0, .2, 1); }
.tg-beam { transform-origin: 160px 66px; }
.tg-bars { display: grid; gap: 8px; }
.tg-bar-row { display: grid; gap: 4px; }
.tg-bar-name { font-size: .85rem; color: var(--tg-text2); font-weight: 600; }
.tg-bar { display: block; height: 10px; border-radius: 5px; background: rgba(255, 255, 255, .06); overflow: hidden; }
.tg-bar-fill { display: block; height: 100%; border-radius: 5px; transition: width .6s cubic-bezier(.4, 0, .2, 1); }
.tg-fill-a { background: var(--tg-accent2); }
.tg-fill-b { background: var(--tg-green); }
.tg-compare-text { margin: 0; font-weight: 600; }

/* Conversion chart */
.tg-chart-controls { display: grid; gap: 14px; margin-bottom: 14px; }
.tg-check-options { display: flex; flex-wrap: wrap; gap: 6px; }
.tg-check-chip span { min-height: 44px; padding: 6px 12px; flex-direction: row; font-size: .82rem; }
.tg-table-wrap { overflow-x: auto; border: 1px solid var(--tg-border); border-radius: var(--tg-radius); background: var(--tg-bg); }
.tg-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: .9rem; }
.tg-table caption { text-align: left; padding: 14px 16px 6px; color: var(--tg-muted); font-size: .85rem; }
/* Once enhanced, the caption is read by screen readers only and .tg-table-note shows the text (a caption is as wide as the table). */
.tg-enhanced .tg-table caption { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.tg-table-note { margin: 0; color: var(--tg-muted); font-size: .85rem; }
.tg-table th, .tg-table td { padding: 9px 10px; border-bottom: 1px solid var(--tg-border); white-space: nowrap; }
.tg-table thead th { font-size: .72rem; text-transform: uppercase; letter-spacing: .6px; color: var(--tg-muted); text-align: right; }
/* Ingredient-form names are long ("Double-acting baking powder"): headers wrap so the values keep one line and the table fits */
.tg-table thead th { white-space: normal; vertical-align: bottom; min-width: 4.5em; }
.tg-table thead th:first-child, .tg-table tbody th { text-align: left; }
.tg-table tbody th { color: var(--tg-accent2); font-weight: 700; position: sticky; left: 0; background: #121214; }
.tg-table thead th:first-child { position: sticky; left: 0; background: #121214; }
.tg-table td { text-align: right; color: var(--tg-green); font-weight: 700; }
.tg-table tbody tr:last-child > * { border-bottom: none; }

html[dir="rtl"] .tg-table caption { text-align: right; }
html[dir="rtl"] .tg-table thead th { text-align: left; }
html[dir="rtl"] .tg-table thead th:first-child,
html[dir="rtl"] .tg-table tbody th { text-align: right; left: auto; right: 0; }
html[dir="rtl"] .tg-table td { text-align: left; direction: ltr; unicode-bidi: isolate; }
html[dir="rtl"] .tg-equation { direction: ltr; }
html[dir="rtl"] .tg-equation .tg-term { direction: rtl; }
html[dir="rtl"] .tg-result-main,
html[dir="rtl"] .tg-spoons { direction: rtl; }

@media (max-width: 600px) {
    .tg-card { padding: 16px; }
    .tg-fields { grid-template-columns: 1fr; }
    .tg-fields-3 { grid-template-columns: 1fr auto; }
    .tg-fields-3 .tg-field:last-child { grid-column: 1 / -1; }
    .tg-swap { align-self: end; }
    .tg-big { font-size: 2rem; }
    .tg-term { flex: 1 1 calc(50% - 30px); min-height: 56px; }
    .tg-table { font-size: .82rem; }
    .tg-table th, .tg-table td { padding: 9px 8px; }
}

@media (prefers-reduced-motion: reduce) {
    .tg-beam, .tg-pan-a, .tg-pan-b, .tg-bar-fill { transition: none; }
}
