/* Crear y editar campos: popover compacto de dos pasos (selector de tipos y formulario) y editor de fórmulas
   (pestañas, autocompletado bajo el área de texto, tarjeta de ejemplos y editor ampliado). Ver `ui/field_form.rs`. */

/* ------------------------------------------------------------------------------------------------ Popover */

.popover.ff-pop {
  position: fixed;
  z-index: 55;
  width: min(416px, calc(100vw - 16px));
  max-height: none;
  padding: 0;
  overflow: visible;
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  --ff-tip: block;
}
.popover.ff-pop.pick { width: min(400px, calc(100vw - 16px)); }

/* Paso 1: buscador y tipos por secciones. */
.ff-pick { display: grid; gap: 6px; padding: 10px 10px 8px; }
.ff-search { position: relative; display: flex; align-items: center; color: var(--text-3); }
.ff-search .icon { position: absolute; left: 12px; pointer-events: none; }
.ff-search .input { width: 100%; height: 38px; padding-left: 36px; }
.ff-pick-list { max-height: min(430px, calc(100dvh - 150px)); overflow: auto; padding-bottom: 2px; font-weight: var(--weight-regular); }
.ff-group-title { padding: 10px 8px 4px; color: var(--text-3); font-size: 11px; font-weight: var(--weight-strong); text-transform: uppercase; letter-spacing: 0.06em; }
.ff-group-title:first-child { padding-top: 4px; }
.ff-type-row { display: flex; align-items: center; gap: 10px; width: 100%; height: 34px; padding: 0 8px; border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text); font: inherit; text-align: left; cursor: pointer; }
.ff-type-row .icon { flex: none; color: var(--text-3); }
.ff-type-row.on { background: var(--surface-3); }
.ff-type-row.current { color: var(--accent-text); }
.ff-none { margin: 0; padding: var(--space-4) 8px; }

/* Paso 2: nombre, tipo, ayuda, opciones y pie. */
.ff-form { display: grid; gap: 10px; padding: 14px 16px 12px; font-weight: var(--weight-regular); }
.ff-form .input:not(.short) { width: 100%; }
.ff-typebtn { display: flex; align-items: center; gap: 10px; width: 100%; height: 36px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: var(--radius-md); background: var(--surface); color: var(--text); font: inherit; text-align: left; cursor: pointer; }
.ff-typebtn:hover { background: var(--surface-2); }
.ff-typebtn:focus-visible { outline: none; border-color: var(--accent); box-shadow: var(--focus); }
.ff-typebtn .icon { flex: none; color: var(--text-3); }
.ff-typebtn .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ff-help { margin: 0; color: var(--text-2); font-size: var(--text-sm); line-height: 1.45; }
.ff-form .field-label { font-weight: var(--weight-medium); }
.ff-foot { display: flex; align-items: center; gap: var(--space-2); padding-top: 6px; }
.ff-foot .grow { flex: 1; }
.ff-link { display: inline-flex; align-items: center; gap: 6px; padding: 4px 2px; border: 0; background: transparent; color: var(--text-2); font: inherit; font-size: var(--text-sm); cursor: pointer; }
.ff-link:hover { color: var(--text); }
.ff-link:focus-visible { outline: none; box-shadow: var(--focus); border-radius: var(--radius-xs); }
.ff-link.add { justify-self: start; }

/* Opciones de una selección. */
.ff-choices { display: grid; gap: 8px; }
.ff-section { color: var(--text); font-size: var(--text-md); font-weight: var(--weight-strong); }
.ff-choices-bar { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); }
.ff-toggle { display: inline-flex; align-items: center; gap: 8px; color: var(--text-2); font-size: var(--text-sm); cursor: pointer; }
.ff-choice-list { justify-items: stretch; max-height: 210px; overflow: auto; padding: 1px; }
.ff-choice-list .choice-row .input { flex: 1; min-width: 0; }
.ff-empty { margin: 0; padding: 8px 2px; text-align: center; justify-self: center; }

/* ------------------------------------------------------------------------------------------------ Fórmula */

.fx-compact { position: relative; display: grid; gap: 8px; }
.ff-tabs { display: flex; gap: 18px; border-bottom: 1px solid var(--border); }
.ff-tab { height: 32px; margin-bottom: -1px; padding: 0 2px; border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--text-3); font: inherit; font-weight: var(--weight-medium); cursor: pointer; }
.ff-tab:hover { color: var(--text); }
.ff-tab.on { border-bottom-color: var(--text); color: var(--text); }
.fx-compact .fx-box .fx-input { min-height: 80px; max-height: 80px; padding-right: 38px; resize: none; }
.fx-compact .fx-format-title { display: none; }
.fx-compact .fx-format { padding-top: 2px; }
.fx-compact .fx-status { padding: 2px 2px; font-size: var(--text-xs); }
.fx-compact .fx-status.hint { color: var(--text-3); }
.fx-compact .fx-preview { min-height: 30px; font-size: var(--text-sm); }
.fx-opened { margin: 0; padding: 16px 12px; border: 1px dashed var(--border-strong); border-radius: var(--radius-md); color: var(--text-2); font-size: var(--text-sm); text-align: center; }

/* Botón de ampliar, en la esquina del área de texto. */
.fx-expand { position: absolute; right: 6px; bottom: 6px; z-index: 1; display: grid; place-items: center; width: 24px; height: 24px; border: 0; border-radius: var(--radius-xs); background: var(--surface); color: var(--text-3); cursor: pointer; }
.fx-expand:hover { background: var(--surface-3); color: var(--text); }

/* Autocompletado: justo debajo del área de texto (nombre en verde y monoespaciado; a la derecha, «función» o «campo»). */
.fx-complete { display: grid; gap: 2px; }
.fx-complete-title { padding: 2px 4px 0; color: var(--text-3); font-size: 10.5px; font-weight: var(--weight-medium); text-transform: uppercase; letter-spacing: 0.05em; }
.fx-options { display: grid; gap: 1px; max-height: 188px; overflow: auto; }
.fx-option { display: flex; align-items: center; gap: var(--space-2); min-width: 0; height: 28px; padding: 0 8px; border-radius: var(--radius-sm); color: var(--text); font-weight: var(--weight-regular); cursor: pointer; }
.fx-option.on { background: var(--surface-3); }
.fx-option-name { flex: none; color: var(--success); font-family: var(--font-mono); font-size: 12px; }
.fx-option-alias { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-3); font-family: var(--font-mono); font-size: 11.5px; }
.fx-option-tag { flex: none; margin-left: auto; color: var(--text-3); font-size: 11.5px; }
[data-theme="dark"] .fx-option-name { color: #6ee7a0; }

/* Tarjeta de ejemplos (o de la ayuda de la función): a la izquierda del popover, a la altura del área de texto. */
.fx-tip { position: absolute; top: 41px; right: calc(100% + 24px); z-index: 1; display: var(--ff-tip, block); width: 240px; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface); box-shadow: var(--shadow-md); color: var(--text-2); font-size: 12px; font-weight: var(--weight-regular); }
.fx-tip > * + * { margin-top: 6px; }
.fx-tip p { margin: 0; line-height: 1.45; }
.fx-tip-title { color: var(--text-2); font-size: 11px; font-weight: var(--weight-strong); }
.fx-tip code { display: block; color: var(--success); font-family: var(--font-mono); font-size: 11.5px; white-space: pre-wrap; overflow-wrap: anywhere; }
[data-theme="dark"] .fx-tip code { color: #6ee7a0; }

/* Editor ampliado. */
.overlay:has(> .fx-modal) { place-items: center; padding: 16px; }
.dialog.fx-modal { width: min(960px, 100%); height: min(680px, calc(100dvh - 32px)); max-height: none; }
.fx-modal-body { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) minmax(280px, 340px); gap: var(--space-4); overflow: hidden; font-weight: var(--weight-regular); }
.fx-main { display: grid; align-content: start; gap: 8px; min-height: 0; overflow: auto; }
.fx-box.big .fx-input { min-height: min(380px, 48dvh); max-height: none; resize: none; }
.fx-side { display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.fx-side-title { color: var(--text-3); font-size: 11px; font-weight: var(--weight-strong); text-transform: uppercase; letter-spacing: 0.06em; }
.fx-modal .fx-ref { flex: 1; height: auto; grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) minmax(0, 1fr); }
.fx-modal .fx-list { border-right: 0; border-bottom: 1px solid var(--border); }

@media (max-width: 760px) {
  .fx-modal-body { grid-template-columns: 1fr; overflow: auto; }
  .fx-main { overflow: visible; }
  .fx-box.big .fx-input { min-height: 180px; }
  .fx-side { min-height: auto; }
  .fx-modal .fx-ref { flex: none; height: 380px; }
}

/* Con poca altura o ancho no cabe la tarjeta de ejemplos a la izquierda; y el popover se recorta con su propio scroll. */
@media (max-width: 900px), (max-height: 640px) {
  .fx-tip { display: none; }
}
@media (max-height: 640px) {
  .popover.ff-pop { max-height: calc(100dvh - 16px); overflow: auto; }
}

/* ------------------------------------------------------------------------------------------------ Móvil: hoja inferior */

@media (max-width: 600px) {
  .popover.ff-pop, .popover.ff-pop.pick {
    left: 0 !important;
    right: 0;
    top: auto !important;
    bottom: 0;
    width: 100%;
    max-height: 90dvh;
    overflow: auto;
    border-radius: 14px 14px 0 0;
    animation: sheet-in var(--fast) var(--ease);
  }
  .backdrop:has(+ .ff-pop) { background: rgb(18 17 15 / 32%); }
  .ff-pick-list { max-height: 56dvh; }
  .ff-form { padding-bottom: max(12px, env(safe-area-inset-bottom)); }
  .dialog.fx-modal { height: calc(100dvh - 32px); }
}
@keyframes sheet-in { from { transform: translateY(24px); opacity: 0; } }
