<script>
(function () {
  // === SOZLANADIGAN O'ZGARUVCHILAR ===
  const inputName = 'limitmatn';        // Имя переменной поля ввода
  const symbolsMax = 150;               // Максимум символов
  const counterColor = '#0062ff';       // Цвет текста счётчика
  const counterFontSize = '12px';       // Размер шрифта счётчика

  // === QOLGAN KOD ===
  function sniffFont(selector, fallback = "'Arial',sans-serif") {
    const probe = document.createElement('div');
    probe.className = selector.replace('.', '');
    probe.style.cssText = 'position:absolute;visibility:hidden;height:0;width:0;overflow:hidden;';
    document.body.appendChild(probe);
    const font = getComputedStyle(probe).fontFamily || fallback;
    probe.remove();
    return font;
  }

  const fontFamily = sniffFont('t-title', "'Arial',sans-serif");

  function insertCSS() {
    const style = document.createElement('style');
    style.textContent = `
      [name="${inputName}"] { resize: unset !important; }
      .t-input-group_ta div { position: relative; }
      .t-input-group_ta .t-input-block::after {
        content: attr(data-xp9k) '/${symbolsMax}';
        position: absolute;
        right: 12px;
        bottom: 12px;
        font-family: ${fontFamily};
        color: ${counterColor};
        font-size: ${counterFontSize};
        padding: 0;
        border-radius: 0;
        background: none;
      }
    `;
    document.head.appendChild(style);
  }

  function updateCounter(el) {
    if (!el || el.name !== inputName) return;
    if (el.value.length > symbolsMax) {
      el.value = el.value.slice(0, symbolsMax);
    }
    el.parentElement?.setAttribute('data-xp9k', el.value.length);
  }

  function t_ready(cb) {
    if (document.readyState !== "loading") {
      cb();
    } else {
      document.addEventListener("DOMContentLoaded", cb);
    }
  }

  t_ready(function() {
    insertCSS();

    const interval = setInterval(function() {
      const inputs = document.querySelectorAll(`[name="${inputName}"]`);
      if (inputs.length > 0) {
        clearInterval(interval);
        inputs.forEach(updateCounter);
        document.addEventListener('input', e => updateCounter(e.target));
      }
    }, 50);

    setTimeout(function() {
      clearInterval(interval);
    }, 2000);
  });
})();
</script>